PageRenderTime 24ms CodeModel.GetById 47ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/gallery/plugin.gallery.upload.php

https://github.com/4g3n7sm1th/cms
PHP | 377 lines | 274 code | 58 blank | 45 comment | 48 complexity | d131c242ea54965fa6230513695885ee MD5 | raw file
  1. <?php
  2. require_once('plugin.gallery.functions.php');
  3. ###############################################################
  4. # Thumbnail Image Class for Thumbnail Generator
  5. ###############################################################
  6. # For updates visit http://www.zubrag.com/scripts/
  7. ###############################################################
  8. class Zubrag_image {
  9. var $save_to_file = true;
  10. var $image_type = -1;
  11. var $quality = 100;
  12. var $max_x = 100;
  13. var $max_y = 100;
  14. var $cut_x = 0;
  15. var $cut_y = 0;
  16. function SaveImage($im, $filename) {
  17. $res = null;
  18. // ImageGIF is not included into some GD2 releases, so it might not work
  19. // output png if gifs are not supported
  20. if(($this->image_type == 1) && !function_exists('imagegif')) $this->image_type = 3;
  21. switch ($this->image_type) {
  22. case 1:
  23. if ($this->save_to_file) {
  24. $res = ImageGIF($im,$filename);
  25. }
  26. else {
  27. header("Content-type: image/gif");
  28. $res = ImageGIF($im);
  29. }
  30. break;
  31. case 2:
  32. if ($this->save_to_file) {
  33. $res = ImageJPEG($im,$filename,$this->quality);
  34. }
  35. else {
  36. header("Content-type: image/jpeg");
  37. $res = ImageJPEG($im, NULL, $this->quality);
  38. }
  39. break;
  40. case 3:
  41. if (PHP_VERSION >= '5.1.2') {
  42. // Convert to PNG quality.
  43. // PNG quality: 0 (best quality, bigger file) to 9 (worst quality, smaller file)
  44. $quality = 9 - min( round($this->quality / 10), 9 );
  45. if ($this->save_to_file) {
  46. $res = ImagePNG($im, $filename, $quality);
  47. }
  48. else {
  49. header("Content-type: image/png");
  50. $res = ImagePNG($im, NULL, $quality);
  51. }
  52. }
  53. else {
  54. if ($this->save_to_file) {
  55. $res = ImagePNG($im, $filename);
  56. }
  57. else {
  58. header("Content-type: image/png");
  59. $res = ImagePNG($im);
  60. }
  61. }
  62. break;
  63. }
  64. return $res; //GIBT DAS BILD AUS - hier sicherung des bildes einbauen! (und die header raus nehmen)
  65. }
  66. function ImageCreateFromType($type,$filename) {
  67. $im = null;
  68. switch ($type) {
  69. case 1:
  70. $im = ImageCreateFromGif($filename);
  71. break;
  72. case 2:
  73. $im = ImageCreateFromJpeg($filename);
  74. break;
  75. case 3:
  76. $im = ImageCreateFromPNG($filename);
  77. break;
  78. }
  79. return $im;
  80. }
  81. // generate thumb from image and save it
  82. function GenerateThumbFile($from_name, $to_name) {
  83. // if src is URL then download file first
  84. $temp = false;
  85. if (substr($from_name,0,7) == 'http://') {
  86. $tmpfname = tempnam("tmp/", "TmP-");
  87. $temp = @fopen($tmpfname, "w");
  88. if ($temp) {
  89. @fwrite($temp, @file_get_contents($from_name)) or die("Cannot download image");
  90. @fclose($temp);
  91. $from_name = $tmpfname;
  92. }
  93. else {
  94. die("Cannot create temp file");
  95. }
  96. }
  97. // check if file exists
  98. if (!file_exists($from_name)) die("Source image does not exist!");
  99. // get source image size (width/height/type)
  100. // orig_img_type 1 = GIF, 2 = JPG, 3 = PNG
  101. list($orig_x, $orig_y, $orig_img_type, $img_sizes) = @GetImageSize($from_name);
  102. // cut image if specified by user
  103. if ($this->cut_x > 0) $orig_x = min($this->cut_x, $orig_x);
  104. if ($this->cut_y > 0) $orig_y = min($this->cut_y, $orig_y);
  105. // should we override thumb image type?
  106. $this->image_type = ($this->image_type != -1 ? $this->image_type : $orig_img_type);
  107. // check for allowed image types
  108. if ($orig_img_type < 1 or $orig_img_type > 3) die("Image type not supported");
  109. if ($orig_x > $this->max_x or $orig_y > $this->max_y) {
  110. // resize
  111. $per_x = $orig_x / $this->max_x;
  112. $per_y = $orig_y / $this->max_y;
  113. if ($per_y > $per_x) {
  114. $this->max_x = $orig_x / $per_y;
  115. }
  116. else {
  117. $this->max_y = $orig_y / $per_x;
  118. }
  119. }
  120. else {
  121. // keep original sizes, i.e. just copy
  122. if ($this->save_to_file) {
  123. @copy($from_name, $to_name);
  124. }
  125. else {
  126. switch ($this->image_type) {
  127. case 1:
  128. header("Content-type: image/gif");
  129. readfile($from_name);
  130. break;
  131. case 2:
  132. header("Content-type: image/jpeg");
  133. readfile($from_name);
  134. break;
  135. case 3:
  136. header("Content-type: image/png");
  137. readfile($from_name);
  138. break;
  139. }
  140. }
  141. return;
  142. }
  143. if ($this->image_type == 1) {
  144. // should use this function for gifs (gifs are palette images)
  145. $ni = imagecreate($this->max_x, $this->max_y);
  146. }
  147. else {
  148. // Create a new true color image
  149. $ni = ImageCreateTrueColor($this->max_x,$this->max_y);
  150. }
  151. // Fill image with white background (255,255,255)
  152. $white = imagecolorallocate($ni, 255, 255, 255);
  153. imagefilledrectangle( $ni, 0, 0, $this->max_x, $this->max_y, $white);
  154. // Create a new image from source file
  155. $im = $this->ImageCreateFromType($orig_img_type,$from_name);
  156. // Copy the palette from one image to another
  157. imagepalettecopy($ni,$im);
  158. // Copy and resize part of an image with resampling
  159. imagecopyresampled(
  160. $ni, $im, // destination, source
  161. 0, 0, 0, 0, // dstX, dstY, srcX, srcY
  162. $this->max_x, $this->max_y, // dstW, dstH
  163. $orig_x, $orig_y); // srcW, srcH
  164. // save thumb file
  165. $saved = $this->SaveImage($ni, $to_name);
  166. if($temp) {
  167. unlink($tmpfname); // this removes the file
  168. }
  169. }
  170. }
  171. class qqUploadedFileXhr {
  172. /**
  173. * Save the file to the specified path
  174. * @return boolean TRUE on success
  175. */
  176. function save($path) {
  177. $input = fopen("php://input", "r");
  178. $temp = tmpfile();
  179. $realSize = stream_copy_to_stream($input, $temp);
  180. fclose($input);
  181. if ($realSize != $this->getSize()){
  182. return false;
  183. }
  184. $target = fopen($path, "w");
  185. fseek($temp, 0, SEEK_SET);
  186. stream_copy_to_stream($temp, $target);
  187. fclose($target);
  188. return true;
  189. }
  190. function getName() {
  191. return $_GET['qqfile'];
  192. }
  193. function getSize() {
  194. if (isset($_SERVER["CONTENT_LENGTH"])){
  195. return (int)$_SERVER["CONTENT_LENGTH"];
  196. } else {
  197. throw new Exception('Getting content length is not supported.');
  198. }
  199. }
  200. }
  201. /**
  202. * Handle file uploads via regular form post (uses the $_FILES array)
  203. */
  204. class qqUploadedFileForm {
  205. /**
  206. * Save the file to the specified path
  207. * @return boolean TRUE on success
  208. */
  209. function save($path) {
  210. if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
  211. return false;
  212. }
  213. return true;
  214. }
  215. function getName() {
  216. return $_FILES['qqfile']['name'];
  217. }
  218. function getSize() {
  219. return $_FILES['qqfile']['size'];
  220. }
  221. }
  222. class qqFileUploader {
  223. private $allowedExtensions = array();
  224. private $sizeLimit = 10485760;
  225. private $file;
  226. var $db;
  227. function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
  228. $allowedExtensions = array_map("strtolower", $allowedExtensions);
  229. $this->allowedExtensions = $allowedExtensions;
  230. $this->sizeLimit = $sizeLimit;
  231. $this->checkServerSettings();
  232. if (isset($_GET['qqfile'])) {
  233. $this->file = new qqUploadedFileXhr();
  234. } elseif (isset($_FILES['qqfile'])) {
  235. $this->file = new qqUploadedFileForm();
  236. } else {
  237. $this->file = false;
  238. }
  239. }
  240. private function checkServerSettings(){
  241. $postSize = $this->toBytes(ini_get('post_max_size'));
  242. $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
  243. if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
  244. $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
  245. die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
  246. }
  247. }
  248. private function toBytes($str){
  249. $val = trim($str);
  250. $last = strtolower($str[strlen($str)-1]);
  251. switch($last) {
  252. case 'g': $val *= 1024;
  253. case 'm': $val *= 1024;
  254. case 'k': $val *= 1024;
  255. }
  256. return $val;
  257. }
  258. /**
  259. * Returns array('success'=>true) or array('error'=>'error message')
  260. */
  261. function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
  262. global $db;
  263. $this->db = &$db;
  264. if (!is_writable($uploadDirectory)){
  265. return array('error' => "Server error. Upload directory isn't writable.");
  266. }
  267. if (!$this->file){
  268. return array('error' => 'No files were uploaded.');
  269. }
  270. $size = $this->file->getSize();
  271. if ($size == 0) {
  272. return array('error' => 'File is empty');
  273. }
  274. if ($size > $this->sizeLimit) {
  275. return array('error' => 'File is too large');
  276. }
  277. $pathinfo = pathinfo($this->file->getName());
  278. $filename = $pathinfo['filename'];
  279. $filename = md5(uniqid());
  280. $ext = $pathinfo['extension'];
  281. if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
  282. $these = implode(', ', $this->allowedExtensions);
  283. return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
  284. }
  285. if(!$replaceOldFile){
  286. /// don't overwrite previous files that were uploaded
  287. while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
  288. $filename .= rand(10, 99);
  289. }
  290. }
  291. if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
  292. $path = $filename . '.' . $ext;
  293. //print_r($_GET);
  294. make_thumb($uploadDirectory.$path, $uploadDirectory.'thumb_'.$path, 150);
  295. updateGalleryDB($_GET['gallery_id'], $path, 'thumb_'.$path);
  296. return array('success'=>true);
  297. } else {
  298. return array('error'=> 'Could not save uploaded file.' .
  299. 'The upload was cancelled, or server error encountered');
  300. }
  301. }
  302. }
  303. // list of valid extensions, ex. array("jpeg", "xml", "bmp")
  304. $allowedExtensions = array();
  305. // max file size in bytes
  306. $sizeLimit = 10 * 1024 * 1024;
  307. //print_r($_POST);
  308. $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
  309. $result = $uploader->handleUpload('./../../media/images/');
  310. // to pass data through iframe you will need to encode all html tags
  311. echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
  312. ?>