<?
require("mb_fn2.php");

function check_login_info($login11)
{
global $html_header, $html_footer, $table_mb, $ed_id, $msg2; 

$sql_query="select count(idnum) from $table_mb where login='$login11' ";
$sql_res=mysql_query("$sql_query");
$row=mysql_fetch_row($sql_res);
$count=$row[0];
if ($count > 0 )
{
$sql_query="select idnum from $table_mb where login='$login11' ";
$sql_res=mysql_query("$sql_query");
$row=mysql_fetch_row($sql_res);
$count_id=$row[0];
if ($count_id!=$ed_id)
{
echo $html_header; 
echo "
<center>
<font FACE='ARIAL, HELVETICA' >
<b> ".$msg2['Login_ch']." <font color='#ff0000'> $login11 </font> ".$msg2['already exist_z']." </b>
</font>
<p>
<font FACE='ARIAL, HELVETICA' size=-1>
".$msg2['input_another_login_password']."
</font>
</center>
";
echo $html_footer;
 
exit;
}
}

}


function edit_ad()
{
global $ed_passw, $ed_id, $table_mb, $moderating, $adphotos, $HTTP_POST_VARS,  $adm_passw, $admcookpassw;

check_fields();

$sql_query="select passw, exptime from $table_mb where idnum=$ed_id";
$sql_res=mysql_query("$sql_query");
$row = mysql_fetch_array ($sql_res);
 
if ($row['passw']!="$ed_passw")
{
if ($adm_passw!="$ed_passw")
{
if($admcookpassw != $adm_passw)
{
echo "incorrect password";
exit;
}
}
}


$db_entry=form_sql_entry($ed_id);
$db_entry['idnum']="";
$db_entry['adphotos']=$adphotos;
$db_entry['time']="";
$db_entry['exptime']="";

$adddays=0;
if ($HTTP_POST_VARS['addexp']!="")
{$adddays=$HTTP_POST_VARS['addexp'];
$time2=time();
if ($row['exptime'] < $time2){$db_entry['exptime']=$time2+$adddays*86400;}
else {$db_entry['exptime']=$row['exptime']+$adddays*86400;}
}
 
$db_entry['catname']="";
$sql_string="";
foreach ($db_entry as $db_key =>$db_value)
{
# $db_entry[$db_key]=~ s/"/'/g;
if ($db_entry[$db_key]!=""){
$sql_string=$sql_string."$db_key='$db_entry[$db_key]', ";
}
}
$sql_string=corr_sqlstring($sql_string);

$sql_query="update $table_mb set $sql_string where idnum=$ed_id";
mysql_query("$sql_query");

print_subm_ad($ed_id,'editad');
exit;
}

function get_idnum()
{

global $id_count, $table_mb, $ct;
$sql_query="select idnum from $table_mb 
order by idnum desc limit 1";
$sql_res=mysql_query("$sql_query");
$row=mysql_fetch_row($sql_res);
$id_count=$row[0];
$id_count++;

return $id_count;
}

