Location: PHPKode > projects > OrangeDeals > orangedeals_v1.2/http/functions.php
<?php


$db_id = mysql_connect($db_host, $db_user, $db_pass)
 or die("Could not connect to DB.");
mysql_select_db($db_name, $db_id)
 or die("Database not found.");
 //time difference; gets for how much the mysql server time is ahead, compared to the http server time;
 $query="SELECT timediff(now(), '".date("Y-m-d H:i:s")."')";
 $result=mysql_query($query, $db_id);
 $tdif=mysql_fetch_row($result); $tdif=explode(":", $tdif[0]);
 if ($tdif[0][0]=="-") {$tdif[0]=abs($tdif[0]); $tdif[3]="-";}
 else $tdif[3]="+"; $tdif=" ".$tdif[3]." interval ".$tdif[0]." hour ".$tdif[3]." interval ".$tdif[1]." minute ".$tdif[3]." interval ".$tdif[2]." second";
 
 session_start();

if (!isset($_SESSION['initiated']))
{
    session_regenerate_id();
    $_SESSION['initiated'] = true;
}//just a security tip


if (isset($_SESSION["user"][0])) {include "language/".$_SESSION["user"][15]; $lang_file=$_SESSION["user"][15];}

if (isset($_SESSION["lang"])) {include "language/".$_SESSION["lang"]; $lang_file=$_SESSION["lang"];}

if (!isset($_SESSION["lang"]) && !isset($_SESSION["user"][0])) {$_SESSION["lang"]=$default_language_file; include "language/".$default_language_file; $lang_file=$default_language_file;}

$title=$lang['title'];
 
 
 
 
  if (isset($_SESSION["user"][0])) {$template=$_SESSION["user"][12];}
 else {$template="default";}

if (isset($_SESSION["user"][0]))
{
 	$query="select * from users where id='".$_SESSION["user"][0]."' and timediff(now(), lastVisit)>'00:03:58'";
	$usr=user($_SESSION["user"][0]);
	$credits=$usr[18];
 $result=mysql_query($query, $db_id);
 $num_rows = mysql_num_rows($result);
 if ( $num_rows>0) {
$update_visit=update_lastVisit($_SESSION["user"][0]);
}
 
 //set the que for every city home offer
 $query_a="select * from cities";
 $result_a=mysql_query($query_a, $db_id);
 
 for ($i=0; $row_a=mysql_fetch_row($result_a); $i++) {
 $cities_a[$i]=$row_a;


$query_b="select * from offers where (date_published_in_home+duration)>'".time()."' and date_approved!='' and date_published_in_home!='' and target_city='".$cities_a[$i][0];
$result_b=mysql_query($query_b, $db_id);

if(!$result_b){
 $query_c="select * from offers where date_approved!='' and date_published_in_home='' and target_city='".$cities_a[$i][0]."' order by date_posted desc LIMIT 1";
 $result_c=mysql_query($query_c, $db_id); 
 $row_c=mysql_fetch_row($result_c);
 
   $query_d="update offers set date_published_in_home='".time()."' where id=".$row_c[0];
   $result_d=mysql_query($query_d, $db_id);
  
   }
   
   $query_wc="select * from offers where (date_published_in_home+duration)>'".time()."' and date_approved!='' and date_published_in_home!='' and for_whole_country='1'";
$result_wc=mysql_query($query_wc, $db_id);

if(!$result_wc){
 $query_wc2="select * from offers where date_approved!='' and date_published_in_home='' and  for_whole_country='1' order by date_posted desc LIMIT 1";
 $result_wc2=mysql_query($query_wc2, $db_id); 
 $row_wc2=mysql_fetch_row($result_wc2);
 
   $query_wc3="update offers set date_published_in_home='".time()."' where id=".$row_wc2[0];
   $result_wc3=mysql_query($query_wc3, $db_id);

   }
   
}

function Sec2Time($time_sec){
  if(is_numeric($time_sec)){
    $value = array(
     "hours" => 0,
      "minutes" => 0, "seconds" => 0,
    );
    if($time_sec >= 3600){
      $value["hours"] = floor($time_sec/3600);
      $time_sec = ($time_sec%3600);
    }
    if($time_sec >= 60){
      $value["minutes"] = floor($time_sec/60);
      $time_sec = ($time_sec%60);
    }
    $value["seconds"] = floor($time_sec);
    return (array) $value;
  }else{
    return (bool) FALSE;
  }
}

}



function menu_rear()
{

global $lang,$template;

echo "<font class='q_text_sm'>language:</font> ";


             $dir=dir("language/");
             while($filename=$dir->read()) {
			 $show_lang_letters=explode(".",$filename);
			  if ($filename[0]!=".")   echo "<a class='q_link' href='ch_lang.php?lang=".$filename."'> ".$show_lang_letters[0]." ";

			  }
             $dir->close();


echo "<br><a class='q_link' href='index.php'>".$lang['home']."</a><br>";
if (!isset($_SESSION["user"][1])) echo "<a class='q_link' href='login.php'>".$lang['login']."</a> 
<br><a class='q_link' href='register.php'>".$lang['register']."</a>";
else echo "<a class='q_link' href='logout.php'>".$lang['logout']."</a> ";
if (isset($_SESSION["user"][1])) echo "<br><a class='q_link' href='profile_view.php?id=".$_SESSION["user"][0]."'>".$lang['profile']."</a> ";

 if (isset($_SESSION["user"][0])) $alert=msg_rep_alert($_SESSION["user"][0]);
 if (isset($alert[1][0])) $alert[1]="<font color='red'>".$alert[1][0]."</font> "; else $alert[1]="";
 if (isset($_SESSION["user"][1])) echo "<br><a class='q_link' href='messages.php?page=0'>".$alert[1].$lang['messages']."</a> ";

}

function menu_down()
{
// include "info.php"; no needed any more for chat users...the line below added
global $lang,$db_id;
 
 if (isset($_SESSION["user"][0])){
 $usr=user($_SESSION["user"][0]);
 
 if ((isset($_SESSION["user"][5]))&&($_SESSION["user"][5]>3)) echo " | <a class='q_link' href='apanel.php'>".$lang['adminPanel']."</a> | ";
 }
}




function about()
{global $lang;

echo"<table width='100%' border='0' cellspacing='3' cellpadding='3'>
  <tr>
    <td valign='top' width='50%' bgcolor='#666666'><font class='footer_text'><a class='q_link' href='pages.php?id=1'>".$lang['become_city_conector']."</a><br><br></font></td>
    <td valign='top' width='50%' bgcolor='#666666'><font class='footer_text'>".$lang['about_btm']."</font>
	<font color=\"#000000\" class=\"q_text_sm\">Powered by </font><a href=\"http://www.tecorange.com\" target=\"_blank\" class=\"q_link\"><font class=\"q_text_sm\" ><b><font color=\"#CC6600\">OrangeDeals ".odeals_version()."</font></b></font></a><font color=\"#000000\" class=\"q_text_sm\"> Open Source Software<br></font>
	</td>
  </tr>
</table>"; 
}





function currencies()
{
global $db_id;
 $query="select * from config where ord='5'";
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 $currencies=explode("-",$row[1]);
 return $currencies;
}

function city($id)
{
 global $db_id;

 $query="select * from cities where id=".$id;
 $result=mysql_query($query, $db_id);
 
 $row=mysql_fetch_row($result);
 return $row;
 }
 

