Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+67 -50
View File
@@ -11,25 +11,38 @@
namespace IPS\Xml;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use BadMethodCallException;
use DOMDocument;
use InvalidArgumentException;
use SimpleXMLElement;
use function count;
use function defined;
use function get_called_class;
use function gettype;
use function in_array;
use function intval;
use function is_array;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Class for managing small XML files
*/
class _SimpleXML extends \SimpleXMLElement
class SimpleXML extends SimpleXMLElement
{
/**
* Create New XML Document
*
* @param string $rootElement Name of Root Element
* @param string|NULL $xmlns Namespace
* @param string $rootElement Name of Root Element
* @param string|null $xmlns Namespace
* @return static
*/
public static function create( $rootElement, $xmlns=NULL )
public static function create( string $rootElement, string $xmlns=NULL ): static
{
if ( $xmlns )
{
@@ -44,11 +57,11 @@ class _SimpleXML extends \SimpleXMLElement
/**
* Load File
*
* @param string $filename Filename of XML file
* @param string $filename Filename of XML file
* @return static
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public static function loadFile( $filename )
public static function loadFile( string $filename ): static
{
return static::loadString( file_get_contents( $filename ) );
}
@@ -56,33 +69,35 @@ class _SimpleXML extends \SimpleXMLElement
/**
* Load String
*
* @param string $xml XML
* @param string $xml XML
* @return static
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public static function loadString( $xml )
public static function loadString( string $xml ): static
{
if ( version_compare( PHP_VERSION, '8.0.0' ) >= 0 )
{
$class = @simplexml_load_string( $xml, \get_called_class() );
$class = @simplexml_load_string( $xml, get_called_class() );
}
else
{
/* Commented out because this is deprecated */
/* Turn off external entity loader to prevent XXE in PHP less than 8.0 */
$entityLoaderValue = libxml_disable_entity_loader( TRUE );
//$entityLoaderValue = libxml_disable_entity_loader( TRUE );
/* Load it */
$class = @simplexml_load_string( $xml, \get_called_class() );
$class = @simplexml_load_string( $xml, get_called_class() );
/* Commented out because this is deprecated */
/* Turn external entity loader back to what it was before so we're not messing with other
PHP scripts on this server */
libxml_disable_entity_loader( $entityLoaderValue );
//libxml_disable_entity_loader( $entityLoaderValue );
}
/* Return */
if ( $class === FALSE )
{
throw new \InvalidArgumentException;
throw new InvalidArgumentException;
}
return $class->subtype();
}
@@ -92,24 +107,24 @@ class _SimpleXML extends \SimpleXMLElement
*
* @return static
*/
protected function subtype()
protected function subtype(): static
{
if ( \get_called_class() === 'IPS\Xml\SimpleXML' )
if ( get_called_class() === 'IPS\Xml\SimpleXML' )
{
if ( $this->getName() === 'rss' )
{
return \IPS\Xml\Rss::loadString( $this->asXml() );
return Rss::loadString( $this->asXml() );
}
if ( $this->getName() === 'feed' )
{
return \IPS\Xml\Atom::loadString( $this->asXml() );
return Atom::loadString( $this->asXml() );
}
if ( mb_strtolower( $this->getName() ) === 'rdf' )
{
/* Verify this is an RSS 1.0 document */
if( \in_array( 'http://purl.org/rss/1.0/', $this->getNamespaces( true ) ) )
if( in_array( 'https://purl.org/rss/1.0/', $this->getNamespaces( true ) ) )
{
return \IPS\Xml\Rss1::loadString( $this->asXml() );
return Rss1::loadString( $this->asXml() );
}
}
}
@@ -120,57 +135,57 @@ class _SimpleXML extends \SimpleXMLElement
/**
* Get articles
*
* @param mixed $guidKey In previous versions, we encoded a key with the GUID. For legacy purposes, this can be passed here.
* @param mixed|null $guidKey In previous versions, we encoded a key with the GUID. For legacy purposes, this can be passed here.
* @return array
* @note Subtypes (ATOM and RSS) define this method
* @throws \BadMethodCallException
* @throws BadMethodCallException
*/
public function articles( $guidKey=NULL )
public function articles( mixed $guidKey=NULL ): array
{
throw new \BadMethodCallException;
throw new BadMethodCallException;
}
/**
* Add Child
* Modified to support passing other data types (including multi-dimensional arrays and other \IPS\XML\SimpleXML objects) as values
*
* @see <a href='http://www.php.net/manual/en/simplexmlelement.addchild.php'>SimpleXMLElement::addChild</a>
* @param string $name Element Name
* @param string $qualifiedName Element Name
* @param mixed $value Value
* @param string|null $ns Namespace
* @return void
* @param string|null $namespace Namespace
* @return SimpleXMLElement|null
*@see <a href='http://www.php.net/manual/en/simplexmlelement.addchild.php'>SimpleXMLElement::addChild</a>
*/
public function addChild( $name, $value=NULL, $ns=NULL )
public function addChild( string $qualifiedName, mixed $value=null, ?string $namespace=null ): null|static
{
/* Arrays are possibly numerically indexed, which XML won't have */
if( \gettype( $name ) === 'integer' )
if( gettype( $qualifiedName ) === 'integer' )
{
$name = $this->getName();
$qualifiedName = $this->getName();
/* If the name ends in s (e.g. <elements>) - we'll name it's children without the s (e.g. <element>) */
if( mb_substr( $name, -2 ) === 'es' )
if( mb_substr( $qualifiedName, -2 ) === 'es' )
{
$name = mb_substr( $name, 0, mb_strlen( $name ) - 2 );
$qualifiedName = mb_substr( $qualifiedName, 0, mb_strlen( $qualifiedName ) - 2 );
}
else if( mb_substr( $name, -1 ) === 's' )
else if( mb_substr( $qualifiedName, -1 ) === 's' )
{
$name = mb_substr( $name, 0, mb_strlen( $name ) - 1 );
$qualifiedName = mb_substr( $qualifiedName, 0, mb_strlen( $qualifiedName ) - 1 );
}
}
/* If it's not an array, we can just let the default SimpleXML method handle this */
if( !\is_array( $value ) and !( $value instanceof \IPS\Xml\SimpleXML ) )
if( !is_array( $value ) and !( $value instanceof SimpleXML) )
{
/* Unless it's a boolean value, then we should cast that to an integer */
if( \gettype( $value ) === 'boolean' )
if( gettype( $value ) === 'boolean' )
{
$value = \intval( $value );
$value = intval( $value );
}
/* Needs CDATA? */
if ( preg_match( '/<|>|&/', $value ) )
if ( preg_match( '/[<>&]/', $value ) )
{
$element = parent::addChild( $name, '', $ns );
$element = parent::addChild( $qualifiedName, '', $namespace );
$node = dom_import_simplexml( $element );
$no = $node->ownerDocument;
@@ -180,7 +195,7 @@ class _SimpleXML extends \SimpleXMLElement
}
else
{
return parent::addChild( $name, $value, $ns );
return parent::addChild( $qualifiedName, $value, $namespace );
}
/* Return */
@@ -189,21 +204,23 @@ class _SimpleXML extends \SimpleXMLElement
else
{
/* Create an element with a blank value */
$element = parent::addChild( $name, '', $ns );
$element = parent::addChild( $qualifiedName, '', $namespace );
/* And loop through each value and rerun this method for each */
foreach ( $value as $k => $v )
{
/* If the value is an XML text element, just get the value */
if ( $v instanceof \IPS\Xml\SimpleXML and \count( $v->children() ) === 0 )
if ( $v instanceof SimpleXML and count( $v->children() ) === 0 )
{
$v = (string) $v;
}
/* And add it */
$element->addChild( $k, $v, $ns );
$element->addChild( $k, $v, $namespace );
}
}
return null;
}
/**
@@ -221,9 +238,9 @@ class _SimpleXML extends \SimpleXMLElement
*
* @return string
*/
public function format()
public function format(): string
{
$dom = new \DOMDocument( '1.0', 'UTF-8' );
$dom = new DOMDocument( '1.0', 'UTF-8' );
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
@$dom->loadXML( $this->asXML() );