<?
require("mb_conf.php");
require("config.php");
include("mb_fn1.php");
 
$html_header=$mbhtml_header;
$html_footer=$mbhtml_footer;
$adsonpage=$mbadsonpage;
$ad_second_width=$mbad_second_width;
$ad_ind_width=$mbad_ind_width;
$cat_fields=$mb_fields;

if ($md=="logout") {  logout();}

connect_to_db(); 

if ($md=="print_menu") 
{
if($admpassw1 != $adm_passw){
$inc_passw="incorrect password !"; 
printpasswordform($inc_passw);
exit;
}
setcookie("admcookpassw", $admpassw1);
print_menu();
exit;
}

if($admcookpassw != $adm_passw){
if ($admcookpassw != ""){
$inc_passw= "Incorrect password or cookies does not work"; 
}
printpasswordform($inc_passw);
exit;
}

if ($md=="") print_menu();
if ($md=="create_db") create_db();
if ($md=="create_tb") create_tb();
if ($md=="sql_tb") print_sql_tb();
if ($md=="moderate") {include("mb_fn2.php"); moderate(); }
if ($md=="adm_delete") {include("mb_fn2.php"); adm_delete();}
if ($md=="make_ads_visible") make_ads_visible();
if ($md=="make_ads_unvisible") make_ads_unvisible();
if ($md=="getemails") getemails($ct);
if ($md=="del_unact_acc") del_unact_acc();

if ($md=="submdeltad") { submdeltad();}


function submdeltad()
{
global $HTTP_POST_VARS, $table_ads,$table_mb;
$array_id=get_array_id($list_id);

$admss=$HTTP_POST_VARS['submad'];
if ($admss!=""){
foreach ($admss  as $value)
{
$sql_query="update $table_mb set visible=1 where idnum=$value";
mysql_query("$sql_query");
}

}


$admss=$HTTP_POST_VARS['delad'];
if ($admss!=""){
foreach ($admss as $value)
{
$sql_query="delete from $table_mb where idnum=$value";
mysql_query("$sql_query");
}
}

$message="
<font FACE='ARIAL, HELVETICA' size='-1'>
<b>
Query has been proceeded successfully !
</b></font>
"; 
admin_message($message);
exit;
}

function del_unact_acc()
{
global  $table_mb, $categories, $html_footer, $html_header;
echo $html_header;
$time_now=time();
$time_delacc=$time_now - 3*86400;
$sql_query="delete from $table_mb where time < $time_delacc and visible=0 ";
mysql_query("$sql_query");
echo "
<font FACE='ARIAL, HELVETICA' COLOR='#880000' >
<font size='-1'><b><a href='mbadm.php'>Top:</a></b></font><p>
<center>
<b>
Unactivated membership accounts has been deleted successfuly !
</b>
</center>
</font>
";
echo $html_footer;
exit;
}

  

function getemails($ct)
{
global  $table_mb, $categories, $html_footer, $html_header;

echo "
<html>
<body bgcolor='#ffffff'>
";
echo "
<p>
<font FACE='ARIAL, HELVETICA' size='-1'>
<a href='mbadm.php'>Top:</a> <br>
<b>   
<br> Membership E-mail List: 
<font size='-2'>(Click on the link to see ad with this email)</b>
</font></font>
<p>
";
$sql_query="select idnum, email from $table_mb where  visible=1
order by email";
$sql_res=mysql_query("$sql_query");
$email1112="0";
while ($row = mysql_fetch_array ($sql_res))
{
$email1112="1";
if ($row['email']!=""){
echo "
<a href='mbind.php?md=details&id=".$row['idnum']."'>
".$row['email']."</a> <br>
";
}
}
if ($email1112=="0")
{echo "<p><font color='#aa0000'>No entries</font>";}
echo "</font> <p>";
echo "
</body>
</html>
";
exit;
}

