Roster.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 mixed|void
  78. */
  79. public function getContact(string $jid)
  80. {
  81. if ($this->isContact($jid)) {
  82. return $this->roster_array[$jid]['contact'];
  83. }
  84. return;
  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
  124. * @param string $name (optional)
  125. * @param array $groups (optional)
  126. */
  127. public function addContact(string $jid, string $subscription, string $name = '', array $groups = []): void
  128. {
  129. $contact = ['jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups];
  130. if ($this->isContact($jid)) {
  131. $this->roster_array[$jid]['contact'] = $contact;
  132. } else {
  133. $this->roster_array[$jid] = ['contact' => $contact];
  134. }
  135. }
  136. /**
  137. * Get presence
  138. *
  139. * @param string $jid
  140. * @return array best presence for jid
  141. */
  142. public function getPresence(string $jid): array
  143. {
  144. $split = explode('/', $jid, 2);
  145. $jid = $split[0];
  146. if ($this->isContact($jid)) {
  147. $current = [
  148. 'resource' => '',
  149. 'active' => '',
  150. 'priority' => -129, //Priorities can only be -128 = 127
  151. 'show' => '',
  152. 'status' => ''
  153. ];
  154. foreach ($this->roster_array[$jid]['presence'] as $resource => $presence) {
  155. //Highest available priority or just highest priority
  156. if ($presence['priority'] > $current['priority'] and
  157. (
  158. ($presence['show'] == "chat" or $presence['show'] == "available") or
  159. ($current['show'] != "chat" or $current['show'] != "available")
  160. )
  161. ) {
  162. $current = $presence;
  163. $current['resource'] = $resource;
  164. }
  165. }
  166. return $current;
  167. }
  168. return [];
  169. }
  170. /**
  171. * Get roster
  172. *
  173. * @return array roster_array
  174. */
  175. public function getRoster(): array
  176. {
  177. return $this->roster_array;
  178. }
  179. }