| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* JCH Optimize - Performs several front-end optimizations for fast downloads |
| 5 |
* |
| 6 |
* @package jchoptimize/core |
| 7 |
* @author Samuel Marshall <samuel@jch-optimize.net> |
| 8 |
* @copyright Copyright (c) 2022 Samuel Marshall / JCH Optimize |
| 9 |
* @license GNU/GPLv3, or later. See LICENSE file |
| 10 |
* |
| 11 |
* If LICENSE file missing, see <http://www.gnu.org/licenses/>. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace JchOptimize\Core; |
| 15 |
|
| 16 |
use _JchOptimizeVendor\V91\Psr\Http\Message\UriInterface; |
| 17 |
use JchOptimize\Core\Uri\UriComparator; |
| 18 |
|
| 19 |
use function defined; |
| 20 |
use function htmlentities; |
| 21 |
use function preg_replace; |
| 22 |
use function strlen; |
| 23 |
use function substr; |
| 24 |
|
| 25 |
defined('_JCH_EXEC') or die('Restricted access'); |
| 26 |
|
| 27 |
class FileUtils |
| 28 |
{ |
| 29 |
/** |
| 30 |
* Prepare a representation of a file URL or value for display, possibly truncated |
| 31 |
* |
| 32 |
* @param UriInterface|null $uri The string being prepared |
| 33 |
* @param string $content |
| 34 |
* @param bool $truncate If true will be truncated at specified length, prepending with an epsilon |
| 35 |
* @param int $length The length in number of characters. |
| 36 |
* |
| 37 |
* @return string |
| 38 |
* @deprecated |
| 39 |
*/ |
| 40 |
public function prepareForDisplay( |
| 41 |
?UriInterface $uri = null, |
| 42 |
string $content = '', |
| 43 |
bool $truncate = true, |
| 44 |
int $length = 60 |
| 45 |
): string { |
| 46 |
if ($uri) { |
| 47 |
return self::prepareFileForDisplay($uri, $truncate, $length); |
| 48 |
} else { |
| 49 |
return self::prepareContentForDisplay($content, $truncate, $length); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public static function prepareFileForDisplay(UriInterface $uri, bool $truncate = true, int $length = 65): string |
| 54 |
{ |
| 55 |
if (!$truncate) { |
| 56 |
return (string) $uri; |
| 57 |
} |
| 58 |
|
| 59 |
$newUri = clone $uri; |
| 60 |
$eps = ''; |
| 61 |
$preEps = ''; |
| 62 |
$path = $newUri->getPath(); |
| 63 |
|
| 64 |
if ($path === '/' && ($query = $newUri->getQuery()) !== '') { |
| 65 |
$queryLen = strlen($query); |
| 66 |
if ($queryLen > $length - 1) { |
| 67 |
$query = substr($query, 0, -($queryLen - $length + 4)) . '...'; |
| 68 |
} |
| 69 |
|
| 70 |
$path .= '?' . $query; |
| 71 |
} |
| 72 |
|
| 73 |
if (UriComparator::isCrossOrigin($newUri)) { |
| 74 |
$domain = $newUri->withPort(null)->withPath('')->withQuery('')->withFragment(''); |
| 75 |
$length -= strlen($domain); |
| 76 |
$preEps = $domain; |
| 77 |
} |
| 78 |
|
| 79 |
if (strlen($path) > $length) { |
| 80 |
$path = substr($path, -$length); |
| 81 |
$preEps = $preEps !== '' ? $preEps . '/' : ''; |
| 82 |
$eps = '...'; |
| 83 |
} |
| 84 |
|
| 85 |
return $preEps . $eps . $path; |
| 86 |
} |
| 87 |
|
| 88 |
public static function prepareContentForDisplay(string $content, bool $truncate = true, int $length = 60): string |
| 89 |
{ |
| 90 |
if (!$truncate) { |
| 91 |
return $content; |
| 92 |
} |
| 93 |
|
| 94 |
if (strlen($content) > $length) { |
| 95 |
$content = substr($content, 0, $length); |
| 96 |
$content = $content . '...'; |
| 97 |
} |
| 98 |
|
| 99 |
return htmlentities($content); |
| 100 |
} |
| 101 |
|
| 102 |
public static function prepareContentValue(string $content, int $length = 60): string |
| 103 |
{ |
| 104 |
return htmlentities(substr($content, 0, $length)); |
| 105 |
} |
| 106 |
|
| 107 |
public static function prepareUrlValue(UriInterface $uri): string |
| 108 |
{ |
| 109 |
if (!UriComparator::isCrossOrigin($uri)) { |
| 110 |
$uri = $uri->withScheme('')->withHost('')->withPort(null)->withUserInfo(''); |
| 111 |
} |
| 112 |
|
| 113 |
$query = ''; |
| 114 |
if ($uri->getPath() === '/' && ($query = $uri->getQuery()) !== '') { |
| 115 |
$queryLen = strlen($query); |
| 116 |
$query = substr($query, 0, -($queryLen - 60)); |
| 117 |
} |
| 118 |
|
| 119 |
return (string) $uri->withQuery($query)->withFragment(''); |
| 120 |
} |
| 121 |
|
| 122 |
public static function prepareOriginValue(UriInterface $uri): string |
| 123 |
{ |
| 124 |
return (string) $uri->withPath('')->withQuery('')->withFragment(''); |
| 125 |
} |
| 126 |
} |
| 127 |
|