Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
+15
-9
@@ -11,23 +11,29 @@
|
||||
namespace IPS\Xml;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use Exception;
|
||||
use IPS\DateTime;
|
||||
use IPS\Http\Url;
|
||||
use function defined;
|
||||
|
||||
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 Atom documents
|
||||
*/
|
||||
class _Atom extends SimpleXML
|
||||
class Atom extends SimpleXML
|
||||
{
|
||||
/**
|
||||
* Get title
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function title()
|
||||
public function title(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
@@ -35,10 +41,10 @@ class _Atom extends SimpleXML
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function articles( $guidKey=NULL )
|
||||
public function articles( mixed $guidKey=NULL ): array
|
||||
{
|
||||
$articles = array();
|
||||
|
||||
@@ -64,16 +70,16 @@ class _Atom extends SimpleXML
|
||||
{
|
||||
try
|
||||
{
|
||||
$link = \IPS\Http\Url::external( $linkToCheck );
|
||||
$link = Url::external( $linkToCheck );
|
||||
}
|
||||
catch ( \Exception $e ) { }
|
||||
catch ( Exception $e ) { }
|
||||
}
|
||||
}
|
||||
|
||||
$articles[ md5( $guidKey . ( (string) $item->id ) ) ] = array(
|
||||
'title' => ( (string) $item->title ) ?: ( mb_substr( strip_tags( $item->content ), 0, 47 ) . '...' ),
|
||||
'content' => isset( $item->content ) ? (string) $item->content : (string) $item->title,
|
||||
'date' => \IPS\DateTime::ts( strtotime( $item->updated ) ),
|
||||
'date' => DateTime::ts( strtotime( $item->updated ) ),
|
||||
'link' => $link
|
||||
);
|
||||
}
|
||||
|
||||
+29
-21
@@ -11,25 +11,29 @@
|
||||
namespace IPS\Xml;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DOMDocument as PHPDOMDocument;
|
||||
use function defined;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper class for managing DOMDocument objects
|
||||
*/
|
||||
class _DOMDocument extends \DOMDocument
|
||||
class DOMDocument extends PHPDOMDocument
|
||||
{
|
||||
/**
|
||||
* Load XML from a file
|
||||
*
|
||||
* @param string $filename The filename
|
||||
* @param int $options Bitmask of LIBXML_* constants
|
||||
* @param int|null $options Bitmask of LIBXML_* constants
|
||||
* @return bool
|
||||
*/
|
||||
public function load( $filename, $options=0 )
|
||||
public function load( string $filename, ?int $options=0 ): bool
|
||||
{
|
||||
return static::loadXML( file_get_contents( $filename ), $options );
|
||||
}
|
||||
@@ -38,11 +42,11 @@ class _DOMDocument extends \DOMDocument
|
||||
* Load HTML from a string
|
||||
*
|
||||
* @param string $source The HTML to open
|
||||
* @param int $options Bitmask of LIBXML_* constants
|
||||
* @param int|null $options Bitmask of LIBXML_* constants
|
||||
* @return bool
|
||||
* @note We are disabling the entity loader after opening the content to prevent XXE
|
||||
*/
|
||||
public function loadHTML( $source, $options=0 )
|
||||
public function loadHTML( string $source, ?int $options=0 ): bool
|
||||
{
|
||||
libxml_use_internal_errors( TRUE );
|
||||
|
||||
@@ -51,16 +55,18 @@ class _DOMDocument extends \DOMDocument
|
||||
{
|
||||
return parent::loadHTML( $source, $options );
|
||||
}
|
||||
|
||||
|
||||
/* Commented out because this is deprecated */
|
||||
/* Turn off external entity loader to prevent XXE */
|
||||
$entityLoaderValue = libxml_disable_entity_loader( TRUE );
|
||||
//$entityLoaderValue = libxml_disable_entity_loader( TRUE );
|
||||
|
||||
/* Load it */
|
||||
$opened = parent::loadHTML( $source, $options );
|
||||
|
||||
|
||||
/* 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 */
|
||||
return $opened;
|
||||
@@ -70,11 +76,11 @@ class _DOMDocument extends \DOMDocument
|
||||
* Load HTML from a file
|
||||
*
|
||||
* @param string $filename The filename
|
||||
* @param int $options Bitmask of LIBXML_* constants
|
||||
* @param int|null $options Bitmask of LIBXML_* constants
|
||||
* @return bool
|
||||
* @note We are disabling the entity loader after opening the content to prevent XXE
|
||||
*/
|
||||
public function loadHTMLFile( $filename, $options=0 )
|
||||
public function loadHTMLFile( string $filename, ?int $options=0 ): bool
|
||||
{
|
||||
return static::loadHTML( file_get_contents( $filename ), $options );
|
||||
}
|
||||
@@ -83,10 +89,10 @@ class _DOMDocument extends \DOMDocument
|
||||
* Load XML from a string
|
||||
*
|
||||
* @param string $source The HTML to open
|
||||
* @param int $options Bitmask of LIBXML_* constants
|
||||
* @param int|null $options Bitmask of LIBXML_* constants
|
||||
* @return bool
|
||||
*/
|
||||
public function loadXML( $source, $options=0 )
|
||||
public function loadXML( string $source, ?int $options=0 ): bool
|
||||
{
|
||||
libxml_use_internal_errors( TRUE );
|
||||
|
||||
@@ -95,16 +101,18 @@ class _DOMDocument extends \DOMDocument
|
||||
{
|
||||
return parent::loadXML( $source, $options );
|
||||
}
|
||||
|
||||
|
||||
/* Commented out because this is deprecated */
|
||||
/* Turn off external entity loader to prevent XXE */
|
||||
$entityLoaderValue = libxml_disable_entity_loader( TRUE );
|
||||
//$entityLoaderValue = libxml_disable_entity_loader( TRUE );
|
||||
|
||||
/* Load it */
|
||||
$opened = parent::loadXML( $source, $options );
|
||||
|
||||
|
||||
/* 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 */
|
||||
return $opened;
|
||||
@@ -113,10 +121,10 @@ class _DOMDocument extends \DOMDocument
|
||||
/**
|
||||
* Prefix HTML content with certain HTML to force DOMDocument to treat the content as UTF-8-encoded HTML
|
||||
*
|
||||
* @param string $content HTML content to prefix
|
||||
* @param string $content HTML content to prefix
|
||||
* @return string
|
||||
*/
|
||||
static public function wrapHtml( $content )
|
||||
static public function wrapHtml( string $content ): string
|
||||
{
|
||||
return "<!DOCTYPE html><html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'/></head><body>" . $content. "</body></html>";
|
||||
}
|
||||
|
||||
+49
-36
@@ -11,27 +11,39 @@
|
||||
namespace IPS\Xml;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use IPS\DateTime;
|
||||
use IPS\File;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use function count;
|
||||
use function defined;
|
||||
|
||||
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 RSS documents
|
||||
*/
|
||||
class _Rss extends SimpleXML
|
||||
class Rss extends SimpleXML
|
||||
{
|
||||
/**
|
||||
* Create RSS document
|
||||
*
|
||||
* @param \IPS\Http\Url $url URL to document
|
||||
* @param string $title Channel Title
|
||||
* @param string $description Channel Description
|
||||
* @return void
|
||||
* @param Url $url URL to document
|
||||
* @param string $title Channel Title
|
||||
* @param string $description Channel Description
|
||||
* @return static
|
||||
* @see <a href='http://cyber.law.harvard.edu/rss/languages.html'>Allowable values for language in RSS</a>
|
||||
*/
|
||||
public static function newDocument( \IPS\Http\Url $url, $title, $description )
|
||||
public static function newDocument( Url $url, string $title, string $description ) : static
|
||||
{
|
||||
$xml = new static( '<rss version="2.0" />' );
|
||||
|
||||
@@ -41,7 +53,7 @@ class _Rss extends SimpleXML
|
||||
$channel->addChild( 'description', $description );
|
||||
|
||||
/* Previously we were regexing and whitelisting language codes for some reason - we should just send the language code always */
|
||||
$locale = mb_strtolower( \IPS\Member::loggedIn()->language()->short );
|
||||
$locale = mb_strtolower( Member::loggedIn()->language()->short );
|
||||
$locale = mb_substr( $locale, 0, mb_strpos( str_replace( '-', '_', $locale ), '_' ) );
|
||||
$channel->addChild( 'language', $locale );
|
||||
|
||||
@@ -51,20 +63,20 @@ class _Rss extends SimpleXML
|
||||
/**
|
||||
* Add Item
|
||||
*
|
||||
* @param string $title Item title
|
||||
* @param \IPS\Http\Url|NULL $link Item link
|
||||
* @param string|NULL $description Item description/content
|
||||
* @param \IPS\DateTime|NULL $date Item date
|
||||
* @param string $guid Item ID
|
||||
* @param \IPS\File $enclosure Enclosure file object
|
||||
* @param string|null $title Item title
|
||||
* @param Url|NULL $link Item link
|
||||
* @param string|null $description Item description/content
|
||||
* @param DateTime|NULL $date Item date
|
||||
* @param string|null $guid Item ID
|
||||
* @param File|null $enclosure Enclosure file object
|
||||
* @return void
|
||||
* @todo [Future] The feed will validate now, but unrecognized attribute values cause warnings when validating. Also, the validator recommends using an Atom feed with the atom:link attribute.
|
||||
*/
|
||||
public function addItem( $title = NULL, \IPS\Http\Url $link = NULL, $description = NULL, \IPS\DateTime $date = NULL, $guid = NULL, $enclosure=NULL )
|
||||
public function addItem( string $title = NULL, Url $link = NULL, string $description = NULL, DateTime $date = NULL, string $guid = NULL, File $enclosure=NULL ) : void
|
||||
{
|
||||
if ( $title === NULL and $description === NULL )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
|
||||
$item = $this->channel->addChild( 'item' );
|
||||
@@ -79,7 +91,7 @@ class _Rss extends SimpleXML
|
||||
if ( $description !== NULL )
|
||||
{
|
||||
$description = preg_replace_callback( "/\s+?(srcset|src)=(['\"])\/\/([^'\"]+?)(['\"])/ims", function( $matches ){
|
||||
$baseUrl = parse_url( \IPS\Settings::i()->base_url );
|
||||
$baseUrl = parse_url( Settings::i()->base_url );
|
||||
|
||||
/* Try to preserve http vs https */
|
||||
if( isset( $baseUrl['scheme'] ) )
|
||||
@@ -97,12 +109,13 @@ class _Rss extends SimpleXML
|
||||
$item->addChild( 'description', $description );
|
||||
}
|
||||
|
||||
if ( $enclosure !== NULL and $enclosure instanceof \IPS\File and $enclosure->mediaType() == 'image' )
|
||||
if ( $enclosure !== NULL and $enclosure->mediaType() == 'image' )
|
||||
{
|
||||
$enclosureUrl = Url::createFromString( $enclosure->url );
|
||||
$child = $item->addChild( 'enclosure' );
|
||||
$child->addAttribute( 'url', (string) $enclosure->url->setScheme( ( \IPS\Request::i()->isSecure() ) ? 'https' : 'http' ) );
|
||||
$child->addAttribute( 'url', (string) $enclosureUrl->setScheme( ( Request::i()->isSecure() ) ? 'https' : 'http' ) );
|
||||
$child->addAttribute( 'length', (string) $enclosure->filesize() );
|
||||
$child->addAttribute( 'type', \IPS\File::getMimeType( (string) $enclosure->url ) );
|
||||
$child->addAttribute( 'type', File::getMimeType( (string) $enclosure->url ) );
|
||||
}
|
||||
|
||||
if ( $guid !== NULL )
|
||||
@@ -122,7 +135,7 @@ class _Rss extends SimpleXML
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function title()
|
||||
public function title(): string
|
||||
{
|
||||
return $this->channel->title;
|
||||
}
|
||||
@@ -130,10 +143,10 @@ class _Rss extends SimpleXML
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function articles( $guidKey=NULL )
|
||||
public function articles( mixed $guidKey=NULL ): array
|
||||
{
|
||||
$articles = array();
|
||||
$items = $this->getItems();
|
||||
@@ -144,9 +157,9 @@ class _Rss extends SimpleXML
|
||||
{
|
||||
try
|
||||
{
|
||||
$link = \IPS\Http\Url::external( (string) $item->link );
|
||||
$link = Url::external( (string) $item->link );
|
||||
}
|
||||
catch ( \Exception $e ) { }
|
||||
catch ( Exception $e ) { }
|
||||
}
|
||||
|
||||
if ( isset( $item->guid ) )
|
||||
@@ -170,7 +183,7 @@ class _Rss extends SimpleXML
|
||||
$text = isset( $item->description ) ? (string) $item->description : (string) $item->title;
|
||||
|
||||
/* If there is a <content:encoded> tag, get the contents of that instead of description */
|
||||
if( \count( $item->children( 'content', true ) ) AND \count( $item->children( 'content', true )->encoded ) )
|
||||
if( count( $item->children( 'content', true ) ) AND count( $item->children( 'content', true )->encoded ) )
|
||||
{
|
||||
$text = (string) $item->children( 'content', true )->encoded[0];
|
||||
}
|
||||
@@ -180,13 +193,13 @@ class _Rss extends SimpleXML
|
||||
|
||||
if ( $pubDate === NULL AND $this->channel->pubDate )
|
||||
{
|
||||
$pubDate = \IPS\DateTime::ts( strtotime( $this->channel->pubDate ), TRUE );
|
||||
$pubDate = DateTime::ts( strtotime( $this->channel->pubDate ), TRUE );
|
||||
}
|
||||
|
||||
$articles[ $guid ] = array(
|
||||
'title' => ( (string) $item->title ) ?: ( mb_substr( $text, 0, 47 ) . '...' ),
|
||||
'content' => (string) $text,
|
||||
'date' => $pubDate ?: \IPS\DateTime::create(),
|
||||
'content' => $text,
|
||||
'date' => $pubDate ?: DateTime::create(),
|
||||
'link' => $link
|
||||
);
|
||||
|
||||
@@ -207,15 +220,15 @@ class _Rss extends SimpleXML
|
||||
/**
|
||||
* Fetch the date
|
||||
*
|
||||
* @param object $item RSS item
|
||||
* @return NULL|\IPS\DateTime
|
||||
* @param object $item RSS item
|
||||
* @return NULL|DateTime
|
||||
*/
|
||||
protected function getDate( $item )
|
||||
protected function getDate( object $item ): ?DateTime
|
||||
{
|
||||
$pubDate = NULL;
|
||||
if ( $item->pubDate )
|
||||
{
|
||||
$pubDate = \IPS\DateTime::ts( strtotime( $item->pubDate ), TRUE );
|
||||
$pubDate = DateTime::ts( strtotime( $item->pubDate ), TRUE );
|
||||
}
|
||||
|
||||
return $pubDate;
|
||||
@@ -224,9 +237,9 @@ class _Rss extends SimpleXML
|
||||
/**
|
||||
* Fetch the items
|
||||
*
|
||||
* @return array
|
||||
* @return self
|
||||
*/
|
||||
protected function getItems()
|
||||
protected function getItems(): self
|
||||
{
|
||||
return $this->channel->item;
|
||||
}
|
||||
|
||||
+15
-10
@@ -11,33 +11,38 @@
|
||||
namespace IPS\Xml;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\DateTime;
|
||||
use function defined;
|
||||
use function in_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 reading an RSS 1.0 document
|
||||
*/
|
||||
class _Rss1 extends Rss
|
||||
class Rss1 extends Rss
|
||||
{
|
||||
/**
|
||||
* Fetch the date
|
||||
*
|
||||
* @param object $item RSS item
|
||||
* @return NULL|\IPS\DateTime
|
||||
* @param object $item RSS item
|
||||
* @return NULL|DateTime
|
||||
*/
|
||||
protected function getDate( $item )
|
||||
protected function getDate( object $item ): ?DateTime
|
||||
{
|
||||
$pubDate = NULL;
|
||||
|
||||
/* If we use the Dublin Core (dc) namespace, we will probably have dc:date */
|
||||
$namespaces = $this->getNamespaces( TRUE );
|
||||
|
||||
if( \in_array( 'http://purl.org/dc/elements/1.1/', $namespaces ) AND $item->children( $namespaces['dc'] )->date )
|
||||
if( in_array( 'https://purl.org/dc/elements/1.1/', $namespaces ) AND $item->children( $namespaces['dc'] )->date )
|
||||
{
|
||||
$pubDate = \IPS\DateTime::ts( strtotime( $item->children( $namespaces['dc'] )->date ) );
|
||||
$pubDate = DateTime::ts( strtotime( $item->children( $namespaces['dc'] )->date ) );
|
||||
}
|
||||
|
||||
return $pubDate ?: parent::getDate( $item );
|
||||
@@ -46,9 +51,9 @@ class _Rss1 extends Rss
|
||||
/**
|
||||
* Fetch the items
|
||||
*
|
||||
* @return array
|
||||
* @return Rss
|
||||
*/
|
||||
protected function getItems()
|
||||
protected function getItems(): Rss
|
||||
{
|
||||
return $this->item;
|
||||
}
|
||||
|
||||
+67
-50
@@ -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() );
|
||||
|
||||
+13
-18
@@ -11,42 +11,37 @@
|
||||
namespace IPS\Xml;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use XMLReader as PHPXMLReader;
|
||||
use function defined;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper class for managing XMLReader objects
|
||||
*/
|
||||
class _XMLReader extends \XMLReader
|
||||
class XMLReader extends PHPXMLReader
|
||||
{
|
||||
/**
|
||||
* Open a file or URL with XMLReader to read it
|
||||
*
|
||||
* @param string $uri The URI/path to open
|
||||
* @param string $encoding The encoding to use, or NULL
|
||||
* @param int $options Bitmask of LIBXML_* constants
|
||||
* @return bool
|
||||
* @param string $uri The URI/path to open
|
||||
* @param string|null $encoding The encoding to use, or NULL
|
||||
* @param int $options Bitmask of LIBXML_* constants
|
||||
* @return PHPXMLReader|bool
|
||||
* @note We are disabling network access while loading the content to prevent XXE
|
||||
*/
|
||||
public static function safeOpen( $uri, $encoding=NULL, $options=0 )
|
||||
public static function safeOpen( string $uri, string $encoding=NULL, int $options=0 ): PHPXMLReader|bool
|
||||
{
|
||||
if( $options === 0 )
|
||||
{
|
||||
$options = LIBXML_NONET;
|
||||
}
|
||||
|
||||
if( version_compare( PHP_VERSION, '8.0.0' ) >= 0 )
|
||||
{
|
||||
return parent::open( $uri, $encoding, $options );
|
||||
}
|
||||
else
|
||||
{
|
||||
$xml = new static;
|
||||
$xml->open( $uri, $encoding, $options );
|
||||
return $xml;
|
||||
}
|
||||
return parent::open( $uri, $encoding, $options );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user