home *** CD-ROM | disk | FTP | other *** search
/ PC Basics 53 / PC Basics Issue 53.iso / Software / Internet / Invboard.exe / PC Basics 53 / Invboard / upload / sources / lib / post_poll.php < prev    next >
Encoding:
PHP Script  |  2002-06-13  |  14.5 KB  |  425 lines

  1. <?php
  2.  
  3. /*
  4. +--------------------------------------------------------------------------
  5. |   IBFORUMS v1
  6. |   ========================================
  7. |   by Matthew Mecham and David Baxter
  8. |   (c) 2001,2002 IBForums
  9. |   http://www.ibforums.com
  10. |   ========================================
  11. |   Web: http://www.ibforums.com
  12. |   Email: phpboards@ibforums.com
  13. |   Licence Info: phpib-licence@ibforums.com
  14. +---------------------------------------------------------------------------
  15. |
  16. |   > New Post module
  17. |   > Module written by Matt Mecham
  18. |
  19. +--------------------------------------------------------------------------
  20. */
  21.  
  22.  
  23.  
  24.  
  25. class post_functions extends Post {
  26.  
  27.     var $nav = array();
  28.     var $title = "";
  29.     var $post  = array();
  30.     var $topic = array();
  31.     var $upload = array();
  32.     var $mod_topic = array();
  33.     var $poll_count = 0;
  34.     var $poll_choices = "";
  35.  
  36.     var $m_group = "";
  37.     
  38.     function post_functions($class) {
  39.     
  40.         global $ibforums, $std, $DB;
  41.         
  42.         // Lets do some tests to make sure that we are allowed to start a new topic
  43.         
  44.         if (! $ibforums->member['g_post_polls'])
  45.         {
  46.             $std->Error( array( LEVEL => 1, MSG => 'no_start_polls') );
  47.         }
  48.         
  49.         if ( ! $class->forum['allow_poll'] )
  50.         {
  51.             $std->Error( array( LEVEL => 1, MSG => 'no_start_polls') );
  52.         }
  53.             
  54.         $this->m_group = $ibforums->member['mgroup'];
  55.         
  56.         if ($class->forum['start_perms'] != '*')
  57.         {
  58.             if (! preg_match( "/(^|,)$this->m_group(,|$)/", $class->forum['start_perms'] ) )
  59.             {
  60.                 $std->Error( array( LEVEL => 1, MSG => 'no_start_polls') );
  61.             }
  62.         }
  63.  
  64.     }
  65.     
  66.     function process($class) {
  67.     
  68.         global $ibforums, $std, $DB, $print;
  69.         
  70.         //-------------------------------------------------
  71.         // Parse the post, and check for any errors.
  72.         //-------------------------------------------------
  73.         
  74.         $this->post   = $class->compile_post();
  75.         
  76.         //-------------------------------------------------
  77.         // check to make sure we have a valid topic title
  78.         //-------------------------------------------------
  79.         
  80.         if ( (strlen($ibforums->input['TopicTitle']) < 2) or (!$ibforums->input['TopicTitle'])  ) {
  81.             $class->obj['post_errors'] = 'no_topic_title';
  82.         }
  83.         
  84.         if ( strlen($ibforums->input['TopicTitle']) > 64 ) {
  85.             $class->obj['post_errors'] = 'topic_title_long';
  86.         }
  87.         
  88.         //-------------------------------------------------
  89.         // check to make sure we have a correct # of choices
  90.         //-------------------------------------------------
  91.         
  92.         $this->poll_choices = $ibforums->input['PollAnswers'];
  93.         
  94.         $this->poll_choices = preg_replace( "/<br><br>/" , ""                              , $this->poll_choices );
  95.         
  96.         $this->poll_choices = preg_replace( "/<br>/e"    , "\$this->regex_count_choices()" , $this->poll_choices );
  97.         
  98.         if ($this->poll_count > 10) {
  99.             $class->obj['post_errors'] = 'poll_to_many';
  100.         }
  101.         
  102.         if ($this->poll_count < 1) {
  103.             $class->obj['post_errors'] = 'poll_not_enough';
  104.         }
  105.  
  106.         //-------------------------------------------------
  107.         // If we don't have any errors yet, parse the upload
  108.         //-------------------------------------------------
  109.         
  110.         if ($class->obj['post_errors'] == "")
  111.         {
  112.             $this->upload = $class->process_upload();
  113.         }
  114.         
  115.         
  116.         if ( ($class->obj['post_errors'] != "") or ($class->obj['preview_post'] != "") ) {
  117.             // Show the form again
  118.             $this->show_form($class);
  119.         } else {
  120.             $this->add_new_poll($class);
  121.         }
  122.     }
  123.     
  124.     
  125.     
  126.     
  127.     
  128.     function add_new_poll($class) {
  129.         
  130.         global $ibforums, $std, $DB, $print;
  131.         
  132.         //-------------------------------------------------
  133.         // Sort out the poll stuff
  134.         // This is somewhat contrived, but it has to be
  135.         // compatible with the current perl version.
  136.         //-------------------------------------------------
  137.         
  138.         $poll_array  = array();
  139.         $count       = 0;
  140.         
  141.         $polls       = explode( "<br>", $this->poll_choices );
  142.         
  143.         foreach ($polls as $polling)
  144.         {
  145.             if ( $polling == "" )
  146.             {
  147.                 continue;
  148.             }
  149.             $poll_array[] = array( $count , $polling, 0 );
  150.             $count++;
  151.         }
  152.         
  153.         //-------------------------------------------------
  154.         // Fix up the topic title
  155.         //-------------------------------------------------
  156.         
  157.         if ($ibforums->vars['etfilter_punct'])
  158.         {
  159.             $ibforums->input['TopicTitle']    = preg_replace( "/\?{1,}/"      , "?"    , $ibforums->input['TopicTitle'] );        
  160.             $ibforums->input['TopicTitle']    = preg_replace( "/(!){1,}/", "!" , $ibforums->input['TopicTitle'] );
  161.         }
  162.         
  163.         if ($ibforums->vars['etfilter_shout'])
  164.         {
  165.             $ibforums->input['TopicTitle'] = ucwords($ibforums->input['TopicTitle']);
  166.         }
  167.         
  168.         $ibforums->input['TopicTitle'] = $class->parser->bad_words( $ibforums->input['TopicTitle'] );
  169.         $ibforums->input['TopicDesc']  = $class->parser->bad_words( $ibforums->input['TopicDesc']  );
  170.         
  171.         //-------------------------------------------------
  172.         // Build the master array
  173.         //-------------------------------------------------
  174.         
  175.         $this->topic = array(
  176.                               'title'            => $ibforums->input['TopicTitle'],
  177.                               'description'      => $ibforums->input['TopicDesc'] ,
  178.                               'state'            => 'open',
  179.                               'posts'            => 0,
  180.                               'starter_id'       => $ibforums->member['id'],
  181.                               'starter_name'     => $ibforums->member['id'] ?  $ibforums->member['name'] : $ibforums->input['UserName'],
  182.                               'start_date'       => time(),
  183.                               'last_poster_id'   => $ibforums->member['id'],
  184.                               'last_poster_name' => $ibforums->member['id'] ?  $ibforums->member['name'] : $ibforums->input['UserName'],
  185.                               'last_post'        => time(),
  186.                               'icon_id'          => $ibforums->input['iconid'],
  187.                               'author_mode'      => $ibforums->member['id'] ? 1 : 0,
  188.                               'poll_state'       => $ibforums->input['allow_disc'] == 0 ? 'open' : 'closed',
  189.                               'last_vote'        => 0,
  190.                               'views'            => 0,
  191.                               'forum_id'         => $class->forum['id'],
  192.                               'approved'         => $class->obj['moderate'] ? 0 : 1,
  193.                               'pinned'           => 0,
  194.                              );
  195.         
  196.         
  197.         //-------------------------------------------------
  198.         // Insert the topic into the database to get the
  199.         // last inserted value of the auto_increment field
  200.         // follow suit with the post
  201.         //-------------------------------------------------
  202.         
  203.         $db_string = $DB->compile_db_insert_string( $this->topic );
  204.         
  205.         $DB->query("INSERT INTO ibf_topics (" .$db_string['FIELD_NAMES']. ") VALUES (". $db_string['FIELD_VALUES'] .")");
  206.         $this->post['topic_id']  = $DB->get_insert_id();
  207.         $this->topic['tid']      = $this->post['topic_id'];
  208.         /*---------------------------------------------------*/
  209.         
  210.         // Update the post info with the upload array info
  211.         
  212.         $this->post['attach_id']   = $this->upload['attach_id'];
  213.         $this->post['attach_type'] = $this->upload['attach_type'];
  214.         $this->post['attach_hits'] = $this->upload['attach_hits'];
  215.         $this->post['new_topic']   = 1;
  216.         
  217.         $db_string = $DB->compile_db_insert_string( $this->post );
  218.         
  219.         $DB->query("INSERT INTO ibf_posts (" .$db_string['FIELD_NAMES']. ") VALUES (". $db_string['FIELD_VALUES'] .")");
  220.         $this->post['pid'] = $DB->get_insert_id();
  221.         
  222.         //-------------------------------------------------
  223.         // Add the poll to the forum_polls table
  224.         // if we are moderating this post
  225.         //-------------------------------------------------
  226.         
  227.         $db_string = $std->compile_db_string(
  228.                                               array (
  229.                                                           'tid'          => $this->topic['tid'],
  230.                                                           'forum_id'     => $class->forum['id'],
  231.                                                           'start_date'   => time(),
  232.                                                           'choices'      => addslashes(serialize($poll_array)),
  233.                                                           'starter_id'   => $ibforums->member['id'],
  234.                                                           'votes'        => 0,
  235.                                                       )
  236.                                               );
  237.                                               
  238.         $DB->query("INSERT INTO ibf_polls (" .$db_string['FIELD_NAMES']. ") VALUES (". $db_string['FIELD_VALUES'] .")");
  239.         
  240.         
  241.                                     
  242.         
  243.         if ($class->obj['moderate'])
  244.         {
  245.             
  246.             // Redirect them with a message telling them the post has to be previewed first
  247.             $print->redirect_screen( $ibforums->lang['moderate_topic'], "act=SF&f={$class->forum['id']}" );
  248.         }
  249.         
  250.         //-------------------------------------------------
  251.         // If we are still here, lets update the
  252.         // board/forum stats
  253.         //-------------------------------------------------
  254.         
  255.         $class->forum['last_title']       = $this->topic['title'];
  256.         $class->forum['last_id']          = $this->topic['tid'];
  257.         $class->forum['last_post']        = time();
  258.         $class->forum['last_poster_name'] = $ibforums->member['id'] ?  $ibforums->member['name'] : $ibforums->input['UserName'];
  259.         $class->forum['last_poster_id']   = $ibforums->member['id'];
  260.         $class->forum['topics']++;
  261.         
  262.         // Update the database
  263.         
  264.         $DB->query("UPDATE ibf_forums    SET last_title='"      .$class->forum['last_title']       ."', ".
  265.                                             "last_id='"         .$class->forum['last_id']          ."', ".
  266.                                             "last_post='"       .$class->forum['last_post']        ."', ".
  267.                                             "last_poster_name='".$class->forum['last_poster_name'] ."', ".
  268.                                             "last_poster_id='"  .$class->forum['last_poster_id']   ."', ".
  269.                                             "topics='"          .$class->forum['topics']           ."' ".
  270.                                             "WHERE id='"        .$class->forum['id']               ."'");
  271.         
  272.         // At this point in time, we'll scan the database for the correct number
  273.         // of topics - if this proves to database intensive, we'll simply increment
  274.         // the value stored in the ibf_stats database.
  275.         
  276.         $DB->query("SELECT COUNT(tid) as topic_cnt FROM ibf_topics");
  277.         $stats = $DB->fetch_row();
  278.         
  279.         $DB->query("UPDATE ibf_stats SET TOTAL_TOPICS='".$stats['topic_cnt']."'");
  280.         
  281.         //-------------------------------------------------
  282.         // If we are a member, lets update thier last post
  283.         // date and increment their post count.
  284.         //-------------------------------------------------
  285.         
  286.         $pcount = "";
  287.         
  288.         if ($ibforums->member['id'])
  289.         {
  290.             if ($class->forum['inc_postcount'])
  291.             {
  292.                 // Increment the users post count
  293.                 
  294.                 $pcount = "posts=posts+1, ";
  295.                 
  296.             }
  297.             
  298.             $ibforums->member['last_post'] = time();
  299.             
  300.             $DB->query("UPDATE ibf_members SET ".$pcount.
  301.                                               "last_post='"    .$ibforums->member['last_post']   ."'".
  302.                                               "WHERE id='"     .$ibforums->member['id']."'");
  303.         }
  304.         
  305.         //-------------------------------------------------
  306.         // Set a last post time cookie
  307.         //-------------------------------------------------
  308.         
  309.         $std->my_setcookie( "LPid", time() );
  310.         
  311.         //-------------------------------------------------
  312.         // Redirect them back to the topic
  313.         //-------------------------------------------------
  314.         
  315.         $std->boink_it($class->base_url."&act=ST&f={$class->forum['id']}&t={$this->topic['tid']}");
  316.         
  317.     }
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.     function show_form($class) {
  325.     
  326.         global $ibforums, $std, $DB, $print, $HTTP_POST_VARS;
  327.         
  328.         // Sort out the "raw" textarea input and make it safe incase
  329.         // we have a <textarea> tag in the raw post var.
  330.         
  331.         $raw_post    = isset($HTTP_POST_VARS['Post'])        ? $HTTP_POST_VARS['Post']       : "";
  332.         $topic_title = isset($HTTP_POST_VARS['TopicTitle'])  ? $HTTP_POST_VARS['TopicTitle'] : "";
  333.         $topic_desc  = isset($HTTP_POST_VARS['TopicDesc'])   ? $HTTP_POST_VARS['TopicDesc']  : "";
  334.         $poll        = isset($HTTP_POST_VARS['PollAnswers']) ? stripslashes($HTTP_POST_VARS['PollAnswers']) : "";
  335.         
  336.         if (isset($raw_post)) {
  337.             $raw_post = preg_replace( "/<textarea>/", "<textarea>", $raw_post );
  338.             $raw_post = str_replace( '$', "$", $raw_post );
  339.             $raw_post = str_replace( '<%', "<%"  , $raw_post );
  340.             $raw_post = stripslashes($raw_post);
  341.         }
  342.         
  343.         // Do we have any posting errors?
  344.         
  345.         if ($class->obj['post_errors']) {
  346.             $class->output .= $class->html->errors( $ibforums->lang[ $class->obj['post_errors'] ]);
  347.         }
  348.         
  349.         if ($class->obj['preview_post']) {
  350.             $class->output .= $class->html->preview( $class->parser->convert( array( 'TEXT' => $this->post['post'], 'CODE' => $class->forum['use_ibc'], 'SMILIES' => $ibforums->input['enableemo'], 'HTML' => $class->forum['use_html']) ) );
  351.         }
  352.         
  353.         $class->output .= $class->html_start_form( array( 1 => array( 'CODE', '11' ), 2 => array( 'f', $class->forum['id']) ) );
  354.         
  355.         //---------------------------------------
  356.         // START TABLE
  357.         //---------------------------------------
  358.         
  359.         $class->output .= $class->html->table_structure();
  360.         
  361.         //---------------------------------------
  362.         
  363.         $topic_title = $class->html->topictitle_fields( array( TITLE => $topic_title, DESC => $topic_desc ) );
  364.         
  365.         $start_table = $class->html->table_top( "{$ibforums->lang['top_txt_poll']} {$class->forum['name']}");
  366.         
  367.         $name_fields = $class->html_name_field();
  368.         
  369.         $post_box    = $class->html_post_body( $raw_post );
  370.         
  371.         $mod_options = $class->mod_options();
  372.         
  373.         $poll_box    = $class->html->poll_box($poll);
  374.         
  375.         $end_form    = $class->html->EndForm( $ibforums->lang['submit_poll'] );
  376.         
  377.         $post_icons  = $class->html_post_icons();
  378.         
  379.         if ($class->obj['can_upload'])
  380.         {
  381.             $upload_field = $class->html->Upload_field( $ibforums->member['g_attach_max'] * 1024 );
  382.         }
  383.         
  384.         //---------------------------------------
  385.         
  386.         $class->output = preg_replace( "/<!--START TABLE-->/" , "$start_table"  , $class->output );
  387.         $class->output = preg_replace( "/<!--NAME FIELDS-->/" , "$name_fields"  , $class->output );
  388.         $class->output = preg_replace( "/<!--POST BOX-->/"    , "$post_box"     , $class->output );
  389.         $class->output = preg_replace( "/<!--POST ICONS-->/"  , "$post_icons"   , $class->output );
  390.         $class->output = preg_replace( "/<!--UPLOAD FIELD-->/", "$upload_field" , $class->output );
  391.         //$class->output = preg_replace( "/<!--MOD OPTIONS-->/" , "$mod_options"  , $class->output );
  392.         $class->output = preg_replace( "/<!--END TABLE-->/"   , "$end_form"     , $class->output );
  393.         $class->output = preg_replace( "/<!--TOPIC TITLE-->/" , "$topic_title"  , $class->output );
  394.         $class->output = preg_replace( "/<!--POLL BOX-->/"    , "$poll_box"     , $class->output );
  395.         
  396.         //---------------------------------------
  397.         
  398.         
  399.         $class->html_add_smilie_box();
  400.         
  401.         $this->nav = array( "<a href='{$class->base_url}&act=SC&c={$class->forum['cat_id']}'>{$class->forum['cat_name']}</a>",
  402.                             "<a href='{$class->base_url}&act=SF&f={$class->forum['id']}'>{$class->forum['name']}</a>",
  403.                           );
  404.         $this->title = $ibforums->lang['posting_new_topic'];
  405.         
  406.         $print->add_output("$class->output");
  407.         $print->do_output( array( 'TITLE'    => $ibforums->vars['board_name']." -> ".$this->title,
  408.                                    'JS'       => 1,
  409.                                    'NAV'      => $this->nav,
  410.                               ) );
  411.         
  412.     }
  413.     
  414.     function regex_count_choices() {
  415.         
  416.         ++$this->poll_count;
  417.         
  418.         return "<br>";
  419.         
  420.     }
  421.     
  422.  
  423. }
  424.  
  425. ?>