Roster.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 = array();
  50. /**
  51. * Constructor
  52. * @param array $roster_array
  53. */
  54. public function __construct($roster_array = 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 = array();
  60. }
  61. }
  62. /**
  63. *
  64. * Check that a given roster array is of a valid structure (empty is still valid)
  65. *
  66. * @param array $roster_array
  67. * @return bool
  68. */
  69. protected function verifyRoster($roster_array)
  70. {
  71. #TODO once we know *what* a valid roster array looks like
  72. return true;
  73. }
  74. /**
  75. *
  76. * Retrieve contact via jid
  77. *
  78. * @param string $jid
  79. * @return mixed
  80. */
  81. public function getContact($jid)
  82. {
  83. if ($this->isContact($jid)) {
  84. return $this->roster_array[$jid]['contact'];
  85. }
  86. }
  87. /**
  88. *
  89. * Discover if a contact exists in the roster via jid
  90. *
  91. * @param string $jid
  92. * @return bool
  93. */
  94. public function isContact($jid)
  95. {
  96. return (array_key_exists($jid, $this->roster_array));
  97. }
  98. /**
  99. *
  100. * Set presence
  101. *
  102. * @param string $presence
  103. * @param integer $priority
  104. * @param string $show
  105. * @param string $status
  106. */
  107. public function setPresence($presence, $priority, $show, $status)
  108. {
  109. $presence = explode('/', $presence, 2);
  110. $jid = $presence[0];
  111. $resource = isset($presence[1]) ? $presence[1] : '';
  112. if ($show != 'unavailable') {
  113. if (!$this->isContact($jid)) {
  114. $this->addContact($jid, 'not-in-roster');
  115. }
  116. $this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status);
  117. } else { //Nuke unavailable resources to save memory
  118. unset($this->roster_array[$jid]['resource'][$resource]);
  119. unset($this->roster_array[$jid]['presence'][$resource]);
  120. }
  121. }
  122. /**
  123. *
  124. * Add given contact to roster
  125. *
  126. * @param string $jid
  127. * @param string $subscription
  128. * @param string $name
  129. * @param array $groups
  130. */
  131. public function addContact($jid, $subscription, $name = '', $groups = array())
  132. {
  133. $contact = array('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] = array('contact' => $contact);
  138. }
  139. }
  140. /*
  141. *
  142. * Return best presence for jid
  143. *
  144. * @param string $jid
  145. */
  146. public function getPresence($jid)
  147. {
  148. $split = explode('/', $jid, 2);
  149. $jid = $split[0];
  150. if ($this->isContact($jid)) {
  151. $current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127
  152. foreach ($this->roster_array[$jid]['presence'] as $resource => $presence) {
  153. //Highest available priority or just highest priority
  154. if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) {
  155. $current = $presence;
  156. $current['resource'] = $resource;
  157. }
  158. }
  159. return $current;
  160. }
  161. }
  162. /**
  163. *
  164. * Get roster
  165. *
  166. */
  167. public function getRoster()
  168. {
  169. return $this->roster_array;
  170. }
  171. }