<?php
class funcClass
{
	var $_imageErr = ''; // Stores error of image uploading
	var $_emailErr = ''; // Stores error of email sending

	function getRandomNo($limit=8)
	{
		$rnd_id = crypt(uniqid(rand(),1)); 
		$rnd_id = strip_tags(stripslashes($rnd_id)); 
		$rnd_id = str_replace(".","",$rnd_id); 
		$rnd_id = strrev(str_replace("/","",$rnd_id)); 
		$rnd_id = substr($rnd_id,0,$limit); 
		return $rnd_id;
	}
	
	function random_num()
	{
		$num1 = rand(1000, 9999);
		$num2 = rand(1000, 9999);
		$num = $num1 . $num2;
		return $num;
	}
	
	function checkEmail($address) 
	{ 
		if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $address))
			return false; 
		else 
			return true; 
			
	} 
	
	function fetchParameterValue($parameter)
	{
		global $sqlObj;
		$query = "SELECT FIELDVALUE FROM ##settings WHERE FIELDCONSTANT = '$parameter'";
		return $sqlObj->LoadResult($query);	
	}
	
	function login($email, $password)
	{
		// global $sqlObj;
		
		// $query = sprintf("SELECT USER_ID, FULLNAME, USER_TYPE FROM ##super_user WHERE EMAIL = '%s' AND PASSWORD  = SHA1('%s') AND IS_ACTIVE=1",
		// mysql_real_escape_string($email),
		// mysql_real_escape_string($password));
		// $result = $sqlObj->getObjectResult($query);
		// return $result;
		global $sqlObj;
		$conntion = $sqlObj->mysqlClass();
		
		$super_user = '##super_user';
		$query =sprintf("SELECT USER_ID, FULLNAME ,USER_TYPE ,IS_ACTIVE FROM $super_user WHERE EMAIL = '$email' AND PASSWORD  = SHA1('$password')",
		mysqli_real_escape_string($conntion,$email),
		mysqli_real_escape_string($conntion,$password));

		$result = $sqlObj->getObjectResult($query);
		return $result;	
	}

	function changepassword($userid, $password)
	{
		global $sqlObj;
		$conntion = $sqlObj->mysqlClass();
		$super_user = '##super_user';
		$query = sprintf("SELECT COUNT(USER_ID) FROM $super_user WHERE USER_ID = '$userid' AND PASSWORD  = SHA1('$password') AND IS_ACTIVE=1",
		mysqli_real_escape_string($conntion,$userid),
		mysqli_real_escape_string($conntion,$password));
		$result = $sqlObj->loadResult($query);
		return $result;	
	}
	
	
	function encodeURL($url)
	{
		return urlencode($url);
	}
	
	function decodeURL($url)
	{
		return urldecode($url);
	}
	
	function str2array( $string, $delimiter = ',', $kv = '=>')
    {
        $string = trim($string);
		// If the delimited character is also on the very end of the string
		// remove that before exploding the string.
		$lastChar = substr($string, -1);    // returns the last character of the string
		
		if ($lastChar == ';')
		{
			$string = substr($string, 0, -1);	
		}
		
		if ($element = explode( $delimiter, $string ))
        {
            // create parts
            foreach ( $element as $key_value )
            {
                // key -> value pair or single value
                $atom = explode( $kv, $key_value );
			
                if(isset($atom[1]))
                {
                  $key_arr[trim($atom[0])] = trim($atom[1]);
                }
                else
                {
                    $key_arr[] = trim($atom[0]);
                }
            }
        }
        else
        {
            $key_arr = false;
        }

        return $key_arr;
    }
	
	function checkSuperAdmin()
	{
		global $mainObj, $sqlObj, $config_abs_path;
		
		$query = "SELECT COUNT(*) FROM ##super_admin WHERE SuperAdminID=$mainObj->userID";
		$cnt = $sqlObj->loadResult($query);
		if(empty($cnt))
		{
			echo '<table cellspacing="0" cellpadding="0" border="0" align="center" height="300" width="80%">
			<tr>
				<td><center><div class="warning">You are not authorised to access this location</div></center></td>
			</tr>
			</table>';
			require_once ('includes/footer.php');
			die();
		}
	}
	
	function UpdateRecord($table,$setField,$setValue,$whereField,$whereValue)
	{
		global $sqlObj;
		$query = "UPDATE $table SET $setField = '$setValue' WHERE $whereField = '$whereValue'";
	
		$sqlObj->Query($query);
	}
	
	function FetchSingleRecord($fetchField,$table,$whereCond)
	{
		global $sqlObj;
		$query = "SELECT $fetchField FROM $table WHERE $whereCond";
		
		return $sqlObj->LoadResult($query);		
	}

	function DeleteRecord($table,$whereField,$whereValue)
	{
		global $sqlObj;
		$query = "DELETE FROM $table WHERE $whereField = '$whereValue'";
   
		$sqlObj->Query($query);
	}
  
  	function CustomDelete($query)
	{
		global $sqlObj;
		$sqlObj->Query($query);
	}
  
  	function DeleteAllRecord($table)
	{
		global $sqlObj;
		$query = "DELETE FROM $table ";
		$sqlObj->Query($query);
	}
	
	function UpdateThisRecord($table,$setArr,$whereArr)
	{
		global $sqlObj;
		
		$setFields='';
		foreach($setArr as $key=>$val)
		{
			if (!empty($setFields))
				$setFields .= ', ';
			$setFields .= "$key = '$val'";
		}
		
		$whereFields='';
		foreach($whereArr as $key=>$val)
		{
			if (!empty($whereFields))
				$whereFields .= ' AND ';
			$whereFields .= "$key = '$val'";
		}

   		$query = "UPDATE $table SET $setFields WHERE $whereFields"; 
    
		$sqlObj->Query($query);
	}

	function GetRecordDetails($table,$fieldArr)
	{
		global $sqlObj;
		$fields='';
		foreach($fieldArr as $key=>$val)
		{
			if (!empty($fields))
				$fields .= ' AND ';
			$fields .= "$key = '$val'";
		}

		$query = "SELECT * FROM $table WHERE $fields";
		$result = $sqlObj->GetObjectResult($query);
		
		return $result;

	}
	
	function insertQuery($query)
	{
		global $sqlObj;
		return $sqlObj->iQuery($query);
	}
	
	function updateQuery($query)
	{
		global $sqlObj;
		return $sqlObj->uQuery($query);
	
	}
		
	function UpdateAll($table,$setField,$setValue)
	{
		global $sqlObj;
		$query = "UPDATE $table SET $setField = '$setValue'";
		$sqlObj->Query($query);
	
	}
	
	function AppendSlashes ($sqlInput)
	{
	  // if(get_magic_quotes_gpc() == 0)
	  // {
		if(is_array($sqlInput))
		{
		  foreach($sqlInput as $key=>$val)
		  {
			 $sqLInput[$key] = addslashes($val);
		  }
		}
		else
		{
		   $sqlInput = addslashes($sqlInput);
		} 
	  // }
	 return $sqlInput;
	}
	

	function FetchAllRecords($table,$sortField,$condition,$start='',$limit='')//fetchAllRecords
	{
		global $sqlObj;

		$query = "SELECT * FROM $table"; 
		if($condition)
		  $query .= $condition;
	
		if($sortField)
		  $query .= " ORDER BY $sortField";
		
		if(!empty($limit))
				$query .= " LIMIT $start,$limit";
			
		$result = $sqlObj->GetObjectResult($query);
		return $result;

	}

	function FetchColumn($table,$fields,$condition)
	{
		global $sqlObj;
		
		$query = "SELECT $fields FROM $table";
		if($condition)
		{
		  $query .= $condition ;
		}
		$result = $sqlObj->GetObjectResult($query);
		return $result;
	}

	function RemoveSlashes($sqlOutput)
	{
	  if(is_array($sqlOutput))
		{
		  foreach($sqlOutput as $key => $val)
		  {
			if(!empty($val)){
				$sqlOutput[$key] = htmlspecialchars(stripslashes($val),ENT_QUOTES);
			}else{
				$sqlOutput[$key] = '';
			}
			 
		  }
		}
		else
		{
			if(!empty($sqlOutput)){
				$sqlOutput = htmlspecialchars(stripslashes($sqlOutput), ENT_QUOTES);
			}else{
				$sqlOutput = '';
			}
		   
		} 
	  return $sqlOutput;
	}
	
	function CustomQuery($query)
	{
		global $sqlObj;
		$result = $sqlObj->GetObjectResult($query);
		return $result;
	}
	
	function CustomQueryArray($query)
	{
		global $sqlObj;
		$result = $sqlObj->loadAssocList($query);
		return $result;
	}
	
  	function CustomQueryCount($query)
	{
		global $sqlObj;
		$result = $sqlObj->LoadResult($query);
		return $result;
	}
	
	function CountAllRecords($tableName,$searchTxt='',$searchArray='')
	{
		global $sqlObj;

		$qry = "SELECT count(*) FROM $tableName";
		if(!empty($searchTxt))
		{
			$searchctr = "";
			$searchArray1=explode(",",$searchArray);
			foreach($searchArray1 as $searchField)
			{ 
				if(!empty($searchctr))
					$searchctr.=" OR ";
				$searchctr .= $searchField ." like '%$searchTxt%'";
			}
			
			if (stristr($tableName," WHERE ") === FALSE)
				$qry .= " WHERE ".$searchctr ;
			else
				$qry .= " AND (".$searchctr . ")";
				
		}
	   	return $sqlObj->LoadResult($qry);
	}

	function FetchRecords($tableName,$searchTxt='',$searchArray='',$sortField='',$start='',$limit='')
	{
		global $sqlObj;

		$qry = "SELECT * FROM $tableName ";
		if(!empty($searchTxt))
		{
			$searchctr = "";
			$searchArray1=explode(",",$searchArray);
			
			foreach($searchArray1 as $searchField)
			{ 
				if(!empty($searchctr))
					$searchctr.=" OR ";
				$searchctr .= $searchField ." like '%$searchTxt%' ";
			}
			if (stristr($tableName," WHERE ") === FALSE)
				$qry .= " WHERE ".$searchctr ;
			else
				$qry .= " AND ".$searchctr ;	
		}
		if(!empty($sortField))
			$qry .= " order by $sortField";

		if(!empty($limit))
			 $qry .= " LIMIT $start,$limit";
	
 		// print $qry; 	
		$result = $sqlObj->GetObjectResult($qry);
		
		return $result;
	}
	
	function FetchReqRecords($table,$field,$value,$start='',$limit='')//fetchTypeMembers
	{
		global $sqlObj;
		$query = "SELECT * FROM $table WHERE $field=$value";
		if(!empty($limit))
			 $query .= " LIMIT $start,$limit";

		$result = $sqlObj->GetObjectResult($query);
		return $result;
	}
	
	function replaceAll($replace, $body)
	{
		$updatedContent = $body;
		foreach ($replace as $key => $val)
		{
			$updatedContent = str_ireplace($key, $val, $updatedContent);
		}
		return $updatedContent;
	}

};

?>
