roll version - now in alpha 2.1
[friendica.git/.git] / addon / js_upload / js_upload.php
1 <?php
2
3 /**
4  *
5  * JavaScript Photo/Image Uploader
6  *
7  * Uses Valum 'qq' Uploader. 
8  * Module Author: Chris Case
9  *
10  * Prior to enabling, ensure that you have a directory 'uploads'
11  * which is writable by the web server.
12  *
13  */
14
15
16 function js_upload_install() {
17         register_hook('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
18         register_hook('photo_post_init',   'addon/js_upload/js_upload.php', 'js_upload_post_init');
19         register_hook('photo_post_file',   'addon/js_upload/js_upload.php', 'js_upload_post_file');
20         register_hook('photo_post_end',    'addon/js_upload/js_upload.php', 'js_upload_post_end');
21 }
22
23
24 function js_upload_uninstall() {
25         unregister_hook('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
26         unregister_hook('photo_post_init',   'addon/js_upload/js_upload.php', 'js_upload_post_init');
27         unregister_hook('photo_post_file',   'addon/js_upload/js_upload.php', 'js_upload_post_file');
28         unregister_hook('photo_post_end',    'addon/js_upload/js_upload.php', 'js_upload_post_end');
29 }
30
31
32 function js_upload_form(&$a,&$b) {
33
34         $b['default_upload'] = false;
35
36         $b['addon_text'] .= '<link href="' . $a->get_baseurl() . '/addon/js_upload/file-uploader/client/fileuploader.css" rel="stylesheet" type="text/css">';
37         $b['addon_text'] .= '<script src="' . $a->get_baseurl() . '/addon/js_upload/file-uploader/client/fileuploader.js" type="text/javascript"></script>';
38    
39         $b['addon_text'] .= <<< EOT
40         
41  <div id="file-uploader-demo1">         
42   <noscript>                    
43    <p>Please enable JavaScript to use file uploader.</p>
44    <!-- or put a simple form for upload here -->
45   </noscript> 
46  </div>
47
48 <script type="text/javascript">
49 var uploader = null;       
50 function getSelected(opt) {
51             var selected = new Array();
52             var index = 0;
53             for (var intLoop = 0; intLoop < opt.length; intLoop++) {
54                if ((opt[intLoop].selected) ||
55                    (opt[intLoop].checked)) {
56                   index = selected.length;
57                   //selected[index] = new Object;
58                   selected[index] = opt[intLoop].value;
59                   //selected[index] = intLoop;
60                }
61             }
62             return selected;
63          } 
64 function createUploader() {
65         uploader = new qq.FileUploader({
66                 element: document.getElementById('file-uploader-demo1'),
67                 action: '{$b['post_url']}',
68                 debug: true,
69                 onSubmit: function(id,filename) {
70
71                         uploader.setParams( {
72                                 newalbum                :       document.getElementById('photos-upload-newalbum').value,
73                                 album                   :       document.getElementById('photos-upload-album-select').value,
74                                 group_allow             :       getSelected(document.getElementById('group_allow')).join(','),
75                                 contact_allow   :       getSelected(document.getElementById('contact_allow')).join(','),
76                                 group_deny              :       getSelected(document.getElementById('group_deny')).join(','),
77                                 contact_deny    :       getSelected(document.getElementById('contact_deny')).join(',')
78                         });
79                 }
80         });           
81 }
82
83
84 // in your app create uploader as soon as the DOM is ready
85 // don't wait for the window to load  
86 window.onload = createUploader;     
87
88
89 </script>
90  
91 EOT;
92
93
94 }
95
96 function js_upload_post_init(&$a,&$b) {
97
98         // list of valid extensions, ex. array("jpeg", "xml", "bmp")
99
100         $allowedExtensions = array("jpeg","gif","png","jpg");
101
102         // max file size in bytes
103
104         $sizeLimit = 6 * 1024 * 1024;
105
106         $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
107
108         $result = $uploader->handleUpload('uploads/');
109
110
111         // to pass data through iframe you will need to encode all html tags
112         $a->data['upload_jsonresponse'] =  htmlspecialchars(json_encode($result), ENT_NOQUOTES);
113
114         if(isset($result['error'])) {
115                 logger('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'] , 'LOGGER_DEBUG');
116                 killme();
117         }
118
119         $a->data['upload_result'] = $result;
120
121 }
122
123 function js_upload_post_file(&$a,&$b) {
124
125         $result = $a->data['upload_result'];
126
127         $b['src']               = 'uploads/' . $result['filename'];
128         $b['filename']  = $result['filename'];
129         $b['filesize']  = filesize($b['src']);
130
131 logger('post_file' . print_r($b, true));
132 }
133
134
135 function js_upload_post_end(&$a,&$b) {
136
137 logger('upload_post_end');
138         if(x($a->data,'upload_jsonresponse')) {
139                 echo $a->data['upload_jsonresponse'];
140                 killme();
141         }
142
143 }
144
145
146 /**
147  * Handle file uploads via XMLHttpRequest
148  */
149 class qqUploadedFileXhr {
150     /**
151      * Save the file to the specified path
152      * @return boolean TRUE on success
153      */
154     function save($path) {    
155         $input = fopen("php://input", "r");
156         $temp = tmpfile();
157         $realSize = stream_copy_to_stream($input, $temp);
158         fclose($input);
159         
160         if ($realSize != $this->getSize()){            
161             return false;
162         }
163         
164         $target = fopen($path, "w");        
165         fseek($temp, 0, SEEK_SET);
166         stream_copy_to_stream($temp, $target);
167         fclose($target);
168         
169         return true;
170     }
171
172     function getName() {
173         return $_GET['qqfile'];
174     }
175
176     function getSize() {
177         if (isset($_SERVER["CONTENT_LENGTH"])){
178             return (int)$_SERVER["CONTENT_LENGTH"];            
179         } else {
180             throw new Exception('Getting content length is not supported.');
181         }      
182     }   
183 }
184
185 /**
186  * Handle file uploads via regular form post (uses the $_FILES array)
187  */
188
189 class qqUploadedFileForm {  
190     /**
191      * Save the file to the specified path
192      * @return boolean TRUE on success
193      */
194     function save($path) {
195         if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
196             return false;
197         }
198         return true;
199     }
200     function getName() {
201         return $_FILES['qqfile']['name'];
202     }
203     function getSize() {
204         return $_FILES['qqfile']['size'];
205     }
206 }
207
208 class qqFileUploader {
209     private $allowedExtensions = array();
210     private $sizeLimit = 10485760;
211     private $file;
212
213     function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){        
214         $allowedExtensions = array_map("strtolower", $allowedExtensions);
215             
216         $this->allowedExtensions = $allowedExtensions;        
217         $this->sizeLimit = $sizeLimit;
218         
219         $this->checkServerSettings();       
220
221         if (isset($_GET['qqfile'])) {
222             $this->file = new qqUploadedFileXhr();
223         } elseif (isset($_FILES['qqfile'])) {
224             $this->file = new qqUploadedFileForm();
225         } else {
226             $this->file = false; 
227         }
228     }
229     
230     private function checkServerSettings(){        
231         $postSize = $this->toBytes(ini_get('post_max_size'));
232         $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));        
233                 logger('mod/photos.php: qqFileUploader(): upload_max_filesize=' . $uploadSize , 'LOGGER_DEBUG');
234         if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
235             $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';             
236             die("{'error':'increase post_max_size and upload_max_filesize to $size'}");    
237         }        
238     }
239     
240     private function toBytes($str){
241         $val = trim($str);
242         $last = strtolower($str[strlen($str)-1]);
243         switch($last) {
244             case 'g': $val *= 1024;
245             case 'm': $val *= 1024;
246             case 'k': $val *= 1024;        
247         }
248         return $val;
249     }
250     
251     /**
252      * Returns array('success'=>true) or array('error'=>'error message')
253      */
254     function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
255         if (!is_writable($uploadDirectory)){
256             return array('error' => t('Server error. Upload directory isn\'t writable.'));
257         }
258         
259         if (!$this->file){
260             return array('error' => t('No files were uploaded.'));
261         }
262         
263         $size = $this->file->getSize();
264         
265         if ($size == 0) {
266             return array('error' => t('Uploaded file is empty'));
267         }
268         
269         if ($size > $this->sizeLimit) {
270
271             return array('error' => t('Uploaded file is too large'));
272         }
273         
274
275                 $maximagesize = get_config('system','maximagesize');
276
277                 if(($maximagesize) && ($size > $maximagesize)) {
278                         return array('error' => t('Image exceeds size limit of ') . $maximagesize );
279
280                 }
281
282         $pathinfo = pathinfo($this->file->getName());
283         $filename = $pathinfo['filename'];
284
285         $ext = $pathinfo['extension'];
286
287         if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
288             $these = implode(', ', $this->allowedExtensions);
289             return array('error' => t('File has an invalid extension, it should be one of ') . $these . '.');
290         }
291         
292         if(!$replaceOldFile){
293             /// don't overwrite previous files that were uploaded
294             while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
295                 $filename .= rand(10, 99);
296             }
297         }
298         
299         if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
300             return array('success'=>true,'filename' => $filename . '.' . $ext);
301         } else {
302             return array('error'=> t('Could not save uploaded file.') .
303                 t('The upload was cancelled, or server error encountered'),'filename' => $filename . '.' . $ext);
304         }
305         
306     }    
307 }