<?php
define("root", $_SERVER['DOCUMENT_ROOT']);
$timezone = "Asia/Jakarta";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);

if (!function_exists("saltEncoding")) {
	function saltEncoding($toencode, $times=8986) {
		#enchsetenev
		$salt='s+(_a*';
		for($zo=0; $zo<$times; $zo=$zo+1) {
			$toencode=hash('sha512', $salt.$toencode);
			$toencode=md5($toencode.$salt);
		}
		return $toencode;
	}
}
function cleanQuery($string) {
	$setup=cvdb();
	$con=mysql_connect($setup["dbs"], $setup["dbu"], $setup["dbp"]);
	if(! $con)
		showMySQLError("Function: CleanQuery");
	if(get_magic_quotes_gpc())
		$string=stripslashes($string);
	$string=(phpversion()>="4.3.0")?mysql_real_escape_string($string):mysql_escape_string($string);
	return $string;
	if(! mysql_close($con))
		showMySQLError("Function: CleanQuery");
}

function cekpost($pstr, $prep) {
	$returnValue=($pstr!="" && isset($pstr))?$pstr:$prep;
	return $returnValue;
}
function numberToRoman($num) {
	$n=intval($num);
	$result='';
	$lookup=array("M" => 1000, "CM" => 900, "D" => 500, "CD" => 400, "C" => 100, "XC" => 90, "L" => 50, "XL" => 40, "X" => 10, "IX" => 9, "V" => 5, "IV" => 4, "I" => 1);
	foreach($lookup as $roman => $value) {
		$matches=intval($n / $value);
		$result .= str_repeat($roman, $matches);
		$n=$n % $value;
	}
	return $result;
}
function cout($string, $specs=array()) {
	if (!isset($specs)) {
		$specs = array('nl'=>'n');
	}
	echo $string;
	if (isset($specs)) {
		if (isset($specs["nl"])) {
			if (strtolower($specs["nl"])=="b") echo "<br />";
			if (strtolower($specs["nl"])=="n") echo "\n";
			if (strtolower($specs["nl"])=="w") echo "";
		}
	}
}
function dateFormat($time=NULL, $format=NULL) {
	if (!isset($time)) $time = time();
	if (!isset($format)) $format = 0;
	$s = "";
	$year = date('o', $time);
	$week = date('W', $time);
	switch ($format) {
		case 0: { $s='l, jS M Y'; break; }
	}
	return date($s, $time);
}

function cleanURL($string) {
	$url=str_replace("'", '', $string);
	$url=str_replace('%20', ' ', $url);
	$url=preg_replace('~[^\\pL0-9_]+~u', '-', $url); // substitutes anything but letters, numbers and '_' with separator
	$url=trim($url, "-");
	$url=iconv("utf-8", "us-ascii//TRANSLIT", $url);  // you may opt for your own custom character map for encoding.
	$url=strtolower($url);
	$url=preg_replace('~[^-a-z0-9_]+~', '', $url); // keep only letters, numbers, '_' and separator
	return $url;
}
function generateRandomString($length=30, $usevowels=false) {
	$randomString="";
	$possible="346789bcdfhjkmnprtwxyBCDFGHJKLMNPQRTWXY";
	if($usevowels)
		$possible .= "AUEaue";
	$maxlength=strlen($possible);
	if($length > $maxlength)
		$length=$maxlength;
	$i=0;
	while($i<$length) {
		$char=substr($possible, mt_rand(0, $maxlength - 1), 1);
		if(! strstr($randomString, $char)) {
			$randomString .= $char;
			$i++;
		}
	}
	return $randomString;
}

function printR($parArr) {
	echo'<pre>';print_r($parArr);echo'</pre>';
}
function check_email_address($email) {
	// First, we check that there's one @ symbol, 
	// and that the lengths are right. 
	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
		// Email invalid because wrong number of characters 
		// in one section or wrong number of @ symbols.
		return false;
	}
	// Split it into sections to make life easier
	$email_array = explode("@", $email);
	$local_array = explode(".", $email_array[0]);
	for ($i = 0; $i < sizeof($local_array); $i++) {
		if
		(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
		'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
		$local_array[$i])) {
			return false;
		}
	}
	// Check if domain is IP. If not, 
	// it should be valid domain name
	if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
		$domain_array = explode(".", $email_array[1]);
		if (sizeof($domain_array) < 2) {
			return false; // Not enough parts to domain
		}
		for ($i = 0; $i < sizeof($domain_array); $i++) {
			if
			(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
			([A-Za-z0-9]+))$",
			$domain_array[$i])) {
				return false;
			}
		}
	}
	return true;
}
function smartCopy($source, $dest, $folderPermission=0755, $filePermission=0644) {
	$result=false;
	if(is_file($source)) {
		if(is_dir($dest)) {
			if($dest[strlen($dest) - 1]!='/')
				$__dest=$dest."/";
			$__dest .= basename($source);
		} else {
			$__dest=$dest;
		}
		$result=copy($source, $__dest);
		chmod($__dest, $filePermission);
	} elseif(is_dir($source)) {
		if(! is_dir($dest)) {
			@mkdir($dest, $folderPermission);
			chmod($dest, $folderPermission);
		}
		if($source[strlen($source) - 1]!='/')
			$source=$source."/";
		if($dest[strlen($dest) - 1]!='/')
			$dest=$dest."/";
		
		$result=true;
		$dirHandle=opendir($source);
		while(($file=readdir($dirHandle))==true) {
			if($file!="." && $file!="..")
				$result=smartCopy($source.$file, $dest.$file, $folderPermission, $filePermission);
		}
		closedir($dirHandle);
	} else {
		$result=false;
	}
	return $result;
}
function ShowFileExtension($filepath) {
	preg_match('/[^?]*/', $filepath, $matches);
	$string=$matches[0];
	$pattern=preg_split('/\./', $string, - 1, PREG_SPLIT_OFFSET_CAPTURE);
	# check if there is any extension
	if(count($pattern)==1) {
		return 'No File Extension Present';
		exit();
	}
	if(count($pattern) > 1) {
		$filenamepart=$pattern[count($pattern) - 1][0];
		preg_match('/[^?]*/', $filenamepart, $matches);
		return $matches[0];
	}
	return false;
}
?>