Location: PHPKode > scripts > Encaps Gallery > encaps-2.3.22s/core/misc.class.php
<?php 
class Misc
{
	function _nl2br($message)
	{
		$message = str_replace("\n", "<br>", "$message");
		$message = str_replace("\r", "", "$message");
		$message = str_replace("'", "&apos;", "$message");
		$message = str_replace("\"", "&quot;", "$message");
		$message = trim($message);
		return $message;
	}
	
	function _br2nl($message)
	{
		$message = preg_replace("=<br(>|([\s/][^>]*)>)\r?\n?=i", "\n", $message);
		$message = str_replace("&quot;", "\"", "$message");
		$message = str_replace("&apos;", "'", "$message");
		return $message;
	}

	function htmlspecialchars_decode($_message)
	{
		$_message = str_replace("&quot;", "\"", "$_message");
		$_message = str_replace("&apos;", "'", "$_message");
		return  $_message;
	}
	
	function _safe_db_text($message)
	{
		$message = str_replace("'", "&#39;", "$message");
		$message = trim($message);
		return $message;
	}

	function _safe_js_text($message)
	{
		$message = str_replace("\n", "", "$message");
		$message = str_replace("\r", "", "$message");
		$message = str_replace("'", "&#39;", "$message");
		$message = str_replace("\"", "&quot;", "$message");
		$message = trim($message);
		return $message;
	}	
	
	function pagination($items_total=0,$items_per_page=0,$page_current=0)
	{
		$html = array("page"=>0,"offset"=>0,"pages"=>0);
		if($items_total==0)
			return $html;
		if($items_per_page==0)
			$items_per_page = $items_total;
		if($page_current==0)
			$page_current = 1;
			
		$html["page"] = $page_current;
		$html["offset"] = ($page_current - 1) * $items_per_page;
		$html["pages"] = (int)($items_total / $items_per_page);  
		if($items_total % $items_per_page)
			$html["pages"]++;
		return $html;
	}
	
