<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: http://www.flynax.com/license-agreement.html
 *	PRODUCT: General Classifieds
 *	
 *	FILE: REEFLESS.CLASS.PHP
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

class reefless extends rlDb
{
	/**
	* requires php class file, creates global class object
	*
	* @param string $className  - loaded class name
	* @param string $type       - class type ( null or admin )
	* @param string $plugin     - plugin name
	* @param mixed $class_param - class parameter (optional)
	**/
	function loadClass( $className, $type = null, $plugin = false, $class_param = false )
	{
		$className = ucfirst($className);
		$className = 'rl' . $className;
		
		if ( !is_object( $className ) )
		{
			$path = $plugin ? RL_PLUGINS . $plugin . RL_DS :RL_CLASSES;
			
			if ( !empty( $type ) && $type == 'admin' )
			{
				$fileSource = $path . 'admin' . RL_DS .  $className . '.class.php' ;
			}
			elseif ( $type == null )
			{
				$fileSource = $path . $className . '.class.php';
			}
	
			global $$className;
			
			if ( !is_object( $$className ) )
			{
				if ( file_exists( $fileSource ) )
				{
					require_once( $fileSource );
				}
				else 
				{
					die( "The '{$className}' class not found" );
				}
				
				if ( $class_param )
				{
					eval('$'. $className .' = &new '. $className .'('. $class_param .');');
				}
				else
				{
					$$className = &new $className;
				}
			
				$GLOBALS[$className] = $$className;
			}
		}
	}

	/**
	* redirect to referer
	*
	* @param string $varString - additional url string
	* 
	* @return redirect
	**/
	function referer( $vars = false, $cur_lang = false, $new_lang = false )
	{
		$request_url = $_SERVER['HTTP_REFERER'];

		//if IE
		if (empty($request_url))
		{
			$request_url = $_SERVER['REQUEST_URI'];
		}
		
		if ( $GLOBALS['config']['mod_rewrite'] && $cur_lang )
		{
			$replace = $search = RL_URL_HOME;
			$search .= $GLOBALS['config']['lang'] == $cur_lang ? '' : $cur_lang . "/";
			$replace .= $GLOBALS['config']['lang'] == $new_lang ? '' : $new_lang . "/";
			
			$request_url = str_replace($search, $replace, $request_url);
		}
		
		if ( !empty($vars) )
		{
			$var_char = false !== strpos($request_url, '?') ? '&' : '?' ;
			$request_url .= $var_char . $vars;
		}
		
		header( "Location: " . $request_url );
		exit;
	}
	
	/**
	* redirect to ...
	*
	* @param array $varString - additional url array
	* @param string $target - target url
	* 
	* @return redirect
	**/
	function redirect( $vars = null, $target = false )
	{
		if ( !$vars && !$target )
			return false;
			
		
		if ($target)
		{
			header( "Location: " . $target );
			exit;
		}
		
		if (defined('REALM'))
		{
			$request_url = str_replace( trim(RL_DIR, RL_DS), '', $_SERVER['PHP_SELF']);
			$request_url = trim($request_url, '/');
		}
		else 
		{
			$request_url = str_replace( trim(RL_DIR, RL_DS), '', $_SERVER['REQUEST_URI']);
			$request_url = trim($request_url, '/');
		}

		$request_url = RL_URL_HOME . $request_url;
		
		if ( is_array($vars) )
		{
			if (defined('REALM'))
			{
				$request_url .= "?";
			}
			else
			{
				if ( false !== strpos( $request_url, '?' ) )
				{
					$request_url .= "&";
				}
				else
				{
					$request_url .= "?";
				}
			}
			
			foreach ( $vars as $var => $value )
			{
				$request_url .= $var."=".$value."&";
			}
			$request_url = substr($request_url, 0, -1);
		}

		header( "Location: " . $request_url );
		exit;
	}
	
	/**
	* refresh page
	*
	* @param array $varString - additional url array
	* 
	* @return redirect
	**/
	function refresh()
	{
		$addUrl = str_replace( RL_DIR, '', $_SERVER['REQUEST_URI']);
		$addUrl = trim($addUrl, '/');
		$refresh = RL_URL_HOME . $addUrl;

		header( "Location: " . $refresh );
		exit;
	}
	
	/**
	* check admin session expire time
	**/
	function checkSessionExpire()
	{
		$ses_exp = session_cache_expire()-5;

		if ( isset($_SESSION['admin_expire_time']) && time() - $_SESSION['admin_expire_time'] > $ses_exp * 60 )
		{
			session_destroy();
			return false;
		}
		else
		{
			$_SESSION['admin_expire_time'] = $_SERVER['REQUEST_TIME'];
			return true;
		}
	}
	
