gzuncompress NineSec Team Shell
NineSec Team Shell
Server IP : 172.19.0.3  /  Your IP : 216.73.216.178
Web Server : Apache/2.4
System : Linux 880f91b28fd7 5.15.0-117-generic #127~20.04.1-Ubuntu SMP Thu Jul 11 15:36:12 UTC 2024 x86_64
User : tomlinde ( 155017)
PHP Version : 5.6.40
Disable Function : dl, syslog, opcache_get_status
MySQL : ON  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0700) :  /home/webpages/lima-city/tomlinde/html/galleryHH/

[  Home  ][  C0mmand  ][  Upload File  ][  Lock Shell  ][  Logout  ]

Current File : /home/webpages/lima-city/tomlinde/html/galleryHH/cachedImage.php
<?php
require_once 'internals/globalConstants.php';

/*requires 
sudo apt-get install php5-gd
a constant IMAGE_CACHE_PATH
a constant IMAGE_ORIGINAL_PATH
*/
class cacheImage {
	private $size;
	private $path;
	private $name;
	private $maxX;
	private $maxY;

	private function setSize($size){
		if($size == ""){
			$this->size = "original";
			return;
		}
		
		$x = stripos($size,"x");
		if($x <= 0)
			throw new Exception("Invalide size parameter (wrong X)");
		
		$firstNum = substr($size,0,$x);
		$secondNum = substr($size, $x+1);
		
		if(!is_numeric($firstNum) OR !is_numeric($secondNum))
			throw new Exception("Invalide size parameter (wrong number)");
		
		if(!file_exists(IMAGE_CACHE_PATH.$size) OR !is_dir(IMAGE_CACHE_PATH.$size))
			throw new Exception("Invalide size parameter (cache directory doesn't exist)");
		
		//its ok
		$this->size = $size;
		$this->maxX = $firstNum;
		$this->maxY = $secondNum;
	}
	private function setPath($path){
		if(stripos($path, "..") !== false)
			throw new Exception("Invalide path parameter (contains ..)");
		
		if(substr($path, strlen($path)-1) != "/")
			$path = $path."/";
		
		$this->path = $path;
	}
	private function setName($name){
		if(stripos($name, "..") == 0 AND stripos($name, "..") === true)
			throw new Exception("Invalide name parameter (contains .. at the beginning)");
		
		if(stripos($name, "/") !== false)
			throw new Exception("Invalide name parameter (contains /)");
		
		$this->name = $name;
	}
	private function imageExistsInCache(){
		return file_exists(IMAGE_CACHE_PATH.$this->size."/".$this->path.$this->name);
	}
	
	private function readImageFromPath($imagePath){
		$nameArray = explode('.', $this->name);
		$ext = strtolower(end($nameArray));
		switch($ext){
			case 'jpg':
			case 'jpeg': $img = imagecreatefromjpeg($imagePath); break;
			case 'png': $img = imagecreatefrompng($imagePath);break;
			case 'gif': $img = imagecreatefromgif($imagePath);break;
			default: throw new Exception("Invalide Image-Type");
		}
		
		if(!$img)
			throw new Exception("could not read the Image (".$imagePath.")");
		
		//Applying exif orientation rotation...
		
		if (extension_loaded('exif') && function_exists('exif_read_data') && $ext == "jpg" && true)
			$img = $this->applyExifOrientation($imagePath, $img);
		
		return $img;
	}
	
	private function applyExifOrientation($imagePath, $img){
		//FIXME: turning off all error messages is bad, but sadly it works :-/
		// http://drupal.org/node/556970
		// if we dont disable the errors, everytime someone want to view an image
		// which is not in the cache, will not be displayed (but written to the cache)
		// another idea: http://stackoverflow.com/questions/8106683/exif-read-data-incorrect-app1-exif-identifier-code
		
		/* sadly it doesn't work right...
		getimagesize($imagePath, $info);
		echo "imageinfo: ".$info['APP1'];
		var_dump($info);
		if($info['APP1'] != 'Exif')
			return $img;*/
		
		$exif = @exif_read_data($imagePath); 
		if ($exif !== false){
			if(array_key_exists("Orientation", $exif)){
				/*
				The comments in the switch are from the following bash command (the bash command isn't pretty but it works...)
				$ for i in {1..8}; do exiftool -m -n -Orientation=$i 20111014_007.jpg|grep รค && rm *.jpg_original; exiftool -Orientation -n 20111014_007.jpg|sed 's/[ ]\+/ /g'; exiftool -Orientation 20111014_007.jpg|sed 's/[ ]\+/ /g'|cut -d " " -f3-;done
				*/
				//imagerotate rotates CCW
				switch($exif["Orientation"]){
					//Orientation : 1
					//Horizontal (normal)
					case 1: break;
					
					//Orientation : 2
					//Mirror horizontal
					case 2: $img = $this->imageflip($img, true); break;
					
					//Orientation : 3
					//Rotate 180
					case 3: $img = imagerotate($img, 180, 0); break;
					
					//Orientation : 4
					//Mirror vertical
					case 4: $img = $this->imageflip($img, false, true); break;
					
					//Orientation : 5
					//Mirror horizontal and rotate 270 CW
					case 5: $img = $this->imageflip($img, true); $img = imagerotate($img, 90, 0);break;
					
					//Orientation : 6
					//Rotate 90 CW
					case 6: $img = imagerotate($img, 270, 0); break;
					
					//Orientation : 7
					//Mirror horizontal and rotate 90 CW
					case 7: $img = $this->imageflip($img, true); $img = imagerotate($img, 270, 0); break;
					
					//Orientation : 8
					//Rotate 270 CW
					case 8: $img = imagerotate($img, 90, 0); break;
				}
			}
		}
		return $img;
	}
	
