Location: PHPKode > projects > ChiCoMaS - Chilek CMS > chicomas/functions.php
<?
defined( '_VALID_DA_' ) or die( 'Direct Access to this location is not allowed.' );

//*****************************************************************************
/* These functions are used to keep track of generation time */
//*****************************************************************************
function t_getmicrotime(){
	list($usec, $sec) = explode(" ",microtime());
	return ((float)$usec + (float)$sec);
}

//*****************************************************************************
function t_pagetime($type){
	static $orig_time;
	if($type=="init")$orig_time=t_getmicrotime();
	if($type=="print")return sprintf("%2.4f",t_getmicrotime()-$orig_time);
}

//*****************************************************************************
function PrintSessionVar(){
	print_r($_SESSION);
	echo "<br>";
}

//*****************************************************************************
function Message($messageid){
	echo "Mid:$messageid";
}

//*****************************************************************************
function ReDirection($url){
	echo "<center><a href=\"$url\">"._CMN_REDIRECTION_."</a><br><br></center>";
	echo "<meta http-equiv='Refresh' content='1; url=$url'>";
	exit();
}

//*****************************************************************************
function IsLogin(){
	$login_ok = false;
	if (session_is_registered(signed_in)){
		if ($_SESSION['signed_in']=="indeed")
			$login_ok = true;
	}
	return $login_ok;
}

//*****************************************************************************
function SetLanguage() {
	global $defaultlanguage;

	$obj_language = new CLanguage();
	$obj_languagearray = new CLanguageArray();
	$obj_languageengine = new CLanguageEngine();

	if (!$_REQUEST['lang']){
		// No change request of language
		if (!session_is_registered("lang")){
			//No Registered
			$lang = $defaultlanguage;
			session_register("lang");
			$_SESSION['lang'] = $lang;
		}
		else{
			//Registered
		}
	}
	else{
		//Change request of language
		$lang = $_REQUEST['lang'];
		$obj_language = $obj_languageengine->GetLanguage($lang);
		if ($obj_language!=null){
			if (session_is_registered("lang")){
				$_SESSION['lang'] = $lang;
			}
			else{
				if ($lang =="")
					$lang = $defaultlanguage;
				session_register("lang");
			}
		}
	}

	$lang = $_SESSION['lang'];

	switch (strtolower($lang)){
	default:
	case "tr":
		$charset = "iso-8859-9";
	break;
	case "en":
		$charset = "iso-8859-1";
	break;
	case "de":
		$charset = "iso-8859-1";
	break;
	}

	if (session_is_registered("charset")){
		$_SESSION['charset'] = $charset;
	}
	else{
		if ($charset =="")
			$charset = "iso-8859-9";
		session_register("charset");
	}

	//Include Language File
	include("languages/".strtolower($_SESSION['lang'])."/language.php");
}


//*****************************************************************************
function ActiveLanguage() {
	$lang = $_SESSION['lang'];
	return $lang;
}

//*****************************************************************************
function SetMode () {

	if (!$_REQUEST['mode']){
		// No change request of mode
		if (!session_is_registered("mode")){
			//No Registered
			$mode = "user";
			session_register("mode");
			$_SESSION['mode'] = $mode;
		}
		else{
			//Registered
		}
	}
	else{
		//Change request of mode
		$mode = $_REQUEST['mode'];
		if (session_is_registered("mode")){
			$_SESSION['mode'] = $mode;
		}
		else{
			if ($mode =="")
				$mode = "user";
			session_register("mode");
		}
	}

	$mode = $_SESSION['mode'];

}

//*****************************************************************************
/* The following function transforms the IP number of the 
visitor to a decimal number */
//*****************************************************************************
function f_ip2dec($a){
$d = 0.0;
$b = explode(".", $a,4);
for ($i = 0; $i < 4; $i++) {
       $d *= 256.0;
       $d += $b[$i];
   };
return $d;
}

//*****************************************************************************
function go_back(){
	return "
	<!--form-->
	<input type=button value=\""._CMN_BACK_."\" onClick=\"history.go(-1)\">
	<!--/form-->";
}

//*****************************************************************************
function get_datetime(){
	$now = mktime();
	$today = date("Y-m-j, H:i:s", $now); //01/2/2006, 13:08
	return $today;
}

