Version 4.1.19

This commit is contained in:
Neo committed 2025-12-19 05:38:56 -08:00
1 parent 28bd025b35
commit 2bd5025018
2674 files changed
+233518 -77766

No files matched your search

+66 -117
View File
@@ -1,135 +1,84 @@
//<?php
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
exit;
}
class cms_hook_Url extends _HOOK_CLASS_
{
protected static $gatewayDir = NULL;
/**
* Get friendly URL data
* Create from string
*
* @param bool $verify If TRUE, will check URL uses correct SEO title
* @return array Parameters
* @throws \OutOfRangeException Invalid URL
* @throws \DomainException URL does not have correct SEO title (exception message will contain correct URL)
* This is overridden so that when we are examing a raw URL the "Gateway File" feature
* can appropriately claim the URL as belonging to it
*
* @param string $url A valid URL as per our definition (see phpDoc on class)
* @param bool $couldBeFriendly If the URL is known to not be friendly, FALSE can be passed here to save the performance implication of checking the URL
* @param bool $autoEncode If true, any invalid components will be automatically encoded rather than an exception thrown - useful if the entire link is user-provided
* @return \IPS\Http\Url
*/
public function getFriendlyUrlData( $verify=false )
public static function createFromString( $url, $couldBeFriendly=TRUE, $autoEncode=FALSE )
{
try
{
return call_user_func_array( 'parent::getFriendlyUrlData', func_get_args() );
}
catch( \OutOfRangeException $ex )
{
/* Pass to IP.Content to handle */
$baseUrl = parse_url( \IPS\Settings::i()->base_url );
if ( \IPS\Settings::i()->htaccess_mod_rewrite )
{
$query = ( isset( $this->data['path'] ) ? $this->data['path'] : '' );
}
else
{
if ( isset( $this->data['path'] ) and mb_strpos( $this->data['path'], 'index.php?' ) )
{
$query = ( isset( $this->data['query'] ) ? ltrim( $this->data['query'], '/' ) : '' );
}
else
{
$query = '';
}
}
/* Because pages can have "nothing" in the URL for SEO reasons, we need to capture query strings first before sending to the parent class */
$queryString = '';
if( mb_strpos( $query, '&' ) !== FALSE )
{
$queryString = ltrim( mb_substr( $query, mb_strpos( $query, '&' ) ), '&' );
$query = mb_substr( $query, 0, mb_strpos( $query, '&' ) );
}
$set = array();
/* See what normal handling makes of it... */
$return = parent::createFromString( $url, $couldBeFriendly, $autoEncode );
/* Need to remember the template we use for FURL verification */
$usedTemplate = NULL;
/* Get furl definition */
$furlDefinition = array('cms_page' => array(
'friendly' => '{@path}',
'real' => 'app=cms&module=pages&controller=page'
) );
$query = preg_replace( '#^(' . preg_quote( rtrim( $baseUrl['path'], '/' ), '#' ) . ')/(index.php)?(?:(?:\?/|\?))?(.+?)?$#', '$3', $query );
$this->examineFurl( $furlDefinition, $query, $set, $usedTemplate );
/* Now set the query string back */
if( $queryString )
/* If the normal handling doesn't recognise it as an internal URL and we
have a gateway file, check that */
if ( !( $return instanceof \IPS\Http\Url\Internal ) and \IPS\Settings::i()->cms_root_page_url )
{
/* Decode it */
$components = static::componentsFromUrlString( $url, $autoEncode );
/* Is it underneath the gateway? */
$gatewayUrlComponents = static::componentsFromUrlString( \IPS\Settings::i()->cms_root_page_url );
if ( $components[ static::COMPONENT_HOST ] === $gatewayUrlComponents[ static::COMPONENT_HOST ] and
$components[ static::COMPONENT_USERNAME ] === $gatewayUrlComponents[ static::COMPONENT_USERNAME ] and
$components[ static::COMPONENT_PASSWORD ] === $gatewayUrlComponents[ static::COMPONENT_PASSWORD ] and
$components[ static::COMPONENT_PORT ] === $gatewayUrlComponents[ static::COMPONENT_PORT ] and
mb_substr( $components[ static::COMPONENT_PATH ], 0, mb_strlen( $gatewayUrlComponents[ static::COMPONENT_PATH ] ) ) === $gatewayUrlComponents[ static::COMPONENT_PATH ]
)
{
$queryString = explode( '&', $queryString );
foreach( $queryString as $k => $v )
$pathFromGatewayUrl = mb_substr( $components[ static::COMPONENT_PATH ], mb_strlen( $gatewayUrlComponents[ static::COMPONENT_PATH ] ) );
$fallback = FALSE;
if ( !$pathFromGatewayUrl or $pathFromGatewayUrl === 'index.php' )
{
$set[ $k ] = $v;
}
}
if ( count( $set ) )
{
if ( ! \IPS\Settings::i()->htaccess_mod_rewrite and isset( $set['path'] ) )
{
/* Is the path URL encoded?
@note this only affects when not using mod_rewrite. We specifically do not urldecode the query string in \IPS\Request::i()->url()
for other reasons, presumaly, hence this code here as it only affects Pages */
if ( str_replace( '%2F', '/', urlencode( urldecode( $set['path'] ) ) ) === $set['path'] )
if ( !$pathFromGatewayUrl )
{
$set['path'] = urldecode( $set['path'] );
}
$fallback = TRUE;
}
$queryString = \IPS\Http\Url::convertQueryAsArrayToString( $components[ static::COMPONENT_QUERY ] );
$pathFromGatewayUrl = trim( mb_substr( $queryString, 0, mb_strpos( $queryString, '&' ) ?: NULL ), '/' );
}
/* Try to find a page */
$page = NULL;
try
{
$page = \IPS\cms\Pages\Page::loadFromPath( $pathFromGatewayUrl );
return \IPS\Http\Url\Friendly::createFromComponents( $components[ static::COMPONENT_HOST ], $components[ static::COMPONENT_SCHEME ], $components[ static::COMPONENT_PATH ], $components[ static::COMPONENT_QUERY ], $components[ static::COMPONENT_PORT ], $components[ static::COMPONENT_USERNAME ], $components[ static::COMPONENT_PASSWORD ], $components[ static::COMPONENT_FRAGMENT ] )
->setFriendlyUrlData( 'content_page_path', array( $pathFromGatewayUrl ), array( 'path' => $pathFromGatewayUrl ), $pathFromGatewayUrl );
}
/* Couldn't find one? Don't accept responsibility, unless there was no $pathFromGatewayUrl and this is the gateway URL */
catch ( \OutOfRangeException $e )
{
if ( $fallback and (string) $return->stripQueryString() === \IPS\Settings::i()->cms_root_page_url )
{
try
{
$page = \IPS\cms\Pages\Page::loadFromPath( '' );
return \IPS\Http\Url\Friendly::createFromComponents( $components[ static::COMPONENT_HOST ], $components[ static::COMPONENT_SCHEME ], $components[ static::COMPONENT_PATH ], $components[ static::COMPONENT_QUERY ], $components[ static::COMPONENT_PORT ], $components[ static::COMPONENT_USERNAME ], $components[ static::COMPONENT_PASSWORD ], $components[ static::COMPONENT_FRAGMENT ] )
->setFriendlyUrlData( 'content_page_path', array( '' ), array( 'path' => '' ), '' );
}
catch ( \OutOfRangeException $e ) { }
}
}
return $set;
}
else
{
throw new \OutOfRangeException();
}
}
}
/**
* Make friendly
*
* @param string|array $seoTemplate The key for making this a friendly URL; or a manual FURL definition
* @param string|array $seoTitles The title(s) needed for the friendly URL
* @param bool $forceHttps Force HTTPS
* @return parent
*/
public function makeFriendly( $seoTemplate, $seoTitles, $forceHttps=FALSE )
{
$called = $seoTemplate;
if ( $seoTemplate === 'content_page_path' )
{
$seoTemplate = array(
'friendly' => '{@path}',
'real' => 'app=cms&module=pages&controller=page'
);
}
parent::makeFriendly( $seoTemplate, $seoTitles, $forceHttps );
if ( $called === 'content_page_path' and \IPS\Settings::i()->cms_use_different_gateway )
{
if ( static::$gatewayDir === NULL )
{
static::$gatewayDir = preg_replace( '#^/?(.+?)/?$#', '\1', str_replace( \IPS\Settings::i()->cms_root_page_url, '', \IPS\Settings::i()->base_url ) );
}
/* If $gatewayDir is empty, we do not want to do this as it will remove all slashes from the entire path */
if ( !empty( static::$gatewayDir ) )
{
$this->data['path'] = str_replace( '/' . static::$gatewayDir, '', $this->data['path'] );
}
$this->url = str_replace( static::baseUrl(), \IPS\Settings::i()->cms_root_page_url, $this->url );
}
/* Return */
return $return;
}
}