/plugins/gallery/plugin.gallery.upload.php
PHP | 377 lines | 274 code | 58 blank | 45 comment | 48 complexity | d131c242ea54965fa6230513695885ee MD5 | raw file
- <?php
- require_once('plugin.gallery.functions.php');
- ###############################################################
- # Thumbnail Image Class for Thumbnail Generator
- ###############################################################
- # For updates visit http://www.zubrag.com/scripts/
- ###############################################################
- class Zubrag_image {
- var $save_to_file = true;
- var $image_type = -1;
- var $quality = 100;
- var $max_x = 100;
- var $max_y = 100;
- var $cut_x = 0;
- var $cut_y = 0;
-
- function SaveImage($im, $filename) {
- $res = null;
-
- // ImageGIF is not included into some GD2 releases, so it might not work
- // output png if gifs are not supported
- if(($this->image_type == 1) && !function_exists('imagegif')) $this->image_type = 3;
- switch ($this->image_type) {
- case 1:
- if ($this->save_to_file) {
- $res = ImageGIF($im,$filename);
- }
- else {
- header("Content-type: image/gif");
- $res = ImageGIF($im);
- }
- break;
- case 2:
- if ($this->save_to_file) {
- $res = ImageJPEG($im,$filename,$this->quality);
- }
- else {
- header("Content-type: image/jpeg");
- $res = ImageJPEG($im, NULL, $this->quality);
- }
- break;
- case 3:
- if (PHP_VERSION >= '5.1.2') {
- // Convert to PNG quality.
- // PNG quality: 0 (best quality, bigger file) to 9 (worst quality, smaller file)
- $quality = 9 - min( round($this->quality / 10), 9 );
- if ($this->save_to_file) {
- $res = ImagePNG($im, $filename, $quality);
- }
- else {
- header("Content-type: image/png");
- $res = ImagePNG($im, NULL, $quality);
- }
- }
- else {
- if ($this->save_to_file) {
- $res = ImagePNG($im, $filename);
- }
- else {
- header("Content-type: image/png");
- $res = ImagePNG($im);
- }
- }
- break;
- }
- return $res; //GIBT DAS BILD AUS - hier sicherung des bildes einbauen! (und die header raus nehmen)
- }
-
- function ImageCreateFromType($type,$filename) {
- $im = null;
- switch ($type) {
- case 1:
- $im = ImageCreateFromGif($filename);
- break;
- case 2:
- $im = ImageCreateFromJpeg($filename);
- break;
- case 3:
- $im = ImageCreateFromPNG($filename);
- break;
- }
- return $im;
- }
-
- // generate thumb from image and save it
- function GenerateThumbFile($from_name, $to_name) {
-
- // if src is URL then download file first
- $temp = false;
- if (substr($from_name,0,7) == 'http://') {
- $tmpfname = tempnam("tmp/", "TmP-");
- $temp = @fopen($tmpfname, "w");
- if ($temp) {
- @fwrite($temp, @file_get_contents($from_name)) or die("Cannot download image");
- @fclose($temp);
- $from_name = $tmpfname;
- }
- else {
- die("Cannot create temp file");
- }
- }
- // check if file exists
- if (!file_exists($from_name)) die("Source image does not exist!");
-
- // get source image size (width/height/type)
- // orig_img_type 1 = GIF, 2 = JPG, 3 = PNG
- list($orig_x, $orig_y, $orig_img_type, $img_sizes) = @GetImageSize($from_name);
- // cut image if specified by user
- if ($this->cut_x > 0) $orig_x = min($this->cut_x, $orig_x);
- if ($this->cut_y > 0) $orig_y = min($this->cut_y, $orig_y);
-
- // should we override thumb image type?
- $this->image_type = ($this->image_type != -1 ? $this->image_type : $orig_img_type);
-
- // check for allowed image types
- if ($orig_img_type < 1 or $orig_img_type > 3) die("Image type not supported");
-
- if ($orig_x > $this->max_x or $orig_y > $this->max_y) {
-
- // resize
- $per_x = $orig_x / $this->max_x;
- $per_y = $orig_y / $this->max_y;
- if ($per_y > $per_x) {
- $this->max_x = $orig_x / $per_y;
- }
- else {
- $this->max_y = $orig_y / $per_x;
- }
-
- }
- else {
- // keep original sizes, i.e. just copy
- if ($this->save_to_file) {
- @copy($from_name, $to_name);
- }
- else {
- switch ($this->image_type) {
- case 1:
- header("Content-type: image/gif");
- readfile($from_name);
- break;
- case 2:
- header("Content-type: image/jpeg");
- readfile($from_name);
- break;
- case 3:
- header("Content-type: image/png");
- readfile($from_name);
- break;
- }
- }
- return;
- }
-
- if ($this->image_type == 1) {
- // should use this function for gifs (gifs are palette images)
- $ni = imagecreate($this->max_x, $this->max_y);
- }
- else {
- // Create a new true color image
- $ni = ImageCreateTrueColor($this->max_x,$this->max_y);
- }
-
- // Fill image with white background (255,255,255)
- $white = imagecolorallocate($ni, 255, 255, 255);
- imagefilledrectangle( $ni, 0, 0, $this->max_x, $this->max_y, $white);
- // Create a new image from source file
- $im = $this->ImageCreateFromType($orig_img_type,$from_name);
- // Copy the palette from one image to another
- imagepalettecopy($ni,$im);
- // Copy and resize part of an image with resampling
- imagecopyresampled(
- $ni, $im, // destination, source
- 0, 0, 0, 0, // dstX, dstY, srcX, srcY
- $this->max_x, $this->max_y, // dstW, dstH
- $orig_x, $orig_y); // srcW, srcH
-
- // save thumb file
- $saved = $this->SaveImage($ni, $to_name);
- if($temp) {
- unlink($tmpfname); // this removes the file
- }
- }
- }
-
- class qqUploadedFileXhr {
- /**
- * Save the file to the specified path
- * @return boolean TRUE on success
- */
- function save($path) {
- $input = fopen("php://input", "r");
- $temp = tmpfile();
- $realSize = stream_copy_to_stream($input, $temp);
- fclose($input);
-
- if ($realSize != $this->getSize()){
- return false;
- }
-
- $target = fopen($path, "w");
- fseek($temp, 0, SEEK_SET);
- stream_copy_to_stream($temp, $target);
- fclose($target);
-
- return true;
- }
- function getName() {
- return $_GET['qqfile'];
- }
- function getSize() {
- if (isset($_SERVER["CONTENT_LENGTH"])){
- return (int)$_SERVER["CONTENT_LENGTH"];
- } else {
- throw new Exception('Getting content length is not supported.');
- }
- }
- }
- /**
- * Handle file uploads via regular form post (uses the $_FILES array)
- */
- class qqUploadedFileForm {
- /**
- * Save the file to the specified path
- * @return boolean TRUE on success
- */
- function save($path) {
- if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
- return false;
- }
- return true;
- }
- function getName() {
- return $_FILES['qqfile']['name'];
- }
- function getSize() {
- return $_FILES['qqfile']['size'];
- }
- }
- class qqFileUploader {
- private $allowedExtensions = array();
- private $sizeLimit = 10485760;
- private $file;
-
- var $db;
- function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
-
- $allowedExtensions = array_map("strtolower", $allowedExtensions);
-
- $this->allowedExtensions = $allowedExtensions;
- $this->sizeLimit = $sizeLimit;
-
- $this->checkServerSettings();
- if (isset($_GET['qqfile'])) {
- $this->file = new qqUploadedFileXhr();
- } elseif (isset($_FILES['qqfile'])) {
- $this->file = new qqUploadedFileForm();
- } else {
- $this->file = false;
- }
- }
-
- private function checkServerSettings(){
- $postSize = $this->toBytes(ini_get('post_max_size'));
- $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
-
- if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
- $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
- die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
- }
- }
-
- private function toBytes($str){
- $val = trim($str);
- $last = strtolower($str[strlen($str)-1]);
- switch($last) {
- case 'g': $val *= 1024;
- case 'm': $val *= 1024;
- case 'k': $val *= 1024;
- }
- return $val;
- }
-
- /**
- * Returns array('success'=>true) or array('error'=>'error message')
- */
-
-
-
- function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
-
- global $db;
- $this->db = &$db;
-
- if (!is_writable($uploadDirectory)){
- return array('error' => "Server error. Upload directory isn't writable.");
- }
-
- if (!$this->file){
- return array('error' => 'No files were uploaded.');
- }
-
- $size = $this->file->getSize();
-
- if ($size == 0) {
- return array('error' => 'File is empty');
- }
-
- if ($size > $this->sizeLimit) {
- return array('error' => 'File is too large');
- }
-
- $pathinfo = pathinfo($this->file->getName());
- $filename = $pathinfo['filename'];
- $filename = md5(uniqid());
- $ext = $pathinfo['extension'];
- if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
- $these = implode(', ', $this->allowedExtensions);
- return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
- }
-
- if(!$replaceOldFile){
- /// don't overwrite previous files that were uploaded
- while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
- $filename .= rand(10, 99);
- }
- }
-
- if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
- $path = $filename . '.' . $ext;
-
- //print_r($_GET);
-
- make_thumb($uploadDirectory.$path, $uploadDirectory.'thumb_'.$path, 150);
-
- updateGalleryDB($_GET['gallery_id'], $path, 'thumb_'.$path);
-
- return array('success'=>true);
- } else {
- return array('error'=> 'Could not save uploaded file.' .
- 'The upload was cancelled, or server error encountered');
- }
-
- }
- }
- // list of valid extensions, ex. array("jpeg", "xml", "bmp")
- $allowedExtensions = array();
- // max file size in bytes
- $sizeLimit = 10 * 1024 * 1024;
- //print_r($_POST);
- $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
- $result = $uploader->handleUpload('./../../media/images/');
- // to pass data through iframe you will need to encode all html tags
- echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
- ?>