function payment_ways()
{
 global $db_id;

 $payment_ways=array();
 $query="select * from payment_ways";
 $result=mysql_query($query, $db_id);
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) $payment_ways[$i]=$row;
 return $payment_ways;
}


 
function home_offers($id){ //id is the user id so to see the towns he is interested
 global $db_id,$biggest_city;

if ($id!='not_member'){
$usr=user($id);
$usr[11]=explode("-",$usr[11]);

$query="select * from offers where (date_published_in_home+duration)>'".time()."' and date_approved!='' and (target_city='".$usr[11][0]."' or target_city='".$usr[11][1]."' or target_city='".$usr[11][2]."' or for_whole_country='1') order by (date_published_in_home+duration) desc LIMIT 4";
$result=mysql_query($query, $db_id);

$num_rows=mysql_num_rows($result);
if($num_rows<=0){

$query="select * from offers where (date_published_in_home+duration)>'".time()."' and date_approved!='' and target_city='".$biggest_city."' order by (date_published_in_home+duration) desc LIMIT 4";
$result=mysql_query($query, $db_id);

}

 }
 
if(!isset($result)){
 $query="select * from offers where (date_published_in_home+duration)>'".time()."' and date_approved!='' and (target_city='".$biggest_city."' or for_whole_country='1') order by (date_published_in_home+duration) desc LIMIT 4";
 $result=mysql_query($query, $db_id);
 }

if($result){

 $offers=array();
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers[$i]=$row;
 $offers[$i][2]=stripslashes($row[2]); 
 $offers[$i][3]=stripslashes($row[3]);
 $offers[$i][4]=stripslashes($row[4]);
 $offers[$i][5]=stripslashes($row[5]);
 $offers[$i][6]=stripslashes($row[6]);
 $offers[$i][7]=stripslashes($row[7]);
 $offers[$i][8]=stripslashes($row[8]);
 $offers[$i][9]=stripslashes($row[9]);
 $offers[$i][10]=stripslashes($row[10]);
 $offers[$i][11]=stripslashes($row[11]);
 $offers[$i][12]=stripslashes($row[12]);
 $offers[$i][13]=stripslashes($row[13]);
 $offers[$i][14]=stripslashes($row[14]);
 $offers[$i][15]=stripslashes($row[15]);
 $offers[$i][16]=stripslashes($row[16]);
 $offers[$i][17]=stripslashes($row[17]);
 }
 }
 return $offers;
}



 function offers_c($id){ //id is the city id 
 global $db_id;

if($id==10000){
$query="select * from offers where (date_published_in_home+duration)>'".time()."' and date_approved!='' and for_whole_country='1'  order by (date_published_in_home+duration) desc LIMIT 4";
}else{
$query="select * from offers where (date_published_in_home+duration)>'".time()."' and date_approved!='' and target_city='".$id."' and for_whole_country='' order by (date_published_in_home+duration) desc LIMIT 4";
}
$result=mysql_query($query, $db_id);

if($result){

 $offers=array();
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers[$i]=$row;
 $offers[$i][2]=stripslashes($row[2]); 
 $offers[$i][3]=stripslashes($row[3]);
 $offers[$i][4]=stripslashes($row[4]);
 $offers[$i][5]=stripslashes($row[5]);
 $offers[$i][6]=stripslashes($row[6]);
 $offers[$i][7]=stripslashes($row[7]);
 $offers[$i][8]=stripslashes($row[8]);
 $offers[$i][9]=stripslashes($row[9]);
 $offers[$i][10]=stripslashes($row[10]);
 $offers[$i][11]=stripslashes($row[11]);
 $offers[$i][12]=stripslashes($row[12]);
 $offers[$i][13]=stripslashes($row[13]);
 $offers[$i][14]=stripslashes($row[14]);
 $offers[$i][15]=stripslashes($row[15]);
 $offers[$i][16]=stripslashes($row[16]);
 $offers[$i][17]=stripslashes($row[17]);
 }
 }
 return $offers;
}



function offers_c_all($id){ //id is the city id 
 global $db_id;

if($id==10000){
$query="select * from offers where date_approved!='' and for_whole_country='1'  order by date_posted desc";
}else{
$query="select * from offers where date_approved!='' and target_city='".$id."' and for_whole_country='' order by date_posted";
}


$result=mysql_query($query, $db_id);

if($result){

 $offers=array();
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers[$i]=$row;
 $offers[$i][2]=stripslashes($row[2]); 
 $offers[$i][3]=stripslashes($row[3]);
 $offers[$i][4]=stripslashes($row[4]);
 $offers[$i][5]=stripslashes($row[5]);
 $offers[$i][6]=stripslashes($row[6]);
 $offers[$i][7]=stripslashes($row[7]);
 $offers[$i][8]=stripslashes($row[8]);
 $offers[$i][9]=stripslashes($row[9]);
 $offers[$i][10]=stripslashes($row[10]);
 $offers[$i][11]=stripslashes($row[11]);
 $offers[$i][12]=stripslashes($row[12]);
 $offers[$i][13]=stripslashes($row[13]);
 $offers[$i][14]=stripslashes($row[14]);
 $offers[$i][15]=stripslashes($row[15]);
 $offers[$i][16]=stripslashes($row[16]);
 $offers[$i][17]=stripslashes($row[17]);
 }
 }
 return $offers;
}




