XOOPS  2.6.0
Abstract.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
23 abstract class Xoops_Locale_Abstract
24 {
28  public static function isMultiByte()
29  {
30  return false;
31  }
32 
36  public static function isRtl()
37  {
38  return false;
39  }
40 
44  public static function setLocale()
45  {
46  return setlocale(LC_ALL, self::getLocale());
47  }
48 
52  public static function getCharset()
53  {
54  return 'UTF-8';
55  }
56 
60  public static function getLocale()
61  {
62  return 'en_US';
63  }
64 
68  public static function getLangCode()
69  {
70  return 'en-US';
71  }
72 
76  public static function getLegacyLanguage()
77  {
78  return 'english';
79  }
80 
84  public static function getTimezone()
85  {
86  return 'Europe/London';
87  }
88 
92  public static function getFonts()
93  {
94  return array(
95  'Arial',
96  'Courier',
97  'Georgia',
98  'Helvetica',
99  'Impact',
100  'Verdana',
101  'Haettenschweiler'
102  );
103  }
104 
108  public static function getFontSizes()
109  {
110  return array(
111  'xx-small' => 'xx-Small',
112  'x-small' => 'x-Small',
113  'small' => 'Small',
114  'medium' => 'Medium',
115  'large' => 'Large',
116  'x-large' => 'x-Large',
117  'xx-large' => 'xx-Large'
118  );
119  }
120 
124  public static function getAdminRssUrls()
125  {
126  return array('http://www.xoops.org/backend.php');
127  }
128 
133  public static function getFormatToday()
134  {
135  return "\T\o\d\a\y G:i";
136  }
137 
141  public static function getFormatYesterday()
142  {
143  return "\Y\\e\s\\t\\e\\r\d\a\y G:i";
144  }
145 
149  public static function getFormatMonthDay()
150  {
151  return "n/j G:i";
152  }
153 
157  public static function getFormatYearMonthDay()
158  {
159  return "Y/n/j G:i";
160  }
161 
165  public static function getFormatLongDate()
166  {
167  return "Y/n/j G:i:s";
168  }
169 
173  public static function getFormatMediumDate()
174  {
175  return "Y/n/j G:i";
176  }
177 
181  public static function getFormatShortDate()
182  {
183  return "Y/n/j";
184  }
185 
194  public static function substr($str, $start, $length, $trimmarker = '...')
195  {
196  if (!self::isMultiByte()) {
197  return (strlen($str) - $start <= $length)
198  ? substr($str, $start, $length)
199  : substr($str, $start, $length - strlen($trimmarker)) . $trimmarker;
200  }
201  if (function_exists('mb_internal_encoding') && @mb_internal_encoding(self::getCharset())) {
202  $str2 = mb_strcut($str, $start, $length - strlen($trimmarker));
203 
204  return $str2 . (mb_strlen($str) != mb_strlen($str2) ? $trimmarker : '');
205  }
206 
207  return $str;
208  }
209 
217  public static function utf8_encode($text)
218  {
219  if (self::isMultiByte()) {
220  if (function_exists('mb_convert_encoding')) {
221  return mb_convert_encoding($text, 'UTF-8', 'auto');
222  }
223  }
224 
225  return utf8_encode($text);
226  }
227 
235  public static function convert_encoding($text, $to = 'utf-8', $from = '')
236  {
238  $xlanguage = $xoops->registry()->get('XLANGUAGE', array());
239  $charset = false;
240  if (isset($xlanguage['charset_base'])) {
241  $charset = $xlanguage['charset_base'];
242  }
243  if (empty($text)) {
244  return $text;
245  }
246  if (empty($from)) {
247  $from = $charset ? $charset : XoopsLocale::getCharset();
248  }
249  if (empty($to) || !strcasecmp($to, $from)) {
250  return $text;
251  }
252 
253  if (self::isMultiByte() && function_exists('mb_convert_encoding')) {
254  $converted_text = @mb_convert_encoding($text, $to, $from);
255  } elseif (function_exists('iconv')) {
256  $converted_text = @iconv($from, $to . "//TRANSLIT", $text);
257  } elseif ('utf-8' == $to) {
258  $converted_text = utf8_encode($text);
259  }
260  $text = empty($converted_text) ? $text : $converted_text;
261 
262  return $text;
263  }
264 
272  public static function trim($text)
273  {
274  $ret = trim($text);
275 
276  return $ret;
277  }
278 
289  public static function formatTimestamp($time, $format = 'l', $timeoffset = null)
290  {
292  $format_copy = $format;
293  $format = strtolower($format);
294 
295  if ($format == 'rss' || $format == 'r') {
296  $TIME_ZONE = '';
297  if ($xoops->getConfig('server_TZ')) {
298  $server_TZ = abs(intval($xoops->getConfig('server_TZ') * 3600.0));
299  $prefix = ($xoops->getConfig('server_TZ') < 0) ? ' -' : ' +';
300  $TIME_ZONE = $prefix . date('Hi', $server_TZ);
301  }
302  $date = gmdate('D, d M Y H:i:s', intval($time)) . $TIME_ZONE;
303 
304  return $date;
305  }
306 
307  if (($format == 'elapse' || $format == 'e') && $time < time()) {
308  $elapse = time() - $time;
309  if ($days = floor($elapse / (24 * 3600))) {
310  $num = $days > 1 ? sprintf(XoopsLocale::LF_AGO_DAYS, $days) : XoopsLocale::LF_AGO_ONE_DAY;
311  } elseif ($hours = floor(($elapse % (24 * 3600)) / 3600)) {
312  $num = $hours > 1 ? sprintf(XoopsLocale::LF_AGO_HOURS, $hours) : XoopsLocale::LF_AGO_ONE_HOUR;
313  } elseif ($minutes = floor(($elapse % 3600) / 60)) {
314  $num = $minutes > 1 ? sprintf(XoopsLocale::LF_AGO_MINUTES, $minutes) : XoopsLocale::LF_AGO_ONE_MINUTE;
315  } else {
316  $seconds = $elapse % 60;
317  $num = $seconds > 1 ? sprintf(XoopsLocale::LF_AGO_SECONDS, $seconds) : sprintf(XoopsLocale::LF_AGO_ONE_SECOND);
318  }
319 
320  return $num;
321  }
322  // disable user timezone calculation and use default timezone,
323  // for cache consideration
324  if ($timeoffset === null) {
325  $timeoffset = ($xoops->getConfig('default_TZ') == '') ? '0.0' : $xoops->getConfig('default_TZ');
326  }
327  $usertimestamp = $xoops->getUserTimestamp($time, $timeoffset);
328  switch ($format) {
329  case 's':
330  $datestring = self::getFormatShortDate();
331  break;
332 
333  case 'm':
334  $datestring = self::getFormatMediumDate();
335  break;
336 
337  case 'mysql':
338  $datestring = 'Y-m-d H:i:s';
339  break;
340 
341  case 'l':
342  $datestring = self::getFormatLongDate();
343  break;
344 
345  case 'c':
346  case 'custom':
347  static $current_timestamp, $today_timestamp, $monthy_timestamp;
348  if (!isset($current_timestamp)) {
349  $current_timestamp = $xoops->getUserTimestamp(time(), $timeoffset);
350  }
351  if (!isset($today_timestamp)) {
352  $today_timestamp = mktime(0, 0, 0, gmdate('m', $current_timestamp), gmdate('d', $current_timestamp), gmdate('Y', $current_timestamp));
353  }
354 
355  if (abs($elapse_today = $usertimestamp - $today_timestamp) < 24 * 60 * 60) {
356  $datestring = ($elapse_today > 0) ? XoopsLocale::getFormatToday() : XoopsLocale::getFormatYesterday();
357  } else {
358  if (!isset($monthy_timestamp)) {
359  $monthy_timestamp[0] = mktime(0, 0, 0, 0, 0, gmdate('Y', $current_timestamp));
360  $monthy_timestamp[1] = mktime(0, 0, 0, 0, 0, gmdate('Y', $current_timestamp) + 1);
361  }
362  if ($usertimestamp >= $monthy_timestamp[0] && $usertimestamp < $monthy_timestamp[1]) {
363  $datestring = self::getFormatMonthDay();
364  } else {
365  $datestring = self::getFormatYearMonthDay();
366  }
367  }
368  break;
369 
370  default:
371  if ($format != '') {
372  $datestring = $format_copy;
373  } else {
374  $datestring = self::getFormatLongDate();
375  }
376  break;
377  }
378 
379  return ucfirst(gmdate($datestring, $usertimestamp));
380  }
381 
387  public static function number_format($number)
388  {
389  return number_format($number, 2, '.', ',');
390  }
391 
398  public static function money_format($format, $number)
399  {
400  if (function_exists('money_format')) {
401  $result = money_format($format, $number);
402  } else {
403  $result = sprintf('%01.2f', $number);
404  }
405 
406  return $result;
407  }
408 }
static utf8_encode($text)
Definition: Abstract.php:217
static getLegacyLanguage()
Definition: Abstract.php:76
static formatTimestamp($time, $format= 'l', $timeoffset=null)
Definition: Abstract.php:289
const LF_AGO_ONE_HOUR
Definition: en_US.php:504
static getInstance()
Definition: Xoops.php:160
$result
Definition: pda.php:33
$text
Definition: qrrender.php:27
const LF_AGO_DAYS
Definition: en_US.php:500
static isMultiByte()
Definition: Abstract.php:28
const LF_AGO_ONE_SECOND
Definition: en_US.php:507
static getFormatYearMonthDay()
Definition: Abstract.php:157
const LF_AGO_MINUTES
Definition: en_US.php:502
static getFormatYesterday()
Definition: Abstract.php:141
$xoops
Definition: admin.php:25
const LF_AGO_SECONDS
Definition: en_US.php:509
static convert_encoding($text, $to= 'utf-8', $from= '')
Definition: Abstract.php:235
static getFormatToday()
Definition: Abstract.php:133
static getFormatMediumDate()
Definition: Abstract.php:173
static getTimezone()
Definition: Abstract.php:84
static trim($text)
Definition: Abstract.php:272
static getAdminRssUrls()
Definition: Abstract.php:124
static getLangCode()
Definition: Abstract.php:68
static getFormatShortDate()
Definition: Abstract.php:181
static number_format($number)
Definition: Abstract.php:387
static getFormatLongDate()
Definition: Abstract.php:165
const LF_AGO_ONE_DAY
Definition: en_US.php:503
$start
const LF_AGO_ONE_MINUTE
Definition: en_US.php:505
const LF_AGO_HOURS
Definition: en_US.php:501
static getFormatMonthDay()
Definition: Abstract.php:149
static substr($str, $start, $length, $trimmarker= '...')
Definition: Abstract.php:194
static money_format($format, $number)
Definition: Abstract.php:398