2008-12-12 03:15:00 +09:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* XMPPHP: The PHP XMPP Library
|
|
|
|
* Copyright (C) 2008 Nathanael C. Fritz
|
|
|
|
* This file is part of SleekXMPP.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-12-12 03:15:00 +09:00
|
|
|
* 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.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-12-12 03:15:00 +09:00
|
|
|
* 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.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-12-12 03:15:00 +09:00
|
|
|
* 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
|
|
|
|
*
|
2019-04-21 09:23:50 +09:00
|
|
|
* @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)
|
|
|
|
* @author zorn-v (https://github.com/zorn-v/xmpphp/)
|
|
|
|
* @author GNU social
|
2008-12-12 03:15:00 +09:00
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
|
|
|
*/
|
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
namespace XMPPHP;
|
|
|
|
|
2008-12-12 03:15:00 +09:00
|
|
|
/**
|
2019-04-21 09:23:50 +09:00
|
|
|
* XMPPHP Roster
|
|
|
|
*
|
|
|
|
* @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$
|
2008-12-12 03:15:00 +09:00
|
|
|
*/
|
2019-04-21 09:23:50 +09:00
|
|
|
class Roster
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Roster array, handles contacts and presence. Indexed by jid.
|
|
|
|
* Contains array with potentially two indexes 'contact' and 'presence'
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
protected $roster_array = [];
|
2019-04-21 09:23:50 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param array $roster_array
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function __construct($roster_array = [])
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
if ($this->verifyRoster($roster_array)) {
|
|
|
|
$this->roster_array = $roster_array; //Allow for pre-population with existing roster
|
|
|
|
} else {
|
2019-11-17 01:51:01 +09:00
|
|
|
$this->roster_array = [];
|
2019-04-21 09:23:50 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that a given roster array is of a valid structure (empty is still valid)
|
|
|
|
*
|
|
|
|
* @param array $roster_array
|
2019-11-17 01:51:01 +09:00
|
|
|
* @return bool true for valid, false otherwise
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
protected function verifyRoster(array $roster_array): bool
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
#TODO once we know *what* a valid roster array looks like
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-12 03:15:00 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Retrieve contact via jid
|
|
|
|
*
|
|
|
|
* @param string $jid
|
2019-11-17 01:51:01 +09:00
|
|
|
* @return mixed|void
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function getContact(string $jid)
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
if ($this->isContact($jid)) {
|
|
|
|
return $this->roster_array[$jid]['contact'];
|
|
|
|
}
|
2019-11-17 01:51:01 +09:00
|
|
|
return;
|
2019-04-21 09:23:50 +09:00
|
|
|
}
|
2008-12-12 03:15:00 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Discover if a contact exists in the roster via jid
|
|
|
|
*
|
|
|
|
* @param string $jid
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function isContact(string $jid): bool
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
return (array_key_exists($jid, $this->roster_array));
|
|
|
|
}
|
2008-12-12 03:15:00 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Set presence
|
|
|
|
*
|
|
|
|
* @param string $presence
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param int $priority
|
2019-04-21 09:23:50 +09:00
|
|
|
* @param string $show
|
|
|
|
* @param string $status
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function setPresence(string $presence, int $priority, string $show, string $status): void
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
$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');
|
|
|
|
}
|
2019-11-17 01:51:01 +09:00
|
|
|
$this->roster_array[$jid]['presence'][$resource] = ['priority' => $priority, 'show' => $show, 'status' => $status];
|
2019-04-21 09:23:50 +09:00
|
|
|
} else { //Nuke unavailable resources to save memory
|
|
|
|
unset($this->roster_array[$jid]['resource'][$resource]);
|
|
|
|
unset($this->roster_array[$jid]['presence'][$resource]);
|
|
|
|
}
|
|
|
|
}
|
2008-12-12 03:15:00 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Add given contact to roster
|
|
|
|
*
|
|
|
|
* @param string $jid
|
|
|
|
* @param string $subscription
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param string $name (optional)
|
|
|
|
* @param array $groups (optional)
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function addContact(string $jid, string $subscription, string $name = '', array $groups = []): void
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
2019-11-17 01:51:01 +09:00
|
|
|
$contact = ['jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups];
|
2019-04-21 09:23:50 +09:00
|
|
|
if ($this->isContact($jid)) {
|
|
|
|
$this->roster_array[$jid]['contact'] = $contact;
|
|
|
|
} else {
|
2019-11-17 01:51:01 +09:00
|
|
|
$this->roster_array[$jid] = ['contact' => $contact];
|
2019-04-21 09:23:50 +09:00
|
|
|
}
|
|
|
|
}
|
2008-12-12 03:15:00 +09:00
|
|
|
|
2019-11-17 01:51:01 +09:00
|
|
|
/**
|
|
|
|
* Get presence
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
|
|
|
* @param string $jid
|
2019-11-17 01:51:01 +09:00
|
|
|
* @return array best presence for jid
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function getPresence(string $jid): array
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
$split = explode('/', $jid, 2);
|
|
|
|
$jid = $split[0];
|
|
|
|
if ($this->isContact($jid)) {
|
2019-11-17 01:51:01 +09:00
|
|
|
$current = [
|
|
|
|
'resource' => '',
|
|
|
|
'active' => '',
|
|
|
|
'priority' => -129, //Priorities can only be -128 = 127
|
|
|
|
'show' => '',
|
|
|
|
'status' => ''
|
|
|
|
];
|
2019-04-21 09:23:50 +09:00
|
|
|
foreach ($this->roster_array[$jid]['presence'] as $resource => $presence) {
|
|
|
|
//Highest available priority or just highest priority
|
2019-11-17 01:51:01 +09:00
|
|
|
if ($presence['priority'] > $current['priority'] and
|
|
|
|
(
|
|
|
|
($presence['show'] == "chat" or $presence['show'] == "available") or
|
|
|
|
($current['show'] != "chat" or $current['show'] != "available")
|
|
|
|
)
|
|
|
|
) {
|
2019-04-21 09:23:50 +09:00
|
|
|
$current = $presence;
|
|
|
|
$current['resource'] = $resource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $current;
|
|
|
|
}
|
2019-11-17 01:51:01 +09:00
|
|
|
return [];
|
2019-04-21 09:23:50 +09:00
|
|
|
}
|
2008-12-12 03:15:00 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Get roster
|
|
|
|
*
|
2019-11-17 01:51:01 +09:00
|
|
|
* @return array roster_array
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function getRoster(): array
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
return $this->roster_array;
|
|
|
|
}
|
2008-12-12 03:15:00 +09:00
|
|
|
}
|