//*****************************************************************************
function list_directory($dir)
{
	if(!($dp = opendir($dir))) die("Cannot open $dir.");
	while($file = readdir($dp)) {
		if(is_file($file)) {
			continue;
		}
		else
			if($file != '.' && $file != '..' && $file != 'CVS'){
				echo '<li><a href="../'. $file .'/">' . $file . '/</a></li>' . "\r\n\t";
		}
	}
	closedir($dp);
}

//*****************************************************************************
//These functions can be used on Turkish(iso-8859-9):
//Turkce(iso-8859-9) icin su fonksiyonlar kullanilabilir:
//*****************************************************************************
if(!function_exists('str_split')){
   function str_split($string,$split_length=1){
       $count = strlen($string); 
       if($split_length < 1){
           return false; 
       } elseif($split_length > $count){
           return array($string);
       } else {
           $num = (int)ceil($count/$split_length); 
           $ret = array(); 
           for($i=0;$i<$num;$i++){ 
               $ret[] = substr($string,$i*$split_length,$split_length); 
           } 
           return $ret;
       }
   }
}

$tr_low_letters = str_split("abcçdefgðhýijklmnoöpqrsþtuüvwxyz");
$tr_up_letters = str_split("ABCÇDEFGÐHIÝJKLMNOÖPQRSÞTUÜVWXYZ");

$en_low_letters = str_split("abccdefgghiijklmnoopqrsstuuvwxyz");
$en_up_letters = str_split("ABCCDEFGGHIIJKLMNOOPQRSSTUUVWXYZ");

function tr_uppercase($str) {
   global $tr_low_letters, $tr_up_letters;
   return str_replace($tr_low_letters, $tr_up_letters, $str);
}

function tr_lowercase($str) {
   global $tr_low_letters, $tr_up_letters;
   return str_replace($tr_up_letters, $tr_low_letters, $str);
}

function tr2en_lowercase($str) {
   global $tr_low_letters, $tr_up_letters;
   global $en_low_letters, $en_up_letters;
   $str = str_replace($tr_low_letters, $en_low_letters, $str);
   return str_replace($tr_up_letters, $en_low_letters, $str);
}

function tr2en_uppercase($str) {
   global $tr_low_letters, $tr_up_letters;
   global $en_low_letters, $en_up_letters;
   $str = str_replace($tr_low_letters, $en_up_letters, $str);
   return str_replace($tr_up_letters, $en_up_letters, $str);
}

function tr_fuppercase($str) {//only first letter uppercase
   return tr_uppercase($str[0]) . tr_lowercase(substr($str,1));
}
//*****************************************************************************

//*****************************************************************************
function valid_email($email) {
    if(ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $email)) {
        return 1;
    }
	else {
        return 0;
	}
}

//*****************************************************************************
function random_number_generator($max_number){
	mt_srand((double)microtime() * 1000000);
	$randnumber = mt_rand(1, $max_number);
	return $randnumber;
}

//*****************************************************************************
// html_entity_decode
// For users prior to PHP 4.3.0 you may do this:
function unhtmlentities ($string){
    $trans_tbl = get_html_translation_table (HTML_ENTITIES);
    $trans_tbl = array_flip ($trans_tbl);
    return strtr ($string, $trans_tbl);
}

//*****************************************************************************

function drop_cap ($string){

	$first_char = $string[0];
	$string = strstr($string, $first_char);
	$string ="<span class=\"dropcap\">".strtoupper($first_char)."</span>".$string;
	return $string;
}

//*****************************************************************************
// GLOBALS
function global_get($var){
	if(!array_key_exists($var,$_GET))
		$_GET[$var]='';
	$GLOBALS[$var]=&$_GET[$var];
}

function global_post($var){
	if(!array_key_exists($var,$_POST))
		$_POST[$var]='';
	$GLOBALS[$var]=&$_POST[$var];
}

function global_session($var){
	if(!array_key_exists($var,$_SESSION))
		$_SESSION[$var]='';
	$GLOBALS[$var]=&$_SESSION[$var];
}

function global_cookie($var){
	if(!array_key_exists($var,$_COOKIE))
		$_COOKIE[$var]='';
	$GLOBALS[$var]=&$_COOKIE[$var];
}

function global_server($var){
	if(!array_key_exists($var,$_SERVER))
		$_SERVER[$var]='';
	$GLOBALS[$var]=&$_SERVER[$var];
}

