<?
function get_short_date($time1)
{ global $months_short;
$months=$months_short;
$d=getdate($time1);
$d2=$d['mon'];  
$date_string=$months[$d2-1]." ".$d['mday'];
return $date_string;
}

function get_date_f($date1)
{ 
global $months_short, $weekdays;

$months1=array('january', 'february', 'march', 'april', 'may', 'june',  
'july', 'august', 'september', 'october', 'november','december');

$months=$months_short;
$dt_mss=split('-',$date1); 
$d2=$dt_mss[1];

$dtstr=$dt_mss[2]." ".$months1[$d2-1]." ".$dt_mss[0];
$evnttime=strtotime($dtstr)+1000;
$evntd=getdate($evnttime);
$evntd2=$evntd['wday'];  

$evntwkd=$weekdays[$evntd2];

$date_string=$months[$d2-1]." ".$dt_mss[2]." ($evntwkd)";
return $date_string;
}

function slctgetstr($value)
{
$value=ereg_replace(' ', '+', $value);
$value=ereg_replace('@', '%40', $value);
$value=ereg_replace('!', '%21', $value);
return $value;
}


function get_ads_count()
{
global $cat_fields, $table_ads, $ct, $page, $adsonpage, $html_header, 
$html_footer; 
 
$where_string=get_where_string();
$sql_query="select count(idnum) from $table_ads where 
$where_string ";
if( !($sql_res=@mysql_query("$sql_query")))
{echo $html_header;
echo "
<center>
<font FACE='ARIAL, HELVETICA'  COLOR='#bb0000' size=-1><b>
Error in connecting to ads MySQL table <font color='#000099'>'$table_ads'</font>.
<br> Seems, this table is not created, 
<a href='admconf.php?md=create_tb'>click here </a> to create this table.
</b></font></center>
";
echo $html_footer;
exit;
}

$row=mysql_fetch_row($sql_res);
$count=$row[0];
return $count;
}


function delete_expevents()
{
global $cat_fields, $table_ads, $exp_period, $exp_perdhlt,$ct, $pstdevc, $flattchext;

if ($flattchext!=""){include_once("flattch.php");}


$time1=time() - $exp_period*86400;
$sql_query="select idnum from $table_ads where (catname='$ct') and 
(CURRENT_DATE > DATE_ADD(eventdt, INTERVAL $pstdevc DAY) or  eventdt='0000-00-00') ";
$sql_res=mysql_query("$sql_query");
while ($row = mysql_fetch_array ($sql_res))
{  
$del_id=$row['idnum'];
delete_events_photos($del_id);
$sql_query="delete from $table_ads where idnum=$del_id";
mysql_query("$sql_query");
 
}
 
}

function delete_events_photos($id)
{
global $photo_path, $multimedia_path, $previewphoto_path, $photos_count;

get_jpg_path($id);
get_att_path($id);

for ($i=1; $i<=$photos_count; $i++)
{
 if (file_exists($photo_path[$i])) {unlink($photo_path[$i]);}
}
if (file_exists($previewphoto_path)) {unlink($previewphoto_path);}
if (file_exists($multimedia_path)) {unlink($multimedia_path);}
}


function get_ads()
{
global $cat_fields, $table_ads,$ct, $page, $adsonpage, 
$ratedads, $plhltads, $HTTP_GET_VARS, $vstminnmbr, $evnt_cat; 
$ord_ratedads="";  
if (($ratedads=="1") or ($plhltads=="1")){$ord_ratedads="adrate desc,";}
if ($evnt_cat[$ct]=="yes") {delete_expevents($ct); $ord_ratedads="eventdt,";}

if ($HTTP_GET_VARS['adsordtp']=='vote'){$ord_ratedads=$ord_ratedads." ratevtrt desc, ";}
if ($HTTP_GET_VARS['adsordtp']=='vis'){$ord_ratedads=$ord_ratedads." cntvstr desc, "; }
if ($HTTP_GET_VARS['adsordtp']=='pml'){$ord_ratedads=$ord_ratedads." cntemll desc, ";}
if ($HTTP_GET_VARS['adsordtp']=='cmmnt'){$ord_ratedads=$ord_ratedads." replcnts desc, ";}
$html_ads="";
$start_num=($page-1)*$adsonpage; 
if($page=="")$start_num=0;
$where_string=get_where_string();

if ($ct==''){$adcmord='adcommkey,';} else {$adcmord='';}
 
//$adsonpagetemp = 55555555; 
 
$sql_query="select * from $table_ads where $where_string  
order by $ord_ratedads $adcmord idnum desc limit $start_num, $adsonpage";
#echo "$sql_query"; exit;
$sql_res=mysql_query("$sql_query");
# $html_ads=$html_ads."<p>";
while ($row = mysql_fetch_array ($sql_res))
{
$html_ads=$html_ads.print_ad($row);
} 
return $html_ads;
}

