Passed
Push — master ( 7f5510...dc3129 )
by Michael
05:12 queued 15s
created

XoopsLocalAbstract::trim()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * XOOPS localization abstract
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.3.0
16
 * @author              Taiwen Jiang <[email protected]>
17
 */
18
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Class XoopsLocalAbstract
23
 */
24
class XoopsLocalAbstract
25
{
26
    /**
27
     * XoopsLocalAbstract::substr()
28
     *
29
     * @param mixed  $str
30
     * @param mixed  $start
31
     * @param mixed  $length
32
     * @param string $trimmarker
33
     *
34
     * @return mixed|string
35
     */
36
    public static function substr($str, $start, $length, $trimmarker = '...')
37
    {
38
        if (!XOOPS_USE_MULTIBYTES) {
39
            return (strlen($str) - $start <= $length) ? substr($str, $start, $length) : substr($str, $start, $length - strlen($trimmarker)) . $trimmarker;
40
        }
41
        if (function_exists('mb_internal_encoding') && @mb_internal_encoding(_CHARSET)) {
42
            $str2 = mb_strcut($str, $start, $length - strlen($trimmarker));
43
44
            return $str2 . (mb_strlen($str) != mb_strlen($str2) ? $trimmarker : '');
45
        }
46
47
        return $str;
48
    }
49
    // Each local language should define its own equivalent utf8_encode
50
    /**
51
     * XoopsLocalAbstract::utf8_encode()
52
     *
53
     * @param  mixed $text
54
     * @return string
55
     */
56
    public static function utf8_encode($text)
57
    {
58
        if (XOOPS_USE_MULTIBYTES == 1) {
0 ignored issues
show
introduced by
The condition XOOPS_USE_MULTIBYTES == 1 is always false.
Loading history...
59
            if (function_exists('mb_convert_encoding')) {
60
                return mb_convert_encoding($text, 'UTF-8', 'auto');
61
            }
62
        }
63
64
        return utf8_encode($text);
65
    }
66
67
    // Each local language should define its own equivalent utf8_encode
68
    /**
69
     * XoopsLocalAbstract::utf8_decode()
70
     *
71
     * @param  mixed $text
72
     * @return string
73
     */
74
    public static function utf8_decode($text)
75
    {
76
        if (XOOPS_USE_MULTIBYTES == 1) {
0 ignored issues
show
introduced by
The condition XOOPS_USE_MULTIBYTES == 1 is always false.
Loading history...
77
            if (function_exists('mb_convert_encoding')) {
78
                return mb_convert_encoding($text, 'ISO-8859-1', 'auto');
79
            }
80
        }
81
82
        return utf8_decode($text);
83
    }
84
85
    /**
86
     * XoopsLocalAbstract::convert_encoding()
87
     *
88
     * @param  mixed  $text
89
     * @param  string $to
90
     * @param  string $from
91
     * @return mixed|string
92
     */
93
    public static function convert_encoding($text, $to = 'utf-8', $from = '')
94
    {
95
        // Early exit if the text is empty
96
        if (empty($text)) {
97
            return $text;
98
        }
99
100
        // Set default $from encoding if not provided
101
        if (empty($from)) {
102
            $from = empty($GLOBALS['xlanguage']['charset_base']) ? _CHARSET : $GLOBALS['xlanguage']['charset_base'];
103
        }
104
105
        // If $to and $from are the same, no conversion is needed
106
        if (empty($to) || !strcasecmp($to, $from)) {
107
            return $text;
108
        }
109
110
        // Initialize a variable to store the converted text
111
        $convertedText = '';
112
113
        // Try to use mb_convert_encoding if available
114
        if (XOOPS_USE_MULTIBYTES && function_exists('mb_convert_encoding')) {
115
            $convertedText = mb_convert_encoding($text, $to, $from);
116
            if (false !== $convertedText) {
117
                return $convertedText;
118
            }
119
        }
120
121
        // Try to use iconv if available
122
        if (function_exists('iconv')) {
123
            $convertedText = iconv($from, $to . '//TRANSLIT', $text);
124
            if (false !== $convertedText) {
125
                return $convertedText;
126
            }
127
        }
128
129
        // Try to use utf8_encode if target encoding is 'utf-8'
130
        if ('utf-8' === $to) {
131
            $convertedText = utf8_encode($text);
132
            if (false !== $convertedText) {
133
                return $convertedText;
134
            }
135
        }
136
137
        // If all conversions fail, return the original text
138
        return $text;
139
        
140
    }
141
142
    /**
143
     * XoopsLocalAbstract::trim()
144
     *
145
     * @param  mixed $text
146
     * @return string
147
     */
148
    public static function trim($text)
149
    {
150
        $ret = trim($text);
151
152
        return $ret;
153
    }
154
155
    /**
156
     * Get description for setting time format
157
     */
158
    public static function getTimeFormatDesc()
159
    {
160
        return _TIMEFORMAT_DESC;
161
    }
162
163
    /**
164
     * Function to display formatted times in user timezone
165
     *
166
     * Setting $timeoffset to null (by default) will skip timezone calculation for user, using default timezone instead, which is a MUST for cached contents
167
     * @param        $time
168
     * @param string $format
169
     * @param null|string   $timeoffset
170
     * @return string
171
     */
172
    public static function formatTimestamp($time, $format = 'l', $timeoffset = null)
173
    {
174
        global $xoopsConfig, $xoopsUser;
175
176
        $format_copy = $format;
177
        $format      = strtolower($format);
178
179
        if ($format === 'rss' || $format === 'r') {
180
            $TIME_ZONE = '';
181
            if (isset($GLOBALS['xoopsConfig']['server_TZ'])) {
182
                $server_TZ = abs((int)($GLOBALS['xoopsConfig']['server_TZ'] * 3600.0));
183
                $prefix    = ($GLOBALS['xoopsConfig']['server_TZ'] < 0) ? ' -' : ' +';
184
                $TIME_ZONE = $prefix . date('Hi', $server_TZ);
0 ignored issues
show
Bug introduced by
It seems like $server_TZ can also be of type double; however, parameter $timestamp of date() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
                $TIME_ZONE = $prefix . date('Hi', /** @scrutinizer ignore-type */ $server_TZ);
Loading history...
185
            }
186
            $date = gmdate('D, d M Y H:i:s', (int)$time) . $TIME_ZONE;
187
188
            return $date;
189
        }
190
191
        if (($format === 'elapse' || $format === 'e') && $time < time()) {
192
            $elapse = time() - $time;
193
            if ($days = floor($elapse / (24 * 3600))) {
194
                $num = $days > 1 ? sprintf(_DAYS, $days) : _DAY;
195
            } elseif ($hours = floor(($elapse % (24 * 3600)) / 3600)) {
196
                $num = $hours > 1 ? sprintf(_HOURS, $hours) : _HOUR;
197
            } elseif ($minutes = floor(($elapse % 3600) / 60)) {
198
                $num = $minutes > 1 ? sprintf(_MINUTES, $minutes) : _MINUTE;
199
            } else {
200
                $seconds = $elapse % 60;
201
                $num     = $seconds > 1 ? sprintf(_SECONDS, $seconds) : _SECOND;
202
            }
203
            $ret = sprintf(_ELAPSE, $num);
204
205
            return $ret;
206
        }
207
        // disable user timezone calculation and use default timezone,
208
        // for cache consideration
209
        if ($timeoffset === null) {
210
            $timeoffset = ($xoopsConfig['default_TZ'] == '') ? '0.0' : $xoopsConfig['default_TZ'];
211
        }
212
        $usertimestamp = xoops_getUserTimestamp($time, $timeoffset);
213
        switch ($format) {
214
            case 's':
215
                $datestring = _SHORTDATESTRING;
216
                break;
217
218
            case 'm':
219
                $datestring = _MEDIUMDATESTRING;
220
                break;
221
222
            case 'mysql':
223
                $datestring = 'Y-m-d H:i:s';
224
                break;
225
226
            case 'l':
227
                $datestring = _DATESTRING;
228
                break;
229
230
            case 'c':
231
            case 'custom':
232
                static $current_timestamp, $today_timestamp, $monthy_timestamp;
233
                if (!isset($current_timestamp)) {
234
                    $current_timestamp = xoops_getUserTimestamp(time(), $timeoffset);
235
                }
236
                if (!isset($today_timestamp)) {
237
                    $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), date('d', $current_timestamp), date('Y', $current_timestamp));
0 ignored issues
show
Bug introduced by
date('d', $current_timestamp) of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
                    $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), /** @scrutinizer ignore-type */ date('d', $current_timestamp), date('Y', $current_timestamp));
