Merge pull request #378 from kijin/pr/url-conversion

경로↔URL 변환 및 정리 함수 추가
This commit is contained in:
Kijin Sung 2016-03-16 16:09:38 +09:00
commit c23a1949cc
14 changed files with 284 additions and 88 deletions

View file

@ -46,4 +46,43 @@ class FilenameFilter
return $filename; return $filename;
} }
/**
* Clean a path to remove ./, ../, trailing slashes, etc.
*
* @param string $path
* @return string
*/
public static function cleanPath($path)
{
// Convert relative paths to absolute paths.
if (!preg_match('@^(?:/|[a-z]:[\\\\/]|\\\\|https?:)@i', $path))
{
$path = \RX_BASEDIR . $path;
}
// Convert backslashes to forward slashes.
$path = str_replace('\\', '/', $path);
// Remove querystrings and URL fragments.
if (($querystring = strpbrk($path, '?#')) !== false)
{
$path = substr($path, 0, -1 * strlen($querystring));
}
// Remove single dots, three or more dots, and duplicate slashes.
$path = preg_replace(array(
'@(?<!^|^http:|^https:)/{2,}@',
'@/(?:(?:\.|\.{3,})/)+@',
), '/', $path);
// Remove double dots and the preceding directory.
while (preg_match('@/(?!\.\.)[^/]+/\.\.(?:/|$)@', $path, $matches))
{
$path = str_replace($matches[0], '/', $path);
}
// Trim trailing slashes.
return rtrim($path, '/');
}
} }

View file

@ -0,0 +1,11 @@
<?php
namespace Rhymix\Framework;
/**
* The storage class.
*/
class Storage
{
}

View file

