2011-11-27 11:21:58 +00:00
|
|
|
<?php
|
|
|
|
namespace wcf\data\chat\message;
|
2012-11-20 15:01:32 +00:00
|
|
|
use \wcf\system\Regex;
|
2011-12-04 21:52:57 +00:00
|
|
|
use \wcf\system\WCF;
|
2012-11-20 15:01:32 +00:00
|
|
|
use \wcf\util\ChatUtil;
|
2011-11-27 11:21:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a chat message.
|
|
|
|
*
|
2012-11-20 15:01:32 +00:00
|
|
|
* @author Tim Düsterhus
|
2012-01-28 16:50:33 +00:00
|
|
|
* @copyright 2010-2012 Tim Düsterhus
|
2011-11-27 11:21:58 +00:00
|
|
|
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
2012-03-12 16:18:15 +00:00
|
|
|
* @package be.bastelstu.wcf.chat
|
2011-11-27 11:21:58 +00:00
|
|
|
* @subpackage data.chat.message
|
|
|
|
*/
|
|
|
|
class ChatMessage extends \wcf\data\DatabaseObject {
|
|
|
|
/**
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\data\DatabaseObject::$databaseTableName
|
2011-11-27 11:21:58 +00:00
|
|
|
*/
|
|
|
|
protected static $databaseTableName = 'chat_message';
|
|
|
|
|
|
|
|
/**
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\data\DatabaseObject::$databaseTableIndexName
|
2011-11-27 11:21:58 +00:00
|
|
|
*/
|
|
|
|
protected static $databaseTableIndexName = 'messageID';
|
|
|
|
|
|
|
|
const TYPE_NORMAL = 0;
|
|
|
|
const TYPE_JOIN = 1;
|
|
|
|
const TYPE_LEAVE = 2;
|
|
|
|
const TYPE_AWAY = 3;
|
|
|
|
const TYPE_BACK = 4;
|
|
|
|
const TYPE_MODERATE = 5;
|
|
|
|
const TYPE_ME = 6;
|
|
|
|
const TYPE_WHISPER = 7;
|
|
|
|
const TYPE_INFORMATION = 8;
|
|
|
|
const TYPE_CLEAR = 9;
|
|
|
|
const TYPE_TEAM = 10;
|
|
|
|
const TYPE_GLOBALMESSAGE = 11;
|
2011-12-27 12:58:28 +00:00
|
|
|
const TYPE_ERROR = 12;
|
2011-11-27 11:21:58 +00:00
|
|
|
|
2012-11-20 15:01:32 +00:00
|
|
|
/**
|
|
|
|
* cache for users
|
|
|
|
* @var array<\wcf\data\user\User>
|
|
|
|
*/
|
|
|
|
protected static $users = array();
|
|
|
|
|
2011-11-27 11:21:58 +00:00
|
|
|
/**
|
2011-12-27 11:35:58 +00:00
|
|
|
* @see \wcf\data\chat\message\ChatMessage::getFormattedMessage()
|
2011-11-27 11:21:58 +00:00
|
|
|
*/
|
|
|
|
public function __toString() {
|
2011-12-05 13:45:50 +00:00
|
|
|
return $this->getFormattedMessage();
|
2011-12-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the formatted message.
|
2012-10-20 14:23:00 +00:00
|
|
|
*
|
|
|
|
* @param string $outputType outputtype for messageparser
|
2011-12-04 21:52:57 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-01-21 18:05:34 +00:00
|
|
|
public function getFormattedMessage($outputType = 'text/html') {
|
2011-12-04 21:52:57 +00:00
|
|
|
$message = $this->message;
|
2012-11-20 15:01:32 +00:00
|
|
|
|
2011-12-04 21:52:57 +00:00
|
|
|
switch ($this->type) {
|
|
|
|
case self::TYPE_JOIN:
|
|
|
|
case self::TYPE_LEAVE:
|
|
|
|
case self::TYPE_BACK:
|
2012-04-15 13:37:41 +00:00
|
|
|
case self::TYPE_AWAY:
|
2012-11-20 15:01:32 +00:00
|
|
|
$message = WCF::getLanguage()->getDynamicVariable('wcf.chat.message.'.$this->type, unserialize($message) ?: array());
|
2012-04-15 13:37:41 +00:00
|
|
|
break;
|
2012-10-20 16:04:46 +00:00
|
|
|
case self::TYPE_MODERATE:
|
2012-11-20 15:01:32 +00:00
|
|
|
$message = unserialize($message);
|
|
|
|
$message = WCF::getLanguage()->getDynamicVariable('wcf.chat.message.'.$this->type.'.'.$message['type'], $message ?: array());
|
|
|
|
$message = self::replaceUserLink($message, $outputType);
|
2012-10-20 16:04:46 +00:00
|
|
|
break;
|
|
|
|
case self::TYPE_WHISPER:
|
2012-11-20 15:01:32 +00:00
|
|
|
$message = unserialize($message);
|
2012-09-07 20:58:13 +00:00
|
|
|
$message = $message['message'];
|
2011-12-10 15:46:35 +00:00
|
|
|
case self::TYPE_NORMAL:
|
2011-12-27 13:10:40 +00:00
|
|
|
case self::TYPE_ME:
|
2012-01-21 18:05:34 +00:00
|
|
|
if (!$this->enableHTML && $outputType == 'text/html') {
|
2011-12-20 20:20:32 +00:00
|
|
|
$message = \wcf\system\bbcode\SimpleMessageParser::getInstance()->parse($message, true, $this->enableSmilies);
|
2011-12-10 15:46:35 +00:00
|
|
|
}
|
2011-12-27 13:10:40 +00:00
|
|
|
break;
|
2012-11-20 15:01:32 +00:00
|
|
|
default:
|
|
|
|
if ($this->enableHTML) {
|
|
|
|
$message = self::replaceUserLink($message, $outputType);
|
|
|
|
}
|
|
|
|
break;
|
2011-12-04 21:52:57 +00:00
|
|
|
}
|
2012-11-20 15:01:32 +00:00
|
|
|
|
2011-12-04 21:52:57 +00:00
|
|
|
return $message;
|
2011-11-27 11:21:58 +00:00
|
|
|
}
|
2011-12-04 21:46:50 +00:00
|
|
|
|
|
|
|
/**
|
2012-11-20 15:01:32 +00:00
|
|
|
* Returns the username.
|
|
|
|
*
|
|
|
|
* @param boolean $colored
|
2011-12-04 21:46:50 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-11-20 15:01:32 +00:00
|
|
|
public function getUsername($colored = false) {
|
|
|
|
$username = $this->username;
|
|
|
|
if ($this->type == self::TYPE_INFORMATION) return WCF::getLanguage()->get('wcf.chat.information');
|
|
|
|
if ($this->type == self::TYPE_ERROR) return WCF::getLanguage()->get('wcf.chat.error');
|
|
|
|
|
|
|
|
if ($colored) {
|
|
|
|
$username = \wcf\util\ChatUtil::gradient($username, $this->color1, $this->color2);
|
|
|
|
}
|
2011-12-04 21:57:15 +00:00
|
|
|
|
2012-09-07 20:58:13 +00:00
|
|
|
if ($this->type == self::TYPE_WHISPER) {
|
2012-11-20 15:01:32 +00:00
|
|
|
$message = unserialize($this->message);
|
2012-09-07 20:58:13 +00:00
|
|
|
$username .= ' -> '.$message['username'];
|
|
|
|
}
|
2011-12-04 21:46:50 +00:00
|
|
|
|
2012-11-20 14:16:27 +00:00
|
|
|
return $username;
|
2011-12-04 21:46:50 +00:00
|
|
|
}
|
2011-12-05 13:45:50 +00:00
|
|
|
|
|
|
|
/**
|
2012-11-20 15:01:32 +00:00
|
|
|
* Replaces a userLink in a message.
|
2011-12-05 13:45:50 +00:00
|
|
|
*/
|
2012-11-20 15:01:32 +00:00
|
|
|
public static function replaceUserLink($message, $outputType) {
|
|
|
|
static $regex = null;
|
|
|
|
if ($regex === null) $regex = new Regex('<span class="userLink" data-user-id="(\d+)" />');
|
|
|
|
|
|
|
|
if ($outputType === 'text/html') {
|
|
|
|
return $regex->replace($message, new \wcf\system\Callback(function ($matches) {
|
|
|
|
return self::getUserLink($matches[1]);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $regex->replace($message, new \wcf\system\Callback(function ($matches) {
|
|
|
|
self::getUserLink($matches[1]);
|
|
|
|
|
|
|
|
return self::$users[$matches[1]]->username;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a fully colored userlink.
|
|
|
|
*/
|
|
|
|
public static function getUserLink($userID) {
|
|
|
|
if (!isset(self::$users[$userID])) {
|
|
|
|
self::$users[$userID] = $user = new \wcf\data\user\User($userID);
|
|
|
|
|
|
|
|
// Username + link to profile
|
|
|
|
$color = ChatUtil::readUserData('color', $user);
|
|
|
|
$profile = \wcf\system\request\LinkHandler::getInstance()->getLink('User', array(
|
|
|
|
'object' => $user
|
|
|
|
));
|
|
|
|
self::$users[$userID]->userLink = '<a href="'.$profile.'" class="userLink" data-user-id="'.$user->userID.'">'.ChatUtil::gradient($user->username, $color[1], $color[2]).'</a>';
|
|
|
|
}
|
2011-12-27 13:09:29 +00:00
|
|
|
|
2012-11-20 15:01:32 +00:00
|
|
|
return self::$users[$userID]->userLink;
|
2011-12-05 13:45:50 +00:00
|
|
|
}
|
|
|
|
|
2011-12-10 14:24:36 +00:00
|
|
|
/**
|
|
|
|
* Converts this message into json-form.
|
|
|
|
*
|
2011-12-27 11:38:05 +00:00
|
|
|
* @param boolean $raw
|
2011-12-10 14:24:36 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-12-27 11:38:05 +00:00
|
|
|
public function jsonify($raw = false) {
|
2012-11-20 15:01:32 +00:00
|
|
|
switch ($this->type) {
|
|
|
|
case self::TYPE_NORMAL:
|
|
|
|
case self::TYPE_ERROR:
|
|
|
|
case self::TYPE_INFORMATION:
|
|
|
|
case self::TYPE_WHISPER:
|
|
|
|
$separator = ':';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$separator = ' ';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-27 11:38:05 +00:00
|
|
|
$array = array(
|
2012-11-20 15:01:32 +00:00
|
|
|
'formattedUsername' => $this->getUsername(true),
|
|
|
|
'formattedMessage' => $this->getFormattedMessage(),
|
2011-12-05 20:21:12 +00:00
|
|
|
'formattedTime' => \wcf\util\DateUtil::format(\wcf\util\DateUtil::getDateTimeByTimestamp($this->time), 'H:i:s'),
|
2012-11-20 15:01:32 +00:00
|
|
|
'separator' => $separator,
|
2012-01-21 18:05:34 +00:00
|
|
|
'message' => $this->getFormattedMessage('text/plain'),
|
2012-05-02 18:27:16 +00:00
|
|
|
'sender' => (int) $this->sender,
|
2011-12-05 13:45:50 +00:00
|
|
|
'username' => $this->getUsername(),
|
2012-05-02 18:27:16 +00:00
|
|
|
'time' => (int) $this->time,
|
|
|
|
'receiver' => (int) $this->receiver,
|
|
|
|
'type' => (int) $this->type,
|
|
|
|
'roomID' => (int) $this->roomID,
|
|
|
|
'messageID' => (int) $this->messageID
|
2011-12-27 11:38:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($raw) return $array;
|
|
|
|
return \wcf\util\JSON::encode($array);
|
2011-12-05 13:45:50 +00:00
|
|
|
}
|
2011-11-27 11:21:58 +00:00
|
|
|
}
|