function bank_payments($sw)
{
 global $db_id;

if($sw==1){
 $query="select * from bank_payments where date_credits_added='' order by date_send desc";
 $result=mysql_query($query, $db_id); $offers=array();
 }
 
 if($sw==2){
 $query="select * from bank_payments where date_credits_added!='' order by date_send desc";
 $result=mysql_query($query, $db_id); $offers=array();
 }
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$bank_payments[$i]=$row;
  $bank_payments[$i][1]=stripslashes($row[1]);
 $bank_payments[$i][2]=stripslashes($row[2]); 
 $bank_payments[$i][3]=stripslashes($row[3]);
 $bank_payments[$i][4]=stripslashes($row[4]);
 $bank_payments[$i][5]=stripslashes($row[5]);
 $bank_payments[$i][6]=stripslashes($row[6]);
 $bank_payments[$i][7]=stripslashes($row[7]);
 $bank_payments[$i][8]=stripslashes($row[8]);
 $bank_payments[$i][9]=stripslashes($row[9]);
  }
 return $bank_payments;
}




 
 function offers_ad($sw)
{
 global $db_id;

if($sw==1){
 $query="select * from offers where date_checked_by_adm='' order by date_posted desc";
 $result=mysql_query($query, $db_id); $offers=array();
 }
 
 if($sw==3){
 $query="select * from offers where date_approved!='' and date_published_in_home='' order by date_posted desc";
 $result=mysql_query($query, $db_id); $offers=array();
 }
 
  if($sw==4){
 $query="select * from offers where date_checked_by_adm!='' and date_approved='' order by date_posted desc";
 $result=mysql_query($query, $db_id); $offers=array();
 }
 
   if($sw==5){
 $query="select * from offers where (date_published_in_home+duration)>'".time()."' order by date_posted desc";
 $result=mysql_query($query, $db_id); $offers=array();
 }
 
    if($sw==6){
 $query="select * from offers where (date_published_in_home+duration)<'".time()."' order by date_posted desc";
 $result=mysql_query($query, $db_id); $offers=array();
 }
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers[$i]=$row;
 $offers[$i][2]=stripslashes($row[2]); 
 $offers[$i][3]=stripslashes($row[3]);
 $offers[$i][4]=stripslashes($row[4]);
 $offers[$i][5]=stripslashes($row[5]);
 $offers[$i][6]=stripslashes($row[6]);
 $offers[$i][7]=stripslashes($row[7]);
 $offers[$i][8]=stripslashes($row[8]);
 $offers[$i][9]=stripslashes($row[9]);
 $offers[$i][10]=stripslashes($row[10]);
 $offers[$i][11]=stripslashes($row[11]);
 $offers[$i][12]=stripslashes($row[12]);
 $offers[$i][13]=stripslashes($row[13]);
 $offers[$i][14]=stripslashes($row[14]);
 $offers[$i][15]=stripslashes($row[15]);
 $offers[$i][16]=stripslashes($row[16]);
 $offers[$i][17]=stripslashes($row[17]);
 }
 return $offers;
}
 
 
 function requests_ad($sw)
{
 global $db_id,$init_users_needed_to_create_team;

if($sw==1){
 $query="select * from requests where date_checked='' and date_closed='' order by date_posted desc";
 $result=mysql_query($query, $db_id); $requests=array();
 }
 
 if($sw==3){
 $query="select * from requests where date_approved!='' and date_closed='' order by date_posted desc";
 $result=mysql_query($query, $db_id); $requests=array();
 }
 
  if($sw==4){
 $query="select * from requests where date_checked!='' and date_approved='' and date_closed='' order by date_posted desc";
 $result=mysql_query($query, $db_id); $requests=array();
 }
 
  
  if($sw==5){
 $query="select * from requests where date_closed!='' order by date_posted desc";
 $result=mysql_query($query, $db_id); $requests=array();
 }

   if($sw==6){
 $query="select * from requests where (date_approved='' and (select count(*) from users_requests where request_id=requests.id and  rank!='5')<".$init_users_needed_to_create_team.") and  date_closed='' order by date_posted desc"; //will be used to show thore with note the required members for approval yet
 $result=mysql_query($query, $db_id); $requests=array();
 }
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {
 $requests[$i]=$row;
 $requests[$i][2]=stripslashes($row[2]); 
 $requests[$i][3]=stripslashes($row[3]);
 $requests[$i][4]=stripslashes($row[4]);
 $requests[$i][5]=stripslashes($row[5]);
 $requests[$i][6]=stripslashes($row[6]);
 $requests[$i][7]=stripslashes($row[7]);
 $requests[$i][8]=stripslashes($row[8]);

 }
 return $requests;
}
 

function offers_b($id)
{
 global $db_id;

 $query="select * from offers where business_id=".$id." and date_approved!='' order by date_posted desc";
 $result=mysql_query($query, $db_id); $offers_b=array();
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers_b[$i]=$row;
 $offers_b[$i][2]=stripslashes($row[2]); 
 $offers_b[$i][3]=stripslashes($row[3]);
 $offers_b[$i][4]=stripslashes($row[4]);
 $offers_b[$i][5]=stripslashes($row[5]);
 $offers_b[$i][6]=stripslashes($row[6]);
 $offers_b[$i][7]=stripslashes($row[7]);
 $offers_b[$i][8]=stripslashes($row[8]);
 $offers_b[$i][9]=stripslashes($row[9]);
 $offers_b[$i][10]=stripslashes($row[10]);
 $offers_b[$i][11]=stripslashes($row[11]);
 $offers_b[$i][12]=stripslashes($row[12]);
 $offers_b[$i][13]=stripslashes($row[13]);
 $offers_b[$i][14]=stripslashes($row[14]);
 $offers_b[$i][15]=stripslashes($row[15]);
 $offers_b[$i][16]=stripslashes($row[16]);
 $offers_b[$i][17]=stripslashes($row[17]);
 }
 return $offers_b;
}


function requests_u($id)
{
 global $db_id;

 $query="select * from users_requests where user_id=".$id." order by date_joined desc";
 $result=mysql_query($query, $db_id); $requests_u=array();
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$requests_u[$i]=$row;
 $requests_u[$i][2]=stripslashes($row[2]); 
 $requests_u[$i][3]=stripslashes($row[3]);
 $requests_u[$i][4]=stripslashes($row[4]);
 $requests_u[$i][5]=stripslashes($row[5]);
  }
 return $requests_u;
}

function user_request($id)
{
 global $db_id;

 $query="select * from users_requests where request_id=".$id;
 $result=mysql_query($query, $db_id); $user_request=array();
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$user_request[$i]=$row;
 $user_request[$i][2]=stripslashes($row[2]); 
 $user_request[$i][3]=stripslashes($row[3]);
 $user_request[$i][4]=stripslashes($row[4]);
 $user_request[$i][5]=stripslashes($row[5]);
  }
 return $user_request;
}


function offers_u($id)
{
 global $db_id;

 $query="select * from offers where user_id=".$id." order by date_posted desc";
 $result=mysql_query($query, $db_id); $offers_u=array();
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers_u[$i]=$row;
 $offers_u[$i][2]=stripslashes($row[2]); 
 $offers_u[$i][3]=stripslashes($row[3]);
 $offers_u[$i][4]=stripslashes($row[4]);
 $offers_u[$i][5]=stripslashes($row[5]);
 $offers_u[$i][6]=stripslashes($row[6]);
 $offers_u[$i][7]=stripslashes($row[7]);
 $offers_u[$i][8]=stripslashes($row[8]);
 $offers_u[$i][9]=stripslashes($row[9]);
 $offers_u[$i][10]=stripslashes($row[10]);
 $offers_u[$i][11]=stripslashes($row[11]);
 $offers_u[$i][12]=stripslashes($row[12]);
 $offers_u[$i][13]=stripslashes($row[13]);
 $offers_u[$i][14]=stripslashes($row[14]);
 $offers_u[$i][15]=stripslashes($row[15]);
 $offers_u[$i][16]=stripslashes($row[16]);
 $offers_u[$i][17]=stripslashes($row[17]);
 }
 return $offers_u;
}


function offers_shopped($id)
{
 global $db_id;

 $query="select * from users_offers where offer_id=".$id;
 $result=mysql_query($query, $db_id); $offers_shopped=array();
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers_shopped[$i]=$row;
 $offers_shopped[$i][2]=stripslashes($row[2]); 
 $offers_shopped[$i][3]=stripslashes($row[3]);
 $offers_shopped[$i][4]=stripslashes($row[4]);
 $offers_shopped[$i][5]=stripslashes($row[5]);
 }
 return $offers_shopped;
}

function offers_user($oid,$uid,$sw)
{
 global $db_id;

if($sw==1){
 $query="select * from users_offers where offer_id=".$oid." and user_id=".$uid." and friend_name=''";
 }
 
 if($sw==2){
 $query="select * from users_offers where offer_id=".$oid." and user_id=".$uid." and friend_name!=''"; }
 
 $result=mysql_query($query, $db_id); $offers_shopped=array();
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers_shopped[$i]=$row;
 $offers_shopped[$i][2]=stripslashes($row[2]); 
 $offers_shopped[$i][3]=stripslashes($row[3]);
 $offers_shopped[$i][4]=stripslashes($row[4]);
 $offers_shopped[$i][5]=stripslashes($row[5]);
 }
 return $offers_shopped;
}