function make_ads_visible()
{
global $list_id, $table_mb;
$array_id=get_array_id($list_id);
foreach ($array_id as $value)
{
$sql_query="update $table_mb set visible=1 where idnum=$value";
mysql_query("$sql_query");
}
$message="
<font FACE='ARIAL, HELVETICA' size='-1'><center>
<b>
Submitted membership accounts have been activated
</b></font></center>
"; 
admin_message($message);
exit;
}

function make_ads_unvisible()
{
global $list_id, $table_mb;
$array_id=get_array_id($list_id);
foreach ($array_id as $value)
{
$sql_query="update $table_mb set visible=2 where idnum=$value";
mysql_query("$sql_query");
}

$message="
<font FACE='ARIAL, HELVETICA' size='-1'>
<b><center>
Submitted membership accounts have been unactivated
</b></font></center>
"; 
admin_message($message);
exit;
}

function adm_delete()
{
global $list_id, $table_mb;
$array_id=get_array_id($list_id);
foreach ($array_id as $value)
{
$sql_query="delete from $table_mb where idnum=$value";
mysql_query("$sql_query");
 
}
$message=
"<font FACE='ARIAL, HELVETICA' color='#000088'> <center>
<b> Membership accounts have been deleted succesfully </b>
</center>
</font>
";
admin_message($message);
exit;
}

function logout()
{
setcookie("admcookpassw", "0");
printpasswordform("Log out Ok !");
exit;
} 

function get_moder_num($vis)
{
global $cat_fields, $table_mb,  $page, $adsonpage ; 
$sql_query="select count(idnum) from $table_mb where 
visible=$vis";
$sql_res=mysql_query("$sql_query");
$row=mysql_fetch_row($sql_res);
$count=$row[0];
return $count;
}

function moderate()
{
global $cat_fields, $table_mb,  $page, $adsonpage, $vis, $html_header,
$html_footer; 

if ($vis=="0") $mode_text="new profiles";
if ($vis=="2") $mode_text="univisible ads";
if ($vis=="3") $mode_text="ads edited by users";

 
$count=get_moder_num($vis);
echo $html_header;
echo"

<table width='700' >
<tr><td>
<font FACE='ARIAL, HELVETICA' size='-1'>
<b><a href='mbadm.php'>Top:</b>
</font>
</td></tr></table>
<table width='700' bgcolor='#dddddd'>
<tr><td>
<center>
<font FACE='ARIAL, HELVETICA'>
<b>Browsing $mode_text</b>
</font>
</center>
</td></tr></table>
";
echo"
 <table width='700' bgcolor='#eeeeee'>
<tr><td>
<font FACE='ARIAL, HELVETICA' size='-1'>
Pages:".moder_pages_list($count)."
</td></tr></table>
 <p>
";

echo get_moder_ads();
echo $html_footer;
exit;
}

 
function get_moder_ads()
{
global $cat_fields, $gener_fields, $specific_fields, 
$table_mb, $ct, $page,$adsonpage, $vis, $ads_fields; 

$html_ads="";
$start_num=($page-1)*$adsonpage; 
if($page=="")$start_num=0;
$sql_query="select * from $table_mb where visible=$vis
order by idnum desc limit $start_num, $adsonpage";
 $sql_res=mysql_query("$sql_query");
$html_ads=$html_ads."<p>";

$html_ads=$html_ads."
<form action='mbadm.php?md=submdeltad' method='post'>
";

while ($row = mysql_fetch_array ($sql_res))
{
$html_ads=$html_ads.print_moder_ad($row);
}
$html_ads=$html_ads.""; 
$html_ads=$html_ads."
<input type='submit' value='Submit'></form> 
";
return $html_ads;
}

