]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Strings.php
Merge pull request #12390 from annando/fixes
[friendica.git] / src / Util / Strings.php
index 0972e44486afc811236bb572d4360689db824d93..379f2a25213dda4ed89fc8d688eb4dffb4c038f6 100644 (file)
@@ -23,6 +23,8 @@ namespace Friendica\Util;
 
 use Friendica\Content\ContactSelector;
 use Friendica\Core\Logger;
+use Friendica\Core\System;
+use ParagonIE\ConstantTime\Base64;
 
 /**
  * This class handles string functions
@@ -219,7 +221,12 @@ class Strings
         */
        public static function formatBytes(int $bytes, int $precision = 2): string
        {
-               $units = ['B', 'KB', 'MB', 'GB', 'TB'];
+               // If this method is called for an infinite (== unlimited) amount of bytes:
+               if ($bytes == INF) {
+                       return INF;
+               }
+
+               $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
                $bytes = max($bytes, 0);
                $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
                $pow = min($pow, count($units) - 1);
@@ -245,16 +252,17 @@ class Strings
         * @param string $s                                     URL to encode
         * @param boolean $strip_padding        Optional. Default false
         * @return string       Encoded URL
+        * @see https://web.archive.org/web/20160506073138/http://salmon-protocol.googlecode.com:80/svn/trunk/draft-panzer-magicsig-01.html#params
         */
        public static function base64UrlEncode(string $s, bool $strip_padding = false): string
        {
-               $s = strtr(base64_encode($s), '+/', '-_');
-
                if ($strip_padding) {
-                       $s = str_replace('=', '', $s);
+                       $s = Base64::encodeUnpadded($s);
+               } else {
+                       $s = Base64::encode($s);
                }
 
-               return $s;
+               return strtr($s, '+/', '-_');
        }
 
        /**
@@ -263,26 +271,11 @@ class Strings
         * @param string $s URL to decode
         * @return string       Decoded URL
         * @throws \Exception
+        * @see https://web.archive.org/web/20160506073138/http://salmon-protocol.googlecode.com:80/svn/trunk/draft-panzer-magicsig-01.html#params
         */
        public static function base64UrlDecode(string $s): string
        {
-               /*
-               *  // Placeholder for new rev of salmon which strips base64 padding.
-               *  // PHP base64_decode handles the un-padded input without requiring this step
-               *  // Uncomment if you find you need it.
-               *
-               *       $l = strlen($s);
-               *       if (!strpos($s,'=')) {
-               *               $m = $l % 4;
-               *               if ($m == 2)
-               *                       $s .= '==';
-               *               if ($m == 3)
-               *                       $s .= '=';
-               *       }
-               *
-               */
-
-               return base64_decode(strtr($s, '-_', '+/'));
+               return Base64::decode(strtr($s, '-_', '+/'));
        }
 
        /**
@@ -488,7 +481,7 @@ class Strings
 
                $blocks = [];
 
-               $text = preg_replace_callback($regex,
+               $return = preg_replace_callback($regex,
                        function ($matches) use ($executionId, &$blocks) {
                                $return = '«block-' . $executionId . '-' . count($blocks) . '»';
 
@@ -499,7 +492,11 @@ class Strings
                        $text
                );
 
-               $text = $callback($text) ?? '';
+               if (is_null($return)) {
+                       Logger::warning('Received null value from preg_replace_callback', ['text' => $text, 'regex' => $regex, 'blocks' => $blocks, 'executionId' => $executionId, 'callstack' => System::callstack(10)]);
+               }
+
+               $text = $callback($return ?? $text) ?? '';
 
                // Restore code blocks
                $text = preg_replace_callback('/«block-' . $executionId . '-([0-9]+)»/iU',