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");
200 $upload_dir = get_config('system','tempdir');
202 $upload_dir = sys_get_temp_dir();
204 $this->pathnm = tempnam($upload_dir,'frn');
206 $temp = fopen($this->pathnm,"w");
207 $realSize = stream_copy_to_stream($input, $temp);
212 if ($realSize != $this->getSize()){
219 return $this->pathnm;
223 return $_GET['qqfile'];
227 if (isset($_SERVER["CONTENT_LENGTH"])){
228 return (int)$_SERVER["CONTENT_LENGTH"];
230 throw new Exception('Getting content length is not supported.');
236 * Handle file uploads via regular form post (uses the $_FILES array)
239 class qqUploadedFileForm {
243 * Save the file to the specified path
244 * @return boolean TRUE on success
253 return $_FILES['qqfile']['tmp_name'];
257 return $_FILES['qqfile']['name'];
260 return $_FILES['qqfile']['size'];
264 class qqFileUploader {
265 private $allowedExtensions = array();
266 private $sizeLimit = 10485760;
269 function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
270 $allowedExtensions = array_map("strtolower", $allowedExtensions);
272 $this->allowedExtensions = $allowedExtensions;
273 $this->sizeLimit = $sizeLimit;
275 if (isset($_GET['qqfile'])) {
276 $this->file = new qqUploadedFileXhr();
277 } elseif (isset($_FILES['qqfile'])) {
278 $this->file = new qqUploadedFileForm();
286 private function toBytes($str){
288 $last = strtolower($str[strlen($str)-1]);
290 case 'g': $val *= 1024;
291 case 'm': $val *= 1024;
292 case 'k': $val *= 1024;
298 * Returns array('success'=>true) or array('error'=>'error message')
300 function handleUpload(){
303 return array('error' => t('No files were uploaded.'));
306 $size = $this->file->getSize();
309 return array('error' => t('Uploaded file is empty'));
312 // if ($size > $this->sizeLimit) {
314 // return array('error' => t('Uploaded file is too large'));
318 $maximagesize = get_config('system','maximagesize');
320 if(($maximagesize) && ($size > $maximagesize)) {
321 return array('error' => t('Image exceeds size limit of ') . $maximagesize );
325 $pathinfo = pathinfo($this->file->getName());
326 $filename = $pathinfo['filename'];
328 $ext = $pathinfo['extension'];
330 if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
331 $these = implode(', ', $this->allowedExtensions);
332 return array('error' => t('File has an invalid extension, it should be one of ') . $these . '.');
335 if ($this->file->save()){
338 'path' => $this->file->getPath(),
339 'filename' => $filename . '.' . $ext
343 'error'=> t('Upload was cancelled, or server error encountered'),
344 'path' => $this->file->getPath(),
345 'filename' => $filename . '.' . $ext