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

+49 -36
View File
@@ -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;
}