PluginProbe
Gallery by BestWebSoft – Customizable Image and Photo Galleries for WordPress / 2.01
Gallery by BestWebSoft – Customizable Image and Photo Galleries for WordPress v2.01
4.8.1 4.8.0 4.7.9 trunk 2.01 2.02 2.03 2.04 2.05 2.06 2.07 2.08 2.09 2.10 2.11 2.12 2011.1.01 2011.1.02 3.01 3.02 3.03 3.04 3.05 3.06 3.1 All 131 releases
gallery-plugin / upload / php.php

php.php in Gallery by BestWebSoft – Customizable Image and Photo Galleries for WordPress 2.01, at upload/php.php

163 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Handle file uploads via XMLHttpRequest
5 */
6 class qqUploadedFileXhr {
7 /**
8 * Save the file to the specified path
9 * @return boolean TRUE on success
10 */
11 function save($path) {
12 $input = fopen("php://input", "r");
13 $temp = tmpfile();
14 $realSize = stream_copy_to_stream($input, $temp);
15 fclose($input);
16
17 if ($realSize != $this->getSize()){
18 return false;
19 }
20
21 $target = fopen($path, "w");
22 fseek($temp, 0, SEEK_SET);
23 stream_copy_to_stream($temp, $target);
24 fclose($target);
25
26 return true;
27 }
28 function getName() {
29 return $_GET['qqfile'];
30 }
31 function getSize() {
32 if (isset($_SERVER["CONTENT_LENGTH"])){
33 return (int)$_SERVER["CONTENT_LENGTH"];
34 } else {
35 throw new Exception('Getting content length is not supported.');
36 }
37 }
38 }
39
40 /**
41 * Handle file uploads via regular form post (uses the $_FILES array)
42 */
43 class qqUploadedFileForm {
44 /**
45 * Save the file to the specified path
46 * @return boolean TRUE on success
47 */
48 function save($path) {
49 if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
50 return false;
51 }
52 return true;
53 }
54 function getName() {
55 return $_FILES['qqfile']['name'];
56 }
57 function getSize() {
58 return $_FILES['qqfile']['size'];
59 }
60 }
61
62 class qqFileUploader {
63 private $allowedExtensions = array();
64 private $sizeLimit = 10485760;
65 private $file;
66
67 function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
68 $allowedExtensions = array_map("strtolower", $allowedExtensions);
69
70 $this->allowedExtensions = $allowedExtensions;
71 $this->sizeLimit = $sizeLimit;
72
73 //$this->checkServerSettings();
74
75 if (isset($_GET['qqfile'])) {
76 $this->file = new qqUploadedFileXhr();
77 } elseif (isset($_FILES['qqfile'])) {
78 $this->file = new qqUploadedFileForm();
79 } else {
80 $this->file = false;
81 }
82 }
83
84 private function checkServerSettings(){
85 $postSize = $this->toBytes(ini_get('post_max_size'));
86 $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
87
88 if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
89 $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
90 die("{error:'increase post_max_size and upload_max_filesize to $size'}");
91 }
92 }
93
94 private function toBytes($str){
95 $val = trim($str);
96 $last = strtolower($str[strlen($str)-1]);
97 switch($last) {
98 case 'g': $val *= 1024;
99 case 'm': $val *= 1024;
100 case 'k': $val *= 1024;
101 }
102 return $val;
103 }
104
105 /**
106 * Returns array('success'=>true) or array('error'=>'error message')
107 */
108 function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
109 if (!is_writable($uploadDirectory)){
110 return "{error:'Server error. Upload directory isn't writable.'}";
111 }
112
113 if (!$this->file){
114 return "{error:'No files were uploaded.'}";
115 }
116
117 $size = $this->file->getSize();
118
119 if ($size == 0) {
120 return "{error:'File is empty'}";
121 }
122
123 if ($size > $this->sizeLimit) {
124 return "{error:'File is too large'}";
125 }
126
127 $pathinfo = pathinfo($this->file->getName());
128 $ext = $pathinfo['extension'];
129 $filename = str_replace(".".$ext, "", $pathinfo['basename']);
130 //$filename = md5(uniqid());
131
132 if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
133 $these = implode(', ', $this->allowedExtensions);
134 return "{error:'File has an invalid extension, it should be one of $these .'}";
135 }
136
137 if(!$replaceOldFile){
138 /// don't overwrite previous files that were uploaded
139 while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
140 $filename .= rand(10, 99);
141 }
142 }
143
144 if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
145 list($width, $height, $type, $attr) = getimagesize($uploadDirectory . $filename . '.' . $ext);
146 return "{success:true,width:".$width.",height:".$height."}";
147 } else {
148 return "{error:'Could not save uploaded file. The upload was cancelled, or server error encountered'}";
149 }
150
151 }
152 }
153
154 // list of valid extensions, ex. array("jpeg", "xml", "bmp")
155 $allowedExtensions = array("jpeg", "jpg", "gif", "png");
156 // max file size in bytes
157 $sizeLimit = 10 * 1024 * 1024;
158
159 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
160 $result = $uploader->handleUpload('files/');
161 // to pass data through iframe you will need to encode all html tags
162 echo $result;
163