function offers_category($id)
{
 global $db_id;

 $query="select * from offers_category where id=".$id;
 $result=mysql_query($query, $db_id);
 if ($result)
 {
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$offers_category=$row;
 $offers_category[1]=stripslashes($row[1]); 
 }
 }
 return $offers_category;
}


function offers_categories()
{
 global $db_id;

 $offers_categories=array();
 $query="select * from offers_category";
 $result=mysql_query($query, $db_id);
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) $offers_categories[$i]=$row;
 return $offers_categories;
}


function coupon($id)
{
 global $db_id;

 $query="select * from users_offers where id=".$id;
 $result=mysql_query($query, $db_id);
 if ($result)
 {
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$coupon=$row;
 $coupon[2]=stripslashes($row[2]); 
 $coupon[3]=stripslashes($row[3]);
 $coupon[4]=stripslashes($row[4]);
 $coupon[5]=stripslashes($row[5]);
 }
 }
 return $coupon;
}

function coupons($sw,$oid,$uid)
{
 global $db_id;

if($sw==6){
 $query="select * from users_offers where offer_id=".$oid." order by date_user_subscibed desc";
 }
 
 if($sw==7){
 $query="select * from users_offers where user_id=".$uid." order by date_user_subscibed desc";
 }
 $result=mysql_query($query, $db_id); $coupons=array();
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) {$coupons[$i]=$row;
 $coupons[$i][2]=stripslashes($row[2]); 
 $coupons[$i][3]=stripslashes($row[3]);
 $coupons[$i][4]=stripslashes($row[4]);
 $coupons[$i][5]=stripslashes($row[5]);
 }
 return $coupons;
}

function cities()
{
 global $db_id;

 $cities=array();
 $query="select * from cities";
 $result=mysql_query($query, $db_id);
 
 for ($i=0; $row=mysql_fetch_row($result); $i++) $cities[$i]=$row;
 return $cities;
}


function msg($msg)
{
 header("Location: msg.php?msg=".$msg);
}

function label($msg)
{
 echo "<font face=\"Fixedsys\" color=\"red\">".$msg."</font>";}
 
function clean($str)
{
 if (is_numeric($str)) $str=floor($str);
 $cleaned=strip_tags($str);
 $cleaned=htmlspecialchars($cleaned);
 $cleaned=mysql_real_escape_string($cleaned);
 $to_clean=array("%20", "\"", "'", "\\", "=", ";", ":");
 $cleaned=str_replace($to_clean, "", $cleaned);
 return $cleaned;
}

function clean_a_desc($str)
{
 if (is_numeric($str)) $str=floor($str);
$str = str_replace(chr(13), "", $str);
$str = str_replace("\n", "[br/]", $str);
$str = str_replace(";", ",", $str);
 $cleaned=strip_tags($str);
 $cleaned=htmlspecialchars($cleaned);
 $cleaned=mysql_real_escape_string($cleaned);
 $to_clean=array("%20", "\"", "'", "\\", "=", ";");
 $cleaned=str_replace($to_clean, "", $cleaned);
  $cleaned=str_replace("[", "<", $cleaned);
  $cleaned=str_replace("]", ">", $cleaned);
  $cleaned=strip_tags($cleaned, '<br/><b><i><u><a>');
 return $cleaned;
}


function gen_stats($dur)
{
 global $db_id;

 $query="SELECT count(*) FROM users";
 $result=mysql_query($query, $db_id);
 $row[0]=mysql_fetch_row($result);
print (mysql_error());
 $query="SELECT count(*) FROM users where hour(timediff(now(), lastVisit))<".$dur;
 $result=mysql_query($query, $db_id);
 $row[1]=mysql_fetch_row($result);
 
 return $row;
}

function clean_u($dur)
{
 global $db_id,$server_name,$charset_m,$server_domain;

 $query="SELECT id,email FROM users where id!=12 AND id!=1 AND hour(timediff(now(), lastVisit))/24>".$dur;
 $result=mysql_query($query, $db_id); $nr=0;
 for (; $row=mysql_fetch_row($result); $nr++){
 	$headers = 'From: noreply@'.$server_domain . "\r\n" .
    'Reply-To: noreply@'.$server_domain .'; charset='.$charset_m . "\r\n" .
	'Content-Type: text/html "\r\n" 
    X-Mailer: PHP/' . phpversion();

	mail($row[1], "Removed from ".$server_name, "Your account has been deleted becouse you were idle (inactive) or becouse you show inappropriate behavior. Do not reply to this address. Private message the forums admin instead. Thank you.", $headers );
  del_u($row[0]);
 }
 return $nr;
}




function is_user($name, $email, $ip)
{
 global $db_id;

 $query="select count(*) from users where name='".$name."' or email='".$email."' or ip='".$ip."'";
 $result=mysql_query($query, $db_id);
 
 $row=mysql_fetch_row($result);
 return $row[0];
}

function is_user_temp($name, $email, $ip)
{
 global $db_id;

 $query="select count(*) from users_temp where name='".$name."' or email='".$email."' or ip='".$ip."'";
 $result=mysql_query($query, $db_id);
 
 $row=mysql_fetch_row($result);
 return $row[0];
}

function login($name, $pass)
{
 global $db_id;

 $query="select * from users where name='".$name."' and pass='".$pass."' and level>0";
 $result=mysql_query($query, $db_id);
 
 $row=mysql_fetch_row($result);
 return $row;
}

function sitted($account, $sitter)
{
 global $db_id;

 $query="select * from users where name='".$account."' and sitter='".$sitter."' and level>0";
 $result=mysql_query($query, $db_id);
 
 $row=mysql_fetch_row($result);
 return $row;
}

function user($id)
{
 global $db_id;
 
 $query="select * from users where id=".preg_replace("/[^0-9]/","", $id);
 $result=mysql_query($query, $db_id);
 if ($result)
 {
  $row=mysql_fetch_row($result);
  $row[1]=stripslashes($row[1]); $row[9]=stripslashes($row[9]);
  return $row;
 } else return 0;
}

function users()
{
 global $db_id;
 
 $query="select * from users";
 $result=mysql_query($query, $db_id); $users=array();
 for ($i=0; $row=mysql_fetch_row($result); $i++)
 {
  $users[$i]=$row;
  $users[$i][1]=stripslashes($users[$i][1]); $users[$i][9]=stripslashes($users[$i][9]);
 }
 return $users;
}

function user_($name)
{
 global $db_id;

 $query="select * from users where name='".$name."'";
 $result=mysql_query($query, $db_id);
 
 if ($result)
 {
  $row=mysql_fetch_row($result);
  $row[1]=stripslashes($row[1]); $row[9]=stripslashes($row[9]);
  return $row;
 } else return 0;
}

function config()
{
 global $db_id;

 $config=array();
 $query="select * from config order by ord asc";
 $result=mysql_query($query, $db_id);
 for ($i=0; $row=mysql_fetch_row($result); $i++) $config[$i]=$row;
 return $config;
}

function config_($var, $val)
{
 global $db_id;

 $query="update config set value='".$val."' where name='".$var."'";
 $result=mysql_query($query, $db_id);
 if ($result) return 1;
 else return 0;
}

function g_points($id, $q, $reason)
{
 global $db_id;

   $query="insert into points_to_add (user_id, points, time_earned, reason) values (".$id.", ".$q.", '".time()."', '".$reason."')";
 $result=mysql_query($query, $db_id);
 if ($result) return 1;
 else return 0;
}