function check_fields()
{
global $db_entry, $visible_val, $cat_fields, $email, $ed_id,
$HTTP_POST_VARS, $photos_count, $phptomaxsize, $userfile, $prviewphotomax,
$incl_prevphoto, $incl_mtmdfile, $mtmdfile_maxs, $userfile_name, $select_text, $msg2;
 
if(!get_magic_quotes_gpc())
{

foreach ($HTTP_POST_VARS as $key5 => $value )
{
$HTTP_POST_VARS[$key5]=addslashes($HTTP_POST_VARS[$key5]);
}
}



check_login_info($HTTP_POST_VARS['login']); 

foreach ( $cat_fields as $key => $value )
{
$HTTP_POST_VARS[$key]=ereg_replace('<', '&#060;', $HTTP_POST_VARS[$key]);
$HTTP_POST_VARS[$key]=ereg_replace('>', '&#062;', $HTTP_POST_VARS[$key]);

if ($cat_fields[$key][2]=="minmax")
{ $HTTP_POST_VARS[$key]=ereg_replace(',', '', $HTTP_POST_VARS[$key]); }

if ($cat_fields[$key][4]=="checkbox")
{
$aa5=split('<option>',$cat_fields[$key][7]);
$i_aa5=0;
foreach ($aa5 as $value1)
{
$i_aa5++;
$namechbx=$key.$i_aa5;
if ($HTTP_POST_VARS[$namechbx]!="")
{$HTTP_POST_VARS[$key]=$HTTP_POST_VARS[$key].$HTTP_POST_VARS[$namechbx]."; ";}
}
}


$str_length1=strlen($HTTP_POST_VARS[$key]);
$aa4=split(':',$cat_fields[$key][3]);
$fmaxsize=$aa4[1];
if ($aa4[2]!="") $fmaxsize=$aa4[2];
if ( $str_length1 > $fmaxsize)
{
$message="
<center>
<font FACE='ARIAL, HELVETICA' COLOR='#880000' >
  ".$msg2['Ad_info_in_field']." <b>".$cat_fields[$key][0]." </b>".$msg2['is_too_large_inf']."
 </font>
</center>
";
output_message($message);
exit;
}

if ($HTTP_POST_VARS[$key]==$select_text)
{$HTTP_POST_VARS[$key]="";}

if ($cat_fields[$key][5]=='1')
{
if (( $HTTP_POST_VARS[$key]=="") or ( $HTTP_POST_VARS[$key]=="http://"))
{
$message="
<center>
<font FACE='ARIAL, HELVETICA' COLOR='#880000' >
 ".$msg2['Ad_field_c']." <b>".$cat_fields[$key][0]."</b>".$msg2['was_mising_on_form']."</font>
</center>
";
output_message($message);
exit;
}
}
}
if ($cat_fields['email'][5]='1'){
$HTTP_POST_VARS['email']=check_email($HTTP_POST_VARS['email']);
}


 

}
 
 

function submit_ad()
{
global $db_entry, $visible_val, $cat_fields, $email, $HTTP_POST_VARS;

global $photo_url, $photo_path, $id_count, 
$userfile, $userfile_name,
$photos_url, $photos_path, $photos_count, $moderating, $ad_idnum;

check_fields();

$visible_val="1";

$idnum=get_idnum();
 
$sql_query=add_sql_entry($idnum);

mysql_query("$sql_query");
 

print_subm_ad($ad_idnum,'submitad');
exit;
}