function get_where_string()
{
global $cat_fields, $ct, $table_ads, $HTTP_GET_VARS, $text_userinfo, $fntclr_1, $msg,
$vstminnmbr;

if ($HTTP_GET_VARS['brief_key']!=""){
if(get_magic_quotes_gpc()){$brf_rpq="\'";}else{$brf_rpq="'";}
$HTTP_GET_VARS['brief_key']=ereg_replace("&#039;", $brf_rpq, $HTTP_GET_VARS['brief_key']); 
}
if(!get_magic_quotes_gpc()){
foreach ($HTTP_GET_VARS as $key => $value )
{$HTTP_GET_VARS[$key]=addslashes($HTTP_GET_VARS[$key]);
}}

$adctnm1="visible=1 and ";
if ($HTTP_GET_VARS['visunvis'] == "1") {$adctnm1="";}
if ($HTTP_GET_VARS['ct'] != "")
{
$adctnm1=$adctnm1."catname='$ct' and ";
}

$where_string=$adctnm1;
$tm_check=time() - $HTTP_GET_VARS['before']*86400;

if ($HTTP_GET_VARS['before'] != "")
{$where_string=$where_string."time > $tm_check and "; 
}

if ($HTTP_GET_VARS['idemail'] != "")
{
$var_idemail=$HTTP_GET_VARS['idemail'];
$sql_query="select email from $table_ads where idnum='$var_idemail'";
$sql_res=mysql_query("$sql_query");
$row = mysql_fetch_row($sql_res);
$searchemail=$row[0];
$where_string=$where_string."email='$searchemail' and "; 
$text_userinfo="
<font FACE='ARIAL, HELVETICA' COLOR='$fntclr_1' font size='-1'>
<b>".$msg['ads_posted_by_user']."</b>
</font>
";
}

if ($HTTP_GET_VARS['mblogin'] != "")
{$where_string=$where_string."login='".$HTTP_GET_VARS['mblogin']."' and ";
$text_userinfo="
<font FACE='ARIAL, HELVETICA' COLOR='$fntclr_1' font size='-1'>
 ".$msg['ads_posted_by_member']."<b>'".$HTTP_GET_VARS['mblogin']."'</b>
</font>
";
}

$rplidval=$HTTP_GET_VARS['replyid'];
if ($rplidval != "")

{$where_string=$where_string."replyid=$rplidval and ";
$text_userinfo="&nbsp;&nbsp; &nbsp;&nbsp; 
<font FACE='ARIAL, HELVETICA' COLOR='$fntclr_1' font size='-1'>
<b>".$msg['replies_for']." 
<a href='index.php?md=details&id=$rplidval' target='inad$rplidval'>
".$msg['ad_rp']."(#$rplidval) 
</a>
</b>
</font>
";
}


if ($HTTP_GET_VARS['ratedads'] != "")
{$where_string=$where_string."adrate > 0 and ";
}

if ($HTTP_GET_VARS['adsordtp']=='pml')
{$where_string=$where_string."cntemll > 0 and ";
}

if ($HTTP_GET_VARS['adsordtp']=='cmmnt')
{$where_string=$where_string."replcnts > 0 and ";
}
 
if ($HTTP_GET_VARS['adsordtp']=='vote')
{$where_string=$where_string."ratevtcn > 0 and ";
}
if ($HTTP_GET_VARS['onlywithphoto'] != "")
{$where_string=$where_string."adphotos='yes' and ";
}

if ($HTTP_GET_VARS['ipaddr1'] != "")
{$where_string=$where_string."ipaddr1='".$HTTP_GET_VARS['ipaddr1']."' and ";
}

if ($HTTP_GET_VARS['idnum'] != "") 
{$where_string=$where_string."idnum =".$HTTP_GET_VARS['idnum']." and ";
 }

if ($HTTP_GET_VARS['brief_key'] != "") 
{$where_string=$where_string.
"(brief like '%".$HTTP_GET_VARS['brief_key']."%' or title like '%".$HTTP_GET_VARS['brief_key']."%') and ";
 }

if ($HTTP_GET_VARS['email']!=""){
$where_string=$where_string."email='".$HTTP_GET_VARS['email']."' and ";
 $cat_fields['email'][2]='keyword';}

 foreach ($cat_fields as $key => $value )
{

if($cat_fields[$key][2] == "keyword") 
{

if ($HTTP_GET_VARS[$key] != "") {  
if (($key=="city") and 
($HTTP_GET_VARS['mblogin']!="" or $HTTP_GET_VARS['idemail']!="" 
or $HTTP_GET_VARS['replyid']!="" or $HTTP_GET_VARS['email']!="" ))

{$kkttrv=1;} else
{$where_string=$where_string."$key like '%".$HTTP_GET_VARS[$key]."%' and ";}
 } 
 
}
if($cat_fields[$key][2] == "minmax")
{
$flmin=$key."1";
$flmax=$key."2";
if (($HTTP_GET_VARS[$flmin] != "") and ($HTTP_GET_VARS[$flmax] != "")) 
{
$HTTP_GET_VARS[$flmin]=ereg_replace(',', '', $HTTP_GET_VARS[$flmin]);
$HTTP_GET_VARS[$flmax]=ereg_replace(',', '', $HTTP_GET_VARS[$flmax]);

$HTTP_GET_VARS[$flmin]=preg_replace ('/[A-Za-z]|\$|,|\?|\+/', "", $HTTP_GET_VARS[$flmin]);
$HTTP_GET_VARS[$flmax]=preg_replace ('/[A-Za-z]|\$|,|\?|\+/', "", $HTTP_GET_VARS[$flmax]);



$where_string=$where_string."$key >= ".$HTTP_GET_VARS[$flmin]."
 and $key <= ".$HTTP_GET_VARS[$flmax]." and ";
}
}
}
$where_string=corr_wherestring($where_string);

if ($HTTP_GET_VARS['mfvrt']=='1'){$where_string=view_fv_ads();}

$valcntvstr=""; 
if ($HTTP_GET_VARS['adsordtp']=='vis'){$valcntvstr="cntvstr > $vstminnmbr and "; }
$where_string=$valcntvstr.$where_string; 
 
return $where_string;
}

function corr_wherestring($string1)
{
$string1=$string1."fdspkdsanbf";
$db_dcf="and fdspkdsanbf";
$string1=ereg_replace($db_dcf,"",$string1);
return $string1;
}


 
 

function get_jpg_path($id_count)
{
global $photo_url, $photo_path,  
$photos_url, $photos_path, $photos_count,
$previewphoto_url, $previewphoto_path, $multimedia_path,
$multimedia_url, $multim_ext;

for ($i=1; $i<=$photos_count; $i++)
{
$photo_url[$i]=$photos_url."p".$id_count."n".$i.".jpg";
$photo_path[$i]=$photos_path."p".$id_count."n".$i.".jpg";
}

$previewphoto_url=$photos_url."p".$id_count."prw".".jpg";
$previewphoto_path=$photos_path."p".$id_count."prw".".jpg";

}

function get_att_path($id_count)
{
global $photo_url, $photo_path,  
$photos_url, $photos_path, $photos_count,
$previewphoto_url, $previewphoto_path, $multimedia_path,
$multimedia_url, $multim_ext, $flattchext;

 
if ($flattchext!=""){$multim_ext=get_fl_ext($id_count);}

$multimedia_url=$photos_url."mtmd".$id_count.$multim_ext;
$multimedia_path=$photos_path."mtmd".$id_count.$multim_ext;
} 

function ads_pages_list()
{
global $ads_count, $adsonpage, $ct, $page, $idemail, $mblogin,  $ratedads, $visunvis, $msg;
$search_str=get_srch_str();
if ($idemail!=""){$search_str="idemail=$idemail";}
if ($mblogin!=""){
$search_str="mblogin=$mblogin";
if ($visunvis=="1"){$search_str="mblogin=$mblogin&visunvis=1";}
}
if ($ratedads=="1"){$search_str="ratedads=1";}
$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='index.php?ct=$ct&md=browse&page=$i&$search_str'>$i</a>]";
}
else
{
$list_pages=$list_pages." [<font color='#ee0000'>$i</font>]";
}
}
$list_pages=$msg['listingads'].":".$list_pages;
if ($ads_count == 0) $list_pages="";
return $list_pages;

}

