XMLObj.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 XMLObject
  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 XMLObj
  43. {
  44. /**
  45. * Tag name
  46. *
  47. * @var string
  48. */
  49. public $name;
  50. /**
  51. * Namespace
  52. *
  53. * @var string
  54. */
  55. public $ns;
  56. /**
  57. * Attributes
  58. *
  59. * @var array
  60. */
  61. public $attrs = [];
  62. /**
  63. * Subs?
  64. *
  65. * @var array
  66. */
  67. public $subs = [];
  68. /**
  69. * Node data
  70. *
  71. * @var string
  72. */
  73. public $data = '';
  74. /**
  75. * Constructor
  76. *
  77. * @param string $name
  78. * @param string $ns (optional)
  79. * @param array $attrs (optional)
  80. * @param string $data (optional)
  81. */
  82. public function __construct(string $name, string $ns = '', array $attrs = [], string $data = '')
  83. {
  84. $this->name = strtolower($name);
  85. $this->ns = $ns;
  86. if (is_array($attrs) && count($attrs)) {
  87. foreach ($attrs as $key => $value) {
  88. $this->attrs[strtolower($key)] = $value;
  89. }
  90. }
  91. $this->data = $data;
  92. }
  93. /**
  94. * Dump this XML Object to output.
  95. *
  96. * @param int $depth (optional)
  97. */
  98. public function printObj(int $depth = 0): void
  99. {
  100. print str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data;
  101. print "\n";
  102. foreach ($this->subs as $sub) {
  103. $sub->printObj($depth + 1);
  104. }
  105. }
  106. /**
  107. * Return this XML Object in xml notation
  108. *
  109. * @param string $str (optional)
  110. * @return string
  111. */
  112. public function toString(string $str = ''): string
  113. {
  114. $str .= "<{$this->name} xmlns='{$this->ns}' ";
  115. foreach ($this->attrs as $key => $value) {
  116. if ($key != 'xmlns') {
  117. $value = htmlspecialchars($value);
  118. $str .= "$key='$value' ";
  119. }
  120. }
  121. $str .= ">";
  122. foreach ($this->subs as $sub) {
  123. $str .= $sub->toString();
  124. }
  125. $body = htmlspecialchars($this->data);
  126. $str .= "$body</{$this->name}>";
  127. return $str;
  128. }
  129. /**
  130. * Has this XML Object the given sub?
  131. *
  132. * @param string $name
  133. * @param string|null $ns
  134. * @return bool
  135. */
  136. public function hasSub(string $name, ?string $ns = null): bool
  137. {
  138. foreach ($this->subs as $sub) {
  139. if (($name == "*" or $sub->name == $name) and ($ns == null or $sub->ns == $ns)) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * Return a sub
  147. *
  148. * @param string $name
  149. * @param array|null $attrs (optional)
  150. * @param string|null $ns (optional)
  151. * @return mixed
  152. */
  153. public function sub(string $name, ?array $attrs = null, ?string $ns = null)
  154. {
  155. #TODO attrs is ignored
  156. foreach ($this->subs as $sub) {
  157. if ($sub->name == $name and ($ns == null or $sub->ns == $ns)) {
  158. return $sub;
  159. }
  160. }
  161. return null;
  162. }
  163. }