<?php
	ob_start();
	session_start();


error_reporting(0);

	$current_file = $_SERVER['SCRIPT_NAME'];

	if (isset($_SERVER['HTTP_REFERER']) && !empty ($_SERVER['HTTP_REFERER'])) {
		$http_referer = $_SERVER['HTTP_REFERER'];
	}
	
	function loggedin() {
		if (isset($_SESSION['user_id']) &&!empty($_SESSION['user_id'])) {
			return true;
		} else 
		{
			return false;
		}
	}
	function loggedinadmin() {
		if (isset($_SESSION['admin-id']) &&!empty($_SESSION['admin-id'])) {
			return true;
		} else 
		{
			return false;
		}
	}
	
	

	
	
	function getfield($field, $table, $primaryID) {
		@$query = "SELECT `$field` FROM `$table` WHERE `$primaryID` ='".$_SESSION['user_id']."'";
			if ($query_run = mysql_query($query)) {
				if($query_result = mysql_result($query_run, 0)) {
					return $query_result;
				}
			}
	
	}
	

	
	
	function getfield1($field1, $table1, $primaryID1) {
		$query1 = "SELECT `$field1` FROM `$table1` WHERE `$primaryID1` ='".$_SESSION['admin-id']."'";
			if ($query_run1 = mysql_query($query1)) {
				if($query_result1 = mysql_result($query_run1, 0)) {
					return $query_result1;
				}
			}
	
	}


	function getfield2($field2, $table2, $id) {
		$query1 = "SELECT `$field2` FROM `$table2` WHERE `$id` ='1'";
			if ($query_run1 = mysql_query($query1)) {
				if($query_result1 = mysql_result($query_run1, 0)) {
					return $query_result1;
				}
			}
	
	}



	function payment_info($rid){
$rid = (int)$rid;
$sql = "SELECT * FROM  `makedonation` WHERE  `id` = {$rid}";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);

}
	


	
	
	
	function fetch_agents(){
$result = mysql_query("SELECT * FROM  `users` WHERE  `user_type` LIKE 'agent' ORDER BY users_id DESC");
$agents = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$agents[] = $row;
}
return $agents;
}


	function fetch_agents_info($uid){
$uid = (int)$uid;
$sql = "SELECT * FROM  `users` WHERE  `users_id` = {$uid}";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);

}

function fetch_rent(){
$result = mysql_query("SELECT * FROM  `houses` WHERE  `house_status` LIKE 'rent'");
$rent = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$rent[] = $row;
}
return $rent;
}

function fetch_rent_apartment(){
$result = mysql_query("SELECT * FROM  `houses` WHERE  `house_status` LIKE 'rent' AND `property_type` LIKE 'apartment'");
$apart = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$apart[] = $row;
}
return $apart;
}


function fetch_profile(){
$result = mysql_query("SELECT * FROM  `profile`");
$profiles = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$profiles[] = $row;
}
return $profiles;
}

function fetch_profilev(){
$result = mysql_query("SELECT * FROM  `videos`");
$profiles = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$profiles[] = $row;
}
return $profiles;
}

function fetch_profilevp(){
$result = mysql_query("SELECT * FROM  `pvideos`");
$profiles = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$profiles[] = $row;
}
return $profiles;
}

function fetch_profile1(){
$result = mysql_query("SELECT * FROM  `profile` WHERE `user_id` ='".$_SESSION['user_id']."'");
$profiles = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$profiles[] = $row;
}
return $profiles;
}


	function fetch_profile_info($pid){
$pid = (int)$pid;
$sql = "SELECT * FROM  `profile` WHERE  `id` = {$pid}";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);

}





function fetch_rent_house(){
$result = mysql_query("SELECT * FROM  `houses` WHERE  `house_status` LIKE 'rent' AND `property_type` LIKE 'house'");
$house = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$house[] = $row;
}
return $house;
}
function fetch_rent_condo(){
$result = mysql_query("SELECT * FROM  `houses` WHERE  `house_status` LIKE 'rent' AND `property_type` LIKE 'condo'");
$condo = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$condo[] = $row;
}
return $condo;
}










function fetch_sale(){
$result = mysql_query("SELECT * FROM  `houses` WHERE  `house_status` LIKE 'sale'");
$sale = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$sale[] = $row;
}
return $sale;
}