function get_srch_str()
{
global $page, $ads_count, $adsonpage, $ct, $mds, $HTTP_GET_VARS;
$search_str="";
$mds_res=0;
#if($HTTP_GET_VARS['mds'] == 'search')
#{
foreach ($HTTP_GET_VARS as $key => $value)
{
if (($key !='md')  and ($key !='page') and ($key !='ct'))
{$mds_res=1;
$value=ereg_replace(' ', '+', $value);
$value=ereg_replace('@', '%40', $value);
$value=ereg_replace('!', '%21', $value);
$search_str=$search_str."$key=$value&";
}
#}
}
if ($mds_res==0)
{
$mds="";
$HTTP_GET_VARS['mds']="";
}
return $search_str;

}

function pages_next_prev()
{
global $page, $ads_count, $adsonpage, $ct, $HTTP_GET_VARS, $idemail, $mblogin,
 $ratedads, $visunvis, $msg;
$search_str=get_srch_str();
if ($idemail!=""){$search_str="idemail=$idemail";}
if ($mblogin!=""){
$search_str="mblogin=$mblogin";
if ($visunvis=="1"){$search_str="mblogin=$mblogin&visunvis=1";}
}
if ($ratedads=="1"){$search_str="ratedads=1";}
$max_pages=($ads_count-$ads_count%$adsonpage)/$adsonpage;
if ($ads_count%$adsonpage > 0) {$max_pages++;}
$next_prev="";
if ($page>1)
{
$a1_prev=$page-1;
$next_prev=$next_prev.
"<a href='index.php?ct=$ct&md=browse&page=$a1_prev&$search_str'>".$msg['previous_pg']."</a>";
}
if($page==0)$page=1;
$next_prev=$next_prev." ".$msg['page_of']." $page ".$msg['of_pg']." $max_pages ";
$a1_next=$page+1;
$a2=$page*$adsonpage;
if ($a2 < $ads_count)
{
$next_prev=$next_prev.
"<a href='index.php?ct=$ct&md=browse&page=$a1_next&$search_str'>".$msg['next_pg']."</a>";
}
if ($ads_count == 0) $next_prev="";
return $next_prev;
}