function business($id)
{
 global $db_id;

 $query="select * from business where id=".$id;
 $result=mysql_query($query, $db_id);
 
 $row=mysql_fetch_row($result);
 return $row;
}



function request($id)
{
 global $db_id;
 
 $query="select * from requests where id=".$id;
 $result=mysql_query($query, $db_id);
 if ($result)
 {
  $row=mysql_fetch_row($result);
  $row[2]=stripslashes($row[2]); $row[3]=stripslashes($row[3]); $row[14]=stripslashes($row[14]);
  return $row;
 } else return 0;
}


function offer($id)
{
 global $db_id;
 
 $query="select * from offers where id=".$id;
 $result=mysql_query($query, $db_id);
 if ($result)
 {
  $row=mysql_fetch_row($result);
  $row[2]=stripslashes($row[2]); $row[14]=stripslashes($row[14]);
  return $row;
 } else return 0;
}





function update_lastVisit($id)
{
 global $db_id;
 $query="select count(*) from messages where recipient=".$id." and timediff((select lastVisit from users where id=".$id."), sent)<'00:00:01'";
 $result=mysql_query($query, $db_id);
 $row[1]=mysql_fetch_row($result); $row[1]=$row[1][0];
 $query="update users set lastVisit=now(), ip='".$_SERVER["REMOTE_ADDR"]."' where id=".$id;
 mysql_query($query, $db_id);
 
 return $row;
}



function check_d($id)
{
 global $db_id;

 $query="select timediff(dueTime, now()) from d_queue where user=".$id;
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 if ($row[0][0])
  if ($row[0][0]=="-")
  {
  
   $query="select user_id from city_connection_payments where user_id=".$id;  
   $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result);
   if ($row[0])//if user has monetary activity will not be deleted will be deactivated
   { $deactivate=1; }
   
      $query="select user_id from credit_orders where user_id=".$id;  
   $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result);
   if ($row[0])//if user has monetary activity will not be deleted will be deactivated
   { $deactivate=1; }
   
   $query="select user_id from offers where date_approved!='' and user_id=".$id;  
   $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result);
   if ($row[0])//if user has monetary activity will not be deleted will be deactivated
   { $deactivate=1; }
   
      $query="select user_id from users_offers where user_id=".$id;  
   $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result);
   if ($row[0])//if user has monetary activity will not be deleted will be deactivated
   { $deactivate=1; }
   
   if ($deactivate==1){
   $query="update users set account_deleted=1, account_deleted_date='".time()."' where id=".$id;
   mysql_query($query, $db_id);
   return 0;
   }else {
   
    $query="delete from business where representative=".$id;
    mysql_query($query, $db_id);
	
	$query="delete from offers where date_approved='' and user_id=".$id; 
    mysql_query($query, $db_id);
	
	$query="delete from users where id=".$id; 
    mysql_query($query, $db_id);
   return 0;
   }

  }
  return 1;
}

function del_u($id)
{
 global $db_id;


   $query="delete from business where representative=".$id;
   mysql_query($query, $db_id);
   $query="delete from messages where recipient=".$id;
   mysql_query($query, $db_id);
   $query="delete from offers where user_id=".$id;
   mysql_query($query, $db_id);
   $query="delete from requests  where user_id=".$id;
   mysql_query($query, $db_id);
   $query="delete from users where id=".$id;
   mysql_query($query, $db_id);
      $query="delete from users_offers where user_id=".$id;
   mysql_query($query, $db_id);
         $query="delete from users_requests where user_id=".$id;
   mysql_query($query, $db_id);
            $query="delete from user_forgot_pass where user_id=".$id;
   mysql_query($query, $db_id);

}




function check_d_all()
{
 global $db_id;

 $query="select timediff(dueTime, now()), user from d_queue";
 $result=mysql_query($query, $db_id); $d_list=array(); $d=0;
 for ($i=0; $row=mysql_fetch_row($result); $i++)
 {
  $d_list[$i]=$row;
  if ($row[0][0]=="-") if (!check_d($row[1])) $d++;
 }
 msg($d." users deleted. ".(count($d_list)-$d)." accounts not due.");
}

function get_d($id)
{
 global $db_id;

 $query="select timediff(dueTime, now()) from d_queue where user=".$id;
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 return $row[0];
}




function cancel_d($id)
{
 global $db_id;

 $query="delete from d_queue where user=".$id;
 $result=mysql_query($query, $db_id);
 if ($result) msg("Delete request withdrawn.");
 else msg("Failed.".mysql_error());
}



function send_report($to, $subject, $contents)
{
 global $db_id;
 
 $query="insert into reports(recipient, subject, contents, sent) values('".$to."', '".mysql_real_escape_string($subject)."', '".mysql_real_escape_string($contents)."', now())";
 $result=mysql_query($query, $db_id);
 if ($result) return 1;
 else return 0;
}

function send_to_all($from, $subject, $contents)
{
 global $db_id;
 
 $users=users();
 for ($i=0; $i<count($users); $i++)
 {
 $query="insert into messages(sender, recipient, subject, contents, sent) values(".$from.", ".$users[$i][0].", '".$subject."', '".$contents."', now())";
  mysql_query($query, $db_id);
 }
}

function send_message($from, $to, $subject, $contents)
{
 global $db_id,$server_domain,$lang,$server_name;
 
 $user_from=user($from);
 $toall=explode("_",$to);
 
if($toall[0]=='allteam'){
$request=request($toall[1]);

if($request[1] && $request[1]==$from){
if($request[13]!='' || $request[12]==''){
msg($lang['team_not_approved']." ".$lang['or']." ".$lang['team_closed']);
die();
}
$user_request=user_request($request[0]);
for ($i=0; $i<count($user_request); $i++) {

 $query="insert into messages(sender, recipient, subject, contents, sent) values(".$from.", ".$user_request[$i][1].", '".$subject."', '".$contents."', now())";
 $result=mysql_query($query, $db_id);
 
 $subject_m=$user_from[1].$lang['send_you_a_message']." ".$server_name;
 $contents_m="<br>".$lang['login_to_view_messages']."<br><br><a href=\'htpp://".$server_name."\'>htpp://".$server_name."</a><br><br>---------<br>".$lang['you_are_receiving_this_mail_couse1']." ".$server_name." ".$lang['you_are_receiving_this_mail_couse2']."<br>";
 
 $user_mail=user($user_request[$i][1]); 
  $query_m="insert into mail_platform_notifications_que(from_mail, to_mail, subject, message, date_submited) values('noreply@".$server_domain."', '".$user_mail[4]."', '".$subject_m."', '".$contents_m."', '".time()."')";
 $result_m=mysql_query($query_m, $db_id);
 
 }
   }
 if ($result) return 1; else return 0;
}else{

  $query="insert into messages(sender, recipient, subject, contents, sent) values(".$from.", ".$to.", '".$subject."', '".$contents."', now())";
 $result=mysql_query($query, $db_id);
 
  $user_mail=user($to); 
   $subject_m=$user_from[1].$lang['send_you_a_message']." ".$server_name;
 $contents_m="<br>".$lang['login_to_view_messages']."<br><br><a href=\'htpp://".$server_name."\'>htpp://".$server_name."</a><br><br>---------<br>".$lang['you_are_receiving_this_mail_couse1']." ".$server_name." ".$lang['you_are_receiving_this_mail_couse2']."<br>";
 
   $query_m="insert into mail_platform_notifications_que (from_mail, to_mail, subject, message, date_submited) values('noreply@".$server_domain."', '".$user_mail[4]."', '".$subject_m."', '".$contents_m."', '".time()."')";
 $result_m=mysql_query($query_m, $db_id);

 if ($result) return 1; else return 0;
 }
}


