5 * Description: JavaScript photo/image uploader. Uses Valum 'qq' Uploader.
7 * Author: Chris Case <http://friendika.openmindspace.org/profile/chris_case>
12 * JavaScript Photo/Image Uploader
14 * Uses Valum 'qq' Uploader.
15 * Module Author: Chris Case
20 function js_upload_install() {
21 register_hook('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
22 register_hook('photo_post_init', 'addon/js_upload/js_upload.php', 'js_upload_post_init');
23 register_hook('photo_post_file', 'addon/js_upload/js_upload.php', 'js_upload_post_file');
24 register_hook('photo_post_end', 'addon/js_upload/js_upload.php', 'js_upload_post_end');
28 function js_upload_uninstall() {
29 unregister_hook('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
30 unregister_hook('photo_post_init', 'addon/js_upload/js_upload.php', 'js_upload_post_init');
31 unregister_hook('photo_post_file', 'addon/js_upload/js_upload.php', 'js_upload_post_file');
32 unregister_hook('photo_post_end', 'addon/js_upload/js_upload.php', 'js_upload_post_end');
36 function js_upload_form(&$a,&$b) {
38 $b['default_upload'] = false;
40 $b['addon_text'] .= '<link href="' . $a->get_baseurl() . '/addon/js_upload/file-uploader/client/fileuploader.css" rel="stylesheet" type="text/css">';
41 $b['addon_text'] .= '<script src="' . $a->get_baseurl() . '/addon/js_upload/file-uploader/client/fileuploader.js" type="text/javascript"></script>';
43 $upload_msg = t('Upload a file');
44 $drop_msg = t('Drop files here to upload');
45 $cancel = t('Cancel');
46 $failed = t('Failed');
48 $maximagesize = intval(get_config('system','maximagesize'));
50 $b['addon_text'] .= <<< EOT
52 <div id="file-uploader-demo1">
54 <p>Please enable JavaScript to use file uploader.</p>
55 <!-- or put a simple form for upload here -->
59 <script type="text/javascript">
61 function getSelected(opt) {
62 var selected = new Array();
64 for (var intLoop = 0; intLoop < opt.length; intLoop++) {
65 if ((opt[intLoop].selected) ||
66 (opt[intLoop].checked)) {
67 index = selected.length;
68 //selected[index] = new Object;
69 selected[index] = opt[intLoop].value;
70 //selected[index] = intLoop;
75 function createUploader() {
76 uploader = new qq.FileUploader({
77 element: document.getElementById('file-uploader-demo1'),
78 action: '{$b['post_url']}',
80 template: '<div class="qq-uploader">' +
81 '<div class="qq-upload-drop-area"><span>$drop_msg</span></div>' +
82 '<div class="qq-upload-button">$upload_msg</div>' +
83 '<ul class="qq-upload-list"></ul>' +
86 // template for one item in file list
87 fileTemplate: '<li>' +
88 '<span class="qq-upload-file"></span>' +
89 '<span class="qq-upload-spinner"></span>' +
90 '<span class="qq-upload-size"></span>' +
91 '<a class="qq-upload-cancel" href="#">$cancel</a>' +
92 '<span class="qq-upload-failed-text">$failed</span>' +
96 sizeLimit: $maximagesize,
97 onSubmit: function(id,filename) {
98 if (typeof acl!="undefined"){
100 newalbum : document.getElementById('photos-upload-newalbum').value,
101 album : document.getElementById('photos-upload-album-select').value,
102 not_visible : document.getElementById('photos-upload-noshare').checked,
103 group_allow : acl.allow_gid.join(','),
104 contact_allow : acl.allow_cid.join(','),
105 group_deny : acl.deny_gid.join(','),
106 contact_deny : acl.deny_cid.join(',')
109 uploader.setParams( {
110 newalbum : document.getElementById('photos-upload-newalbum').value,
111 album : document.getElementById('photos-upload-album-select').value,
112 not_visible : document.getElementById('photos-upload-noshare').checked,
113 group_allow : getSelected(document.getElementById('group_allow')).join(','),
114 contact_allow : getSelected(document.getElementById('contact_allow')).join(','),
115 group_deny : getSelected(document.getElementById('group_deny')).join(','),
116 contact_deny : getSelected(document.getElementById('contact_deny')).join(',')
124 // in your app create uploader as soon as the DOM is ready
125 // don't wait for the window to load
126 window.onload = createUploader;
136 function js_upload_post_init(&$a,&$b) {
138 // list of valid extensions, ex. array("jpeg", "xml", "bmp")
140 $allowedExtensions = array("jpeg","gif","png","jpg");
142 // max file size in bytes
144 $sizeLimit = get_config('system','maximagesize'); //6 * 1024 * 1024;
146 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
148 $result = $uploader->handleUpload();
151 // to pass data through iframe you will need to encode all html tags
152 $a->data['upload_jsonresponse'] = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
154 if(isset($result['error'])) {
155 logger('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'] , 'LOGGER_DEBUG');
156 echo json_encode($result);
160 $a->data['upload_result'] = $result;
164 function js_upload_post_file(&$a,&$b) {
166 $result = $a->data['upload_result'];
168 $b['src'] = $result['path'];
169 $b['filename'] = $result['filename'];
170 $b['filesize'] = filesize($b['src']);
175 function js_upload_post_end(&$a,&$b) {
177 logger('upload_post_end');
178 if(x($a->data,'upload_jsonresponse')) {
179 echo $a->data['upload_jsonresponse'];
187 * Handle file uploads via XMLHttpRequest
189 class qqUploadedFileXhr {
191 private $pathnm = '';
194 * Save the file in the temp dir.
195 * @return boolean TRUE on success
198 $input = fopen("php://input", "r");
199 $this->pathnm = tempnam(sys_get_temp_dir(),'frn');
200 $temp = fopen($this->pathnm,"w");
201 $realSize = stream_copy_to_stream($input, $temp);
206 if ($realSize != $this->getSize()){
213 return $this->pathnm;
217 return $_GET['qqfile'];
221 if (isset($_SERVER["CONTENT_LENGTH"])){
222 return (int)$_SERVER["CONTENT_LENGTH"];
224 throw new Exception('Getting content length is not supported.');
230 * Handle file uploads via regular form post (uses the $_FILES array)
233 class qqUploadedFileForm {
237 * Save the file to the specified path
238 * @return boolean TRUE on success
247 return $_FILES['qqfile']['tmp_name'];
251 return $_FILES['qqfile']['name'];
254 return $_FILES['qqfile']['size'];
258 class qqFileUploader {
259 private $allowedExtensions = array();
260 private $sizeLimit = 10485760;
263 function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
264 $allowedExtensions = array_map("strtolower", $allowedExtensions);
266 $this->allowedExtensions = $allowedExtensions;
267 $this->sizeLimit = $sizeLimit;
269 if (isset($_GET['qqfile'])) {
270 $this->file = new qqUploadedFileXhr();
271 } elseif (isset($_FILES['qqfile'])) {
272 $this->file = new qqUploadedFileForm();
280 private function toBytes($str){
282 $last = strtolower($str[strlen($str)-1]);
284 case 'g': $val *= 1024;
285 case 'm': $val *= 1024;
286 case 'k': $val *= 1024;
292 * Returns array('success'=>true) or array('error'=>'error message')
294 function handleUpload(){
297 return array('error' => t('No files were uploaded.'));
300 $size = $this->file->getSize();
303 return array('error' => t('Uploaded file is empty'));
306 // if ($size > $this->sizeLimit) {
308 // return array('error' => t('Uploaded file is too large'));
312 $maximagesize = get_config('system','maximagesize');
314 if(($maximagesize) && ($size > $maximagesize)) {
315 return array('error' => t('Image exceeds size limit of ') . $maximagesize );
319 $pathinfo = pathinfo($this->file->getName());
320 $filename = $pathinfo['filename'];
322 $ext = $pathinfo['extension'];
324 if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
325 $these = implode(', ', $this->allowedExtensions);
326 return array('error' => t('File has an invalid extension, it should be one of ') . $these . '.');
329 if ($this->file->save()){
332 'path' => $this->file->getPath(),
333 'filename' => $filename . '.' . $ext
337 'error'=> t('Upload was cancelled, or server error encountered'),
338 'path' => $this->file->getPath(),
339 'filename' => $filename . '.' . $ext