Loading history...
Bug introduced by
date('Y', $current_timestamp) of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
                    $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), date('d', $current_timestamp), /** @scrutinizer ignore-type */ date('Y', $current_timestamp));
Loading history...
Bug introduced by
date('m', $current_timestamp) of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
                    $today_timestamp = mktime(0, 0, 0, /** @scrutinizer ignore-type */ date('m', $current_timestamp), date('d', $current_timestamp), date('Y', $current_timestamp));
Loading history...
238
                }
239
240
                if (abs($elapse_today = $usertimestamp - $today_timestamp) < 24 * 60 * 60) {
241
                    $datestring = ($elapse_today > 0) ? _TODAY : _YESTERDAY;
242
                } else {
243
                    if (!isset($monthy_timestamp)) {
244
                        $monthy_timestamp[0] = mktime(0, 0, 0, 0, 0, date('Y', $current_timestamp));
245
                        $monthy_timestamp[1] = mktime(0, 0, 0, 0, 0, date('Y', $current_timestamp) + 1);
246
                    }
247
                    $datestring = _YEARMONTHDAY;
248
                    if ($usertimestamp >= $monthy_timestamp[0] && $usertimestamp < $monthy_timestamp[1]) {
249
                        $datestring = _MONTHDAY;
250
                    }
251
                }
252
                break;
253
254
            default:
255
                $datestring = _DATESTRING;
256
                if ($format != '') {
257
                    $datestring = $format_copy;
258
                }
259
                break;
260
        }
261
262
        return ucfirst(date($datestring, $usertimestamp));
263
    }
264
265
    /**
266
     * XoopsLocalAbstract::number_format()
267
     *
268
     * @param  mixed $number
269
     * @return mixed
270
     */
271
    public function number_format($number)
272
    {
273
        return $number;
274
    }
275
276
    /**
277
     * XoopsLocalAbstract::money_format()
278
     *
279
     * @param  mixed $format
280
     * @param  mixed $number
281
     * @return mixed
282
     */
283
    public function money_format($format, $number)
284
    {
285
        return $number;
286
    }
287
288
    /**
289
     * XoopsLocalAbstract::__call()
290
     *
291
     * @param  mixed $name
292
     * @param  mixed $args
293
     * @return mixed
294
     */
295
    public function __call($name, $args)
296
    {
297
        if (function_exists($name)) {
298
            return call_user_func_array($name, $args);
299
        }
300
        return null;
301
    }
302
}
303