Advertisement
Guest User

Untitled

a guest
Oct 25th, 2013
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 52.64 KB | None | 0 0
  1. class forums{
  2.     function forums() {
  3.         global $menuon;
  4.         $menuon = 'forums';
  5.         nav(url('forums',t('Forums')));
  6.         set_title(t('Forums'));
  7.         clear_as();
  8.     }
  9.  
  10.     function index() {
  11.         global $settings;
  12.        
  13.         // forum lists
  14.         $categories = array();
  15.         $q = "SELECT id,name from ".tb()."forums WHERE forum_type='category'  ORDER BY weight";
  16.         $res = sql_query($q);
  17.         $threads = $posts = 0;
  18.         c('<table width="100%" class="stories">');
  19.         c('<tr class="row1">
  20.                 <td width="51"></td>
  21.                 <td>'.t('Forum').'</td>
  22.                 <td width="60">'.t('Threads').'</td>
  23.                 <td width="60">'.t('Posts').'</td>
  24.                 <td width="250">'.t('Last post').'</td>
  25.                 </tr>');
  26.         while ($category = sql_fetch_array($res)) {
  27.             c('<tr class="table_line1"><td colspan="5">'.$category['name'].'</td></tr>');
  28.             $q = "SELECT * FROM ".tb()."forums WHERE parent_id={$category['id']} ORDER BY weight";
  29.             $res2 = sql_query($q, $conn);
  30.             WHILE ($forum = sql_fetch_array($res2)) {
  31.                 $fsettings = unserialize($forum['settings']);
  32.                 if (!$fsettings['hide']) {
  33.                     // online
  34.                     $timeline = time()-$settings['online']*60;
  35.                     $lastpost = ' - ';
  36.                     if (strlen($forum['image']) > 10)
  37.                         $forum_image = '<img src="'.$forum['image'].'" align="left" />';
  38.                     else
  39.                         $forum_image = '';
  40.                     if ($forum['lastposttopicid']) {
  41.                     $q2 = sql_query("SELECT * FROM ".tb()."forum_threads WHERE id={$forum['lastposttopicid']} ");
  42.                     $isresolved = sql_fetch_array($q2);
  43.                     if($isresolved['resolved']){
  44.             $resolved = '<font color="green">[RESOLVED]</font> ';
  45.             }else{
  46.             $resolved = '';
  47.             }
  48.                         $lastpost = url('forums/viewthread/'.$forum['lastposttopicid'],$resolved.h($forum['lastposttopic'])).'<br /><span class="sub">'.get_date($forum['lastpostcreated']).' by '.url('u/'.$forum['lastpostname'],$forum['lastpostname']).'</span>';
  49.                     }
  50.                     c('<tr class="row1">
  51.                         <td align="center"><img src="'.uhome().'/modules/forums/forum_icon.png" /></td>
  52.                         <td>'.$forum_image.'<strong>'.url('forums/listthreads/'.$forum['id'],h($forum['name'])).'</strong> <br /><span class="sub">'.$forum['description'].'</span></td>');
  53.                     if (!in_array(2,explode('|',$forum['read_roles'])) && !allow_access(explode('|',$forum['read_roles'])) && !forum_mod('',explode(',',$forum['moderators'])) ) {
  54.                         if ($forum['read_roles']) {
  55.                             $roles = array();
  56.                             $res4 = sql_query("select name from ".tb()."roles where id in(".str_replace('|',',',$forum['read_roles']).") order by id");
  57.                             while ($role = sql_fetch_array($res4)) {
  58.                                 $roles[] = h($role['name']);
  59.                             }
  60.                             $forum['need_roles'] = '<br /><span class="sub">'.t('Allowed Roles').': '.implode(', ',$roles).'</span>';
  61.                         }
  62.                         c('<td colspan="3"><i>'.t('You have no permission to view this forum').'</i>'.$forum['need_roles'].'</td>');
  63.                     }
  64.                     else {
  65.                         c('<td>'.$forum['threads'].'</td>
  66.                         <td>'.$forum['posts'].'</td>
  67.                         <td>'.$lastpost.'</td>
  68.                         </tr>');
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.         if (allow_access(3)) {
  74.             c('<tr class="row2"><td align="right"><img src="'.uhome().'/files/icons/recyle.png" /></td>
  75.             <td  colspan="4">'.url('forums/listthreads/0',t('Recycle Bin')).'</td></tr>');
  76.         }
  77.         c('</table>');
  78.     }
  79.  
  80.     function listthreads($fid) {
  81.         global $offset, $num_per_page, $page, $forum_id, $settings, $client, $ss;
  82.         $forum = valid_forum($fid);
  83.         if (!forum_mod('',explode(',',$forum['moderators'])) && !$ss['is_bot']) {
  84.             do_auth($forum['read_roles']);
  85.         }
  86.         $forum_id = $fid;
  87.         nav(url('forums/listthreads/'.$forum['id'],h($forum['name'])));
  88.         if ($fid > 0)
  89.             button('forums/newthread/'.$fid, t('New Thread'));
  90.         set_title(h($forum['name']));
  91.         c('<table class="stories">
  92.             <tr class="table_line1">
  93.             <td colspan="6">'.h($forum['name']).'</td>
  94.             </tr>
  95.             <tr class="table_line2">
  96.             <td colspan="2"></td>
  97.             <td>'.t('Title').'</td>
  98.             <td width="60">'.t('Views').'</td>
  99.             <td width="60">'.t('Replies').'</td>
  100.             <td width="160">'.t('Last post').'</td>
  101.             </tr>');
  102.    
  103.        
  104.        
  105.            
  106.         $res = sql_query("SELECT * FROM ".tb()."forum_threads where fid='$fid'  ORDER by thread_type desc,lastpostcreated DESC LIMIT $offset,$num_per_page ");
  107.         while ($row = sql_fetch_array($res)) {
  108.             $this->list_threads($row);
  109.         }
  110.         c('</table>');
  111.  
  112.         // pager
  113.         $pb       = new PageBar($forum['threads'], $num_per_page, $page);
  114.         $pb->paras = uhome().'/index.php?p=forums/listthreads/'.$fid;
  115.         $pagebar  = $pb->whole_num_bar();
  116.         c($pagebar);
  117.        
  118.         c('<br /><table class="stories"><tr class="table_line2"><td>'.t('Forum information').'</td></tr>
  119.         <tr class="row1"><td>'.t('Moderators').': ');
  120.         if (strlen($forum['moderators'])) {
  121.             $moderators = explode(',',$forum['moderators']);
  122.             foreach ($moderators as $moderator) {
  123.                 $mods[] = url('u/'.trim($moderator),$moderator);
  124.             }
  125.             c(implode(', ',$mods));
  126.         }
  127.         else {
  128.             c('<i>None</i>');
  129.         }
  130.         c('</td></tr>
  131.         </table>');
  132.     }
  133.  
  134.     function recentthreads($pid) {
  135.         if (!is_numeric($pid)) die();
  136.         $res = sql_query("select * from ".tb()."pages where id='$pid'");
  137.         $jcow_page = sql_fetch_array($res);
  138.         if (!$jcow_page['id']) die('wrong page id');
  139.         if ($jcow_page['uid'] == $client['id']) {
  140.             button($this->name.'/writestory/'.$jcow_page['id'],$this->write_story);
  141.         }
  142.         if ($jcow_page['type'] != 'u') {
  143.             die();
  144.         }
  145.         include_once('modules/u/u.php');
  146.         if (!$user = u::settabmenu($jcow_page['uri'],1,'u')) die('wrong uname');
  147.         $title = t("{1}'s {2}",$user['username'],$this->label_entry);
  148.         $nav[] = url($this->name.'/liststories/page_'.$user['username'],$user['username']);
  149.    
  150.         c('<table class="stories">
  151.             <tr class="table_line2">
  152.             <td colspan="2"></td>
  153.             <td>'.t('Title').'</td>
  154.             <td width="60">'.t('Views').'</td>
  155.             <td width="60">'.t('Replies').'</td>
  156.             <td width="160">'.t('Last post').'</td>
  157.             </tr>');
  158.            
  159.         $res = sql_query("SELECT * FROM ".tb()."forum_threads where userid='{$jcow_page['uid']}'  ORDER by thread_type desc,lastpostcreated DESC LIMIT 10");
  160.         while ($row = sql_fetch_array($res)) {
  161.             $this->list_threads($row);
  162.         }
  163.         c('</table>');
  164.     }
  165.    
  166.     function list_threads($row) {
  167.         global $num_per_page;
  168.         if (!$row['lastreply']) {
  169.                 $row['lastreply'] = $row['created'];
  170.                 $row['lastreplyuid'] = $row['uid'];
  171.                 $row['lastreplyuname'] = $row['username'];
  172.             }
  173.             if($row['resolved']){
  174.             $resolved = '<font color="green">[RESOLVED]</font>';
  175.             }else{
  176.             $resolved = '';
  177.             }
  178.             if ($row['got_poll']) $row['type'] = t('Poll').': ';
  179.             $icon = 'topic_standard.gif';
  180.             if ($row['posts'] >= 12) {
  181.                 $icon = 'topic_hot.gif';
  182.             }
  183.             if ($row['thread_lock']) {
  184.                 $icon = 'topic_lock.gif';
  185.             }
  186.             if ($row['thread_type'] == 1) {
  187.                 $icon = 'topic_sticky.gif';
  188.             }
  189.             if ($row['thread_type'] == 2) {
  190.                 $icon = 'topic_global.gif';
  191.             }
  192.             if ($row['posts'] > $num_per_page) {
  193.                 $pager = array();
  194.                 $thread_url = url('forums/viewthread/'.$row['id']);
  195.                 if (eregi('\?',$thread_url)) {
  196.                     $thread_url = $thread_url.'&';
  197.                 }
  198.                 else {
  199.                     $thread_url = $thread_url.'?';
  200.                 }
  201.                 $row['pager'] = ' <span class="sub"><img src="'.uhome().'/files/common_css/pager_icon.png" /> ';
  202.                 $pager[] = url($thread_url,'1');
  203.                 $pager[] = url($thread_url.'page=2','2');
  204.                 if ($row['posts'] > ($num_per_page*2)) {
  205.                     $num = ceil($row['posts']/$num_per_page);
  206.                     $pager[] = url($thread_url.'page='.$num,$num);
  207.                 }
  208.                 $row['pager'] .= implode(', ',$pager);
  209.                 $row['pager'] .= '</span>';
  210.             }
  211.             $attach = $row['got_attach'] ? ' <img src="'.uhome().'/files/icons/forums/topic_attach.gif" alt="got attachments" />' : '';
  212.             $uicon = $row['icon'] ? '<img src="'.uhome().'/files/icons/forums/icon'.$row['icon'].'.png" />' : ' ';
  213.             c('<tr class="row1">
  214.             <td width="20" align="center"><img src="'.uhome().'/files/icons/forums/'.$icon.'" /></td>
  215.             <td width="16" align="center">'.$uicon.'</td>
  216.             <td><span class="forum_title">'.$resolved.$row['type'].url('forums/viewthread/'.$row['id'],h($row['topic'])).$attach.$row['pager'].'</span><br /><span class="sub">by '.url('u/'.$row['username'],$row['username']).', '.get_date($row['created']).'</span></td>
  217.             <td align="center">'.$row['views'].'</td>
  218.             <td align="center">'.($row['posts']-1).'</td>
  219.             <td><span class="sub">'.get_date($row['lastpostcreated']).'</span><br /><span class="sub">by '.url('u/'.$row['lastpostusername'],$row['lastpostusername']).'</span></td>
  220.             </tr>');
  221.         }
  222.  
  223.     function viewthread($tid) {
  224.         global $page, $offset, $num_per_page, $client, $settings, $ss, $config;
  225.         $thread = valid_thread($tid);
  226.         if ($thread['fid'] == 0) {
  227.             if (!forum_mod()) {
  228.                 die('Sorry, This thread is in Recyle Bin');
  229.             }
  230.         }
  231.         $forum = valid_forum($thread['fid']);
  232.         if (!forum_mod('',explode(',',$forum['moderators']))  && !$ss['is_bot']) {
  233.             do_auth($forum['read_roles']);
  234.         }
  235.         nav(url('forums/listthreads/'.$forum['id'],h($forum['name'])));
  236.         nav(url('forums/viewthread/'.$thread['id'],h($thread['topic'])));
  237.         set_title(h($thread['topic']));
  238.         enreport();
  239.         if ($thread['got_poll']) {
  240.             $res = sql_query("select * from ".tb()."forum_polls where tid='{$thread['id']}'  limit 1");
  241.             $poll = sql_fetch_array($res);
  242.             c('<table class="stories"><tr class="table_line1"><td>'.h($poll['question']).'</td></tr></table>');
  243.             $opts = unserialize($poll['options']);
  244.             c('<table class="stories">');
  245.             c('<form method="post" action="'.url('forums/vote').'" />');
  246.             c('<tr class="table_line2"><td>'.t('Options').' ('.t('Maximum selection').': <strong>'.$poll['options_per_user'].'</strong>)</td><td width="50%">'.t('Votes').'</td></tr>');
  247.             $opt_type = 'radio';
  248.             if ($poll['options_per_user'] > 1) $opt_type = 'checkbox';
  249.             if (is_array($opts)) {
  250.                 foreach ($opts as $key=>$opt) {
  251.                     $length = ceil($opt['votes']*100/($poll['total']+1));
  252.                     $alpha = 0.4;
  253.                     $alpha2 = 40;
  254.                     if ($length >= 20) {
  255.                         $alpha = 0.6;
  256.                         $alpha2 = 60;
  257.                     }
  258.                     if ($length >= 30) {
  259.                         $alpha = 0.8;
  260.                         $alpha2 = 80;
  261.                     }
  262.                     if ($length > 40) {
  263.                         $alpha = 0.9;
  264.                         $alpha2 = 90;
  265.                     }
  266.                     if ($length > 50) {
  267.                         $alpha = 1;
  268.                         $alpha2 = 100;
  269.                     }
  270.                    
  271.                     c('<tr class="row1"><td><input type="'.$opt_type.'" value="'.$key.'" name="keys[]" />
  272.                      '.h($opt['option']).'</td><td><div class="votes" style="width:'.$length.'%;filter:alpha(opacity='.$alpha2.');opacity: '.$alpha.';-moz-opacity:'.$alpha.';">'.$opt['votes'].'</div></td></tr>');
  273.                 }
  274.             }
  275.             c('<tr class="row2"><td colspan="2">
  276.             <input type="hidden" name="poll_id" value="'.$poll['id'].'" />');
  277.             if ($poll['timeout'] > time()) {
  278.                 if ($client['id'] && eregi(','.$client['id']."(,|$)",$poll['voters']))
  279.                     c('<input type="submit" value="'.t('Voted').'" disabled />');
  280.                 else
  281.                     c('<input type="submit" value="'.t('Vote').'" />');
  282.             }
  283.             else {
  284.                 c('Closed in '.get_date($poll['timeout'],'date'));
  285.             }
  286.             c(' ('.t('{1} people voted',$poll['total']).')</td></tr>
  287.             </form></table><br />');
  288.         }
  289.         if($thread['resolved']){
  290.             $resolved = '<font color="green" style="background:white;">[RESOLVED]</font> ';
  291.             }else{
  292.             $resolved = '';
  293.             }
  294.         $res = sql_query("SELECT p.*,u.username,u.avatar,u.forum_posts,u.created as ucreated,u.lastlogin,u.signature FROM ".tb()."forum_posts as p left join ".tb()."accounts as u on u.id=p.uid where p.tid='$tid' ORDER by p.id LIMIT $offset,$num_per_page ");
  295.         c('<script src=\"modules/forums/ioquatix-jquery-syntax-36a1d65/examples/jquery-1.4.4.min.js\" type=\"text/javascript\"></script><script src=\"modules/forums/ioquatix-jquery-syntax-36a1d65/public/jquery.syntax.min.js\" type=\"text/javascript\"></script>
  296.                          
  297. <script type=\"text/javascript\">
  298.    // This function is executed when the page has finished loading.
  299.    jQuery(function($) {
  300.        // This function highlights (by default) pre and code tags which are annotated correctly.
  301.        $.syntax();
  302.         replace: true
  303.    });
  304.    
  305. </script><table class="stories"><tr class="table_line1"><td>'.$resolved.h($thread['topic']).'</td></tr></table>');
  306.         c('<table class="stories">');
  307.         while ($post = sql_fetch_array($res)) {
  308.             if ($post['is_first'] && $client['id']) {
  309.                 button('forums/newthread/'.$forum['id'], t('New Thread'));
  310.                 button('forums/newreply/'.$post['id'], t('New Reply'));
  311.             }
  312.             if (!$post['lastreply']) {
  313.                 $post['lastreply'] = $post['created'];
  314.                 $post['lastreplyuid'] = $post['uid'];
  315.                 $post['lastreplyuname'] = $post['username'];
  316.             }
  317.             $icon = 'topic_1.gif';
  318.             if ($post['lastlogin'] > (time()-300))
  319.                 $post['user_online'] = '<img src="'.uhome().'/files/icons/online.gif" />';
  320.             else
  321.                 $post['user_online'] = '<img src="'.uhome().'/files/icons/offline.gif" />';
  322.             if ($post['comments'] >= 5 or $post['views'] >= 100) {
  323.                 $icon = 'topic_2.gif';
  324.             }
  325.             if (strlen($post['title'])) $post['title'] = '<span class="forum_post_title">'.h($post['title']).'</span>';
  326.             c('<tr class="table_line2"><td colspan="2"><a name="num'.$post['id'].'"></a> <i>'.get_date($post['created']).':</i></td></tr>');
  327.             c('<tr class="row1">
  328.             <td valign="top" width="150" align="center">'.url('u/'.$post['username'].'', t($post['username'])).'<br />'.$post['user_online'].'<br />'.avatar($post).'<br /><span class="sub">'.t('Forum posts').': '.$post['forum_posts'].'<br />'.t('Joined').': '.get_date($post['ucreated'],'date')
  329.             .'<br />'.url('u/'.$post['username'],t('View Profile')).'<br />'.url('message/compose/u'.$post['uid'],t('Send Message')).'</span><br /><br /></td>
  330.             <td valign="top">'.$post['title'].nl2br(decode_bb(h($post['message']))) );
  331.             if ($post['got_attach']) {
  332.                 c('<fieldset><legend>'.t('Attachments').'</legend>');
  333.                 $res2 = sql_query("select * from ".tb()."forum_attachments where pid='{$post['id']}'");
  334.                 while ($attach = sql_fetch_array($res2)) {
  335.                     c('<p>');
  336.                     if (eregi("(gif|jpg|png)$",$attach['orginal_name'])) {
  337.                         c('<span class="sub">'.h($attach['des']).'</span>');
  338.                         c('<br /><img src="'.uhome().'/'.$attach['uri'].'" />');
  339.                     }
  340.                     else {
  341.                         c('<strong>'.h($attach['orginal_name']).'</strong> ('.get_filesize($attach['size']).', '.$attach['downloads'].' downloads)<br /><span class="sub">'.h($attach['des']).'</span><a href="'.url('forums/download/'.$attach['id']).'"> '.t('Download').'</a>');
  342.                     }
  343.                     c('</p>');
  344.                 }
  345.                 c('</fieldset>');
  346.             }
  347.  
  348.             c($this->post_operations($post));
  349.             if ($post['stream_id']) {
  350.                 c('
  351.                 <div style="width:100%;padding-left:60%;clear:both;">');
  352.                
  353.                 c('</div>');
  354.             }
  355.             c('</td>
  356.             </tr>');
  357.             if($post['signature']){
  358.             c('<tr class="row1">
  359.             <td colspan="2">
  360.             '.nl2br(decode_bb($post['signature'])).'
  361.             </td>
  362.             </tr>');
  363.             }
  364.         }
  365.         c('</table>');
  366.  
  367.         // pager
  368.         $pb       = new PageBar($thread['posts'], $num_per_page, $page);
  369.         $pb->paras = uhome().'/index.php?p=forums/viewthread/'.$tid;
  370.         $pagebar  = $pb->whole_num_bar();
  371.         c($pagebar);
  372.         $set_thread['id'] = $thread['id'];
  373.         $set_thread['views'] = $thread['views'] + 1;
  374.         sql_update($set_thread,tb().'forum_threads');
  375.         if ($thread['old_fid']) {
  376.             $res = sql_query("select name from ".tb()."forums where id='{$thread['old_fid']}' ");
  377.             $row = sql_fetch_array($res);
  378.             c('<div>'.t('This thread was moved from {1}',url('forums/listthreads/'.$thread['old_fid'],h($row['name']))).'</div>');
  379.         }
  380.         c('<table border="0" width="100%"><tr><td> ');
  381.         if (forum_mod($thread['fid'])) {
  382.             c('<form method="post" action="'.url('forums/mod_thread').'" />
  383.             '.t('Moderator Tools').':<select name="mod_type">
  384.             <option value="0">Select..</option>');
  385.             if ($thread['fid'] == 0) {
  386.                 if ($client['level'] == 9) {
  387.                     c('<option value="torecover">'.t('Recover thread').'</option>
  388.                     <option value="todelete">'.t('Delete thread').'</option>');
  389.                 }
  390.             }
  391.             else {
  392.                 if ($thread['thread_lock']) {
  393.                     c('<option value="tounlock">'.t('Unlock thread').'</option>');
  394.                 }
  395.                 else {
  396.                     c('<option value="tolock">'.t('Lock thread').'</option>');
  397.                 }
  398.                 c('<option value="tomove">'.t('Move thread to other Forums').'</option>
  399.                 <option value="torecyle">'.t('Move thread to Recycle Bin').'</option>');
  400.                 if ($client['level'] == 9) {
  401.                     c('<option value="todelete">'.t('Delete thread').'</option>');
  402.                 }
  403.                 if ($thread['thread_type'] > 0) {
  404.                     c('<option value="tostandard">'.t('Change to Standard').'</option>');
  405.                 }
  406.                 else {
  407.                     c('<option value="tosticky">'.t('Change to Sticky').'</option>
  408.                 <option value="toglobal">'.t('Change to Global').'</option>');
  409.                 }
  410.             }
  411.             c('</select>
  412.             <input type="hidden" name="tid" value="'.$thread['id'].'" />
  413.             <input type="submit" value="Go" />
  414.             </form>');
  415.         }
  416.         if ($client['id']) {
  417.             $subscribe = sql_query("select * from ".tb()."forum_subscribes where tid='{$thread['id']}' and uid='{$client['id']}'");
  418.             if (sql_counts($subscribe)) {
  419.                 $subscribe_url = url('forums/unsubscribe/'.$thread['id'],t('Un-Subscribe'));
  420.             }
  421.             else {
  422.                 $subscribe_url = '<img src="'.uhome().'/files/icons/add.png" />'.url('forums/subscribe/'.$thread['id'],t('Subscribe') );
  423.             }
  424.         }
  425.         else {
  426.             $subscribe_url = '';
  427.         }
  428.         c('</td><td align="right">'.$subscribe_url.'</td></tr></table>');
  429.         section_close();
  430.         if (!$thread['thread_lock'] && $client['id']) {
  431.             c('<form method="post" action="'.url('forums/newreplypost').'">');
  432.             c('<p>
  433.                             '.label(t('Title').' <i>('.t('Optional').')</i>').'
  434.                             <input type="text" name="title" size="45" />
  435.             </p>');
  436.             c(post_form(6,$post).'
  437.             <p>
  438.                             <input class="button" type="submit" value="'.t('Submit').'" />
  439.                             <input type="hidden" name="pid" value="'.$thread['pid'].'" />
  440.                             </p>
  441.             </form>');
  442.             section_close(t('Quick reply'));
  443.         }
  444.     }
  445.  
  446.     function subscribe($tid) {
  447.         global $client;
  448.         if (!$client['id']) die('please login');
  449.         $thread = valid_thread($tid);
  450.         if ($thread['fid'] == 0) {
  451.             if (!forum_mod()) {
  452.                 die('Sorry, This thread is in Recyle Bin');
  453.             }
  454.         }
  455.         $forum = valid_forum($thread['fid']);
  456.         if (!forum_mod('',explode(',',$forum['moderators']))  && !$ss['is_bot']) {
  457.             do_auth($forum['read_roles']);
  458.         }
  459.         $subscribe = sql_query("select * from ".tb()."forum_subscribes where tid='{$thread['id']}' and uid='{$client['id']}'");
  460.         if (!sql_counts($subscribe)) {
  461.             sql_insert(array('uid'=>$client['id'],'tid'=>$thread['id']),tb()."forum_subscribes");
  462.         }
  463.         redirect(url('forums/viewthread/'.$tid),1);
  464.     }
  465.  
  466.     function unsubscribe($tid) {
  467.         global $client;
  468.         if (!$client['id']) die('please login');
  469.         $thread = valid_thread($tid);
  470.         sql_query("delete from ".tb()."forum_subscribes where tid='{$thread['id']}' and uid='{$client['id']}'");
  471.         redirect(url('forums/viewthread/'.$tid),1);
  472.     }
  473.  
  474.    
  475.     function mod_thread() {
  476.         global $client;
  477.         need_login();
  478.         $thread = valid_thread($_POST['tid']);
  479.         if (!forum_mod($thread['fid'])) die('access denied');
  480.         $set_thread['id'] = $thread['id'];
  481.         switch($_POST['mod_type']) {
  482.             case 'tosticky':
  483.                 $set_thread['thread_type'] = 1;
  484.                 sql_update($set_thread,tb().'forum_threads');
  485.                 break;
  486.             case 'toglobal':
  487.                 $set_thread['thread_type'] = 2;
  488.                 sql_update($set_thread,tb().'forum_threads');
  489.                 break;
  490.             case 'tostandard':
  491.                 $set_thread['thread_type'] = 0;
  492.                 sql_update($set_thread,tb().'forum_threads');
  493.                 break;
  494.             case 'tolock':
  495.                 $set_thread['thread_lock'] = 1;
  496.                 sql_update($set_thread,tb().'forum_threads');
  497.                 redirect(url('forums/viewthread/'.$thread['id']),1);
  498.                 break;
  499.             case 'tomove':
  500.                 $res = sql_query("select name from ".tb()."forums where id='{$thread['fid']}' ");
  501.                 $row = sql_fetch_array($res);
  502.                 c('Move This thread: '.url('forums/viewthread/'.$thread['id'],h($thread['topic'])));
  503.                 c('<br />From '.url('forums/listthreads/'.$thread['fid'],$row['name']));
  504.                 c('<br />to:
  505.                 <form action="'.url('forums/movethread').'" method="post">
  506.                 <select name="new_fid">');
  507.                 $res = sql_query("select f.*,g.name as gname from ".tb()."forums as f left join ".tb()."forums as g on g.id=f.parent_id where f.forum_type='forum' order by f.parent_id");
  508.                 while ($row = sql_fetch_array($res)) {
  509.                     c('<option value="'.$row['id'].'">'.h($row['gname']).' - '.h($row['name']).'</option>');
  510.                 }
  511.                 c('</select>
  512.                 <input type="submit" value="Go" />
  513.                 <input type="hidden" name="tid" value="'.$thread['id'].'" />
  514.                 </form>');
  515.                 stop_here();
  516.                 break;
  517.             case 'torecover':
  518.                 $set_thread['fid'] = $thread['old_fid'];
  519.                 $set_thread['old_fid'] = 0;
  520.                 sql_update($set_thread,tb().'forum_threads');
  521.                 // update num
  522.                 sql_query("update ".tb()."forums set threads=threads+1,posts=posts+{$thread['posts']} where id='{$thread['old_fid']}' ");
  523.                 redirect(url('forums/viewthread/'.$thread['id']),1);
  524.                 break;
  525.             case 'tounlock':
  526.                 $set_thread['thread_lock'] = 0;
  527.                 sql_update($set_thread,tb().'forum_threads');
  528.                 redirect(url('forums/viewthread/'.$thread['id']),1);
  529.                 break;
  530.             case 'torecyle':
  531.                 c('Are you sure to Move the thread to Recyle Bin: '.url('forums/viewthread/'.$thread['id'],h($thread['topic'])).'?');
  532.                 c('<p>'.url('forums/recylethread/'.$thread['id'],t('Yes')).', <a href="#" onclick="javascript:history.go(-1)">'.t('No').'</a></p>');
  533.                 stop_here();
  534.                 break;             
  535.             case 'todelete':
  536.                 c('Are you sure to Delete this thread: '.url('forums/viewthread/'.$thread['id'],h($thread['topic'])).'?');
  537.                 c('<p>'.url('forums/deletethread/'.$thread['id'],t('Yes')).', <a href="#" onclick="javascript:history.go(-1)">'.t('No').'</a></p>');
  538.                 stop_here();
  539.                 break;
  540.             }
  541.         redirect(url('forums/listthreads/'.$thread['fid']),1);
  542.     }
  543.    
  544.     function movethread() {
  545.         global $client;
  546.         need_login();
  547.         $thread = valid_thread($_POST['tid']);
  548.         $forum = valid_forum($thread['fid']);
  549.         $new_forum = valid_forum($_POST['new_fid']);
  550.         if (!forum_mod($thread['fid'])) die('access denied');
  551.         $set_thread['id'] = $thread['id'];
  552.         $set_thread['old_fid'] = $thread['fid'];
  553.         $set_thread['fid'] = $new_forum['id'];
  554.         sql_update($set_thread,tb().'forum_threads');
  555.         // update num
  556.         sql_query("update ".tb()."forums set threads=threads+1,posts=posts+{$thread['posts']} where id='{$_POST['new_fid']}' ");
  557.         sql_query("update ".tb()."forums set threads=threads-1,posts=posts-{$thread['posts']} where id='{$thread['fid']}' ");
  558.         redirect(url('forums/viewthread/'.$thread['id']),1);
  559.     }
  560.    
  561.     function recylethread($tid) {
  562.         global $client;
  563.         need_login();
  564.         $thread = valid_thread($tid);
  565.         if (!forum_mod($thread['fid'])) die('access denied');
  566.         $set_thread['id'] = $thread['id'];
  567.         $set_thread['old_fid'] = $thread['fid'];
  568.         $set_thread['fid'] = 0;
  569.         sql_update($set_thread, tb()."forum_threads");
  570.         // update num
  571.         sql_query("update ".tb()."forums set threads=threads-1,posts=posts-{$thread['posts']} where id='{$thread['fid']}' ");
  572.         redirect(url('forums/listthreads/'.$thread['fid']),1);
  573.     }
  574.    
  575.     function deletethread($tid) {
  576.         global $client;
  577.         need_login();
  578.         $thread = valid_thread($tid);
  579.         if (!forum_mod($thread['fid'])) die('access denied');
  580.         // delete thread
  581.         sql_query("delete from ".tb()."forum_threads where id='$tid' ");
  582.         // delete post
  583.         sql_query("delete from ".tb()."forum_posts where tid='$tid' ");
  584.         // delete attach
  585.         $res = sql_query("select * from ".tb()."forum_attachments where tid='$tid' ");
  586.         while ($attach = sql_fetch_array($res)) {
  587.             sql_query("delete from ".tb()."forum_attachments where id='{$attach['id']}' ");
  588.             unlink($attach['uri']);
  589.         }
  590.         // update num
  591.         sql_query("update ".tb()."forums set threads=threads-1,posts=posts-{$thread['posts']} where id='{$thread['fid']}' ");
  592.         redirect(url('forums/listthreads/'.$thread['fid']),1);
  593.     }
  594.    
  595.    
  596.     function download($aid) {
  597.         global $client, $from_url;
  598.         $res = sql_query("select * from ".tb()."forum_attachments where id='$aid' ");
  599.         $attach = sql_fetch_array($res);
  600.         if (!$attach['id']) die('wrong aid');
  601.         $res = sql_query("select * from ".tb()."forum_posts where id='{$attach['pid']}' ");
  602.         $post = sql_fetch_array($res);
  603.         if (!$post['id']) die('wrong pid from aid');
  604.         $thread = valid_thread($post['tid']);
  605.         $forum = valid_forum($thread['fid']);
  606.         if (!eregi(uhome(),$from_url)) die('Sorry, you should download files from forum thread');
  607.         if (!forum_mod('',explode(',',$forum['moderators'])) ) {
  608.             do_auth($forum['read_roles']);
  609.         }
  610.         if ($attach['uri']) {
  611.             $ext = substr($attach['orginal_name'],-3,3);
  612.             if (eregi("\.",$ext)) $ext = substr($ext,-2,2);
  613.             header("Content-disposition: filename=".$attach['orginal_name']);
  614.             header("Content-type: ".$ext);
  615.             readfile($attach['uri']);
  616.         }
  617.         else die('no file uri');
  618.         $q = "UPDATE ".tb()."forum_attachments SET downloads=downloads+1 WHERE id='$aid' ";
  619.         sql_query($q, $conn);
  620.  
  621.     }
  622.  
  623.     function vote() {
  624.         global $client;
  625.         if (!$client['id']) {
  626.             redirect('member/login/1');
  627.         }
  628.         if (!count($_POST['keys']))
  629.             sys_back('No option selected');
  630.         $res = sql_query("select * from ".tb()."forum_polls where id='{$_POST['poll_id']}' ");
  631.         $poll = sql_fetch_array($res);
  632.         if (!$poll['id']) die('wrong pid');
  633.         if (eregi(','.$client['id']."(,|$)",$poll['voters']))
  634.             sys_back('You have voted before');
  635.         $thread = valid_thread($poll['tid']);
  636.         $opts = unserialize($poll['options']);
  637.         for ($i=0; $i< $poll['options_per_user']; $i++) {
  638.             if (is_numeric($_POST['keys'][$i])) {
  639.                 $key = $_POST['keys'][$i];
  640.                 if (is_array($opts[$key]))
  641.                     $opts[$key]['votes']++;
  642.             }
  643.         }
  644.         $set_poll['id'] = $poll['id'];
  645.         $set_poll['options'] = serialize($opts);
  646.         $set_poll['voters'] = $poll['voters'].','.$client['id'];
  647.         $set_poll['total'] = $poll['total'] + 1;
  648.         sql_update($set_poll,tb().'forum_polls');
  649.         redirect('forums/viewthread/'.$thread['id'],1);
  650.     }
  651.  
  652.     function newthread($fid) {
  653.         limit_posting(1);
  654.         global $client;
  655.         $forum = valid_forum($fid);
  656.         if (!forum_mod('',explode(',',$forum['moderators'])) ) {
  657.             do_auth($forum['thread_roles']);
  658.         }
  659.         if (!$client['id']) {
  660.             redirect('member/login/1');
  661.         }
  662.         nav(url('forums/listthreads/'.$forum['id'],h($forum['name'])));
  663.         c('
  664.         <form method="post" action="index.php?p=forums/newthreadpost">');
  665.         if (strlen(get_text('forum_post_rule'))) {
  666.             c('
  667.       <div style="background:#eee;color:black;border:#ccc 1px solid;padding:3px;margin:5px;">
  668.             <div style="padding:5px;font-weight:bold">'.t('Posting Rules').'</div><br />
  669.             '.get_text('forum_post_rule').'<br /><br /></div>');
  670.         }
  671.         c('<p><label>'.t('Icon').':</label><input type="radio" name="icon" value="0" checked />None');
  672.         for($i=1;$i<=8;$i++) {
  673.             c('<input type="radio" name="icon" value="'.$i.'" /><img src="'.uhome().'/files/icons/forums/icon'.$i.'.png" /> &nbsp; ');
  674.         }
  675.         c('</p>');
  676.         c('<p>
  677.                         '.label(t('Title')).'
  678.                         <input type="text" name="title" size="45" />
  679.         </p>');
  680.         c(post_form());
  681.         c('<p>
  682.                         '.label(t('Post options')).'</label>
  683.                         <input type="checkbox" name="parseurl" value="1" checked />'.t('Automatically parse URLs').'<br />
  684.                         <input type="checkbox" name="subscribe" value="1" />'.t('Subscribe to this thread'));
  685.         if (allow_access($forum['upload_roles']))
  686.             c('<br /><input type="checkbox" name="attachment" value="1" />'.t('Upload attachments on the next step'));
  687.         c('
  688.                         </p>
  689.                     <p>'.label(t('Poll')).'
  690.         <input type="checkbox" name="createapoll" value="1" />'.t('Create a poll').'
  691.         </p>
  692.         <script type="text/javascript">
  693.         jQuery(document).ready(function($) {
  694.             $("input[name=\'createapoll\']").click(
  695.                 function() {
  696.                 $("#poll_creation").toggle("slow");
  697.                 }
  698.                 );
  699.         })
  700.       </script>
  701.       <div id="poll_creation" style="display:none">
  702.       <fieldset>'.label(t('Poll')).'</legend>
  703.       <p>'.label(t('Poll question')).'<input type="text" size="40" name="poll_question" />
  704.       </p>
  705.       <p>'.label(t('Running days')).'</label><input type="text" size="2" name="poll_run_days" value="7" />
  706.       </p>
  707.       <p>'.label(t('Poll options')).'</label><textarea name="poll_options" rows="13"></textarea><br />
  708.       <span>'.t('Place each option on a new line. You may enter up to 13 options').'</span></p>
  709.       <p><label>'.t('Options per user').'</label><input type="text" size="2" name="options_per_user" value="1" />
  710.       <span>'.t('This is the number of options each user may select when voting').'</span></p>
  711.       </div>');
  712.         c('
  713.         <input type="hidden" name="fid" value="'.$fid.'" />
  714.         <p>
  715.                         <input class="button" type="submit" value="'.t('Submit').'" />
  716.                         </p>
  717.         </form>');
  718.         section_close(t('New Thread'));
  719.     }
  720.     function newthreadpost() {
  721.         global $client;
  722.         if (!$client['id']) {
  723.             redirect('member/login/1');
  724.         }
  725.         $forum = valid_forum($_POST['fid']);
  726.         if (!forum_mod('',explode(',',$forum['moderators'])) ) {
  727.             do_auth($forum['thread_roles']);
  728.         }
  729.         $thread['topic'] = $_POST['title'];
  730.         $thread['fid'] = $_POST['fid'];
  731.         $thread['username'] = $client['uname'];
  732.         $thread['userid'] = $client['id'];
  733.         $thread['icon'] = $_POST['icon'];
  734.         $thread['posts'] = 1;
  735.         $thread['lastpostusername'] = $client['uname'];
  736.         $thread['created'] = time();
  737.         $thread['lastpostcreated'] = time();
  738.         $post['title'] = $_POST['title'];
  739.         if ($_POST['parseurl']) $_POST['form_content'] = parseurl($_POST['form_content']);
  740.         $post['message'] = $_POST['form_content'];
  741.         check_thread($thread);
  742.         check_post($post);
  743.         // poll
  744.         if ($_POST['createapoll']) {
  745.             $thread['got_poll'] = 1;
  746.             if (strlen($_POST['poll_question']) < 5)
  747.                 sys_back(t('Poll question is too short'));
  748.             $poll_options = stripslashes($_POST['poll_options']);
  749.             $poll_options = explode("\r\n",$poll_options);
  750.             foreach ($poll_options as $val) {
  751.                 if (strlen(trim($val)) > 0)
  752.                     $poll_opts[] = array('option'=>$val,'votes'=>0);
  753.             }
  754.             if ($_POST['poll_run_days'] < 1)
  755.                 sys_back(t('Please input a valid poll running days'));
  756.             $timeout = time() + $_POST['poll_run_days']*3600*24;
  757.             if (count($poll_opts) < 2)
  758.                 sys_back(t('You need to set at least 2 options'));
  759.             if ($_POST['options_per_user'] < 1 || $_POST['options_per_user'] > count($poll_opts))
  760.                 sys_back(t('Please input a valid "options per user"'));
  761.             $poll['options'] = serialize($poll_opts);
  762.             $poll['question'] = $_POST['poll_question'];
  763.             $poll['created'] = time();
  764.             $poll['options_per_user'] = $_POST['options_per_user'];
  765.             $poll['timeout'] = $timeout;
  766.         }
  767.         limit_posting();
  768.         sql_insert($thread,tb().'forum_threads');
  769.         $tid = insert_id();
  770.         if ($_POST['subscribe']) {
  771.             sql_insert(array('uid'=>$client['id'],'tid'=>$tid),tb()."forum_subscribes");
  772.         }
  773.         if ($_POST['createapoll']) {
  774.             $poll['tid'] = $tid;
  775.             sql_insert($poll,tb().'forum_polls');
  776.         }
  777.         $post['tid'] = $tid;
  778.         $post['is_first'] = 1;
  779.         $post['uid'] = $client['id'];
  780.         $post['created'] = time();
  781.  
  782.  
  783.         sql_insert($post,tb().'forum_posts');
  784.         $pid = insert_id();
  785.  
  786.         if (in_array(1,$forum['read_roles']) || in_array(2,$forum['read_roles'])) {
  787.             $attachment = array(
  788.                     'cwall_id' => 'none',
  789.                     'uri' => 'forums/viewthread/'.$tid,
  790.                     'name' => h($_POST['title'])
  791.                     );
  792.             $app = array('name'=>'forums','id'=>$tid);
  793.             $replyurl = url('forums/newreply/'.$pid,t('Reply'));
  794.             $stream_id = stream_publish(
  795.                 addslashes(h(utf8_substr(stripslashes($_POST['form_content']),120)).' <span style="float:right;">'.$replyurl.'</span>'),
  796.                 $attachment,$app);
  797.         }
  798.         sql_query("update ".tb()."forum_posts set stream_id='$stream_id' where id='$pid'");
  799.  
  800.  
  801.         $set_thread['id'] = $tid;
  802.         $set_thread['pid'] = $pid;
  803.         sql_update($set_thread,tb().'forum_threads');
  804.         $set_forum['id'] = $forum['id'];
  805.         $set_forum['posts'] = $forum['posts'] + 1;
  806.         $set_forum['threads'] = $forum['threads'] + 1;
  807.         $set_forum['lastpostname'] = $client['uname'];
  808.         $set_forum['lastpostcreated'] = time();
  809.         $set_forum['lastposttopic'] = $_POST['title'];
  810.         $set_forum['lastposttopicid'] = $tid;
  811.         sql_update($set_forum,tb().'forums');
  812.         $set_account['id'] = $client['id'];
  813.         $set_account['forum_posts'] = $client['forum_posts']+1;
  814.         sql_update($set_account,tb().'accounts');
  815.  
  816.         if ($_POST['attachment'])
  817.             redirect('forums/attachments/'.$pid,1);
  818.         else
  819.             redirect('forums/viewthread/'.$tid,1);
  820.     }
  821.     function newreply($pid, $quote = false) {
  822.         limit_posting(1);
  823.         global $client;
  824.         if (!$client['id']) {
  825.             redirect('member/login/1');
  826.         }
  827.         $res = sql_query("select * from ".tb()."forum_posts where id='$pid' ");
  828.         $post = sql_fetch_array($res);
  829.         if (!$post['id']) die('wrong pid');
  830.         $thread = valid_thread($post['tid']);
  831.         if ($thread['thread_lock']) {
  832.             sys_back(t('Sorry, this thread has been locked'));
  833.         }
  834.         $forum = valid_forum($thread['fid']);
  835.         if (!forum_mod('',explode(',',$forum['moderators'])) ) {
  836.             do_auth($forum['thread_roles']);
  837.         }
  838.         nav(url('forums/listthreads/'.$forum['id'],h($forum['name'])));
  839.         nav(url('forums/viewthread/'.$thread['id'],h($thread['topic'])));
  840.         c('
  841.         <form method="post" action="'.uhome().'/index.php?p=forums/newreplypost">');
  842.         $res = sql_query("select username from ".tb()."accounts where id='{$post['uid']}' ");
  843.         $user = sql_fetch_array($res); 
  844.         c('<p>
  845.                         <label>'.t('Title').' <i>('.t('Optional').')</i>:</label>
  846.                         <input type="text" name="title" size="45" value="re: '.$user['username'].'" />
  847.         </p>');
  848.  
  849.         if ($quote) {
  850.             $sfrom = array("/\[quote](.*)\[\/quote]/isU","/\[quote=([0-9a-z_]+)](.*)\[\/quote]/isU");
  851.             $sto = array('','');
  852.             $quote_msg = preg_replace($sfrom, $sto, $post['message']);
  853.             $set_post = array('message' => '[quote='.$user['username'].']'.h($quote_msg).'[/quote]');
  854.         }
  855.         c(post_form(16, $set_post));
  856.         c('
  857.         <p>
  858.                         <label>'.t('Post options').'</label>
  859.                         <input type="checkbox" name="parseurl" value="1" checked />'.t('Automatically parse URLs').'<br />
  860.                         <input type="checkbox" name="attachment" value="1" />'.t('Upload attachments on the next step').'
  861.                         </p>
  862.         <input type="hidden" name="pid" value="'.$pid.'" />
  863.         <input type="submit" class="button" value="'.t('Submit').'" />
  864.         </form>');
  865.         section_close(t('New Thread'));
  866.     }
  867.    
  868.     function newreplypost() {
  869.         global $client,$num_per_page;
  870.         if (!$client['id']) {
  871.             redirect('member/login/1');
  872.         }
  873.         $res = sql_query("select * from ".tb()."forum_posts where id=".$_POST['pid']."");
  874.         $post = sql_fetch_array($res);
  875.         if (!$post['id']) die('wrong pid');
  876.         $thread = valid_thread($post['tid']);
  877.         if ($thread['thread_lock']) {
  878.             die(t('Sorry, this thread has been locked'));
  879.         }
  880.         $forum = valid_forum($thread['fid']);
  881.         if (!forum_mod('',explode(',',$forum['moderators'])) ) {
  882.             do_auth($forum['thread_roles']);
  883.         }
  884.         $set_post['tid'] = $thread['id'];
  885.         $set_post['title'] = $_POST['title'];
  886.         if ($_POST['parseurl']) $_POST['form_content'] = parseurl($_POST['form_content']);
  887.         $set_post['message'] = $_POST['form_content'];
  888.         $set_post['uid'] = $client['id'];
  889.         $set_post['created'] = time();
  890.         check_post($set_post);
  891.         limit_posting();
  892.         sql_insert($set_post,tb().'forum_posts');
  893.         $pid = insert_id();
  894.  
  895.         $posturl = url('forums/viewthread/'.$thread['id']);
  896.         $set_thread['id'] = $thread['id'];
  897.         $set_thread['posts'] = $thread['posts']+1;
  898.  
  899.         if ($set_thread['posts'] > $num_per_page) {
  900.             $num = ceil($set_thread['posts']/$num_per_page);
  901.             if (eregi('\?',$thread_url)) {
  902.                 $posturl = $posturl.'&page='.$num;
  903.             }
  904.             else {
  905.                 $posturl = $posturl.'&page='.$num;
  906.             }
  907.         }
  908.         $posturl = $posturl.'#num'.$post['id'];
  909.  
  910.         if (in_array(1,$forum['read_roles']) || in_array(2,$forum['read_roles'])) {
  911.             $posturl = '<a href="'.$posturl.'">'.t('View').'</a>';
  912.             $attachment = array(
  913.                     'cwall_id' => 'none',
  914.                     'name'=> t('Reply to'),
  915.                     'des' => addslashes(h(utf8_substr($post['message'],60)))
  916.                     );
  917.             $app = array('name'=>'forums','id'=>$thread['id']);
  918.             $replyurl = url('forums/newreply/'.$pid,t('Reply'));
  919.             $stream_id = stream_publish(
  920.                 addslashes(h(utf8_substr(stripslashes($_POST['form_content']),120)).' <span style="float:right;">'.$posturl.' | '.$replyurl.'</span>'),
  921.                 $attachment,$app);
  922.             sql_query("update ".tb()."forum_posts set stream_id='$stream_id' where id='$pid'");
  923.         }
  924.        
  925.  
  926.  
  927.         $set_thread['lastpostusername'] = $client['uname'];
  928.         $set_thread['lastpostcreated'] = time();
  929.         sql_update($set_thread,tb().'forum_threads');
  930.         $set_forum['id'] = $forum['id'];
  931.         $set_forum['posts'] = $forum['posts'] + 1;
  932.         $set_forum['lastpostname'] = $client['uname'];
  933.         $set_forum['lastpostcreated'] = time();
  934.         $set_forum['lastposttopic'] = addslashes($thread['topic']);
  935.         $set_forum['lastposttopicid'] = $thread['id'];
  936.         sql_update($set_forum,tb().'forums');
  937.         $set_account['id'] = $client['id'];
  938.         $set_account['forum_posts'] = $client['forum_posts']+1;
  939.         sql_update($set_account,tb().'accounts');
  940.         send_note($post['uid'],t('{1} replied your post',name2profile($client['username'])).': '.url('forums/viewthread/'.$thread['id'],h(utf8_substr($_POST['form_content'],50)) ) );
  941.         // subscribe
  942.         $res = sql_query("select u.id,u.email from ".tb()."forum_subscribes as s left join ".tb()."accounts as u on u.id=s.uid where s.tid='{$thread['id']}'");
  943.         $msg = t('{1} has replied a Thread to which you have subscribed:',$client['username']).'<br />
  944.         '.url('forums/viewthread/'.$thread['id'],h($thread['topic']) );
  945.         while ($row = sql_fetch_array($res)) {
  946.             if ($row['id'] != $client['id']) {
  947.                 @jcow_mail($row['email'], 'Reply to Thread: '.h($thread['topic']).'', $msg);
  948.             }
  949.         }
  950.        
  951.  
  952.  
  953.         if ($_POST['attachment'])
  954.             redirect('forums/attachments/'.$pid,1);
  955.         else {
  956.             $thread_url = url('forums/viewthread/'.$thread['id']);
  957.             $thread['posts']++;
  958.             if ($thread['posts'] > $num_per_page) {
  959.                 $num = ceil($thread['posts']/$num_per_page);
  960.                 if (eregi('\?',$thread_url)) {
  961.                     $thread_url = $thread_url.'&page='.$num;
  962.                 }
  963.                 else {
  964.                     $thread_url = $thread_url.'?page='.$num;
  965.                 }
  966.             }
  967.             redirect($thread_url,1);
  968.         }
  969.     }
  970.  
  971.  
  972.     function editpost($pid) {
  973.         global $client;
  974.         if (!$client['id']) {
  975.             redirect('member/login/1');
  976.         }
  977.         $res = sql_query("select * from ".tb()."forum_posts where id='$pid' ");
  978.         $post = sql_fetch_array($res);
  979.         if (!$post['id']) die('wrong pid');
  980.         if ($post['uid'] != $client['id']) die('access denied');
  981.         $thread = valid_thread($post['tid']);
  982.         $forum = valid_forum($thread['fid']);
  983.         nav(url('forums/listthreads/'.$forum['id'],h($forum['name'])));
  984.         nav(url('forums/viewthread/'.$thread['id'],h($thread['topic'])));
  985.         c('<table class="stories">
  986.         <tr class="table_line1"><td colspan="2">'.t('Edit post').'</td></tr></table>');
  987.         c('<form action="'.url('forums/editpostpost').'" method="post">
  988.         <p><label>'.t('Title').'</label><input type="text" size="45" name="title" value="'.h($post['title']).'" /></p>');
  989.                 if($post['is_first']){
  990.                 c('<p><label>'.t('Is Resolved').'</label><select name="resolved"><option value="0">No</option><option value="1">Yes</option></select></p>');
  991.                 }
  992.         c(post_form(16,$post));
  993.         c('
  994.         <p>
  995.                         <label>'.t('Post options').'</label>
  996.                         <input type="checkbox" name="parseurl" value="1" checked />'.t('Automatically parse URLs').'<br />'
  997.                         .$subscribe.'
  998.                         '.url('forums/attachments/'.$post['id'], t('Manage attachments')).'
  999.                         </p>
  1000.         <input type="hidden" name="pid" value="'.$post['id'].'" />
  1001.         <input type="submit" class="button" value="'.t('Save changes').'" />
  1002.         </form>');
  1003.     }
  1004.    
  1005.     function editpostpost() {
  1006.         global $client;
  1007.         if (!$client['id']) {
  1008.             redirect('member/login/1');
  1009.         }
  1010.         if ($_POST['parseurl']) $_POST['form_content'] = parseurl($_POST['form_content']);
  1011.         $res = sql_query("select * from ".tb()."forum_posts where id='{$_POST['pid']}' ");
  1012.         $post = sql_fetch_array($res);
  1013.         if (!$post['id']) die('wrong pid');
  1014.         if ($post['uid'] != $client['id']) die('access denied');
  1015.         $thread = valid_thread($post['tid']);
  1016.         $set_post['id'] = $post['id'];
  1017.         $set_post['title'] = $_POST['title'];
  1018.         $set_post['message'] = $_POST['form_content'];
  1019.                 check_post($set_post);
  1020.         sql_update($set_post, tb().'forum_posts');
  1021.         if ($post['is_first']) {
  1022.             $set_thread['id'] = $post['tid'];
  1023.             $set_thread['topic'] = $_POST['title'];
  1024.             $set_thread['resolved'] = $_POST['resolved'];
  1025.             check_thread($set_thread);
  1026.             sql_update($set_thread, tb().'forum_threads');
  1027.         }
  1028.         redirect(url('forums/viewthread/'.$post['tid']),1);
  1029.     }
  1030.    
  1031.     function deletepost($pid) {
  1032.         global $client;
  1033.         if (!$client['id']) {
  1034.             redirect('member/login/1');
  1035.         }
  1036.         $res = sql_query("select * from ".tb()."forum_posts where id='{$pid}' ");
  1037.         $post = sql_fetch_array($res);
  1038.         if (!$post['id']) die('wrong pid');
  1039.         if ($post['is_first']) die('you can not delete the primary post. To delete the thread instead');
  1040.         if ($post['uid'] != $client['id'] && !allow_access(3)) die('access denied');
  1041.         $thread = valid_thread($post['tid']);
  1042.         sql_query("delete from ".tb()."forum_posts where id='$pid' ");
  1043.         // delete attach
  1044.         $res = sql_query("select * from ".tb()."forum_attachments where pid='$pid' ");
  1045.         while ($attach = sql_fetch_array($res)) {
  1046.             sql_query("delete from ".tb()."forum_attachments where id='{$attach['id']}' ");
  1047.             unlink($attach['uri']);
  1048.         }
  1049.         // update num
  1050.         sql_query("update ".tb()."forum_threads set posts=posts-1 where id='{$thread['id']}' ");
  1051.         sql_query("update ".tb()."forums set posts=posts-1 where id='{$thread['fid']}' ");
  1052.         redirect(url('forums/viewthread/'.$post['tid']),1);
  1053.     }
  1054.  
  1055.     function attachments($pid) {
  1056.         GLOBAL $client;
  1057.         if (!$client['id']) {
  1058.             redirect('member/login/1');
  1059.         }
  1060.         $res = sql_query("select * from ".tb()."forum_posts where id='$pid' ");
  1061.         $post = sql_fetch_array($res);
  1062.         if (!$post['id']) die('wront pid');
  1063.         if ($post['uid'] != $client['id']) {
  1064.             die('not your post');
  1065.         }
  1066.         $thread = valid_thread($post['tid']);
  1067.         $forum = valid_forum($thread['fid']);
  1068.         if (!forum_mod('',explode(',',$forum['moderators'])) ) {
  1069.             do_auth($forum['upload_roles']);
  1070.         }
  1071.         nav(url('forums/listthreads/'.$forum['id'],h($forum['name'])));
  1072.         nav(url('forums/viewthread/'.$thread['id'],h($thread['topic'])));
  1073.         // current files
  1074.         c('<table class="stories">
  1075.         <tr class="table_line1"><td colspan="2">'.t('Current files').'</td></tr>');
  1076.         $res = sql_query("select * from ".tb()."forum_attachments where pid={$pid}  order by id DESC");
  1077.         while ($row = sql_fetch_array($res)) {
  1078.             c('<tr class="row1"><td><a href="'.uhome().'/'.$row['uri'].'">'.h($row['orginal_name']).'</a> <span class="sub">('.h($row['des']).')</span></td><td>'.url('forums/attachment_delete/'.$row['id'],t('Delete')).'</td></tr>');
  1079.         }
  1080.  
  1081.         c('</table><br /><table class="stories">
  1082.         <tr class="table_line1"><td colspan="2">'.t('Upload').'</td></tr></table>');
  1083.         c('<form action="'.url('forums/attachmentspost').'" method="post" enctype="multipart/form-data">
  1084.                         <p><label>'.t('File').'</label><input type="file" name="myfile" />
  1085.                         <span>(jpg, png, gif, zip, rar, tar, gz)<br />
  1086.                         Maximum:100k bytes</span></p>
  1087.                         <p><label>'.t('Description').'</label><input type="text" name="description" size="45" />
  1088.                         </p>
  1089.                         <input type="submit" class="button" value="'.t('Submit').'" />
  1090.                         <input type="hidden" name="pid" value="'.$pid.'" />
  1091.                          [ '.url('forums/viewthread/'.$thread['id'],h('Finish') ).' ]
  1092.                 </form>');
  1093.     }
  1094.  
  1095.     function attachmentspost() {
  1096.         GLOBAL $db,$client,$ubase;
  1097.         if (!$client['id']) {
  1098.             redirect('member/login/1');
  1099.         }
  1100.         $timeline = time();
  1101.         $res = sql_query("select * from ".tb()."forum_posts where id='{$_POST['pid']}' ");
  1102.         $post = sql_fetch_array($res);
  1103.         if ($post['uid'] != $client['id']) {
  1104.             die('not your post');
  1105.         }
  1106.         $thread = valid_thread($post['tid']);
  1107.         $forum = valid_forum($thread['fid']);
  1108.         if (!forum_mod('',explode(',',$forum['moderators'])) ) {
  1109.             do_auth($forum['upload_roles']);
  1110.         }
  1111.         // up
  1112.         if ($uri = save_file($_FILES['myfile'],array('jpg','png','gif','zip','rar','tar','gz'))) {
  1113.             $size = $_FILES['myfile']['size'];
  1114.             if ($size > 100*1024) {
  1115.                 sys_back('Sorry, you cannot upload a file larger than 100k');
  1116.             }
  1117.             $name = addslashes($_FILES['myfile']['name']);
  1118.             sql_query("insert into `".tb()."forum_attachments` (pid,uri,des,size,orginal_name,tid) values({$_POST['pid']},'$uri','".$_POST['description']."','$size','$name','{$post['tid']}')");
  1119.             $set_thread['id'] = $post['tid'];
  1120.             $set_thread['got_attach'] = 1;
  1121.             sql_update($set_thread, tb().'forum_threads');
  1122.             $set_post['id'] = $_POST['pid'];
  1123.             $set_post['got_attach'] = 1;
  1124.             sql_update($set_post, tb().'forum_posts');
  1125.             redirect($ubase.$this->name.'/attachments/'.$_POST['pid'], 1);
  1126.         }
  1127.         else {
  1128.             die('sorry,failed to upload pic');
  1129.         }
  1130.     }
  1131.    
  1132.     function attachment_delete($aid) {
  1133.         GLOBAL $db,$client,$ubase;
  1134.         if (!$client['id']) {
  1135.             redirect('member/login/1');
  1136.         }
  1137.         $timeline = time();
  1138.         $res = sql_query("select * from ".tb()."forum_attachments where id='{$aid}' ");
  1139.         $attach = sql_fetch_array($res);
  1140.         if (!$attach['id']) die('wrong aid');
  1141.         $res = sql_query("select * from ".tb()."forum_posts where id='{$attach['pid']}' ");
  1142.         $post = sql_fetch_array($res);
  1143.         if (!$post['id']) die('wrong pid from aid');
  1144.         if ($post['uid'] != $client['id']) {
  1145.             die('not your post');
  1146.         }
  1147.         // delete
  1148.         sql_query("delete from ".tb()."forum_attachments where id='$aid' ");
  1149.         unlink($attach['uri']);
  1150.         redirect(url('forums/attachments/'.$post['id']), 1);
  1151.     }
  1152.    
  1153.     function post_operations($post) {
  1154.         global $client;
  1155.         c('<div class="post_operations" ><ul valign="bottom">');
  1156.         if ($post['uid'] == $client['id'] || $client['level'] == 9)
  1157.         c('<li>'.url('forums/editpost/'.$post['id'],t('Edit')).'</li>');
  1158.         if (allow_access(3))
  1159.             c('<li>'.url('forums/deletepost/'.$post['id'],t('Delete')).'</li>');
  1160.         if ($client['id'])
  1161.         c('<li>'.url('forums/newreply/'.$post['id'].'/quote',t('Quote')).'</li>
  1162.         <li>'.url('forums/newreply/'.$post['id'],t('Reply')).'</li>');
  1163.        
  1164.         if ($post['stream_id']) {
  1165.                 //c(comment_form($post['stream_id'],'',1));
  1166.                 c(forum_comment_form($post['stream_id']));
  1167.             }
  1168.            
  1169.         c('</ul></div>');
  1170.     }
  1171.    
  1172.     function ajax_post() {
  1173.         global $client;
  1174.         if (!$client['id']) {
  1175.             die('please login');
  1176.         }
  1177.         if (!$_POST['thread_title'] || !$_POST['thread_content']) {
  1178.             forums::ajax_error(t('Please fill in all the required blanks'));
  1179.         }
  1180.         if (!$_POST['fid']) {
  1181.             forums::ajax_error(t('Please select a forum..'));
  1182.         }
  1183.         $forum = valid_forum($_POST['fid']);
  1184.         if (!forum_mod('',explode(',',$forum['moderators'])) ) {
  1185.             if (!allow_access($forum['thread_roles'])) {
  1186.                 die(t('Access denied'));
  1187.             }
  1188.         }
  1189.         $thread['topic'] = $_POST['thread_title'];
  1190.         $thread['fid'] = $_POST['fid'];
  1191.         $thread['username'] = $client['uname'];
  1192.         $thread['userid'] = $client['id'];
  1193.         $thread['icon'] = $_POST['icon'];
  1194.         $thread['posts'] = 1;
  1195.         $thread['lastpostusername'] = $client['uname'];
  1196.         $thread['created'] = time();
  1197.         $thread['lastpostcreated'] = time();
  1198.         $post['title'] = $_POST['title'];
  1199.         if ($_POST['parseurl']) $_POST['thread_content'] = parseurl($_POST['thread_content']);
  1200.         $post['message'] = $_POST['thread_content'];
  1201.         limit_posting(0,1);
  1202.         sql_insert($thread,tb().'forum_threads');
  1203.         $tid = insert_id();
  1204.         if ($_POST['subscribe']) {
  1205.             sql_insert(array('uid'=>$client['id'],'tid'=>$tid),tb()."forum_subscribes");
  1206.         }
  1207.         $post['tid'] = $tid;
  1208.         $post['is_first'] = 1;
  1209.         $post['uid'] = $client['id'];
  1210.         $post['created'] = time();
  1211.         sql_insert($post,tb().'forum_posts');
  1212.         $pid = insert_id();
  1213.         $set_thread['id'] = $tid;
  1214.         $set_thread['pid'] = $pid;
  1215.         sql_update($set_thread,tb().'forum_threads');
  1216.         $set_forum['id'] = $forum['id'];
  1217.         $set_forum['posts'] = $forum['posts'] + 1;
  1218.         $set_forum['threads'] = $forum['threads'] + 1;
  1219.         $set_forum['lastpostname'] = $client['uname'];
  1220.         $set_forum['lastpostcreated'] = time();
  1221.         $set_forum['lastposttopic'] = $_POST['title'];
  1222.         $set_forum['lastposttopicid'] = $tid;
  1223.         sql_update($set_forum,tb().'forums');
  1224.         $set_account['id'] = $client['id'];
  1225.         $set_account['forum_posts'] = $client['forum_posts']+1;
  1226.         sql_update($set_account,tb().'accounts');
  1227.        
  1228.         if (in_array(1,$forum['read_roles']) || in_array(2,$forum['read_roles'])) {
  1229.             $attachment = array(
  1230.                     'cwall_id' => 'none',
  1231.                     'uri' => 'forums/viewthread/'.$tid,
  1232.                     'name' => h($_POST['thread_title'])
  1233.                     );
  1234.             $app = array('name'=>'forums','id'=>$tid);
  1235.             $replyurl = url('forums/newreply/'.$pid,t('Reply'));
  1236.             $stream_id = stream_publish(
  1237.                 addslashes(h(utf8_substr(stripslashes($_POST['thread_content']),120)).' '.$replyurl),
  1238.                 $attachment,$app);
  1239.         }
  1240.         sql_query("update ".tb()."forum_posts set stream_id='$stream_id' where id='$pid'");
  1241.  
  1242.  
  1243.  
  1244.         echo t('Thread created!').' <a href="'.url('forums/viewthread/'.$tid).'"><strong>'.t('View').'</strong></a>';
  1245.         exit;
  1246.     }
  1247.  
  1248.     function ajax_form($page_type='') {
  1249.         global $client;
  1250.         if (!$client) die('login');
  1251.         $forums  = '<select name="fid">
  1252.         <option value="0">'.t('Please select a forum..').'</option>';
  1253.         $res = sql_query("select f.*,g.name as gname from ".tb()."forums as f left join ".tb()."forums as g on g.id=f.parent_id where f.forum_type='forum' order by f.parent_id");
  1254.         while ($row = sql_fetch_array($res)) {
  1255.             if ($row['id'] == $_POST['fid'])
  1256.                 $row['selected'] = 'selected';
  1257.             $forums .= '<option value="'.$row['id'].'" '.$row['selected'].'>'.h($row['gname']).' - '.h($row['name']).'</option>';
  1258.         }
  1259.         $forums .= '</select>';
  1260.         echo '
  1261.         <div>
  1262. ';
  1263. if ($_POST['thread_title']) {
  1264.     $pre_title = h(stripslashes($_POST['thread_title']));
  1265. }
  1266. else {
  1267.     $pre_title = '';
  1268. }
  1269. echo '
  1270.         <strong>'.t('Thread title').'</strong>:
  1271.         <input type="text" size="55" name="thread_title" value="'.$pre_title.'"  /><br />
  1272.         <textarea rows="9" cols="55" name="thread_content">'.h(stripslashes($_POST['thread_content'])).'</textarea><br />
  1273.         '.$forums.'
  1274.         </div>';
  1275.         exit;
  1276.     }
  1277.  
  1278.     function ajax_error($msg) {
  1279.         echo '<div style="color:red">'.$msg.'</div>';
  1280.         echo forums::ajax_form();
  1281.         exit;
  1282.     }
  1283. }
  1284.  
  1285. function valid_forum($fid) {
  1286.     if (is_numeric($fid) && $fid == 0) {
  1287.         if (!forum_mod()) die('Forum Recyle Bin: Access Denied');
  1288.         $forum = array('id'=>0,'name'=>t('Recyle Bin'));
  1289.         return $forum;
  1290.     }
  1291.     $res = sql_query("select * from ".tb()."forums where id='$fid' ");
  1292.     $forum = sql_fetch_array($res);
  1293.     $forum['read_roles'] = explode('|',$forum['read_roles']);
  1294.     $forum['upload_roles'] = explode('|',$forum['upload_roles']);
  1295.     $forum['thread_roles'] = explode('|',$forum['thread_roles']);
  1296.     $forum['reply_roles'] = explode('|',$forum['reply_roles']);
  1297.     if (!$forum['id']) die('wrong fid');
  1298.     return $forum;
  1299. }
  1300. function valid_thread($tid) {
  1301.     $res = sql_query("select * from ".tb()."forum_threads where id='$tid' ");
  1302.     $thread = sql_fetch_array($res);
  1303.     if (!$thread['id']) {
  1304.             redirect('forum',1);
  1305.         }
  1306.     return $thread;
  1307. }
  1308.  
  1309. function post_form($rows = 16, $post = array()) {
  1310.     $output = '
  1311.                         <p>
  1312.                         '.label(t('Message')).'
  1313.                         <textarea name="form_content" id="form_message" rows="'.$rows.'" class="fpost"  style="width:90%" >'.h($post['message']).'</textarea>
  1314.                         <div  style="padding-left:20px;">
  1315.                         Bbcode permitido: [url] [img] [code] [b] [i] [u] <a href="http://es.wikipedia.org/wiki/BBCode" target="_blank">¿Qué es bbcode?</a>
  1316.                         </div>
  1317.                         ';
  1318.     return $output;
  1319. }
  1320.  
  1321. function check_thread($thread) {
  1322.     if (strlen($thread['topic']) < 6)
  1323.         sys_back(t('The title is too short'));
  1324. }
  1325.  
  1326. function check_post($post) {
  1327.     if (strlen($post['message']) < 6)
  1328.         sys_back(t('The message is too short'));
  1329. }
  1330.  
  1331. function forum_mod($fid = '', $mods = 0) {
  1332.     global $client;
  1333.     if (!$client['id'])
  1334.         return false;
  1335.     if (in_array(3, $client['roles']))
  1336.         return true;
  1337.     if (!$fid && !count($mods))
  1338.         return false;
  1339.     if (!is_array($mods)) {
  1340.         $res = sql_query("select moderators from ".tb()."forums where id='$fid' ");
  1341.         $row = sql_fetch_array($res);
  1342.         if (strlen($row['moderators'])) {
  1343.             $mods = explode(',',$row['moderators']);
  1344.         }
  1345.         else {
  1346.             return false;
  1347.         }
  1348.     }
  1349.     if (in_array($client['username'], $mods))
  1350.         return true;
  1351.     else
  1352.         return false;
  1353. }
  1354.  
  1355.  
  1356. function forum_comment_form($stream_id) {
  1357.     global $config, $client;
  1358.     if (!$client['id']) {
  1359.         return '';
  1360.     }
  1361.     $res = sql_query("select * from ".tb()."streams where id='$stream_id'");
  1362.     $stream = sql_fetch_array($res);
  1363.     if (!$stream['id']) return '';
  1364.     if (!$flag) $flag = t('Comment');
  1365.     $likeit = '<span><li class="dodislike"><a href="#" >'.t('Dislike').'</a></li><li class="dolike"><a href="#">'.t('Like').'</a></li></span>';
  1366.    
  1367.     if ($stream['uid'] != $client['id']) {
  1368.         $res = sql_query("select * from ".tb()."liked where stream_id='$stream_id' and uid='{$client['id']}' limit 1");
  1369.         if (sql_counts($res)) {
  1370.             $likeit = '<span><li class="dolike"><a href="#">'.t('Unlike').'</a></li></span>';
  1371.         }
  1372.         $res = sql_query("select * from ".tb()."disliked where stream_id='$stream_id' and uid='{$client['id']}' limit 1");
  1373.         if (sql_counts($res)) {
  1374.             $likeit = '<span><li class="dodislike"><a href="#">'.t('Un-dislike').'</a></li></span>';
  1375.         }
  1376.     }
  1377.     else {
  1378.         return '';
  1379.     }
  1380.         return '
  1381.         '.$likeit.'
  1382.             <div class="quick_comment_form" style="display:none;">
  1383.                 <table border="0"><tr><td valign="top">
  1384.                 <img src="'.uhome().'/uploads/avatars/s_'.$client['avatar'].'" width="25" height="25" />
  1385.                 </td><td>
  1386.                 <textarea name="message" rows="2" class="fpost commentmessage""  style="width:350px;" ></textarea>
  1387.                 <input type="button" value=" '.$flag.' " class="fbutton commentsubmit" />
  1388.                 </td></tr>
  1389.                 </table>
  1390.             </div>
  1391.             <input type="hidden" name="wall_id" value="'.$stream_id.'" />
  1392.             <div style="padding-top:20px;text-align:right"></div>';
  1393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement