<?php
include('header.php');
echo $h;

$action = $_GET['action'];
if(!isset($action)) $action = "add";

IF ($action=="add"){
if(isset($_GET['id']) AND intval($_GET['id']) != 0){
$qxxxx = mysql_query("SELECT * FROM news where id='".intval($_GET['id'])."' order by id desc");
$xxd = mysql_num_rows($qxxxx);
if($xxd == 0){
$input = ''.$LANGAUGE_CP[707].' '.intval($_GET['id']).'';
}else{
$pd = mysql_fetch_array($qxxxx);
$pd['sub'] = stripslashes($pd['sub']);
$pd['sub'] = htmlspecialchars($pd['sub']);
$input = '<input size="10" name="idnews" value="'.intval($_GET['id']).'" readonly="readonly" /> <span style="color:red;">'.$pd['sub'].'</span>';
}

}else{
$input = '<input size="10" name="idnews" />';
}

?>
<div id="x1">
<form name="vbform1" method="post" action="comment.php?action=insert" enctype="multipart/form-data">

  <table class='xtable' width="100%" id="AutoNumber11">
     <tr>
      <td width="100%" colspan="2" class='xtt' align="center"><?php echo $LANGAUGE_CP[708]; ?></td>
    </tr>

    <tr>
      <td width="20%" class='xtr'><?php echo $LANGAUGE_CP[709]; ?></td>
      <td width="80%" class='xtd'><?php echo $input; ?></td>
    </tr>

    <tr>
      <td width="20%" class='xtr'><?php echo $LANGAUGE_CP[177]; ?></td>
      <td width="80%" class='xtd'><input size="50" name="name" /></td>
    </tr>

    <tr>
      <td width="20%" class='xtr'><?php echo $LANGAUGE_CP[178]; ?></td>
      <td width="80%" class='xtd'><input size="50" name="email" /></td>
    </tr>

    <tr>
      <td width="20%" class='xtr'><?php echo $LANGAUGE_CP[710];?></td>
      <td width="80%" class='xtd'>
<select  size='1' name='country'>

<?php
$sql = mysql_query ("SELECT * FROM contries order by orders ASC");
while($cat=mysql_fetch_array($sql))
{
$cat['name_ar'] = stripslashes($cat['name_ar']);
$cat['name_ar'] = htmlspecialchars($cat['name_ar']);
echo '<option value="'.$cat['id'].'">'.$cat['name_ar'].'</option>';
}
?>
</select>

      </td>
    </tr>

    <tr>
      <td width="100%" colspan="2" class='xtr' align="center">
<label for="textarea"><?php echo $LANGAUGE_CP[711]; ?></label><br /><br />
<textarea id="textarea" name="text" rows="10" cols="120"></textarea>
      </td>
    </tr>

     <tr>
      <td width="100%" colspan="2" class='xtr' align="center"><input class='buttonx' type="submit" value=" <?php echo $LANGAUGE_CP[18]; ?> " name="cmdAdd" /></td>
    </tr>
  </table>
</form>
</div>
<?php
if($_GET["s"] == 1){
$hh = "checked='checked'";
}elseif($_GET["s"] == 0){
$hh = "";
}else{
$hh = "";
}

if($_GET["c"] == 1){
$cc = "AND active=1";
$rr = "&c=1";
}elseif($_GET["c"] == 2){
$cc = "AND active=0";
$rr = "&c=2";
}else{
$cc = "";
$rr = "";
}

$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$page = ($page == 0 ? 1 : $page);
$perpage = 20;
$startpoint = ($page * $perpage) - $perpage;

$queryx = mysql_query("SELECT * FROM comment where cat='1' ".$cc." order by id desc LIMIT $startpoint,$perpage");
$xx = mysql_num_rows($queryx);
if($xx == 0){
echo "";
}else{

echo "<form name='xxx1' action='comment.php?action=opx' method='post'>

<table class='xtable' width='100%' id='AutoNumbe5r15'>
<tr>
      <td width='100%' colspan='8' align='center' class='xtx'>".$LANGAUGE_CP[97]."</td>
</tr>

<tr>
      <td width='100%' colspan='8' align='center' class='xtr'>".$LANGAUGE_CP[712]."</td>
</tr>

  <tr>
      <td align='center' width='2%' class='xtr'>#</td>
      <td width='28%' align='center' class='xtr'>".$LANGAUGE_CP[189]."</td>
      <td width='30%' align='center' class='xtr'>".$LANGAUGE_CP[713]."</td>
      <td width='10%' align='center' class='xtr'>".$LANGAUGE_CP[714]."</td>
      <td width='17%' align='center' class='xtr'>".$LANGAUGE_CP[57]."</td>
      <td width='5%' align='center' class='xtr'>".$LANGAUGE_CP[28]."</td>
      <td width='5%' align='center' class='xtr'>".$LANGAUGE_CP[26]."</td>
      <td width='5%' align='center' class='xtr'>".$LANGAUGE_CP[27]."</td>
    </tr>";
$i = 0;
while($p = mysql_fetch_array($queryx))
{
$i++;
$p['name'] = stripslashes($p['name']);
$p['name'] = htmlspecialchars($p['name']);

$p['email'] = stripslashes($p['email']);
$p['email'] = htmlspecialchars($p['email']);

$p['text'] = stripslashes($p['text']);
$p['text'] = htmlspecialchars($p['text']);

if($p['date']=="- - -"){
$d = "";
}else{
$d = date("j/n/Y",$p['date']);
}

if($p['active'] == 1){
$im = "images/on.gif";
$li = "comment.php?action=actx&id=$p[id]&act=0";
$te = $LANGAUGE_CP[72];
}else{
$im = "images/off.gif";
$li = "comment.php?action=actx&id=$p[id]&act=1";
$te = $LANGAUGE_CP[73];
}

$sqq = mysql_query ("SELECT * FROM contries where id='".$p['country']."'");
$con = mysql_fetch_array($sqq);
$con['name_ar'] = stripslashes($con['name_ar']);
$contry = htmlspecialchars($con['name_ar']);

$queryxx = mysql_query("SELECT * FROM news where id='".$p['idt']."'");
$px = mysql_fetch_array($queryxx);
$px['sub'] = stripslashes($px['sub']);
$px['sub'] = htmlspecialchars($px['sub']);

echo "<input type='hidden' name='idx[]' value='".$p['id']."' />
<tr>
      <td align='center' width='2%' class='xtr'><input ".$hh." type='checkbox' name='del[$i]' value='".$p['id']."' /></td>
      <td width='28%' class='xtd'><a href='mailto:".$p['email']."'>".$p['name']."</a></td>
      <td width='30%' align='center' class='xtd'><a target='_blank' href='../news.php?action=view&id=".$p['idt']."'>".$px['sub']."</a></td>
      <td width='10%' align='center' class='xtd'>".$contry."</td>
      <td width='17%' align='center' class='xtd'>".$d."</td>
      <td width='5%' align='center' class='xtd'><a href='$li'><img border='0' src='$im' alt='$te ".$p['name']."' /></a></td>
      <td width='5%' align='center' class='xtd'><a href='comment.php?action=edit&id=$p[id]'><img border='0' src='images/edit.gif' alt='".$p['name']."' /></a></td>
      <td width='5%' align='center' class='xtd'><a href='comment.php?action=delete&id=$p[id]'><img border='0' src='images/delete.gif' alt='".$p['name']."' /></a></td>
</tr>

<tr>
<td colspan='8' class='xtd'>".$p['text']."</tr>";

}
echo "<tr>
<td colspan='8' align='center' class='xtr'><input name='activ' class='addsubmit' type='submit' value=' ".$LANGAUGE_CP[108]." ' /> <input name='noactiv' class='addsubmit' type='submit' value=' ".$LANGAUGE_CP[107]." ' /> <input name='delete' class='addsubmit' type='submit' id='delete' value=' ".$LANGAUGE_CP[27]." ' /> <input class='addsubmit' name='edit' type='submit' value=' ".$LANGAUGE_CP[26]." ' /><br />
<a href='comment.php?&s=1&page=$page".$rr."'>".$LANGAUGE_CP[109]."</a> - <a href='comment.php?&s=0&page=$page".$rr."'>".$LANGAUGE_CP[110]."</a></td>
</tr>";

echo "</table></form>";

$pagesnum = @ceil(mysql_num_rows(mysql_query("SELECT id FROM comment where cat='1' ".$cc."")) / $perpage);
if($pagesnum > 1){
echo "<br />
<p align='center'>";
for ($i=1; $i<=$pagesnum; $i++) {
if ($i != $page) {
$z = "[<a href='comment.php?page=$i".$rr."'>$i</a>] ";
} else {
$z = "[<u>$i</u>]";
}
echo "$z";
}
echo "</p>";
}
}

}ELSEIF ($action=="insert"){

$name = addslashes($_POST['name']);
$name = trim($name);

$email = addslashes($_POST['email']);
$email = trim($email);

$country = addslashes($_POST['country']);

$text = addslashes($_POST['text']);
$text = trim($text);

$idnews = intval($_POST['idnews']);

$ip = $_SERVER['REMOTE_ADDR'];

if (empty($name)){
$msg = $LANGAUGE_CP[715];
$err = "no";
}

if (empty($text)){
$msg .= $LANGAUGE_CP[716];
$err = "no";
}

if (empty($idnews)){
$msg .= $LANGAUGE_CP[717];
$err = "no";
}

if ($email != "" AND !eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-z]{2,4})$',$email)){
$msg .= $LANGAUGE_CP[718];
$err = "no";
}