function msg_rep_alert($id)
{
 global $db_id;

 $output=array();
 
 $query="select count(*) from messages where (recipient=".$id." and read_flag!='1' AND receiver_del!=1)"; 
 $result=mysql_query($query, $db_id); $output[1]=mysql_fetch_row($result);
 return $output;
}

function messages($id)
{
 global $db_id;

 $query="select * from messages where (recipient=".$id." AND receiver_del!=1) order by sent desc";
 $result=mysql_query($query, $db_id); $reports=array();
 for ($i=0; $row=mysql_fetch_row($result); $i++)
 {
		$reports[$i]=$row;
		if (strtotime($row[5])>strtotime($_SESSION["user"][6])) $reports[$i][9]=1; else $reports[$i][9]=0;//if message is new
 }
 return $reports;
}

function messages_send($id)
{
 global $db_id;

 $query="select * from messages where (sender=".$id." AND sender_del!=1) order by sent desc";
 $result=mysql_query($query, $db_id); $reports=array();
 for ($i=0; $row=mysql_fetch_row($result); $i++)
 {
		$reports[$i]=$row;
		if (strtotime($row[5])>strtotime($_SESSION["user"][6])) $reports[$i][9]=1; else $reports[$i][9]=0;//if message is new
 }
 return $reports;
}







function message($id)
{
 global $db_id;

 $query="select * from messages where id=".$id;
 $result=mysql_query($query, $db_id); $reports=array();
 $row=mysql_fetch_row($result);
 return $row;
}


function delmsg($id, $owner, $op)
{
 global $db_id;
$message=message($id);

 if ($owner==$message[1] && $message[8]==1){
  $query="delete from messages where id=".$id;
 $result=mysql_query($query, $db_id); $reports=array();
 if ($result) header('Location: messages.php?op=1&page=0');
 else msg("Failed.".mysql_error());
 }
 
  if ($owner==$message[2] && $message[7]==1){
  $query="delete from messages where id=".$id;
 $result=mysql_query($query, $db_id); $reports=array(); 
 if ($result) header('Location: messages.php?page=0');
 else msg("Failed.".mysql_error());
 }
 
   if ($owner==$message[2] && $message[7]!=1 && $message[2]!=$message[1] ){
$query="update messages set receiver_del='1' where id=".$id;
 $result=mysql_query($query, $db_id); $reports=array();
 if ($result) header('Location: messages.php?page=0');
 else msg("Failed.".mysql_error());
 }
 
 
    if ($owner==$message[1] && $message[8]!=1 && $message[2]!=$message[1]){
$query="update messages set sender_del='1' where id=".$id;
 $result=mysql_query($query, $db_id); $reports=array(); 
 if ($result) header('Location: messages.php?op=1&page=0');
 else msg("Failed.".mysql_error());
 }
 
  
   if ($op!=1){ 
     if ($owner==$message[1] && $message[7]!=1 && $message[2]==$message[1]){
$query="update messages set receiver_del='1' where id=".$id;
 $result=mysql_query($query, $db_id); $reports=array(); 
 if ($result) header('Location: messages.php?page=0');
 else msg("Failed.".mysql_error());
 }
}
 if ($op==1){
      if ($owner==$message[2] && $message[8]!=1 && $message[2]==$message[1]){
$query="update messages set sender_del='1' where id=".$id;
 $result=mysql_query($query, $db_id); $reports=array(); 
 if ($result) header('Location: messages.php?op=1&page=0');
 else msg("Failed.".mysql_error());
 }
 }
 
  $message=message($id);
       if ($owner==$message[2] && $message[7]==1 && $message[8]==1 && $message[2]==$message[1]){
  $query="delete from messages where id=".$id;
 $result=mysql_query($query, $db_id); $reports=array(); 
 if ($result) header('Location: messages.php?page=0');
 else msg("Failed.".mysql_error());
 }
 
}

function delallmsg($id, $op)
{
 global $db_id;
 $owner=$id;

if ($op==1){
$messages=messages_send($id);
} else $messages=messages($id);

for ($k=0; $k<count($messages); $k++)
	{

$message=message($messages[$k][0]);
delmsg($message[0], $owner, $op);
}
}

function delacc($id)
{
 global $db_id;

 $query="select count(*) from d_queue where user=".$id;
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 if (!$row[0])
 {
  $date=strtotime("+1 day"); $date=strftime("%Y-%m-%d %H:%M:%S", $date);
  $query="insert into d_queue(user, dueTime) values('".$id."', '".$date."')";
  $result=mysql_query($query, $db_id);
  if ($result) msg("Account will be deleted in 24 hours.");
  else msg("Failed.".mysql_error());
 } else msg("Account is already scheduled for deletion.");
}




function pass($id, $pass)
{
 global $db_id;

 $query="update users set pass='".$pass."' where id=".$id;
 $result=mysql_query($query, $db_id);
}

function code($id, $code)
{
 global $db_id;

$query="insert into user_forgot_pass (user_id, code, date_asked) values('".$id."', '".$code."', '".time()."')";
 $result=mysql_query($query, $db_id);
}


function ban($name, $value)
{
 global $db_id;

 $query="update users set level='".$value."' where name='".$name."'";
 $result=mysql_query($query, $db_id);
 if ($result)
  if ($value) msg("Succes. User '".$name."' level changed.");
  else msg("Succes. User '".$name."' level changed.");
 else msg("Failure.".mysql_error());
}



function ch_lang($id, $language)
{
 global $db_id;

 $query="update users set lang='".$language."' where id=".$id;
 $result=mysql_query($query, $db_id);
 if ($result) return 1;
 else return 0;
}



function profile($id, $desc, $real_name , $receive_mails, $lang, $image, $show_full_name,$city,$city1,$city2)
{
 global $db_id;

 $query="update users set description='".$desc."', real_name='".$real_name."', receive_mails='".$receive_mails."', lang='".$lang."', image='".$image."', show_full_name='".$show_full_name."', cities='".$city."-".$city1."-".$city2."' where id=".$id;
 $result=mysql_query($query, $db_id);
 if ($result) msg("Succes. Profile changed.");
 else msg("Failure.".mysql_error());
}



