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

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

IF ($action=="add"){
?>
<form name="vbform" method="post" action="guestbook_cp.php?action=insert">

<table class='xtable' width="100%" id="AutoNum4ber1">

     <tr>
      <td width="100%" colspan="2" class='xtx' align="center"><?php echo $LANGAUGE_CP[280]; ?></td>
    </tr>

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

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

    <tr>
      <td width="35%" class='xtr'><?php echo $LANGAUGE_CP[281]; ?></td>
      <td width="65%" class='xtd'>
<select  size='1' name='countryid'>
<?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%" class='xtd' colspan="2" align="center">
النص<br />
<script type='text/javascript'>edToolbar('mytxtarea'); </script>
<textarea name='message' id='mytxtarea' rows='15' cols='100'></textarea>

      </td>
    </tr>

     <tr>
      <td width="100%" colspan="2" class='xtr' align="center">
      <input type="submit" value=" <?php echo $LANGAUGE_CP[18]; ?> " name="cmdAdd" /></td>
    </tr>
  </table>
</form>

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

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

echo "<br />
<table class='xtable' width='100%' id='AutoNumber15'>
  <tr>
<td width='100%' colspan='7' class='xtt' align='center'>".$LANGAUGE_CP[97]."</td>
</tr>

  <tr>
      <td width='33%' align='center' class='xtr'>".$LANGAUGE_CP[189]."</td>
      <td width='20%' align='center' class='xtr'>".$LANGAUGE_CP[282]."</td>
      <td width='19%' align='center' class='xtr'>".$LANGAUGE_CP[57]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[191]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[28]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[26]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[27]."</td>
    </tr>";
while($p = mysql_fetch_array($queryx))
{

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

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

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

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

if($con['name_ar']==""){
$rrrw = "غير معروف";
}else{
$rrrw = $con['name_ar'];
}

$d = date("j/n/Y",$p['date']);

if($p['active'] == 1){
$im = "images/on.gif";
$li = "guestbook_cp.php?action=activ&id=$p[id]&act=0";
$te = "إضغط هنا للحجب";
}else{
$im = "images/off.gif";
$li = "guestbook_cp.php?action=activ&id=$p[id]&act=1";
$te = "إضغط هنا للتفعيل";
}

echo "<tr>
      <td width='33%' class='xtd'>» <a href='mailto:".$p['email']."'>".$p['name']."</a></td>
      <td width='20%' align='center' class='xtd'>".$rrrw."</td>
      <td width='19%' align='center' class='xtd'>$d</td>
      <td width='7%' align='center' class='xtd'><a href='guestbook_cp.php?action=show&id=$p[id]'><img border='0' src='images/show.gif' alt='".$p['name']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='$li'><img border='0' src='$im' alt='$te ".$p['name']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='guestbook_cp.php?action=edit&id=$p[id]'><img border='0' src='images/edit.gif' alt='".$p['name']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='guestbook_cp.php?action=delete&id=$p[id]'><img border='0' src='images/delete.gif' alt='".$p['name']."' /></a></td>
    </tr>";
}

echo "</table>";

$pagesnum = @ceil(mysql_num_rows(mysql_query("SELECT id FROM sjl_alzwar where active=1")) / $perpage);
if($pagesnum > 1){
echo "<p align='center'>";
for ($i=1; $i<=$pagesnum; $i++) {
if ($i != $page) {
$z = "[<a href='guestbook_cp.php?action=add&page=$i'>$i</a>] ";
} else {
$z = "[<u>$i</u>]";
}
echo "$z";
}
echo "</p>";
}
}


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

$name = addslashes($_POST['name']);
$country = addslashes($_POST['country']);
$email = addslashes($_POST['email']);
$threads = addslashes($_POST['message']);
$d = time();
$ip = $_SERVER['REMOTE_ADDR'];
$countryid = intval($_POST['countryid']);

if (empty($name)){
echo $LANGAUGE_CP[180];
}else{
$query =  mysql_query ("INSERT INTO sjl_alzwar (name,country,email,date,active,threads,ip,countryid) VALUES ('$name','$country','$email','$d','1','$threads','$ip','$countryid')") or die ("error");
print $LANGAUGE_CP[21];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=guestbook_cp.php?action=add' />";
}


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

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

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

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

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

echo "<form name='vbform' method='POST' action='guestbook_cp.php?action=editok'>
<input type='hidden' name='id' value='$p[id]' />
<input type='hidden' name='x' value='".$_GET['x']."' />

