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