| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2022 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* VikBooking HTML main helper. |
| 16 |
* |
| 17 |
* @since 1.5 |
| 18 |
*/ |
| 19 |
abstract class JHtmlVikbooking |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Helper method used to obtain a shorten version of the given text. |
| 23 |
* |
| 24 |
* Even if a maximum threshold is provided, this doesn't mean that the |
| 25 |
* length of the resulting text will be exactly equal to that amount. |
| 26 |
* This beacuse the function doesn't break words. |
| 27 |
* |
| 28 |
* @param string $text The text to shorten. |
| 29 |
* @param int|null $max The maximum number of allowed characters. |
| 30 |
* |
| 31 |
* @return string |
| 32 |
* |
| 33 |
* @since 1.8 |
| 34 |
*/ |
| 35 |
public static function shorten(string $text, ?int $max = null): string |
| 36 |
{ |
| 37 |
// add a white space after the closure of any tag |
| 38 |
$text = preg_replace_callback("/(<\/[a-z0-9\-]+>)|(\s*\/\s*>)/", function($match) { |
| 39 |
return $match[0] . " "; |
| 40 |
}, $text); |
| 41 |
|
| 42 |
// get rid of HTML tags |
| 43 |
$text = strip_tags($text); |
| 44 |
|
| 45 |
// remove duplicate white spaces and replace new lines with spaces |
| 46 |
$text = preg_replace("/(\s{2,})|(\R+)/", ' ', $text); |
| 47 |
|
| 48 |
// calculate total length of the text |
| 49 |
$len = strlen($text); |
| 50 |
|
| 51 |
// Check whether we should take a substring of the text. |
| 52 |
// Reserve an additional 25% of characters to avoid breaking the |
| 53 |
// text too close to the end of the string. |
| 54 |
if ($max && $len > $max * 1.25) { |
| 55 |
// explode the string in words |
| 56 |
$chunks = explode(' ', $text); |
| 57 |
|
| 58 |
$text = ''; |
| 59 |
|
| 60 |
// keep adding words until we reach the maximum threshold |
| 61 |
while ($chunks && strlen($text) < $max) { |
| 62 |
$text .= array_shift($chunks) . ' '; |
| 63 |
} |
| 64 |
|
| 65 |
// get rid of trailing special characters and add the ellipsis |
| 66 |
$text = rtrim($text, '.,?!;:#\'"([{ ') . '...'; |
| 67 |
} |
| 68 |
|
| 69 |
return $text; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Calculates the maximum upload file size and returns string with unit or the size in bytes. |
| 74 |
* |
| 75 |
* @param bool $unitOutput This parameter determines whether the return value |
| 76 |
* should be a string with a unit. |
| 77 |
* |
| 78 |
* @return float|string The maximum upload size of files with the appropriate unit or in bytes. |
| 79 |
*/ |
| 80 |
public static function maxuploadsize($unitOutput = true) |
| 81 |
{ |
| 82 |
static $max_size = false; |
| 83 |
|
| 84 |
if ($max_size === false) |
| 85 |
{ |
| 86 |
$max_size = self::parseSize(ini_get('post_max_size')); |
| 87 |
$upload_max = self::parseSize(ini_get('upload_max_filesize')); |
| 88 |
|
| 89 |
// check what is the highest value between post and upload max sizes |
| 90 |
if ($upload_max > 0 && ($upload_max < $max_size || $max_size == 0)) |
| 91 |
{ |
| 92 |
$max_size = $upload_max; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (!$unitOutput) |
| 97 |
{ |
| 98 |
// return numerical max size |
| 99 |
return $max_size; |
| 100 |
} |
| 101 |
|
| 102 |
// format max size |
| 103 |
return JHtml::fetch('number.bytes', $max_size, 'auto', 0); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns the size in bytes without the unit for the comparison. |
| 108 |
* |
| 109 |
* @param string $size The size which is received from the PHP settings. |
| 110 |
* |
| 111 |
* @return float The size in bytes without the unit. |
| 112 |
*/ |
| 113 |
private static function parseSize($size) |
| 114 |
{ |
| 115 |
// extract the size unit |
| 116 |
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); |
| 117 |
// take only the size numbers |
| 118 |
$size = preg_replace('/[^0-9\.]/', '', $size); |
| 119 |
|
| 120 |
$return = round($size); |
| 121 |
|
| 122 |
if ($unit) |
| 123 |
{ |
| 124 |
// calculate the correct size according to the specified unit |
| 125 |
$return = round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); |
| 126 |
} |
| 127 |
|
| 128 |
return $return; |
| 129 |
} |
| 130 |
} |
| 131 |
|