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
19 use Friendica\Core\Config;
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');
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');
38 function js_upload_form(&$a,&$b) {
40 $b['default_upload'] = false;
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>';
45 $upload_msg = t('Upload a file');
46 $drop_msg = t('Drop files here to upload');
47 $cancel = t('Cancel');
48 $failed = t('Failed');
50 $maximagesize = intval(Config::get('system','maximagesize'));
52 $b['addon_text'] .= <<< EOT
54 <div id="file-uploader-demo1">
56 <p>Please enable JavaScript to use file uploader.</p>
57 <!-- or put a simple form for upload here -->
61 <script type="text/javascript">
63 function getSelected(opt) {
64 var selected = new Array();
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;
77 function createUploader() {
78 uploader = new qq.FileUploader({
79 element: document.getElementById('file-uploader-demo1'),
80 action: '{$b['post_url']}',
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>' +
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>' +
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(',')
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(',')
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;
138 function js_upload_post_init(&$a,&$b) {
140 // list of valid extensions, ex. array("jpeg", "xml", "bmp")
142 $allowedExtensions = array("jpeg","gif","png","jpg");
144 // max file size in bytes
146 $sizeLimit = Config::get('system','maximagesize'); //6 * 1024 * 1024;
148 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
150 $result = $uploader->handleUpload();
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);
156 if(isset($result['error'])) {
157 logger('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'] , 'LOGGER_DEBUG');
158 echo json_encode($result);
162 $a->data['upload_result'] = $result;
166 function js_upload_post_file(&$a,&$b) {
168 $result = $a->data['upload_result'];
170 $b['src'] = $result['path'];
171 $b['filename'] = $result['filename'];
172 $b['filesize'] = filesize($b['src']);
177 function js_upload_post_end(&$a,&$b) {
179 logger('upload_post_end');
180 if(x($a->data,'upload_jsonresponse')) {
181 echo $a->data['upload_jsonresponse'];
189 * Handle file uploads via XMLHttpRequest
191 class qqUploadedFileXhr {
193 private $pathnm = '';
196 * Save the file in the temp dir.
197 * @return boolean TRUE on success
200 $input = fopen("php://input", "r");
202 $upload_dir = Config::get('system','tempdir');
204 $upload_dir = sys_get_temp_dir();
206 $this->pathnm = tempnam($upload_dir,'frn');
208 $temp = fopen($this->pathnm,"w");
209 $realSize = stream_copy_to_stream($input, $temp);
214 if ($realSize != $this->getSize()){
221 return $this->pathnm;
225 return $_GET['qqfile'];
229 if (isset($_SERVER["CONTENT_LENGTH"])){
230 return (int)$_SERVER["CONTENT_LENGTH"];
232 throw new Exception('Getting content length is not supported.');
238 * Handle file uploads via regular form post (uses the $_FILES array)
241 class qqUploadedFileForm {
245 * Save the file to the specified path
246 * @return boolean TRUE on success
255 return $_FILES['qqfile']['tmp_name'];
259 return $_FILES['qqfile']['name'];
262 return $_FILES['qqfile']['size'];
266 class qqFileUploader {
267 private $allowedExtensions = array();
268 private $sizeLimit = 10485760;
271 function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
272 $allowedExtensions = array_map("strtolower", $allowedExtensions);
274 $this->allowedExtensions = $allowedExtensions;
275 $this->sizeLimit = $sizeLimit;
277 if (isset($_GET['qqfile'])) {
278 $this->file = new qqUploadedFileXhr();
279 } elseif (isset($_FILES['qqfile'])) {
280 $this->file = new qqUploadedFileForm();
288 private function toBytes($str){
290 $last = strtolower($str[strlen($str)-1]);
292 case 'g': $val *= 1024;
293 case 'm': $val *= 1024;
294 case 'k': $val *= 1024;
300 * Returns array('success'=>true) or array('error'=>'error message')
302 function handleUpload(){
305 return array('error' => t('No files were uploaded.'));
308 $size = $this->file->getSize();
311 return array('error' => t('Uploaded file is empty'));
314 // if ($size > $this->sizeLimit) {
316 // return array('error' => t('Uploaded file is too large'));
320 $maximagesize = Config::get('system','maximagesize');
322 if(($maximagesize) && ($size > $maximagesize)) {
323 return array('error' => t('Image exceeds size limit of ') . $maximagesize );
327 $pathinfo = pathinfo($this->file->getName());
328 $filename = $pathinfo['filename'];
330 $ext = $pathinfo['extension'];
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 . '.');
337 if ($this->file->save()){
340 'path' => $this->file->getPath(),
341 'filename' => $filename . '.' . $ext
345 'error'=> t('Upload was cancelled, or server error encountered'),
346 'path' => $this->file->getPath(),
347 'filename' => $filename . '.' . $ext