Roster.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * XMPPHP: The PHP XMPP Library
  4. * Copyright (C) 2008 Nathanael C. Fritz
  5. * This file is part of SleekXMPP.
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. *
  21. * @category xmpphp
  22. * @package XMPPHP
  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. * @author Alexander Birkner (https://github.com/BirknerAlex)
  27. * @author zorn-v (https://github.com/zorn-v/xmpphp/)
  28. * @author GNU social
  29. * @copyright 2008 Nathanael C. Fritz
  30. */
  31. namespace XMPPHP;
  32. /**
  33. * XMPPHP Roster
  34. *
  35. * @package XMPPHP
  36. * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
  37. * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
  38. * @author Michael Garvin <JID: gar@netflint.net>
  39. * @copyright 2008 Nathanael C. Fritz
  40. * @version $Id$
  41. */
  42. class Roster
  43. {
  44. /**
  45. * Roster array, handles contacts and presence. Indexed by jid.
  46. * Contains array with potentially two indexes 'contact' and 'presence'
  47. * @var array
  48. */
  49. protected $roster_array = [];
  50. /**
  51. * Constructor
  52. * @param array $roster_array
  53. */
  54. public function __construct($roster_array = [])
  55. {
  56. if ($this->verifyRoster($roster_array)) {
  57. $this->roster_array = $roster_array; //Allow for pre-population with existing roster
  58. } else {
  59. $this->roster_array = [];
  60. }
  61. }
  62. /**
  63. * Check that a given roster array is of a valid structure (empty is still valid)
  64. *
  65. * @param array $roster_array
  66. * @return bool true for valid, false otherwise
  67. */
  68. protected function verifyRoster(array $roster_array): bool
  69. {
  70. #TODO once we know *what* a valid roster array looks like
  71. return true;
  72. }
  73. /**
  74. * Retrieve contact via jid
  75. *
  76. * @param string $jid
  77. * @return array|null
  78. */
  79. public function getContact(string $jid): ?array
  80. {
  81. if ($this->isContact($jid)) {
  82. return $this->roster_array[$jid]['contact'];
  83. }
  84. return null;
  85. }
  86. /**
  87. * Discover if a contact exists in the roster via jid
  88. *
  89. * @param string $jid
  90. * @return bool
  91. */
  92. public function isContact(string $jid): bool
  93. {
  94. return (array_key_exists($jid, $this->roster_array));
  95. }
  96. /**
  97. * Set presence
  98. *
  99. * @param string $presence
  100. * @param int $priority
  101. * @param string $show
  102. * @param string $status
  103. */
  104. public function setPresence(string $presence, int $priority, string $show, string $status): void
  105. {
  106. $presence = explode('/', $presence, 2);
  107. $jid = $presence[0];
  108. $resource = isset($presence[1]) ? $presence[1] : '';
  109. if ($show != 'unavailable') {
  110. if (!$this->isContact($jid)) {
  111. $this->addContact($jid, 'not-in-roster');
  112. }
  113. $this->roster_array[$jid]['presence'][$resource] = ['priority' => $priority, 'show' => $show, 'status' => $status];
  114. } else { //Nuke unavailable resources to save memory
  115. unset($this->roster_array[$jid]['resource'][$resource]);
  116. unset($this->roster_array[$jid]['presence'][$resource]);
  117. }
  118. }
  119. /**
  120. * Add given contact to roster
  121. *
  122. * @param string $jid
  123. * @param string $subscription (optional)
  124. * @param string $name (optional)
  125. * @param array $groups (optional)
  126. */
  127. public function addContact(
  128. string $jid,
  129. string $subscription = 'none',
  130. string $name = '',
  131. array $groups = []
  132. ): void {
  133. $contact = ['jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups];
  134. if ($this->isContact($jid)) {
  135. $this->roster_array[$jid]['contact'] = $contact;
  136. } else {
  137. $this->roster_array[$jid] = ['contact' => $contact];
  138. }
  139. }
  140. /**
  141. * Get presence
  142. *
  143. * @param string $jid
  144. * @return array best presence for jid
  145. */
  146. public function getPresence(string $jid): array
  147. {
  148. $split = explode('/', $jid, 2);
  149. $jid = $split[0];
  150. if ($this->isContact($jid)) {
  151. $current = [
  152. 'resource' => '',
  153. 'active' => '',
  154. 'priority' => -129, //Priorities can only be -128 = 127
  155. 'show' => '',
  156. 'status' => ''
  157. ];
  158. foreach ($this->roster_array[$jid]['presence'] as $resource => $presence) {
  159. //Highest available priority or just highest priority
  160. if ($presence['priority'] > $current['priority'] and
  161. (
  162. ($presence['show'] == "chat" or $presence['show'] == "available") or
  163. ($current['show'] != "chat" or $current['show'] != "available")
  164. )
  165. ) {
  166. $current = $presence;
  167. $current['resource'] = $resource;
  168. }
  169. }
  170. return $current;
  171. }
  172. return [];
  173. }
  174. /**
  175. * Get roster
  176. *
  177. * @return array roster_array
  178. */
  179. public function getRoster(): array
  180. {
  181. return $this->roster_array;
  182. }
  183. }