function global_files($var){
	if(!array_key_exists($var,$_FILES))
		$_FILES[$var]='';
	$GLOBALS[$var]=&$_FILES[$var];
}

function global_env($var){
	if(!array_key_exists($var,$_ENV))
		$_ENV[$var]='';
	$GLOBALS[$var]=&$_ENV[$var];
}

function global_request($var){
	if(!array_key_exists($var,$_REQUEST))
		$_REQUEST[$var]='';
	$GLOBALS[$var]=&$_REQUEST[$var];
}
//*****************************************************************************

function getFilename($file, $extension = true){
	return ($extension || false === $dot = strrpos( basename( $file ), '.' )) ? basename( $file ) : substr( basename( $file ), 0, $dot );
}
//echo getFilename( 'foo.php' ); // foo.php
//echo getFilename( 'foo.php', false ); // foo
//echo getFilename( 'dir/foo.php' ); // foo.php
//echo getFilename( 'foo' ); // foo - note that $extension is true here
//echo getFilename( '.php', false ); // '' - empty string

//*****************************************************************************
function secureFilename ($filename){ //File or Directory

	$filename = tr2en_lowercase($filename);
	$filename_parts = pathinfo($filename);
	//echo "dir:".$filename_parts['dirname'], "<br>";
	//echo "basename:".$filename_parts['basename'], "<br>";
	//echo "ext:".$filename_parts['extension'], "<br>";

	if (($filename_parts['dirname']==".") || ($filename_parts['dirname']=="./"))
		$directory = "";
	else
		$directory = $filename_parts['dirname'];

	if (
			($filename_parts['extension']=="php") || ($filename_parts['extension']=="php3") ||
			($filename_parts['extension']=="js") || ($filename_parts['extension']=="asp")
		)
		$filename = $directory."/".getFilename($filename_parts['basename'], true).".txt";

	return $filename;
}

/********************************************************************************/
function random_hex_color(){
	return sprintf("%02X%02X%02X", mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
} 

//$hex = random_hex_color();
//echo '<span style="background: #'.$hex.'">Hello World</span>';

/********************************************************************************/
function isEven($num) {
	return ($num % 2 == 0);
}

//$n=40;
//if(isEven($n)) { echo "n is even"; } else { echo "n is not even"; }

/********************************************************************************/
function round_down($value){
	list($val,$dummy) = explode(".",$value);
	return $val;
}
//$n=1.7;
//echo round_down($n);

function round_up($value){
	list($val,$dummy) = explode(".",$value);
	return $dummy?$val+1:$val;
}

//$n=1.2;
//echo round_up($n);

/********************************************************************************/
function is_url($url) {
	return preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url);
}

//echo is_url("http://www.google.com/");

/********************************************************************************/

function EncryptText($strText,$strPwd) {
	$i=0;$C=0;
	$strBuff='';

	if(strlen($strPwd)) {
		for($i=0;$i<strlen($strText);$i++) {
			$C=ord(substr($strText,$i, 1));
			$C+=ord(substr($strPwd,($i % strlen($strPwd)) + 1, 1));
			$strBuff = $strBuff.chr(EncryptABS($C));
		}
	} else {
		$strBuff = $strText;
	}
	return $strBuff;
}

//$s = EncryptText("hello world", "1234");
//echo $s;


function DecryptText($strText, $strPwd) {
	$i=0;$C=0;
	$strBuff='';

	if(strlen($strPwd)) {
		for($i=0;$i<strlen($strText);$i++) {
			$C=ord(substr($strText,$i, 1));
			$C-=ord(substr($strPwd,($i % strlen($strPwd)) + 1, 1));
			$strBuff = $strBuff.chr(EncryptABS($C));
		}
	} else {
		$strBuff = $strText;
	}
	return $strBuff;
}

//$s = EncryptText("hello world", "1234");
//echo DecryptText($s, "1234");
/********************************************************************************/

function EncryptABS($c) {
	if($c>255) $c-=255;
	if($c<0) $c+=255;
	return $c;
}

//echo EncryptABS(312);

/********************************************************************************/

//$path_parts = pathinfo('/www/htdocs/index.html');

//echo $path_parts['dirname'], "\n";
//echo $path_parts['basename'], "\n";
//echo $path_parts['extension'], "\n";

?>
Return current item: ChiCoMaS - Chilek CMS