From f570e9d1a6822e4fc6899efe11de7eeb4bc8a146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 23 May 2013 22:09:47 +0200 Subject: [PATCH] Improve time intervals for commands --- .../command/commands/ColorCommand.class.php | 2 +- .../command/commands/MuteCommand.class.php | 17 +++---- .../command/commands/UnmuteCommand.class.php | 2 +- .../command/commands/WhisperCommand.class.php | 4 +- .../option/TimeIntervalOptionType.class.php | 41 ----------------- file/lib/util/ChatUtil.class.php | 44 +++++++++---------- language/de.xml | 9 ++-- option.xml | 11 ++++- 8 files changed, 47 insertions(+), 83 deletions(-) delete mode 100644 file/lib/system/option/TimeIntervalOptionType.class.php diff --git a/file/lib/system/command/commands/ColorCommand.class.php b/file/lib/system/command/commands/ColorCommand.class.php index ea99325..fc96f3f 100644 --- a/file/lib/system/command/commands/ColorCommand.class.php +++ b/file/lib/system/command/commands/ColorCommand.class.php @@ -40,7 +40,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler) parent::__construct($commandHandler); try { - list($color[1], $color[2]) = explode(' ', $commandHandler->getParameters()); + list($color[1], $color[2]) = explode(' ', $commandHandler->getParameters(), 2); } catch (\wcf\system\exception\SystemException $e) { $color[1] = $color[2] = $commandHandler->getParameters(); diff --git a/file/lib/system/command/commands/MuteCommand.class.php b/file/lib/system/command/commands/MuteCommand.class.php index 18ca85e..4cbbf2e 100644 --- a/file/lib/system/command/commands/MuteCommand.class.php +++ b/file/lib/system/command/commands/MuteCommand.class.php @@ -24,12 +24,13 @@ class MuteCommand extends \chat\system\command\AbstractRestrictedCommand { public function __construct(\chat\system\command\CommandHandler $commandHandler) { parent::__construct($commandHandler); - $parameters = $commandHandler->getParameters(); - if (($comma = strpos($parameters, ',')) !== false) { - $username = substr($parameters, 0, $comma); - $this->time = ChatUtil::timeModifier(substr($parameters, $comma + 1)); + try { + list($username, $modifier) = explode(',', $commandHandler->getParameters(), 2); + $modifier = ChatUtil::timeModifier(\wcf\util\StringUtil::trim($modifier)); + $this->time = strtotime($modifier, TIME_NOW); + $this->time = min(max(-0x80000000, $this->time), 0x7FFFFFFF); } - else { + catch (\wcf\system\exception\SystemException $e) { throw new \chat\system\command\NotFoundException(); } @@ -48,7 +49,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler) public function executeAction() { if ($suspension = suspension\Suspension::getSuspensionByUserRoomAndType($this->user, $this->room, suspension\Suspension::TYPE_MUTE)) { - if ($suspension->time > TIME_NOW + $this->time) { + if ($suspension->time > $this->time) { throw new \wcf\system\exception\UserInputException('text', WCF::getLanguage()->get('wcf.chat.suspension.exists')); } @@ -61,7 +62,7 @@ public function executeAction() { 'userID' => $this->user->userID, 'roomID' => ChatUtil::readUserData('roomID'), 'type' => suspension\Suspension::TYPE_MUTE, - 'time' => TIME_NOW + $this->time + 'time' => $this->time ) )); $this->suspensionAction->executeAction(); @@ -91,7 +92,7 @@ public function getType() { public function getMessage() { return serialize(array( 'link' => $this->link, - 'until' => TIME_NOW + $this->time, + 'until' => $this->time, 'type' => str_replace(array('chat\system\command\commands\\', 'command'), '', strtolower(get_class($this))) )); } diff --git a/file/lib/system/command/commands/UnmuteCommand.class.php b/file/lib/system/command/commands/UnmuteCommand.class.php index 5820d1b..fd5fed2 100644 --- a/file/lib/system/command/commands/UnmuteCommand.class.php +++ b/file/lib/system/command/commands/UnmuteCommand.class.php @@ -43,7 +43,7 @@ public function executeAction() { $action->executeAction(); } else { - throw new \wcf\system\exception\UserInputException('text', WCF::getLanguage()->get('wcf.chat.suspension.notExists')); + throw new \wcf\system\exception\UserInputException('text', WCF::getLanguage()->get('wcf.chat.suspension.notExists')); } } diff --git a/file/lib/system/command/commands/WhisperCommand.class.php b/file/lib/system/command/commands/WhisperCommand.class.php index f63985d..0049c8c 100644 --- a/file/lib/system/command/commands/WhisperCommand.class.php +++ b/file/lib/system/command/commands/WhisperCommand.class.php @@ -24,7 +24,9 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler) $username = substr($parameters, 0, $comma); $this->message = substr($parameters, $comma + 1); } - else throw new \chat\system\command\NotFoundException(); + else { + throw new \chat\system\command\NotFoundException(); + } $this->user = User::getUserByUsername($username); if (!$this->user->userID) throw new \chat\system\command\UserNotFoundException($username); diff --git a/file/lib/system/option/TimeIntervalOptionType.class.php b/file/lib/system/option/TimeIntervalOptionType.class.php deleted file mode 100644 index c7d0339..0000000 --- a/file/lib/system/option/TimeIntervalOptionType.class.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @package be.bastelstu.chat - * @subpackage system.option - */ -class TimeIntervalOptionType extends \wcf\system\option\TextOptionType { - /** - * @see \wcf\system\option\IOptionType::getData() - */ - public function getData(\wcf\data\option\Option $option, $newValue) { - return \chat\util\ChatUtil::timeModifier($newValue); - } - - /** - * @see \wcf\system\option\TextOptionType::getFormElement() - */ - public function getFormElement(\wcf\data\option\Option $option, $value) { - $tmp = ''; - if ($value > 86400) { - $tmp = floor($value / 86400).'d'; - $value -= floor($value / 86400) * 86400; - } - if ($value > 3600) { - $tmp .= floor($value / 3600).'h'; - $value -= floor($value / 3600) * 3600; - } - $tmp .= floor($value / 60); - if ($value % 60 != 0) { - $tmp .= ','.($value % 60).'s'; - } - - return parent::getFormElement($option, $tmp); - } -} diff --git a/file/lib/util/ChatUtil.class.php b/file/lib/util/ChatUtil.class.php index 370a24a..5b0eccb 100644 --- a/file/lib/util/ChatUtil.class.php +++ b/file/lib/util/ChatUtil.class.php @@ -127,6 +127,7 @@ public static function readUserData($field, \wcf\data\user\User $user = null) { /** * Writes user data + * * @param array $data * @param \wcf\data\user\User $user */ @@ -166,50 +167,45 @@ public static function str_split($string, $length = 1) { * @return integer */ public static function timeModifier($time) { - $regex = new \wcf\system\Regex('([0-9]+[s|h|d|w|m|y]?)', \wcf\system\Regex::CASE_INSENSITIVE); - if (!$regex->match($time, true)) return 0; + $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; $matches = $regex->getMatches(); - $result = 0; - foreach ($matches[1] as $time) { - // 60 seconds a minute - $multiplier = 60; - $modifier = substr($time, -1); + $result = ''; + foreach ($matches as $match) { + list(, $time, $modifier) = $match; switch ($modifier) { case 'y': case 'Y': - // twelve months a year - $multiplier *= 12; + $result .= '+'.$time.'year'; + break; case 'm': case 'M': - // about 4.3 weeks per month - $multiplier *= 4.34812141; + $result .= '+'.$time.'month'; + break; case 'w': case 'W': - // seven days a weeks - $multiplier *= 7; + $result .= '+'.$time.'week'; + break; case 'd': case 'D': - // 24 hours a day - $multiplier *= 24; + $result .= '+'.$time.'day'; + break; case 'h': case 'H': - // 60 minutes an hour - $multiplier *= 60; - $time = substr($time, 0, -1); + $result .= '+'.$time.'hour'; break; case 's': case 'S': - // 60 seconds per minute - $multiplier /= 60; - $time = substr($time, 0, -1); + $result .= '+'.$time.'second'; + break; + default: + $result .= '+'.$time.'minute'; } - - $result += $time * $multiplier; } - return (int) round($result, 0); + return $result; } /** diff --git a/language/de.xml b/language/de.xml index 772a625..13414bd 100644 --- a/language/de.xml +++ b/language/de.xml @@ -37,18 +37,17 @@ - + - + - + - - + diff --git a/option.xml b/option.xml index 74bb57c..05c8e8b 100644 --- a/option.xml +++ b/option.xml @@ -23,12 +23,16 @@ chat.general integer 5 + 1 + 15 2