1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-10-31 14:10:08 +00:00
Tims-Chat/file/lib/util/ChatUtil.class.php

153 lines
3.9 KiB
PHP
Raw Normal View History

2011-12-05 15:53:12 +00:00
<?php
2013-01-19 19:36:40 +00:00
namespace chat\util;
2013-01-07 17:46:19 +00:00
use \wcf\data\package\PackageCache;
use \wcf\system\user\storage\UserStorageHandler;
use \wcf\system\WCF;
2011-12-05 15:53:12 +00:00
/**
* Chat utilities
*
* @author Tim Düsterhus
2013-01-19 19:36:40 +00:00
* @copyright 2010-2013 Tim Düsterhus
2011-12-05 15:53:12 +00:00
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
2013-01-19 19:36:40 +00:00
* @package be.bastelstu.chat
2011-12-05 15:53:12 +00:00
* @subpackage util
*/
2012-03-12 21:51:29 +00:00
final class ChatUtil {
2011-12-10 13:43:18 +00:00
/**
* Matches a time-interval with modifiers.
* Each part may be optionally separated by a comma
*
2013-01-19 19:36:40 +00:00
* @see \chat\util\ChatUtil::timeModifier()
2011-12-10 13:43:18 +00:00
*/
2011-12-14 16:25:16 +00:00
const TIME_MODIFIER_REGEX = '((?:[0-9]+[s|h|d|w|m|y|S|H|D|W|M|Y]?,?)+)';
2011-12-10 13:43:18 +00:00
/**
* Package identifier of Tims Chat.
*
* @var string
*/
2013-01-19 19:36:40 +00:00
const PACKAGE_IDENTIFIER = 'be.bastelstu.chat';
/**
* Cached packageID of Tims Chat.
*
* @var integer
*/
private static $packageID = null;
/**
* Returns the packageID of Tims Chat.
*/
public static function getPackageID() {
if (self::$packageID === null) {
2013-01-07 17:46:19 +00:00
self::$packageID = PackageCache::getInstance()->getPackageID(self::PACKAGE_IDENTIFIER);
}
return self::$packageID;
}
2011-12-05 15:53:12 +00:00
/**
* Creates a gradient out of two colors represented by an integer.
* The first byte is red, the second byte is green, the third one is blue.
* The numbers can be easily expressed in hexadecimal notation: 0xFF0000 being red.
*
* @param string $string
* @param integer $start
* @param integer $end
* @return string
2011-12-05 15:53:12 +00:00
*/
public static function gradient($string, $start, $end) {
if (($length = mb_strlen($string)) === 0) return '';
2011-12-14 16:24:41 +00:00
2013-04-23 14:36:04 +00:00
if ($start === $end) {
2013-04-23 14:49:54 +00:00
return '<span style="color:rgb('.($start >> 16 & 255).','.($start >> 8 & 255).','.($start & 255).')">'.\wcf\util\StringUtil::encodeHTML($string).'</span>';
2013-04-23 14:36:04 +00:00
}
$r = (int) ((($start >> 16 & 255) - ($end >> 16 & 255)) / ($length - 1));
$g = (int) ((($start >> 8 & 255) - ($end >> 8 & 255)) / ($length - 1));
$b = (int) ((($start & 255) - ($end & 255)) / ($length - 1));
2011-12-05 15:53:12 +00:00
$result = '';
$string = self::str_split($string);
for ($i = 0; $i < $length; $i++) {
2013-01-19 19:36:40 +00:00
$result .= '<span style="color:rgb('.(($start >> 16 & 255) - $i * $r).','.(($start >> 8 & 255) - $i * $g).','.(($start & 255) - $i * $b).')">'.\wcf\util\StringUtil::encodeHTML($string[$i]).'</span>';
2011-12-05 15:53:12 +00:00
}
return $result;
}
2011-12-10 13:25:36 +00:00
2011-12-13 17:15:30 +00:00
/**
* Splits a string into smaller chunks.
* UTF-8 safe version of str_split().
*
* @param string $string
* @param integer $length
* @return array<string>
*/
public static function str_split($string, $length = 1) {
$result = array();
for ($i = 0, $max = mb_strlen($string); $i < $max; $i += $length) {
$result[] = mb_substr($string, $i, $length);
2011-12-13 17:15:30 +00:00
}
return $result;
}
2011-12-10 13:43:18 +00:00
/**
* Creates an interval out of a string with modifiers.
* Modifiers may be mixed. Valid modifiers are: _s_econd, _h_our, _d_ay, _w_week, _m_onth, _y_ear
* '2' -> 2 minutes
* '2h' -> 2 hours
* '1y12m2w3d12h' -> 1 year, 12 months, 2 weeks, 3 days, 12 hours
*
* @param string $time
* @return integer
*/
public static function timeModifier($time) {
2013-05-23 20:09:47 +00:00
$regex = new \wcf\system\Regex('([0-9]+)([shdwmy]?)', \wcf\system\Regex::CASE_INSENSITIVE);
if (!$regex->match($time, true, \wcf\system\Regex::ORDER_MATCH_BY_SET)) return 0;
2011-12-23 19:28:09 +00:00
$matches = $regex->getMatches();
2013-05-23 20:09:47 +00:00
$result = '';
foreach ($matches as $match) {
list(, $time, $modifier) = $match;
2011-12-10 13:43:18 +00:00
switch ($modifier) {
case 'y':
case 'Y':
2013-05-23 20:09:47 +00:00
$result .= '+'.$time.'year';
break;
2011-12-10 13:43:18 +00:00
case 'm':
case 'M':
2013-05-23 20:09:47 +00:00
$result .= '+'.$time.'month';
break;
2011-12-10 13:43:18 +00:00
case 'w':
case 'W':
2013-05-23 20:09:47 +00:00
$result .= '+'.$time.'week';
break;
2011-12-10 13:43:18 +00:00
case 'd':
case 'D':
2013-05-23 20:09:47 +00:00
$result .= '+'.$time.'day';
break;
2011-12-10 13:43:18 +00:00
case 'h':
case 'H':
2013-05-23 20:09:47 +00:00
$result .= '+'.$time.'hour';
2011-12-10 13:43:18 +00:00
break;
case 's':
case 'S':
2013-05-23 20:09:47 +00:00
$result .= '+'.$time.'second';
break;
default:
$result .= '+'.$time.'minute';
2011-12-10 13:43:18 +00:00
}
}
2013-05-23 20:09:47 +00:00
return $result;
2011-12-10 13:43:18 +00:00
}
2012-06-04 19:34:07 +00:00
/**
* Disables the constructor.
*/
2012-03-12 21:51:29 +00:00
private function __construct() { }
2011-12-05 15:53:12 +00:00
}