	/**
	* read directory
	*
	* @param string $dir - directory path
	* @param bool $dir_mode - read directories only
	* @param bool $type - require content type
	* 
	* @return directory content
	**/
	function scanDir( $dir = null, $dir_mode = false, $type = false )
	{
		if (is_dir($dir)) 
		{
			if ($dh = opendir($dir)) 
			{
				$index = 0;
				while (($file = readdir($dh)) !== false) 
				{
					if (!ereg( '^\.{1,2}', $file ))
					{
						if ($type)
						{
							$content[$index]['name'] = $file;
							$content[$index]['type'] = filetype($dir . $file);
							$index++;
						}
						else
						{
							if ($dir_mode)
							{
								if (is_dir($dir . $file))
								{
									$content[] = $file;
								}
							}
							else
							{
								$content[] = $file;
							}
						}
					}
				}
				closedir($dh);
			}
		}

		return $content;
	}
	
	/**
	* get page content
	*
	* @param string $url - page url
	* 
	* @return page content
	**/
	function getPageContent($url)
	{
		$content = null;
		$user_agent = 'Flynax Bot';

		if (ini_get('allow_url_fopen'))
		{
			$stream = fopen($url, "r");
			if ($stream) {
			   while (!feof($stream)) {
			       $content .= fgets($stream, 4096);
			   }
			   fclose($stream);
			}
		}
		elseif (extension_loaded('curl'))
		{
			set_time_limit(50);
			
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
			curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
			$content = curl_exec($ch);
			curl_close($ch);
		}
		else
		{
			return 'Unable to get content from: '. $url;
		}

		return $content;
	}
	