function start()
{
global $ct,$ads_fields,$categories, $ads_count, $cat_fields,$fields_sets,$allcatfields;
connect_to_db();
$c_res1=0;
foreach ($categories as $key => $value)
{
if ($key == $ct ){$c_res1=1;}
}
if ($c_res1==0){
if ($ct !=""){
echo $html_header;
echo "<h3> Incorrect Category </h3>";
echo $html_footer;
exit;
}
}
 
getfldlst();
 
$ads_count=get_ads_count();
}

function getfldlst()
{
global $ct,$ads_fields,$categories, $ads_count, $cat_fields,$fields_sets,$allcatfields;

if ($ct!="")
{
$a1=$categories[$ct][1];
$a2=$fields_sets[$a1];
}
else 
{$a2=$allcatfields;}
foreach ($a2 as $a2_value)
{
$cat_fields[$a2_value]=$ads_fields[$a2_value];
}

}


function corr_sqlstring($string1)
{
$string1=$string1."fdspkdsnbf";
$db_dcf=", fdspkdsnbf";
$string1=ereg_replace($db_dcf,"",$string1);
return $string1;
}

function connect_to_db()
{
global $host_name, $db_user,$db_password, $html_header, 
$html_footer, $db_name, $table_ads;

if (!(@mysql_connect("$host_name","$db_user","$db_password")))
{echo $html_header;
echo "
<center>
<font FACE='ARIAL, HELVETICA'  COLOR='#bb0000' size=-1><b>
Error in connecting to your MySQL server.  <br> 
<a href='admconf.php'>Click here</a> to set up correct options for
your MySQL server.
 
</b></font></center>
";
echo $html_footer;
exit;
}
if (!(@mysql_select_db("$db_name")))
{echo $html_header;
echo "
<center>
<font FACE='ARIAL, HELVETICA'  COLOR='#bb0000' size=-1><b>
Error in connecting to your MySQL database <font color='#000099'>'$db_name'</font>.
<br> <a href='admconf.php'>Click here</a> to set up correct options for
your MySQL server.
</b></font></center>
";
echo $html_footer;
exit;
}
}

?>