<?php
/*
 *
 * sflog!
 * Copyright (C) 2005 Alberto Sfolcini <a.sfolcini@gmail.com>
 *
 * This file is a part of sflog!.
 *
 * sflog! is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * sflog! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */


/**
 * @title			: Utils       
 * @description		: Contains usefull functions 
 * @date			: 20-Oct-2005   
 * @author			: Alberto Sfolcini  <a.sfolcini@gmail.com>
 */


/**
 * redirect to $relative_url given as parameter
 * @param $relative_url string
 */ 
 function redirect($relative_url){
	header ("Location: http://".$HTTP_SERVER_VARS['HTTP_HOST'] .dirname($HTTP_SERVER_VARS['PHP_SELF']) ."/".$relative_url);	
 }
 
/**
 * Remove directory and its files
 * @param $dir string
 * @return boolean
 * 
 * 25.07.2005
 * Author: Anton Makarenko
 * makarenkoa at ukrpost dot net
 * webmaster at eufimb dot edu dot ua
 * Original idea:
 * http://ua2.php.net/manual/en/function.rmdir.php
 * 28-Apr-2005 07:35
 * development at lab-9 dot com
 */
function rm_dir($target,$verbose=false){
	$exceptions=array('.','..');
	if (!$sourcedir=@opendir($target)){
   		if ($verbose)
       		echo '<strong>Couldn&#146;t open '.$target."</strong><br />\n";
   		return false;
   	}
	while(false!==($sibling=readdir($sourcedir))){
   		if(!in_array($sibling,$exceptions)){
       		$object=str_replace('//','/',$target.'/'.$sibling);
       		if($verbose)
           		echo 'Processing: <strong>'.$object."</strong><br />\n";
       		if(is_dir($object))
           		rm_dir($object);
       		if(is_file($object)){
           		$result=@unlink($object);
           		if ($verbose&&$result)
               	echo "File has been removed<br />\n";
           		if ($verbose&&(!$result))
               		echo "<strong>Couldn&#146;t remove file</strong>";
          	}
       	}
   	}
	closedir($sourcedir);
	if($result=@rmdir($target)){
   		if ($verbose)
       		echo "Target directory has been removed<br />\n";
   	return true;
   }
   if ($verbose)
	   echo "<strong>Couldn&#146;t remove target directory</strong>";
  return false;
}

/**
 * stripos
 * 
 */
//function stripos($haystack,$needle,$offset = 0){
//   return(strpos(strtolower($haystack),strtolower($needle),$offset));
//}

/**
 * Get filename extension
 * @param $fileName string
 * @return string
 */
function getFileExt($fileName){
	$ext = strrchr($fileName, '.');
	$ext = substr($ext,-(strlen($ext)-1));
	return $ext; 
}

/**
 * Is an image? return a boolean!
 * @param $fileName string
 */
function is_an_image($fileName){
	$ext = getFileExt($fileName, '.');
	$img_types = array('jpg','jpeg','png','bmp','gif','tif','tiff','wbmp');
	foreach($img_types as $val){
		if (strcasecmp(trim($ext),$val)==0)
			return true;
	}
	return false;
}

/**
 * Show a scaled image
 * 
 * If you want to show thumbnails keeping the original proportions, 
 * with defined maximum width and height, you can use this function. 
 * This is useful when showing tables of user-uploaded images, that 
 * not necessarily are same-sized. However, for big images (like wallpapers),
 * a better option is to create separated thumbnails with a image-editing software.
 * 
 * @param $file file to be scaled and shown
 * @param $url url to fullsize image
 * @param $border image border tag, default is 0
 * @param $comment string
 * @return img html tag
 */
function show_thumbnail($file,$url,$maxSize,$border="0",$comment=""){
 	$max = $maxSize;
	$size = array();
   	$size = getimagesize($file);

   	if ( $size[0] <= $max && $size[1] <= $max ){
    	if (file_exists($url))
    		$ret = '<a target="_new" href="'.$url.'"><img src="'.$file.'" '.$size[3].' border="'.$border.'" alt="'.$comment.'"></a>';
    	else
    		$ret = '<img src="'.$file.'" '.$size[3].' border="'.$border.'" alt="'.$comment.'">';
   	}else{
      	$k = ( $size[0] >= $size[1] ) ? $size[0] / $max : $size[1] / $max;
		if (file_exists($url))
			$ret = '<a target="_new" href="'.$url.'"><img src="'.$file.'" width="'.floor($size[0]/$k).'" height="'.floor($size[1]/$k).'" border="'.$border.'" alt="'.$comment.'"></a>';
		else
			$ret = '<img src="'.$file.'" width="'.floor($size[0]/$k).'" height="'.floor($size[1]/$k).'" border="'.$border.'" alt="'.$comment.'">';
   	}

 	return $ret;
}

 
/**
 * Create Thumbnails
 * @use GDlib2
 * @param $name String Image filename, path included
 * @param $filename String thumbnail filename, path included
 * @param $new_w int width
 * @param $new_h int height 
 */
function createThumb($name,$filename,$new_w,$new_h){
	$system=getFileExt($name);
	if (preg_match('/jpg|jpeg/',$system)){
		$src_img=imagecreatefromjpeg($name);
	}
	if (preg_match('/png/',$system)){
		$src_img=imagecreatefrompng($name);
	}
		
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	if ($old_x > $old_y) {
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y) {
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y) {
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
		
	if (preg_match("/png/",$system))
	{
		imagepng($dst_img,$filename); 
	} else {
		imagejpeg($dst_img,$filename); 
	}
	imagedestroy($dst_img); 
	imagedestroy($src_img); 
}  
 
 ?>