Roster.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. * @copyright 2008 Nathanael C. Fritz
  27. */
  28. /**
  29. * XMPPHP Roster Object
  30. *
  31. * @category xmpphp
  32. * @package XMPPHP
  33. * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
  34. * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
  35. * @author Michael Garvin <JID: gar@netflint.net>
  36. * @copyright 2008 Nathanael C. Fritz
  37. * @version $Id$
  38. */
  39. class Roster {
  40. /**
  41. * Roster array, handles contacts and presence. Indexed by jid.
  42. * Contains array with potentially two indexes 'contact' and 'presence'
  43. * @var array
  44. */
  45. protected $roster_array = array();
  46. /**
  47. * Constructor
  48. *
  49. */
  50. public function __construct($roster_array = array()) {
  51. if ($this->verifyRoster($roster_array)) {
  52. $this->roster_array = $roster_array; //Allow for prepopulation with existing roster
  53. } else {
  54. $this->roster_array = array();
  55. }
  56. }
  57. /**
  58. *
  59. * Check that a given roster array is of a valid structure (empty is still valid)
  60. *
  61. * @param array $roster_array
  62. */
  63. protected function verifyRoster($roster_array) {
  64. #TODO once we know *what* a valid roster array looks like
  65. return True;
  66. }
  67. /**
  68. *
  69. * Add given contact to roster
  70. *
  71. * @param string $jid
  72. * @param string $subscription
  73. * @param string $name
  74. * @param array $groups
  75. */
  76. public function addContact($jid, $subscription, $name='', $groups=array()) {
  77. $contact = array('jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups);
  78. if ($this->isContact($jid)) {
  79. $this->roster_array[$jid]['contact'] = $contact;
  80. } else {
  81. $this->roster_array[$jid] = array('contact' => $contact);
  82. }
  83. }
  84. /**
  85. *
  86. * Retrieve contact via jid
  87. *
  88. * @param string $jid
  89. */
  90. public function getContact($jid) {
  91. if ($this->isContact($jid)) {
  92. return $this->roster_array[$jid]['contact'];
  93. }
  94. }
  95. /**
  96. *
  97. * Discover if a contact exists in the roster via jid
  98. *
  99. * @param string $jid
  100. */
  101. public function isContact($jid) {
  102. return (array_key_exists($jid, $this->roster_array));
  103. }
  104. /**
  105. *
  106. * Set presence
  107. *
  108. * @param string $presence
  109. * @param integer $priority
  110. * @param string $show
  111. * @param string $status
  112. */
  113. public function setPresence($presence, $priority, $show, $status) {
  114. list($jid, $resource) = split("/", $presence);
  115. if ($show != 'unavailable') {
  116. if (!$this->isContact($jid)) {
  117. $this->addContact($jid, 'not-in-roster');
  118. }
  119. $resource = $resource ? $resource : '';
  120. $this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status);
  121. } else { //Nuke unavailable resources to save memory
  122. unset($this->roster_array[$jid]['resource'][$resource]);
  123. }
  124. }
  125. /*
  126. *
  127. * Return best presence for jid
  128. *
  129. * @param string $jid
  130. */
  131. public function getPresence($jid) {
  132. $split = split("/", $jid);
  133. $jid = $split[0];
  134. if($this->isContact($jid)) {
  135. $current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127
  136. foreach($this->roster_array[$jid]['presence'] as $resource => $presence) {
  137. //Highest available priority or just highest priority
  138. if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) {
  139. $current = $presence;
  140. $current['resource'] = $resource;
  141. }
  142. }
  143. return $current;
  144. }
  145. }
  146. /**
  147. *
  148. * Get roster
  149. *
  150. */
  151. public function getRoster() {
  152. return $this->roster_array;
  153. }
  154. }
  155. ?>