4 * Description: JavaScript photo/image uploader. Helpful for uploading multiple files at once. Uses Valum 'qq' Uploader.
6 * Author: Chris Case <http://friendika.openmindspace.org/profile/chris_case>
7 * Maintainer: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
15 use Friendica\Util\Strings;
17 global $js_upload_jsonresponse;
18 global $js_upload_result;
20 function js_upload_install()
22 Hook::register('photo_upload_form', __FILE__, 'js_upload_form');
23 Hook::register('photo_post_init', __FILE__, 'js_upload_post_init');
24 Hook::register('photo_post_file', __FILE__, 'js_upload_post_file');
25 Hook::register('photo_post_end', __FILE__, 'js_upload_post_end');
28 function js_upload_form(App $a, array &$b)
30 $b['default_upload'] = false;
32 DI::page()->registerStylesheet(__DIR__ . '/file-uploader/client/fileuploader.css');
33 DI::page()->registerFooterScript(__DIR__ . '/file-uploader/client/fileuploader.js');
35 $tpl = Renderer::getMarkupTemplate('js_upload.tpl', 'addon/js_upload');
36 $b['addon_text'] .= Renderer::replaceMacros($tpl, [
37 '$upload_msg' => DI::l10n()->t('Select files for upload'),
38 '$drop_msg' => DI::l10n()->t('Drop files here to upload'),
39 '$cancel' => DI::l10n()->t('Cancel'),
40 '$failed' => DI::l10n()->t('Failed'),
41 '$post_url' => $b['post_url'],
42 '$maximagesize' => intval(DI::config()->get('system', 'maximagesize')),
46 function js_upload_post_init(App $a, array &$b)
48 global $js_upload_result, $js_upload_jsonresponse;
50 // list of valid extensions
51 $allowedExtensions = ['jpeg', 'gif', 'png', 'jpg'];
53 // max file size in bytes
54 $sizeLimit = DI::config()->get('system', 'maximagesize');
56 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
58 $result = $uploader->handleUpload();
60 // to pass data through iframe you will need to encode all html tags
61 $js_upload_jsonresponse = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
63 if (isset($result['error'])) {
64 Logger::info('mod/photos.php: photos_post(): error uploading photo: ' . $result['error']);
65 echo json_encode($result);
69 $js_upload_result = $result;
72 function js_upload_post_file(App $a, array &$b)
74 global $js_upload_result;
76 $result = $js_upload_result;
78 $b['src'] = $result['path'];
79 $b['filename'] = $result['filename'];
80 $b['filesize'] = filesize($b['src']);
84 function js_upload_post_end(App $a, int &$b)
86 global $js_upload_jsonresponse;
88 Logger::notice('upload_post_end');
89 if (!empty($js_upload_jsonresponse)) {
90 echo $js_upload_jsonresponse;
96 * Handle file uploads via XMLHttpRequest
98 class qqUploadedFileXhr
100 private $pathnm = '';
103 * Save the file in the temp dir.
105 * @return boolean TRUE on success
109 $input = fopen('php://input', 'r');
111 $upload_dir = DI::config()->get('system', 'tempdir');
113 $upload_dir = sys_get_temp_dir();
115 $this->pathnm = tempnam($upload_dir, 'frn');
117 $temp = fopen($this->pathnm, 'w');
118 $realSize = stream_copy_to_stream($input, $temp);
123 if ($realSize != $this->getSize()) {
131 return $this->pathnm;
136 return $_GET['qqfile'];
141 if (isset($_SERVER['CONTENT_LENGTH'])) {
142 return (int)$_SERVER['CONTENT_LENGTH'];
144 throw new Exception('Getting content length is not supported.');
150 * Handle file uploads via regular form post (uses the $_FILES array)
152 class qqUploadedFileForm
155 * Save the file to the specified path
157 * @return boolean TRUE on success
166 return $_FILES['qqfile']['tmp_name'];
171 return $_FILES['qqfile']['name'];
176 return $_FILES['qqfile']['size'];
182 private $allowedExtensions = [];
183 private $sizeLimit = 10485760;
186 function __construct(array $allowedExtensions = [], $sizeLimit = 10485760)
188 $allowedExtensions = array_map('strtolower', $allowedExtensions);
190 $this->allowedExtensions = $allowedExtensions;
191 $this->sizeLimit = $sizeLimit;
193 if (isset($_GET['qqfile'])) {
194 $this->file = new qqUploadedFileXhr();
195 } elseif (isset($_FILES['qqfile'])) {
196 $this->file = new qqUploadedFileForm();
203 private function toBytes($str)
206 $last = strtolower($str[strlen($str) - 1]);
219 * Returns array('success'=>true) or array('error'=>'error message')
221 function handleUpload()
224 return ['error' => DI::l10n()->t('No files were uploaded.')];
227 $size = $this->file->getSize();
230 return ['error' => DI::l10n()->t('Uploaded file is empty')];
233 // if ($size > $this->sizeLimit) {
235 // return array('error' => DI::l10n()->t('Uploaded file is too large'));
239 $maximagesize = DI::config()->get('system', 'maximagesize');
241 if (($maximagesize) && ($size > $maximagesize)) {
242 return ['error' => DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize))];
245 $pathinfo = pathinfo($this->file->getName());
246 $filename = $pathinfo['filename'];
248 if (!isset($pathinfo['extension'])) {
249 Logger::warning('extension isn\'t set.', ['filename' => $filename]);
251 $ext = $pathinfo['extension'] ?? '';
253 if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
254 return ['error' => DI::l10n()->t('File has an invalid extension, it should be one of %s.', implode(', ', $this->allowedExtensions))];
257 if ($this->file->save()) {
260 'path' => $this->file->getPath(),
261 'filename' => $filename . '.' . $ext
265 'error' => DI::l10n()->t('Upload was cancelled, or server error encountered'),
266 'path' => $this->file->getPath(),
267 'filename' => $filename . '.' . $ext