function print_subm_ad($ad_idnum,$ed_subm)
{
global $cat_fields, $photos_count, $html_header, $html_footer, $id,
$ct, $categories, $ad_second_width, $left_width_sp, $exp_period, $incl_prevphoto,
$prphotolimits, $pr_lim_width, $pr_lim_height, $previewphoto_path, $previewphoto_url,
$moderating, $savepherr, $msg2, $msg;

$sdtpcol="#ffffff";
$sdtpcol1="#ffcccc";

$row=get_edit_info($ad_idnum);
$row=check_row($row);

$time1=$row['time'];
$date_posted=get_date($time1); 
$time2=$row['exptime'];
$date_expp=get_date($time2); 
 
if ($ed_subm=='editad') 
{ $info11="
 ".$msg2['Member_acc_edited_succ']." 
";
}

if ($ed_subm=='submitad') 
{ $info11="
 ".$msg2['Member_acc_subm_succ']." 
";
}

 
 
if ($savepherr=="1")
{ $info11=$info11."
<br>
<font FACE='ARIAL, HELVETICA' COLOR='#aa0000' size='-1'>
Due to incorrect server configuration your 
photos does not saved into database 
</font>
";
}

echo $html_header;
echo "
<center><table width='500'><tr><td>
<font FACE='ARIAL, HELVETICA' COLOR='BLACK'> <font size=-1>
<b><a href='mbind.php'>".$msg['top'].":</a></b></font>
&nbsp; 
 
<hr size='1'>
<center>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='+1'> 
<b> $info11 </b></font>
</center><p>
";

echo "
<table width='100%' border=0 cellspacing=3 cellpadding=3>
<tr>
<td bgcolor='#ddeeff'>
<font FACE='ARIAL, HELVETICA' color='#000088' size=+1>
  &nbsp; <b>".$row['login']."</b></font>
</td>
</tr>
</table>
<table width='100%'  bgcolor='#ffffff' cellspacing=5 cellpadding=5>
<tr><td>
<font size=-1>
<font color='#007700' >&nbsp;&nbsp;".$msg['adsid']."  : </font><font color='#aa0000'>
".$row['idnum']."
</font>; 
&nbsp;
<font color='#007700' >  ".$msg['date_posted_d'].": </font> <font color='#aa0000' >
 $date_posted </font>;
&nbsp;
<font color='#007700' >  ".$msg['expire_date_d'].": </font> <font color='#aa0000' >
 $date_expp </font>;
 
<table width='100%'  border=0 bordercolor='#ffffff' cellspacing=3 cellpadding=0>
 
";


foreach ( $cat_fields as $key => $value )
{
echo "
<tr><td colspan='2' height='1' bgcolor='$sdtpcol1'><spacer type='block' height='1' width='1'></td></tr>
<tr><td bgcolor='$sdtpcol' width='40%'>
<font FACE='ARIAL, HELVETICA' COLOR='#000088' size='-1'>
&nbsp; ".$cat_fields[$key][0].":</font>
</td><td bgcolor='$sdtpcol' width='60%'>
<font FACE='ARIAL, HELVETICA' COLOR='BLACK' size='-1'>
 ".$row[$key]." &nbsp; </td></tr>
 
";
}

echo "
<tr><td colspan='2' height='1' bgcolor='$sdtpcol1'><spacer type='block' height='1' width='1'></td></tr>
</table></td></tr></table>
";

echo "
<p>
<center>
<font FACE='ARIAL, HELVETICA' COLOR='#000088' size='-1'>
 
</font>
</center>
<p>
";

echo  "
<hr size='1'>
</tr></td></table>
";

echo $html_footer;

exit;
}

function form_sql_entry($idnum)
{
global $ct,  $table_mb, $visible_val, $cat_fields,
$HTTP_POST_VARS, $moderating, $adphotos, $feembshp, $mbrshp_per;

$time_now=time();
 
$db_entry['idnum']=$idnum;

 
$db_entry['time']=$time_now;

$adddays=0;
if ($HTTP_POST_VARS['addexp']!=""){$adddays=$HTTP_POST_VARS['addexp'];}
$db_entry['exptime']=$time_now+($mbrshp_per+$adddays)*86400;


$db_entry['visible']=$visible_val;
 
foreach ($cat_fields as $key => $value)
{
$db_entry[$key]=$HTTP_POST_VARS[$key];
}
 
return $db_entry;
}
 
function add_sql_entry($idnum)
{
global $ct, $db_entry, $table_mb, $visible_val, $cat_fields,
$HTTP_POST_VARS ,$ad_idnum;

 
$ad_idnum=$idnum;
$db_entry=form_sql_entry($idnum);
 
$db_sql_string="";
$db_sql_var="";
foreach ($db_entry as $db_key => $db_value)
{
#$db_entry[$db_key]=ereg_replace("\"", "'", $db_entry[$db_key]);
if ($db_key !="")
{
if ($db_entry[$db_key] == "" )
{
$db_sql_string=$db_sql_string."NULL, ";
}
else{
$db_sql_string=$db_sql_string."'$db_entry[$db_key]', ";
}
$db_sql_var=$db_sql_var."$db_key, ";
}
}
$db_sql_string=corr_sqlstring($db_sql_string);
$db_sql_var=corr_sqlstring($db_sql_var);
$sql="insert into $table_mb ( $db_sql_var ) values( $db_sql_string )";

return $sql;

} 

?>
