2008-04-02 05:50:27 +01:00
|
|
|
<?php
|
2008-04-02 08:23:24 +01:00
|
|
|
/*
|
|
|
|
XMPPHP: The PHP XMPP Library
|
|
|
|
Copyright (C) 2008 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
XMPPHP is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
XMPPHP is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with XMPPHP; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2008-05-01 15:29:33 +01:00
|
|
|
class XMPPHP_XMLObj {
|
|
|
|
/**
|
|
|
|
* Tag name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public $name;
|
2008-05-01 15:29:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Namespace
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public $ns;
|
2008-05-01 15:29:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public $attrs = array();
|
2008-05-01 15:29:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subs?
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public $subs = array();
|
2008-05-01 15:29:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Node data
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public $data = '';
|
2008-04-02 05:50:27 +01:00
|
|
|
|
2008-04-28 23:05:31 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $ns
|
|
|
|
* @param array $attrs
|
|
|
|
* @param string $data
|
|
|
|
*/
|
|
|
|
public function __construct($name, $ns = '', $attrs = array(), $data = '') {
|
2008-04-02 05:50:27 +01:00
|
|
|
$this->name = strtolower($name);
|
2008-04-28 23:05:31 +01:00
|
|
|
$this->ns = $ns;
|
|
|
|
if(is_array($attrs) && count($attrs)) {
|
2008-04-02 05:50:27 +01:00
|
|
|
foreach($attrs as $key => $value) {
|
|
|
|
$this->attrs[strtolower($key)] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
2008-05-01 15:29:33 +01:00
|
|
|
/**
|
|
|
|
* Dump this XML Object to output.
|
|
|
|
*
|
|
|
|
* @param integer $depth
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public function printObj($depth = 0) {
|
2008-04-02 05:50:27 +01:00
|
|
|
print str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data;
|
|
|
|
print "\n";
|
|
|
|
foreach($this->subs as $sub) {
|
2008-04-28 23:05:31 +01:00
|
|
|
$sub->printObj($depth + 1);
|
2008-04-02 05:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-01 15:29:33 +01:00
|
|
|
/**
|
|
|
|
* Return this XML Object in xml notation
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public function toString($str = '') {
|
2008-04-04 08:19:17 +01:00
|
|
|
$str .= "<{$this->name} xmlns='{$this->ns}' ";
|
|
|
|
foreach($this->attrs as $key => $value) {
|
|
|
|
if($key != 'xmlns') {
|
2008-04-06 23:50:29 +01:00
|
|
|
$value = htmlspecialchars($value);
|
2008-04-04 08:19:17 +01:00
|
|
|
$str .= "$key='$value' ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$str .= ">";
|
|
|
|
foreach($this->subs as $sub) {
|
2008-04-28 23:05:31 +01:00
|
|
|
$str .= $sub->toString();
|
2008-04-04 08:19:17 +01:00
|
|
|
}
|
2008-04-06 23:50:29 +01:00
|
|
|
$body = htmlspecialchars($this->data);
|
2008-04-04 08:19:17 +01:00
|
|
|
$str .= "$body</{$this->name}>";
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2008-05-01 15:29:33 +01:00
|
|
|
/**
|
|
|
|
* Has this XML Object the given sub?
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public function hasSub($name) {
|
2008-04-02 05:50:27 +01:00
|
|
|
foreach($this->subs as $sub) {
|
2008-04-28 23:05:31 +01:00
|
|
|
if($sub->name == $name) return true;
|
2008-04-02 05:50:27 +01:00
|
|
|
}
|
2008-05-01 15:29:33 +01:00
|
|
|
return false;
|
2008-04-02 05:50:27 +01:00
|
|
|
}
|
|
|
|
|
2008-05-01 15:29:33 +01:00
|
|
|
/**
|
|
|
|
* Return a sub
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $attrs
|
|
|
|
* @param string $ns
|
|
|
|
*/
|
2008-04-28 23:05:31 +01:00
|
|
|
public function sub($name, $attrs = null, $ns = null) {
|
2008-04-02 05:50:27 +01:00
|
|
|
foreach($this->subs as $sub) {
|
2008-05-01 15:29:33 +01:00
|
|
|
if($sub->name == $name) {
|
|
|
|
return $sub;
|
|
|
|
}
|
2008-04-02 05:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|