3 * XMPPHP: The PHP XMPP Library
4 * Copyright (C) 2008 Nathanael C. Fritz
5 * This file is part of SleekXMPP.
7 * XMPPHP is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * XMPPHP is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XMPPHP; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
24 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
25 * @author Michael Garvin <JID: gar@netflint.net>
26 * @copyright 2008 Nathanael C. Fritz
30 * XMPPHP Roster Object
34 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
35 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
36 * @author Michael Garvin <JID: gar@netflint.net>
37 * @copyright 2008 Nathanael C. Fritz
43 * Roster array, handles contacts and presence. Indexed by jid.
44 * Contains array with potentially two indexes 'contact' and 'presence'
47 protected $roster_array = array();
52 public function __construct($roster_array = array()) {
53 if ($this->verifyRoster($roster_array)) {
54 $this->roster_array = $roster_array; //Allow for prepopulation with existing roster
56 $this->roster_array = array();
62 * Check that a given roster array is of a valid structure (empty is still valid)
64 * @param array $roster_array
66 protected function verifyRoster($roster_array) {
67 #TODO once we know *what* a valid roster array looks like
73 * Add given contact to roster
76 * @param string $subscription
78 * @param array $groups
80 public function addContact($jid, $subscription, $name='', $groups=array()) {
81 $contact = array('jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups);
82 if ($this->isContact($jid)) {
83 $this->roster_array[$jid]['contact'] = $contact;
85 $this->roster_array[$jid] = array('contact' => $contact);
91 * Retrieve contact via jid
95 public function getContact($jid) {
96 if ($this->isContact($jid)) {
97 return $this->roster_array[$jid]['contact'];
103 * Discover if a contact exists in the roster via jid
107 public function isContact($jid) {
108 return (array_key_exists($jid, $this->roster_array));
115 * @param string $presence
116 * @param integer $priority
117 * @param string $show
118 * @param string $status
120 public function setPresence($presence, $priority, $show, $status) {
121 list($jid, $resource) = split("/", $presence);
122 if ($show != 'unavailable') {
123 if (!$this->isContact($jid)) {
124 $this->addContact($jid, 'not-in-roster');
126 $resource = $resource ? $resource : '';
127 $this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status);
128 } else { //Nuke unavailable resources to save memory
129 unset($this->roster_array[$jid]['resource'][$resource]);
135 * Return best presence for jid
139 public function getPresence($jid) {
140 $split = split("/", $jid);
142 if($this->isContact($jid)) {
143 $current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127
144 foreach($this->roster_array[$jid]['presence'] as $resource => $presence) {
145 //Highest available priority or just highest priority
146 if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) {
147 $current = $presence;
148 $current['resource'] = $resource;
159 public function getRoster() {
160 return $this->roster_array;