<?

function debug($var){
	echo "<hr><div align=\"left\"><pre>";
	print_r($var);
	echo "</pre></div><hr>";
}

function debugrs($rs) {
	global $sys;
	require_once(gluePath($sys['root'], "system/class/adodb/tohtml.inc.php"));
	rs2html($rs,'border=1 cellpadding=1 cellspacing=0');
}

function iframeJSdebug($var) {
	echo "</script>";
	debug($var);
	echo "<script>";
}

function isCleanPath($path) {
	if (preg_match("/^\.{1,}\//",$path) || 
		preg_match("/\/\.{1,}$/",$path) ||
		preg_match("/\/\.{1,}\//",$path) ||
		$path == "..") {
		return false;
	} else {
		return true;
	}
}


function isCleanFilename($filename) {
	global $ilegalCharacters;
	for ($i = 0 ; $i < sizeof($ilegalCharacters) ; $i++) {
		if (substr_count($filename, $ilegalCharacters[$i]) > 0){
			return false;
		}
	}
	if (substr_count($filename, "./") > 0){
			return false;
	}
	
	if ($filename == ".." || $filename == ".") {
		return false;
	}
	return true;
}

function cleanPath($dir) {
	$dir = trim($dir);
	//make all backslashes slashes
	$dir = str_replace("\\", "/", $dir);
	$dir = str_replace("%2F", "/", $dir);
	//multiple slashes to single slahes
	$dir = preg_replace('~/+~', '/', $dir);
	//strip trailing slash
	if (substr($dir, -1) == "/") {
		$dir = substr($dir, 0, strlen($dir)-1);
	}
	if ($dir == "..") {
		$dir = "";
	}
	return $dir;
}


mb_internal_encoding("UTF-8");
function gluePath() {
	$arg_list = func_get_args();
	for ($i = 0 ; $i < sizeof($arg_list); $i++) {
		if ($i == 0) {
			$p = rtrim($arg_list[$i], "/");
		} else {
			$p = trim($arg_list[$i], "/");
		}
		if (strlen($p) > 0) {$parts[] = $p;}
	}
	return @implode($parts, "/");
}



	function sortTitle($title, $sortCriteria, $url, $imgFolder = "images/interface/") {
	?>
	<a href="<?echo $url?>&sort=<?echo $sortCriteria?>&sortDir=<?
if ($sortCriteria == $_SESSION['detailSort']) {
	if ($_SESSION['sortDir'] == "ASC") {
		echo "DESC";
	} else {
		echo "ASC";
	}
} else {
	echo $_SESSION['sortDir'];
}
?>"><?echo $title;


?></a> 
<?
if ($_SESSION['detailSort'] == $sortCriteria && $_SESSION['detailSort']){
	if ($_SESSION['sortDir'] == "DESC") {
		?>
		<img src="<?echo $imgFolder;?>arrow-list-down.gif" width="8" height="7" border="0" alt="">
		<?
	} else {
		?>
		<img src="<?echo $imgFolder;?>arrow-list-up.gif" width="8" height="7" border="0" alt="">
		<?
	}

}
	}

function isCfgOn($option) {
	if (strtolower(ini_get($option)) == "on" || ini_get($option) == "1" || strtolower(ini_get($option)) == "yes") {
		return true;
	} else {
		return false;
	}
}


function arrayMultiSort($array, $sortCriteria) {


	if (is_array($array)) {
	$keys = array_keys($array);
	$tmpArray = $array;
	
	$sortKeys = array_keys($sortCriteria);
	$result = array_diff ($keys, $sortKeys); 
	$keys = array_merge($sortKeys, $result);
	
	$php = "array_multisort(\n";
	for ($i = 0 ; $i < sizeof($keys) ; $i++) {
		$php .= "\$tmpArray[".$keys[$i]."]";
		if ($i < sizeof($keys)-1) {
			$php .= ",";
		}
		if (array_key_exists($keys[$i], $sortCriteria)) {
			if ($i >= sizeof($keys)-1) {
				$php .= ",";
			}
			$php .= $sortCriteria[$keys[$i]];
			if ($i < sizeof($keys)-1) {
				$php .= ",";
			}
		}
		$php .= "\n";
	}
	$php .= ");";
	
	//debug($php);
	eval($php);
	return $tmpArray;
	} else {
	return false;
	}
}

function bin2asc ($temp) {
 $len = strlen($temp);
 for ($i=0;$i<$len;$i+=8) $data.=chr(bindec(substr($temp,$i,8)));
 return $data;
}
function asc2bin ($temp) {
 $len = strlen($temp);
 for ($i=0; $i<$len; $i++) $data.=sprintf("%08b",ord(substr($temp,$i,1)));
 return $data;
}


function onespace($a) {
	 return str_replace("  ", " ", $a);
}


