Merge branch 'photos' of https://github.com/fabrixxm/friendika into fabrixxm-photos
[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         $upload_msg = t('Upload a file');
40         $drop_msg = t('Drop files here to upload');
41         $cancel = t('Cancel');
42         $failed = t('Failed');
43
44         $b['addon_text'] .= <<< EOT
45         
46  <div id="file-uploader-demo1">         
47   <noscript>                    
48    <p>Please enable JavaScript to use file uploader.</p>
49    <!-- or put a simple form for upload here -->
50   </noscript> 
51  </div>
52
53 <script type="text/javascript">
54 var uploader = null;       
55 function getSelected(opt) {
56             var selected = new Array();
57             var index = 0;
58             for (var intLoop = 0; intLoop < opt.length; intLoop++) {
59                if ((opt[intLoop].selected) ||
60                    (opt[intLoop].checked)) {
61                   index = selected.length;
62                   //selected[index] = new Object;
63                   selected[index] = opt[intLoop].value;
64                   //selected[index] = intLoop;
65                }
66             }
67             return selected;
68          } 
69 function createUploader() {
70         uploader = new qq.FileUploader({
71                 element: document.getElementById('file-uploader-demo1'),
72                 action: '{$b['post_url']}',
73
74         template: '<div class="qq-uploader">' + 
75                 '<div class="qq-upload-drop-area"><span>$drop_msg</span></div>' +
76                 '<div class="qq-upload-button">$upload_msg</div>' +
77                 '<ul class="qq-upload-list"></ul>' + 
78              '</div>',
79
80         // template for one item in file list
81         fileTemplate: '<li>' +
82                 '<span class="qq-upload-file"></span>' +
83                 '<span class="qq-upload-spinner"></span>' +
84                 '<span class="qq-upload-size"></span>' +
85                 '<a class="qq-upload-cancel" href="#">$cancel</a>' +
86                 '<span class="qq-upload-failed-text">$failed</span>' +
87             '</li>',        
88
89                 debug: true,
90                 onSubmit: function(id,filename) {
91
92                         uploader.setParams( {
93                                 newalbum                :       document.getElementById('photos-upload-newalbum').value,
94                                 album                   :       document.getElementById('photos-upload-album-select').value,
95                                 group_allow             :       getSelected(document.getElementById('group_allow')).join(','),
96                                 contact_allow   :       getSelected(document.getElementById('contact_allow')).join(','),
97                                 group_deny              :       getSelected(document.getElementById('group_deny')).join(','),
98                                 contact_deny    :       getSelected(document.getElementById('contact_deny')).join(',')
99                         });
100                 }
101         });           
102 }
103
104
105 // in your app create uploader as soon as the DOM is ready
106 // don't wait for the window to load  
107 window.onload = createUploader;     
108
109
110 </script>
111  
112 EOT;
113
114
115 }
116
117 function js_upload_post_init(&$a,&$b) {
118
119         // list of valid extensions, ex. array("jpeg", "xml", "bmp")
120
121         $allowedExtensions = array("jpeg","gif","png","jpg");
122
123         // max file size in bytes
124
125         $sizeLimit = 6 * 1024 * 1024;
126
127         $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
128
129         $result = $uploader->handleUpload();
130
131
132         // to pass data through iframe you will need to encode all html tags
133         $a->data['upload_jsonresponse'] =  htmlspecialchars(json_encode($result), ENT_NOQUOTES);
134
135         if(isset($result['error'])) {
136                 logger('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'] , 'LOGGER_DEBUG');
137                 killme();
138         }
139
140         $a->data['upload_result'] = $result;
141
142 }
143
144 function js_upload_post_file(&$a,&$b) {
145
146         $result = $a->data['upload_result'];
147
148         $b['src']               = $result['path'];
149         $b['filename']  = $result['filename'];
150         $b['filesize']  = filesize($b['src']);
151
152 }
153
154
155 function js_upload_post_end(&$a,&$b) {
156
157 logger('upload_post_end');
158         if(x($a->data,'upload_jsonresponse')) {
159                 echo $a->data['upload_jsonresponse'];
160                 killme();
161         }
162
163 }
164
165
166 /**
167  * Handle file uploads via XMLHttpRequest
168  */
169 class qqUploadedFileXhr {
170
171         private $pathnm = '';
172
173     /**
174      * Save the file in the temp dir.
175      * @return boolean TRUE on success
176      */
177     function save() {    
178         $input = fopen("php://input", "r");
179         $this->pathnm = tempnam(sys_get_temp_dir(),'frn');
180                 $temp = fopen($this->pathnm,"w");
181         $realSize = stream_copy_to_stream($input, $temp);
182
183         fclose($input);
184                 fclose($temp);
185         
186         if ($realSize != $this->getSize()){            
187             return false;
188         }
189         return true;
190     }
191
192         function getPath() {
193                 return $this->pathnm;
194         }
195
196     function getName() {
197         return $_GET['qqfile'];
198     }
199
200     function getSize() {
201         if (isset($_SERVER["CONTENT_LENGTH"])){
202             return (int)$_SERVER["CONTENT_LENGTH"];            
203         } else {
204             throw new Exception('Getting content length is not supported.');
205         }      
206     }   
207 }
208
209 /**
210  * Handle file uploads via regular form post (uses the $_FILES array)
211  */
212
213 class qqUploadedFileForm {  
214
215
216     /**
217      * Save the file to the specified path
218      * @return boolean TRUE on success
219      */
220
221
222     function save() {
223         return true;
224     }
225
226         function getPath() {
227                 return $_FILES['qqfile']['tmp_name'];
228         }
229
230     function getName() {
231         return $_FILES['qqfile']['name'];
232     }
233     function getSize() {
234         return $_FILES['qqfile']['size'];
235     }
236 }
237
238 class qqFileUploader {
239     private $allowedExtensions = array();
240     private $sizeLimit = 10485760;
241     private $file;
242
243     function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){        
244         $allowedExtensions = array_map("strtolower", $allowedExtensions);
245             
246         $this->allowedExtensions = $allowedExtensions;        
247         $this->sizeLimit = $sizeLimit;
248         
249         $this->checkServerSettings();       
250
251         if (isset($_GET['qqfile'])) {
252             $this->file = new qqUploadedFileXhr();
253         } elseif (isset($_FILES['qqfile'])) {
254             $this->file = new qqUploadedFileForm();
255         } else {
256             $this->file = false; 
257         }
258
259     }
260     
261     private function checkServerSettings(){        
262         $postSize = $this->toBytes(ini_get('post_max_size'));
263         $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));        
264                 logger('mod/photos.php: qqFileUploader(): upload_max_filesize=' . $uploadSize , 'LOGGER_DEBUG');
265         if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
266             $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';             
267             die("{'error':'increase post_max_size and upload_max_filesize to $size'}");    
268         }        
269     }
270     
271     private function toBytes($str){
272         $val = trim($str);
273         $last = strtolower($str[strlen($str)-1]);
274         switch($last) {
275             case 'g': $val *= 1024;
276             case 'm': $val *= 1024;
277             case 'k': $val *= 1024;        
278         }
279         return $val;
280     }
281     
282     /**
283      * Returns array('success'=>true) or array('error'=>'error message')
284      */
285     function handleUpload(){
286         
287         if (!$this->file){
288             return array('error' => t('No files were uploaded.'));
289         }
290         
291         $size = $this->file->getSize();
292         
293         if ($size == 0) {
294             return array('error' => t('Uploaded file is empty'));
295         }
296         
297         if ($size > $this->sizeLimit) {
298
299             return array('error' => t('Uploaded file is too large'));
300         }
301         
302
303                 $maximagesize = get_config('system','maximagesize');
304
305                 if(($maximagesize) && ($size > $maximagesize)) {
306                         return array('error' => t('Image exceeds size limit of ') . $maximagesize );
307
308                 }
309
310         $pathinfo = pathinfo($this->file->getName());
311         $filename = $pathinfo['filename'];
312
313         $ext = $pathinfo['extension'];
314
315         if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
316             $these = implode(', ', $this->allowedExtensions);
317             return array('error' => t('File has an invalid extension, it should be one of ') . $these . '.');
318         }
319         
320         if ($this->file->save()){
321             return array(
322                                 'success'=>true,
323                                 'path' => $this->file->getPath(), 
324                                 'filename' => $filename . '.' . $ext
325                         );
326         } else {
327             return array(
328                                 'error'=> t('Upload was cancelled, or server error encountered'),
329                                 'path' => $this->file->getPath(), 
330                                 'filename' => $filename . '.' . $ext
331                         );
332         }
333         
334     }    
335 }