]> git.mxchange.org Git - friendica-addons.git/blobdiff - js_upload/js_upload.php
Bluesky: Support personal data servers
[friendica-addons.git] / js_upload / js_upload.php
index 6a86575fe8e9a8ec7649d08b274ed1400329bdae..089cb7e214802739e5a921487bda2726adee421f 100644 (file)
@@ -14,6 +14,9 @@ use Friendica\Core\Renderer;
 use Friendica\DI;
 use Friendica\Util\Strings;
 
+global $js_upload_jsonresponse;
+global $js_upload_result;
+
 function js_upload_install()
 {
        Hook::register('photo_upload_form', __FILE__, 'js_upload_form');
@@ -22,12 +25,12 @@ function js_upload_install()
        Hook::register('photo_post_end', __FILE__, 'js_upload_post_end');
 }
 
-function js_upload_form(App $a, array &$b)
+function js_upload_form(array &$b)
 {
        $b['default_upload'] = false;
 
-       DI::page()->registerStylesheet('addon/js_upload/file-uploader/client/fileuploader.css');
-       DI::page()->registerFooterScript('addon/js_upload/file-uploader/client/fileuploader.js');
+       DI::page()->registerStylesheet(__DIR__ . '/file-uploader/client/fileuploader.css');
+       DI::page()->registerFooterScript(__DIR__ . '/file-uploader/client/fileuploader.js');
 
        $tpl = Renderer::getMarkupTemplate('js_upload.tpl', 'addon/js_upload');
        $b['addon_text'] .= Renderer::replaceMacros($tpl, [
@@ -36,37 +39,41 @@ function js_upload_form(App $a, array &$b)
                '$cancel' => DI::l10n()->t('Cancel'),
                '$failed' => DI::l10n()->t('Failed'),
                '$post_url' => $b['post_url'],
-               '$maximagesize' => intval(DI::config()->get('system', 'maximagesize')),
+               '$maximagesize' => Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize')),
        ]);
 }
 
-function js_upload_post_init(App $a, &$b)
+function js_upload_post_init(array &$b)
 {
+       global $js_upload_result, $js_upload_jsonresponse;
+
        // list of valid extensions
        $allowedExtensions = ['jpeg', 'gif', 'png', 'jpg'];
 
        // max file size in bytes
-       $sizeLimit = DI::config()->get('system', 'maximagesize');
+       $sizeLimit = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
 
        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
 
        $result = $uploader->handleUpload();
 
        // to pass data through iframe you will need to encode all html tags
-       $a->data['upload_jsonresponse'] = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
+       $js_upload_jsonresponse = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
 
        if (isset($result['error'])) {
-               Logger::log('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'], Logger::DEBUG);
+               Logger::info('mod/photos.php: photos_post(): error uploading photo: ' . $result['error']);
                echo json_encode($result);
                exit();
        }
 
-       $a->data['upload_result'] = $result;
+       $js_upload_result = $result;
 }
 
-function js_upload_post_file(App $a, &$b)
+function js_upload_post_file(array &$b)
 {
-       $result = $a->data['upload_result'];
+       global $js_upload_result;
+
+       $result = $js_upload_result;
 
        $b['src'] = $result['path'];
        $b['filename'] = $result['filename'];
@@ -74,11 +81,13 @@ function js_upload_post_file(App $a, &$b)
 
 }
 
-function js_upload_post_end(App $a, &$b)
+function js_upload_post_end(int &$b)
 {
-       Logger::log('upload_post_end');
-       if (!empty($a->data['upload_jsonresponse'])) {
-               echo $a->data['upload_jsonresponse'];
+       global $js_upload_jsonresponse;
+
+       Logger::notice('upload_post_end');
+       if (!empty($js_upload_jsonresponse)) {
+               echo $js_upload_jsonresponse;
                exit();
        }
 }
@@ -191,21 +200,6 @@ class qqFileUploader
 
        }
 
-       private function toBytes($str)
-       {
-               $val = trim($str);
-               $last = strtolower($str[strlen($str) - 1]);
-               switch ($last) {
-                       case 'g':
-                               $val *= 1024;
-                       case 'm':
-                               $val *= 1024;
-                       case 'k':
-                               $val *= 1024;
-               }
-               return $val;
-       }
-
        /**
         * Returns array('success'=>true) or array('error'=>'error message')
         */
@@ -227,7 +221,7 @@ class qqFileUploader
 //             }
 
 
-               $maximagesize = DI::config()->get('system', 'maximagesize');
+               $maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
 
                if (($maximagesize) && ($size > $maximagesize)) {
                        return ['error' => DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize))];