PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / html / vikbooking.php

vikbooking.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/html/vikbooking.php

131 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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