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 $b['addon_text'] .= <<< EOT
50 <div id="file-uploader-demo1">
52 <p>Please enable JavaScript to use file uploader.</p>
53 <!-- or put a simple form for upload here -->
57 <script type="text/javascript">
59 function getSelected(opt) {
60 var selected = new Array();
62 for (var intLoop = 0; intLoop < opt.length; intLoop++) {
63 if ((opt[intLoop].selected) ||
64 (opt[intLoop].checked)) {
65 index = selected.length;
66 //selected[index] = new Object;
67 selected[index] = opt[intLoop].value;
68 //selected[index] = intLoop;
73 function createUploader() {
74 uploader = new qq.FileUploader({
75 element: document.getElementById('file-uploader-demo1'),
76 action: '{$b['post_url']}',
78 template: '<div class="qq-uploader">' +
79 '<div class="qq-upload-drop-area"><span>$drop_msg</span></div>' +
80 '<div class="qq-upload-button">$upload_msg</div>' +
81 '<ul class="qq-upload-list"></ul>' +
84 // template for one item in file list
85 fileTemplate: '<li>' +
86 '<span class="qq-upload-file"></span>' +
87 '<span class="qq-upload-spinner"></span>' +
88 '<span class="qq-upload-size"></span>' +
89 '<a class="qq-upload-cancel" href="#">$cancel</a>' +
90 '<span class="qq-upload-failed-text">$failed</span>' +
94 onSubmit: function(id,filename) {
95 if (typeof acl!="undefined"){
97 newalbum : document.getElementById('photos-upload-newalbum').value,
98 album : document.getElementById('photos-upload-album-select').value,
99 group_allow : acl.allow_gid.join(','),
100 contact_allow : acl.allow_cid.join(','),
101 group_deny : acl.deny_gid.join(','),
102 contact_deny : acl.deny_cid.join(',')
105 uploader.setParams( {
106 newalbum : document.getElementById('photos-upload-newalbum').value,
107 album : document.getElementById('photos-upload-album-select').value,
108 group_allow : getSelected(document.getElementById('group_allow')).join(','),
109 contact_allow : getSelected(document.getElementById('contact_allow')).join(','),
110 group_deny : getSelected(document.getElementById('group_deny')).join(','),
111 contact_deny : getSelected(document.getElementById('contact_deny')).join(',')
119 // in your app create uploader as soon as the DOM is ready
120 // don't wait for the window to load
121 window.onload = createUploader;
131 function js_upload_post_init(&$a,&$b) {
133 // list of valid extensions, ex. array("jpeg", "xml", "bmp")
135 $allowedExtensions = array("jpeg","gif","png","jpg");
137 // max file size in bytes
139 $sizeLimit = get_config('system','maximagesize'); //6 * 1024 * 1024;
141 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
143 $result = $uploader->handleUpload();
146 // to pass data through iframe you will need to encode all html tags
147 $a->data['upload_jsonresponse'] = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
149 if(isset($result['error'])) {
150 logger('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'] , 'LOGGER_DEBUG');
151 echo json_encode($result);
155 $a->data['upload_result'] = $result;
159 function js_upload_post_file(&$a,&$b) {
161 $result = $a->data['upload_result'];
163 $b['src'] = $result['path'];
164 $b['filename'] = $result['filename'];
165 $b['filesize'] = filesize($b['src']);
170 function js_upload_post_end(&$a,&$b) {
172 logger('upload_post_end');
173 if(x($a->data,'upload_jsonresponse')) {
174 echo $a->data['upload_jsonresponse'];
182 * Handle file uploads via XMLHttpRequest
184 class qqUploadedFileXhr {
186 private $pathnm = '';
189 * Save the file in the temp dir.
190 * @return boolean TRUE on success
193 $input = fopen("php://input", "r");
194 $this->pathnm = tempnam(sys_get_temp_dir(),'frn');
195 $temp = fopen($this->pathnm,"w");
196 $realSize = stream_copy_to_stream($input, $temp);
201 if ($realSize != $this->getSize()){
208 return $this->pathnm;
212 return $_GET['qqfile'];
216 if (isset($_SERVER["CONTENT_LENGTH"])){
217 return (int)$_SERVER["CONTENT_LENGTH"];
219 throw new Exception('Getting content length is not supported.');
225 * Handle file uploads via regular form post (uses the $_FILES array)
228 class qqUploadedFileForm {
232 * Save the file to the specified path
233 * @return boolean TRUE on success
242 return $_FILES['qqfile']['tmp_name'];
246 return $_FILES['qqfile']['name'];
249 return $_FILES['qqfile']['size'];
253 class qqFileUploader {
254 private $allowedExtensions = array();
255 private $sizeLimit = 10485760;
258 function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
259 $allowedExtensions = array_map("strtolower", $allowedExtensions);
261 $this->allowedExtensions = $allowedExtensions;
262 $this->sizeLimit = $sizeLimit;
264 if (isset($_GET['qqfile'])) {
265 $this->file = new qqUploadedFileXhr();
266 } elseif (isset($_FILES['qqfile'])) {
267 $this->file = new qqUploadedFileForm();
275 private function toBytes($str){
277 $last = strtolower($str[strlen($str)-1]);
279 case 'g': $val *= 1024;
280 case 'm': $val *= 1024;
281 case 'k': $val *= 1024;
287 * Returns array('success'=>true) or array('error'=>'error message')
289 function handleUpload(){
292 return array('error' => t('No files were uploaded.'));
295 $size = $this->file->getSize();
298 return array('error' => t('Uploaded file is empty'));
301 if ($size > $this->sizeLimit) {
303 return array('error' => t('Uploaded file is too large'));
307 $maximagesize = get_config('system','maximagesize');
309 if(($maximagesize) && ($size > $maximagesize)) {
310 return array('error' => t('Image exceeds size limit of ') . $maximagesize );
314 $pathinfo = pathinfo($this->file->getName());
315 $filename = $pathinfo['filename'];
317 $ext = $pathinfo['extension'];
319 if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
320 $these = implode(', ', $this->allowedExtensions);
321 return array('error' => t('File has an invalid extension, it should be one of ') . $these . '.');
324 if ($this->file->save()){
327 'path' => $this->file->getPath(),
328 'filename' => $filename . '.' . $ext
332 'error'=> t('Upload was cancelled, or server error encountered'),
333 'path' => $this->file->getPath(),
334 'filename' => $filename . '.' . $ext