@ -18,10 +18,7 @@ class URL
*/ */
public static function getCurrentURL(array $changes = array()) public static function getCurrentURL(array $changes = array())
{ {
$proto = \RX_SSL ? 'https://' : 'http://'; $url = self::getCurrentDomainURL(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/');
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
$local = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
$url = $proto . $host . $local;
if (count($changes)) if (count($changes))
{ {
return self::modifyURL($url, $changes); return self::modifyURL($url, $changes);
@ -32,6 +29,19 @@ class URL
} }
} }
/**
* Get the current domain.
*
* @param string $path
* @return string
*/
public static function getCurrentDomainURL($path = '/')
{
$proto = \RX_SSL ? 'https://' : 'http://';
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
return $proto . $host . '/' . ltrim($path, '/');
}
/** /**
* Convert a URL to its canonical format. * Convert a URL to its canonical format.
* *
@ -42,9 +52,7 @@ class URL
{ {
if (preg_match('#^\.?/([^/]|$)#', $url) || !preg_match('#^(https?:|/)#', $url)) if (preg_match('#^\.?/([^/]|$)#', $url) || !preg_match('#^(https?:|/)#', $url))
{ {
$proto = \RX_SSL ? 'https://' : 'http://'; $url = self::getCurrentDomainURL(\RX_BASEURL . ltrim($url, './'));
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
$url = $proto . $host . \RX_BASEURL . ltrim($url, './');
} }
return preg_replace_callback('#^(https?:|)//([^/]+)#i', function($matches) { return preg_replace_callback('#^(https?:|)//([^/]+)#i', function($matches) {
if ($matches[1] === '') $matches[1] = \RX_SSL ? 'https:' : 'http:'; if ($matches[1] === '') $matches[1] = \RX_SSL ? 'https:' : 'http:';
@ -125,6 +133,53 @@ class URL
} }
} }
/**
* Convert a server-side path to a URL.
*
* This method returns false if the path cannot be converted to a URL,
* e.g. if the path is outside of the document root.
*
* @param string $path
* @return string|false
*/
public static function fromServerPath($path)
{
$cleanpath = Filters\FilenameFilter::cleanPath($path);
if (substr($path, -1) === '/')
{
$cleanpath .= '/';
}
$root = Filters\FilenameFilter::cleanPath($_SERVER['DOCUMENT_ROOT']);
if ($cleanpath === $root)
{
return self::getCurrentDomainURL('/');
}
if (starts_with($root . '/', $cleanpath))
{
return self::getCurrentDomainURL(substr($cleanpath, strlen($root)));
}
return false;
}
/**
* Convert a URL to a server-side path.
*
* This method returns false if the URL cannot be converted to a server-side path,
* e.g. if the URL belongs to an external domain.
*
* @param string $url
* @return string
*/
public static function toServerPath($url)
{
$url = self::getCanonicalURL($url);
if (!self::isInternalURL($url))
{
return false;
}
return Filters\FilenameFilter::cleanPath($_SERVER['DOCUMENT_ROOT'] . parse_url($url, \PHP_URL_PATH));
}
/** /**
* Encode UTF-8 domain into IDNA (punycode) * Encode UTF-8 domain into IDNA (punycode)
* *

View file

@ -133,6 +133,19 @@ function class_basename($class)
return basename(str_replace('\\', '/', is_object($class) ? get_class($class) : $class)); return basename(str_replace('\\', '/', is_object($class) ? get_class($class) : $class));
} }
/**
* Clean a path to remove ./, ../, trailing slashes, etc.
*
* This function is an alias to Rhymix\Framework\Filters\FilenameFilter::cleanPath().
*
* @param string $path
* @return string
*/
function clean_path($path)
{
return Rhymix\Framework\Filters\FilenameFilter::cleanPath($path);
}
/** /**
* This function is a shortcut to htmlspecialchars(). * This function is a shortcut to htmlspecialchars().
* *
@ -337,6 +350,34 @@ function base64_decode_urlsafe($str)
return @base64_decode(str_pad(strtr($str, '-_', '+/'), ceil(strlen($str) / 4) * 4, '=', STR_PAD_RIGHT)); return @base64_decode(str_pad(strtr($str, '-_', '+/'), ceil(strlen($str) / 4) * 4, '=', STR_PAD_RIGHT));
} }
/**
* Convert a server-side path to a URL.
*
* This function is an alias to Rhymix\Framework\URL::fromServerPath().
* It returns false if the path cannot be converted.
*
* @param string $path
* @return string|false
*/
function path2url($path)
{
return Rhymix\Framework\URL::fromServerPath($path);
}
/**
* Convert a URL to a server-side path.
*
* This function is an alias to Rhymix\Framework\URL::toServerPath().
* It returns false if the URL cannot be converted.
*
* @param string $url
* @return string|false
*/
function url2path($url)
{
return Rhymix\Framework\URL::toServerPath($url);
}
/** /**
* Convert hexadecimal color codes to an array of R, G, B values. * Convert hexadecimal color codes to an array of R, G, B values.
* This function can handle both 6-digit and 3-digit notations, optionally prefixed with '#'. * This function can handle both 6-digit and 3-digit notations, optionally prefixed with '#'.

View file

@ -1,6 +1,17 @@
<?php <?php
// This is global bootstrap for autoloading
require dirname(__DIR__) . '/common/autoload.php'; // Set some superglobal variables for unit tests.
$_SERVER['HTTPS'] = 'on';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$_SERVER['SERVER_NAME'] = 'www.rhymix.org';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['DOCUMENT_ROOT'] = dirname(dirname(__DIR__));
$_SERVER['SCRIPT_FILENAME'] = dirname(__DIR__) . '/index.php';
$_SERVER['SCRIPT_NAME'] = '/rhymix/index.php';
$_SERVER['REQUEST_URI'] = '/rhymix/index.php';
// Include the autoloader.
require_once dirname(__DIR__) . '/common/autoload.php';
function _debug() { function _debug() {
$args = func_get_args(); $args = func_get_args();

View file

@ -1,3 +1,2 @@
<?php <?php
// Here you can initialize variables that will be available to your tests // Here you can initialize variables that will be available to your tests
$_SERVER['SCRIPT_NAME'] = '/xe/index.php';

View file

@ -1,7 +1,4 @@
<?php <?php
require_once _XE_PATH_.'classes/context/Context.class.php';
require_once _XE_PATH_.'classes/handler/Handler.class.php';
require_once _XE_PATH_.'classes/frontendfile/FrontEndFileHandler.class.php';
class ContextTest extends \Codeception\TestCase\Test class ContextTest extends \Codeception\TestCase\Test
{ {

View file

@ -1,5 +1,4 @@
<?php <?php
require_once _XE_PATH_.'classes/file/FileHandler.class.php';
class FileHandlerTest extends \Codeception\TestCase\Test class FileHandlerTest extends \Codeception\TestCase\Test
{ {

View file

@ -1,5 +1,4 @@
<?php <?php
require_once _XE_PATH_.'classes/frontendfile/FrontEndFileHandler.class.php';
class FrontEndFileHandlerTest extends \Codeception\TestCase\Test class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
{ {
@ -23,8 +22,8 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/common.js', 'body')); $handler->loadFile(array('./common/js/common.js', 'body'));
$handler->loadFile(array('./common/js/common.js', 'head')); $handler->loadFile(array('./common/js/common.js', 'head'));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'body')); $handler->loadFile(array('./common/js/xml_js_filter.js', 'body'));
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected); $this->assertEquals($handler->getJsFileList(), $expected);
}); });
@ -39,8 +38,8 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler = new FrontEndFileHandler(); $handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/css/xe.css')); $handler->loadFile(array('./common/css/xe.css'));
$handler->loadFile(array('./common/css/mobile.css')); $handler->loadFile(array('./common/css/mobile.css'));
$expected[] = array('file' => '/xe/common/css/xe.css' . $this->_filemtime('common/css/xe.css'), 'media' => 'all', 'targetie' => null); $expected[] = array('file' => '/rhymix/common/css/xe.css' . $this->_filemtime('common/css/xe.css'), 'media' => 'all', 'targetie' => null);
$expected[] = array('file' => '/xe/common/css/mobile.css' . $this->_filemtime('common/css/mobile.css'), 'media' => 'all', 'targetie' => null); $expected[] = array('file' => '/rhymix/common/css/mobile.css' . $this->_filemtime('common/css/mobile.css'), 'media' => 'all', 'targetie' => null);
$this->assertEquals($handler->getCssFileList(), $expected); $this->assertEquals($handler->getCssFileList(), $expected);
}); });
@ -54,10 +53,10 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/common.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected); $this->assertEquals($handler->getJsFileList(), $expected);
}); });
@ -67,10 +66,10 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/common.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected); $this->assertEquals($handler->getJsFileList(), $expected);
}); });
@ -81,9 +80,9 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
$handler->unloadFile('./common/js/js_app.js', '', 'all'); $handler->unloadFile('./common/js/js_app.js', '', 'all');
$expected[] = array('file' => '/xe/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null); $expected[] = array('file' => '/rhymix/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected); $this->assertEquals($handler->getJsFileList(), $expected);
}); });
@ -92,9 +91,9 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/js_app.js', 'head', 'ie6')); $handler->loadFile(array('./common/js/js_app.js', 'head', 'ie6'));
$handler->loadFile(array('./common/js/js_app.js', 'head', 'ie7')); $handler->loadFile(array('./common/js/js_app.js', 'head', 'ie7'));
$handler->loadFile(array('./common/js/js_app.js', 'head', 'ie8')); $handler->loadFile(array('./common/js/js_app.js', 'head', 'ie8'));
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie6'); $expected[] = array('file' => '/rhymix/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie6');
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie7'); $expected[] = array('file' => '/rhymix/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie7');
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie8'); $expected[] = array('file' => '/rhymix/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie8');
$this->assertEquals($handler->getJsFileList(), $expected); $this->assertEquals($handler->getJsFileList(), $expected);
}); });
@ -127,9 +126,9 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/css/common.css', null, 'ie7')); $handler->loadFile(array('./common/css/common.css', null, 'ie7'));
$handler->loadFile(array('./common/css/common.css', null, 'ie8')); $handler->loadFile(array('./common/css/common.css', null, 'ie8'));
$expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => 'ie6'); $expected[] = array('file' => '/rhymix/common/css/common.css', 'media'=>'all', 'targetie' => 'ie6');
$expected[] = array('file' => '/xe/common/css/common.css','media'=>'all', 'targetie' => 'ie7'); $expected[] = array('file' => '/rhymix/common/css/common.css','media'=>'all', 'targetie' => 'ie7');
$expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => 'ie8'); $expected[] = array('file' => '/rhymix/common/css/common.css', 'media'=>'all', 'targetie' => 'ie8');
$this->assertEquals($handler->getCssFileList(), $expected); $this->assertEquals($handler->getCssFileList(), $expected);
}); });
@ -139,9 +138,9 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/css/common.css', 'screen')); $handler->loadFile(array('./common/css/common.css', 'screen'));
$handler->loadFile(array('./common/css/common.css', 'handled')); $handler->loadFile(array('./common/css/common.css', 'handled'));
$expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => null); $expected[] = array('file' => '/rhymix/common/css/common.css', 'media'=>'all', 'targetie' => null);
$expected[] = array('file' => '/xe/common/css/common.css','media'=>'screen', 'targetie' => null); $expected[] = array('file' => '/rhymix/common/css/common.css','media'=>'screen', 'targetie' => null);
$expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'handled', 'targetie' => null); $expected[] = array('file' => '/rhymix/common/css/common.css', 'media'=>'handled', 'targetie' => null);
$this->assertEquals($handler->getCssFileList(), $expected); $this->assertEquals($handler->getCssFileList(), $expected);
}); });
@ -151,8 +150,8 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler = new FrontEndFileHandler(); $handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/css/xe.css')); $handler->loadFile(array('./common/css/xe.css'));
$handler->loadFile(array('./common/css/mobile.css')); $handler->loadFile(array('./common/css/mobile.css'));
$expected[] = array('file' => '/xe/files/cache/minify/common.css.xe.min.css', 'media' => 'all', 'targetie' => null); $expected[] = array('file' => '/rhymix/files/cache/minify/common.css.xe.min.css', 'media' => 'all', 'targetie' => null);
$expected[] = array('file' => '/xe/files/cache/minify/common.css.mobile.min.css', 'media' => 'all', 'targetie' => null); $expected[] = array('file' => '/rhymix/files/cache/minify/common.css.mobile.min.css', 'media' => 'all', 'targetie' => null);
$result = $handler->getCssFileList(); $result = $handler->getCssFileList();
$result[0]['file'] = preg_replace('/\?\d+$/', '', $result[0]['file']); $result[0]['file'] = preg_replace('/\?\d+$/', '', $result[0]['file']);
$result[1]['file'] = preg_replace('/\?\d+$/', '', $result[1]['file']); $result[1]['file'] = preg_replace('/\?\d+$/', '', $result[1]['file']);

View file

@ -1,5 +1,4 @@
<?php <?php
require_once _XE_PATH_.'classes/security/Security.class.php';
class OldSecurityTest extends \Codeception\TestCase\Test class OldSecurityTest extends \Codeception\TestCase\Test
{ {

View file

@ -1,7 +1,4 @@
<?php <?php
require_once _XE_PATH_.'classes/file/FileHandler.class.php';
require_once _XE_PATH_.'classes/template/TemplateHandler.class.php';
$_SERVER['SCRIPT_NAME'] = '/xe/tests/unit/classes/template/index.php';
class TemplateHandlerTest extends \Codeception\TestCase\Test class TemplateHandlerTest extends \Codeception\TestCase\Test
{ {
@ -148,12 +145,12 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// relative path1 // relative path1
array( array(
'<img src="http://naver.com/naver.gif"><input type="image" src="../local.gif" />', '<img src="http://naver.com/naver.gif"><input type="image" src="../local.gif" />',
'?><img src="http://naver.com/naver.gif"><input type="image" src="/xe/tests/unit/classes/local.gif" />' '?><img src="http://naver.com/naver.gif"><input type="image" src="/rhymix/tests/unit/classes/local.gif" />'
), ),
// relative path2 // relative path2
array( array(
'<img src="http://naver.com/naver.gif"><input type="image" src="../../../dir/local.gif" />', '<img src="http://naver.com/naver.gif"><input type="image" src="../../../dir/local.gif" />',
'?><img src="http://naver.com/naver.gif"><input type="image" src="/xe/tests/dir/local.gif" />' '?><img src="http://naver.com/naver.gif"><input type="image" src="/rhymix/tests/dir/local.gif" />'
), ),
// error case // error case
array( array(
@ -213,7 +210,7 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// issue 584 // issue 584
array( array(
'<img cond="$oBodex->display_extra_images[\'mobile\'] && $arr_extra && $arr_extra->bodex->mobile" src="./images/common/mobile.gif" title="mobile" alt="mobile" />', '<img cond="$oBodex->display_extra_images[\'mobile\'] && $arr_extra && $arr_extra->bodex->mobile" src="./images/common/mobile.gif" title="mobile" alt="mobile" />',
PHP_EOL . 'if($__Context->oBodex->display_extra_images[\'mobile\'] && $__Context->arr_extra && $__Context->arr_extra->bodex->mobile){ ?><img src="/xe/tests/unit/classes/template/images/common/mobile.gif" title="mobile" alt="mobile" /><?php } ?>' PHP_EOL . 'if($__Context->oBodex->display_extra_images[\'mobile\'] && $__Context->arr_extra && $__Context->arr_extra->bodex->mobile){ ?><img src="/rhymix/tests/unit/classes/template/images/common/mobile.gif" title="mobile" alt="mobile" /><?php } ?>'
), ),
// issue 831 // issue 831
array( array(
@ -222,8 +219,8 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
), ),
// issue 746 // issue 746
array( array(
'<img src="../myxe/xe/img.png" />', '<img src="../whatever/img.png" />',
'?><img src="/xe/tests/unit/classes/myxe/xe/img.png" />' '?><img src="/rhymix/tests/unit/classes/whatever/img.png" />'
), ),
// issue 696 // issue 696
array( array(
@ -233,47 +230,47 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img cond="$foo->bar" src="../common/mobile.gif" />', '<img cond="$foo->bar" src="../common/mobile.gif" />',
PHP_EOL . 'if($__Context->foo->bar){ ?><img src="/xe/tests/unit/classes/common/mobile.gif" /><?php } ?>' PHP_EOL . 'if($__Context->foo->bar){ ?><img src="/rhymix/tests/unit/classes/common/mobile.gif" /><?php } ?>'
), ),
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img cond="$foo->bar > 100" alt="a!@#$%^&*()_-=[]{}?/" src="../common/mobile.gif" />', '<img cond="$foo->bar > 100" alt="a!@#$%^&*()_-=[]{}?/" src="../common/mobile.gif" />',
PHP_EOL . 'if($__Context->foo->bar > 100){ ?><img alt="a!@#$%^&*()_-=[]{}?/" src="/xe/tests/unit/classes/common/mobile.gif" /><?php } ?>' PHP_EOL . 'if($__Context->foo->bar > 100){ ?><img alt="a!@#$%^&*()_-=[]{}?/" src="/rhymix/tests/unit/classes/common/mobile.gif" /><?php } ?>'
), ),
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img src="../common/mobile.gif" cond="$foo->bar" />', '<img src="../common/mobile.gif" cond="$foo->bar" />',
PHP_EOL . 'if($__Context->foo->bar){ ?><img src="/xe/tests/unit/classes/common/mobile.gif" /><?php } ?>' PHP_EOL . 'if($__Context->foo->bar){ ?><img src="/rhymix/tests/unit/classes/common/mobile.gif" /><?php } ?>'
), ),
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img class="tmp_class" cond="!$module_info->title" src="../img/common/blank.gif" />', '<img class="tmp_class" cond="!$module_info->title" src="../img/common/blank.gif" />',
PHP_EOL . 'if(!$__Context->module_info->title){ ?><img class="tmp_class" src="/xe/tests/unit/classes/img/common/blank.gif" /><?php } ?>' PHP_EOL . 'if(!$__Context->module_info->title){ ?><img class="tmp_class" src="/rhymix/tests/unit/classes/img/common/blank.gif" /><?php } ?>'
), ),
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img cond="$mi->title" class="tmp_class"|cond="$mi->use" src="../img/common/blank.gif" />', '<img cond="$mi->title" class="tmp_class"|cond="$mi->use" src="../img/common/blank.gif" />',
PHP_EOL . 'if($__Context->mi->title){ ?><img<?php if($__Context->mi->use){ ?> class="tmp_class"<?php } ?> src="/xe/tests/unit/classes/img/common/blank.gif" /><?php } ?>' PHP_EOL . 'if($__Context->mi->title){ ?><img<?php if($__Context->mi->use){ ?> class="tmp_class"<?php } ?> src="/rhymix/tests/unit/classes/img/common/blank.gif" /><?php } ?>'
), ),
array( array(
'<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />', '<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />',
'?><input foo="bar" /> <?php if($__Context->foo->bar){ ?><img alt="alt" src="/xe/tests/unit/classes/common/mobile.gif" /><?php } ?>' '?><input foo="bar" /> <?php if($__Context->foo->bar){ ?><img alt="alt" src="/rhymix/tests/unit/classes/common/mobile.gif" /><?php } ?>'
), ),
array( array(
'<input foo="bar" />' . "\n" . '<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />', '<input foo="bar" />' . "\n" . '<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />',
'?><input foo="bar" />' . PHP_EOL . '<input foo="bar" /> <?php if($__Context->foo->bar){ ?><img alt="alt" src="/xe/tests/unit/classes/common/mobile.gif" /><?php } ?>' '?><input foo="bar" />' . PHP_EOL . '<input foo="bar" /> <?php if($__Context->foo->bar){ ?><img alt="alt" src="/rhymix/tests/unit/classes/common/mobile.gif" /><?php } ?>'
), ),
array( array(
'asf <img src="{$foo->bar}" />', 'asf <img src="{$foo->bar}" />',
'?>asf <img src="<?php echo $__Context->foo->bar ?>" />' '?>asf <img src="<?php echo $__Context->foo->bar ?>" />'
), ),
array( array(
'<img alt="" '.PHP_EOL.' src="../myxe/xe/img.png" />', '<img alt="" '.PHP_EOL.' src="../whatever/img.png" />',
'?><img alt="" '.PHP_EOL.' src="/xe/tests/unit/classes/myxe/xe/img.png" />' '?><img alt="" '.PHP_EOL.' src="/rhymix/tests/unit/classes/whatever/img.png" />'
), ),
array( array(
'<input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="../myxe/xe/img.png" /> <input>asdf src="../img/img.gif" asdf</input>', '<input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="../whatever/img.png" /> <input>asdf src="../img/img.gif" asdf</input>',
'?><input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="/xe/tests/unit/classes/myxe/xe/img.png" /> <input>asdf src="../img/img.gif" asdf</input>' '?><input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="/rhymix/tests/unit/classes/whatever/img.png" /> <input>asdf src="../img/img.gif" asdf</input>'
), ),
array( array(
'<input>asdf src="../img/img.gif" asdf</input>', '<input>asdf src="../img/img.gif" asdf</input>',

View file

@ -100,11 +100,9 @@ class SecurityTest extends \Codeception\TestCase\Test
$_SERVER['REQUEST_METHOD'] = 'POST'; $_SERVER['REQUEST_METHOD'] = 'POST';
$this->assertTrue(Rhymix\Framework\Security::checkCSRF()); $this->assertTrue(Rhymix\Framework\Security::checkCSRF());
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$_SERVER['HTTP_REFERER'] = 'http://www.foobar.com/'; $_SERVER['HTTP_REFERER'] = 'http://www.foobar.com/';
$this->assertFalse(Rhymix\Framework\Security::checkCSRF()); $this->assertFalse(Rhymix\Framework\Security::checkCSRF());
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$this->assertTrue(Rhymix\Framework\Security::checkCSRF('http://www.rhymix.org/')); $this->assertTrue(Rhymix\Framework\Security::checkCSRF('http://www.rhymix.org/'));
} }

View file

@ -4,37 +4,42 @@ class URLTest extends \Codeception\TestCase\Test
{ {
public function testGetCurrentURL() public function testGetCurrentURL()
{ {
$protocol = \RX_SSL ? 'https://' : 'http://'; $old_request_uri = $_SERVER['REQUEST_URI'];
$_SERVER['HTTP_HOST'] = 'www.rhymix.org'; $_SERVER['REQUEST_URI'] = '/rhymix/index.php?foo=bar&xe=sucks';
$_SERVER['REQUEST_URI'] = '/index.php?foo=bar&xe=sucks';
$full_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// Getting the current URL // Getting the current URL
$this->assertEquals($full_url, Rhymix\Framework\URL::getCurrentURL()); $this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar&xe=sucks', Rhymix\Framework\URL::getCurrentURL());
// Adding items to the query string // Adding items to the query string
$this->assertEquals($full_url . '&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::getCurrentURL(array('var' => '1', 'arr' => array(2, 3)))); $this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar&xe=sucks&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::getCurrentURL(array('var' => '1', 'arr' => array(2, 3))));
// Removing item from the query string // Removing item from the query string
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php?xe=sucks', Rhymix\Framework\URL::getCurrentURL(array('foo' => null))); $this->assertEquals('https://www.rhymix.org/rhymix/index.php?xe=sucks', Rhymix\Framework\URL::getCurrentURL(array('foo' => null)));
// Removing all items from the query string // Removing all items from the query string
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php', Rhymix\Framework\URL::getCurrentURL(array('foo' => null, 'xe' => null))); $this->assertEquals('https://www.rhymix.org/rhymix/index.php', Rhymix\Framework\URL::getCurrentURL(array('foo' => null, 'xe' => null)));
// Adding and removing parameters at the same time // Adding and removing parameters at the same time
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php?xe=sucks&l=ko', Rhymix\Framework\URL::getCurrentURL(array('l' => 'ko', 'foo' => null))); $this->assertEquals('https://www.rhymix.org/rhymix/index.php?xe=sucks&l=ko', Rhymix\Framework\URL::getCurrentURL(array('l' => 'ko', 'foo' => null)));
$_SERVER['REQUEST_URI'] = $old_request_uri;
}
public function testGetCurrentDomainURL()
{
$this->assertEquals('https://www.rhymix.org/', Rhymix\Framework\URL::getCurrentDomainURL());
$this->assertEquals('https://www.rhymix.org/', Rhymix\Framework\URL::getCurrentDomainURL('/'));
$this->assertEquals('https://www.rhymix.org/foo/bar', Rhymix\Framework\URL::getCurrentDomainURL('/foo/bar'));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar', Rhymix\Framework\URL::getCurrentDomainURL('rhymix/index.php?foo=bar'));
} }
public function testGetCanonicalURL() public function testGetCanonicalURL()
{ {
$protocol = \RX_SSL ? 'https://' : 'http://';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$tests = array( $tests = array(
'foo/bar' => $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'foo/bar', 'foo/bar' => 'https://www.rhymix.org/rhymix/foo/bar',
'./foo/bar' => $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'foo/bar', './foo/bar' => 'https://www.rhymix.org/rhymix/foo/bar',
'/foo/bar' => $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'foo/bar', '/foo/bar' => 'https://www.rhymix.org/rhymix/foo/bar',
'//www.example.com/foo' => $protocol . 'www.example.com/foo', '//www.example.com/foo' => 'https://www.example.com/foo',
'http://xn--cg4bkiv2oina.com/' => 'http://삼성전자.com/', 'http://xn--cg4bkiv2oina.com/' => 'http://삼성전자.com/',
); );
@ -60,21 +65,17 @@ class URLTest extends \Codeception\TestCase\Test
public function testModifyURL() public function testModifyURL()
{ {
$protocol = \RX_SSL ? 'https://' : 'http://';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$url = $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php?foo=bar';
// Conversion to absolute // Conversion to absolute
$this->assertEquals($url, Rhymix\Framework\URL::modifyURL('./index.php?foo=bar')); $this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar', $url = Rhymix\Framework\URL::modifyURL('./index.php?foo=bar'));
// Adding items to the query string // Adding items to the query string
$this->assertEquals($url . '&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::modifyURL($url, array('var' => '1', 'arr' => array(2, 3)))); $this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::modifyURL($url, array('var' => '1', 'arr' => array(2, 3))));
// Removing item from the query string // Removing item from the query string
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php', Rhymix\Framework\URL::modifyURL($url, array('foo' => null))); $this->assertEquals('https://www.rhymix.org/rhymix/index.php', Rhymix\Framework\URL::modifyURL($url, array('foo' => null)));
// Adding and removing parameters at the same time // Adding and removing parameters at the same time
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php?l=ko', Rhymix\Framework\URL::modifyURL($url, array('l' => 'ko', 'foo' => null))); $this->assertEquals('https://www.rhymix.org/rhymix/index.php?l=ko', Rhymix\Framework\URL::modifyURL($url, array('l' => 'ko', 'foo' => null)));
} }
public function testIsInternalURL() public function testIsInternalURL()
@ -82,6 +83,25 @@ class URLTest extends \Codeception\TestCase\Test
// This function is checked in Security::checkCSRF() // This function is checked in Security::checkCSRF()
} }
public function testURLFromServerPath()
{
$this->assertEquals('https://www.rhymix.org/rhymix/', Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php', Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR . 'index.php'));
$this->assertEquals('https://www.rhymix.org/rhymix/foo/bar', Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR . '/foo/bar'));
$this->assertEquals(false, Rhymix\Framework\URL::fromServerPath(dirname(dirname(\RX_BASEDIR))));
$this->assertEquals(false, Rhymix\Framework\URL::fromServerPath('C:/Windows'));
}
public function testURLToServerPath()
{
$this->assertEquals(\RX_BASEDIR . 'index.php', Rhymix\Framework\URL::toServerPath('http://www.rhymix.org/rhymix/index.php'));
$this->assertEquals(\RX_BASEDIR . 'foo/bar', Rhymix\Framework\URL::toServerPath('http://www.rhymix.org/rhymix/foo/bar?arg=baz'));
$this->assertEquals(\RX_BASEDIR . 'foo/bar', Rhymix\Framework\URL::toServerPath('./foo/bar'));
$this->assertEquals(\RX_BASEDIR . 'foo/bar', Rhymix\Framework\URL::toServerPath('foo/bar/../bar'));
$this->assertEquals(false, Rhymix\Framework\URL::toServerPath('http://other.domain.com/'));
$this->assertEquals(false, Rhymix\Framework\URL::toServerPath('//other.domain.com/'));
}
public function testEncodeIdna() public function testEncodeIdna()
{ {
$this->assertEquals('xn--9i1bl3b186bf9e.xn--3e0b707e', Rhymix\Framework\URL::encodeIdna('퓨니코드.한국')); $this->assertEquals('xn--9i1bl3b186bf9e.xn--3e0b707e', Rhymix\Framework\URL::encodeIdna('퓨니코드.한국'));

View file

@ -1,5 +1,7 @@
<?php <?php
use Rhymix\Framework\Filters\FilenameFilter;
class FilenameFilterTest extends \Codeception\TestCase\Test class FilenameFilterTest extends \Codeception\TestCase\Test
{ {
public function testFilenameFilterClean() public function testFilenameFilterClean()
@ -35,8 +37,37 @@ class FilenameFilterTest extends \Codeception\TestCase\Test
foreach ($tests as $from => $to) foreach ($tests as $from => $to)
{ {
$result = Rhymix\Framework\Filters\FilenameFilter::clean($from); $result = FilenameFilter::clean($from);
$this->assertEquals($to, $result); $this->assertEquals($to, $result);
} }
} }
public function testFilenameFilterCleanPath()
{
// Remove extra dots and slashes.
$this->assertEquals('/usr/share/foo/bar.jpg', FilenameFilter::cleanPath('/usr/share/foo//./baz/../bar.jpg'));
$this->assertEquals('/usr/share/foo/bar.jpg', FilenameFilter::cleanPath('/usr/share/foo/././baz/../../foo/bar.jpg'));
$this->assertEquals('/usr/share', FilenameFilter::cleanPath('/usr/share/foo/..'));
$this->assertEquals('/usr/share', FilenameFilter::cleanPath('/usr/share/foo/bar/../baz/../../'));
// Test internal paths.
$this->assertEquals(\RX_BASEDIR . 'common/js/debug.js', FilenameFilter::cleanPath('common/js/debug.js'));
$this->assertEquals(\RX_BASEDIR . 'common/js/debug.js', FilenameFilter::cleanPath('./common/js/debug.js'));
// Test Windows paths.
$this->assertEquals('C:/Windows/Notepad.exe', FilenameFilter::cleanPath('C:\\Windows\\System32\\..\\Notepad.exe'));
$this->assertEquals('//vboxsrv/hello/world', FilenameFilter::cleanPath('\\\\vboxsrv\\hello\\world'));
// Test absolute URLs.
$this->assertEquals('https://www.rhymix.org/foo/bar', FilenameFilter::cleanPath('https://www.rhymix.org/foo/.//bar'));
$this->assertEquals('//www.rhymix.org/foo/bar', FilenameFilter::cleanPath('//www.rhymix.org/foo/.//bar'));
// Do not remove .. if there is no parent directory.
$this->assertEquals('C:/../foobar', FilenameFilter::cleanPath('C:\\..\foobar\\'));
$this->assertEquals('/../foobar', FilenameFilter::cleanPath('/../foobar/'));
// Remove query strings and URL fragments.
$this->assertEquals(\RX_BASEDIR . 'index.php', FilenameFilter::cleanPath('index.php?foo=bar'));
$this->assertEquals(\RX_BASEDIR . 'index.php', FilenameFilter::cleanPath('index.php#baz'));
}
} }