function print_moder_ad ($row)
{
global $cat_fields, $photos_count, $all_fields, $photo_path,$photo_url,
$previewphoto_path,$prphotolimits,$pr_lim_width,$pr_lim_height,
$previewphoto_url,  $select_text, $real_format, $mb_fields, $categories;

$html_ad="
<table width='700' bgcolor='#eeeeee'><tr><td>
<font FACE='ARIAL, HELVETICA' size=-1>
";
$idnum=$row['idnum'];

$time1=$row['time'];
$date_posted=get_date($time1); 
$catname=$row['catname'];
$html_ad=$html_ad."id: $idnum; Date posted: $date_posted<br>";
foreach ($mb_fields as $key => $value) 
{
if (($row[$key]!="") and
($row[$key]!=$select_text) and ($row[$key]!='http://')
and ($row[$key]!='NULL') and ($row[$key]!='null'))
{
if ($all_fields[$key][6]=='real') {$row[$key]=sprintf($real_format, $row[$key]);}
$html_ad=$html_ad."
<font color='#000099'>".$mb_fields[$key][0]."</font> : $row[$key] <br>
";
}
}
$html_ad=$html_ad."
<br><a href='mbind.php?md=editform&ed_id=$idnum' target='edit$idnum'>
<font size='-2'><b>Edit this entry</a>

&nbsp;&nbsp; 
Submit<input type='checkbox' name='submad[]' value='$idnum'>
&nbsp;&nbsp; 
Delete<input type='checkbox' name='delad[]' value='$idnum'>
</b></font>

";
$html_ad=$html_ad."</td></tr></table><p>";
return $html_ad;
}
 
 

function moder_pages_list($ads_count)
{
global $adsonpage, $ct, $page, $vis;

$num_pages=($ads_count-$ads_count%$adsonpage)/$adsonpage;
if ($ads_count%$adsonpage > 0) {$num_pages++;}
$list_pages="";
for ($i = 1; $i <= $num_pages; $i++) 
{
if ($i != $page){
$list_pages=$list_pages."[<a href='mbadm.php?md=moderate&vis=$vis&page=$i'>$i</a>]";
}
else
{
$list_pages=$list_pages."[$i]";
}
}
return $list_pages;
}

function  printpasswordform($inc_passw)
{
global $html_header, $html_footer;

echo $html_header; 
echo "
 

<p>
<font FACE='ARIAL, HELVETICA' COLOR='#990000'  >
<b> $inc_passw </b>
</font>
<p>
<form action='mbadm.php?md=print_menu' method='post'>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'>
<b>
Input admin password : </b></font>
<input type='password' name='admpassw1'>
<input type='submit' value='Submit'>
</form>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-2'>
<br><b>(Cookies must be set up)
 </b></font>
 
<p>
 
";
echo $html_footer;
exit;
}

