<?php
/*
+===========================================+
|      ArabPortal V2.2.x Copyright © 2009   |
|   -------------------------------------   |
|                     BY                    |
|              Arab Portal Team             |
|   -------------------------------------   |
|      Web: http://www.ArabPortal.Info      |
|   -------------------------------------   |
|  Last Updated: 25/08/2008 Time: 03:35 AM  |
+===========================================+
*/

include('global.php');
$apt->GroupAllow('view_site');
$cat_img = "newnews.gif";
$fast_bbcode = 1;
$thumb_width = $apt->getsettings("thumb_width");
if($thumb_width <= 0)$thumb_width = 100;

//---------------------------------------------------
// menu
//---------------------------------------------------
$menu = new menu;

if($apt->getsettings('use_adsin_news') == 'no'){
unset($ads_head,$ads_foot);
}

if($apt->get['action']=="")
{
    $apt->head(LANG_TITLE_NEWS);
    $newsColcount    = intval($apt->getsettings("newsColcount"));
    $getImageCat = $apt->getImageCat();
    $countCat_array = $apt->allcountCat(1);
    $middle_menu =  $menu->middle_menu();

    if($newsColcount < 1 ) $newsColcount = 1;
    $index_middle = $apt->table_cat_module(LANG_TITLE_NEWS);
    $result = $apt->query("SELECT * FROM rafia_cat WHERE catType='1' and subcat=0 ORDER BY ordercat ASC");
     $index_middle .= "<center><table border='0' width='100%' align='center' cellpadding='".$newsColcount."'><tr>";
    while($row = $apt->dbarray($result))
    {
        @extract($row);
        $cat_image     =  $getImageCat[$id];
        $title         =  $apt->format_data_out($title);
        $dsc           =  $apt->format_data_out($dsc);
        $numrows       =  $countopic + $apt->countcat($id,'countopic');
        $numcomment    =  $countcomm + $apt->countcat($id,'countcomm');
        $tdwidth =  100/$newsColcount;
        $index_middle .= "<td align=\"center\" width=\"".$tdwidth."%\"  valign=\"top\">";
         eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_cat' ) . "\";");
         $index_middle .= "</td>";
         $count++;
         if ($count ==  $newsColcount)
         {
             $index_middle .= "</tr>";
             $count = 0;
         }
    }
    $index_middle .= "</tr></table><br>";
    $menu->menuid  =  $apt->getsettings("news_menuid");
    $right_menu    =  $menu->_menu(1);
    

    if($apt->getsettings("news_left_menu"))
    {
        $left_menu     =  $menu->_menu(2);
    }
    $apt->html_Output($left_menu);
}
else if ($apt->get['action']=="byuser")
{
    checkcookie();
    $userid = $apt->setid('userid');
    $perpagelist       = $apt->getsettings("newsperpagelist");
    $perpage_comment   = $apt->getsettings("newsperpagecomment");
    $apt->head(LANG_TITLE_NEWS."->".$title);
    $result = $apt->query ("SELECT * FROM rafia_news
                                            WHERE userid='$userid'
                                            AND allow='yes'
                                            ORDER BY timestamp DESC
                                            LIMIT $start,$perpagelist");
    while($row = $apt->dbarray($result))
    {
        @extract($row);
        $postid    =  $id ;
        $numrows       =  $apt->dbnumquery("rafia_comment","news_id =$postid","id");
        $apt->numrows = $numrows;
        $pagenum   =  $apt->pagenumlist($perpage_comment,$postid,"news.php");
        $news_head =  $apt->rafia_code($news_head);
        $date      =  $apt->Hijri($date_time)." ".$apt->gettime($date_time);
        $title     =  $apt->format_data_out($title);
        $name      =  $apt->format_data_out($name);

            if($apt->getsettings('html_links')=='yes'){
            $news_link = "news_view_".$id.".html";
            }else{
            $news_link = "news.php?action=view&id=".$id;
            }

        eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_topic_list' ) . "\";");
    }
    $apt->numrows = $apt->dbnumquery("rafia_news","allow='yes' and userid='$userid'","id");
    $pagenum        = $apt->pagenum($perpagelist,"byuser&userid=$userid");
    if($pagenum)
    {
        eval("\$list_pagenum = \" " . $apt->gettemplate ( 'news_list_pagenum' ) . "\";");
        $replace       = "<img border=\"0\" src=\"themes/$themepath/$cat_img\" align=\"left\">";
        $list_pagenum  = eregi_replace($replace,"",$list_pagenum);
        $index_middle  .= $list_pagenum;
    }
    $menu->menuid =  $apt->getsettings("news_menuid");
    $right_menu   =  $menu->_menu(1);
    
    if($apt->getsettings("news_left_menu"))
    $left_menu    =  $menu->_menu(2);
    $apt->html_Output($left_menu);
}
//---------------------------------------------------
/*+=============== عرض الخبر =================+ */
//---------------------------------------------------
else if ($apt->get['action']=="view")
{
    $id = $apt->setid('id');
    if (( $apt->cookie['cgroup'] == $apt->a_g) || ( $apt->cookie['cgroup'] == $apt->s_g) || ( $apt->cookie['cgroup'] == $apt->m_g))
    {
       $row = $apt->dbfetch("SELECT rafia_news.*,rafia_users.userid,rafia_users.username,
                                rafia_users.datetime,rafia_users.allposts,rafia_users.signature,
                                rafia_users.homepage,rafia_users.avatar,rafia_users.usergroup FROM rafia_news,rafia_users
                                WHERE id='$id' LIMIT 1");
    }
    else
    {
        $row = $apt->dbfetch("SELECT rafia_news.*,rafia_users.userid,rafia_users.username,
                                 rafia_users.datetime,rafia_users.allposts,rafia_users.signature,
                                 rafia_users.homepage,rafia_users.avatar,rafia_users.usergroup FROM rafia_news,rafia_users
                                 WHERE allow='yes' AND id='$id' LIMIT 1");
    }
    if(empty($row))$apt->errmsg(LANG_ERROR_URL);
    @extract($row);
    if(!$apt->catgroup('groupview'))
    {
        $apt->head(LANG_TITLE_LOG_IN);
        eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
        $apt->foot($pageft);
        exit;
    }

    $title         =  $apt->format_data_out($title);
    $news_head     = $apt->rep_words($news_head);
    $news_head     =  $apt->rafia_code($news_head,10000,$ues_editor);

    include_once('func/ArAutoSummarize.class.php');
    $Summarize = new ArAutoSummarize();
    $pagekey  = $Summarize->getMetaKeywords($news_head, 20);
    $pagedesc = $Summarize->doRateSummarize($news_head, 20,'');
    $apt->head($title,0,$pagekey,$pagedesc);

    $post          = $apt->rep_words ($post);
    $post          =  $apt->rafia_code($post,10000,$ues_editor);

    $apt->query("UPDATE rafia_news SET reader = reader+1 WHERE id = '$id'");
    $apt->usereaderp("news_id",$id);
    $postid = $id;
    if($catmig == 0)
    {
        $newsimage = $apt->getnewsimage($id,$uploadfile);
    }
    else
    {
        $getImageCat = $apt->getImageCat();
        $newsimage  = $getImageCat[$cat_id];
    }

    $topic_nav = $apt->next_old_post("SELECT id FROM rafia_news WHERE allow='yes' and cat_id='$cat_id' and timestamp < '$timestamp'  ORDER BY timestamp DESC LIMIT 1");
    $topic_nav.= $apt->next_new_post("SELECT id FROM rafia_news WHERE allow='yes' and cat_id='$cat_id' and timestamp > '$timestamp'  ORDER BY timestamp LIMIT 1");
    $index_middle .= $apt->table_cat_link(LANG_TITLE_NEWS,$cat_id,$title);
    if ($start == 0)
    {
        if(( $userid == 0) || ($userid == $apt->Guestid))
        {
             $usertitle    =  $username;
             $username     =  $apt->format_data_out($name);
             $datetime     =  $apt->Hijri($datetime);
             $allposts     =  1;
        }
        else
        {
            $usertitles    =  explode("-", $apt->userRating($allposts));
		if( ($usergroup == $apt->a_g) || ($usergroup == $apt->m_g)){
            $usertitle     =  $apt->usergroup($usergroup);
		}else{
            $usertitle     =  $usertitles[1];
		}
            $userimgtitle  =  $usertitles[0];
            $username      =  $apt->format_data_out($username);
            $homepage      =  $apt->addToURL ($homepage);
            $signature     =  $apt->format_data_out ($signature);
            $datetime      =  $apt->Hijri($datetime);
            if($avatar == 0){
			    $avatar_pic = '';
            }else{
		    $avatarfile = $apt->upload_path."/".$userid.".".'avatar';
		    if(file_exists($avatarfile)){
		       $avatar_pic = "<img src=members.php?action=image&userid=".$userid."><br>";
		    }else{
		       $avatar_pic = '';
		    }
             }
        }

        if(isset($apt->get[highlight]))
        {
            $news_head     = $apt->highlight_words($news_head);
            $post          = $apt->highlight_words($post);
        }
        $date          =  $apt->Hijri($date_time)." ".$apt->gettime($date_time);
        $c_comment     =  $apt->dbnumquery("rafia_comment","news_id ='$id' AND allow='yes'","id");
        if (!$apt->checkcadmincat($cat_id)){$edit_link = '';}else{
           $edit_link = "<a href=\"$PHP_SELF?action=edit&id=$id\"><img border=\"0\" src=\"themes/$apt->themepath/edit.gif\"></a>";
        }
        eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_table' ) . "\";");
    }
      if($close == '0')
        {
            $isclose    = "close&cat_id=$cat_id&newsid";
            $replay_img  = "replay.gif";
        }
        else
        {
             $isclose    = "open&cat_id=$cat_id&newsid";
            $replay_img  = "close.gif";
        }
    
    if ( $apt->checkcadmincat($cat_id,'edit_news'))
    {
        if($sticky == '0')
        {
            $issticky = "sticky&cat_id=$cat_id&newsid";
        }
        else
        {
            $issticky = "unsticky&cat_id=$cat_id&newsid";
        }
        $index_middle .= $apt->adminJump ("edit&id","comment&cat_id=$cat_id&id",$issticky,$isclose);
}

   $able_addreply = $apt->CheckGroup('add_news_c');
   	$add_new_post_link = "<a href=\"$PHP_SELF?action=add&cat_id=$cat_id\"><img border='0' src='themes/$themepath/$cat_img'></a>";
   if($apt->cat_group_check('add_news','groupost'))$add_new_post = $add_new_post_link; else $add_new_post = '';
   if($able_addreply==1) $add_new_reply = "<a href=\"$PHP_SELF?action=addcomment&id=$postid\"><img border='0' src='themes/$themepath/$replay_img'></a>";

   $add_cat_id = $cat_id;

    if($c_comment > 0)
    {
        $perpage_comment   = $apt->getsettings("newsperpagecomment");
         $result = $apt->query ("SELECT rafia_comment.* ,rafia_users.userid,rafia_users.username,
                                   rafia_users.datetime,rafia_users.allposts,rafia_users.signature,
                                   rafia_users.homepage,rafia_users.avatar,rafia_users.usergroup
                                   FROM rafia_comment,rafia_users
                                   WHERE news_id='$id'
                                   AND rafia_comment.userid = rafia_users.userid
                                   AND allow='yes' ORDER BY id ASC
                                   LIMIT $start,$perpage_comment");
         $apt->numrows = $apt->dbnumquery("rafia_comment","news_id='$id' AND allow='yes'","id");
         $pagenum = $apt->pagenum($perpage_comment,"view&id=$id");
         eval("\$index_middle .=\" " . $apt->gettemplate ( 'post_tools' ) . "\";");
         while($row =$apt->dbarray($result))
         {
             @extract($row);
             if(($userid == 0 ) || ($userid == $apt->Guestid ))
             {
                 $usertitle    =  $username;
                 $username     =  $apt->format_data_out($name);
                 $datetime     =  $apt->Hijri($date_time);
                 $allposts     =  1;
             }
             else
             {
            $usertitles    =  explode("-", $apt->userRating($allposts));
		if( ($usergroup == $apt->a_g) || ($usergroup == $apt->m_g)){
            $usertitle     =  $apt->usergroup($usergroup);
		}else{
            $usertitle     =  $usertitles[1];
		}
            $userimgtitle  =  $usertitles[0];
            $username      =  $apt->format_data_out($username);
            $datetime      =  $apt->Hijri($datetime);
            $signature =  $apt->format_data_out($signature);
             }
             $title         =  $apt->format_data_out($title);
             $comment       =  $apt->rep_words($comment);
             $comment       =  $apt->rafia_code($comment);

             $date          =  $apt->Hijri($timestamp)." ".$apt->gettime($timestamp);
             if ($apt->CheckGroup('download_news_file'))
             {
                 $upload_file    =  $apt->getuploadfile($uploadfile);
             }else{
                 $upload_file    =  "<p><font color=\"#FF0000\" face=\"Windows UI\">".LANG_MSG_NOT_VIEW_ATTACHMENTS."</font></p>";
             }
             //  Added (view_news_img) by Myrosy
             if ($apt->CheckGroup('view_news_img'))
             {
                 $upload_file    =  $apt->getuploadfile($uploadfile);
             }else{
                 $upload_file    =  "<p><font color=\"#FF0000\" face=\"Windows UI\">".LANG_MSG_NOT_VIEW."</font></p>";
             }
            // end Myrosy
             $apt->color($color);


		if($avatar == 0){
		    $avatar_pic = '';
            }else{
		    $avatarfile = $apt->upload_path."/".$userid.".".'avatar';
		    if(file_exists($avatarfile)){
		       $avatar_pic = "<img src=members.php?action=image&userid=".$userid."><br>";
		    }else{
		       $avatar_pic = '';
		    }
             }

        if (!$apt->checkcadmincat($cat_id)){$edit_link = '';}else{
           $Cedit_link = "<a href=\"$PHP_SELF?action=editcomment&id=$id\"><img border=\"0\" src=\"themes/$apt->themepath/edit.gif\"></a>";
        }

            eval("\$index_middle .= \"" . $apt->gettemplate ( 'news_comment_table' ) . "\";");
            unset($upload_file);
        }

      eval("\$index_middle .= \" " . $apt->gettemplate ( 'post_tools' ) . "\";");
    }
    else
    {
        eval("\$index_middle .= \" " . $apt->gettemplate ( 'post_tools' ) . "\";");
    }

       if(($apt->CheckGroup('add_news_c'))&& ($close == '0'))
       {
         $fo = new form;
         $captcha = rand(1000,9999);
         $fo->cap = $captcha;
         $_SESSION['memccode']= $captcha;

           $index_middle .= $fo->nfastreplay(intval($apt->get['id']),$fast_bbcode);
       }

    $index_middle .=  $apt->listJumpf("$PHP_SELF?action=list&cat_id=",1);
    $menu->menuid =  $apt->getsettings("news_menuid");
    $right_menu   =  $menu->_menu(1);

    if($apt->getsettings("news_left_menu"))    $left_menu    =  $menu->_menu(2);
    $apt->html_Output($left_menu);
}
else if ($apt->get['action']=="image")
{
     $id = $apt->setid('id');
     //if ($apt->CheckGroup('download_forum_file'))  // Removed by Myrosy
    if ($apt->CheckGroup('view_news_img'))   // Added by Myrosy
    {
        $result = $apt->query("SELECT * FROM rafia_upload WHERE upid='$id'");
        if($apt->dbnumrows($result)>0)
        {
            $rowfile = $apt->dbarray($result);
            @extract($rowfile);
            $pathfile = $apt->upload_path."/".$uppostid.".".$upcat;
            header ("Content-type: $uptypes");
            $open = @fopen($pathfile,r);
            $data = @fread($open,@filesize($pathfile));
            @fclose($open);
            print($data);
            $apt->query("UPDATE rafia_upload SET upclicks = upclicks+1 WHERE upid = '$id'");
        }
    }
    else
    {
         $apt->errmsg (LANG_MSG_NOT_VIEW);  // Added by Myrosy
    }
}
else if ($apt->get['action'] == 'code')
{
    $apt->showsource();
    exit;
}
else if ($apt->get['action'] == 'down')
{
     $id = $apt->setid('id');
    if ($apt->CheckGroup('download_forum_file'))
    {
        $result = $apt->query("SELECT * FROM rafia_upload
                                      WHERE upid='$id'");
     if($apt->dbnumrows($result)>0)
     {
          $rowfile = $apt->dbarray($result);
          @extract($rowfile);
          $pathfile = $apt->upload_path."/".$uppostid.".".$upcat;
          $download = new downloadfile($pathfile,$upname,$upsize);
          if(!$download->download())
          {
              $apt->errmsg(LANG_ERROR_URL);
          }
          else
          {
               $apt->query("UPDATE rafia_upload SET upclicks = upclicks+1 WHERE upid = '$id'");
          }
    }
    }else{
          $apt->errmsg (LANG_MSG_NOT_VIEW_ATTACHMENTS);
    }
}
/*+=============== قائمة المواضيع في الاقسام =================+ */
else if ($apt->get['action']=="list")
{
     $cat_id = $apt->setid('cat_id');
     $countCat_array = $apt->allcountCat(1);
     $result = $apt->query("SELECT * FROM rafia_cat WHERE id='$cat_id' ORDER BY id DESC");
     $row    = $apt->dbarray($result);
     @extract($row);
     $apt->head(LANG_TITLE_NEWS."->".$title);
     $index_middle   = $apt->table_cat_link(LANG_TITLE_NEWS) ;
     $title         =  $apt->format_data_out($title);
     $dscin         =  $apt->format_data_out($dscin);

		$add_new_post_link = "<a href=\"$PHP_SELF?action=add&cat_id=$cat_id\"><img border='0' src='themes/$themepath/$cat_img'></a>";
   	if($apt->cat_group_check('add_news','groupost'))$add_new_post = $add_new_post_link; else $add_new_post = '';

    $result = $apt->query("SELECT * FROM rafia_cat WHERE catType='1' and subcat ='$cat_id' ORDER BY ordercat ASC");

    if($apt->dbnumrows($result)>0)
    {
        while($row = $apt->dbarray($result))
        {
            @extract($row);
            $title         = $apt->format_data_out($title);
            $dsc           = $apt->format_data_out($dsc);
            $numrows       = $countopic + $apt->countcat($id,'countopic');
            $numcomment    = $countcomm + $apt->countcat($id,'countcomm');
            eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_cat' ) . "\";");
        }
    }


    $perpagelist       = $apt->getsettings("newsperpagelist");
    $perpage_comment   = $apt->getsettings("newsperpagecomment");
      eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_cat_tools' ) . "\";");

      $order_dir = DESC;

     $order_by = $apt->getsettings('news_order_by');

    $getImageCat = $apt->getImageCat();
    if ($start == 0)
    {
        $result = $apt->query ("SELECT rafia_news.*,COUNT(rafia_comment.news_id) as numrows
                                FROM rafia_news LEFT JOIN rafia_comment
                                ON  rafia_news.id = rafia_comment.news_id
                                WHERE rafia_news.cat_id='$cat_id'
                                AND rafia_news.allow = 'yes'
                                AND rafia_news.sticky='1'
                                GROUP BY rafia_news.id
                                ORDER BY rafia_news.".$order_by." $order_dir");
                                
       while($row = $apt->dbarray($result))
       {
            @extract($row);
            $newsimage     = '';
            $title         =  $apt->format_data_out($title);
            $news_head    =  $apt->rafia_code($news_head,600);
            $name          =  $apt->format_data_out($name);
            $apt->numrows  =  $numrows ;
            $pagenum       =  $apt->pagenumlist ($perpage_comment,$id);
            $title         =  $apt->title_cut(LANG_MSG_STACKED." : ".$title,$apt->getsettings("max_title_cut"));
            $date          =  $apt->Hijri($date_time)." ".$apt->gettime($date_time);

            if($catmig == 0)
            {
                if($apt->getsettings("use_gd_thumb") == 'yes'){$usegd=true;}else{$usegd=false;}
                $newsimage = $apt->getnewsimage($id,$uploadfile,$usegd);
            }
            else
            {
                $newsimage  = $getImageCat[$cat_id];
            }
            if($apt->getsettings('html_links')=='yes'){
            $news_link = "news_view_".$id.".html";
            }else{
            $news_link = "news.php?action=view&id=".$id;
            }

            eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_topic_list' ) . "\";");
       }
    }
    $result = $apt->query ("SELECT rafia_news.*,COUNT(rafia_comment.news_id) as numrows
                                FROM rafia_news LEFT JOIN rafia_comment
                                ON  rafia_news.id = rafia_comment.news_id
                                WHERE rafia_news.cat_id='$cat_id'
                                AND rafia_news.allow = 'yes'
                                AND rafia_news.sticky!='1'
                                GROUP BY rafia_news.id
                                ORDER BY rafia_news.".$order_by." $order_dir
                                LIMIT $start,$perpagelist");
                                
    while($row = $apt->dbarray($result))
    {
        @extract($row);
        $newsimage     = '';
        $title         =  $apt->format_data_out($title);
        $news_head    =  $apt->rafia_code($news_head,600);
        $name          =  $apt->format_data_out($name);
        $apt->numrows  =  $numrows ;
        $pagenum       =  $apt->pagenumlist ($perpage_comment,$id);
        $title         =  $apt->title_cut($title,$apt->getsettings("max_title_cut"));
        $date          =  $apt->Hijri($date_time)." ".$apt->gettime($date_time);
        if($catmig == 0)
        {
                if($apt->getsettings("use_gd_thumb") == 'yes'){$usegd=true;}else{$usegd=false;}
                $newsimage = $apt->getnewsimage($id,$uploadfile,$usegd);
        }
        else
        {
           $newsimage  = $getImageCat[$cat_id];
        }
            if($apt->getsettings('html_links')=='yes'){
            $news_link = "news_view_".$id.".html";
            }else{
            $news_link = "news.php?action=view&id=".$id;
            }


        eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_topic_list' ) . "\";");
    }
    $apt->numrows = $apt->dbnumquery("rafia_news","allow='yes' and cat_id='$cat_id'","id");
    $pagenum        = $apt->pagenum($perpagelist,"list&cat_id=$cat_id");
    if($pagenum)
    {
        eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_list_pagenum' ) . "\";");
    }
    $index_middle .=  $apt->listJumpf("$PHP_SELF?action=list&cat_id=",1);
    $menu->menuid =  $apt->getsettings("news_menuid");
    $right_menu   =  $menu->_menu(1);
    
    if($apt->getsettings("news_left_menu"))
    $left_menu    =  $menu->_menu(2);
    $apt->html_Output($left_menu);
}
/*+=============== إضافة الاخبار =================+ */
else if($apt->get['action']=="add")
{
    
    if(isset($apt->get['cat_id']))
    $cat_id = $apt->setid('cat_id');

    if(! $apt->cat_group_check('add_news','groupost'))
    {
        $apt->head(LANG_TITLE_LOG_IN);
        eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
        $apt->foot($pageft);
        exit;
   }

	  $chose_func = "<tr><td align='right' nowrap='' class='info_bar'>نوع العملية :</td>
	  <td align='right' width='100%'>
	  <input type='radio' name='doshow' value='0' checked=''> حفظ الموضوع &nbsp;
	  <input type='radio' name='doshow' value='1'>عرض الموضوع</td></tr>";
    if(($apt->CheckGroup('ues_editor')) and ($apt->getsettings("ues_editor")==1))
    {
        $apt->ues_editor = '1';
        $chose_func = "";
    }
    $apt->head(LANG_TITLE_ADD_NEWS,1);
    $index_middle = $apt->table_cat_link(LANG_TITLE_NEWS,$apt->get['cat_id']);
         unset($usernamef,$useridf,$selectform,$form_code,$editor,$hiddencat,$main_n,$fileupload,$imgupload);	
         $fo = new form;
         $fo->counthead = $apt->getsettings('txtcount1');
         $fo->countpost = $apt->getsettings('txtcount2');
         $counthead = $fo->counthead;
	   $cap = $apt->getcaptcha();
         $countpost = $fo->countpost;
	      if( $apt->cookie['cid'] == $apt->Guestid)
            {
                $usernamef = $fo->inputform  ($apt->lang_form['28'],"text","name","","");
                $useridf = $fo->hiddenform  ("userid", '0');
            }

            if(!isset($apt->get['cat_id']))
            {
                $selectform =  $fo->selectform ("1");
            }else{
                $selectform =  $fo->hiddenform ("cat_id",$cat_id);
		}
            $txtcount =  $fo->txtcount();
            $txtcounth =  $fo->txtcounth();

            if($apt->ues_editor == 0)
            {
                  eval("\$form_code = \"" . $apt->gettemplate ( 'form_code' ) . "\";");
            }
            else
            {
                $editor =  $apt->editor();
                $fo->use_smiles =0;
            }
            if(isset($apt->get['cat_id']))
            {
                $hiddencat =  $fo->hiddenform("cat_id", $apt->get['cat_id']);
            }
		if ($apt->cookie['cgroup'] == $apt->a_g){
            $main_n = $fo->yesorno("خـبـر رئـيـسـي ؟","main", "0");}
            if(($apt->getsettings('imgallow') == 'yes') ||($apt->CheckGroup('add_news_img')))
            {
                $fileupload =  $fo->inputform  ($apt->lang_form['26'],"file","phpfile","");
                $imgupload = $fo->inputimgform  ($apt->lang_form['27']);
            }

    //$index_middle = $fo->news_form('add');
    eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_add' ) . "\";");
    
    $apt->html_Output('',0);
}
/*+=============== أدراج الخبر لقاعدة البينات=================+ */
else if($apt->get['action']=="insert")
{
     if(! $apt->cat_group_check('add_news','groupost'))
     {
        $apt->head(LANG_TITLE_LOG_IN);
        eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
        $apt->foot($pageft);
        exit;
    }

     @extract($_POST);
    if($doshow==1){
    $apt->head(LANG_TITLE_ADD_NEWS);
    $index_middle = $apt->table_cat_link(LANG_TITLE_NEWS,$apt->get['cat_id']);
    if(($apt->CheckGroup('ues_editor')) and ($apt->getsettings("ues_editor")==1)){
        $apt->ues_editor = 1;
        $ues_editor = 1;
    }

        $news_head_s     = $apt->rep_words($post_head);
        $news_head_s     = $apt->rafia_code($news_head_s,10000,$ues_editor);
        $post_s          = $apt->rep_words ($post);
        $post_s          = $apt->rafia_code($post_s,10000,$ues_editor);

         $pre_post =  $news_head_s . $post_s;

	   $title = stripslashes($title);
	   $post_head = stripslashes($post_head);
	   $post = stripslashes($post);
         $fo = new form;
         $fo->counthead = $apt->getsettings('txtcount1');
         $fo->countpost = $apt->getsettings('txtcount2');
         $counthead = $fo->counthead;
	   $cap = $apt->getcaptcha();
         $countpost = $fo->countpost;
	      if( $apt->cookie['cid'] == $apt->Guestid)
            {
                $usernamef = $fo->inputform  ($apt->lang_form['28'],"text","name","","");
                $useridf = $fo->hiddenform  ("userid", '0');
            }

            if(!isset($cat_id))
            {
                $selectform =  $fo->selectform ("1");
            }else{
                $selectform =  $fo->hiddenform ("cat_id",$cat_id);
		}
            $txtcount =  $fo->txtcount();
            $txtcounth =  $fo->txtcounth();

            if($apt->ues_editor == 0)
            {
                  eval("\$form_code = \"" . $apt->gettemplate ( 'form_code' ) . "\";");
            }
            else
            {
                $editor =  $apt->editor();
                $fo->use_smiles =0;
            }
            if(isset($apt->get['cat_id']))
            {
                $hiddencat =  $fo->hiddenform("cat_id", $apt->get['cat_id']);
            }
		if ($apt->cookie['cgroup'] == $apt->a_g){
            $main_n = $fo->yesorno("خـبـر رئـيـسـي ؟","main", "0");}
            if(($apt->getsettings('imgallow') == 'yes') ||($apt->CheckGroup('add_news_img')))
            {
                $fileupload =  $fo->inputform  ($apt->lang_form['26'],"file","phpfile","");
                $imgupload = $fo->inputimgform  ($apt->lang_form['27']);
            }

    eval("\$index_middle .= \" " . $apt->gettemplate ( 'news_show' ) . "\";");
    
    $apt->html_Output('',0);

    }else{

    if($CONF['mach_ip'] == 1){
    $this_url = explode('/',$_SERVER['HTTP_HOST']);
    $reff_url = explode('/',$_SERVER['HTTP_REFERER']);

    if($this_url[0] !== $reff_url[2])
    $apt->bodymsg('عفوا ... لا يمكنك اضافة خبر من خارج الموقع',"news.php?action=add&cat_id=$cat_id");
    }

    $cap = $apt->getcaptcha();
    if($spam !== "newsnotspam_$cap"){
    $apt->bodymsg('عفوا ... كود التأكيد غير متطابق',"news.php?action=add&cat_id=$cat_id");
    exit;
    }

   $userid        = $apt->format_data($apt->cookie['cid']);
   $name          = $apt->format_data($name);
   $title         = $apt->format_post($title);
   $cat_id        = $apt->format_data($cat_id);
   $inindex       = $apt->format_data($inindex);
   $inmenu        = $apt->format_data($inmenu);
   if (!$apt->cookie['cgroup'] == $apt->a_g){$main = 0;}else{
   $main          = $apt->format_data($main);}
   $catmig        = $apt->format_data($apt->post[catmig]);
   $imagewidth    = $apt->format_data($imagewidth);
   $imagealign    = $apt->format_data($imagealign);
   
   $arr_post_vars = array($title,
                            $post,
                            $apt->cookie['cname']);
                            
    if($apt->cookie['cid']==0 or $apt->cookie['cid']== $apt->Guestid)
    {
         $name  = $apt->format_data($apt->post['name']);
         if(!isset($apt->post['name']))    $name  = $name."(زائر)";
    }
    else
    {
        $name  = $apt->format_data($apt->cookie['cname']);
    }
     if (!$apt->full($arr_post_vars))
     {
         $apt->errmsg(LANG_ERROR_VALIDATE);
     }
     if (!$apt->txtcounmxs($news_head,$apt->getsettings("txtcount1")))
     {
         $apt->errmsg(LANG_ERROR_LETTER_MAX);
     }
     if (!$apt->txtcounmxs($post,$apt->getsettings("txtcount2")))
     {
         $apt->errmsg(LANG_ERROR_LETTER_MAX);
     }
    if ($apt->CheckGroup('add_news_img'))
    {
        if ((is_uploaded_file($apt->files["phpfile"]['tmp_name']))&&($apt->post['catmig']=='0'))
        {
            $up  = new RaFiaUpload;
            $up->cat ="news";
            $up->uploads = $apt->files["phpfile"];
         if ($up->uploads["size"] > ($apt->getsettings("newsfilemxsize")*1024))
         {
             $apt->errmsg(LANG_ERROR_SIZE_MUST_LESS.$apt->getsettings("newsfilemxsize")." kb");
         }
         if ($up->checkend($apt->getsettings("newsfileupload")) == false)
         {
             $apt->errmsg(LANG_ERROR_FILE_TYPES.$apt->getsettings("newsfileupload"));
         }
      }
 }
    if(($apt->CheckGroup('ues_editor')) and ($apt->getsettings("ues_editor")==1))
    {
        $ues_editor = 1;
    }

        if($ues_editor == 1){
        $news_head	= $apt->format_html_post($post_head);
        $post		= $apt->format_html_post($post);
        }else{
        $news_head	= $apt->format_post($post_head);
        $post     	= $apt->format_post($post);
        }
    $newsallow     = $apt->CheckGroup('stat_news',1);
    $timestamp = $apt->time;
    $Spams  = new Spams();
    if( $Spams->checkSpams() == false )
    {
        $apt->bodymsg(LANG_ERROR_WAIT_SECONDS,"$PHP_SELF?action=list&cat_id=$cat_id");
    }

    $result    = $apt->query("insert into rafia_news  (cat_id,
                                                         title,
                                                         date_time,
                                                         userid,
                                                         name,
                                                         news_head,
                                                         post,
                                                         allow,
                                                         lastuserid,
                                                         timestamp,
                                                         inindex,
                                                         main,
                                                         inmenu,
                                                         catmig,
                                                         ues_editor)
                                                   values
                                                         ('$cat_id',
                                                         '$title',
                                                         '$timestamp',
                                                         '$userid',
                                                         '$name',
                                                         '$news_head',
                                                         '$post',
                                                         '$newsallow',
                                                         '$userid',
                                                         '$timestamp',
                                                         '$inindex',
                                                         '$main',
                                                         '$inmenu',
                                                         '$catmig',
                                                         '$ues_editor')");
    if ($result)
    {
        $id = $apt->insertid();
        $Tid = $id;
        if ((is_uploaded_file($apt->files["phpfile"]['tmp_name']))&&($apt->post['catmig']=='0'))
         {
              $up->uploadfile();
              $upid = $apt->insertid();
              $apt->query("update rafia_news set
                             uploadfile  = '$upid'
                             where id = '$id'");
//------------------------------------------
if($apt->getsettings("use_gd_thumb") == 'yes'){
$thefile = $CONF['upload_path'].'/'.$Tid.".news";
$type = GetImageSize($thefile);
$type = $type[mime];

$thethump = $CONF['upload_path'].'/'.$Tid.".newsthumb";

if($type=='image/gif'){
$image = ImageCreateFromGIF($thefile);
}elseif($type=='image/png'){
$image = ImageCreateFromPNG($thefile);
}else{
$image = ImageCreateFromJPEG($thefile);
}

$width = imagesx($image) ;
$height = imagesy($image) ;
if($width >= $new_width){
$new_width = $width;
$new_height = $height;
}else{$new_width = $thumb_width; 
$new_height = ($new_width * $height) / $width ;}
if (function_exists('ImagecreateTrueColor')){
$thumb = ImageCreateTrueColor($new_width,$new_height);
ImageCopyResampled($thumb,$image,0,0,0,0,$new_width,$new_height,$width,$height);
}else{
$thumb = ImageCreate($new_width,$new_height);
ImageCopyResized($thumb,$image,0,0,0,0,$new_width,$new_height,$width,$height);
}
ImageJPEG($thumb, $thethump);
imagedestroy($image);
}
//-----------------------------------------

        }
        if($main == 1){$apt->query("update rafia_news set main = '0' where id != '$id'");}
        $apt->query("UPDATE rafia_cat SET countopic=countopic+1, lastpostid ='$id' WHERE id = '$cat_id'");
	  $Counter->increment('newsCount');  

         $mail = new email;
         $mail->send_to_moderate($cat_id);
        if($userid >0 && $userid != $apt->Guestid)
        {
             if(($H == 1) && ($userid > 0 ))
            {
                $apt->query("insert into rafia_alert (news_id, userid) values ('$id', '$userid')");
            }
            $apt->query("UPDATE rafia_users SET allposts = allposts+1 , lastadd='$timestamp' WHERE userid = '$userid'");
         }
        if ($newsallow != "yes")
        {
            $url = "$PHP_SELF?action=list&cat_id=$cat_id";
            $apt->bodymsg(LANG_MSG_THREAD_HAS_ADDED,$url);
        }
        else
        {
            $url = "$PHP_SELF?action=view&id=$id";
            $apt->bodymsg(LANG_MSG_POST_HAS_BEEN_ADDED,$url);
        }
    }
    else
    {
         $url = "$PHP_SELF?action=list&cat_id=$cat_id";
         $apt->bodymsg(LANG_ERROR_ADD_DB,$url);
    }
    }
}
/*+=============== تحرير الاخبار =================+ */
else if($apt->get['action']=="edit")
{
     $id = $apt->setid('id');
      if(!$apt->CheckGroup('edit_news_own') && !$apt->CheckGroup('edit_news') )
      {
          $apt->errmsg(LANG_ERROR_CAN_NOT_EDIT);
      }

    $result = $apt->query("select * from rafia_news where id='$id'");
    $apt->row = $apt->dbarray($result);
        if ($apt->cookie['cid'] == $apt->row['userid'] ||  $apt->checkcadmincat($apt->row[cat_id]) || $apt->CheckGroup('edit_news'))
        {
            if($apt->row['ues_editor'] == '1')
            {
                $apt->ues_editor = 1;
            }
            $apt->head(LANG_TITLE_EDIT_POST);
            $fo = new form;
            $index_middle = $fo->news_form('edit');
            //$right_menu   =  $menu->_menu(1);

            $apt->html_Output(0,"");
       }
       else
       {
           $apt->errmsg(LANG_ERROR_CAN_NOT_EDIT);
      }
}
/*+=============== تحديث الخبر في قاعدة البينات =================+ */
else if($apt->get['action']=="UN")
{
     @extract($_POST);

     $id = $apt->setid('id');
    if(!$apt->CheckGroup('edit_news_own') && !$apt->CheckGroup('edit_news') )
    {
        $apt->errmsg(LANG_ERROR_CAN_NOT_EDIT);
    }
   $name           = $apt->format_data($name);
   $title          = $apt->format_data(htmlspecialchars($title));
   $cat_id         = $apt->format_data($cat_id);
   $catid          = $apt->format_data($catid);
   $inindex        = $apt->format_data($inindex);
   $inmenu         = $apt->format_data($inmenu);
   if (!$apt->cookie['cgroup'] == $apt->a_g){$main = 0;}else{
   $main           = $apt->format_data($main);}
   $catmig         = $apt->format_data($catmig);
   $imagewidth     = $apt->format_data($imagewidth);
   $imagealign     = $apt->format_data($imagealign);
   $allow          = $apt->format_data($allow);
   $ues_editor     = $apt->format_data($ues_editor);

    $result = $apt->query("select * from rafia_news where id='$id'");
    $apt->row = $apt->dbarray($result);

    if ($apt->cookie['cid'] == $apt->row['userid'] ||  $apt->checkcadmincat($apt->row['cat_id'])  || $apt->CheckGroup('edit_news'))
    {
       if ($apt->post['del'] =='1')
      {
          $apt->delmsg($cat_id,$id);
      }
	$EDT_VARS = array($id,$title,$cat_id,$post);
      if (!$apt->full($EDT_VARS))
      {
          $apt->errmsg(LANG_ERROR_VALIDATE);
      }
    if (!$apt->txtcounmxs($post_head,$apt->getsettings("txtcount1")))
    {
        $apt->errmsg(LANG_ERROR_LETTER_MAX);
    }
    if (!$apt->txtcounmxs($post,$apt->getsettings("txtcount2")))
    {
        $apt->errmsg(LANG_ERROR_LETTER_MAX);
    }
    if($apt->row['ues_editor']==1){
        $news_head	= $apt->format_html_post($post_head);
        $post		= $apt->format_html_post($post);
    }else{
    if(($apt->CheckGroup('ues_editor')) and ($apt->getsettings("ues_editor")==1))
    {
        $news_head	= $apt->format_html_post($post_head);
        $post		= $apt->format_html_post($post);
    }else{
        $news_head	= $apt->format_post($post_head);
        $post     	= $apt->format_post($post);
    }
    }

    if ($editupload =="delete")
    {
        $row = $apt->dbfetch("SELECT * FROM rafia_upload WHERE upid='$uploadfile'");
        @extract($row);
        $filename = $apt->upload_path."/".$uppostid.".".$upcat;
        if(@unlink($filename))
        {
           @unlink($apt->upload_path."/".$uppostid.".".$upcat."thump");
           $apt->query("DELETE FROM rafia_upload WHERE upid='$uploadfile'");
           $apt->query("update rafia_news set uploadfile = '0' where id = '$id'");
        }
    }
    elseif($editupload =="new")
    {
        if ($apt->CheckGroup('add_news_img'))
        {
            if (is_uploaded_file($apt->files["phpfile"]['tmp_name']))
            {
                $up  = new RaFiaUpload;
                $up->cat ="news";
                $up->uploads = $apt->files["phpfile"];
                if ($up->uploads["size"] > ($apt->getsettings("newsfilemxsize")*1024))
                {
                    $apt->errmsg(LANG_ERROR_SIZE_MUST_LESS.$apt->getsettings("newsfilemxsize")." kb");
                }
                if ($up->checkend( $apt->getsettings("newsfileupload")) == false)
                {
                    $apt->errmsg(LANG_ERROR_FILE_TYPES.$apt->getsettings("newsfileupload"));
                }
                $up->uploadfile(1);

if($apt->getsettings("use_gd_thumb") == 'yes'){
$Tid=$id;
$thefile = $CONF['upload_path'].'/'.$Tid.".news";
$type = GetImageSize($thefile);
$type = $type[mime];

$thethump = $CONF['upload_path'].'/'.$Tid.".newsthumb";

if($type=='image/gif'){
$image = ImageCreateFromGIF($thefile);
}elseif($type=='image/png'){
$image = ImageCreateFromPNG($thefile);
}else{
$image = ImageCreateFromJPEG($thefile);
}

$width = imagesx($image) ;
$height = imagesy($image) ;
if($width <= $new_width){
$new_width = $width;
$new_height = $height;
}else{
$new_width = $thumb_width; 
$new_height = ($new_width * $height) / $width ;
}
if (function_exists('ImagecreateTrueColor'))
{
$thumb = ImageCreateTrueColor($new_width,$new_height);
ImageCopyResampled($thumb,$image,0,0,0,0,$new_width,$new_height,$width,$height);
}
else
{
$thumb = ImageCreate($new_width,$new_height);
ImageCopyResized($thumb,$image,0,0,0,0,$new_width,$new_height,$width,$height);
}
ImageJPEG($thumb, $thethump);
imagedestroy($image);
}
//------------------------------------------------
            }
        }
    }
    elseif ((is_uploaded_file($apt->files["phpfile"]['tmp_name']))&&($apt->post['catmig']=='0'))
    {
        if ($apt->CheckGroup('add_news_img'))
        {
            $up  = new RaFiaUpload;
            $up->cat ="news";
            $up->uploads = $apt->files["phpfile"];
            if ($up->uploads["size"] > ($apt->getsettings("newsfilemxsize")*1024))
            {
                $apt->errmsg(LANG_ERROR_SIZE_MUST_LESS.$apt->getsettings("newsfilemxsize")." kb");
            }
            if ($up->checkend($apt->getsettings("newsfileupload")) == false)
            {
                $apt->errmsg(LANG_ERROR_FILE_TYPES.$apt->getsettings("newsfileupload"));
            }
            $up->uploadfile();
            $upid = $apt->insertid();
            $apt->query("update rafia_news set uploadfile  = '$upid' where id = '$id'");
        }
    }
    $cat_id  =  $apt->post['catid'];
    if ( $apt->checkcadmincat($cat_id) )
    {
          $allow   = $apt->adminunset($apt->post['allow']);
          $edit_by = $apt->cookie['cname']."@edit";
          $result  = $apt->query("update rafia_news set title='$title',
                                                          news_head='$news_head',
                                                          post='$post',
                                                          allow='$allow',
                                                          inindex='$inindex',
                                                          inmenu='$inmenu',
                                                          main='$main',
                                                          catmig='$catmig',
                                                          edit_by='$edit_by'
                                                          where id=$id");
          if($apt->post['cat_id'] != $cat_id)
          {
              if ($apt->checkcmodcat('can_move'))
              {
                  $edit_by = $apt->cookie['cname']."@move";
                  $apt->query("update rafia_news set cat_id='".$apt->post['cat_id']."',edit_by='$edit_by' where id=$id");
              }
          }
    }
    else
    {
         $result = $apt->query("update rafia_news set title='$title',
                                                        news_head='$news_head',
                                                        post='$post'
                                                        where id=$id");
    }
    if($main == 1){
    $apt->query("update rafia_news set main = '0' where id != '$id'");}
    $url = "$PHP_SELF?action=view&id=$id";
    if ($result)
    {
       if($apt->post['allow']=='wit')
        $apt->bodymsg(LANG_MSG_YOUR_POST_HAS_EDITED,"$PHP_SELF?action=wait");
        else
        $apt->bodymsg(LANG_MSG_YOUR_POST_HAS_EDITED,$url);
    }
    else
    {
         $apt->bodymsg(LANG_ERROR_ADD_DB,$url);
    }
}
//  Start Added By Myrosy 22/10/2006  
    else
    {
        $apt->head(LANG_ERROR);
        $apt->errmsg(LANG_ERROR_CAN_NOT_EDIT);
    }
}
//  End Added By Myrosy 22/10/2006

/*+===============  =================+ */
else if($apt->get['action']=="addcomment")
{
    $apt->GroupAllow('add_news_c') ;
    $id = $apt->setid('id');
        $result = $apt->query("SELECT * FROM rafia_news WHERE id='$id'");
        $apt->row = $apt->dbarray($result);
        @extract($apt->row);
        if ($close == 1)
        {
            $apt->errmsg(LANG_ERROR_POST_CLOSED);
        }
       $apt->head(LANG_TITLE_ADD_COMMENT);

        if(isset($apt->get[qc]))
        {
            $qc = intval($apt->get[qc]);
            $result = $apt->query("select name,comment from rafia_comment where id='$qc'");
            $row    = $apt->dbarray($result);
            $post_text = "  [QUOTE]الكاتب :".$row['name']."\n".$row['comment']."[/QUOTE] \n";
        }
         $fo = new form;
         $d = '';
           $countpost = $apt->getsettings('txtcount6');
           $smiles =  $fo->smiles();
           $txtcount =  $fo->txtcount();
           if( $apt->cookie['cid'] == $apt->Guestid){
                 $namef = $fo->inputform  ($apt->lang_form['28'],"text","name","","");
            }
		eval("\$form_code = \"" . $apt->gettemplate ( 'form_code' ) . "\";");
           if ($apt->CheckGroup('upload_comment_file'))
           {
                 $formf = $fo->inputform($apt->lang_form['29'],"file","phpfile","");
           }
		eval("\$index_middle = \"" . $apt->gettemplate ( 'news_addcomment' ) . "\";");
           if(!isset($apt->get[qp])&&!isset($apt->get[qc])&& $d == 1)
           {
               $index_middle .=   $apt->iframe();
           }
         $right_menu   =  $menu->_menu(1);
         $apt->html_Output("");
}
/*+===============  =================+ */
else if($apt->get['action']=="vfc")
{
    $apt->head(LANG_TITLE_ADD_COMMENT);
    $id = $apt->setid('id');
     if($apt->dbnumquery("rafia_comment","news_id='$id' AND allow='yes'","id") > 0)
     {
         $perpage_comment   = $apt->getsettings("newsperpagecomment");
         $result = $apt->query("SELECT * FROM rafia_comment
                                                WHERE news_id='$id'
                                                AND allow='yes' ORDER BY id DESC
                                                LIMIT $start,$perpage_comment");
       $apt->numrows = $apt->dbnumquery("rafia_comment","news_id='$id' AND allow='yes'","id");
       print $apt->pagenum($perpage_comment,"vfc&id=$id");
       while($row = $apt->dbarray($result))
       {
           @extract($row);
           $name    =  stripslashes($name);
           $comment =  $apt->rafia_code($comment);
           $date    =  $apt->Hijri($timestamp);
           eval("echo \"".$apt->gettemplate("s_view_topic_comment")."\";");
       }
       $result = $apt->query("SELECT * FROM rafia_news
                                WHERE id='$news_id'");
       $row = $apt->dbarray($result);
       @extract($row);
       $name    = stripslashes($name);
       $comment = $apt->rafia_code($post);
       $date    = $apt->Hijri($date_time);
       if ($start==0)
       {
           eval("echo \"".$apt->gettemplate("s_view_topic_comment")."\";");
       }
       print $apt->pagenum($perpage_comment,"vfc&id=$news_id");
       }
       else
       {
           $result = $apt->query("SELECT * FROM rafia_news WHERE id='$id'");
           $row = $apt->dbarray($result);
           @extract($row);
           $name    = stripslashes($name);
           $comment = $apt->rafia_code($post);
           $date    =  $apt->Hijri($date_time);
           if ($start==0)
           {
               eval("echo \"".$apt->gettemplate("s_view_topic_comment")."\";");
           }
       }
   exit;
}
/*+===============  =================+ */
else if($apt->get['action']=="insertcomment")
{

    $apt->GroupAllow('add_news_c') ;
    @extract($_POST);

    if($CONF['mach_ip'] == 1){
    $this_url = explode('/',$_SERVER['HTTP_HOST']);
    $reff_url = explode('/',$_SERVER['HTTP_REFERER']);

    if($this_url[0] !== $reff_url[2])
    $apt->bodymsg('عفوا ... لا يمكنك اضافة تعليق من خارج الموقع',"index.php");
    }

    if($spam !== 'commenttnotspam'){
    $apt->bodymsg('عفوا ... كود التأكيد غير متطابق',"news.php?action=addcomment&id=$id");
    exit;
    }

    if (!$apt->full($post))
    {
        $apt->errmsg(LANG_ERROR_VALIDATE);
    }
    if (!$apt->txtcounmxs($post,$apt->getsettings('txtcount6')))
    {
        $apt->errmsg(LANG_ERROR_LETTER_MAX);
    }
    $userid     = intval($apt->format_data($apt->cookie['cid']));
    if($apt->cookie['cid']==0 or $apt->cookie['cid']== $apt->Guestid)
    {
         $name  = $apt->format_data($apt->post['name']);
         if(!isset($apt->post['name']))    $name  = $name."(زائر)";
         if (!$apt->checkifues("username",$name))
         {
             $apt->errmsg (LANG_ERROR_USERNAME_EXISTED);
         }
        $name  = $name."(زائر)";
    }
    else
    {
        $name  = $apt->format_data($apt->cookie['cname']);
    }
    $title          = $apt->format_data($title);
    $titlepost      = $apt->format_data($titlepost);
    $comment    = $apt->format_post($post);
    $usesig     = intval($apt->format_post($usesig));
    $cat_id     = intval($apt->format_post($cat_id));
    $post_id    = intval($apt->format_post($post_id));
    $timestamp = time();


    if ($apt->CheckGroup('upload_comment_file'))
    {
        if (is_uploaded_file($apt->files["phpfile"]['tmp_name']))
        {
            $up  = new RaFiaUpload;
            $up->cat ="comment";
            $up->uploads = $apt->files["phpfile"];
            if ($up->uploads["size"] > ($apt->getsettings("commentfilemxsize")*1024))
            {
                $apt->errmsg(LANG_ERROR_SIZE_MUST_LESS.$apt->getsettings("commentfilemxsize")." kb");
            }
            if ($up->checkend( $apt->getsettings("commentfileupload")) == false)
            {
                $apt->errmsg("هذا النوع من الملفات لا يسمح  برفعه للمنتدى <br>");
            }
        }
    }

    $Spams  = new Spams();
    if( $Spams->checkSpams() == false )
    {
        $apt->bodymsg(LANG_ERROR_WAIT_SECONDS,"$PHP_SELF?action=list&cat_id=$cat_id");
    }

    $commallow     = $apt->CheckGroup('allow_comment',1);
    $result = $apt->query("insert into rafia_comment (news_id,
                                                        userid,
                                                        name,
                                                        title,
                                                        comment,
                                                        cat_id,
                                                        timestamp,allow)
                                                  values
                                                        ('$post_id',
                                                        '$userid',
                                                        '$name',
                                                        '$title',
                                                        '$comment',
                                                        '$cat_id',
                                                        '$timestamp','$commallow')");
    if ($result)
    {
        $commentid = $apt->insertid();
        if (is_uploaded_file($apt->files["phpfile"]['tmp_name']))
         {
              $up->uploadfile();
              $upid = $apt->insertid();
              $apt->query("update rafia_comment set
                             uploadfile  = '$upid'
                             where id = '$commentid'");
        }
        $apt->query ("UPDATE rafia_cat SET countcomm=countcomm+1, lastpostid ='$post_id' WHERE id='$cat_id'");
        $apt->query ("UPDATE rafia_news SET c_comment = c_comment+1 , timestamp='$timestamp', lastuserid='$userid' WHERE id = '$post_id' ");
        if($userid >0 and $userid != $apt->Guestid)
        {
            $apt->query ("UPDATE rafia_users SET allposts = allposts+1, lastadd='$timestamp' WHERE userid = '$userid' ");
        }
        $Counter->increment('commentCount');  
        $url  =  $apt->getcommenturl($commentid,news_id);
        $mail = new email;
        $mail->send_to_users('news_id',
                              $userid,
                              $post_id,
                              $url,
                              $H);
        $apt->bodymsg(LANG_MSG_POST_HAS_BEEN_ADDED,$url);
    }
    else
    {
        $url = "news.php?action=view&id=$post_id";
        $apt->bodymsg(LANG_ERROR_ADD_DB,$url);
    }
}
/*+===============  =================+ */
else if($apt->get['action']=="editcomment")
{
      $apt->GroupAllow('add_news_c') ;
      $id = $apt->setid('id');
      if(!$apt->CheckGroup('edit_comment_own') &&  !$apt->CheckGroup('edit_news') )
      {
          $apt->head(LANG_ERROR);
          $apt->errmsg(LANG_ERROR_CAN_NOT_EDIT);
      }
      $result     = $apt->query("select * from rafia_comment where id='$id'");
      $apt->row = $apt->dbarray($result);
      $apt->row['post_id'] = $apt->row['news_id'];
      if ($apt->cookie['cid'] == $apt->row['userid'] ||  $apt->checkcadmincat($apt->row['cat_id']) )
      {
          $apt->head(LANG_TITLE_EDIT_POST);
             $fo = new form;
             $index_middle =$fo->comment_form('edit');
             $right_menu   =  $menu->_menu(1);
             
             $apt->html_Output("");
      }
      else
      {
          $apt->errmsg(LANG_ERROR_CAN_NOT_EDIT);
      }
}
/*+===============  =================+ */
else if($apt->get['action']=="UC")
{
      $id = $apt->setid('id');
      if(!$apt->CheckGroup('edit_comment_own') &&  !$apt->CheckGroup('edit_comment') )
      {
          $apt->errmsg(LANG_ERROR_CAN_NOT_EDIT);
      }
      @extract($_POST);
      if ($apt->post['del'] =='1')
      {
           $cat_id = $apt->post['cat_id'] ;
           $apt->delmsg($cat_id,'',$id);
      }
     if (!$apt->full($post))
     {
         $apt->errmsg(LANG_ERROR_VALIDATE);
     }
     if (!$apt->txtcounmxs($post,$apt->getsettings('txtcount6')))
     {
         $apt->errmsg(LANG_ERROR_LETTER_MAX);
     }
       //////////////////////////////////////////////
   if ($apt->post['editupload'] == "delete" )
    {
        $row = $apt->dbfetch("SELECT * FROM rafia_upload WHERE upid='$uploadfile'");
        @extract($row);
        $filename = $apt->upload_path."/".$uppostid.".".$upcat;
        if(@unlink($filename))
        {
            $apt->query("DELETE FROM rafia_upload WHERE upid='$uploadfile'");
            $apt->query("update rafia_comment set
                                          uploadfile = '0'
                                          where id = '$id'");
        }
    }
     //////////////////////////////////////////////
    if($apt->post['editupload'] == 'new' )
    {
        if ($apt->CheckGroup('upload_forum_file'))
        {
            if (is_uploaded_file($apt->files["phpfile"]['tmp_name']))
            {
                $up  = new RaFiaUpload;
                $up->cat = "comment";
                $up->uploads = $apt->files["phpfile"];
                if ($up->uploads["size"] > ($apt->getsettings("commentfilemxsize")*1024))
                {
                  $apt->errmsg(LANG_ERROR_SIZE_MUST_LESS.$apt->getsettings("commentfilemxsize")." kb");
                }
                if ($up->checkend( $apt->getsettings("commentfileupload")) == false)
                {
                    $apt->errmsg("هذا النوع من الملفات لا يسمح  برفعه للمنتدى <br>");
                }
                $up->uploadfile(1);
            }
        }
    }
    ////////////////
      $title   =  $apt->format_data($title);
      $comment =  $apt->format_post($post);
    if ($apt->checkcadmincat($apt->post['cat_id']) )
    {
        $result = $apt->query("update rafia_comment set
                                        title = '$title',
                                        allow = '$allow',
                                        comment = '$comment'
                                        where id = '$id'");
    }
    else
    {
        $result = $apt->query("update rafia_comment set
                                        comment = '$comment',
                                        allow = '$allow'
                                        where id = '$id'");
    }
    $url = "$commentpage#comment$id";
    if ($result)
    {
        $apt->bodymsg(LANG_MSG_YOUR_POST_HAS_EDITED,$url);
    }
    else
    {
        $apt->bodymsg(LANG_ERROR_ADD_DB,$url);
    }
}
//---------------------------------------------------
//
//---------------------------------------------------
else if($apt->get['action']=="admin")
{
    $menu->menuid =  "1,3";
    $apt->head(LANG_TITLE_MODERATE_POSTS);
    if (( $apt->cookie['cgroup'] == $apt->a_g) || ( $apt->cookie['cgroup'] == $apt->m_g))
    {
        if ( $apt->cookie['cgroup'] == $apt->a_g)
        {
            $result = $apt->query("SELECT * FROM rafia_cat WHERE catType ='1' ORDER BY id DESC");
        }
        else
        {
            $result = $apt->query("SELECT rafia_cat.*
                                     FROM rafia_cat,rafia_moderate
                                     WHERE rafia_cat.catType ='1' AND rafia_cat.id=rafia_moderate.moderatecatid
                                     AND rafia_moderate.moderateid =".$apt->cookie['cid']."
                                     ORDER BY id DESC");
        }
        $countwit = $apt->dbnumquery("rafia_news","allow!='yes'","id");
        if ($countwit == "")
        {
            $countwit=" لا يوجد";
        }
        $index_middle .=  $apt->admintablehead("$PHP_SELF?action=wait","أقسام الاخبار");
        while($row = $apt->dbarray($result))
        {
            @extract($row);
                $numrows = $apt->dbnumquery("rafia_news","cat_id='$id'","id");
                $index_middle .= $apt->admintablecell("$PHP_SELF?action=news&cat_id=$id");
        }
        $index_middle .= $apt->admintableclose();
        $menu->menuid =  $apt->getsettings("news_menuid");
        $right_menu   =  $menu->_menu(1);
        
        if($apt->getsettings("news_left_menu"))
        $left_menu    =  $menu->_menu(2);
        $apt->html_Output($left_menu);
    }
    else
    {
        $apt->head(LANG_TITLE_LOG_IN);
        eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
        $apt->foot($pageft);
        exit;
    }
}
//---------------------------------------------------
//
//---------------------------------------------------
else if($apt->get['action']=="news")
{
    $perpage =50;
    $cat_id = $apt->setid('cat_id');
    if( $apt->checkcadmincat($cat_id) )
    {
       $apt->head(LANG_TITLE_MODERATE_POSTS);
       $result = $apt->query ("SELECT * FROM rafia_news WHERE
                                cat_id=$cat_id ORDER BY id DESC
                                LIMIT  $start,$perpage");
       $numrows = $apt->dbnumquery("rafia_news","allow='yes' and cat_id=$cat_id","id");
       $index_middle .= $apt->pagenum($perpage,"news&cat_id=$cat_id");
        $index_middle .=$apt->admin_form_opan("donews");
        $index_middle .=$apt->admin_table_head("مواضيع الاخبار");
       while($apt->row =$apt->dbarray($result))
       {
           $result2 = $apt->query("SELECT news_id FROM rafia_comment WHERE news_id=" . $apt->row["id"] . "");
           $numrows = $apt->dbnumrows($result2);
           $index_middle .=  $apt->admin_table_cell("cat_id=$cat_id&id","edit","");
       }
         $index_middle .= $apt->admin_table_close();
        $index_middle .=  $apt->admin_form_close(1);
       $apt->html_Output($left_menu);
}
else
{
       $apt->head(LANG_TITLE_LOG_IN);
      eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
}
}
//---------------------------------------------------
//
//---------------------------------------------------
else if($apt->get['action']=="donews")
{
    if ( $apt->checkcadmincat($apt->post['cat_id']))
    {
        $cat_id =  $apt->post['cat_id'];
        $catid =  $apt->post['catid'];
        $do =  $apt->post['do'];
        if (count($do) > 0)
        {
             if(($apt->post['move']) && $apt->checkcmodcat('can_move'))
             {
                 foreach($apt->post['do'] as $id)
                 {
                    $result = $apt->query("update rafia_news set cat_id='$catid' where id='$id'");
                    $result = $apt->query("update rafia_comment set cat_id='$catid' where news_id='$id'");
                }
                if ($result)
                {
                    $numrows = $apt->dbnumquery("rafia_news","cat_id='$catid'","id");
                    $result  = $apt->query("update rafia_cat set countopic='$numrows' where id='$catid'");
                    $numrows = $apt->dbnumquery("rafia_comment","cat_id='$catid'","id");
                    $result  = $apt->query("update rafia_cat set countcomm='$numrows' where id='$catid'");
                    $numrows = $apt->dbnumquery("rafia_news","cat_id='$cat_id'","id");
                    $result  = $apt->query("update rafia_cat set countopic='$numrows' where id='$cat_id'");
                    $numrows = $apt->dbnumquery("rafia_comment","cat_id='$cat_id'","id");
                    $result  = $apt->query("update rafia_cat set countcomm='$numrows' where id='$cat_id'");
                    $apt->bodymsg(LANG_MSG_THREAD_HAS_MOVED,$apt->refe);
                 }
            }
            elseif(($apt->post['del']) && $apt->checkcmodcat('can_delete'))
            {
                foreach($apt->post['do'] as $id)
                {
                    $result  = $apt->query("delete from rafia_news where id='$id'");
                    $filename = $apt->upload_path."/".$id.".news";
                    if(!@unlink($filename))
                    {
                        $filename = $apt->upload_path."/".$id.".news";
                        $filethump = $apt->upload_path."/".$id.".newsthumb";
                        @unlink($filename);
                        @unlink($filethump);
                    }
                    if($apt->getuploadsdb($id,'news_id')){
                        $apt->query("delete from  rafia_comment where news_id='$id'");
                    }
                }
                if ($result)
                {
                    $numrows = $apt->dbnumquery("rafia_news","cat_id='$cat_id'","id");
                    $result  = $apt->query("update rafia_cat set countopic='$numrows' where id='$cat_id'");
                    $numrows = $apt->dbnumquery("rafia_comment","cat_id='$cat_id'","id");
                    $result  = $apt->query("update rafia_cat set countcomm='$numrows' where id='$cat_id'");
                    $numrows = $apt->dbnumquery("rafia_news","allow='yes'","id");
                    $Counter->SetCount('newsCount',$numrows);
                    $numcomment = $apt->dbnumquery("rafia_comment","id");
                    $Counter->SetCount('commentCount',$numcomment);
                    $apt->bodymsg(LANG_MSG_POST_HAS_BEEN_DELETED,$apt->refe);
                }
            }
            elseif($apt->post['allow'])
            {
                 foreach($apt->post['do'] as $id)
                 {
                    $result = $apt->query("update rafia_news set allow='yes' where id=$id");
                 }
                 if ($result)
                 {
                    $numrows = $apt->dbnumquery("rafia_news","allow='yes'","id");
                    $Counter->SetCount('newsCount',$numrows);
                    $apt->bodymsg(LANG_MSG_POST_HAS_APPROVED,$apt->refe);
                }
             }
         }
         else
        {
            $apt->errmsg(LANG_ERROR_CHOICE);
        }
    }
     else
    {
         $apt->errmsg(LANG_ERROR_DEL_BY_ADMIN);
}
}
elseif ($apt->get['action']=="comment")
{
    $cat_id = $apt->setid('cat_id');
    $id = $apt->setid('id');
   if( !$apt->checkcadmincat($cat_id) )
   {
       $apt->head(LANG_TITLE_LOG_IN);
       eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
      exit;
   }
    $apt->head(LANG_TITLE_MODERATE_POSTS);
    $menu->menuid =  "1,3";
    $admin_menu = $menu->_menu(1);
    $result = $apt->query("SELECT * FROM rafia_comment WHERE
                             news_id='$id' ORDER BY id ASC");
    if($apt->dbnumrows($result) > 0)
    {
         $index_middle .=  $apt->admin_form_opan("deletecomment&cat_id=".$cat_id);
         $index_middle .=   $apt->admin_table_head("إدارة الردود والتعليقات");
        while($apt->row =  $apt->dbarray($result))
        {
            $apt->row["title"] = "الكاتب : ".$apt->row['name']."<hr>".$apt->title_cut($apt->rafia_code($apt->row['comment']),150);
            $index_middle .=   $apt->admin_table_cell("","editcomment","",0);
        }
         $index_middle .=   $apt->admin_table_close();
         $index_middle .=  $apt->admin_form_close(0,0);
    }
    else
    {
        $index_middle .=  "<p>&nbsp;لايوجد ردود في هذا الموضوع .</p>";
    }
     $apt->html_Output($left_menu);
}
else if($apt->get['action']=="del")
{
    $cat_id = $apt->post['cat_id'];
	if($cat_id=='')$apt->bodymsg('عذرا .. ارجع و حدد قسم للمشاركة',"news.php?action=edit&id=".$apt->post['idp']);
    if((!$apt->checkcadmincat($cat_id))  ||(!$apt->checkcmodcat('can_delete')))
    {
       $apt->head(LANG_TITLE_LOG_IN);
      eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
      exit;
   }
    if(!empty( $apt->post['idp']))
    {
        $id = $apt->post['idp'];
        $result = $apt->query("delete from  rafia_news where id=$id  and cat_id=$cat_id");
        if($result)
        {
            $Counter->dropFrom('newsCount',1);
            $filename = $apt->upload_path."/".$id.".news";
            @unlink($filename);
            @unlink($apt->upload_path."/".$id.".newsthumb");
            if($apt->getuploadsdb($id,'news_id'))
            {
                $numcomment = $apt->dbnumquery("rafia_comment","news_id='$id'","id");
                $Counter->dropFrom('commentCount',$numcomment);
               $apt->query("delete from  rafia_comment where news_id='$id'");
           }
        }
    }
    else if(!empty( $apt->post['idc']))
    {
         $id = $apt->post['idc'];
         $filename = $apt->upload_path."/".$id.".comment";
         @unlink($filename);
         $result = $apt->query("delete from rafia_comment where id='$id' and cat_id='$cat_id'");
         $Counter->dropFrom('commentCount',1);
    }
    if($result)
    {
        $Rurl = "news.php?action=list&cat_id=$cat_id";
        $apt->bodymsg(LANG_MSG_POST_HAS_BEEN_DELETED,$Rurl);
    }
}
else if($apt->get['action']=="wait")
{
     if (( $apt->cookie['cgroup'] == $apt->a_g) || ( $apt->cookie['cgroup'] == $apt->m_g))
    {
        $apt->head(LANG_TITLE_MODERATE_POSTS);
        $menu->menuid =  "1,3";
        $admin_menu = $menu->_menu(1);
        $result = $apt->query("SELECT * FROM rafia_news WHERE allow !='yes' ORDER BY id ASC");
        if($apt->dbnumrows($result))
        {
            $index_middle .=$apt->admin_form_opan("donews");
            $index_middle .=$apt->admin_table_head("مواضيع تحت الانتظار");
             while($apt->row = $apt->dbarray($result))
            {
                $index_middle .=$apt->admin_table_cell("news_id","edit","",2);
            }
            $index_middle .= $apt->admin_table_close();
            $index_middle .= $apt->admin_form_close(1,0);
            }
            else
            {
                $index_middle .= "<p>لا يوجد مواضيع تحت الانتظار.</p>";
            }
              $apt->html_Output($left_menu);
        }
        else
        {
            $apt->head(LANG_TITLE_LOG_IN);
            eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
        }
}
else if($apt->get['action']=="deletecomment")
{
    $cat_id  = $apt->setid('cat_id');
    $do  = $apt->post['do'];
    if(( $apt->checkcadmincat($cat_id) ) && ($apt->checkcmodcat('can_delete')))
    {
        if (count($do) >0)
        {
		if($apt->post['del']){
            foreach($do as $id)
            {
                $result = $apt->query("delete from  rafia_comment where id=$id");
                $filename = $apt->upload_path."/".$id.".comment";
                @unlink($filename);
            }
        }elseif($apt->post['allow']){
            foreach($do as $id)
            {
                $result = $apt->query("update rafia_comment set allow='yes' where id=$id");
            }
        }
        }
        $url = $apt->refe;
        if ($result)
        {
		if($apt->post['del']){
            $apt->bodymsg(LANG_MSG_POST_HAS_BEEN_DELETED,$url);
		}else{
            $apt->bodymsg(LANG_MSG_POST_HAS_APPROVED,$url);
		}
        }
        else
        {
            $apt->bodymsg(LANG_ERROR_ADD_DB,$url);
        }
    }
    else
    {
        $apt->head(LANG_TITLE_LOG_IN);
         eval("print \"" . $apt->gettemplate ( 'Login_main' ) . "\";");
    }
}
elseif($apt->get['action']=="sticky")
{
    $newsid = $apt->setid('newsid');
    $cat_id  = $apt->setid('cat_id');
   if (( $apt->cookie['cgroup'] == $apt->a_g) || ( $apt->cookie['cgroup'] == $apt->m_g))
   {
        $edit_by = $apt->cookie['cname']."@sticky";
        $result  = $apt->query("update rafia_news set sticky='1' ,edit_by='$edit_by' where id=$newsid and cat_id=$cat_id");
        if ($result)
        {
             $url="news.php?action=view&id=$newsid";
             $apt->bodymsg(LANG_MSG_THREAD_HAS_STUCK,$url);
        }
    }
}
elseif($apt->get['action']=="unsticky")
{
    $newsid = $apt->setid('newsid');
    $cat_id  = $apt->setid('cat_id');
    if (( $apt->cookie['cgroup'] == "1") || ( $apt->cookie['cgroup'] == "4"))
    {
        $edit_by = $apt->cookie['cname']."@unsticky";
        $result  = $apt->query("update rafia_news set sticky='0' ,edit_by='$edit_by'  where id=$newsid and cat_id=$cat_id");
        if ($result)
        {
           $url = "news.php?action=view&id=$newsid";
            $apt->bodymsg(LANG_MSG_THREAD_HAS_UNSTUCK,$url);
        }
    }
}
elseif($apt->get['action']=="open")
{
     $newsid = $apt->setid('newsid');
     $cat_id  = $apt->setid('cat_id');
    if (( $apt->cookie['cgroup'] == $apt->a_g) || ( $apt->cookie['cgroup'] == $apt->m_g))
    {
        $edit_by = $apt->cookie['cname']."@open";
        $result = $apt->query("update rafia_news set close='0', edit_by='$edit_by'  where id=$newsid and cat_id=$cat_id");
        if ($result)
        {
            $url="news.php?action=view&id=$newsid";
            $apt->bodymsg(LANG_MSG_POST_HAS_OPENED,$url);
        }
     }
}
elseif($apt->get['action']=="close")
{
     $newsid = $apt->setid('newsid');
     $cat_id  = $apt->setid('cat_id');
    if (( $apt->cookie['cgroup'] == $apt->a_g) || ( $apt->cookie['cgroup'] == $apt->m_g))
    {
        $edit_by = $apt->cookie['cname']."@close";
        $result  = $apt->query("update rafia_news set close='1', edit_by='$edit_by'  where id=$newsid and cat_id=$cat_id");
        if ($result)
        {
            $url="news.php?action=view&id=$newsid";
            $apt->bodymsg(LANG_MSG_THREAD_HAS_CLOSED,$url);
        }
    }
}
if(isset($fo))
{
    print $apt->script->post_java();
}
$apt->foot($pageft);
?>