12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619 |
- <?php
- /**
- * @package Grav\Common
- *
- * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
- * @license MIT License; see LICENSE file for details.
- */
- namespace Grav\Common;
- use Grav\Common\Helpers\Truncator;
- use Grav\Common\Page\Interfaces\PageInterface;
- use Grav\Common\Markdown\Parsedown;
- use Grav\Common\Markdown\ParsedownExtra;
- use Grav\Common\Page\Markdown\Excerpts;
- use RocketTheme\Toolbox\Event\Event;
- use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
- abstract class Utils
- {
- protected static $nonces = [];
- protected const ROOTURL_REGEX = '{^((?:http[s]?:\/\/[^\/]+)|(?:\/\/[^\/]+))(.*)}';
- // ^((?:http[s]?:)?[\/]?(?:\/))
- /**
- * Simple helper method to make getting a Grav URL easier
- *
- * @param string|object $input
- * @param bool $domain
- * @param bool $fail_gracefully
- * @return bool|null|string
- */
- public static function url($input, $domain = false, $fail_gracefully = false)
- {
- if ((!is_string($input) && !method_exists($input, '__toString')) || !trim($input)) {
- if ($fail_gracefully) {
- $input = '/';
- } else {
- return false;
- }
- }
- $input = (string)$input;
- if (Uri::isExternal($input)) {
- return $input;
- }
- $grav = Grav::instance();
- /** @var Uri $uri */
- $uri = $grav['uri'];
- if (static::contains((string)$input, '://')) {
- /** @var UniformResourceLocator $locator */
- $locator = $grav['locator'];
- $parts = Uri::parseUrl($input);
- if (is_array($parts)) {
- // Make sure we always have scheme, host, port and path.
- $scheme = $parts['scheme'] ?? '';
- $host = $parts['host'] ?? '';
- $port = $parts['port'] ?? '';
- $path = $parts['path'] ?? '';
- if ($scheme && !$port) {
- // If URL has a scheme, we need to check if it's one of Grav streams.
- if (!$locator->schemeExists($scheme)) {
- // If scheme does not exists as a stream, assume it's external.
- return str_replace(' ', '%20', $input);
- }
- // Attempt to find the resource (because of parse_url() we need to put host back to path).
- $resource = $locator->findResource("{$scheme}://{$host}{$path}", false);
- if ($resource === false) {
- if (!$fail_gracefully) {
- return false;
- }
- // Return location where the file would be if it was saved.
- $resource = $locator->findResource("{$scheme}://{$host}{$path}", false, true);
- }
- } elseif ($host || $port) {
- // If URL doesn't have scheme but has host or port, it is external.
- return str_replace(' ', '%20', $input);
- }
- if (!empty($resource)) {
- // Add query string back.
- if (isset($parts['query'])) {
- $resource .= '?' . $parts['query'];
- }
- // Add fragment back.
- if (isset($parts['fragment'])) {
- $resource .= '#' . $parts['fragment'];
- }
- }
- } else {
- // Not a valid URL (can still be a stream).
- $resource = $locator->findResource($input, false);
- }
- } else {
- $root = $uri->rootUrl();
- if (static::startsWith($input, $root)) {
- $input = static::replaceFirstOccurrence($root, '', $input);
- }
- $input = ltrim($input, '/');
- $resource = $input;
- }
- if (!$fail_gracefully && $resource === false) {
- return false;
- }
- $domain = $domain ?: $grav['config']->get('system.absolute_urls', false);
- return rtrim($uri->rootUrl($domain), '/') . '/' . ($resource ?? '');
- }
- /**
- * Check if the $haystack string starts with the substring $needle
- *
- * @param string $haystack
- * @param string|string[] $needle
- * @param bool $case_sensitive
- *
- * @return bool
- */
- public static function startsWith($haystack, $needle, $case_sensitive = true)
- {
- $status = false;
- $compare_func = $case_sensitive ? 'mb_strpos' : 'mb_stripos';
- foreach ((array)$needle as $each_needle) {
- $status = $each_needle === '' || $compare_func($haystack, $each_needle) === 0;
- if ($status) {
- break;
- }
- }
- return $status;
- }
- /**
- * Check if the $haystack string ends with the substring $needle
- *
- * @param string $haystack
- * @param string|string[] $needle
- * @param bool $case_sensitive
- *
- * @return bool
- */
- public static function endsWith($haystack, $needle, $case_sensitive = true)
- {
- $status = false;
- $compare_func = $case_sensitive ? 'mb_strrpos' : 'mb_strripos';
- foreach ((array)$needle as $each_needle) {
- $expectedPosition = mb_strlen($haystack) - mb_strlen($each_needle);
- $status = $each_needle === '' || $compare_func($haystack, $each_needle, 0) === $expectedPosition;
- if ($status) {
- break;
- }
- }
- return $status;
- }
- /**
- * Check if the $haystack string contains the substring $needle
- *
- * @param string $haystack
- * @param string|string[] $needle
- * @param bool $case_sensitive
- *
- * @return bool
- */
- public static function contains($haystack, $needle, $case_sensitive = true)
- {
- $status = false;
- $compare_func = $case_sensitive ? 'mb_strpos' : 'mb_stripos';
- foreach ((array)$needle as $each_needle) {
- $status = $each_needle === '' || $compare_func($haystack, $each_needle) !== false;
- if ($status) {
- break;
- }
- }
- return $status;
- }
- /**
- * Function that can match wildcards
- *
- * match_wildcard('foo*', $test), // TRUE
- * match_wildcard('bar*', $test), // FALSE
- * match_wildcard('*bar*', $test), // TRUE
- * match_wildcard('**blob**', $test), // TRUE
- * match_wildcard('*a?d*', $test), // TRUE
- * match_wildcard('*etc**', $test) // TRUE
- *
- * @param string $wildcard_pattern
- * @param string $haystack
- * @return false|int
- */
- public static function matchWildcard($wildcard_pattern, $haystack) {
- $regex = str_replace(
- array("\*", "\?"), // wildcard chars
- array('.*','.'), // regexp chars
- preg_quote($wildcard_pattern, '/')
- );
- return preg_match('/^'.$regex.'$/is', $haystack);
- }
- /**
- * Returns the substring of a string up to a specified needle. if not found, return the whole haystack
- *
- * @param string $haystack
- * @param string $needle
- * @param bool $case_sensitive
- *
- * @return string
- */
- public static function substrToString($haystack, $needle, $case_sensitive = true)
- {
- $compare_func = $case_sensitive ? 'mb_strpos' : 'mb_stripos';
- if (static::contains($haystack, $needle, $case_sensitive)) {
- return mb_substr($haystack, 0, $compare_func($haystack, $needle, $case_sensitive));
- }
- return $haystack;
- }
- /**
- * Utility method to replace only the first occurrence in a string
- *
- * @param string $search
- * @param string $replace
- * @param string $subject
- *
- * @return string
- */
- public static function replaceFirstOccurrence($search, $replace, $subject)
- {
- if (!$search) {
- return $subject;
- }
- $pos = mb_strpos($subject, $search);
- if ($pos !== false) {
- $subject = static::mb_substr_replace($subject, $replace, $pos, mb_strlen($search));
- }
- return $subject;
- }
- /**
- * Utility method to replace only the last occurrence in a string
- *
- * @param string $search
- * @param string $replace
- * @param string $subject
- * @return string
- */
- public static function replaceLastOccurrence($search, $replace, $subject)
- {
- $pos = strrpos($subject, $search);
- if($pos !== false)
- {
- $subject = static::mb_substr_replace($subject, $replace, $pos, mb_strlen($search));
- }
- return $subject;
- }
- /**
- * Multibyte compatible substr_replace
- *
- * @param string $original
- * @param string $replacement
- * @param int $position
- * @param int $length
- * @return string
- */
- public static function mb_substr_replace($original, $replacement, $position, $length)
- {
- $startString = mb_substr($original, 0, $position, "UTF-8");
- $endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8");
- return $startString . $replacement . $endString;
- }
- /**
- * Merge two objects into one.
- *
- * @param object $obj1
- * @param object $obj2
- *
- * @return object
- */
- public static function mergeObjects($obj1, $obj2)
- {
- return (object)array_merge((array)$obj1, (array)$obj2);
- }
- /**
- * Lowercase an entire array. Useful when combined with `in_array()`
- *
- * @param array $a
- * @return array|false
- */
- public static function arrayLower(Array $a)
- {
- return array_map('mb_strtolower', $a);
- }
- /**
- * Simple function to remove item/s in an array by value
- *
- * @param $search array
- * @param $value string|array
- * @return array
- */
- public static function arrayRemoveValue(Array $search, $value)
- {
- foreach ((array) $value as $val) {
- $key = array_search($val, $search);
- if ($key !== false) {
- unset($search[$key]);
- }
- }
- return $search;
- }
- /**
- * Recursive Merge with uniqueness
- *
- * @param array $array1
- * @param array $array2
- * @return array
- */
- public static function arrayMergeRecursiveUnique($array1, $array2)
- {
- if (empty($array1)) {
- // Optimize the base case
- return $array2;
- }
- foreach ($array2 as $key => $value) {
- if (is_array($value) && isset($array1[$key]) && is_array($array1[$key])) {
- $value = static::arrayMergeRecursiveUnique($array1[$key], $value);
- }
- $array1[$key] = $value;
- }
- return $array1;
- }
- /**
- * Returns an array with the differences between $array1 and $array2
- *
- * @param array $array1
- * @param array $array2
- * @return array
- */
- public static function arrayDiffMultidimensional($array1, $array2)
- {
- $result = array();
- foreach ($array1 as $key => $value) {
- if (!is_array($array2) || !array_key_exists($key, $array2)) {
- $result[$key] = $value;
- continue;
- }
- if (is_array($value)) {
- $recursiveArrayDiff = static::ArrayDiffMultidimensional($value, $array2[$key]);
- if (count($recursiveArrayDiff)) {
- $result[$key] = $recursiveArrayDiff;
- }
- continue;
- }
- if ($value != $array2[$key]) {
- $result[$key] = $value;
- }
- }
- return $result;
- }
- /**
- * Array combine but supports different array lengths
- *
- * @param array $arr1
- * @param array $arr2
- * @return array|false
- */
- public static function arrayCombine($arr1, $arr2)
- {
- $count = min(count($arr1), count($arr2));
- return array_combine(array_slice($arr1, 0, $count), array_slice($arr2, 0, $count));
- }
- /**
- * Array is associative or not
- *
- * @param array $arr
- * @return bool
- */
- public static function arrayIsAssociative($arr)
- {
- if ([] === $arr) {
- return false;
- }
- return array_keys($arr) !== range(0, count($arr) - 1);
- }
- /**
- * Return the Grav date formats allowed
- *
- * @return array
- */
- public static function dateFormats()
- {
- $now = new \DateTime();
- $date_formats = [
- 'd-m-Y H:i' => 'd-m-Y H:i (e.g. '.$now->format('d-m-Y H:i').')',
- 'Y-m-d H:i' => 'Y-m-d H:i (e.g. '.$now->format('Y-m-d H:i').')',
- 'm/d/Y h:i a' => 'm/d/Y h:i a (e.g. '.$now->format('m/d/Y h:i a').')',
- 'H:i d-m-Y' => 'H:i d-m-Y (e.g. '.$now->format('H:i d-m-Y').')',
- 'h:i a m/d/Y' => 'h:i a m/d/Y (e.g. '.$now->format('h:i a m/d/Y').')',
- ];
- $default_format = Grav::instance()['config']->get('system.pages.dateformat.default');
- if ($default_format) {
- $date_formats = array_merge([$default_format => $default_format.' (e.g. '.$now->format($default_format).')'], $date_formats);
- }
- return $date_formats;
- }
- /**
- * Get current date/time
- *
- * @param string|null $default_format
- * @return string
- * @throws \Exception
- */
- public static function dateNow($default_format = null)
- {
- $now = new \DateTime();
- if (is_null($default_format)) {
- $default_format = Grav::instance()['config']->get('system.pages.dateformat.default');
- }
- return $now->format($default_format);
- }
- /**
- * Truncate text by number of characters but can cut off words.
- *
- * @param string $string
- * @param int $limit Max number of characters.
- * @param bool $up_to_break truncate up to breakpoint after char count
- * @param string $break Break point.
- * @param string $pad Appended padding to the end of the string.
- *
- * @return string
- */
- public static function truncate($string, $limit = 150, $up_to_break = false, $break = ' ', $pad = '…')
- {
- // return with no change if string is shorter than $limit
- if (mb_strlen($string) <= $limit) {
- return $string;
- }
- // is $break present between $limit and the end of the string?
- if ($up_to_break && false !== ($breakpoint = mb_strpos($string, $break, $limit))) {
- if ($breakpoint < mb_strlen($string) - 1) {
- $string = mb_substr($string, 0, $breakpoint) . $pad;
- }
- } else {
- $string = mb_substr($string, 0, $limit) . $pad;
- }
- return $string;
- }
- /**
- * Truncate text by number of characters in a "word-safe" manor.
- *
- * @param string $string
- * @param int $limit
- *
- * @return string
- */
- public static function safeTruncate($string, $limit = 150)
- {
- return static::truncate($string, $limit, true);
- }
- /**
- * Truncate HTML by number of characters. not "word-safe"!
- *
- * @param string $text
- * @param int $length in characters
- * @param string $ellipsis
- *
- * @return string
- */
- public static function truncateHtml($text, $length = 100, $ellipsis = '...')
- {
- return Truncator::truncateLetters($text, $length, $ellipsis);
- }
- /**
- * Truncate HTML by number of characters in a "word-safe" manor.
- *
- * @param string $text
- * @param int $length in words
- * @param string $ellipsis
- *
- * @return string
- */
- public static function safeTruncateHtml($text, $length = 25, $ellipsis = '...')
- {
- return Truncator::truncateWords($text, $length, $ellipsis);
- }
- /**
- * Generate a random string of a given length
- *
- * @param int $length
- *
- * @return string
- */
- public static function generateRandomString($length = 5)
- {
- return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $length);
- }
- /**
- * Provides the ability to download a file to the browser
- *
- * @param string $file the full path to the file to be downloaded
- * @param bool $force_download as opposed to letting browser choose if to download or render
- * @param int $sec Throttling, try 0.1 for some speed throttling of downloads
- * @param int $bytes Size of chunks to send in bytes. Default is 1024
- * @throws \Exception
- */
- public static function download($file, $force_download = true, $sec = 0, $bytes = 1024)
- {
- if (file_exists($file)) {
- // fire download event
- Grav::instance()->fireEvent('onBeforeDownload', new Event(['file' => $file]));
- $file_parts = pathinfo($file);
- $mimetype = static::getMimeByExtension($file_parts['extension']);
- $size = filesize($file); // File size
- // clean all buffers
- while (ob_get_level()) {
- ob_end_clean();
- }
- // required for IE, otherwise Content-Disposition may be ignored
- if (ini_get('zlib.output_compression')) {
- ini_set('zlib.output_compression', 'Off');
- }
- header('Content-Type: ' . $mimetype);
- header('Accept-Ranges: bytes');
- if ($force_download) {
- // output the regular HTTP headers
- header('Content-Disposition: attachment; filename="' . $file_parts['basename'] . '"');
- }
- // multipart-download and download resuming support
- if (isset($_SERVER['HTTP_RANGE'])) {
- list($a, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
- list($range) = explode(',', $range, 2);
- list($range, $range_end) = explode('-', $range);
- $range = (int)$range;
- if (!$range_end) {
- $range_end = $size - 1;
- } else {
- $range_end = (int)$range_end;
- }
- $new_length = $range_end - $range + 1;
- header('HTTP/1.1 206 Partial Content');
- header("Content-Length: {$new_length}");
- header("Content-Range: bytes {$range}-{$range_end}/{$size}");
- } else {
- $range = 0;
- $new_length = $size;
- header('Content-Length: ' . $size);
- if (Grav::instance()['config']->get('system.cache.enabled')) {
- $expires = Grav::instance()['config']->get('system.pages.expires');
- if ($expires > 0) {
- $expires_date = gmdate('D, d M Y H:i:s T', time() + $expires);
- header('Cache-Control: max-age=' . $expires);
- header('Expires: ' . $expires_date);
- header('Pragma: cache');
- }
- header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', filemtime($file)));
- // Return 304 Not Modified if the file is already cached in the browser
- if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
- strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($file))
- {
- header('HTTP/1.1 304 Not Modified');
- exit();
- }
- }
- }
- /* output the file itself */
- $chunksize = $bytes * 8; //you may want to change this
- $bytes_send = 0;
- $fp = @fopen($file, 'rb');
- if ($fp) {
- if ($range) {
- fseek($fp, $range);
- }
- while (!feof($fp) && (!connection_aborted()) && ($bytes_send < $new_length) ) {
- $buffer = fread($fp, $chunksize);
- echo($buffer); //echo($buffer); // is also possible
- flush();
- usleep($sec * 1000000);
- $bytes_send += strlen($buffer);
- }
- fclose($fp);
- } else {
- throw new \RuntimeException('Error - can not open file.');
- }
- exit;
- }
- }
- /**
- * Return the mimetype based on filename extension
- *
- * @param string $extension Extension of file (eg "txt")
- * @param string $default
- *
- * @return string
- */
- public static function getMimeByExtension($extension, $default = 'application/octet-stream')
- {
- $extension = strtolower($extension);
- // look for some standard types
- switch ($extension) {
- case null:
- return $default;
- case 'json':
- return 'application/json';
- case 'html':
- return 'text/html';
- case 'atom':
- return 'application/atom+xml';
- case 'rss':
- return 'application/rss+xml';
- case 'xml':
- return 'application/xml';
- }
- $media_types = Grav::instance()['config']->get('media.types');
- if (isset($media_types[$extension])) {
- if (isset($media_types[$extension]['mime'])) {
- return $media_types[$extension]['mime'];
- }
- }
- return $default;
- }
- /**
- * Get all the mimetypes for an array of extensions
- *
- * @param array $extensions
- * @return array
- */
- public static function getMimeTypes(array $extensions)
- {
- $mimetypes = [];
- foreach ($extensions as $extension) {
- $mimetype = static::getMimeByExtension($extension, false);
- if ($mimetype && !in_array($mimetype, $mimetypes)) {
- $mimetypes[] = $mimetype;
- }
- }
- return $mimetypes;
- }
- /**
- * Return the mimetype based on filename extension
- *
- * @param string $mime mime type (eg "text/html")
- * @param string $default default value
- *
- * @return string
- */
- public static function getExtensionByMime($mime, $default = 'html')
- {
- $mime = strtolower($mime);
- // look for some standard mime types
- switch ($mime) {
- case '*/*':
- case 'text/*':
- case 'text/html':
- return 'html';
- case 'application/json':
- return 'json';
- case 'application/atom+xml':
- return 'atom';
- case 'application/rss+xml':
- return 'rss';
- case 'application/xml':
- return 'xml';
- }
- $media_types = (array)Grav::instance()['config']->get('media.types');
- foreach ($media_types as $extension => $type) {
- if ($extension === 'defaults') {
- continue;
- }
- if (isset($type['mime']) && $type['mime'] === $mime) {
- return $extension;
- }
- }
- return $default;
- }
- /**
- * Get all the extensions for an array of mimetypes
- *
- * @param array $mimetypes
- * @return array
- */
- public static function getExtensions(array $mimetypes)
- {
- $extensions = [];
- foreach ($mimetypes as $mimetype) {
- $extension = static::getExtensionByMime($mimetype, false);
- if ($extension && !\in_array($extension, $extensions, true)) {
- $extensions[] = $extension;
- }
- }
- return $extensions;
- }
- /**
- * Return the mimetype based on filename
- *
- * @param string $filename Filename or path to file
- * @param string $default default value
- *
- * @return string
- */
- public static function getMimeByFilename($filename, $default = 'application/octet-stream')
- {
- return static::getMimeByExtension(pathinfo($filename, PATHINFO_EXTENSION), $default);
- }
- /**
- * Return the mimetype based on existing local file
- *
- * @param string $filename Path to the file
- *
- * @return string|bool
- */
- public static function getMimeByLocalFile($filename, $default = 'application/octet-stream')
- {
- $type = false;
- // For local files we can detect type by the file content.
- if (!stream_is_local($filename) || !file_exists($filename)) {
- return false;
- }
- // Prefer using finfo if it exists.
- if (\extension_loaded('fileinfo')) {
- $finfo = finfo_open(FILEINFO_SYMLINK | FILEINFO_MIME_TYPE);
- $type = finfo_file($finfo, $filename);
- finfo_close($finfo);
- } else {
- // Fall back to use getimagesize() if it is available (not recommended, but better than nothing)
- $info = @getimagesize($filename);
- if ($info) {
- $type = $info['mime'];
- }
- }
- return $type ?: static::getMimeByFilename($filename, $default);
- }
- /**
- * Returns true if filename is considered safe.
- *
- * @param string $filename
- * @return bool
- */
- public static function checkFilename($filename)
- {
- $dangerous_extensions = Grav::instance()['config']->get('security.uploads_dangerous_extensions', []);
- array_walk($dangerous_extensions, function(&$val) {
- $val = '.' . $val;
- });
- $extension = '.' . pathinfo($filename, PATHINFO_EXTENSION);
- return !(
- // Empty filenames are not allowed.
- !$filename
- // Filename should not contain horizontal/vertical tabs, newlines, nils or back/forward slashes.
- || strtr($filename, "\t\v\n\r\0\\/", '_______') !== $filename
- // Filename should not start or end with dot or space.
- || trim($filename, '. ') !== $filename
- // Filename should not contain .php in it.
- || static::contains($extension, $dangerous_extensions)
- );
- }
- /**
- * Normalize path by processing relative `.` and `..` syntax and merging path
- *
- * @param string $path
- *
- * @return string
- */
- public static function normalizePath($path)
- {
- // Resolve any streams
- /** @var UniformResourceLocator $locator */
- $locator = Grav::instance()['locator'];
- if ($locator->isStream($path)) {
- $path = $locator->findResource($path);
- }
- // Set root properly for any URLs
- $root = '';
- preg_match(self::ROOTURL_REGEX, $path, $matches);
- if ($matches) {
- $root = $matches[1];
- $path = $matches[2];
- }
- // Strip off leading / to ensure explode is accurate
- if (Utils::startsWith($path,'/')) {
- $root .= '/';
- $path = ltrim($path, '/');
- }
- // If there are any relative paths (..) handle those
- if (Utils::contains($path, '..')) {
- $segments = explode('/', trim($path, '/'));
- $ret = [];
- foreach ($segments as $segment) {
- if (($segment === '.') || $segment === '') {
- continue;
- }
- if ($segment === '..') {
- array_pop($ret);
- } else {
- $ret[] = $segment;
- }
- }
- $path = implode('/', $ret);
- }
- // Stick everything back together
- $normalized = $root . $path;
- return $normalized;
- }
- /**
- * Check whether a function is disabled in the PHP settings
- *
- * @param string $function the name of the function to check
- *
- * @return bool
- */
- public static function isFunctionDisabled($function)
- {
- return \in_array($function, explode(',', ini_get('disable_functions')), true);
- }
- /**
- * Get the formatted timezones list
- *
- * @return array
- */
- public static function timezones()
- {
- $timezones = \DateTimeZone::listIdentifiers(\DateTimeZone::ALL);
- $offsets = [];
- $testDate = new \DateTime();
- foreach ($timezones as $zone) {
- $tz = new \DateTimeZone($zone);
- $offsets[$zone] = $tz->getOffset($testDate);
- }
- asort($offsets);
- $timezone_list = [];
- foreach ($offsets as $timezone => $offset) {
- $offset_prefix = $offset < 0 ? '-' : '+';
- $offset_formatted = gmdate('H:i', abs($offset));
- $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
- $timezone_list[$timezone] = "(${pretty_offset}) ".str_replace('_', ' ', $timezone);
- }
- return $timezone_list;
- }
- /**
- * Recursively filter an array, filtering values by processing them through the $fn function argument
- *
- * @param array $source the Array to filter
- * @param callable $fn the function to pass through each array item
- *
- * @return array
- */
- public static function arrayFilterRecursive(Array $source, $fn)
- {
- $result = [];
- foreach ($source as $key => $value) {
- if (is_array($value)) {
- $result[$key] = static::arrayFilterRecursive($value, $fn);
- continue;
- }
- if ($fn($key, $value)) {
- $result[$key] = $value; // KEEP
- continue;
- }
- }
- return $result;
- }
- /**
- * Flatten an array
- *
- * @param array $array
- * @return array
- */
- public static function arrayFlatten($array)
- {
- $flatten = array();
- foreach ($array as $key => $inner) {
- if (is_array($inner)) {
- foreach ($inner as $inner_key => $value) {
- $flatten[$inner_key] = $value;
- }
- } else {
- $flatten[$key] = $inner;
- }
- }
- return $flatten;
- }
- /**
- * Flatten a multi-dimensional associative array into dot notation
- *
- * @param array $array
- * @param string $prepend
- * @return array
- */
- public static function arrayFlattenDotNotation($array, $prepend = '')
- {
- $results = array();
- foreach ($array as $key => $value) {
- if (is_array($value)) {
- $results = array_merge($results, static::arrayFlattenDotNotation($value, $prepend.$key.'.'));
- } else {
- $results[$prepend.$key] = $value;
- }
- }
- return $results;
- }
- /**
- * Opposite of flatten, convert flat dot notation array to multi dimensional array
- *
- * @param array $array
- * @param string $separator
- * @return array
- */
- public static function arrayUnflattenDotNotation($array, $separator = '.')
- {
- $newArray = [];
- foreach ($array as $key => $value) {
- $dots = explode($separator, $key);
- if (\count($dots) > 1) {
- $last = &$newArray[$dots[0]];
- foreach ($dots as $k => $dot) {
- if ($k === 0) {
- continue;
- }
- $last = &$last[$dot];
- }
- $last = $value;
- } else {
- $newArray[$key] = $value;
- }
- }
- return $newArray;
- }
- /**
- * Checks if the passed path contains the language code prefix
- *
- * @param string $string The path
- *
- * @return bool|string Either false or the language
- *
- */
- public static function pathPrefixedByLangCode($string)
- {
- $languages_enabled = Grav::instance()['config']->get('system.languages.supported', []);
- $parts = explode('/', trim($string, '/'));
- if (count($parts) > 0 && in_array($parts[0], $languages_enabled)) {
- return $parts[0];
- }
- return false;
- }
- /**
- * Get the timestamp of a date
- *
- * @param string $date a String expressed in the system.pages.dateformat.default format, with fallback to a
- * strtotime argument
- * @param string $format a date format to use if possible
- * @return int the timestamp
- */
- public static function date2timestamp($date, $format = null)
- {
- $config = Grav::instance()['config'];
- $dateformat = $format ?: $config->get('system.pages.dateformat.default');
- // try to use DateTime and default format
- if ($dateformat) {
- $datetime = \DateTime::createFromFormat($dateformat, $date);
- } else {
- $datetime = new \DateTime($date);
- }
- // fallback to strtotime() if DateTime approach failed
- if ($datetime !== false) {
- return $datetime->getTimestamp();
- }
- return strtotime($date);
- }
- /**
- * @param array $array
- * @param string $path
- * @param null $default
- * @return mixed
- *
- * @deprecated 1.5 Use ->getDotNotation() method instead.
- */
- public static function resolve(array $array, $path, $default = null)
- {
- user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use ->getDotNotation() method instead', E_USER_DEPRECATED);
- return static::getDotNotation($array, $path, $default);
- }
- /**
- * Checks if a value is positive
- *
- * @param string $value
- *
- * @return boolean
- */
- public static function isPositive($value)
- {
- return in_array($value, [true, 1, '1', 'yes', 'on', 'true'], true);
- }
- /**
- * Generates a nonce string to be hashed. Called by self::getNonce()
- * We removed the IP portion in this version because it causes too many inconsistencies
- * with reverse proxy setups.
- *
- * @param string $action
- * @param bool $previousTick if true, generates the token for the previous tick (the previous 12 hours)
- *
- * @return string the nonce string
- */
- private static function generateNonceString($action, $previousTick = false)
- {
- $grav = Grav::instance();
- $username = isset($grav['user']) ? $grav['user']->username : '';
- $token = session_id();
- $i = self::nonceTick();
- if ($previousTick) {
- $i--;
- }
- return ($i . '|' . $action . '|' . $username . '|' . $token . '|' . $grav['config']->get('security.salt'));
- }
- /**
- * Get the time-dependent variable for nonce creation.
- *
- * Now a tick lasts a day. Once the day is passed, the nonce is not valid any more. Find a better way
- * to ensure nonces issued near the end of the day do not expire in that small amount of time
- *
- * @return int the time part of the nonce. Changes once every 24 hours
- */
- private static function nonceTick()
- {
- $secondsInHalfADay = 60 * 60 * 12;
- return (int)ceil(time() / $secondsInHalfADay);
- }
- /**
- * Creates a hashed nonce tied to the passed action. Tied to the current user and time. The nonce for a given
- * action is the same for 12 hours.
- *
- * @param string $action the action the nonce is tied to (e.g. save-user-admin or move-page-homepage)
- * @param bool $previousTick if true, generates the token for the previous tick (the previous 12 hours)
- *
- * @return string the nonce
- */
- public static function getNonce($action, $previousTick = false)
- {
- // Don't regenerate this again if not needed
- if (isset(static::$nonces[$action][$previousTick])) {
- return static::$nonces[$action][$previousTick];
- }
- $nonce = md5(self::generateNonceString($action, $previousTick));
- static::$nonces[$action][$previousTick] = $nonce;
- return static::$nonces[$action][$previousTick];
- }
- /**
- * Verify the passed nonce for the give action
- *
- * @param string|string[] $nonce the nonce to verify
- * @param string $action the action to verify the nonce to
- *
- * @return boolean verified or not
- */
- public static function verifyNonce($nonce, $action)
- {
- //Safety check for multiple nonces
- if (is_array($nonce)) {
- $nonce = array_shift($nonce);
- }
- //Nonce generated 0-12 hours ago
- if ($nonce === self::getNonce($action)) {
- return true;
- }
- //Nonce generated 12-24 hours ago
- $previousTick = true;
- return $nonce === self::getNonce($action, $previousTick);
- }
- /**
- * Simple helper method to get whether or not the admin plugin is active
- *
- * @return bool
- */
- public static function isAdminPlugin()
- {
- if (isset(Grav::instance()['admin'])) {
- return true;
- }
- return false;
- }
- /**
- * Get a portion of an array (passed by reference) with dot-notation key
- *
- * @param array $array
- * @param string|int $key
- * @param null $default
- * @return mixed
- */
- public static function getDotNotation($array, $key, $default = null)
- {
- if (null === $key) {
- return $array;
- }
- if (isset($array[$key])) {
- return $array[$key];
- }
- foreach (explode('.', $key) as $segment) {
- if (!is_array($array) || !array_key_exists($segment, $array)) {
- return $default;
- }
- $array = $array[$segment];
- }
- return $array;
- }
- /**
- * Set portion of array (passed by reference) for a dot-notation key
- * and set the value
- *
- * @param array $array
- * @param string|int $key
- * @param mixed $value
- * @param bool $merge
- *
- * @return mixed
- */
- public static function setDotNotation(&$array, $key, $value, $merge = false)
- {
- if (null === $key) {
- return $array = $value;
- }
- $keys = explode('.', $key);
- while (count($keys) > 1) {
- $key = array_shift($keys);
- if ( ! isset($array[$key]) || ! is_array($array[$key]))
- {
- $array[$key] = array();
- }
- $array =& $array[$key];
- }
- $key = array_shift($keys);
- if (!$merge || !isset($array[$key])) {
- $array[$key] = $value;
- } else {
- $array[$key] = array_merge($array[$key], $value);
- }
- return $array;
- }
- /**
- * Utility method to determine if the current OS is Windows
- *
- * @return bool
- */
- public static function isWindows()
- {
- return strncasecmp(PHP_OS, 'WIN', 3) === 0;
- }
- /**
- * Utility to determine if the server running PHP is Apache
- *
- * @return bool
- */
- public static function isApache() {
- return isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false;
- }
- /**
- * Sort a multidimensional array by another array of ordered keys
- *
- * @param array $array
- * @param array $orderArray
- * @return array
- */
- public static function sortArrayByArray(array $array, array $orderArray)
- {
- $ordered = array();
- foreach ($orderArray as $key) {
- if (array_key_exists($key, $array)) {
- $ordered[$key] = $array[$key];
- unset($array[$key]);
- }
- }
- return $ordered + $array;
- }
- /**
- * Sort an array by a key value in the array
- *
- * @param mixed $array
- * @param string|int $array_key
- * @param int $direction
- * @param int $sort_flags
- * @return array
- */
- public static function sortArrayByKey($array, $array_key, $direction = SORT_DESC, $sort_flags = SORT_REGULAR)
- {
- $output = [];
- if (!is_array($array) || !$array) {
- return $output;
- }
- foreach ($array as $key => $row) {
- $output[$key] = $row[$array_key];
- }
- array_multisort($output, $direction, $sort_flags, $array);
- return $array;
- }
- /**
- * Get path based on a token
- *
- * @param string $path
- * @param PageInterface|null $page
- * @return string
- * @throws \RuntimeException
- */
- public static function getPagePathFromToken($path, PageInterface $page = null)
- {
- $path_parts = pathinfo($path);
- $grav = Grav::instance();
- $basename = '';
- if (isset($path_parts['extension'])) {
- $basename = '/' . $path_parts['basename'];
- $path = rtrim($path_parts['dirname'], ':');
- }
- $regex = '/(@self|self@)|((?:@page|page@):(?:.*))|((?:@theme|theme@):(?:.*))/';
- preg_match($regex, $path, $matches);
- if ($matches) {
- if ($matches[1]) {
- if (null === $page) {
- throw new \RuntimeException('Page not available for this self@ reference');
- }
- } elseif ($matches[2]) {
- // page@
- $parts = explode(':', $path);
- $route = $parts[1];
- $page = $grav['page']->find($route);
- } elseif ($matches[3]) {
- // theme@
- $parts = explode(':', $path);
- $route = $parts[1];
- $theme = str_replace(ROOT_DIR, '', $grav['locator']->findResource("theme://"));
- return $theme . $route . $basename;
- }
- } else {
- return $path . $basename;
- }
- if (!$page) {
- throw new \RuntimeException('Page route not found: ' . $path);
- }
- $path = str_replace($matches[0], rtrim($page->relativePagePath(), '/'), $path);
- return $path . $basename;
- }
- public static function getUploadLimit()
- {
- static $max_size = -1;
- if ($max_size < 0) {
- $post_max_size = static::parseSize(ini_get('post_max_size'));
- if ($post_max_size > 0) {
- $max_size = $post_max_size;
- } else {
- $max_size = 0;
- }
- $upload_max = static::parseSize(ini_get('upload_max_filesize'));
- if ($upload_max > 0 && $upload_max < $max_size) {
- $max_size = $upload_max;
- }
- }
- return $max_size;
- }
- /**
- * Convert bytes to the unit specified by the $to parameter.
- *
- * @param int $bytes The filesize in Bytes.
- * @param string $to The unit type to convert to. Accepts K, M, or G for Kilobytes, Megabytes, or Gigabytes, respectively.
- * @param int $decimal_places The number of decimal places to return.
- *
- * @return int Returns only the number of units, not the type letter. Returns 0 if the $to unit type is out of scope.
- *
- */
- public static function convertSize($bytes, $to, $decimal_places = 1)
- {
- $formulas = array(
- 'K' => number_format($bytes / 1024, $decimal_places),
- 'M' => number_format($bytes / 1048576, $decimal_places),
- 'G' => number_format($bytes / 1073741824, $decimal_places)
- );
- return $formulas[$to] ?? 0;
- }
- /**
- * Return a pretty size based on bytes
- *
- * @param int $bytes
- * @param int $precision
- * @return string
- */
- public static function prettySize($bytes, $precision = 2)
- {
- $units = array('B', 'KB', 'MB', 'GB', 'TB');
- $bytes = max($bytes, 0);
- $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
- $pow = min($pow, count($units) - 1);
- // Uncomment one of the following alternatives
- $bytes /= 1024 ** $pow;
- // $bytes /= (1 << (10 * $pow));
- return round($bytes, $precision) . ' ' . $units[$pow];
- }
- /**
- * Parse a readable file size and return a value in bytes
- *
- * @param string|int $size
- * @return int
- */
- public static function parseSize($size)
- {
- $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
- $size = preg_replace('/[^0-9\.]/', '', $size);
- if ($unit) {
- $size = $size * pow(1024, stripos('bkmgtpezy', $unit[0]));
- }
- return (int) abs(round($size));
- }
- /**
- * Multibyte-safe Parse URL function
- *
- * @param string $url
- * @return array
- * @throws \InvalidArgumentException
- */
- public static function multibyteParseUrl($url)
- {
- $enc_url = preg_replace_callback(
- '%[^:/@?&=#]+%usD',
- function ($matches) {
- return urlencode($matches[0]);
- },
- $url
- );
- $parts = parse_url($enc_url);
- if($parts === false) {
- throw new \InvalidArgumentException('Malformed URL: ' . $url);
- }
- foreach($parts as $name => $value) {
- $parts[$name] = urldecode($value);
- }
- return $parts;
- }
- /**
- * Process a string as markdown
- *
- * @param string $string
- *
- * @param bool $block Block or Line processing
- * @param null $page
- * @return string
- * @throws \Exception
- */
- public static function processMarkdown($string, $block = true, $page = null)
- {
- $grav = Grav::instance();
- $page = $page ?? $grav['page'] ?? null;
- $defaults = [
- 'markdown' => $grav['config']->get('system.pages.markdown', []),
- 'images' => $grav['config']->get('system.images', [])
- ];
- $extra = $defaults['markdown']['extra'] ?? false;
- $excerpts = new Excerpts($page, $defaults);
- // Initialize the preferred variant of Parsedown
- if ($extra) {
- $parsedown = new ParsedownExtra($excerpts);
- } else {
- $parsedown = new Parsedown($excerpts);
- }
- if ($block) {
- $string = $parsedown->text($string);
- } else {
- $string = $parsedown->line($string);
- }
- return $string;
- }
- /**
- * Find the subnet of an ip with CIDR prefix size
- *
- * @param string $ip
- * @param int $prefix
- *
- * @return string
- */
- public static function getSubnet($ip, $prefix = 64)
- {
- if (!filter_var($ip, FILTER_VALIDATE_IP)) {
- return $ip;
- }
- // Packed representation of IP
- $ip = inet_pton($ip);
- // Maximum netmask length = same as packed address
- $len = 8*strlen($ip);
- if ($prefix > $len) $prefix = $len;
- $mask = str_repeat('f', $prefix>>2);
- switch($prefix & 3)
- {
- case 3: $mask .= 'e'; break;
- case 2: $mask .= 'c'; break;
- case 1: $mask .= '8'; break;
- }
- $mask = str_pad($mask, $len>>2, '0');
- // Packed representation of netmask
- $mask = pack('H*', $mask);
- // Bitwise - Take all bits that are both 1 to generate subnet
- $subnet = inet_ntop($ip & $mask);
- return $subnet;
- }
- /**
- * Wrapper to ensure html, htm in the front of the supported page types
- *
- * @param array|null $defaults
- * @return array|mixed
- */
- public static function getSupportPageTypes(array $defaults = null)
- {
- $types = Grav::instance()['config']->get('system.pages.types', $defaults);
- // remove html/htm
- $types = static::arrayRemoveValue($types, ['html', 'htm']);
- // put them back at the front
- $types = array_merge(['html', 'htm'], $types);
- return $types;
- }
- }
|