Source

wcf-shoutbox / files / lib / page / ShoutboxContentPage.class.php

Full commit
<?php
require_once(WCF_DIR.'lib/page/AbstractPage.class.php');
require_once(WCF_DIR.'lib/system/event/EventHandler.class.php');
require_once(WCF_DIR.'lib/data/message/bbcode/MessageParser.class.php');
require_once(WCF_DIR.'lib/data/message/bbcode/URLParser.class.php');
require_once(WCF_DIR.'lib/data/message/bbcode/URLBBCode.class.php');
require_once(WCF_DIR.'lib/util/StringUtil.class.php');


/**
 * @author	Christian Fischer
 * @package	net.northshirebund.wbb.shoutbox
 */
class ShoutboxContentPage extends AbstractPage {
	/**
	 * Name of the template for the called page.
	 */
	public $templateName	= 'shoutboxContent';
	public $templateNameXml	= 'shoutboxContentXml';
	public $messages		= array();
	public $errormessage    = null;
	public $lastmessage		= 0;
	public $xmloutput		= false;
	
	
	/**
	 * @see Page::readParameters()
	 */
	public function readParameters() {
		parent::readParameters();
		
		if (isset($_POST['message'])) {
			$message = StringUtil::trim($_POST['message']);
			$userid  = WCF::getUser()->userID;
			
			if(CHARSET != 'UTF-8') {
				$message = StringUtil::convertEncoding('UTF-8', CHARSET, $message);
			}
			
			if($message != '') {
				if(WCF::getUser()->getPermission('user.shoutbox.canWrite')) {
					if(substr($message, 0, 2) == '/w') {
						$message = ltrim(substr($message, 2));
						$name    = null;
						$toUser  = -1;
						
						if(substr($message, 0, 1) == '"') {
							$pos     = strpos($message, '"', 1);
							$name    = trim(substr($message, 1, $pos - 1));
							$message = ltrim(substr($message, $pos + 1));
						}
						else {
							$pos     = strpos($message, ' ', 1);
							$name    = trim(substr($message, 0, $pos));
							$message = ltrim(substr($message, $pos));
						}
						
						$result = WCF::getDB()->sendQuery('SELECT userID FROM wcf'.WCF_N.'_user WHERE username=\'' . $name . '\'');
						while($row  = WCF::getDB()->fetchArray($result)) {
							$toUser = $row['userID'];
							break;
						}
						
						if(WCF::getUser()->getPermission('user.shoutbox.canWhisper')) {
							if($toUser == -1) {
								$this->errormessage = eval('return "' . WCF::getLanguage()->get('net.northshirebund.wbb.shoutbox.usernotfound') . '";');
							}
							else {
								WCF::getDB()->sendQuery("
									INSERT INTO wcf".WCF_N."_shoutbox(userid,to_user,message_text,message_time,message_ip)
									VALUES ('".$userid."','".$toUser."','".escapeString($message)."','".TIME_NOW."', '".UserUtil::getIpAddress()."')"
								);
									
								$messageid = WCF::getDB()->getInsertID();
								
								WCF::getDB()->sendQuery("DELETE FROM wcf".WCF_N."_shoutbox WHERE message_id<'".($messageid-SHOUTBOX_STOREDMESSAGES)."'");
							}
						}
						else {
							$this->errormessage = WCF::getLanguage()->get('net.northshirebund.wbb.shoutbox.nowhisperallowed');
						}
					}
					else {
						WCF::getDB()->sendQuery("
							INSERT INTO wcf".WCF_N."_shoutbox(userid,message_text,message_time,message_ip)
							VALUES ('".$userid."','".escapeString($message)."','".TIME_NOW."', '".UserUtil::getIpAddress()."')"
						);
							
						$messageid = WCF::getDB()->getInsertID();
				 		
				 		WCF::getDB()->sendQuery("DELETE FROM wcf".WCF_N."_shoutbox WHERE message_id<'".($messageid-SHOUTBOX_STOREDMESSAGES)."'");
				 	}
				}
				else {
					$this->errormessage = WCF::getLanguage()->get('net.northshirebund.wbb.shoutbox.nopostingallowed');
				}
			}
		}
		
		if(isset($_REQUEST['since'])) {
			$this->lastmessage = intval($_REQUEST['since']);
		}
		
		if(isset($_REQUEST['output']) && $_REQUEST['output'] == 'xml') {
			$this->xmloutput = true;
		}
		
		return;
	}
	
	
	protected function readMessages() {
		$parser = MessageParser::getInstance();
		$parser->setOutputType('text/html');
		
		$canWhisper = WCF::getUser()->getPermission('user.shoutbox.canWhisper');
		
		$sql = '
			SELECT		sb.*, 
						u.userID, u.username,
						u2.username AS to_username
			FROM 		wcf'.WCF_N.'_shoutbox	sb
			LEFT JOIN	wcf'.WCF_N.'_user		u
			USING		(userid)
			LEFT JOIN	wcf'.WCF_N.'_user		u2
			ON			(sb.to_user = u2.userID)
			WHERE		message_time > '.$this->lastmessage.' AND (to_user=0 OR to_user='.WCF::getUser()->userID.' OR u.userID='.WCF::getUser()->userID.')
			ORDER BY 	message_time DESC
			LIMIT		0,'.SHOUTBOX_MESSAGECOUNT.'
		';		
		
		$result = WCF::getDB()->sendQuery($sql);
		//WCF::getTPL()->assign('sql', $sql);
		
		while($row = WCF::getDB()->fetchArray($result)) {
			$message              = $row['message_text'];
			$row['class']         = 'shoutboxMessage';
			$row['isEmote']       = '0';
			$row['isWhisperIn']   = '0';
			$row['isWhisperOut']  = '0';
			
			// incoming whispers
			if($row['to_user'] != 0 && $row['to_user'] == WCF::getUser()->userID) {
				$row['class']        = 'shoutboxWhisperIn';
				$row['isWhisperIn']  = '1';
			}
			// outgoing whispers
			else if($row['to_user'] != 0) {
				$row['class']        = 'shoutboxWhisperOut';
				$row['isWhisperOut'] = '1';
			}
			// emotes
			else if(substr($message, 0, 4) == '/me ') {
				$row['class']        = 'shoutboxEmote';
				$row['isEmote']      = '1';
				$message             = substr($message, 4);
			}
			
			// parse URLs
			$message = URLParser::parse($message);
			
			// parse smilies
			$message = $parser->parse($message, /*smilies*/true, /*html*/false, SHOUTBOX_ALLOWBBCODE, /*highlighting*/false);
			
			// convert [url] into <a href...
			if(!SHOUTBOX_ALLOWBBCODE) {
				$message = preg_replace('/\[url\]([a-zA-Z]+\:\/\/.+?)\[\/url\]/', '<a href="\1" target="_blank">\1</a>', $message);
				$message = preg_replace('/\[url\](.+?)\[\/url\]/', '<a href="http://\1" target="_blank">\1</a>', $message);
			}
			
			// convert *emote* into <i>emote</i>
			$message = preg_replace('/\*(.+?)\*/', '<i>\1</i>', $message);
			
			$row['message_text'] = $message;
			
			// set name for guests
			if ($row['userID'] == 0) {
				$row['username'] = WCF::getLanguage()->get('net.northshirebund.wbb.shoutbox.guest');
			}
			
			// set whisperable username, if the current user is able to whisper
			if ($row['userID'] != 0 && $row['userID'] != WCF::getUser()->userID && $canWhisper) {
				$row['username_whisper'] = '<a href="javascript:parent.sbWhisperTo(\''.StringUtil::encodeHTML($row['username']).'\');">'.$row['username'].'</a>';
			}
			else {
				$row['username_whisper'] = $row['username'];
			}
			
			array_push($this->messages, $row);
			
			$this->lastmessage = max($this->lastmessage, $row['message_time']);
		}
		
		if($this->xmloutput) {
			$this->messages = array_reverse($this->messages);
		}
		
		return;
	}
	
	
	/**
	 * @see Page::assignVariables()
	 */
	public function assignVariables() {
		parent::assignVariables();
		
		$this->readMessages();
		
		WCF::getTPL()->assign(array(
			'messages'		=> $this->messages,
			'lastmessage'	=> $this->lastmessage,
			'errormessage'  => $this->errormessage
		));
	}
	
	
	/** * @see Page::show() */
	public function show() { // assign variables 
		$this->assignVariables();		
		
		// call show event
		EventHandler::fireAction($this, 'show');
		
		// update session with referrer as request URI,
		// so the site, which embeds the shoutbox, will appear in the user online list
		if(isset($_SERVER['HTTP_REFERER'])) {
			$referrer = $_SERVER['HTTP_REFERER'];
			
			if(strpos($referrer, 'index.php') == -1) {
				$referrer .= 'index.php';
			}
			
			WCF::getSession()->requestURI = $referrer;
		}
		
		// show template
		if($this->xmloutput) {
			if (!empty($this->templateNameXml)) {
				header('Content-type: text/xml');
				WCF::getTPL()->display($this->templateNameXml, false);
			}
		}
		else {
			if (!empty($this->templateName)) {
				WCF::getTPL()->display($this->templateName);
			}
		}
		
		return;
	}
}
?>