function print_menu()
{
global $html_header, $html_footer, $db_name, $table_mb, 
$moderating, $categories;
echo $html_header;
 
echo "
</center>
<table width='850'  border=0 cellspacing=0 cellpadding=1>
<tr><td width='50%' valign='top'>
<font FACE='ARIAL, HELVETICA' size='-1' >
 
<b> 
<a href='admin.php'>Main admin interface</a> &nbsp; &nbsp; &nbsp;
<a href='admconf.php'>Setting Options</a>  
</b>
 
<p>
<b><a href='mbind.php'>Membership Database</a>
&nbsp;&nbsp;&nbsp; 
<a href='mbind.php?ct=$ct&md=add_form' >Submit new member</a>
<p>
<font size='-1'>
<a href='mbadm.php?md=moderate&vis=0'  >
New unactivated accounts</a> (".get_moder_num("0").")
<p>
<a href='mbadm.php?md=del_unact_acc'  >
Delete new accounts that is unactivated during 3 days</a>
<p>
<a href='mbadm.php?md=moderate&vis=2' >
Accounts unactivated by admin</a> (".get_moder_num("2").")  
</b>
</font>
</font>
 
 
<p>
<font FACE='ARIAL, HELVETICA' size=-1>
<b><a href='mbadm.php?ct=$key&md=getemails'>
Print members' contact e-mails
</a>
</b>

<p><b><a href='mbadm.php?md=logout'>Log Out</a></b>

</font>

</td><td width='50%' valign='top' align='left'>

<table width='450' bgcolor='#dddddd' border=0 cellspacing=0 cellpadding=1>
<tr><td> 
<font FACE='ARIAL, HELVETICA' size=-1>
<b>Make accounts  active/unactive</b>
</font></td></tr><tr><td>
<TABLE   bgcolor='#ffffff' border='0' cellspacing=0 cellpadding=0 WIDTH='100%' >
<tr><td> 
<table> 
<tr><td valign='top'> 
<form action='mbadm.php?md=make_ads_visible' method='post'>
<center>
<b>ID#:
</b><input type='text' name='list_id'>
</td><td valign='top'>
<input type='submit' value='Make accounts active'>
</form>
</td></tr><tr><td  valign='top'> 
<form action='mbadm.php?md=make_ads_unvisible' method='post'>
<center>
<b>ID#:</b><input type='text' name='list_id'>
</td><td valign='top'>
<input type='submit' value='Make accounts unactive'>
</td></tr></table>
</form>

<font FACE='ARIAL, HELVETICA' size=-2><b>
separate by comma, e.g. if you input 12,22,34-37,40  then  will be
submitted accounts with the following id: 12,22,34,35,36,37,40 
</b></font>
</center>
 

</td></tr></table>
</td></tr></table>

<p>

<table width='450' bgcolor='#dddddd' border=0 cellspacing=0 cellpadding=1>
<tr><td> 
<font FACE='ARIAL, HELVETICA' size=-1>
<b>Delete membership accounts</b>
</font></td></tr><tr><td>
<TABLE   bgcolor='#ffffff' border='0' cellspacing=0 cellpadding=0 WIDTH='100%' >
<tr><td> 

<form action='mbadm.php?md=adm_delete' method='post'>
<center>
<b>ID#:</b><input type='text' name='list_id'>
<input type='submit' value='Delete'>
</center>
<p><font FACE='ARIAL, HELVETICA' size=-2><b>
Separate by comma, e.g. if you input 12,22,34-37,40  then  will be
deleted accounts with the following id: 12,22,34,35,36,37,40 
</b></font>
 
</form>

</td></tr></table>
</td></tr></table>
<p>
<table width='450' bgcolor='#dddddd' border=0 cellspacing=0 cellpadding=1>
<tr><td> 
<font FACE='ARIAL, HELVETICA' size=-1>
<b>Edit membership account</b>
</font></td></tr><tr><td>
<TABLE   bgcolor='#ffffff' border='0' cellspacing=0 cellpadding=0 WIDTH='100%' >
<tr><td> <center>
<form action='mbind.php?md=editform' method='post'>
<b>ID#:</b><input type='text' name='ed_id'>
<input type='submit' value='Edit'>
</center>
</form>
</td></tr></table>
</td></tr></table>
</center>
<p>

 
</td></tr></table>
</center>";
echo $html_footer;
exit;
}

 

function get_array_id($string_id)
{
$i=0;
$string1=split(",",$string_id);
foreach ($string1 as $value1)
{
$string2=split("-", $value1);
if ($string2[1]!="")
{
for ($j=$string2[0];$j<=$string2[1];$j++)
{$array_id[$i]=$j; $i++;}
}
else{$array_id[$i]=$value1; $i++;}
}
return $array_id;
}

 
function admin_message($message)
{
global $cat_fields, $photos_count, $html_header, $html_footer, $id,
$ct, $categories, $ad_second_width, $left_width_sp, $exp_period;
echo $html_header;
echo "
<center><table width='600'><tr><td>
<font FACE='ARIAL, HELVETICA' COLOR='BLACK'> <font size=-1>
<b><a href='mbadm.php'>Top:</a></b></font>
&nbsp; 
<hr size='1'><p>
$message
<p><hr size='1'>
</tr></td></table>
</center>
";
echo $html_footer;
exit;
}
 
?>