* @revision 05 * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons Attribution-Share Alike 3.0 Germany * @homepage http://oss.tiggerswelt.net/xmpp * @copyright Copyright © 2008 tiggersWelt.net */ /** * XMPP Session * ============ * Session-based IM-Implementation for tiggersWelt.net * * @class XMPP_Session * @package tiggerXMPP */ class XMPP_Session { protected $LastMessage = NULL; protected $XMPP = NULL; private $Handle = NULL; public $JID = ""; public $Ressource = ""; public $ThreadID = ""; public static function &requestSession(&$XMPP, $JID, $Message = NULL, $Class = NULL) { if (!($Session =& self::getSession($XMPP, $JID, $Message))) $Session =& self::newSession($XMPP, $JID, $Message, $Class); return $Session; } public static function &getSession(&$XMPP, $JID, $Message = NULL) { global $XMPP_SESSION_CACHE; $Res = XMPP_Stream::getJID (false, false, true, $JID); $JID = XMPP_Stream::getJID (true, true, false, $JID); $Thread = (is_object($Message) ? (isset($Message->Thread) ? $Message->Thread : (isset($Message->thread) ? $Message->thread : NULL)) : NULL); if (!is_array($XMPP_SESSION_CACHE)) $XMPP_SESSION_CACHE = array(); if (!isset($XMPP_SESSION_CACHE[$JID])) return false; foreach ($XMPP_SESSION_CACHE[$JID] as $ID=>$Session) { if (($Thread !== NULL) && ($Session->ThreadID != $Thread)) continue; else return $XMPP_SESSION_CACHE[$JID][$ID]; } } public static function &newSession(&$XMPP, $JID, $Message = NULL, $Class = NULL) { global $XMPP_SESSION_CACHE; $Res = XMPP_Stream::getJID (false, false, true, $JID); $JID = XMPP_Stream::getJID (true, true, false, $JID); if (!is_array($XMPP_SESSION_CACHE)) $XMPP_SESSION_CACHE = array(); if (!isset($XMPP_SESSION_CACHE[$JID])) $XMPP_SESSION_CACHE[$JID] = array(); if ($Class == NULL) $Instance =& new XMPP_Session; else $Instance =& new $Class; $Instance->JID = $JID; $Instance->Ressource = $Res; $Instance->setXMPP($XMPP); $Instance->ThreadID = md5(time() . $JID); if (is_object($Message)) { $Instance->setLastMessage($Message); if (isset($Message->Thread)) $Instance->ThreadID = $Message->Thread; elseif (isset($Message->thread)) $Instance->ThreadID = $Message->thread; } $XMPP_SESSION_CACHE[$JID][] =& $Instance; return $Instance; } public function setXMPP(&$XMPP) { $this->XMPP =& $XMPP; } public function setLastMessage(&$Message) { $this->LastMessage =& $Message; } public function sendMessage($Message, $Type = XMPP_Stream::MESSAGE_NORMAL, $Subject = "") { $this->XMPP->sendMessage($this->JID . "/" . $this->Ressource, $Message, $Type, $Subject, $this->ThreadID); } public function reply($Message, $toMessage = NULL) { if ($toMessage == NULL) $toMessage =& $this->LastMessage; if (!is_object($toMessage)) return false; self::sendMessage($Message, $toMessage->Type, ""); } } ?>