<table class='xtable' width='100%' id='AutoNumber15'>
<tr>
<td width='100%' colspan='2' class='xtx' align='center'>".$LANGAUGE_CP[32]."</td>
</tr>
<tr>
<td width='30%' class='xtr'>".$LANGAUGE_CP[177]."</td>
<td width='70%' class='xtd'><input name='name' size='60' value='".$p['name']."' /></td>
</tr>

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

<tr>
<td width='30%' class='xtr'>".$LANGAUGE_CP[281]."</td>
<td width='70%' class='xtd'>
<select size='1' name='countryid'>";

$sql = mysql_query ("SELECT * FROM contries order by orders ASC") or die ("error");
while($sub = mysql_fetch_array($sql))
{
$sub['name_ar'] = stripslashes($sub['name_ar']);
$sub['name_ar'] = htmlspecialchars($sub['name_ar']);

if($sub['id'] ==  $p['countryid']){
echo "<option value='$sub[id]' selected='selected'>".$sub['name_ar']."</option>";
}else{
echo "<option value='$sub[id]'>".$sub['name_ar']."</option>";
}

}
echo "</select>

</td>
</tr>

<tr>
<td width='100%' class='xtd' colspan='2' align='center'>
النص<br />";
?>
<script type='text/javascript'>edToolbar('mytxtarea'); </script>
<textarea name='message' id='mytxtarea' rows='15' cols='80'><?php echo $p['threads']; ?></textarea>
<?php
echo "</td>
</tr>

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

</table>

</form>";

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

$id    = intval($_POST['id']);
$name = addslashes($_POST['name']);
$country = addslashes($_POST['country']);
$email = addslashes($_POST['email']);
$threads = addslashes($_POST['message']);

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

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

if($_POST['x']==1){
$rr = "guestbook_cp.php?action=act";
}else{
$rr = "guestbook_cp.php?action=add";
}

