<?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
*
*/
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);
* 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]);
+ }
+
+ $text = $callback($return ?? $text) ?? '';
// Restore code blocks
$text = preg_replace_callback('/«block-' . $executionId . '-([0-9]+)»/iU',
}
/**
- * This function converts a PHP's shorhand notation string for file sizes in to an integer number of total bytes.
+ * This function converts a file size string written in PHP's shorthand notation 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 $shorthand;
}
+ /**
+ * Converts an URL in a nicer format (without the scheme and possibly shortened)
+ *
+ * @param string $url URL that is about to be reformatted
+ * @return string reformatted link
+ */
+ public static function getStyledURL(string $url): string
+ {
+ $parts = parse_url($url);
+ $scheme = [$parts['scheme'] . '://www.', $parts['scheme'] . '://'];
+ $styled_url = str_replace($scheme, '', $url);
+
+ if (strlen($styled_url) > 30) {
+ $styled_url = substr($styled_url, 0, 30) . "…";
+ }
+
+ return $styled_url;
+ }
}