diff options
| author | Tristan Zur <tzur@webserver.ccwn.org> | 2016-08-15 21:12:21 +0200 |
|---|---|---|
| committer | Tristan Zur <tzur@webserver.ccwn.org> | 2016-08-15 21:12:21 +0200 |
| commit | c257d5b4f1c8d589e1ab0d44f6e68451f1518e68 (patch) | |
| tree | a08aea2d4a91b9f6e9737e84008e12e7a2f9d379 | |
| parent | 854ca75400a8bae53f5fc708c87c822b02bbcda8 (diff) | |
Introducing notification via XMPP
| -rw-r--r-- | modules/gallery/helpers/gallery_event.php | 4 | ||||
| -rw-r--r-- | modules/notification/helpers/BOSH.php | 183 | ||||
| -rw-r--r-- | modules/notification/helpers/Exception.php | 41 | ||||
| -rw-r--r-- | modules/notification/helpers/Roster.php | 158 | ||||
| -rw-r--r-- | modules/notification/helpers/XMLObj.php | 153 | ||||
| -rw-r--r-- | modules/notification/helpers/XMLStream.php | 807 | ||||
| -rw-r--r-- | modules/notification/helpers/XMPP.php | 415 | ||||
| -rw-r--r-- | modules/notification/helpers/XMPPLog.php | 114 | ||||
| -rw-r--r-- | modules/notification/helpers/notification.php | 109 | ||||
| -rw-r--r-- | modules/notification/views/item_added.txt.php | 2 | ||||
| -rw-r--r-- | modules/notification/views/item_deleted.txt.php | 2 | ||||
| -rw-r--r-- | modules/notification/views/item_updated.txt.php | 4 | ||||
| -rw-r--r-- | modules/user/controllers/admin_users.php | 2 | ||||
| -rw-r--r-- | modules/user/controllers/users.php | 3 | ||||
| -rw-r--r-- | modules/user/helpers/user_installer.php | 7 | ||||
| -rw-r--r-- | modules/user/models/user.php | 1 | ||||
| -rw-r--r-- | modules/user/module.info | 2 | ||||
| -rw-r--r-- | modules/user/views/admin_users.html.php | 4 |
18 files changed, 1990 insertions, 21 deletions
diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index a319b9c..bdac37a 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -586,9 +586,9 @@ class gallery_event_Core { $v = new View("user_profile_info.html"); $fields = array("name" => t("Name"), "locale" => t("Language Preference"), - "email" => t("Email"), "full_name" => t("Full name"), "url" => t("Web site")); + "email" => t("Email"), "full_name" => t("Full name"), "url" => t("Web site"), "jabberid" => t("Jabber ID")); if (!$data->user->guest) { - $fields = array("name" => t("Name"), "full_name" => t("Full name"), "url" => t("Web site")); + $fields = array("name" => t("Name"), "full_name" => t("Full name"), "url" => t("Web site"), "jabberid" => t("Jabber ID")); } $v->user_profile_data = array(); foreach ($fields as $field => $label) { diff --git a/modules/notification/helpers/BOSH.php b/modules/notification/helpers/BOSH.php new file mode 100644 index 0000000..19379c6 --- /dev/null +++ b/modules/notification/helpers/BOSH.php @@ -0,0 +1,183 @@ +<?php
+namespace BirknerAlex\XMPPHP;
+ /**
+ * XMPPHP: The PHP XMPP Library
+ * Copyright (C) 2008 Nathanael C. Fritz
+ * This file is part of SleekXMPP.
+ *
+ * XMPPHP is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XMPPHP is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XMPPHP; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @author Alexander Birkner (https://github.com/BirknerAlex)
+ * @copyright 2008 Nathanael C. Fritz
+ */
+/**
+ * XMPPHP Main Class
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @copyright 2008 Nathanael C. Fritz
+ * @version $Id$
+ */
+class BOSH extends XMPP
+{
+ protected $rid;
+ protected $sid;
+ protected $http_server;
+ protected $http_buffer = Array();
+ protected $session = false;
+ public function connect($server, $wait = '1', $session = false)
+ {
+ $this->http_server = $server;
+ $this->use_encryption = false;
+ $this->session = $session;
+ $this->rid = 3001;
+ $this->sid = null;
+ if ($session) {
+ $this->loadSession();
+ }
+ if (!$this->sid) {
+ $body = $this->__buildBody();
+ $body->addAttribute('hold', '1');
+ $body->addAttribute('to', $this->host);
+ $body->addAttribute('route', "xmpp:{$this->host}:{$this->port}");
+ $body->addAttribute('secure', 'true');
+ $body->addAttribute('xmpp:version', '1.6', 'urn:xmpp:xbosh');
+ $body->addAttribute('wait', strval($wait));
+ $body->addAttribute('ack', '1');
+ $body->addAttribute('xmlns:xmpp', 'urn:xmpp:xbosh');
+ $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
+ xml_parse($this->parser, $buff, false);
+ $response = $this->__sendBody($body);
+ $rxml = new \SimpleXMLElement($response);
+ $this->sid = $rxml['sid'];
+ } else {
+ $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
+ xml_parse($this->parser, $buff, false);
+ }
+ }
+ public function __sendBody($body = null, $recv = true)
+ {
+ if (!$body) {
+ $body = $this->__buildBody();
+ }
+ $ch = curl_init($this->http_server);
+ curl_setopt($ch, CURLOPT_HEADER, 0);
+ curl_setopt($ch, CURLOPT_POST, 1);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $body->asXML());
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+ $header = array('Accept-Encoding: gzip, deflate', 'Content-Type: text/xml; charset=utf-8');
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
+ curl_setopt($ch, CURLOPT_VERBOSE, 0);
+ $output = '';
+ if ($recv) {
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ $output = curl_exec($ch);
+ $this->http_buffer[] = $output;
+ }
+ curl_close($ch);
+ return $output;
+ }
+ public function __buildBody($sub = null)
+ {
+ $xml = new \SimpleXMLElement("<body xmlns='http://jabber.org/protocol/httpbind' xmlns:xmpp='urn:xmpp:xbosh' />");
+ $xml->addAttribute('content', 'text/xml; charset=utf-8');
+ $xml->addAttribute('rid', $this->rid);
+ $this->rid += 1;
+ if ($this->sid) $xml->addAttribute('sid', $this->sid);
+ #if($this->sid) $xml->addAttribute('xmlns', 'http://jabber.org/protocol/httpbind');
+ $xml->addAttribute('xml:lang', 'en');
+ if ($sub) { // ok, so simplexml is lame
+ $p = dom_import_simplexml($xml);
+ $c = dom_import_simplexml($sub);
+ $cn = $p->ownerDocument->importNode($c, true);
+ $p->appendChild($cn);
+ $xml = simplexml_import_dom($p);
+ }
+ return $xml;
+ }
+ public function __process()
+ {
+ if ($this->http_buffer) {
+ $this->__parseBuffer();
+ } else {
+ $this->__sendBody();
+ $this->__parseBuffer();
+ }
+ }
+ public function __parseBuffer()
+ {
+ while ($this->http_buffer) {
+ $idx = key($this->http_buffer);
+ $buffer = $this->http_buffer[$idx];
+ unset($this->http_buffer[$idx]);
+ if ($buffer) {
+ $xml = new \SimpleXMLElement($buffer);
+ $children = $xml->xpath('child::node()');
+ foreach ($children as $child) {
+ $buff = $child->asXML();
+ $this->log->log("RECV: $buff", Log::LEVEL_VERBOSE);
+ xml_parse($this->parser, $buff, false);
+ }
+ }
+ }
+ }
+ public function send($msg)
+ {
+ $this->log->log("SEND: $msg", Log::LEVEL_VERBOSE);
+ $msg = new \SimpleXMLElement($msg);
+ #$msg->addAttribute('xmlns', 'jabber:client');
+ $this->__sendBody($this->__buildBody($msg), true);
+ #$this->__parseBuffer();
+ }
+ public function reset()
+ {
+ $this->xml_depth = 0;
+ unset($this->xmlobj);
+ $this->xmlobj = array();
+ $this->setupParser();
+ #$this->send($this->stream_start);
+ $body = $this->__buildBody();
+ $body->addAttribute('to', $this->host);
+ $body->addAttribute('xmpp:restart', 'true', 'urn:xmpp:xbosh');
+ $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
+ $response = $this->__sendBody($body);
+ $this->been_reset = true;
+ xml_parse($this->parser, $buff, false);
+ }
+ public function loadSession()
+ {
+ if (isset($_SESSION['XMPPHP_BOSH_RID'])) $this->rid = $_SESSION['XMPPHP_BOSH_RID'];
+ if (isset($_SESSION['XMPPHP_BOSH_SID'])) $this->sid = $_SESSION['XMPPHP_BOSH_SID'];
+ if (isset($_SESSION['XMPPHP_BOSH_authed'])) $this->authed = $_SESSION['XMPPHP_BOSH_authed'];
+ if (isset($_SESSION['XMPPHP_BOSH_jid'])) $this->jid = $_SESSION['XMPPHP_BOSH_jid'];
+ if (isset($_SESSION['XMPPHP_BOSH_fulljid'])) $this->fulljid = $_SESSION['XMPPHP_BOSH_fulljid'];
+ }
+ public function saveSession()
+ {
+ $_SESSION['XMPPHP_BOSH_RID'] = (string)$this->rid;
+ $_SESSION['XMPPHP_BOSH_SID'] = (string)$this->sid;
+ $_SESSION['XMPPHP_BOSH_authed'] = (boolean)$this->authed;
+ $_SESSION['XMPPHP_BOSH_jid'] = (string)$this->jid;
+ $_SESSION['XMPPHP_BOSH_fulljid'] = (string)$this->fulljid;
+ }
+}
\ No newline at end of file diff --git a/modules/notification/helpers/Exception.php b/modules/notification/helpers/Exception.php new file mode 100644 index 0000000..ec660fc --- /dev/null +++ b/modules/notification/helpers/Exception.php @@ -0,0 +1,41 @@ +<?php
+/**
+ * XMPPHP: The PHP XMPP Library
+ * Copyright (C) 2008 Nathanael C. Fritz
+ * This file is part of SleekXMPP.
+ *
+ * XMPPHP is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XMPPHP is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XMPPHP; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @author Alexander Birkner (https://github.com/BirknerAlex)
+ * @copyright 2008 Nathanael C. Fritz
+ */
+/**
+ * XMPPHP Main Class
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @copyright 2008 Nathanael C. Fritz
+ * @version $Id$
+ */
+class XMPPException extends \Exception {
+}
diff --git a/modules/notification/helpers/Roster.php b/modules/notification/helpers/Roster.php new file mode 100644 index 0000000..783344a --- /dev/null +++ b/modules/notification/helpers/Roster.php @@ -0,0 +1,158 @@ +<?php
+/**
+ * XMPPHP: The PHP XMPP Library
+ * Copyright (C) 2008 Nathanael C. Fritz
+ * This file is part of SleekXMPP.
+ *
+ * XMPPHP is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XMPPHP is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XMPPHP; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @author Alexander Birkner (https://github.com/BirknerAlex)
+ * @copyright 2008 Nathanael C. Fritz
+ */
+/**
+ * XMPPHP Main Class
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @copyright 2008 Nathanael C. Fritz
+ * @version $Id$
+ */
+class Roster {
+ /**
+ * Roster array, handles contacts and presence. Indexed by jid.
+ * Contains array with potentially two indexes 'contact' and 'presence'
+ * @var array
+ */
+ protected $roster_array = array();
+ /**
+ * Constructor
+ *
+ */
+ public function __construct($roster_array = array()) {
+ if ($this->verifyRoster($roster_array)) {
+ $this->roster_array = $roster_array; //Allow for prepopulation with existing roster
+ } else {
+ $this->roster_array = array();
+ }
+ }
+ /**
+ *
+ * Check that a given roster array is of a valid structure (empty is still valid)
+ *
+ * @param array $roster_array
+ */
+ protected function verifyRoster($roster_array) {
+ #TODO once we know *what* a valid roster array looks like
+ return True;
+ }
+ /**
+ *
+ * Add given contact to roster
+ *
+ * @param string $jid
+ * @param string $subscription
+ * @param string $name
+ * @param array $groups
+ */
+ public function addContact($jid, $subscription, $name='', $groups=array()) {
+ $contact = array('jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups);
+ if ($this->isContact($jid)) {
+ $this->roster_array[$jid]['contact'] = $contact;
+ } else {
+ $this->roster_array[$jid] = array('contact' => $contact);
+ }
+ }
+ /**
+ *
+ * Retrieve contact via jid
+ *
+ * @param string $jid
+ */
+ public function getContact($jid) {
+ if ($this->isContact($jid)) {
+ return $this->roster_array[$jid]['contact'];
+ }
+ }
+ /**
+ *
+ * Discover if a contact exists in the roster via jid
+ *
+ * @param string $jid
+ */
+ public function isContact($jid) {
+ return (array_key_exists($jid, $this->roster_array));
+ }
+ /**
+ *
+ * Set presence
+ *
+ * @param string $presence
+ * @param integer $priority
+ * @param string $show
+ * @param string $status
+ */
+ public function setPresence($presence, $priority, $show, $status) {
+ $presence = explode('/', $presence, 2);
+ $jid = $presence[0];
+ $resource = isset($presence[1]) ? $presence[1] : '';
+ if ($show != 'unavailable') {
+ if (!$this->isContact($jid)) {
+ $this->addContact($jid, 'not-in-roster');
+ }
+ $this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status);
+ } else { //Nuke unavailable resources to save memory
+ unset($this->roster_array[$jid]['resource'][$resource]);
+ unset($this->roster_array[$jid]['presence'][$resource]);
+ }
+ }
+ /*
+ *
+ * Return best presence for jid
+ *
+ * @param string $jid
+ */
+ public function getPresence($jid) {
+ $split = explode('/', $jid, 2);
+ $jid = $split[0];
+ if($this->isContact($jid)) {
+ $current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127
+ foreach($this->roster_array[$jid]['presence'] as $resource => $presence) {
+ //Highest available priority or just highest priority
+ if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) {
+ $current = $presence;
+ $current['resource'] = $resource;
+ }
+ }
+ return $current;
+ }
+ }
+ /**
+ *
+ * Get roster
+ *
+ */
+ public function getRoster() {
+ return $this->roster_array;
+ }
+}
+?>
\ No newline at end of file diff --git a/modules/notification/helpers/XMLObj.php b/modules/notification/helpers/XMLObj.php new file mode 100644 index 0000000..1416ced --- /dev/null +++ b/modules/notification/helpers/XMLObj.php @@ -0,0 +1,153 @@ +<?php
+ /**
+ * XMPPHP: The PHP XMPP Library
+ * Copyright (C) 2008 Nathanael C. Fritz
+ * This file is part of SleekXMPP.
+ *
+ * XMPPHP is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XMPPHP is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XMPPHP; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @author Alexander Birkner (https://github.com/BirknerAlex)
+ * @copyright 2008 Nathanael C. Fritz
+ */
+/**
+ * XMPPHP Main Class
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @copyright 2008 Nathanael C. Fritz
+ * @version $Id$
+ */
+class XMLObj {
+ /**
+ * Tag name
+ *
+ * @var string
+ */
+ public $name;
+
+ /**
+ * Namespace
+ *
+ * @var string
+ */
+ public $ns;
+
+ /**
+ * Attributes
+ *
+ * @var array
+ */
+ public $attrs = array();
+
+ /**
+ * Subs?
+ *
+ * @var array
+ */
+ public $subs = array();
+
+ /**
+ * Node data
+ *
+ * @var string
+ */
+ public $data = '';
+ /**
+ * Constructor
+ *
+ * @param string $name
+ * @param string $ns
+ * @param array $attrs
+ * @param string $data
+ */
+ public function __construct($name, $ns = '', $attrs = array(), $data = '') {
+ $this->name = strtolower($name);
+ $this->ns = $ns;
+ if(is_array($attrs) && count($attrs)) {
+ foreach($attrs as $key => $value) {
+ $this->attrs[strtolower($key)] = $value;
+ }
+ }
+ $this->data = $data;
+ }
+ /**
+ * Dump this XML Object to output.
+ *
+ * @param integer $depth
+ */
+ public function printObj($depth = 0) {
+ print str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data;
+ print "\n";
+ foreach($this->subs as $sub) {
+ $sub->printObj($depth + 1);
+ }
+ }
+ /**
+ * Return this XML Object in xml notation
+ *
+ * @param string $str
+ */
+ public function toString($str = '') {
+ $str .= "<{$this->name} xmlns='{$this->ns}' ";
+ foreach($this->attrs as $key => $value) {
+ if($key != 'xmlns') {
+ $value = htmlspecialchars($value);
+ $str .= "$key='$value' ";
+ }
+ }
+ $str .= ">";
+ foreach($this->subs as $sub) {
+ $str .= $sub->toString();
+ }
+ $body = htmlspecialchars($this->data);
+ $str .= "$body</{$this->name}>";
+ return $str;
+ }
+ /**
+ * Has this XML Object the given sub?
+ *
+ * @param string $name
+ * @return boolean
+ */
+ public function hasSub($name, $ns = null) {
+ foreach($this->subs as $sub) {
+ if(($name == "*" or $sub->name == $name) and ($ns == null or $sub->ns == $ns)) return true;
+ }
+ return false;
+ }
+ /**
+ * Return a sub
+ *
+ * @param string $name
+ * @param string $attrs
+ * @param string $ns
+ */
+ public function sub($name, $attrs = null, $ns = null) {
+ #TODO attrs is ignored
+ foreach($this->subs as $sub) {
+ if($sub->name == $name and ($ns == null or $sub->ns == $ns)) {
+ return $sub;
+ }
+ }
+ }
+}
\ No newline at end of file diff --git a/modules/notification/helpers/XMLStream.php b/modules/notification/helpers/XMLStream.php new file mode 100644 index 0000000..346a768 --- /dev/null +++ b/modules/notification/helpers/XMLStream.php @@ -0,0 +1,807 @@ +<?php
+ /**
+ * XMPPHP: The PHP XMPP Library
+ * Copyright (C) 2008 Nathanael C. Fritz
+ * This file is part of SleekXMPP.
+ *
+ * XMPPHP is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XMPPHP is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XMPPHP; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @author Alexander Birkner (https://github.com/BirknerAlex)
+ * @copyright 2008 Nathanael C. Fritz
+ */
+/**
+ * XMPPHP Main Class
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @copyright 2008 Nathanael C. Fritz
+ * @version $Id$
+ */
+class XMLStream {
+ /**
+ * @var resource
+ */
+ protected $socket;
+ /**
+ * @var resource
+ */
+ protected $parser;
+ /**
+ * @var string
+ */
+ protected $buffer;
+ /**
+ * @var integer
+ */
+ protected $xml_depth = 0;
+ /**
+ * @var string
+ */
+ protected $host;
+ /**
+ * @var integer
+ */
+ protected $port;
+ /**
+ * @var string
+ */
+ protected $stream_start = '<stream>';
+ /**
+ * @var string
+ */
+ protected $stream_end = '</stream>';
+ /**
+ * @var boolean
+ */
+ protected $disconnected = true;
+ /**
+ * @var boolean
+ */
+ protected $sent_disconnect = false;
+ /**
+ * @var array
+ */
+ protected $ns_map = array();
+ /**
+ * @var array
+ */
+ protected $current_ns = array();
+ /**
+ * @var array
+ */
+ protected $xmlobj = null;
+ /**
+ * @var array
+ */
+ protected $nshandlers = array();
+ /**
+ * @var array
+ */
+ protected $xpathhandlers = array();
+ /**
+ * @var array
+ */
+ protected $idhandlers = array();
+ /**
+ * @var array
+ */
+ protected $eventhandlers = array();
+ /**
+ * @var integer
+ */
+ protected $lastid = 0;
+ /**
+ * @var string
+ */
+ protected $default_ns;
+ /**
+ * @var string
+ */
+ protected $until = '';
+ /**
+ * @var string
+ */
+ protected $until_count = '';
+ /**
+ * @var array
+ */
+ protected $until_happened = false;
+ /**
+ * @var array
+ */
+ protected $until_payload = array();
+ /**
+ * @var XMPXMPPPLog
+ */
+ protected $log;
+ /**
+ * @var boolean
+ */
+ protected $reconnect = true;
+ /**
+ * @var boolean
+ */
+ protected $been_reset = false;
+ /**
+ * @var boolean
+ */
+ protected $is_server;
+ /**
+ * @var float
+ */
+ protected $last_send = 0;
+ /**
+ * @var boolean
+ */
+ protected $use_ssl = false;
+ /**
+ * @var integer
+ */
+ protected $reconnectTimeout = 30;
+ /**
+ * Constructor
+ *
+ * @param string $host
+ * @param string $port
+ * @param boolean $printlog
+ * @param string $loglevel
+ * @param boolean $is_server
+ */
+ public function __construct($host = null, $port = null, $printlog = false, $loglevel = null, $is_server = false) {
+ $this->reconnect = !$is_server;
+ $this->is_server = $is_server;
+ $this->host = $host;
+ $this->port = $port;
+ $this->setupParser();
+ $this->log = new XMPPLog($printlog, $loglevel);
+ }
+ /**
+ * Destructor
+ * Cleanup connection
+ */
+ public function __destruct() {
+ if(!$this->disconnected && $this->socket) {
+ $this->disconnect();
+ }
+ }
+
+ /**
+ * Return the log instance
+ *
+ * @return XMPPLog
+ */
+ public function getLog() {
+ return $this->log;
+ }
+
+ /**
+ * Get next ID
+ *
+ * @return integer
+ */
+ public function getId() {
+ $this->lastid++;
+ return $this->lastid;
+ }
+ /**
+ * Set SSL
+ *
+ * @return integer
+ */
+ public function useSSL($use=true) {
+ $this->use_ssl = $use;
+ }
+ /**
+ * Add ID Handler
+ *
+ * @param integer $id
+ * @param string $pointer
+ * @param string $obj
+ */
+ public function addIdHandler($id, $pointer, $obj = null) {
+ $this->idhandlers[$id] = array($pointer, $obj);
+ }
+ /**
+ * Add Handler
+ *
+ * @param string $name
+ * @param string $ns
+ * @param string $pointer
+ * @param string $obj
+ * @param integer $depth
+ */
+ public function addHandler($name, $ns, $pointer, $obj = null, $depth = 1) {
+ #TODO deprication warning
+ $this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
+ }
+ /**
+ * Add XPath Handler
+ *
+ * @param string $xpath
+ * @param string $pointer
+ * @param
+ */
+ public function addXPathHandler($xpath, $pointer, $obj = null) {
+ if (preg_match_all("/\(?{[^\}]+}\)?(\/?)[^\/]+/", $xpath, $regs)) {
+ $ns_tags = $regs[0];
+ } else {
+ $ns_tags = array($xpath);
+ }
+ foreach($ns_tags as $ns_tag) {
+ list($l, $r) = explode('}', $ns_tag);
+ if ($r != null) {
+ $xpart = array(substr($l, 1), $r);
+ } else {
+ $xpart = array(null, $l);
+ }
+ $xpath_array[] = $xpart;
+ }
+ $this->xpathhandlers[] = array($xpath_array, $pointer, $obj);
+ }
+ /**
+ * Add Event Handler
+ *
+ * @param integer $id
+ * @param string $pointer
+ * @param string $obj
+ */
+ public function addEventHandler($name, $pointer, $obj) {
+ $this->eventhandlers[] = array($name, $pointer, $obj);
+ }
+ /**
+ * Connect to XMPP Host
+ *
+ * @param integer $timeout Timeout in seconds
+ * @param boolean $persistent
+ * @param boolean $sendinit Send XMPP starting sequence after connect
+ * automatically
+ *
+ * @throws XMPPException When the connection fails
+ */
+ public function connect($timeout = 30, $persistent = false, $sendinit = true) {
+ $this->sent_disconnect = false;
+ $starttime = time();
+
+ do {
+ $this->disconnected = false;
+ $this->sent_disconnect = false;
+ if($persistent) {
+ $conflag = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
+ } else {
+ $conflag = STREAM_CLIENT_CONNECT;
+ }
+ $conntype = 'tcp';
+ if($this->use_ssl) $conntype = 'ssl';
+ $this->log->log("Connecting to $conntype://{$this->host}:{$this->port}");
+ try {
+ $this->socket = @stream_socket_client("$conntype://{$this->host}:{$this->port}", $errno, $errstr, $timeout, $conflag);
+ } catch (Exception $e) {
+ throw new XMPPException($e->getMessage());
+ }
+ if(!$this->socket) {
+ $this->log->log("Could not connect.", XMPPLog::LEVEL_ERROR);
+ $this->disconnected = true;
+ # Take it easy for a few seconds
+ sleep(min($timeout, 5));
+ }
+ } while (!$this->socket && (time() - $starttime) < $timeout);
+
+ if ($this->socket) {
+ stream_set_blocking($this->socket, 1);
+ if($sendinit) $this->send($this->stream_start);
+ } else {
+ throw new XMPPException("Could not connect before timeout.");
+ }
+ }
+ /**
+ * Reconnect XMPP Host
+ *
+ * @throws XMPPException When the connection fails
+ * @uses $reconnectTimeout
+ * @see setReconnectTimeout()
+ */
+ public function doReconnect() {
+ if(!$this->is_server) {
+ $this->log->log("Reconnecting ($this->reconnectTimeout)...", XMPPLog::LEVEL_WARNING);
+ $this->connect($this->reconnectTimeout, false, false);
+ $this->reset();
+ $this->event('reconnect');
+ }
+ }
+ public function setReconnectTimeout($timeout) {
+ $this->reconnectTimeout = $timeout;
+ }
+
+ /**
+ * Disconnect from XMPP Host
+ */
+ public function disconnect() {
+ $this->log->log("Disconnecting...", XMPPLog::LEVEL_VERBOSE);
+ if(false == (bool) $this->socket) {
+ return;
+ }
+ $this->reconnect = false;
+ $this->send($this->stream_end);
+ $this->sent_disconnect = true;
+ $this->processUntil('end_stream', 5);
+ $this->disconnected = true;
+ }
+ /**
+ * Are we are disconnected?
+ *
+ * @return boolean
+ */
+ public function isDisconnected() {
+ return $this->disconnected;
+ }
+ /**
+ * Checks if the given string is closed with the same tag as it is
+ * opened. We try to be as fast as possible here.
+ *
+ * @param string $buff Read buffer of __process()
+ *
+ * @return boolean true if the buffer seems to be complete
+ */
+ protected function bufferComplete($buff)
+ {
+ if (substr($buff, -1) != '>') {
+ return false;
+ }
+ //we always have a space since the namespace needs to be
+ //declared. could be a tab, though
+ $start = substr(
+ $buff, 1,
+ min(strpos($buff, '>', 2), strpos($buff, ' ', 2)) - 1
+ );
+ $stop = substr($buff, -strlen($start) - 3);
+ if ($start == '?xml') {
+ //starting with an xml tag. this means a stream is being
+ // opened, which is not much of data, so no fear it's
+ // not complete
+ return true;
+ }
+ if (substr($stop, -2) == '/>') {
+ //one tag, i.e. <success />
+ return true;
+ }
+ if ('</' . $start . '>' == $stop) {
+ return true;
+ }
+ return false;
+ }
+ /**
+ * Core reading tool
+ *
+ * @param mixed $maximum Limit when to return
+ * - 0: only read if data is immediately ready
+ * - NULL: wait forever and ever
+ * - integer: process for this amount of microseconds
+ * @param boolean $return_when_received Immediately return when data have been
+ * received
+ *
+ * @return boolean True when all goes well, false when something fails
+ */
+ private function __process($maximum = 5, $return_when_received = false)
+ {
+ $remaining = $maximum;
+
+ do {
+ $starttime = (microtime(true) * 1000000);
+ $read = array($this->socket);
+ $write = array();
+ $except = array();
+ if (is_null($maximum)) {
+ $secs = NULL;
+ $usecs = NULL;
+ } else if ($maximum == 0) {
+ $secs = 0;
+ $usecs = 0;
+ } else {
+ $usecs = $remaining % 1000000;
+ $secs = floor(($remaining - $usecs) / 1000000);
+ }
+ $updated = @stream_select($read, $write, $except, $secs, $usecs);
+ if ($updated === false) {
+ $this->log->log("Error on stream_select()", XMPPLog::LEVEL_VERBOSE);
+ if ($this->reconnect) {
+ $this->doReconnect();
+ } else {
+ fclose($this->socket);
+ $this->socket = NULL;
+ return false;
+ }
+ } else if ($updated > 0) {
+ $buff = '';
+ do {
+ if ($buff != '') {
+ //disable blocking for now because fread() will
+ // block until the 4k are full if we already
+ // read a part of the packet
+ stream_set_blocking($this->socket, 0);
+ }
+ $part = fread($this->socket, 4096);
+ stream_set_blocking($this->socket, 1);
+ if (!$part) {
+ if($this->reconnect) {
+ $this->doReconnect();
+ } else {
+ fclose($this->socket);
+ $this->socket = NULL;
+ return false;
+ }
+ }
+ $this->log->log("RECV: $part", XMPPLog::LEVEL_VERBOSE);
+ $buff .= $part;
+ } while (!$this->bufferComplete($buff));
+ xml_parse($this->parser, $buff, false);
+ if ($return_when_received) {
+ return true;
+ }
+ } else {
+ # $updated == 0 means no changes during timeout.
+ }
+ $endtime = (microtime(true)*1000000);
+ $time_past = $endtime - $starttime;
+ $remaining = $remaining - $time_past;
+ } while (is_null($maximum) || $remaining > 0);
+ return true;
+ }
+
+ /**
+ * Process
+ *
+ * @return string
+ */
+ public function process() {
+ $this->__process(NULL);
+ }
+ /**
+ * Process until a timeout occurs
+ *
+ * @param integer $timeout Time in seconds
+ *
+ * @return string
+ *
+ * @see __process()
+ */
+ public function processTime($timeout=NULL) {
+ if (is_null($timeout)) {
+ return $this->__process(NULL);
+ } else {
+ return $this->__process($timeout * 1000000);
+ }
+ }
+ /**
+ * Process until a specified event or a timeout occurs
+ *
+ * @param string|array $event Event name or array of event names
+ * @param integer $timeout Timeout in seconds
+ *
+ * @return array Payload
+ */
+ public function processUntil($event, $timeout = -1)
+ {
+ if ($this->disconnected) {
+ throw new XMPPException('You need to connect first');
+ }
+ $start = time();
+ if (!is_array($event)) {
+ $event = array($event);
+ }
+ $this->until[] = $event;
+ end($this->until);
+ $event_key = key($this->until);
+ reset($this->until);
+ $this->until_count[$event_key] = 0;
+ $updated = '';
+ while (!$this->disconnected
+ && $this->until_count[$event_key] < 1
+ && ($timeout == -1 || time() - $start < $timeout)
+ ) {
+ $maximum = $timeout == -1
+ ? NULL
+ : ($timeout - (time() - $start)) * 1000000;
+ $ret = $this->__process($maximum, true);
+ if (!$ret) {
+ break;
+ }
+ }
+ if (array_key_exists($event_key, $this->until_payload)) {
+ $payload = $this->until_payload[$event_key];
+ unset($this->until_payload[$event_key]);
+ unset($this->until_count[$event_key]);
+ unset($this->until[$event_key]);
+ } else {
+ $payload = array();
+ }
+ return $payload;
+ }
+ /**
+ * Obsolete?
+ */
+ public function Xapply_socket($socket) {
+ $this->socket = $socket;
+ }
+ /**
+ * XML start callback
+ *
+ * @see xml_set_element_handler
+ *
+ * @param resource $parser
+ * @param string $name
+ */
+ public function startXML($parser, $name, $attr) {
+ if($this->been_reset) {
+ $this->been_reset = false;
+ $this->xml_depth = 0;
+ }
+ $this->xml_depth++;
+ if(array_key_exists('XMLNS', $attr)) {
+ $this->current_ns[$this->xml_depth] = $attr['XMLNS'];
+ } else {
+ $this->current_ns[$this->xml_depth] = $this->current_ns[$this->xml_depth - 1];
+ if(!$this->current_ns[$this->xml_depth]) $this->current_ns[$this->xml_depth] = $this->default_ns;
+ }
+ $ns = $this->current_ns[$this->xml_depth];
+ foreach($attr as $key => $value) {
+ if(strstr($key, ":")) {
+ $key = explode(':', $key);
+ $key = $key[1];
+ $this->ns_map[$key] = $value;
+ }
+ }
+ if(!strstr($name, ":") === false)
+ {
+ $name = explode(':', $name);
+ $ns = $this->ns_map[$name[0]];
+ $name = $name[1];
+ }
+ $obj = new XMLObj($name, $ns, $attr);
+ if($this->xml_depth > 1) {
+ $this->xmlobj[$this->xml_depth - 1]->subs[] = $obj;
+ }
+ $this->xmlobj[$this->xml_depth] = $obj;
+ }
+ /**
+ * XML end callback
+ *
+ * @see xml_set_element_handler
+ *
+ * @param resource $parser
+ * @param string $name
+ */
+ public function endXML($parser, $name) {
+ #$this->log->log("Ending $name", XMPPLog::LEVEL_DEBUG);
+ #print "$name\n";
+ if($this->been_reset) {
+ $this->been_reset = false;
+ $this->xml_depth = 0;
+ }
+ $this->xml_depth--;
+ if($this->xml_depth == 1) {
+ #clean-up old objects
+ #$found = false; #FIXME This didn't appear to be in use --Gar
+ foreach($this->xpathhandlers as $handler) {
+ if (is_array($this->xmlobj) && array_key_exists(2, $this->xmlobj)) {
+ $searchxml = $this->xmlobj[2];
+ $nstag = array_shift($handler[0]);
+ if (($nstag[0] == null or $searchxml->ns == $nstag[0]) and ($nstag[1] == "*" or $nstag[1] == $searchxml->name)) {
+ foreach($handler[0] as $nstag) {
+ if ($searchxml !== null and $searchxml->hasSub($nstag[1], $ns=$nstag[0])) {
+ $searchxml = $searchxml->sub($nstag[1], $ns=$nstag[0]);
+ } else {
+ $searchxml = null;
+ break;
+ }
+ }
+ if ($searchxml !== null) {
+ if($handler[2] === null) $handler[2] = $this;
+ $this->log->log("Calling {$handler[1]}", XMPPLog::LEVEL_DEBUG);
+ $handler[2]->$handler[1]($this->xmlobj[2]);
+ }
+ }
+ }
+ }
+ foreach($this->nshandlers as $handler) {
+ if($handler[4] != 1 and array_key_exists(2, $this->xmlobj) and $this->xmlobj[2]->hasSub($handler[0])) {
+ $searchxml = $this->xmlobj[2]->sub($handler[0]);
+ } elseif(is_array($this->xmlobj) and array_key_exists(2, $this->xmlobj)) {
+ $searchxml = $this->xmlobj[2];
+ }
+ if($searchxml !== null and $searchxml->name == $handler[0] and ($searchxml->ns == $handler[1] or (!$handler[1] and $searchxml->ns == $this->default_ns))) {
+ if($handler[3] === null) $handler[3] = $this;
+ $this->log->log("Calling {$handler[2]}", XMPPLog::LEVEL_DEBUG);
+ $handler[3]->$handler[2]($this->xmlobj[2]);
+ }
+ }
+ foreach($this->idhandlers as $id => $handler) {
+ if(array_key_exists('id', $this->xmlobj[2]->attrs) and $this->xmlobj[2]->attrs['id'] == $id) {
+ if($handler[1] === null) $handler[1] = $this;
+ $handler[1]->$handler[0]($this->xmlobj[2]);
+ #id handlers are only used once
+ unset($this->idhandlers[$id]);
+ break;
+ }
+ }
+ if(is_array($this->xmlobj)) {
+ $this->xmlobj = array_slice($this->xmlobj, 0, 1);
+ if(isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMLObj) {
+ $this->xmlobj[0]->subs = null;
+ }
+ }
+ unset($this->xmlobj[2]);
+ }
+ if($this->xml_depth == 0 and !$this->been_reset) {
+ if(!$this->disconnected) {
+ if(!$this->sent_disconnect) {
+ $this->send($this->stream_end);
+ }
+ $this->disconnected = true;
+ $this->sent_disconnect = true;
+ fclose($this->socket);
+ if($this->reconnect) {
+ $this->doReconnect();
+ }
+ }
+ $this->event('end_stream');
+ }
+ }
+ /**
+ * XML character callback
+ * @see xml_set_character_data_handler
+ *
+ * @param resource $parser
+ * @param string $data
+ */
+ public function charXML($parser, $data) {
+ if(array_key_exists($this->xml_depth, $this->xmlobj)) {
+ $this->xmlobj[$this->xml_depth]->data .= $data;
+ }
+ }
+ /**
+ * Event?
+ *
+ * @param string $name
+ * @param string $payload
+ */
+ public function event($name, $payload = null) {
+ $this->log->log("EVENT: $name", XMPPLog::LEVEL_DEBUG);
+ foreach($this->eventhandlers as $handler) {
+ if($name == $handler[0]) {
+ if($handler[2] === null) {
+ $handler[2] = $this;
+ }
+ $handler[2]->$handler[1]($payload);
+ }
+ }
+ foreach($this->until as $key => $until) {
+ if(is_array($until)) {
+ if(in_array($name, $until)) {
+ $this->until_payload[$key][] = array($name, $payload);
+ if(!isset($this->until_count[$key])) {
+ $this->until_count[$key] = 0;
+ }
+ $this->until_count[$key] += 1;
+ #$this->until[$key] = false;
+ }
+ }
+ }
+ }
+ /**
+ * Read from socket
+ */
+ public function read() {
+ $buff = @fread($this->socket, 1024);
+ if(!$buff) {
+ if($this->reconnect) {
+ $this->doReconnect();
+ } else {
+ fclose($this->socket);
+ return false;
+ }
+ }
+ $this->log->log("RECV: $buff", XMPPLog::LEVEL_VERBOSE);
+ xml_parse($this->parser, $buff, false);
+ }
+ /**
+ * Send to socket
+ *
+ * @param string $msg
+ */
+ public function send($msg, $timeout=NULL) {
+ if (is_null($timeout)) {
+ $secs = NULL;
+ $usecs = NULL;
+ } else if ($timeout == 0) {
+ $secs = 0;
+ $usecs = 0;
+ } else {
+ $maximum = $timeout * 1000000;
+ $usecs = $maximum % 1000000;
+ $secs = floor(($maximum - $usecs) / 1000000);
+ }
+
+ $read = array();
+ $write = array($this->socket);
+ $except = array();
+
+ $select = @stream_select($read, $write, $except, $secs, $usecs);
+
+ if($select === False) {
+ $this->log->log("ERROR sending message; reconnecting.");
+ $this->doReconnect();
+ # TODO: retry send here
+ return false;
+ } elseif ($select > 0) {
+ $this->log->log("Socket is ready; send it.", XMPPLog::LEVEL_VERBOSE);
+ } else {
+ $this->log->log("Socket is not ready; break.", XMPPLog::LEVEL_ERROR);
+ return false;
+ }
+
+ $sentbytes = @fwrite($this->socket, $msg);
+ $this->log->log("SENT: " . mb_substr($msg, 0, $sentbytes, '8bit'), XMPPLog::LEVEL_VERBOSE);
+ if($sentbytes === FALSE) {
+ $this->log->log("ERROR sending message; reconnecting.", XMPPLog::LEVEL_ERROR);
+ $this->doReconnect();
+ return false;
+ }
+ $this->log->log("Successfully sent $sentbytes bytes.", XMPPLog::LEVEL_VERBOSE);
+ return $sentbytes;
+ }
+ public function time() {
+ list($usec, $sec) = explode(" ", microtime());
+ return (float)$sec + (float)$usec;
+ }
+ /**
+ * Reset connection
+ */
+ public function reset() {
+ $this->xml_depth = 0;
+ unset($this->xmlobj);
+ $this->xmlobj = array();
+ $this->setupParser();
+ if(!$this->is_server) {
+ $this->send($this->stream_start);
+ }
+ $this->been_reset = true;
+ }
+ /**
+ * Setup the XML parser
+ */
+ public function setupParser() {
+ $this->parser = xml_parser_create('UTF-8');
+ xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
+ xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
+ xml_set_object($this->parser, $this);
+ xml_set_element_handler($this->parser, 'startXML', 'endXML');
+ xml_set_character_data_handler($this->parser, 'charXML');
+ }
+ public function readyToProcess() {
+ $read = array($this->socket);
+ $write = array();
+ $except = array();
+ $updated = @stream_select($read, $write, $except, 0);
+ return (($updated !== false) && ($updated > 0));
+ }
+}
diff --git a/modules/notification/helpers/XMPP.php b/modules/notification/helpers/XMPP.php new file mode 100644 index 0000000..69f80df --- /dev/null +++ b/modules/notification/helpers/XMPP.php @@ -0,0 +1,415 @@ +<?php
+ /**
+ * XMPPHP: The PHP XMPP Library
+ * Copyright (C) 2008 Nathanael C. Fritz
+ * This file is part of SleekXMPP.
+ *
+ * XMPPHP is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XMPPHP is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XMPPHP; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @author Alexander Birkner (https://github.com/BirknerAlex)
+ * @copyright 2008 Nathanael C. Fritz
+ */
+/**
+ * XMPPHP Main Class
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @copyright 2008 Nathanael C. Fritz
+ * @version $Id$
+ */
+class XMPP extends XMLStream {
+ /**
+ * @var string
+ */
+ public $server;
+ /**
+ * @var string
+ */
+ public $user;
+
+ /**
+ * @var string
+ */
+ protected $password;
+
+ /**
+ * @var string
+ */
+ protected $resource;
+
+ /**
+ * @var string
+ */
+ protected $fulljid;
+
+ /**
+ * @var string
+ */
+ protected $basejid;
+
+ /**
+ * @var boolean
+ */
+ protected $authed = false;
+ protected $session_started = false;
+
+ /**
+ * @var boolean
+ */
+ protected $auto_subscribe = false;
+
+ /**
+ * @var boolean
+ */
+ protected $use_encryption = true;
+
+ /**
+ * @var boolean
+ */
+ public $track_presence = true;
+
+ /**
+ * @var object
+ */
+ public $roster;
+ /**
+ * Constructor
+ *
+ * @param string $host
+ * @param integer $port
+ * @param string $user
+ * @param string $password
+ * @param string $resource
+ * @param string $server
+ * @param boolean $printlog
+ * @param string $loglevel
+ */
+ public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) {
+ parent::__construct($host, $port, $printlog, $loglevel);
+
+ $this->user = $user;
+ $this->password = $password;
+ $this->resource = $resource;
+ if(!$server) $server = $host;
+ $this->basejid = $this->user . '@' . $this->host;
+ $this->roster = new Roster();
+ $this->track_presence = true;
+ $this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
+ $this->stream_end = '</stream:stream>';
+ $this->default_ns = 'jabber:client';
+
+ $this->addXPathHandler('{http://etherx.jabber.org/streams}features', 'features_handler');
+ $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}success', 'sasl_success_handler');
+ $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}failure', 'sasl_failure_handler');
+ $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-tls}proceed', 'tls_proceed_handler');
+ $this->addXPathHandler('{jabber:client}message', 'message_handler');
+ $this->addXPathHandler('{jabber:client}presence', 'presence_handler');
+ $this->addXPathHandler('iq/{jabber:iq:roster}query', 'roster_iq_handler');
+ }
+ /**
+ * Turn encryption on/ff
+ *
+ * @param boolean $useEncryption
+ */
+ public function useEncryption($useEncryption = true) {
+ $this->use_encryption = $useEncryption;
+ }
+
+ /**
+ * Turn on auto-authorization of subscription requests.
+ *
+ * @param boolean $autoSubscribe
+ */
+ public function autoSubscribe($autoSubscribe = true) {
+ $this->auto_subscribe = $autoSubscribe;
+ }
+ /**
+ * Send XMPP Message
+ *
+ * @param string $to
+ * @param string $body
+ * @param string $type
+ * @param string $subject
+ */
+ public function message($to, $body, $type = 'chat', $subject = null, $payload = null) {
+ if ($this->disconnected) {
+ throw new XMPPException('You need to connect first');
+ }
+ if(empty($type)) {
+ $type = 'chat';
+ }
+ $to = htmlspecialchars($to);
+ $body = htmlspecialchars($body);
+ $subject = htmlspecialchars($subject);
+
+ $out = "<message from=\"{$this->fulljid}\" to=\"$to\" type='$type'>";
+ if($subject) $out .= "<subject>$subject</subject>";
+ $out .= "<body>$body</body>";
+ if($payload) $out .= $payload;
+ $out .= "</message>";
+
+ $this->send($out);
+ }
+ /**
+ * Set Presence
+ *
+ * @param string $status
+ * @param string $show
+ * @param string $to
+ */
+ public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=null) {
+ if ($this->disconnected) {
+ throw new XMPPException('You need to connect first');
+ }
+ if($type == 'available') $type = '';
+ $to = htmlspecialchars($to);
+ $status = htmlspecialchars($status);
+ if($show == 'unavailable') $type = 'unavailable';
+
+ $out = "<presence";
+ if($to) $out .= " to=\"$to\"";
+ if($type) $out .= " type='$type'";
+ if($show == 'available' and !$status and $priority !== null) {
+ $out .= "/>";
+ } else {
+ $out .= ">";
+ if($show != 'available') $out .= "<show>$show</show>";
+ if($status) $out .= "<status>$status</status>";
+ if($priority !== null) $out .= "<priority>$priority</priority>";
+ $out .= "</presence>";
+ }
+
+ $this->send($out);
+ }
+ /**
+ * Send Auth request
+ *
+ * @param string $jid
+ */
+ public function subscribe($jid) {
+ $this->send("<presence type='subscribe' to='{$jid}' from='{$this->fulljid}' />");
+ #$this->send("<presence type='subscribed' to='{$jid}' from='{$this->fulljid}' />");
+ }
+ /**
+ * Message handler
+ *
+ * @param string $xml
+ */
+ public function message_handler($xml) {
+ if(isset($xml->attrs['type'])) {
+ $payload['type'] = $xml->attrs['type'];
+ } else {
+ $payload['type'] = 'chat';
+ }
+ $body = $xml->sub('body');
+ $payload['from'] = $xml->attrs['from'];
+ $payload['body'] = is_object($body) ? $body->data : FALSE; // $xml->sub('body')->data;
+ $payload['xml'] = $xml;
+ $this->log->log("Message: {$payload['body']}", XMPPLog::LEVEL_DEBUG);
+ $this->event('message', $payload);
+ }
+ /**
+ * Presence handler
+ *
+ * @param string $xml
+ */
+ public function presence_handler($xml) {
+ $payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available';
+ $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
+ $payload['from'] = $xml->attrs['from'];
+ $payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
+ $payload['priority'] = (isset($xml->sub('priority')->data)) ? intval($xml->sub('priority')->data) : 0;
+ $payload['xml'] = $xml;
+ if($this->track_presence) {
+ $this->roster->setPresence($payload['from'], $payload['priority'], $payload['show'], $payload['status']);
+ }
+ $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPLog::LEVEL_DEBUG);
+ if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') {
+ if($this->auto_subscribe) {
+ $this->send("<presence type='subscribed' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
+ $this->send("<presence type='subscribe' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
+ }
+ $this->event('subscription_requested', $payload);
+ } elseif(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribed') {
+ $this->event('subscription_accepted', $payload);
+ } else {
+ $this->event('presence', $payload);
+ }
+ }
+ /**
+ * Features handler
+ *
+ * @param string $xml
+ */
+ protected function features_handler($xml) {
+ if($xml->hasSub('starttls') and $this->use_encryption) {
+ $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
+ } elseif($xml->hasSub('bind') and $this->authed) {
+ $id = $this->getId();
+ $this->addIdHandler($id, 'resource_bind_handler');
+ $this->send("<iq xmlns=\"jabber:client\" type=\"set\" id=\"$id\"><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><resource>{$this->resource}</resource></bind></iq>");
+ } else {
+ $this->log->log("Attempting Auth...");
+ if ($this->password) {
+ $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
+ } else {
+ $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>");
+ }
+ }
+ }
+ /**
+ * SASL success handler
+ *
+ * @param string $xml
+ */
+ protected function sasl_success_handler($xml) {
+ $this->log->log("Auth success!");
+ $this->authed = true;
+ $this->reset();
+ }
+
+ /**
+ * SASL feature handler
+ *
+ * @param string $xml
+ */
+ protected function sasl_failure_handler($xml) {
+ $this->log->log("Auth failed!", XMPPLog::LEVEL_ERROR);
+ $this->disconnect();
+
+ throw new XMPPException('Auth failed!');
+ }
+ /**
+ * Resource bind handler
+ *
+ * @param string $xml
+ */
+ protected function resource_bind_handler($xml) {
+ if($xml->attrs['type'] == 'result') {
+ $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
+ $this->fulljid = $xml->sub('bind')->sub('jid')->data;
+ $jidarray = explode('/',$this->fulljid);
+ $this->jid = $jidarray[0];
+ }
+ $id = $this->getId();
+ $this->addIdHandler($id, 'session_start_handler');
+ $this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>");
+ }
+ /**
+ * Retrieves the roster
+ *
+ */
+ public function getRoster() {
+ $id = $this->getID();
+ $this->send("<iq xmlns='jabber:client' type='get' id='$id'><query xmlns='jabber:iq:roster' /></iq>");
+ }
+ /**
+ * Roster iq handler
+ * Gets all packets matching XPath "iq/{jabber:iq:roster}query'
+ *
+ * @param string $xml
+ */
+ protected function roster_iq_handler($xml) {
+ $status = "result";
+ $xmlroster = $xml->sub('query');
+ foreach($xmlroster->subs as $item) {
+ $groups = array();
+ if ($item->name == 'item') {
+ $jid = $item->attrs['jid']; //REQUIRED
+ $name = $item->attrs['name']; //MAY
+ $subscription = $item->attrs['subscription'];
+ foreach($item->subs as $subitem) {
+ if ($subitem->name == 'group') {
+ $groups[] = $subitem->data;
+ }
+ }
+ $contacts[] = array($jid, $subscription, $name, $groups); //Store for action if no errors happen
+ } else {
+ $status = "error";
+ }
+ }
+ if ($status == "result") { //No errors, add contacts
+ foreach($contacts as $contact) {
+ $this->roster->addContact($contact[0], $contact[1], $contact[2], $contact[3]);
+ }
+ }
+ if ($xml->attrs['type'] == 'set') {
+ $this->send("<iq type=\"reply\" id=\"{$xml->attrs['id']}\" to=\"{$xml->attrs['from']}\" />");
+ }
+ }
+ /**
+ * Session start handler
+ *
+ * @param string $xml
+ */
+ protected function session_start_handler($xml) {
+ $this->log->log("Session started");
+ $this->session_started = true;
+ $this->event('session_start');
+ }
+ /**
+ * TLS proceed handler
+ *
+ * @param string $xml
+ */
+ protected function tls_proceed_handler($xml) {
+ $this->log->log("Starting TLS encryption");
+ stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
+ $this->reset();
+ }
+ /**
+ * Retrieves the vcard
+ *
+ */
+ public function getVCard($jid = Null) {
+ $id = $this->getID();
+ $this->addIdHandler($id, 'vcard_get_handler');
+ if($jid) {
+ $this->send("<iq type='get' id='$id' to='$jid'><vCard xmlns='vcard-temp' /></iq>");
+ } else {
+ $this->send("<iq type='get' id='$id'><vCard xmlns='vcard-temp' /></iq>");
+ }
+ }
+ /**
+ * VCard retrieval handler
+ *
+ * @param XML Object $xml
+ */
+ protected function vcard_get_handler($xml) {
+ $vcard_array = array();
+ $vcard = $xml->sub('vcard');
+ // go through all of the sub elements and add them to the vcard array
+ foreach ($vcard->subs as $sub) {
+ if ($sub->subs) {
+ $vcard_array[$sub->name] = array();
+ foreach ($sub->subs as $sub_child) {
+ $vcard_array[$sub->name][$sub_child->name] = $sub_child->data;
+ }
+ } else {
+ $vcard_array[$sub->name] = $sub->data;
+ }
+ }
+ $vcard_array['from'] = $xml->attrs['from'];
+ $this->event('vcard', $vcard_array);
+ }
+}
diff --git a/modules/notification/helpers/XMPPLog.php b/modules/notification/helpers/XMPPLog.php new file mode 100644 index 0000000..5b79dec --- /dev/null +++ b/modules/notification/helpers/XMPPLog.php @@ -0,0 +1,114 @@ +<?php
+ /**
+ * XMPPHP: The PHP XMPP Library
+ * Copyright (C) 2008 Nathanael C. Fritz
+ * This file is part of SleekXMPP.
+ *
+ * XMPPHP is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XMPPHP is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XMPPHP; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @author Alexander Birkner (https://github.com/BirknerAlex)
+ * @copyright 2008 Nathanael C. Fritz
+ */
+/**
+ * XMPPHP Main Class
+ *
+ * @category xmpphp
+ * @package XMPPHP
+ * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
+ * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
+ * @copyright 2008 Nathanael C. Fritz
+ * @version $Id$
+ */
+class XMPPLog {
+
+ const LEVEL_ERROR = 0;
+ const LEVEL_WARNING = 1;
+ const LEVEL_INFO = 2;
+ const LEVEL_DEBUG = 3;
+ const LEVEL_VERBOSE = 4;
+
+ /**
+ * @var array
+ */
+ protected $data = array();
+ /**
+ * @var array
+ */
+ protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
+ /**
+ * @var integer
+ */
+ protected $runlevel;
+ /**
+ * @var boolean
+ */
+ protected $printout;
+ /**
+ * Constructor
+ *
+ * @param boolean $printout
+ * @param string $runlevel
+ */
+ public function __construct($printout = false, $runlevel = self::LEVEL_INFO) {
+ $this->printout = (boolean)$printout;
+ $this->runlevel = (int)$runlevel;
+ }
+ /**
+ * Add a message to the log data array
+ * If printout in this instance is set to true, directly output the message
+ *
+ * @param string $msg
+ * @param integer $runlevel
+ */
+ public function log($msg, $runlevel = self::LEVEL_INFO) {
+ $time = time();
+ #$this->data[] = array($this->runlevel, $msg, $time);
+ if($this->printout and $runlevel <= $this->runlevel) {
+ $this->writeLine($msg, $runlevel, $time);
+ }
+ }
+ /**
+ * Output the complete log.
+ * Log will be cleared if $clear = true
+ *
+ * @param boolean $clear
+ * @param integer $runlevel
+ */
+ public function printout($clear = true, $runlevel = null) {
+ if($runlevel === null) {
+ $runlevel = $this->runlevel;
+ }
+ foreach($this->data as $data) {
+ if($runlevel <= $data[0]) {
+ $this->writeLine($data[1], $runlevel, $data[2]);
+ }
+ }
+ if($clear) {
+ $this->data = array();
+ }
+ }
+
+ protected function writeLine($msg, $runlevel, $time) {
+ //echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
+ echo $time." [".$this->names[$runlevel]."]: ".$msg."\n";
+ flush();
+ }
+}
diff --git a/modules/notification/helpers/notification.php b/modules/notification/helpers/notification.php index 96493a2..7eaa067 100644 --- a/modules/notification/helpers/notification.php +++ b/modules/notification/helpers/notification.php @@ -1,4 +1,10 @@ <?php defined("SYSPATH") or die("No direct script access."); +require_once("Exception.php"); +require_once("XMPPLog.php"); +require_once("Roster.php"); +require_once("XMLObj.php"); +require_once("XMLStream.php"); +require_once("XMPP.php"); /** * Gallery - a web based photo album viewer and editor * Copyright (C) 2000-2013 Bharat Mediratta @@ -66,7 +72,7 @@ class notification { } } - static function get_subscribers($item) { + static function get_subscriber_users($item) { $subscriber_ids = array(); foreach (ORM::factory("subscription") ->select("user_id") @@ -81,20 +87,46 @@ class notification { if (empty($subscriber_ids)) { return array(); } - $users = identity::get_user_list($subscriber_ids); + return identity::get_user_list($subscriber_ids); + } + + static function get_email_subscribers($item) { + $users = self::get_subscriber_users($item); + + if (empty($users)) { + return array(); + } $subscribers = array(); foreach ($users as $user) { - if (access::user_can($user, "view", $item) && !empty($user->email)) { + if (access::user_can($user, "view", $item) + && !empty($user->email) + && empty($user->jabberid)) { $subscribers[$user->email] = $user->locale; } } return $subscribers; } - static function send_item_updated($original, $item) { - foreach (self::get_subscribers($item) as $email => $locale) { - $v = new View("item_updated.html"); + static function get_jabber_subscribers($item) { + $users = self::get_subscriber_users($item); + + if (empty($users)) { + return array(); + } + + $subscribers = array(); + foreach ($users as $user) { + if (access::user_can($user, "view", $item) + && !empty($user->jabberid)) { + $subscribers[$user->jabberid] = $user->locale; + } + } + return $subscribers; + } + + private static function getItemUpdatedView($viewname, $item, $original, $locale) { + $v = new View($viewname); $v->original = $original; $v->item = $item; $v->subject = $item->is_album() ? @@ -102,16 +134,22 @@ class notification { ($item->is_photo() ? t("Photo \"%title\" updated", array("title" => $original->title, "locale" => $locale)) : t("Movie \"%title\" updated", array("title" => $original->title, "locale" => $locale))); + return $v; + } + + static function send_item_updated($original, $item) { + foreach (self::get_jabber_subscribers($item) as $jabberid => $locale) { + $v = self::getItemUpdatedView("item_updated.txt", $item, $original, $locale); + self::notifyViaXMPP($jabberid, $locale, $v->render()); + } + foreach (self::get_email_subscribers($item) as $email => $locale) { + $v = self::getItemUpdatedView("item_updated.html", $item, $original, $locale); self::_notify($email, $locale, $item, $v->render(), $v->subject); } } - static function send_item_add($item) { - $parent = $item->parent(); - foreach (self::get_subscribers($item) as $email => $locale) { - $v = new View("item_added.html"); - $v->item = $item; - $v->subject = $item->is_album() ? + private static function getItemAddSubject($item, $parent, $locale) { + return $item->is_album() ? t("Album \"%title\" added to \"%parent_title\"", array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) : ($item->is_photo() ? @@ -119,14 +157,29 @@ class notification { array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) : t("Movie \"%title\" added to \"%parent_title\"", array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale))); - self::_notify($email, $locale, $item, $v->render(), $v->subject); - } } - static function send_item_deleted($item) { + static function send_item_add($item) { $parent = $item->parent(); - foreach (self::get_subscribers($item) as $email => $locale) { - $v = new View("item_deleted.html"); + Kohana_Log::add("error", "adding item notifiy"); + + foreach (self::get_jabber_subscribers($item) as $jabberid => $locale) { + Kohana_Log::add("error", "adding item notifiy jabber"); + $jabberView = new View("item_added.txt"); + $jabberView->item = $item; + $jabberView->subject = self::getItemAddSubject($item, $parent, $locale); + self::notifyViaXMPP($jabberid, $locale, $jabberView->render()); + } + foreach (self::get_email_subscribers($item) as $email => $locale) { + $emailView = new View("item_added.html"); + $emailView->item = $item; + $emailView->subject = self::getItemAddSubject($item, $parent, $locale); + self::_notify($email, $locale, $item, $emailView->render(), $emailView->subject); + } + } + + private static function getItemDeletedView($viewname, $item, $parent, $locale) { + $v = new View($viewname); $v->item = $item; $v->subject = $item->is_album() ? t("Album \"%title\" removed from \"%parent_title\"", @@ -137,13 +190,24 @@ class notification { : t("Movie \"%title\" removed from \"%parent_title\"", array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale))); + return $v; + } + + static function send_item_deleted($item) { + $parent = $item->parent(); + foreach (self::get_jabber_subscribers($item) as $jabberid => $locale) { + $v = self::getItemDeletedView("item_deleted.txt", $item, $parent, $locale); + self::notifyViaXMPP($jabberid, $locale, $v->render()); + } + foreach (self::get_email_subscribers($item) as $email => $locale) { + $v = self::getItemDeletedView("item_deleted.html", $item, $parent, $locale); self::_notify($email, $locale, $item, $v->render(), $v->subject); } } static function send_comment_published($comment) { $item = $comment->item(); - foreach (self::get_subscribers($item) as $email => $locale) { + foreach (self::get_email_subscribers($item) as $email => $locale) { $v = new View("comment_published.html"); $v->comment = $comment; $v->subject = $item->is_album() ? @@ -197,6 +261,15 @@ class notification { } } + private static function notifyViaXMPP($jabberid, $locale, $text) { + $XMPP = new XMPP('jabber.mail-zur.de', 5222, 'gallery', 'gallery', 'pics'); + $XMPP->connect(5); + $XMPP->processUntil('session_start', 10); + //$XMPP->presence(); + $XMPP->message($jabberid, $text, 'chat'); + $XMPP->disconnect(); + } + private static function _notify($email, $locale, $item, $text, $subject) { if (!batch::in_progress()) { Sendmail::factory() diff --git a/modules/notification/views/item_added.txt.php b/modules/notification/views/item_added.txt.php new file mode 100644 index 0000000..1f96e95 --- /dev/null +++ b/modules/notification/views/item_added.txt.php @@ -0,0 +1,2 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<?= html::clean($subject) ?> (<?= $item->abs_url() ?>) diff --git a/modules/notification/views/item_deleted.txt.php b/modules/notification/views/item_deleted.txt.php new file mode 100644 index 0000000..19c9aab --- /dev/null +++ b/modules/notification/views/item_deleted.txt.php @@ -0,0 +1,2 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<?= html::clean($subject) ?> (<?= $item->parent()->abs_url() ?>) diff --git a/modules/notification/views/item_updated.txt.php b/modules/notification/views/item_updated.txt.php new file mode 100644 index 0000000..594d050 --- /dev/null +++ b/modules/notification/views/item_updated.txt.php @@ -0,0 +1,4 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<?= html::clean($subject) ?> +<? if ($original->title != $item->title): ?>title changed to '<?= html::clean($item->title) ?>'<? endif ?> <? if ($original->description != $item->description): ?>description changed<? endif ?> +(<?= $item->abs_url() ?>) diff --git a/modules/user/controllers/admin_users.php b/modules/user/controllers/admin_users.php index ed589a3..3bb702b 100644 --- a/modules/user/controllers/admin_users.php +++ b/modules/user/controllers/admin_users.php @@ -72,6 +72,7 @@ class Admin_Users_Controller extends Admin_Controller { $user->full_name = $form->add_user->full_name->value; $user->password = $form->add_user->password->value; $user->email = $form->add_user->email->value; + $user->jabberid = $form->add_user->jabberid->value; $user->url = $form->add_user->url->value; $user->locale = $form->add_user->locale->value; $user->admin = $form->add_user->admin->checked; @@ -154,6 +155,7 @@ class Admin_Users_Controller extends Admin_Controller { $user->email = $form->edit_user->email->value; $user->url = $form->edit_user->url->value; $user->locale = $form->edit_user->locale->value; + $user->jabberid = $form->edit_user->jabberid->value; if ($user->id != identity::active_user()->id) { $user->admin = $form->edit_user->admin->checked; } diff --git a/modules/user/controllers/users.php b/modules/user/controllers/users.php index ee81344..b93da8b 100644 --- a/modules/user/controllers/users.php +++ b/modules/user/controllers/users.php @@ -29,6 +29,7 @@ class Users_Controller extends Controller { $valid = $form->validate(); $user->full_name = $form->edit_user->full_name->value; $user->url = $form->edit_user->url->value; + $user->jabberid = $form->edit_user->jabberid->value; if (count(locales::installed()) > 1 && $user->locale != $form->edit_user->locale->value) { @@ -211,6 +212,8 @@ class Users_Controller extends Controller { self::_add_locale_dropdown($group, $user); $group->input("url")->label(t("URL"))->id("g-url")->value($user->url) ->error_messages("url", t("You must enter a valid url")); + $group->input("jabberid")->label(t("Jabber ID"))->id("g-jabberid")->value($user->jabberid) + ->error_messages("length", t("Your jabber ID is too long")); module::event("user_edit_form", $user, $form); $group->submit("")->value(t("Save")); diff --git a/modules/user/helpers/user_installer.php b/modules/user/helpers/user_installer.php index 67f6a3d..1c59acb 100644 --- a/modules/user/helpers/user_installer.php +++ b/modules/user/helpers/user_installer.php @@ -52,6 +52,12 @@ class user_installer { module::clear_var("user", "mininum_password_length"); module::set_version("user", $version = 4); } + + if ($version == 4) { + $db = Database::instance(); + $db->query("ALTER TABLE {users} ADD COLUMN `jabberid` VARCHAR(255) default NULL"); + module::set_version("user", $version = 5); + } } static function uninstall() { @@ -85,6 +91,7 @@ class user_installer { `hash` char(32) default NULL, `url` varchar(255) default NULL, `locale` char(10) default NULL, + `jabberid` VARCHAR(255) default NULL, PRIMARY KEY (`id`), UNIQUE KEY(`hash`), UNIQUE KEY(`name`)) diff --git a/modules/user/models/user.php b/modules/user/models/user.php index af05c0c..0b75b81 100644 --- a/modules/user/models/user.php +++ b/modules/user/models/user.php @@ -80,6 +80,7 @@ class User_Model_Core extends ORM implements User_Definition { "admin" => array("callbacks" => array(array($this, "valid_admin"))), "email" => array("rules" => array("length[1,255]", "valid::email"), "callbacks" => array(array($this, "valid_email"))), + "jabberid" => array("rules" => array("length[1,255]", "valid::email")), "full_name" => array("rules" => array("length[0,255]")), "locale" => array("rules" => array("length[2,10]")), "name" => array("rules" => array("length[1,32]", "required"), diff --git a/modules/user/module.info b/modules/user/module.info index d5128db..9864d79 100644 --- a/modules/user/module.info +++ b/modules/user/module.info @@ -1,6 +1,6 @@ name = "Users and Groups" description = "Gallery 3 user and group management" -version = 4 +version = 5 author_name = "Gallery Team" author_url = "http://codex.galleryproject.org/Gallery:Team" diff --git a/modules/user/views/admin_users.html.php b/modules/user/views/admin_users.html.php index 033c9da..b48c9f1 100644 --- a/modules/user/views/admin_users.html.php +++ b/modules/user/views/admin_users.html.php @@ -63,6 +63,7 @@ <th><?= t("Username") ?></th> <th><?= t("Full name") ?></th> <th><?= t("Email") ?></th> + <th><?= t("Jabber ID") ?></th> <th><?= t("Last login") ?></th> <th><?= t("Albums/Photos") ?></th> <th><?= t("Actions") ?></th> @@ -85,6 +86,9 @@ <?= html::clean($user->email) ?> </td> <td> + <?= html::clean($user->jabberid) ?> + </td> + <td> <?= ($user->last_login == 0) ? "" : gallery::date($user->last_login) ?> </td> <td> |
