]> git.mxchange.org Git - friendica.git/commitdiff
introduced getBytesFromShorthand function to covert from shorthand notation to int
authorMarek Bachmann <marek.bachmann@comtec.eecs.uni-kassel.de>
Sat, 26 Nov 2022 20:43:31 +0000 (21:43 +0100)
committerMarek Bachmann <marek.bachmann@comtec.eecs.uni-kassel.de>
Sat, 26 Nov 2022 20:43:31 +0000 (21:43 +0100)
mod/photos.php
src/Util/Strings.php

index 9fdddfca4474ea88b252dd3e73ee21074663ddd1..173823527673109743c8428a6b3828660e002b6f 100644 (file)
@@ -918,43 +918,19 @@ function photos_content(App $a)
                 * limit of Friendica ('maximagesize') or upload_max_filesize
                 * and outputs the lower one.
                */
-               // init output var in case no number value could be retrieved.
                $maximagesize_Mbytes = 0;
-               $sizelimitedby = '';
                {
                        // Get the relevant size limits for uploads. Abbreviated var names: MaxImageSize -> mis; upload_max_filesize -> umf
                        $mis_bytes = DI::config()->get('system', 'maximagesize');
-                       // get_cfg_var('upload_max_filesize') outputs a string in the shorthand notation, need to convert it to bytes
-                       $umf_string = get_cfg_var('upload_max_filesize');
-                       $umf_bytes = filter_var($umf_string, FILTER_SANITIZE_NUMBER_INT);
-                       switch (substr($umf_string, -1)) {
-                               // According to https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes only K, M, and G is used.
-                               case 'M':
-                                       $umf_bytes *= 10**6;
-                                       break;
-                               case 'K':
-                                       $umf_bytes *= 10**3;
-                                       break;
-                               case 'G':
-                                       $umf_bytes *= 10**9;
-                                       break;
-                       }
-                       if (is_numeric($mis_bytes) && is_numeric($umf_bytes)) {
-                               // When PHP is configured with upload_max_filesize less than maximagesize provide this lower limit.
-                               if ($umf_bytes < $mis_bytes) {
-                                       $maximagesize_Mbytes = ($umf_bytes / (10 ** 6));
-                                       $sizelimitedby = 'PHP.ini';
-                               } else {
-                                       $maximagesize_Mbytes = ($mis_bytes / (10 ** 6));
+                       $umf_bytes = Strings::getBytesFromShorthand(get_cfg_var('upload_max_filesize'));
 
-                               }
+                       if (is_numeric($mis_bytes)) {
+                               // When PHP is configured with upload_max_filesize less than maximagesize provide this lower limit.
+                               ($umf_bytes < $mis_bytes) ?
+                                       ($maximagesize_Mbytes = ($umf_bytes / (10 ** 6))) : ($maximagesize_Mbytes = ($mis_bytes / (10 ** 6)));
                        }
                }
-               $usage_message = DI::l10n()->t('The maximum accepted image size is %.3g MB', $maximagesize_Mbytes);
-               // Do we want to bother the end user with this information? If yes uncomment
-               //if (strlen($sizelimitedby) > 0) {
-               //      $usage_message .= ' ('. $sizelimitedby . ')';
-               //}
+               $usage_message = DI::l10n()->t('The maximum accepted image size is %.3g MB', Strings::getBytesFromShorthand(get_cfg_var('upload_max_filesize')));
 
                $tpl = Renderer::getMarkupTemplate('photos_upload.tpl');
 
index 84a8b6727a3d4d4383bf22a1c06d5b58d2682f24..8c22c321df5892e426a594c12de5ac3be05d1ac8 100644 (file)
@@ -515,4 +515,35 @@ class Strings
 
                return $text;
        }
+
+       /**
+        * This function converts a PHP's shorhand notation string for file sizes in to an integer number of total bytes.
+        * For example: The string for shorthand notation of '2M' (which is 2,097,152 Bytes) is converted to 2097152
+        * For more information about file size shorhand notation see:
+        * https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
+        * @param string $shorthand
+        * @return int
+        */
+       public static function getBytesFromShorthand(string $shorthand): int
+       {
+               $shorthand  = trim($shorthand);
+
+               if (is_numeric($shorthand))
+                       return $shorthand;
+
+               $last = strtolower($shorthand[strlen($shorthand)-1]);
+               $shorthand  = substr($shorthand, 0, -1); // necessary since PHP 7.1; otherwise optional
+
+               switch($last) {
+                       // The 'G' modifier is available since PHP 5.1.0
+                       case 'g':
+                               $shorthand *= 1024;
+                       case 'm':
+                               $shorthand *= 1024;
+                       case 'k':
+                               $shorthand *= 1024;
+               }
+
+               return $shorthand;
+       }
 }