	function _generateRandom($length=6)
	{
		$_rand_src[0][0]=48;
		$_rand_src[0][1]=57;
		$_rand_src[1][0]=65;
		$_rand_src[1][1]=90;
		$_rand_src[2][0]=97;
		$_rand_src[2][1]=122;
		srand ((double) microtime() * 1000000);
		for($i=0;$i<$length;$i++){
			$i1=rand(0,2);
			$pass.=chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));
		}
		return $pass;
	}
	
	function file_upload($path,$_htpf)
	{
		if(!$_htpf['name'])
			return;		
		$filename = func_translit($_htpf['name']);
		if(!preg_match("#\.(jpg|jpeg|png|gif|swf|mov|pano)$#i",$filename))
		{
			Misc::_show_message_color("Only the following files types are allowed for upload: <b>.jpg .jpeg .png .gif .swf .mov .pano</b>","RED");
			return;
		}
		if(file_exists($path.$filename))
			$filename = preg_replace("#(\.\S+)$#","_".time()."$1",$filename);			
		if(!is_writable($path))
		{
			Misc::_show_message_color("Directory $path is not writable","RED");
		}		
		switch ($_htpf['error'])
		{
			case (0):
				if(!copy($_htpf['tmp_name'],$path.$filename))
				{
					Misc::_show_message_color("Warning: File upload failed - please check rwx_gallery and rwx_gallery/thumbs permissions","RED");
				}
				else
				{
					Misc::_show_message_color("Uploaded: ".$path.$filename,"GREEN");
					chmod($path.$filename,0666);
					return $filename;
				}				
				break;
			case (1):
				Misc::_show_message_color('The uploaded file exceeds the upload_max_filesize directive from php.ini','RED');
				break;
			case (2):
				Misc::_show_message_color('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form','RED');
				break;
			case (3):
				Misc::_show_message_color('The uploaded file was only partially uploaded','RED');
				break;
			case (4):
				Misc::_show_message_color('No file was upload','RED');
				break;
			case (6):
				Misc::_show_message_color('Missing a temporary folder (in php.ini)','RED');
				break;
			case (7):
				Misc::_show_message_color('Failed to write file to disk','RED');
				break;
			case (8):
				Misc::_show_message_color('File upload stopped by extension','RED');
				break;
		}
		return "";
	}

	function file_unlink($path,$_filename)
	{
		if( $_filename != "" && file_exists($path.$_filename))
		{
			if(unlink($path.$_filename) && ! file_exists($path.$_filename))
			{
				Misc::_show_message_color("Deleted: ".$path.$_filename);
				return true;
			}
			else
			{
				Misc::_show_message_color("Skip to delete the file: ".$path.$_filename.", please check permissions","RED");
				return false;
			}		
		}
		return true;
	}

	function rndpwd ($pwd, $data, $case='') 
	{
		if ($case == 'DE') {
			$data = urldecode($data);
		}
		$key[] = '';
		$box[] = '';
		$temp_swap = '';
		$pwd_length = 0;

		$pwd_length = strlen($pwd);
		for ($i = 0; $i <= 255; $i++) {
			$key[$i] = ord(substr($pwd, ($i % $pwd_length), 1));
			$box[$i] = $i;
		}
		$x = 0;
		for ($i = 0; $i <= 255; $i++) {
			$x = ($x + $box[$i] + $key[$i]) % 256;
			$temp_swap = $box[$i];
			$box[$i] = $box[$x];
			$box[$x] = $temp_swap;
		}

		$temp = '';
		$k = '';

		$cipherby = '';
		$cipher = '';

		$a = 0;
		$j = 0;

		for ($i = 0; $i < strlen($data); $i++) {
			$a = ($a + 1) % 256;
			$j = ($j + $box[$a]) % 256;
        		$temp = $box[$a];
			$box[$a] = $box[$j];

			$box[$j] = $temp;

			$k = $box[(($box[$a] + $box[$j]) % 256)];
			$cipherby = ord(substr($data, $i, 1)) ^ $k;

			$cipher .= chr($cipherby);
		}

		if ($case == 'DE') {
			$cipher = urldecode(urlencode($cipher));
		} else {
			$cipher = urlencode($cipher);
		}
			return $cipher;
	}

	function resize($_src_dir_file,$_dst_dir_file,$_thumb_size,$_thumb_type="")
	{
		$filename = $_src_dir_file;
		$size = getimagesize($_src_dir_file);
		$src_w = $size[0];
		$src_h = $size[1];

		if( !file_exists($_src_dir_file) || !preg_match("#(\.png|\.gif|\.jpg|\.jpeg)$#i",$_src_dir_file) )
		{
			Misc::_show_message_color("Resize failed for [$_src_dir_file] - it's not .png/.gif/.jpg/.jpeg","YELLOW");
			return false;
		}
		/*if (!is_writable($_dst_dir))
		{
			Misc::_show_message_color("Directory $_dst_dir is not writable","RED");
			return false;
		}*/
		if((int)$_thumb_size==0 || ($_thumb_size>=$src_w && $_thumb_size>=$src_h))
		{
			if($_src_dir_file != $_dst_dir_file)
			if(copy($_src_dir_file, $_dst_dir_file))
			{
				chmod($_dst_dir_file,0666);
				Misc::_show_message_color("File is copied from $_src_dir_file to $_dst_dir_file","");
				return true;
			}else{
				Misc::_show_message_color("Can't copy $_src_dir_file to $_dst_dir_file","RED");
				return false;
			}
		}
	
		//-----------------------------------------
		if($_thumb_type == "h")
		{
			if( $src_h <= $_thumb_size)
				return;

			$dst_w = $src_w/($src_h/$_thumb_size);
			$dst_h = $_thumb_size;

			$src_x = 0;
			$src_y = 0;
		}
		//-----------------------------------------
		else if($_thumb_type == "h_not_w")
		{
			if($src_h <= $_thumb_size)
				return;
			$src_x = 0;
			$src_y = 0;
			$dst_w = $src_w;
			$dst_h = $src_h = $_thumb_size;
		}
		//------------------------------------------
		else if($_thumb_type == "w")
		{
			if($src_w <= $_thumb_size )
				return;
			{
				$dst_h = $src_h/($src_w/$_thumb_size);
				$dst_w = $_thumb_size;
			}
				$src_x = 0;
				$src_y = 0;
		}
		//------------------------------------------
		else if($_thumb_type == "w_and_h")
		{
			if($src_w <= $_thumb_size && $src_h <= $_thumb_size)
				return;
			if($src_w > $src_h)
			{
				$src_x = ($src_w-$src_h)/2;
				$src_y = 0;
				$src_w = $src_w - $src_x*2;
			}
			else
			{
				$src_y = ($src_h-$src_w)/2;
				$src_x = 0;
				$src_h = $src_h - $src_y*2;
			}
			$dst_w = $_thumb_size;
			$dst_h = $_thumb_size;
		}
		//-------------------------------------------
		else
		{
			if($src_w <= $_thumb_size && $src_h <= $_thumb_size)
				return;
			if($src_w > $src_h)
			{
				$dst_w = $_thumb_size;
				$dst_h = $src_h/($src_w/$_thumb_size);
			}
			else
			{
				$dst_w = $src_w/($src_h/$_thumb_size);
				$dst_h = $_thumb_size;
			}
				$src_x = 0;
				$src_y = 0;
		}
		
		$dst_x = 0;
		$dst_y = 0;		
	
		if(preg_match("#(\.png)$#i",$_src_dir_file)&& function_exists('imagepng'))
		{
			$dst_image = imagecreate($dst_w,$dst_h);  
			$src_image = imagecreatefrompng($_src_dir_file);
			imagecopyresized($dst_image, $src_image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
			imagepng($dst_image,$_dst_dir_file);
			chmod($_dst_dir_file,0666);
			ImageDestroy($src_image);
		}
		else if(preg_match("#(\.jpe*g)$#i",$_src_dir_file) && function_exists('imagejpeg'))
		{
			$src_image = imagecreatefromjpeg($_src_dir_file);                
			$dst_image = imagecreatetruecolor($dst_w,$dst_h);  
		    ImageCopyResampled($dst_image, $src_image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
			imagejpeg($dst_image,$_dst_dir_file,100);
			chmod($_dst_dir_file,0666);
		    ImageDestroy($src_image);  				
		}
		else if(preg_match("#(\.gif)$#i",$_src_dir_file) && function_exists('imagegif'))
		{
			$dst_image = imagecreate($dst_w,$dst_h);  
			$src_image = imagecreatefromgif($_src_dir_file);
			imagecopyresized($dst_image, $src_image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
			imagegif($dst_image,$_dst_dir_file);
			chmod($_dst_dir_file,0666);
			ImageDestroy($src_image);  	
		}
		else
		{
			Misc::_show_message_color("Resize failed, can't access GD library","YELLOW");
			return false;
		}
			
		Misc::_show_message_color("Resized: ".$_dst_dir_file,"GREEN");
		return true;
	}

	function _show_message_color($_message,$_color="")
	{
		$bgcolor_table = "";
		$bgcolor_td = "";
		switch ($_color) 
		{
			case "RED": $bgcolor_table = "#FF0000"; $bgcolor_td = "#FFEEEE"; break;
			case "YELLOW": $bgcolor_table = "#FFF000"; $bgcolor_td = "#FFFEEE"; break;
			case "GREEN": $bgcolor_table = "#00FF00"; $bgcolor_td = "#EEFFEE"; break;
			case "BLACK": $bgcolor_table = "#000000"; $bgcolor_td = "#FFFFFF"; break;
			default: $bgcolor_table = "#000000"; $bgcolor_td = "#FFFFFF"; break;
		}
		?><table bgcolor="<?php echo $bgcolor_table?>" width="400" cellspacing="1" align="center"><tr><td bgcolor="<?php echo $bgcolor_td?>" align="left"><?php echo $_message?></td></tr></table><?php 
	}
	
	function safetext($text) 
	{
        $text = preg_replace("/[^a-z0-9_-]/i", "", $text);
        return $text;
    }

	function _move_file($_src_dir,$_dst_dir,$_filename)
	{
		if(file_exists($_src_dir.$_filename) 
			&& copy($_src_dir.$_filename,$_dst_dir.$_filename)
			&& unlink($_src_dir.$_filename))
		{		
			Misc::_show_message_color("File ".$_src_dir."/".$_filename." is moved to ".$_dst_dir."/".$_filename,"GREEN");
			return true;
		}
		else
		{
			Misc::_show_message_color("File ".$_src_dir."/".$_filename." isn't moved to ".$_dst_dir."/".$_filename.", please check the file permissions","RED");
			return false;
		}
	}	
		
}
?>
Return current item: Encaps Gallery