4 * Description: JavaScript photo/image uploader. 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;
16 function js_upload_install()
18 Hook::register('photo_upload_form', __FILE__, 'js_upload_form');
19 Hook::register('photo_post_init', __FILE__, 'js_upload_post_init');
20 Hook::register('photo_post_file', __FILE__, 'js_upload_post_file');
21 Hook::register('photo_post_end', __FILE__, 'js_upload_post_end');
24 function js_upload_form(App $a, array &$b)
26 $b['default_upload'] = false;
28 DI::page()->registerStylesheet('addon/js_upload/file-uploader/client/fileuploader.css');
29 DI::page()->registerFooterScript('addon/js_upload/file-uploader/client/fileuploader.js');
31 $tpl = Renderer::getMarkupTemplate('js_upload.tpl', 'addon/js_upload');
32 $b['addon_text'] .= Renderer::replaceMacros($tpl, [
33 '$upload_msg' => DI::l10n()->t('Select files for upload'),
34 '$drop_msg' => DI::l10n()->t('Drop files here to upload'),
35 '$cancel' => DI::l10n()->t('Cancel'),
36 '$failed' => DI::l10n()->t('Failed'),
37 '$post_url' => $b['post_url'],
38 '$maximagesize' => intval(DI::config()->get('system', 'maximagesize')),
42 function js_upload_post_init(App $a, &$b)
44 // list of valid extensions
45 $allowedExtensions = ['jpeg', 'gif', 'png', 'jpg'];
47 // max file size in bytes
48 $sizeLimit = DI::config()->get('system', 'maximagesize');
50 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
52 $result = $uploader->handleUpload();
54 // to pass data through iframe you will need to encode all html tags
55 $a->data['upload_jsonresponse'] = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
57 if (isset($result['error'])) {
58 Logger::log('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'], Logger::DEBUG);
59 echo json_encode($result);
63 $a->data['upload_result'] = $result;
66 function js_upload_post_file(App $a, &$b)
68 $result = $a->data['upload_result'];
70 $b['src'] = $result['path'];
71 $b['filename'] = $result['filename'];
72 $b['filesize'] = filesize($b['src']);
76 function js_upload_post_end(App $a, &$b)
78 Logger::log('upload_post_end');
79 if (!empty($a->data['upload_jsonresponse'])) {
80 echo $a->data['upload_jsonresponse'];
86 * Handle file uploads via XMLHttpRequest
88 class qqUploadedFileXhr
93 * Save the file in the temp dir.
95 * @return boolean TRUE on success
99 $input = fopen('php://input', 'r');
101 $upload_dir = DI::config()->get('system', 'tempdir');
103 $upload_dir = sys_get_temp_dir();
105 $this->pathnm = tempnam($upload_dir, 'frn');
107 $temp = fopen($this->pathnm, 'w');
108 $realSize = stream_copy_to_stream($input, $temp);
113 if ($realSize != $this->getSize()) {
121 return $this->pathnm;
126 return $_GET['qqfile'];
131 if (isset($_SERVER['CONTENT_LENGTH'])) {
132 return (int)$_SERVER['CONTENT_LENGTH'];
134 throw new Exception('Getting content length is not supported.');
140 * Handle file uploads via regular form post (uses the $_FILES array)
142 class qqUploadedFileForm
145 * Save the file to the specified path
147 * @return boolean TRUE on success
156 return $_FILES['qqfile']['tmp_name'];
161 return $_FILES['qqfile']['name'];
166 return $_FILES['qqfile']['size'];
172 private $allowedExtensions = [];
173 private $sizeLimit = 10485760;
176 function __construct(array $allowedExtensions = [], $sizeLimit = 10485760)
178 $allowedExtensions = array_map('strtolower', $allowedExtensions);
180 $this->allowedExtensions = $allowedExtensions;
181 $this->sizeLimit = $sizeLimit;
183 if (isset($_GET['qqfile'])) {
184 $this->file = new qqUploadedFileXhr();
185 } elseif (isset($_FILES['qqfile'])) {
186 $this->file = new qqUploadedFileForm();
193 private function toBytes($str)
196 $last = strtolower($str[strlen($str) - 1]);
209 * Returns array('success'=>true) or array('error'=>'error message')
211 function handleUpload()
214 return ['error' => DI::l10n()->t('No files were uploaded.')];
217 $size = $this->file->getSize();
220 return ['error' => DI::l10n()->t('Uploaded file is empty')];
223 // if ($size > $this->sizeLimit) {
225 // return array('error' => DI::l10n()->t('Uploaded file is too large'));
229 $maximagesize = DI::config()->get('system', 'maximagesize');
231 if (($maximagesize) && ($size > $maximagesize)) {
232 return ['error' => DI::l10n()->t('Image exceeds size limit of ') . $maximagesize];
236 $pathinfo = pathinfo($this->file->getName());
237 $filename = $pathinfo['filename'];
239 if (!isset($pathinfo['extension'])) {
240 Logger::warning('extension isn\'t set.', ['filename' => $filename]);
242 $ext = $pathinfo['extension'] ?? '';
244 if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
245 $these = implode(', ', $this->allowedExtensions);
246 return ['error' => DI::l10n()->t('File has an invalid extension, it should be one of ') . $these . '.'];
249 if ($this->file->save()) {
252 'path' => $this->file->getPath(),
253 'filename' => $filename . '.' . $ext
257 'error' => DI::l10n()->t('Upload was cancelled, or server error encountered'),
258 'path' => $this->file->getPath(),
259 'filename' => $filename . '.' . $ext