	/**
	* delete directory (recursive)
	*
	* @param string $dirname - directory name
	* @param bool $passive - passive mode, remove the requested direcotry in case if it is empty
	* 
	* @return bool
	**/
	function deleteDirectory( $dirname = false, $passive = false )
	{
		if ( is_dir($dirname) )
		{
			$dir_handle = opendir($dirname);
		}
		
		if ( !$dir_handle )
		{
			return false;
		}
		
		// passive mode
		if ( $passive )
		{
			$empty = true;
			$file = readdir($dir_handle);
			
			while( $file = readdir($dir_handle) )
			{
				if ( $file != "." && $file != ".." )
				{
					$empty = false;
				}
			}
			
			if ( $empty )
			{
				rmdir($dirname);
			}
			
			return true;
			exit;
		}
		
		while( $file = readdir($dir_handle) )
		{
			if ( $file != "." && $file != ".." )
			{
				if ( !is_dir($dirname . RL_DS . $file) )
   				{
					unlink($dirname . RL_DS . $file);
   				}
				else
				{
					$this -> deleteDirectory($dirname . RL_DS . $file);
				}
			}
		}
		
  		closedir($dir_handle);
		rmdir($dirname);
		
		return true;
	}

	
	/**
	* get tmp file
	*
	* @param string $field - file input name
	* @param string $parent - parent file input name, ex: ... name="profile[photo]", 'profile' is parent, 'photo' is field
	* @param string $dom - id of the dom object
	* 
	* @return html dom block
	**/
	function getTmpFile( $aParams = false )
	{
		global $lang;
		
		$field = $aParams['field'];
		$parent = $aParams['parent'];
		$id = $aParams['id'] ? $field .'_'. $aParams['id'] .'_tmp' : false;
		
		if ( $_FILES[$parent]['name'][$field] || $_FILES[$field]['name'] )
		{
			$file_name = $parent && $_FILES[$parent] ? $_FILES[$parent]['name'][$field] : $_FILES[$field]['name'];
			$file_name = mt_rand() .'_'. $file_name;
			$file_type = $parent && $_FILES[$parent] ? $_FILES[$parent]['type'][$field] : $_FILES[$field]['type'];
			$file_tmp_dir = $parent && $_FILES[$parent] ? $_FILES[$parent]['tmp_name'][$field] : $_FILES[$field]['tmp_name'];
			$file_dir = RL_UPLOAD . $file_name;
			
			if ( move_uploaded_file( $file_tmp_dir, $file_dir ) )
			{
				chmod(  $file_dir, 0777 );
				
				/* print image */
				if ( strpos($file_type, 'image') !== false && is_readable($file_dir) )
				{
					$file_info = getimagesize(RL_UPLOAD . $file_name);
					$resize_type = $file_info[0] > $file_info[1] ? 'width' : 'height';
					
					$content = '<img alt="" title="'. $file_name .'" style="'. $resize_type .': 250px;" src="'. RL_URL_HOME .'tmp/upload/'. $file_name .'" />';		
				}
				/* print file */
				else
				{
					$file_name_display = substr($file_name, strpos($file_name, '_')+1);
					$content = '<span style="font-style:italic;" class="dark_13" title="'. $file_name .'"><b>'. $file_name_display .'</b></span>';		
				}
				
				if ( $parent )
				{
					$_SESSION['tmp_files'][$parent][$field] = $file_name;
				}
				else
				{
					$_SESSION['tmp_files'][$field] = $file_name;
				}
			}
			else
			{
				trigger_error( "Can't move uploaded file", E_WARNING );
				$GLOBALS['rlDebug'] -> logger("Can't move uploaded file");
			}
		}
		elseif ( ($_SESSION['tmp_files'][$parent][$field] && is_readable(RL_UPLOAD . $_SESSION['tmp_files'][$parent][$field])) || ($_SESSION['tmp_files'][$field] && is_readable(RL_UPLOAD . $_SESSION['tmp_files'][$field])) )
		{
			$file_name = $_SESSION['tmp_files'][$parent][$field] ? $_SESSION['tmp_files'][$parent][$field] : $_SESSION['tmp_files'][$field];
			$file_info = getimagesize(RL_UPLOAD . $file_name);
			
			/* print image */
			if ( strpos($file_info['mime'], 'image') !== false )
			{
				$resize_type = $file_info[0] > $file_info[1] ? 'width' : 'height';
				$content = '<img class="thumbnail" alt="" title="'. $file_name .'" style="'. $resize_type .': 250px;" src="'. RL_URL_HOME .'tmp/upload/'. $file_name .'" />';
			}
			/* print file */
			else
			{
				$file_name_display = substr($file_name, strpos($file_name, '_')+1);
				$content = '<span style="font-style:italic;" class="dark_13" title="'. $file_name .'"><b>'. $file_name_display .'</b></span>';		
			}
		}
		
		if ( $content )
		{
			$side = RL_LANG_DIR == 'rtl' ? 'right' : 'left';
			$id = $id ? 'id="'. $id .'"' : '';
			$out = '<div '. $id .' class="fleft" style="margin: 0 0 5px;">'
					.'<div style="padding: 3px 0 5px;">'
					.'<table class="sTable"><tr><td>'
					.$lang['currently_uploaded_file'] 
					.'</td><td class="ralign" style="padding-'. $side .': 10px;">'
					.'<a href="javascript:void(0)" onclick="xajax_removeTmpFile(\''. $field .'\', \''. $parent .'\', \''. $id .'\');">'
					.$lang['remove']
					.'</a>'
					.'</td></tr></table>'
					.'</div>'
					. $content 
					.'</div><div class="clear"></div>';
		}
		
		return $out;
	}
	
	/**
	* remove tmp file
	*
	* @param string $field - file input name
	* @param string $parent - parent file input name, ex: ... name="profile[photo]", 'profile' is parent, 'photo' is field
	* @param string $id - id of the dom object
	*
	* @package Ajax
	*
	**/
	function ajaxRemoveTmpFile( $field = false, $parent = false, $id = false )
	{
		global $_response;

		// check admin session expire
		if ( $this -> checkSessionExpire() === false )
		{
			$redirect_url = RL_URL_HOME . ADMIN ."/index.php";
			$redirect_url .= empty($_SERVER['QUERY_STRING']) ? '?session_expired' : '?'. $_SERVER['QUERY_STRING'] .'&session_expired';
			$_response -> redirect( $redirect_url );
		}
		
		$file_name = $parent ? $_SESSION['tmp_files'][$parent][$field] : $_SESSION['tmp_files'][$field];
		unlink( RL_UPLOAD . $file_name );
		
		$field_name = $parent ? $parent ."[{$field}]" : $field;
		
		if ( $id )
		{
			$_response -> script("$('div.#{$id}').slideUp('slow')");
		}
		else
		{
			$_response -> script("$('input[name=\"{$field_name}\"]').parent().find('div:first').slideUp('slow')");
		}
		
		return $_response;
	}
	