function glueURL($args) {
	$url = "";
	$i = 0;
	foreach ($args as $key => $value) {
		if (strlen($value) > 0) {
			if ($i == 0) {
				$url .= "?";
			} else {
				$url .= "&";
			}
			$url .= $key . "=" . $value;
			$i++;
		}
	}
	if (strlen($url) == 0) {
		$url = "?";
	}
	return $url;
}

function setDBPermVar($var, $val) {
	global $db, $settings;
	$sql = "SELECT * FROM ".$settings->config['permvars_tablename']." WHERE var = '".$var."'"; 
	$rs = $db->query($sql);
	$updateSQL = $db->GetUpdateSQL($rs, array ("val"=>$val));
	$db->query($updateSQL);
	return $db->Affected_Rows();
}

function getDBPermVar($var) {
	global $db, $settings;
	return $db->GetOne("SELECT `val` FROM ".$settings->config['permvars_tablename']." WHERE `var` = '".$var."'");
}



function my_fnmatch($pattern, $file) {

   for($i=0; $i<strlen($pattern); $i++) {
     if($pattern[$i] == "*") {
       for($c=$i; $c<max(strlen($pattern), strlen($file)); $c++) {
         if(my_fnmatch(substr($pattern, $i+1), substr($file, $c))) {
           return true;
         }
       }
       return false;
     }
     if($pattern[$i] == "[") {
       $letter_set = array();
       for($c=$i+1; $c<strlen($pattern); $c++) {
         if($pattern[$c] != "]") {
           array_push($letter_set, $pattern[$c]);
         }
         else break;
       }
       foreach ($letter_set as $letter) {
         if(my_fnmatch($letter.substr($pattern, $c+1), substr($file, $i))) {
           return true;
         }
       }
       return false; 
     } 
     if($pattern[$i] == "?") {
       continue;
     }   
     if($pattern[$i] != $file[$i]) {
       return false;
     }
   }
   return true;
 }

function getUpOneDir($path) {
	global $str;
	$splited = explode("/", $path);
	$i = sizeof($splited);
	for ($j = 0 ; $j < $i-1 ; $j++) {
	$upOneDirAddr .= $splited[$j];
		if ($j == $i-2) {
		} else {
		$upOneDirAddr .= "/";
		}
	}
	if ($upOneDirAddr) {
		$rtrn = $upOneDirAddr;
	} else {
		$rtrn = "/";
	}
	return $rtrn;
}


function getBaseURL() {
	global $fm;
	$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
	$proto = strtolower($_SERVER["SERVER_PROTOCOL"]);
	$protocol = substr($proto, 0, strpos($proto, "/")).$s; 
	$port = ($_SERVER["SERVER_PORT"] == "80" || $_SERVER["SERVER_PORT"] == "443") ? "" : (":".$_SERVER["SERVER_PORT"]);
	$REQUEST_URI = $_SERVER['SCRIPT_NAME'];
	if ($_SERVER['QUERY_STRING'] != '') {
		$REQUEST_URI .= '?'.$_SERVER['QUERY_STRING'];
	}
	$_SERVER['REQUEST_URI'] = $REQUEST_URI;
	$folder = $_SERVER['REQUEST_URI'];
	
	if (substr($folder, -1) == "#") {
		$folder = substr($folder, 0, strlen($folder)-1);
	}
	if (substr($folder, -1) == "?") {
		$folder = substr($folder, 0, strlen($folder)-1);
	}
	if (strtolower(substr($folder, -9)) == "index.php") {
		$folder = substr($folder, 0, strlen($folder)-9);
	}
	if (substr($folder, -1) != "/") {
		$folder = $folder."/";
	}
	
	/*
	if (substr($_SERVER['SERVER_NAME'], 0, 3) != "www") {
		$_SERVER['SERVER_NAME'] = "www.".$_SERVER['SERVER_NAME'];
	}*/
	
	
	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$folder;
}

function getBaseURLnoSecure() {
	global $fm;
	$REQUEST_URI = $_SERVER['SCRIPT_NAME'];
	if ($_SERVER['QUERY_STRING'] != '') {
		$REQUEST_URI .= '?'.$_SERVER['QUERY_STRING'];
	}
	$_SERVER['REQUEST_URI'] = $REQUEST_URI;
	$folder = $_SERVER['REQUEST_URI'];
	
	if (substr($folder, -1) == "#") {
		$folder = substr($folder, 0, strlen($folder)-1);
	}
	if (substr($folder, -1) == "?") {
		$folder = substr($folder, 0, strlen($folder)-1);
	}
	if (strtolower(substr($folder, -9)) == "index.php") {
		$folder = substr($folder, 0, strlen($folder)-9);
	}
	if (substr($folder, -1) != "/") {
		$folder = $folder."/";
	}
	return "http://".$_SERVER['SERVER_NAME'].$folder;
}

?>