XMLObj.php 3.4 KB

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