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\Config;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Renderer;
17 function js_upload_install()
19 Hook::register('photo_upload_form', __FILE__, 'js_upload_form');
20 Hook::register('photo_post_init', __FILE__, 'js_upload_post_init');
21 Hook::register('photo_post_file', __FILE__, 'js_upload_post_file');
22 Hook::register('photo_post_end', __FILE__, 'js_upload_post_end');
25 function js_upload_form(App $a, array &$b)
27 $b['default_upload'] = false;
29 DI::page()->registerStylesheet('addon/js_upload/file-uploader/client/fileuploader.css');
30 DI::page()->registerFooterScript('addon/js_upload/file-uploader/client/fileuploader.js');
32 $tpl = Renderer::getMarkupTemplate('js_upload.tpl', 'addon/js_upload');
33 $b['addon_text'] .= Renderer::replaceMacros($tpl, [
34 '$upload_msg' => DI::l10n()->t('Select files for upload'),
35 '$drop_msg' => DI::l10n()->t('Drop files here to upload'),
36 '$cancel' => DI::l10n()->t('Cancel'),
37 '$failed' => DI::l10n()->t('Failed'),
38 '$post_url' => $b['post_url'],
39 '$maximagesize' => intval(DI::config()->get('system', 'maximagesize')),
43 function js_upload_post_init(App $a, &$b)
45 // list of valid extensions
46 $allowedExtensions = ['jpeg', 'gif', 'png', 'jpg'];
48 // max file size in bytes
49 $sizeLimit = DI::config()->get('system', 'maximagesize');
51 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
53 $result = $uploader->handleUpload();
55 // to pass data through iframe you will need to encode all html tags
56 $a->data['upload_jsonresponse'] = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
58 if (isset($result['error'])) {
59 Logger::log('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'], Logger::DEBUG);
60 echo json_encode($result);
64 $a->data['upload_result'] = $result;
67 function js_upload_post_file(App $a, &$b)
69 $result = $a->data['upload_result'];
71 $b['src'] = $result['path'];
72 $b['filename'] = $result['filename'];
73 $b['filesize'] = filesize($b['src']);
77 function js_upload_post_end(App $a, &$b)
79 Logger::log('upload_post_end');
80 if (!empty($a->data['upload_jsonresponse'])) {
81 echo $a->data['upload_jsonresponse'];
87 * Handle file uploads via XMLHttpRequest
89 class qqUploadedFileXhr
94 * Save the file in the temp dir.
96 * @return boolean TRUE on success
100 $input = fopen('php://input', 'r');
102 $upload_dir = DI::config()->get('system', 'tempdir');
104 $upload_dir = sys_get_temp_dir();
106 $this->pathnm = tempnam($upload_dir, 'frn');
108 $temp = fopen($this->pathnm, 'w');
109 $realSize = stream_copy_to_stream($input, $temp);
114 if ($realSize != $this->getSize()) {
122 return $this->pathnm;
127 return $_GET['qqfile'];
132 if (isset($_SERVER['CONTENT_LENGTH'])) {
133 return (int)$_SERVER['CONTENT_LENGTH'];
135 throw new Exception('Getting content length is not supported.');
141 * Handle file uploads via regular form post (uses the $_FILES array)
143 class qqUploadedFileForm
146 * Save the file to the specified path
148 * @return boolean TRUE on success
157 return $_FILES['qqfile']['tmp_name'];
162 return $_FILES['qqfile']['name'];
167 return $_FILES['qqfile']['size'];
173 private $allowedExtensions = [];
174 private $sizeLimit = 10485760;
177 function __construct(array $allowedExtensions = [], $sizeLimit = 10485760)
179 $allowedExtensions = array_map('strtolower', $allowedExtensions);
181 $this->allowedExtensions = $allowedExtensions;
182 $this->sizeLimit = $sizeLimit;
184 if (isset($_GET['qqfile'])) {
185 $this->file = new qqUploadedFileXhr();
186 } elseif (isset($_FILES['qqfile'])) {
187 $this->file = new qqUploadedFileForm();
194 private function toBytes($str)
197 $last = strtolower($str[strlen($str) - 1]);
210 * Returns array('success'=>true) or array('error'=>'error message')
212 function handleUpload()
215 return ['error' => DI::l10n()->t('No files were uploaded.')];
218 $size = $this->file->getSize();
221 return ['error' => DI::l10n()->t('Uploaded file is empty')];
224 // if ($size > $this->sizeLimit) {
226 // return array('error' => DI::l10n()->t('Uploaded file is too large'));
230 $maximagesize = DI::config()->get('system', 'maximagesize');
232 if (($maximagesize) && ($size > $maximagesize)) {
233 return ['error' => DI::l10n()->t('Image exceeds size limit of ') . $maximagesize];
237 $pathinfo = pathinfo($this->file->getName());
238 $filename = $pathinfo['filename'];
240 if (!isset($pathinfo['extension'])) {
241 Logger::warning('extension isn\'t set.', ['filename' => $filename]);
243 $ext = $pathinfo['extension'] ?? '';
245 if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
246 $these = implode(', ', $this->allowedExtensions);
247 return ['error' => DI::l10n()->t('File has an invalid extension, it should be one of ') . $these . '.'];
250 if ($this->file->save()) {
253 'path' => $this->file->getPath(),
254 'filename' => $filename . '.' . $ext
258 'error' => DI::l10n()->t('Upload was cancelled, or server error encountered'),
259 'path' => $this->file->getPath(),
260 'filename' => $filename . '.' . $ext