	/**
	* generate hash string
	*
	* @param int $number - number of characters
	* @param string $case - case of characters (lower, upper, hex, password)
	* @param bool $numbers - include numbers
	*
	**/
	function generateHash( $number = 32, $case = 'lower', $numbers = true )
	{
		switch ($case){
			case 'lower':
				$chars = range('a', 'z');
				break;
			case 'upper':
				$chars = range('A', 'Z');
				break;
			case 'hex':
				$chars = range('A', 'F');
				break;
			case 'password':
				$chars = range('a', 'z');
				$chars = array_merge( $chars, range('A', 'Z'));
				$chars = array_merge( $chars, array('!','@','#','^','*','(',')','[',']') );
				break;
		}
		
		for ($i = 0; $i < $number; $i++)
		{
			$turn = $numbers ? rand(0, 1) : 0;
			
			if ( $turn )
			{
				$string .= rand(0, 9);
			}
			else
			{
				$index = rand(0, count($chars)-1);
				$string .= $chars[$index];
			}
		}
		
		return $string;
	}
	
	/**
	* create new writable directory
	*
	* @param string $dir - directory to create
	*
	* @return bool
	*
	**/
	function rlMkdir( $dir = false )
	{
		if ( !$dir )
			return false;
			
		$dir = str_replace(RL_ROOT, '', $dir);
		$dirs = explode(RL_DS, $dir);
		$directory = RL_ROOT;
		
		foreach ( $dirs as $next )
		{
			$directory .= $next . RL_DS;
			
			if ( is_dir($directory) )
			{
				if ( !is_writable($directory) )
				{
					chmod($directory, 0755);
					if ( !is_writable($directory) )
					{
						chmod($directory, 0777);
					}
				}
			}
			else
			{
				mkdir($directory);
				chmod($directory, 0755);
				if ( !is_writable($directory) )
				{
					chmod($directory, 0777);
				}
			}
		}
		
		return true;
	}
	
	/**
	* set writable permisions for directory
	*
	* @param string $dir - directory
	*
	**/
	function rlChmod( $dir = false )
	{
		if ( !$dir )
			return;
			
		chmod($dir, 0755);
		if ( !is_writable($dir) )
		{
			chmod($dir, 0777);
		}
	}
	
	/**
	* parse multilingual value
	*
	* @param string $string - value string
	* @param string $lang - return value by request language code
	*
	* @return array - values by languages, ex: array('en' => val, 'gr' => val2)
	*
	**/
	function parseMultilingual( $string = false, $lang = false )
	{
		global $config;
		
		preg_match_all('/\{\|([a-zA-Z]{2})\|\}([^\{\|]*){\|\/[a-zA-Z]{2}\|\}/', $string, $matches);
		$codes = $matches[1];
		$values = $matches[2];
		
		if ( $codes && $values )
		{
			foreach ( $codes as $index => $code )
			{
				if ( $values[$index] )
				{
					$out[$code] = $values[$index];
				}
			}
		}
		else
		{
			$out[RL_LANG_CODE] = $string;
		}
		
		if ( $lang )
		{
			if ( $out[$lang] )
			{
				return $out[$lang];
			}
			elseif ( $out[$config['lang']] )
			{
				return $out[$config['lang']];
			}
			else
			{
				return current($out);
			}
		}
		
		return $out ? $out : false;
	}
	
	/**
	* array multisort
	*
	* @param array $array - array to sort | by referent
	* @param string $field - field name to sort by
	* @param constant $sort_type - sorting type (array_multisort() function default params)
	*
	**/
	function rlArraySort( &$array = false, $field = false, $sort_type = SORT_ASC )
	{
		if ( !$array || !$field )
			return $array || false;
			
		foreach ($array as $key => $value)
		{
			$sort[] = $value[$field];
		}
		
		array_multisort($sort, $sort_type, $array);
		unset($sort);
	}
	
	/**
	* touch directory
	*
	* @param array $array - array to sort | by referent
	* @param string $field - field name to sort by
	* @param constant $sort_type - sorting type (array_multisort() function default params)
	*
	**/
	function flTouch( $dir = RL_ROOT, $ext = 'tpl' )
	{
		$files = $this -> scanDir($dir, false, true);
		
		if ( $files )
		{
			foreach ($files as $file)
			{
				if ( $file['type'] == 'dir' )
				{
					$this -> flTouch(rtrim($dir) . RL_DS . $file['name']. RL_DS, $ext);
				}
				elseif ( $file['type'] == 'file' )
				{
					if ( $ext )
					{
						$file_ext = array_reverse(explode('.', $file['name']));
						if ( $file_ext[0] == $ext )
						{
							touch(rtrim($dir) . RL_DS . $file['name']);
						}
					}
					else
					{
						touch(rtrim($dir) . RL_DS . $file['name']);
					}
				}
			}
		}
	}
}