function fetch_sale_apartment(){
$result = mysql_query("SELECT * FROM  `houses` WHERE `house_status` LIKE 'sale' AND `property_type` LIKE 'apartment'");
$apart = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$apart[] = $row;
}
return $apart;
}

function fetch_sale_home(){
$result = mysql_query("SELECT * FROM  `houses` WHERE `house_status` LIKE 'sale' AND `property_type` LIKE 'home'");
$home = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$home[] = $row;
}
return $home;
}








function fetch_rent_info($rid){
$rid = (int)$rid;
$sql = "SELECT * FROM  `houses` WHERE  `house_id` = {$rid}";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);

}

function user_count(){
return mysql_result(mysql_query("SELECT COUNT(`users_id`) FROM `users`"));

}

function array_sanitize(&$item){
$item = htmlentities(strip_tags(mysql_real_escape_string($item)));

}
function sanitize($data){
return htmlentities(strip_tags(mysql_real_escape_string($data)));

}

function email_exist($email){
$email = sanitize($email);
return (mysql_result(mysql_query("SELECT COUNT(`users_id`) FROM `users` WHERE `email` = '$email'"),0) == 1) ? true : false;
}

function users_id_from_email($email){
$email = sanitize($email);
return mysql_result(mysql_query("SELECT `users_id` FROM `users` WHERE `email` = '$email'"), 'users_id');

}

function recover($mode ,$email){
$mode = sanitize($mode);
$email= sanitize($email);

$user_name = getfield('username','users','users_id');
$firstname = getfield('first_name','users','users_id');
$email = getfield(users_id_from_email($email),'users','users_id');

if ($mode == 'username'){
email($email, 'your username', "hello" . $first_name .", \n\nYour username is: " .$user_name." \n\n - portvale estate");
}else if($mode == 'password'){
//recover password
}
}




function search_houses($houses){
$returned_results = array();
$where = " ";
$houses = preg_split('/[\s]+/ ' , $houses);
$total_houses = count($houses);

foreach($houses as $hous => $house){
$where .= "`address` LIKE '%$house%'";
if ($hous != ($total_houses - 1)) {
$where .= " AND ";
}
}
$results = "SELECT * FROM  `houses` WHERE `house_status` LIKE 'rent' AND `address` LIKE '%$house%' OR `state` LIKE '%$house%'";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) :0;
if ($results_num === 0){
return false;
}else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] =array(
'house_id' =>$results_row['house_id'],
'address' =>$results_row['address'],'property_type' =>$results_row['property_type'],'bedrooms' =>$results_row['bedrooms'],'monthly_rent' =>$results_row['monthly_rent'],'description' =>$results_row['description'],'state' =>$results_row['state'],'square_feet' =>$results_row['square_feet'],'photos' =>$results_row['photos']

);

}
return $returned_results;
}

}

function search_sale($sale){
$returned_results = array();
$where = " ";
$sale = preg_split('/[\s]+/ ' , $sale);
$total_sale = count($sale);

foreach($sale as $sa => $sale){
$where .= "`address` LIKE '%$sale%'";
if ($sa != ($total_sale - 1)) {
$where .= " AND ";
}
}
$results = "SELECT * FROM  `houses` WHERE `house_status` LIKE 'sale' AND `address` LIKE '%$sale%' OR `state` LIKE '%$sale%'";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) :0;
if ($results_num === 0){
return false;
}else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] =array(
'house_id' =>$results_row['house_id'],
'address' =>$results_row['address'],'property_type' =>$results_row['property_type'],'bedrooms' =>$results_row['bedrooms'],'monthly_rent' =>$results_row['monthly_rent'],'description' =>$results_row['description'],'state' =>$results_row['state'],'square_feet' =>$results_row['square_feet'],'photos' =>$results_row['photos']

);

}
return $returned_results;
}

}
function search_agents($agent){
$returned_results = array();
$where = " ";
$agent = preg_split('/[\s]+/ ' , $agent);
$total_agents = count($agent);

foreach($agent as $age => $agent){
$where .= "`first_name` LIKE '%$agent%'";
if ($age != ($total_agents - 1)) {
$where .= " AND ";
}
}
$results = "SELECT * FROM `users` WHERE `user_type` LIKE 'agent' AND (`username` LIKE '%$agent%' OR `first_name` LIKE '%$agent%' OR `last_name` LIKE '%$agent%')";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) :0;
if ($results_num === 0){
return false;
}else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] =array(
'users_id' =>$results_row['users_id'],
'username' =>$results_row['username'],'email' =>$results_row['email'],'first_name' =>$results_row['first_name'],'last_name' =>$results_row['last_name'],'profile' =>$results_row['profile'],'phone' =>$results_row['phone'],'country' =>$results_row['country'],'user_type' =>$results_row['user_type']

);

}
return $returned_results;
}

}