if (empty($name)){
echo $LANGAUGE_CP[180];
}else{
$query =  mysql_query("UPDATE sjl_alzwar SET name='$name',country='$country',email='$email',threads='$threads',countryid='$countryid' where id='$id'");
echo $LANGAUGE_CP[34];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=".$rr."' />";
}

}ELSEIF ($action=="delete"){
$id = intval($_GET['id']);
if($_GET['x']==1){
$rr = "guestbook_cp.php?action=act";
}else{
$rr = "guestbook_cp.php?action=add";
}

$query =  mysql_query("DELETE FROM sjl_alzwar WHERE id='$id'");
echo $LANGAUGE_CP[35];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=$rr' />";


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

if($_GET['x']==1){
$rr = "guestbook_cp.php?action=act";
}else{
$rr = "guestbook_cp.php?action=add";
}

if ($act == 0){
$query =  mysql_query("UPDATE sjl_alzwar SET active='$act' where id='$id'");
echo $LANGAUGE_CP[61];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=$rr' />";
}elseif($act == 1){
$query =  mysql_query("UPDATE sjl_alzwar SET active='$act' where id='$id'");
echo $LANGAUGE_CP[62];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=$rr' />";
}else{
echo $LANGAUGE_CP[63];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=$rr' />";
}

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

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

$queryx = mysql_query("SELECT * FROM sjl_alzwar where active='0' order by id desc LIMIT $startpoint,$perpage");
$xx = mysql_num_rows($queryx);
if($xx == 0){
echo "<div align='center'>لايوجد مشاركات تحتاج للتفعيل</div>";
}else{

echo "<form name='xxx' action='guestbook_cp.php?action=actok' method='post'>

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

<tr>
      <td width='6%' align='center' class='xtr'>#</td>
      <td width='43%' align='center' class='xtr'>".$LANGAUGE_CP[189]."</td>
      <td width='15%' align='center' class='xtr'>".$LANGAUGE_CP[282]."</td>
      <td width='15%' align='center' class='xtr'>".$LANGAUGE_CP[57]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[191]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[26]."</td>
      <td width='7%' 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['country'] = stripslashes($p['country']);
$p['country'] = htmlspecialchars($p['country']);

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

$d = date("j/n/Y",$p['date']);

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

echo "<tr>
      <td width='6%' class='xtd' align='center'><input type='checkbox' name='del[$i]' value='$p[id]' /></td>
      <td width='43%' class='xtd'>» <a href='mailto:".$p['email']."'>".$p['name']."</a></td>
      <td width='15%' class='xtd' align='center'>".$con['name_ar']."</td>
      <td width='15%' class='xtd' align='center'>$d</td>
      <td width='7%' align='center' class='xtd'><a href='guestbook_cp.php?action=show&id=$p[id]&x=1'><img border='0' src='images/show.gif' alt='".$p['name']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='guestbook_cp.php?action=edit&id=$p[id]&x=1'><img border='0' src='images/edit.gif' alt='".$p['name']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='guestbook_cp.php?action=delete&id=$p[id]&x=1'><img border='0' src='images/delete.gif' alt='".$p['name']."' /></a></td>
    </tr>";
}
echo "
<tr>
<td colspan='7' align='center' class='xtr'><input name='delete' type='submit' id='delete' value=' ".$LANGAUGE_CP[27]." ' /> <input name='activ' type='submit' value=' ".$LANGAUGE_CP[108]." ' /></td>
</tr>
</table>

</form>";

$pagesnum = @ceil(mysql_num_rows(mysql_query("SELECT id FROM sjl_alzwar where active='0'")) / $perpage);
if($pagesnum > 1){
echo "<p align='center'>";
for ($i=1; $i<=$pagesnum; $i++) {
if ($i != $page) {
$z = "[<a href='guestbook_cp.php?action=act&page=$i'>$i</a>] ";
} else {
$z = "[<u>$i</u>]";
}
echo "$z";
}
echo "</p>";
}
}

}ELSEIF ($action=="actok"){
if(isset($_POST['delete'])){

if(count($_POST['del']) > 0){
foreach($_POST['del'] AS $id)
{
$query =  mysql_query("DELETE FROM sjl_alzwar WHERE id='$id'");
}
echo $LANGAUGE_CP[35];
echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=guestbook_cp.php?action=act' />";
}else{
echo $LANGAUGE_CP[63];
}

}else{
echo $LANGAUGE_CP[284];
echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=guestbook_cp.php?action=act' />";
}


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

if(count($_POST['del']) > 0){
foreach($_POST['del'] AS $id)
{
$query = mysql_query("UPDATE sjl_alzwar SET active='1' where id='$id' AND active='0'");
}
echo $LANGAUGE_CP[34];
echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=guestbook_cp.php?action=act' />";
}else{
echo $LANGAUGE_CP[284];
}

}else{
echo $LANGAUGE_CP[284];
echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=guestbook_cp.php?action=act' />";
}



}ELSEIF ($action=="show"){
include('../includes/bbcode.php');

$id = intval($_GET['id']);
$query = mysql_query("SELECT * FROM sjl_alzwar where id='$id'");
$p = mysql_fetch_array($query);

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

$text = $p['threads'];
$text = htmlspecialchars($text);
$text = stripslashes($text);
$text = str_replace( "&lt;", "<", $text );
$text = str_replace( "&gt;", ">", $text );
$text = str_replace( "&#092;" , "\\", $text);
$text = str_replace( "&#39;" , "'", $text);
$text = str_replace( "&#33;" , "!", $text);
$text = str_replace( "&#036;" , "$", $text);
$text = str_replace( "&#124;" , "|", $text);
$text = str_replace( "&amp;" , "&", $text);
$text = str_replace( '&quot;' , '"', $text);
$text = str_replace( "&#153;", "(tm)", $text );
$text = Replace($text);
$text = str_replace("[LIST]","<ul>",$text);
$text = str_replace("[/LIST]","</ul>",$text);
$text = str_replace("[LIST1]","<ol>",$text);
$text = str_replace("[/LIST1]","</ol>",$text);
$text = str_replace("[*]","<li>",$text);

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

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

$d = date("j/n/Y",$p['date']);

$queryw = mysql_query("SELECT * FROM contries where id='".$p['countryid']."'");
$pw = mysql_fetch_array($queryw);

$pw['name_ar'] = stripslashes($pw['name_ar']);
$pw['name_ar'] = htmlspecialchars($pw['name_ar']);

echo "<table class='xtable' width='100%' id='AutoNumber15'>

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

<tr>
<td width='30%' class='xtr'>".$LANGAUGE_CP[197]."</td>
<td width='70%' class='xtd'>".$p['name']."</td>
</tr>

<tr>
<td width='30%' class='xtr'>".$LANGAUGE_CP[198]."</td>
<td width='70%' class='xtd'>".$p['email']."</td>
</tr>

<tr>
<td width='30%' class='xtr'>".$LANGAUGE_CP[286]."</td>
<td width='70%' class='xtd'>".$pw['name_ar']."</td>
</tr>

<tr>
<td width='30%' class='xtr'>".$LANGAUGE_CP[199]."</td>
<td width='70%' class='xtd'>".$p['ip']."</td>
</tr>

<tr>
<td width='30%' class='xtr'>".$LANGAUGE_CP[57]."</td>
<td width='70%' class='xtd'>".$d."</td>
</tr>

<tr>
<td width='100%' colspan='2' class='xtd'>".$text."</td>
</tr>

</table>";
}ELSEIF ($action=="setting"){
$sql = "SELECT * FROM opitions where id=1";
$result = mysql_query ($sql) or die ("Query failed");
$p_cats = mysql_fetch_array($result);

$td1 = "70%";
$td2 = "30%";

echo "<form method='POST' action='guestbook_cp.php?action=update'>

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

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

<tr>
<td width='$td1' class='xtr'>".$LANGAUGE_CP[288]."</td>
<td width='$td2' class='xtd'><input name='sjl_loop' size='10' value='$p_cats[sjl_loop]' /></td>
</tr>

<tr>
<td width='$td1' class='xtr'>".$LANGAUGE_CP[289]."</td>
<td width='$td2' class='xtd'>
<select size='1' name='add_threads'>";
if ($p_cats[add_threads]=="1"){
echo "<OPTION value='1' selected='selected'>".$LANGAUGE_CP[139]."</OPTION>
<OPTION value='0'>".$LANGAUGE_CP[140]."</OPTION";
}else{
echo "<OPTION value='0' selected='selected'>".$LANGAUGE_CP[140]."</OPTION>
<OPTION value='1'>".$LANGAUGE_CP[139]."</OPTION>";
}
echo "</select>
</td>
</tr>

<tr>
<td width='$td1' class='xtr'>".$LANGAUGE_CP[290]."</td>
<td width='$td2' class='xtd'>
<select size='1' name='activeguestbook'>";
if ($p_cats[activeguestbook]=="1"){
echo "<OPTION value='1' selected='selected'>".$LANGAUGE_CP[139]."</OPTION>
<OPTION value='0'>".$LANGAUGE_CP[140]."</OPTION>";
}else{
echo "<OPTION value='0' selected='selected'>".$LANGAUGE_CP[140]."</OPTION>
<OPTION value='1'>".$LANGAUGE_CP[139]."</OPTION>";
}
echo "</select>
</td>
</tr>

<tr>
<td width='$td1' class='xtr'>".$LANGAUGE_CP[291]."</td>
<td width='$td2' class='xtd'>";
echo " <select size='1' name='foldergeustbook'>";

if($p_cats['foldergeustbook'] == ""){
$result = mysql_query ("SELECT * FROM pathstyle") or die ("Query failed");
while ($p_s=mysql_fetch_array($result))
{
$p_s['name'] = stripslashes($p_s['name']);
$p_s['name'] = htmlspecialchars($p_s['name']);

$p_s['pathfolder'] = stripslashes($p_s['pathfolder']);
$p_s['pathfolder'] = htmlspecialchars($p_s['pathfolder']);

if($p_s['pathfolder'] == $p_cats['folder']){
echo "<option value='".$p_s['pathfolder']."' selected='selected'>".$p_s['name']."</option>";
}else{
echo "<option value='".$p_s['pathfolder']."'>".$p_s['name']."</option>";
}
}

}else{

$result = mysql_query ("SELECT * FROM pathstyle") or die ("Query failed");
while ($p_s=mysql_fetch_array($result))
{
$p_s['name'] = stripslashes($p_s['name']);
$p_s['name'] = htmlspecialchars($p_s['name']);

$p_s['pathfolder'] = stripslashes($p_s['pathfolder']);
$p_s['pathfolder'] = htmlspecialchars($p_s['pathfolder']);

if($p_s['pathfolder'] == $p_cats['foldergeustbook']){
echo "<option value='".$p_s['pathfolder']."' selected='selected'>".$p_s['name']."</option>";
}else{
echo "<option value='".$p_s['pathfolder']."'>".$p_s['name']."</option>";
}
}
}

echo "</select>";
echo "</td>
</tr>

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

</table>

</form>";
}ELSEIF ($action=="update"){
$add_threads = $_POST['add_threads'];
$sjl_loop = $_POST['sjl_loop'];
$activeguestbook = $_POST['activeguestbook'];
$foldergeustbook = $_POST['foldergeustbook'];

$query1 =  mysql_query("UPDATE opitions SET
add_threads='$add_threads',
sjl_loop='$sjl_loop',
activeguestbook='$activeguestbook',foldergeustbook='$foldergeustbook' where id='1'");
echo $LANGAUGE_CP[34];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=guestbook_cp.php?action=setting' />";
}
echo $f;
?>