	private function imageflip($img, $x, $y = false){
		$size_x = imagesx($img);
		$size_y = imagesy($img);
		if($x)
			imagecopyresampled($result, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y);
		elseif($y)
			imagecopyresampled($result, $img, 0, 0, 0, ($size_y-1), $size_x, $size_y, $size_x, 0-$size_y);
		elseif($x && $y)
			imagecopyresampled($result, $img, 0, 0, ($size_x-1), ($size_y-1), $size_x, $size_y, 0-$size_x, 0-$size_y);
		else
			$result = $img;
		return $result;
	}
	
	private function createCacheFromImg($img){
		if($this->size == "original"){
			$this->saveImageToCache($img);
			return;
		}
		
		# Get image size and scale ratio
		$width = imagesx($img);
		$height = imagesy($img);
		$scale = min($this->maxX/$width, $this->maxY/$height);
		
		//we also do upscaling (since we care about this at an other point)
		$new_width = floor($scale*$width);
		$new_height = floor($scale*$height);

		# Create a new temporary image
		$new_img = imagecreatetruecolor($new_width, $new_height);

		# Copy and resize old image into new image
		imagecopyresized($new_img, $img, 0, 0, 0, 0,
				$new_width, $new_height, $width, $height);
		
		$this->saveImageToCache($new_img);
	}
	private function saveImageToCache($img){
		$imagePath = IMAGE_CACHE_PATH.$this->size."/".$this->path.$this->name;
		
		if(!file_exists(IMAGE_CACHE_PATH.$this->size."/".$this->path))
			mkdir(IMAGE_CACHE_PATH.$this->size."/".$this->path, 0777, true);
		$nameArray = explode('.', $this->name);
		$ext = strtolower(end($nameArray));
		switch($ext){
			case 'jpg':
			case 'jpeg': imagejpeg($img, $imagePath, 80); break;
			case 'png': imagepng($img, $imagePath, 9); break;
			case 'gif': imagegif($img, $imagePath); break;
			default: throw new Exception("Invalide Image-Type");
		}
	}
	
	public function getImage($inPath, $inName, $inSize){
		$this->setSize($inSize);
		$this->setName($inName);
		$this->setPath($inPath);
		
		if(!$this->imageExistsInCache()){
			$img = $this->readImageFromPath(IMAGE_ORIGINAL_PATH.$this->path.$this->name);
			if($this->maxX < imagesx($img) OR $this->maxY < imagesy($img)){
				$this->createCacheFromImg($img);
				imagedestroy($img);
				$img = $this->readImageFromPath(IMAGE_CACHE_PATH.$this->size."/".$this->path.$this->name);
			}
		} else
			$img = $this->readImageFromPath(IMAGE_CACHE_PATH.$this->size."/".$this->path.$this->name);
		
		return $img;
	}
};
/*$cli = true;
if($cli){
	$inPath = $argv['2'];
	$inName = $argv['3'];
	$inSize = $argv['4'];
} else {*/
	$inPath = $_GET["path"];
	$inName = $_GET["name"];
	if(array_key_exists("size", $_GET)){
		$inSize = $_GET["size"];
	}else{
		$inSize = "";
	}
	
//}

//FIXME: this is the lazy way (maybe a better way would be http://www.php.net/manual/en/function.exif-imagetype.php)...
//find imgtype:
$fileExt = substr($inName, strrpos($inName, '.'));

try{
	$cImage = new cacheImage();
	$img = $cImage->getImage($inPath, $inName, $inSize);
	switch(strtolower($fileExt)){
		case ".jpg":
		case ".jpeg": header("Content-type: image/jpeg"); imagejpeg($img); break;
		case ".png": header("Content-type: image/png"); imagepng($img); break;
		case ".gif": header("Content-type: image/gif"); imagegif($img); break;
	}
	imagedestroy($img);
} catch (Exception $e) {
	echo "something went wrong :-/\n";
}
?>

NineSec Team - 2022