Location: PHPKode > projects > PHPEcho CMS > modules/forum/index.php
<?php
/*  +--------------------------------------------
*   |
*   |     Product:       PHPEchoCMS
*   |     Author:        Anas Husseini
*   |     License:       GPL
*   |     Last updated:  06.03.07
*   |     For version:   2.0  
*   |     Desc:          Forum Module
*   |        
*   +--------------------------------------------
*/

if( !@$BASE_LOAD )
{
    die('Application error.');
}

function show_forum()
{
    global $root_path, $smarty, $settings, $core;
    
    // -------------------
    // Get lang for module
    // -------------------
    
    $mlang = $core->getlang("forum");
    
    $content = ""; 

    if (@$_POST['post']=='thread')
    {
	if (@$_POST['id'] && $_POST['title'] && $_POST['contents'])
        {
       	     // Add the thread to the specified section
	     $ins = "INSERT INTO `".PREFIX."threads` VALUES ('', '".addslashes($_POST['id'])."', '-1', '".addslashes($_POST['title']).
              "', '".str_replace("\n", "<br>", addslashes($_POST['contents']))."', '".$_SESSION['username']."','".date("d-m-Y H:i")."', '0')";
	      $res = query($ins, 1);
	      $ins = "UPDATE `".PREFIX."sections` SET `lastdate`=".date("d-m-Y H:i")." WHERE `id`=".addslashes($_POST['id']);
              $res = query($ins, 1);
              $nb = query("SELECT max(`id`) FROM `".PREFIX."threads`", 2);
              redirect ("index.php?module=forum&show=thread&id=".$nb);
         }
	 else
	 {
	      $content = $mlang['017'];
	  
	 }
    }

    elseif (@$_POST['post']=='reply')
    {
	if (@$_POST['title'] && $_POST['contents'] && $_POST['id'])
    	{
            // Add the reply to the specified thread
	    $ins = "INSERT INTO `".PREFIX."threads` VALUES ('', '-1', '".addslashes($_POST['id'])."', '".addslashes($_POST['title']).
            "', '".str_replace("\n", "<br>", addslashes($_POST['contents']))."',
            '".$_SESSION['username']."','".date("d-m-Y H:i")."', '-1')";
	    $res = query($ins, 1);
	    $ins = "UPDATE `".PREFIX."threads` SET `nb_replies`=`nb_replies`+1 WHERE `id`=".addslashes($_POST['id']);
	    $res = query($ins, 1);
	    redirect ("index.php?module=forum&show=thread&id=".$_POST['id']);
    	}
	else
		$content = $mlang['017'];
    }
    
    elseif (@$_GET['show']=='section' && $_GET['id'])
    { 
        // Show Threads in a selected Section
         
	    $p_section = query("SELECT * FROM `".PREFIX."sections` WHERE `id`=".addslashes($_GET['id']), 4);
       
        if ($p_section['id'])
        {
            $content .= "<p><font size='3'><b><a href='index.php?module=forum'>".$mlang['000'].
                                  "</a> &gt; ".$p_section['name']."</b></font></p>"; 

            $content .= "<table width='100%'><tr>"; 
            $content .= "<td width='70%' class='title'><b>".$mlang['004']."</b></td>";
            $content .= "<td width='10%' class='title'><b><center>".$mlang['005']."</center></b></td>";
            $content .= "<td width='10%' class='title'><b><center>".$mlang['006']."</center></b></td>";
            $content .= "<td width='10%' class='title'><b><center>".$mlang['007']."</center></b></td></tr></table>";
	 
	        $numthreads = query("select numthreads from `".PREFIX."settings`", 2);
            
            if (!@$_GET['page'])
		        $limit = "LIMIT ".$numthreads;
	        else
		        $limit = "LIMIT ".(($_GET['page']-1)*$numthreads).",".$numthreads;
	
	        $allthreads = query("SELECT * FROM `".PREFIX."threads` WHERE section_id=".addslashes($_GET['id'])." ORDER BY id DESC ".$limit, 1);
	        $nb_threads = query("SELECT COUNT(*) FROM `".PREFIX."threads` WHERE section_id=".addslashes($_GET['id']), 2);
         
            if ($nb_threads>0)
            {
                while ($threads = mysql_fetch_array($allthreads))
                {
                    $smarty->assign ( "title" , $threads['title']);
                    $smarty->assign ( "link" , "index.php?module=forum&show=thread&id=".$threads['id']);
	                $smarty->assign( "author", $threads['author']);
                    $smarty->assign( "replies", $threads['nb_replies']);
	                $smarty->assign( "date", $threads['date']);
                    $content .= $smarty->fetch('modules/section.tpl');
                }
	            if ($nb_threads > $numthreads)
		        {
			        $content .= "<br>Pages: ";
			        $nn = ($nb_threads % $numthreads == 0)?($nb_threads / $numthreads):($nb_threads / $numthreads + 1);
		
			        for ($j=1; $j<=$nn; $j++)
				    
                    		    if (($j == $_GET['page']) || ($j==1 && !$_GET['page']))
					    $content .= $j."&nbsp;";
				    else
					    $content .= "<a href=index.php?module=forum&show=section&id=".addslashes($_GET['id']).
					    "&page=".$j.">".$j."</a>&nbsp;";
		         }
              }
              else
	             $content .= $mlang['016'];

                 
              if ($_SESSION['username'])
              {
                  // Show "Post a new Thread" block
	              $content .= "<br><form action='index.php?module=forum' method=post>";
                  $content .= "<h3>".$mlang['008']."</h3>".$mlang['010']."<input type=text name=title size=60>";
                  $content .= "<br>".$mlang['012']."<br><textarea name=contents rows=8 cols=70></textarea><br>";
                  $content .= "<br><input type=submit value=".$mlang['013'].">";
                  $content .= "<input type=hidden name=id value=".addslashes($_GET['id'])."><input type=hidden name=post value='thread'></form>";
              }
        }
        else
            $content .= $mlang['014'];
        }

    elseif (@$_GET['show']=='thread' && $_GET['id'])
    {
 	    // Show a thread and its replies (child-threads)
	    
	    $thread = query("SELECT * FROM `".PREFIX."threads` WHERE id=".addslashes($_GET['id']), 4);
	    $p_section = query("SELECT * FROM `".PREFIX."sections` WHERE id=".$thread['section_id'], 4);

        if ($thread['id'])
        {
            $nb = query("SELECT count(*) FROM `".PREFIX."threads`", 2);
	        $content .= "<p><font size='3'><b><a href='index.php?module=forum'>".$mlang['000'].
                                      "</a> &gt; <a href=index.php?module=forum&show=section&id=".$p_section['id'].">".$p_section['name']."</a>".
                                      " &gt; ".$thread['title']."</b></font></p>"; 
                
	        $smarty->assign("title", $thread['title']);
	        $smarty->assign("author", $thread['author']);
	        $smarty->assign("date", $thread['date']);
	        $smarty->assign("contents", $thread['contents']);
	        $content .= $smarty->fetch('modules/thread.tpl')."<br>";
               
	   
	    // Now show the replies       
	    $numthreads = query("select numthreads from `".PREFIX."settings`", 2);
            if (!$_GET['page'])
		   $limit = " LIMIT ".$numthreads;
	    else
		   $limit = " LIMIT ".(($_GET['page']-1)*$numthreads).",".$numthreads;

	    $nb = query("SELECT COUNT(*) FROM `".PREFIX."threads` WHERE parent=".addslashes($_GET['id']), 2);
            $allreplies = query("SELECT * FROM `".PREFIX."threads` WHERE parent=".addslashes($_GET['id']).$limit, 1);
        
	    if ($nb>0)
            {
	          while ($thread = mysql_fetch_array($allreplies))
	          { 
		        $smarty->assign("title", $thread['title']);
		        $smarty->assign("author", $thread['author']);
		        $smarty->assign("date", $thread['date']);
		        $smarty->assign("contents", $thread['contents']);
		        $content .= $smarty->fetch('modules/thread.tpl');
	          }
            
	      if ($nb > $numthreads)
	      {
		   $content .= "<br>Pages: ";
		   $nn = ($nb % $numthreads == 0)?($nb / $numthreads):($nb / $numthreads + 1);
		
		   for ($j=1; $j<=$nn; $j++)
			  if (($j == $_GET['page']) || ($j==1 && !$_GET['page']))
				  $content .= $j."&nbsp;";
			  else
				  $content .= "<a href=index.php?module=forum&show=thread&id=".addslashes($_GET['id']).
				  "&page=".$j.">".$j."</a>&nbsp;";
	      }
	    }
        
        if ($_SESSION['username'])
        {    
	        // Show "Post a new Reply" block
        
            $content .= "<br><form action='index.php?module=forum&show=thread&id=".addslashes($_GET['id'])."' method=post>";
            $content .= "<h3>".$mlang['009']."</h3>".$mlang['010']."<input type=text name=title size=60>";
            $content .= "<br>".$mlang['012']."<br><textarea name=contents rows=8 cols=70></textarea><br>";
            $content .= "<br><input type=submit value=".$mlang['013'].">";
            $content .= "<input type=hidden name=id value=".addslashes($_GET['id'])."><input type=hidden name=post value='reply'></form>";
        }
        }
        else
	        $content .= $mlang['015'];
    }

    else 
    {  
        // Show Forum Sections
        
        $content .= "<table width='100%'><tr>"; 
        $content .= "<td width='80%' class='title'><b>".$mlang['001']."</b></td>";
        $content .= "<td width='20%' class='title'><b><center>".$mlang['002']."</center></b></td></tr></table>";   

        $nb_sections = query("SELECT count(*) FROM `phpecms_sections`", 2); 
	    $allsections = query("SELECT * FROM `".PREFIX."sections`", 1);

        if ($nb_sections>0)
        {
          for ($i=0; $i<$nb_sections; $i++)
          {
	        $sections = mysql_fetch_array($allsections); 
	        $smarty->assign ( "name" , $sections['name']);
            $smarty->assign ( "link" , "index.php?module=forum&show=section&id=".$sections['id']);
            $smarty->assign ( "desc" , $sections['desc']);
	        $smarty->assign( "lastdate", $sections['lastdate']);
            $content .= $smarty->fetch ('modules/forum.tpl');  
           } 
        }
        else
	      $content .= $mlang['003'];    
    } 

    return $content;
}

echo show_forum();

?>
Return current item: PHPEcho CMS