* @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 © 2009 tiggersWelt.net */ require_once ('tiggerXMPP/extension.php'); require_once ('phpEvents/socket/stream/xml/tag.php'); /** * XEP-0012: Last activity * * Small implemtation of XEP 0012 for tiggerXMPP * * @class XEP_0012 * @package tiggerXMPP */ class tiggerXMPP_XEP_0012 extends tiggerXMPP_Extension { /* Our XMPP-Namespace */ const NAMESPACE = "jabber:iq:last"; /* Internal Start-Time (needed for responses) */ private $StartTime = 0; // {{{ __construct /** * Constructor of this class * * @param object $XMPP Handle of XMPP-Instance * * @access public * @return void */ public function __construct ($XMPP) { parent::__construct ($XMPP); $this->StartTime = time (); } // }}} // {{{ handle /** * Handle an incoming packet * * @param object $Tag Packet to handle * * @access public * @return object */ public function handle ($Tag) { $Resp = new phpEvents_Socket_Stream_XML_Tag ('query'); $Resp->setNamespace ($Tag->getNamespace ()); $Resp->setAttribute ('seconds', $this->getLastActivity ()); return $Resp; } // }}} // {{{ getLastActivity /** * Retrive the last activity of an entity according to XEP-0012 * * @param string $JID (optional) JID of entity * * @access public * @return int * @todo Support for last status? */ public function getLastActivity ($JID = null) { if ($JID === null) { if (is_object ($P = $this->getParent ()) && ($P->Type == XMPP_Stream::STREAM_TYPE_CLIENT)) return time () - max ($P->getLastPacket (), $this->StartTime); else return time () - $this->StartTime; } elseif (is_object ($P = $this->getParent ())) { $Resp = new phpEvents_Socket_Stream_XML_Tag ('iq'); $Resp->setOriginator ($P->getJID ()); $Resp->setDestination ($JID); $Resp->setAttribute ('id', $P->getUniqueID ()); $Resp->setAttribute ('type', 'get'); $tagQuery = new phpEvents_Socket_Stream_XML_Tag ('query', $Resp); $tagQuery->setNamespace (self::NAMESPACE); $P->sendXML ($Resp); $Resp = $P->waitBlock (array (), $Resp->getAttribute ('id')); if ($Resp->haveSubtags ('error') || !$Resp->haveSubtags ('query')) return null; if (!is_object ($tagQuery = $Resp->getSubtagByName ('query'))) return false; return intval ($tagQuery->seconds); } return false; } // }}} } ?>