2019-04-21 09:23:50 +09:00
|
|
|
<?php
|
|
|
|
|
2008-08-22 22:17:14 +09:00
|
|
|
/**
|
|
|
|
* XMPPHP: The PHP XMPP Library
|
|
|
|
* Copyright (C) 2008 Nathanael C. Fritz
|
|
|
|
* This file is part of SleekXMPP.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-08-22 22:17:14 +09:00
|
|
|
* 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.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-08-22 22:17:14 +09:00
|
|
|
* 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.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-08-22 22:17:14 +09:00
|
|
|
* 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
|
|
|
|
*
|
2019-04-21 09:23:50 +09:00
|
|
|
* @category xmpphp
|
|
|
|
* @package XMPPHP
|
|
|
|
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
|
|
|
|
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
|
|
|
|
* @author Michael Garvin <JID: gar@netflint.net>
|
|
|
|
* @author Alexander Birkner (https://github.com/BirknerAlex)
|
|
|
|
* @author zorn-v (https://github.com/zorn-v/xmpphp/)
|
|
|
|
* @author GNU social
|
2008-08-22 22:17:14 +09:00
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
|
|
|
*/
|
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
namespace XMPPHP;
|
|
|
|
|
2008-08-22 22:17:14 +09:00
|
|
|
/**
|
2019-04-21 09:23:50 +09:00
|
|
|
* XMPPHP XMLObject
|
|
|
|
*
|
|
|
|
* @package XMPPHP
|
|
|
|
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
|
|
|
|
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
|
|
|
|
* @author Michael Garvin <JID: gar@netflint.net>
|
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
|
|
|
* @version $Id$
|
2008-08-22 22:17:14 +09:00
|
|
|
*/
|
2019-04-21 09:23:50 +09:00
|
|
|
class XMLObj
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Tag name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Namespace
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $ns;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public $attrs = [];
|
2019-04-21 09:23:50 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subs?
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public $subs = [];
|
2019-04-21 09:23:50 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Node data
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $data = '';
|
2008-08-22 22:17:14 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $name
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param string $ns (optional)
|
|
|
|
* @param array $attrs (optional)
|
|
|
|
* @param string $data (optional)
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function __construct(string $name, string $ns = '', array $attrs = [], string $data = '')
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
$this->name = strtolower($name);
|
|
|
|
$this->ns = $ns;
|
|
|
|
if (is_array($attrs) && count($attrs)) {
|
|
|
|
foreach ($attrs as $key => $value) {
|
|
|
|
$this->attrs[strtolower($key)] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
2008-08-22 22:17:14 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Dump this XML Object to output.
|
|
|
|
*
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param int $depth (optional)
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function printObj(int $depth = 0): void
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
print str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data;
|
|
|
|
print "\n";
|
|
|
|
foreach ($this->subs as $sub) {
|
|
|
|
$sub->printObj($depth + 1);
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 22:17:14 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Return this XML Object in xml notation
|
|
|
|
*
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param string $str (optional)
|
2019-04-21 09:23:50 +09:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function toString(string $str = ''): string
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
$str .= "<{$this->name} xmlns='{$this->ns}' ";
|
|
|
|
foreach ($this->attrs as $key => $value) {
|
|
|
|
if ($key != 'xmlns') {
|
|
|
|
$value = htmlspecialchars($value);
|
|
|
|
$str .= "$key='$value' ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$str .= ">";
|
|
|
|
foreach ($this->subs as $sub) {
|
|
|
|
$str .= $sub->toString();
|
|
|
|
}
|
|
|
|
$body = htmlspecialchars($this->data);
|
|
|
|
$str .= "$body</{$this->name}>";
|
|
|
|
return $str;
|
|
|
|
}
|
2008-08-22 22:17:14 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Has this XML Object the given sub?
|
|
|
|
*
|
|
|
|
* @param string $name
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param string|null $ns
|
|
|
|
* @return bool
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function hasSub(string $name, ?string $ns = null): bool
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
foreach ($this->subs as $sub) {
|
|
|
|
if (($name == "*" or $sub->name == $name) and ($ns == null or $sub->ns == $ns)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-22 22:17:14 +09:00
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
/**
|
|
|
|
* Return a sub
|
|
|
|
*
|
|
|
|
* @param string $name
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param array|null $attrs (optional)
|
|
|
|
* @param string|null $ns (optional)
|
2019-04-21 09:23:50 +09:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function sub(string $name, ?array $attrs = null, ?string $ns = null)
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
#TODO attrs is ignored
|
|
|
|
foreach ($this->subs as $sub) {
|
|
|
|
if ($sub->name == $name and ($ns == null or $sub->ns == $ns)) {
|
|
|
|
return $sub;
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 01:51:01 +09:00
|
|
|
return null;
|
2019-04-21 09:23:50 +09:00
|
|
|
}
|
2008-08-22 22:17:14 +09:00
|
|
|
}
|