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\L10n;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
18 function js_upload_install()
20 Hook::register('photo_upload_form', __FILE__, 'js_upload_form');
21 Hook::register('photo_post_init', __FILE__, 'js_upload_post_init');
22 Hook::register('photo_post_file', __FILE__, 'js_upload_post_file');
23 Hook::register('photo_post_end', __FILE__, 'js_upload_post_end');
26 function js_upload_form(App $a, array &$b)
28 $b['default_upload'] = false;
30 DI::page()->registerStylesheet('addon/js_upload/file-uploader/client/fileuploader.css');
31 DI::page()->registerFooterScript('addon/js_upload/file-uploader/client/fileuploader.js');
33 $tpl = Renderer::getMarkupTemplate('js_upload.tpl', 'addon/js_upload');
34 $b['addon_text'] .= Renderer::replaceMacros($tpl, [
35 '$upload_msg' => DI::l10n()->t('Select files for upload'),
36 '$drop_msg' => DI::l10n()->t('Drop files here to upload'),
37 '$cancel' => DI::l10n()->t('Cancel'),
38 '$failed' => DI::l10n()->t('Failed'),
39 '$post_url' => $b['post_url'],
40 '$maximagesize' => intval(Config::get('system', 'maximagesize')),
44 function js_upload_post_init(App $a, &$b)
46 // list of valid extensions
47 $allowedExtensions = ['jpeg', 'gif', 'png', 'jpg'];
49 // max file size in bytes
50 $sizeLimit = Config::get('system', 'maximagesize');
52 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
54 $result = $uploader->handleUpload();
56 // to pass data through iframe you will need to encode all html tags
57 $a->data['upload_jsonresponse'] = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
59 if (isset($result['error'])) {
60 Logger::log('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'], Logger::DEBUG);
61 echo json_encode($result);
65 $a->data['upload_result'] = $result;
68 function js_upload_post_file(App $a, &$b)
70 $result = $a->data['upload_result'];
72 $b['src'] = $result['path'];
73 $b['filename'] = $result['filename'];
74 $b['filesize'] = filesize($b['src']);
78 function js_upload_post_end(App $a, &$b)
80 Logger::log('upload_post_end');
81 if (!empty($a->data['upload_jsonresponse'])) {
82 echo $a->data['upload_jsonresponse'];
88 * Handle file uploads via XMLHttpRequest
90 class qqUploadedFileXhr
95 * Save the file in the temp dir.
97 * @return boolean TRUE on success
101 $input = fopen('php://input', 'r');
103 $upload_dir = Config::get('system', 'tempdir');
105 $upload_dir = sys_get_temp_dir();
107 $this->pathnm = tempnam($upload_dir, 'frn');
109 $temp = fopen($this->pathnm, 'w');
110 $realSize = stream_copy_to_stream($input, $temp);
115 if ($realSize != $this->getSize()) {
123 return $this->pathnm;
128 return $_GET['qqfile'];
133 if (isset($_SERVER['CONTENT_LENGTH'])) {
134 return (int)$_SERVER['CONTENT_LENGTH'];
136 throw new Exception('Getting content length is not supported.');
142 * Handle file uploads via regular form post (uses the $_FILES array)
144 class qqUploadedFileForm
147 * Save the file to the specified path
149 * @return boolean TRUE on success
158 return $_FILES['qqfile']['tmp_name'];
163 return $_FILES['qqfile']['name'];
168 return $_FILES['qqfile']['size'];
174 private $allowedExtensions = [];
175 private $sizeLimit = 10485760;
178 function __construct(array $allowedExtensions = [], $sizeLimit = 10485760)
180 $allowedExtensions = array_map('strtolower', $allowedExtensions);
182 $this->allowedExtensions = $allowedExtensions;
183 $this->sizeLimit = $sizeLimit;
185 if (isset($_GET['qqfile'])) {
186 $this->file = new qqUploadedFileXhr();
187 } elseif (isset($_FILES['qqfile'])) {
188 $this->file = new qqUploadedFileForm();
195 private function toBytes($str)
198 $last = strtolower($str[strlen($str) - 1]);
211 * Returns array('success'=>true) or array('error'=>'error message')
213 function handleUpload()
216 return ['error' => DI::l10n()->t('No files were uploaded.')];
219 $size = $this->file->getSize();
222 return ['error' => DI::l10n()->t('Uploaded file is empty')];
225 // if ($size > $this->sizeLimit) {
227 // return array('error' => DI::l10n()->t('Uploaded file is too large'));
231 $maximagesize = Config::get('system', 'maximagesize');
233 if (($maximagesize) && ($size > $maximagesize)) {
234 return ['error' => DI::l10n()->t('Image exceeds size limit of ') . $maximagesize];
238 $pathinfo = pathinfo($this->file->getName());
239 $filename = $pathinfo['filename'];
241 if (!isset($pathinfo['extension'])) {
242 Logger::warning('extension isn\'t set.', ['filename' => $filename]);
244 $ext = $pathinfo['extension'] ?? '';
246 if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
247 $these = implode(', ', $this->allowedExtensions);
248 return ['error' => DI::l10n()->t('File has an invalid extension, it should be one of ') . $these . '.'];
251 if ($this->file->save()) {
254 'path' => $this->file->getPath(),
255 'filename' => $filename . '.' . $ext
259 'error' => DI::l10n()->t('Upload was cancelled, or server error encountered'),
260 'path' => $this->file->getPath(),
261 'filename' => $filename . '.' . $ext