$ip = $_SERVER['REMOTE_ADDR'];

if($err != "no"){
$query =  mysql_query ("INSERT INTO comment (name,active,email,text,idt,date,cat,country,ip) VALUES ('$name',1,'$email','$text','$idnews','".time()."',1,'$country','$ip')") or die ("error");
echo $LANGAUGE_CP[21];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php?action=add' />";
}else{
$msg .= $LANGAUGE_CP[719];
echo "<table class='xtable' width='100%' id='AutoNumbe5r15'>
<tr>
      <td width='100%' align='center' class='xtx'>".$LANGAUGE_CP[720]."</td>
</tr>
<tr>
      <td width='100%' class='xtd'>".$msg."</td>
</tr>
</table>";
}

}ELSEIF ($action=="opx"){

if(isset($_POST['delete'])){
if(count($_POST['del']) > 0){
$del_id = join(',',$_POST['del']);
$sql = mysql_query("DELETE FROM comment WHERE id IN($del_id)");
print $LANGAUGE_CP[35];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php' />";
}else{
echo $LANGAUGE_CP[63];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php' />";
}
}


if(isset($_POST['activ'])){
if(count($_POST['del']) > 0){
$del_id = join(',',$_POST['del']);
$sql = mysql_query("UPDATE comment SET active='1' WHERE id IN($del_id)");
print $LANGAUGE_CP[62];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php' />";
}else{
echo $LANGAUGE_CP[63];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php' />";
}
}


if(isset($_POST['noactiv'])){
if(count($_POST['del']) > 0){
$del_id = join(',',$_POST['del']);
$sql = mysql_query("UPDATE comment SET active='0' WHERE id IN($del_id)");
print $LANGAUGE_CP[61];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php' />";
}else{
echo $LANGAUGE_CP[63];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php' />";
}
}

if(isset($_POST['edit'])){

if(count($_POST['del']) > 0){
$c = count($_POST['del']);

$del_id = join(',',$_POST['del']);
$queryx = mysql_query("SELECT * FROM comment where id IN($del_id) order by id desc");

echo"<form name='vbform' method='POST' action='comment.php?action=editall'>
<table class='xtable' width='100%' id='AutoNumber15555'>

<tr>
<td width='100%' colspan='2' class='xtx' align='center'>".$LANGAUGE_CP[32]."</td>
</tr>";
$ii = 0;
while($p = mysql_fetch_array($queryx))
{
++$ii;

$p['name'] = stripslashes($p['name']);
$p['name'] = htmlspecialchars($p['name']);

$p['email'] = stripslashes($p['email']);
$p['email'] = htmlspecialchars($p['email']);

$p['text'] = stripslashes($p['text']);
$p['text'] = htmlspecialchars($p['text']);

$p['country'] = stripslashes($p['country']);
$p['country'] = htmlspecialchars($p['country']);


echo "<input type='hidden' name='id[]' value='".$p['id']."' />
<tr>
<td width='20%' class='xtr'>".$LANGAUGE_CP[197]."</td>
<td width='80%' class='xtd'><input name='name[]' size='60' value='".$p['name']."' /></td>
</tr>

<tr>
<td width='20%' class='xtr'>".$LANGAUGE_CP[198]."</td>
<td width='80%' class='xtd'><input name='email[]' size='60' value='".$p['email']."' /></td>
</tr>

<tr>
<td width='20%' class='xtr'>".$LANGAUGE_CP[117]."</td>
<td width='80%' class='xtd'>
<select size='1' name='active[]'>";
if($p['active'] == 1){
echo "<option value='1' selected='selected'>".$LANGAUGE_CP[108]."</option>";
echo "<option value='0'>".$LANGAUGE_CP[107]."</option>";
}else{
echo "<option value='1'>".$LANGAUGE_CP[108]."</option>";
echo "<option value='0' selected='selected'>".$LANGAUGE_CP[107]."</option>";
}
echo "</select>
</td>
</tr>

<tr>
<td width='100%' class='xtd'  colspan='2' align='center'>
<br />
<textarea name='text[]' rows='10' cols='120'>".$p['text']."</textarea>
<br /><br /></td>
</tr>";

if($ii != $c){
echo "<tr>
<td width='100%' colspan='2' align='center' class='xtr'>- - - - - - - -</td>
</tr>";
}

}

echo "<tr>
<td width='100%' colspan='2' align='center' class='xtr'><input class='addsubmit' type='submit' value=' ".$LANGAUGE_CP[33]." ' name='B1' /></td>
</tr>

</table>
</form>";

}else{
echo $LANGAUGE_CP[63];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php' />";
}

}


}ELSEIF ($action=="editall"){

$number = count($_POST['id']);

for($i=0;$i<$number;$i++){
$id = $_POST['id'][$i];
$id = intval($id);

$name = addslashes($_POST['name'][$i]);
$name = trim($name);

$email = addslashes($_POST['email'][$i]);
$email = trim($email);

$text = addslashes($_POST['text'][$i]);
$text = trim($text);

$active = $_POST['active'][$i];

if(!empty($name) OR !empty($text)){
echo "$name . . . ".$LANGAUGE_CP[111]."<br />";
$query =  mysql_query("UPDATE comment SET name='$name',email='$email',text='$text',active='$active' where id='$id'");
}

}
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=comment.php?action=add' />";

}ELSEIF ($action=="edit"){
$id = intval($_GET['id']);
$query = mysql_query("SELECT * FROM comment where id='$id'");
$p = mysql_fetch_array($query);

$p['name'] = stripslashes($p['name']);
$p['name'] = htmlspecialchars($p['name']);

$p['text'] = stripslashes($p['text']);
$p['text'] = htmlspecialchars($p['text']);

$p['email'] = stripslashes($p['email']);
$p['email'] = htmlspecialchars($p['email']);

echo "<form name='vbform' method='POST' action='comment.php?action=editok' enctype='multipart/form-data'>
<input type='hidden' name='id' value='$p[id]' />

<table class='xtable' width='100%' id='AutoNumber155'>

<tr>
      <td width='100%' colspan='2' align='center' class='xtt'>".$LANGAUGE_CP[32]."</td>
</tr>

<tr>
<td width='20%' class='xtr'>".$LANGAUGE_CP[709]."</td>
<td width='80%' class='xtd'><input name='idnews' size='10' value='".$p['idt']."' /></td>
</tr>

<tr>
<td width='20%' class='xtr'>".$LANGAUGE_CP[197]."</td>
<td width='80%' class='xtd'><input name='name' size='50' value='".$p['name']."' /></td>
</tr>

<tr>
<td width='20%' class='xtr'>".$LANGAUGE_CP[198]."</td>
<td width='80%' class='xtd'><input dir='ltr' name='email' size='50' value='".$p['email']."' /></td>
</tr>

<tr>
      <td width='100%' colspan='2' class='xtr' align='center'>
<label for='textarea'>".$LANGAUGE_CP[721]."</label><br /><br />
<textarea id='textarea' name='text' rows=10 cols=120>".$p['text']."</textarea>
      </td>
    </tr>

<tr>
<td width='100%' colspan='2' align='center' class='xtr'><input class='buttonx' type='submit' value=' ".$LANGAUGE_CP[33]." ' name='B1' /></td>
</tr>

</table>
</form>";

}ELSEIF ($action=="editok"){

$id    = intval($_POST['id']);
$name = addslashes($_POST['name']);
$name = trim($name);

$email = addslashes($_POST['email']);
$email = trim($email);

$text = addslashes($_POST['text']);
$text = trim($text);

$idnews = intval($_POST['idnews']);

if (empty($name)){
echo $LANGAUGE_CP[722];
}elseif (empty($text)){
echo $LANGAUGE_CP[723];
}else{
$query =  mysql_query("UPDATE comment SET name='$name', email='$email', text='$text', idt='$idnews' where id='$id'");
echo $LANGAUGE_CP[34];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=comment.php?action=add' />";
}

}ELSEIF ($action=="delete"){
$id = intval($_GET['id']);

if(isset($_POST['ok']) && $_POST['ok']=="yes"){
$idx = intval($_POST['idx']);
$query =  mysql_query("DELETE FROM comment where id='$idx'");
echo $LANGAUGE_CP[35];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=comment.php?action=add' />";
}else{
echo "<div align='center'>
<form name='form' method='POST' action='comment.php?action=delete'>
<input type='hidden' name='idx' value='".$id."' />
<input type='hidden' name='ok' value='yes' />
<input type='submit' value=' ".$LANGAUGE_CP[724]." ' name='B1' />
</form></div>";
}

}ELSEIF ($action=="actx"){
$id = intval($_GET['id']);
$act = intval($_GET['act']);
if ($act == 0){
$query =  mysql_query("UPDATE comment SET active='$act' where id='$id'");
echo $LANGAUGE_CP[61];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=comment.php?action=add' />";
}elseif ($act == 1){
$query =  mysql_query("UPDATE comment SET active='$act' where id='$id'");
echo $LANGAUGE_CP[62];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=comment.php?action=add' />";
}else{
echo $LANGAUGE_CP[63];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=comment.php?action=add' />";
}
}

echo $f;
?>