function reg_temp($name, $pass, $email, $city,$city1,$city2 ,$aff,$lang_file)
{
 global $db_id,$lang,$server_name,$template,$server_domain,$charset_m,$countries_flag_s;

 $query="insert into users_temp(name, pass, email, joined, cities, referrer_id,lang, ip) values('".$name."', '".$pass."', '".$email."',date_sub(now(), interval 1 second), '".$city."-".$city1."-".$city2."','".$aff."','".$lang_file."','".$_SERVER["REMOTE_ADDR"]."')"; //date_sub(now(), interval 1 second) added so show as new the admin welcoem msg
 $result=mysql_query($query, $db_id);
 if ($result){
 $to=mysql_insert_id();


$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: noreply@'.$server_domain.' <noreply@'.$server_domain.'>' . "\r\n";
//$headers .= 'Cc: hide@address.com' . "\r\n";
//$headers .= 'Bcc: hide@address.com' . "\r\n";
 
 // mail($email, '=?UTF-8?B?'.base64_encode($subject_m).'?=', $message_m, $headers);
  
  
mail($email, $name." ".$lang['welcome_to']." ".$server_name, $lang['one_last_step']."<br><br>
	<a href='http://".$server_name."/confirm_account.php?id=".$to."' target='_blank'>http://".$server_name."/confirm_account.php?id=".$to."</a><br><br>".$lang['thank_you_the_team_of']." ".$server_name, $headers );
 
include "header.php"; echo $lang['reg_completed']."<br><br><a class='q_link' href='login.php'>".$lang['login']."</a><br><br><font class='q_text_sm'>(".$lang['check_spam'].")</font><br><br>"; include "footer.php";
 }else{ include "header.php"; echo "Failed.".mysql_error()."<br><br><a class='q_link' href='register.php'>".$lang['back']."</a>"; include "footer.php";}
}

function reg($name, $pass, $email, $cities,$aff,$lang_file,$id)
{
 global $db_id,$lang,$admin_u_name,$reff_credits;

 $query="insert into users(name, pass, email, level, joined, lastVisit, points, ip, grPath, cities,lang, receive_mails, referrer_id) values('".$name."', '".$pass."', '".$email."', 1, date_sub(now(), interval 2 second), date_sub(now(), interval 2 second), 0, '".$_SERVER["REMOTE_ADDR"]."', 'default', '".$cities."', '".$lang_file."', 1,'".$aff."')"; //date_sub(now(), interval 2 second) added so show as new the admin welcoem msg
  $result=mysql_query($query, $db_id);
  $to=mysql_insert_id();
  
  $query="delete from users_temp where id=".$id;
 $result1=mysql_query($query, $db_id);

 if ($result && $result1){
  $sender=user_($admin_u_name);
 send_message($sender[0], $to, $lang['intro_msg_subject'].' '.$name.'!', $lang['intro_msg_contents']);

//update aff credits
   $get_cre=0;
   

 
   
      $query="select user_id from credit_orders where user_id='".$aff."'";  
   $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result);
   if ($row[0])//if user has monetary activity will get the credits
   { $get_cre=1; }
   
   $query="select user_id from offers where date_approved!='' and user_id='".$aff."'";  
   $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result);
   if ($row[0])//if user has monetary activity will get the credits
   { $get_cre=1; }
   
      $query="select user_id from users_offers where user_id='".$aff."'";  
   $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result);
   if ($row[0])//if user has monetary activity will get the credits
   { $get_cre=1; }
   
   if($get_cre==1){
	$usr_aff=user($aff);
	if($usr_aff[18]=='')$usr_aff[18]=0;
	
    $new_credits=$usr_aff[18]+$reff_credits;

$query="update users set credits='".$new_credits."' where id='".$aff."'";
$result=mysql_query($query, $db_id);
}


echo $lang['act_completed']."<br><br><a class='q_link' href='login.php'>".$lang['login']."</a>"; 
 }else{ echo "Failed.".mysql_error()."<br><br><a class='q_link' href='register.php'>".$lang['back']."</a>"; }
 
}


function request_edit($user_id,$rid,$title, $description,$image,$for_whole_country,$city,$max_price,$currency,$category,$notes,$closed)
{
 global $db_id,$template,$lang;
 
$user_created_r=user($user_id);

 $query="select count(*) from requests where (title='".$title."' or description='".$description_full."') and (for_whole_country='".$for_whole_country."' or city='".$city."')";
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 if ($row[0])
 {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo "There is buy team for this product/service<br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php";}
 else
 {
 
 if($closed==1) $date_closed=time(); else $date_closed='';
 
 $query="update requests set title='".$title."', description='".$description."',image='".$image."',for_whole_country='".$for_whole_country."',city='".$city."',max_price='".$max_price."',currency-'".$currency."',category='".$category."',notes='".$notes."',date_closed='".$date_closed."' where id=".$rid;
$result=mysql_query($query, $db_id);
  
  
    if ($result) {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo $lang['success']."<br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php";}
  else msg("Failed.".mysql_error());
 }
}



  function request_add($user_id,$title, $description,$image,$for_whole_country,$city,$max_price,$currency,$category,$users_invited,$notes)
{
 global $db_id,$template,$lang,$credits_needed_to_post_request,$credits_needed_participate_request,$countries_flag_s,$admin_u_name;

$user_created_r=user($user_id);

 $query="select count(*) from requests where (title='".$title."' or description='".$description_full."') and (for_whole_country='".$for_whole_country."' or city='".$city."')";
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 if ($row[0])
 {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo "There is buy team for this product/service<br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php"; die();}
 else
 {
  $query="insert into requests(user_id, title, description, image, for_whole_country, city, max_price, currency, category, date_posted,notes) values('".$user_id."', '".$title."','".$description."','".$image."','".$for_whole_country."','".$city."','".$max_price."','".$currency."','".$category."','".time()."','".$notes."')";
  $result=mysql_query($query, $db_id);
 
$users_invited=explode("-", $users_invited);
$query="select LAST_INSERT_ID()";
$result=mysql_query($query, $db_id);
$last_r_id=mysql_fetch_row($result);

   $query="insert into users_requests(user_id, request_id, date_joined,rank) values(".$user_id.", ".$last_r_id[0].",'".time()."','5')"; //5 is the founder of team
  $result=mysql_query($query, $db_id);

for ($i=0; $i<count($users_invited); $i++){

    $query_in="insert into buy_teams_invited(from_user, to_user, for_buy_team, date_send) values('".$user_id."', '".$users_invited[$i]."','".$last_r_id[0]."','".time()."')";
  $result=mysql_query($query_in, $db_id);
  
send_message($user_id, $users_invited[$i], '<img src="core_templates/'.$template.'/b_teams_invite.gif" border="0" alt="'.$lang['invitation_to_buy_team'].'" title="'.$lang['invitation_to_buy_team'].'" />'.$title.'!', $lang["he_she"].' <a class="q_link" hre="profile_view.php?id='.$user_id.'">'.$user_created_r[1].'</a>'.$lang["just_created_a_buy_team"].' '.$credits_needed_participate_request.'  '.$lang["Credits"].'.<br><br><a class="q_link" href="accept_b_team.php?id='.$last_r_id[0].'&user='.$users_invited[$i].'">'.$lang["accept"].'</a><br><br><a class="q_link" href="b_team_view.php?id='.$last_r_id[0].'">'.$title.'</a>');
  
  }
  
  $new_credits=$user_created_r[18]-$credits_needed_to_post_request;

$query="update users set credits='".$new_credits."' where id=".$_SESSION["user"][0];
$result=mysql_query($query, $db_id);
  
      $admin_id=user_($admin_u_name);
 send_message($user_id, $admin_id[0], 'New Team Added', 'New Team Added');
 
    if ($result) {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo $lang['request_added']."<br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php";}
  else msg("Failed.".mysql_error());
 }
}



function bank_dep_add($user_id, $bank, $payment_branch, $amount,$transcript_number)
{
 global $db_id,$template,$lang,$countries_flag_s,$admin_u_name;

  $query="insert into bank_payments(user_id, bank, payment_branch, amount, transcript_number,date_send) values('".$user_id."', '".$bank."','".$payment_branch."','".$amount."','".$transcript_number."','".time()."')";
  $result=mysql_query($query, $db_id);
  
    $admin_id=user_($admin_u_name);
 send_message($user_id, $admin_id[0], 'New Bank Deposit Submited', 'New Bank Deposit Submited');
 
    if ($result) {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo $lang['success']."<br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php";}
  else msg("Failed.".mysql_error());

}

