<?php
/**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Friendica\Content\ContactSelector;
use Friendica\Core\Logger;
+use Friendica\Core\System;
+use ParagonIE\ConstantTime\Base64;
/**
* This class handles string functions
*/
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);
* @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, '+/', '-_');
}
/**
* @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, '-_', '+/'));
}
/**
* Returns the regular expression string to match URLs in a given text
*
* @return string
- * @see https://daringfireball.net/2010/07/improved_regex_for_matching_urls
*/
public static function autoLinkRegEx(): string
{
return '@
-(?<![=\'\]"/]) # Not preceded by [, =, \', ], ", /
+(?<![=\'\]"/]) # Not preceded by [, =, \', ], ", /
\b
-( # Capture 1: entire matched URL
- https?:// # http or https protocol
+( # Capture 1: entire matched URL
+ ' . self::linkRegEx() . '
+)@xiu';
+ }
+
+ /**
+ * Returns the regular expression string to match only an HTTP URL
+ *
+ * @return string
+ */
+ public static function onlyLinkRegEx(): string
+ {
+ return '@^' . self::linkRegEx() . '$@xiu';
+ }
+
+ /**
+ * @return string
+ * @see https://daringfireball.net/2010/07/improved_regex_for_matching_urls
+ */
+ private static function linkRegEx(): string
+ {
+ return 'https?:// # http or https protocol
(?:
- [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’.] # Domain can\'t start with a .
- [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’]+ # Domain can\'t end with a .
+ [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’.] # Domain can\'t start with a .
+ [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’]+ # Domain can\'t end with a .
\.
[^/\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’]+/? # Followed by a slash
)
- (?: # One or more:
- [^\s\xA0()<>]+ # Run of non-space, non-()<>
- | # or
- \(([^\s\xA0()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
- | # or
- [^\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars
- )*
-)@xiu';
+ (?: # One or more:
+ [^\s\xA0()<>]+ # Run of non-space, non-()<>
+ | # or
+ \(([^\s\xA0()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
+ | # or
+ [^\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars
+ )*';
}
/**
$blocks = [];
- $text = preg_replace_callback($regex,
+ $return = preg_replace_callback($regex,
function ($matches) use ($executionId, &$blocks) {
$return = '«block-' . $executionId . '-' . count($blocks) . '»';
$text
);
- $text = $callback($text) ?? '';
+ if (is_null($return)) {
+ Logger::notice('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',
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
+ * @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);
+
+ switch($last) {
+ case 'g':
+ $shorthand *= 1024;
+ case 'm':
+ $shorthand *= 1024;
+ case 'k':
+ $shorthand *= 1024;
+ }
+
+ return $shorthand;
+ }
+
}