function search_users($users){
$returned_results = array();
$where = " ";
$users = preg_split('/[\s]+/ ' , $users);
$total_users = count($users);

foreach($users as $user => $users){
$where .= "`username` LIKE '%$users%'";
if ($user != ($total_users - 1)) {
$where .= " AND ";
}
}
$results = "SELECT * FROM `users` WHERE (`username` LIKE '%$users%' OR `phone` LIKE '%$users%' OR `email` LIKE '%$users%')";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) :0;
if ($results_num === 0){
return false;
}else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] =array(
'user_id' =>$results_row['user_id'],'email' =>$results_row['email'],'phone' =>$results_row['phone']
);

}
return $returned_results;
}

}



function search_trans($users){
$returned_results = array();
$where = " ";
$users = preg_split('/[\s]+/ ' , $users);
$total_users = count($users);

foreach($users as $user => $users){
$where .= "`username` LIKE '%$users%'";
if ($user != ($total_users - 1)) {
$where .= " AND ";
}
}
$results = "SELECT * FROM `users` WHERE (`username` LIKE '%$users%' OR `phone` LIKE '%$users%' OR `email` LIKE '%$users%')";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) :0;
if ($results_num === 0){
return false;
}else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] =array(
'user_id' =>$results_row['user_id'],
'username' =>$results_row['username'],'email' =>$results_row['email'],'firstname' =>$results_row['firstname'],'lastname' =>$results_row['lastname'],'sex' =>$results_row['sex'],'phone' =>$results_row['phone'],'bankname' =>$results_row['bankname'],'accountno' =>$results_row['accountno'],'accountname' =>$results_row['accountname']

);

}
return $returned_results;
}

}


function search_listings($listings){
$returned_results = array();
$where = " ";
$listings = preg_split('/[\s]+/ ' , $listings);
$total_listings = count($listings);

foreach($listings as $list => $listing){
$where .= "`address` LIKE '%$listing%'";
if ($list != ($total_listings - 1)) {
$where .= " AND ";
}
}
$results = "SELECT * FROM  `houses` WHERE `address` LIKE '%$listing%' OR `state` LIKE '%$listing%'";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) :0;
if ($results_num === 0){
return false;
}else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] =array(
'house_id' =>$results_row['house_id'],
'address' =>$results_row['address'],'property_type' =>$results_row['property_type'],'bedrooms' =>$results_row['bedrooms'],'monthly_rent' =>$results_row['monthly_rent'],'description' =>$results_row['description'],'state' =>$results_row['state'],'square_feet' =>$results_row['square_feet'],'photos' =>$results_row['photos'],'house_status' =>$results_row['house_status']

);

}
return $returned_results;
}

}





function search_questions($questions){
$returned_results = array();
$where = " ";
$questions = preg_split('/[\s]+/ ' , $questions);
$total_questions = count($questions);

foreach($questions as $quest => $question){
$where .= "`post` LIKE '%$question%'";
if ($quest != ($total_questions - 1)) {
$where .= " AND ";
}
}
$results = "SELECT * FROM  `post` WHERE `post` LIKE '%$question%'";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) :0;
if ($results_num === 0){
return false;
}else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] =array(
'post_id' =>$results_row['post_id'],
'post' =>$results_row['post'],
'users_id' =>$results_row['users_id'],
'date' =>$results_row['date'],
'time' =>$results_row['time'],

);

}
return $returned_results;
}

}





function search_advices($advices){
$returned_results = array();
$where = " ";
$advices= preg_split('/[\s]+/ ' , $advices);
$total_advices = count($advices);

foreach($advices as $advi => $advice){
$where .= "`comment` LIKE '%$advice%'";
if ($advi != ($total_advices - 1)) {
$where .= " AND ";
}
}
$results = "SELECT * FROM  `comment` WHERE `comment` LIKE '%$advice%'";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) :0;
if ($results_num === 0){
return false;
}else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] =array(
'id' =>$results_row['id'],
'comment' =>$results_row['comment'],
'post_id' =>$results_row['post_id'],
'users_id' =>$results_row['users_id'],

);

}
return $returned_results;
}

}




		
?>