function offer_edit($offer_id,$user_id, $business_id,$title, $offer_terms, $description_full,$image,$catalog_price,$offer_price,$for_whole_country,$target_city,$quantity_limit_min,$quantity_limit_max,$currency,$how_many_per_user,$how_many_for_friends,$offer_category)
{
 global $db_id,$template,$lang,$admin_u_name;

 $query="select count(*) from offers where (title='".$title."' or description_full='".$description_full."') and (for_whole_country='".$for_whole_country."' or target_city='".$target_city."') and id!=".$offer_id;
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 if ($row[0])
 {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo "Offer with this Title exists for the specified Locations <br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php";}
 else
 {
 
  $query="update offers set title='".$title."', offer_terms='".$offer_terms."', description_full='".$description_full."',image='".$image."',catalog_price='".$catalog_price."',offer_price='".$offer_price."',for_whole_country='".$for_whole_country."',target_city='".$target_city."',quantity_limit_max='".$quantity_limit_max."',quantity_limit_min='".$quantity_limit_min."',currency='".$currency."',how_many_per_user='".$how_many_per_user."',how_many_for_friends='".$how_many_for_friends."' ,offer_category='".$offer_category."' where id=".$offer_id." and user_id=".$user_id." and business_id=".$business_id;
  $result=mysql_query($query, $db_id);
  
    $admin_id=user_($admin_u_name);
 send_message($user_id, $admin_id[0], 'Offer Edited', 'Offer Edited');
 
    if ($result) {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo $lang['offer_updated']."<br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php";}
  else msg("Failed.".mysql_error());
 }
}






function offer_add($user_id, $business_id,$title, $offer_terms, $description_full,$image,$catalog_price,$offer_price,$for_whole_country,$target_city,$quantity_limit_min,$quantity_limit_max,$currency,$how_many_per_user,$how_many_for_friends,$offer_category)
{
 global $db_id,$template,$lang,$admin_u_name;

$user_posting_offer=user($user_id);

 $query="select count(*) from offers where (title='".$title."' or description_full='".$description_full."') and (for_whole_country='".$for_whole_country."' or target_city='".$target_city."')";
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 if ($row[0])
 {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo "Offer with this Title exists for the specified Locations <br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php";}
 else
 {
  $query="insert into offers(user_id, business_id, title, offer_terms, description_full, image, date_posted, catalog_price, offer_price, for_whole_country, target_city, quantity_limit_max, quantity_limit_min,currency,how_many_per_user,how_many_for_friends,offer_category) values('".$user_id."', '".$business_id."','".$title."','".$offer_terms."','".$description_full."','".$image."',".time().",'".$catalog_price."','".$offer_price."','".$for_whole_country."','".$target_city."','".$quantity_limit_max."','".$quantity_limit_min."','".$currency."','".$how_many_per_user."','".$how_many_for_friends."','".$offer_category."')";
  $result=mysql_query($query, $db_id);
  
    $new_credits=$user_posting_offer[18]-$credits_needed_to_post_offer;

$query="update users set credits='".$new_credits."' where id=".$_SESSION["user"][0];
$result=mysql_query($query, $db_id);
  
  
  $admin_id=user_($admin_u_name);
 send_message($user_id, $admin_id[0], 'New Offer Posted', 'New Offer Posted');
  
    if ($result) {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo $lang['offer_added']."<br><br><a class='q_link' href='profile_view.php?id=".$user_id."'>".$lang['profile']."</a>"; include "footer.php";}
  else msg("Failed.".mysql_error());
 }
}



function business_add($representative, $business_full_name,$business_title, $logo, $map_image,$city,$description,$full_address,$phone,$fax,$email,$website,$tax_id, $tax_authority)
{
 global $db_id,$template,$lang;

 $query="select count(*) from business where business_full_name='".$business_title."' or tax_id='".$tax_id."'";
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 if ($row[0]) { msg("Business with this Tax ID exists.");}
 else
 {
  $query="insert into business(business_title, business_full_name, representative,description,logo,map_image, city,address,phone,fax, email,website,tax_id,tax_authority) values('".$business_title."', '".$business_full_name."',".$representative.",'".$description."','".$logo."','".$map_image."','".$city."','".$full_address."','".$phone."','".$fax."','".$email."','".$website."','".$tax_id."','".$tax_authority."')";
  $result=mysql_query($query, $db_id);
  $query="select LAST_INSERT_ID()";
  $result=mysql_query($query, $db_id);
  $row=mysql_fetch_row($result);
  $query="update users set business_id=".$row[0].", rank='representative' where id=".$representative;
  $result=mysql_query($query, $db_id);
  if ($result) {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo $lang['business_added']."<br><br><a class='q_link' href='profile_view.php?id=".$representative."'>".$lang['back']."</a>"; include "footer.php";}
  else msg("Failed.".mysql_error());
 }
}

function business_edit($business_id, $business_full_name,$business_title, $logo, $map_image,$city,$description,$full_address,$phone,$fax,$email,$website,$tax_id, $tax_authority)
{
 global $db_id,$template,$lang;

 $query="select count(*) from business where (business_full_name='".$business_title."' or tax_id='".$tax_id."') and id!='".$business_id."'";
 $result=mysql_query($query, $db_id);
 $row=mysql_fetch_row($result);
 if ($row[0]) { msg("Business with this Tax ID exists.");}
 else
 {
  $query="update business set business_title='".$business_title."', business_full_name='".$business_full_name."', description='".$description."',logo='".$logo."',map_image='".$map_image."', city='".$city."',address='".$full_address."',phone='".$phone."',fax='".$fax."', email='".$email."',website='".$website."',tax_id='".$tax_id."',tax_authority='".$tax_authority."' where id=".$business_id." and representative=".$_SESSION["user"][0];
  $result=mysql_query($query, $db_id);
  if ($result) {include "header.php"; echo "<link rel='stylesheet' type='text/css' href='core_templates/".$template."/default.css'>"; echo $lang['changes_saved']."<br><br><a class='q_link' href='profile_view.php?id=".$_SESSION["user"][0]."'>".$lang['back']."</a>"; include "footer.php";}
  else msg("Failed.".mysql_error());
 }
}

function odeals_version()
{
$version='1.2';
return $version;
}

function version_check()
{
global $lang;
if (@fopen('http://www.tecorange.com/tecorange_software_info.htm','r')) {

$version=odeals_version();
$chunk= file_get_contents('http://www.tecorange.com/tecorange_software_info.htm');
$pattern = "/#ODEALS-Latest Version#<strong>(.*?)<\/strong>#Latest Version#/i";
preg_match($pattern, $chunk, $match);
$latest_version=$match[1];

$chunk1= file_get_contents('http://www.tecorange.com/tecorange_software_info.htm');
$pattern1 = "/#ODEALS-Info URL#<strong>(.*?)<\/strong>#Info URL#/i";
preg_match($pattern1, $chunk1, $match1);
$info_url=$match1[1];

$output=$version."--".$lang['version_of_odeals']."<b> ".$version." </b>";

if($version==$match[1]) $output=$output."<br>".$lang['you_have_the_latest_version'];
if($version<$match[1]) $output=$output."<br>".$lang['there_is_a_newer_version']."<br><a href='".$info_url."'>".$info_url."</a>";

return $output;
}else {return '0--No version info at the momment';}
}


?>
Return current item: OrangeDeals