From aa1b3cfb523d27371ebc653a0a1d8016f2eee2e2 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 19 Apr 2020 21:58:45 +0800 Subject: [PATCH 1/4] Normalize dir separator of Exceptions::cleanPath --- system/Debug/Exceptions.php | 26 ++++++++++++++++---------- system/bootstrap.php | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index ef7c45a3ed6e..b7f2f0e0299f 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -372,17 +372,23 @@ protected function determineCodes(Throwable $exception): array */ public static function cleanPath(string $file): string { - if (strpos($file, APPPATH) === 0) + switch (true) { - $file = 'APPPATH/' . substr($file, strlen(APPPATH)); - } - elseif (strpos($file, SYSTEMPATH) === 0) - { - $file = 'SYSTEMPATH/' . substr($file, strlen(SYSTEMPATH)); - } - elseif (strpos($file, FCPATH) === 0) - { - $file = 'FCPATH/' . substr($file, strlen(FCPATH)); + case strpos($file, VIEWPATH) === 0: + $file = 'VIEWPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VIEWPATH)); + break; + case strpos($file, APPPATH) === 0: + $file = 'APPPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(APPPATH)); + break; + case strpos($file, SYSTEMPATH) === 0: + $file = 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(SYSTEMPATH)); + break; + case strpos($file, FCPATH) === 0: + $file = 'FCPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(FCPATH)); + break; + case defined('VENDORPATH') && strpos($file, VENDORPATH) === 0; + $file = 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VENDORPATH)); + break; } return $file; diff --git a/system/bootstrap.php b/system/bootstrap.php index 33ab45598d07..150881917c05 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -87,6 +87,14 @@ define('TESTPATH', realpath($paths->testsDirectory) . DIRECTORY_SEPARATOR); } +/** + * The path to the views directory + */ +if (! defined('VIEWPATH')) +{ + define('VIEWPATH', realpath($paths->viewDirectory) . DIRECTORY_SEPARATOR); +} + /* * --------------------------------------------------------------- * GRAB OUR CONSTANTS & COMMON @@ -139,6 +147,16 @@ class_alias('Config\Services', 'CodeIgniter\Services'); // Now load Composer's if it's available if (is_file(COMPOSER_PATH)) { + /** + * The path to the vendor directory. + * + * We do not want to enforce this, so set the constant if Composer was used. + */ + if (! defined('VENDORPATH')) + { + define('VENDORPATH', realpath(ROOTPATH . 'vendor') . DIRECTORY_SEPARATOR); + } + require_once COMPOSER_PATH; } From 8148ad8ba8fc674351dc4cc171f4cb0c2d66990e Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 19 Apr 2020 23:52:05 +0800 Subject: [PATCH 2/4] Add tests for PR #2847 --- system/Test/bootstrap.php | 2 ++ tests/system/Debug/ExceptionsTest.php | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 4a217fef12e7..8d03abf8c519 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -16,12 +16,14 @@ defined('APPPATH') || define('APPPATH', realpath($paths->appDirectory) . DIRECTORY_SEPARATOR); defined('WRITEPATH') || define('WRITEPATH', realpath($paths->writableDirectory) . DIRECTORY_SEPARATOR); defined('SYSTEMPATH') || define('SYSTEMPATH', realpath($paths->systemDirectory) . DIRECTORY_SEPARATOR); +defined('VIEWPATH') || define('VIEWPATH', realpath($paths->viewDirectory) . DIRECTORY_SEPARATOR); defined('ROOTPATH') || define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR); defined('CIPATH') || define('CIPATH', realpath(SYSTEMPATH . '../') . DIRECTORY_SEPARATOR); defined('FCPATH') || define('FCPATH', realpath(PUBLICPATH) . DIRECTORY_SEPARATOR); defined('TESTPATH') || define('TESTPATH', realpath(HOMEPATH . 'tests/') . DIRECTORY_SEPARATOR); defined('SUPPORTPATH') || define('SUPPORTPATH', realpath(TESTPATH . '_support/') . DIRECTORY_SEPARATOR); defined('COMPOSER_PATH') || define('COMPOSER_PATH', realpath(HOMEPATH . 'vendor/autoload.php')); +defined('VENDORPATH') || define('VENDORPATH', realpath(HOMEPATH . 'vendor') . DIRECTORY_SEPARATOR); // Load Common.php from App then System if (file_exists(APPPATH . 'Common.php')) diff --git a/tests/system/Debug/ExceptionsTest.php b/tests/system/Debug/ExceptionsTest.php index d9f1dae8b8cc..d54f4c7b8028 100644 --- a/tests/system/Debug/ExceptionsTest.php +++ b/tests/system/Debug/ExceptionsTest.php @@ -7,4 +7,26 @@ public function testNew() $actual = new Exceptions(new \Config\Exceptions(), \Config\Services::request(), \Config\Services::response()); $this->assertInstanceOf(Exceptions::class, $actual); } + + /** + * @dataProvider dirtyPathsProvider + */ + public function testCleanPaths($file, $expected) + { + $this->assertEquals($expected, Exceptions::cleanPath($file)); + } + + public function dirtyPathsProvider() + { + $ds = DIRECTORY_SEPARATOR; + + return [ + [APPPATH . 'Config' . $ds . 'App.php', 'APPPATH' . $ds . 'Config' . $ds . 'App.php'], + [APPPATH . 'Views' . $ds . 'welcome_message.php', 'VIEWPATH' . $ds . 'welcome_message.php'], + [SYSTEMPATH . 'CodeIgniter.php', 'SYSTEMPATH' . $ds . 'CodeIgniter.php'], + [VIEWPATH . 'errors' . $ds . 'html' . $ds . 'error_exception.php', 'VIEWPATH' . $ds . 'errors' . $ds . 'html' . $ds . 'error_exception.php'], + [VENDORPATH . 'autoload.php', 'VENDORPATH' . $ds . 'autoload.php'], + [FCPATH . 'index.php', 'FCPATH' . $ds . 'index.php'], + ]; + } } From 4a5fd8f082bca073f5ddd3fa4c74219d3eabcffc Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 19 Apr 2020 23:52:05 +0800 Subject: [PATCH 3/4] Add tests for PR #2847 --- system/Test/bootstrap.php | 2 ++ tests/system/Debug/ExceptionsTest.php | 40 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 4a217fef12e7..8d03abf8c519 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -16,12 +16,14 @@ defined('APPPATH') || define('APPPATH', realpath($paths->appDirectory) . DIRECTORY_SEPARATOR); defined('WRITEPATH') || define('WRITEPATH', realpath($paths->writableDirectory) . DIRECTORY_SEPARATOR); defined('SYSTEMPATH') || define('SYSTEMPATH', realpath($paths->systemDirectory) . DIRECTORY_SEPARATOR); +defined('VIEWPATH') || define('VIEWPATH', realpath($paths->viewDirectory) . DIRECTORY_SEPARATOR); defined('ROOTPATH') || define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR); defined('CIPATH') || define('CIPATH', realpath(SYSTEMPATH . '../') . DIRECTORY_SEPARATOR); defined('FCPATH') || define('FCPATH', realpath(PUBLICPATH) . DIRECTORY_SEPARATOR); defined('TESTPATH') || define('TESTPATH', realpath(HOMEPATH . 'tests/') . DIRECTORY_SEPARATOR); defined('SUPPORTPATH') || define('SUPPORTPATH', realpath(TESTPATH . '_support/') . DIRECTORY_SEPARATOR); defined('COMPOSER_PATH') || define('COMPOSER_PATH', realpath(HOMEPATH . 'vendor/autoload.php')); +defined('VENDORPATH') || define('VENDORPATH', realpath(HOMEPATH . 'vendor') . DIRECTORY_SEPARATOR); // Load Common.php from App then System if (file_exists(APPPATH . 'Common.php')) diff --git a/tests/system/Debug/ExceptionsTest.php b/tests/system/Debug/ExceptionsTest.php index d9f1dae8b8cc..0a79410b0fe4 100644 --- a/tests/system/Debug/ExceptionsTest.php +++ b/tests/system/Debug/ExceptionsTest.php @@ -7,4 +7,44 @@ public function testNew() $actual = new Exceptions(new \Config\Exceptions(), \Config\Services::request(), \Config\Services::response()); $this->assertInstanceOf(Exceptions::class, $actual); } + + /** + * @dataProvider dirtyPathsProvider + */ + public function testCleanPaths($file, $expected) + { + $this->assertEquals($expected, Exceptions::cleanPath($file)); + } + + public function dirtyPathsProvider() + { + $ds = DIRECTORY_SEPARATOR; + + return [ + [ + APPPATH . 'Config' . $ds . 'App.php', + 'APPPATH' . $ds . 'Config' . $ds . 'App.php', + ], + [ + APPPATH . 'Views' . $ds . 'welcome_message.php', + 'VIEWPATH' . $ds . 'welcome_message.php', + ], + [ + SYSTEMPATH . 'CodeIgniter.php', + 'SYSTEMPATH' . $ds . 'CodeIgniter.php', + ], + [ + VIEWPATH . 'errors' . $ds . 'html' . $ds . 'error_exception.php', + 'VIEWPATH' . $ds . 'errors' . $ds . 'html' . $ds . 'error_exception.php', + ], + [ + VENDORPATH . 'autoload.php', + 'VENDORPATH' . $ds . 'autoload.php', + ], + [ + FCPATH . 'index.php', + 'FCPATH' . $ds . 'index.php', + ], + ]; + } } From 4cb62d1023aac5082bedeba7583f8d10497f0658 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 20 Apr 2020 18:12:25 +0800 Subject: [PATCH 4/4] Removed view paths on paths to clean --- system/Debug/Exceptions.php | 3 --- system/Test/bootstrap.php | 1 - system/bootstrap.php | 8 -------- tests/system/Debug/ExceptionsTest.php | 8 -------- 4 files changed, 20 deletions(-) diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index b7f2f0e0299f..7f7ca2cb2628 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -374,9 +374,6 @@ public static function cleanPath(string $file): string { switch (true) { - case strpos($file, VIEWPATH) === 0: - $file = 'VIEWPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VIEWPATH)); - break; case strpos($file, APPPATH) === 0: $file = 'APPPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(APPPATH)); break; diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 8d03abf8c519..83585f374171 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -16,7 +16,6 @@ defined('APPPATH') || define('APPPATH', realpath($paths->appDirectory) . DIRECTORY_SEPARATOR); defined('WRITEPATH') || define('WRITEPATH', realpath($paths->writableDirectory) . DIRECTORY_SEPARATOR); defined('SYSTEMPATH') || define('SYSTEMPATH', realpath($paths->systemDirectory) . DIRECTORY_SEPARATOR); -defined('VIEWPATH') || define('VIEWPATH', realpath($paths->viewDirectory) . DIRECTORY_SEPARATOR); defined('ROOTPATH') || define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR); defined('CIPATH') || define('CIPATH', realpath(SYSTEMPATH . '../') . DIRECTORY_SEPARATOR); defined('FCPATH') || define('FCPATH', realpath(PUBLICPATH) . DIRECTORY_SEPARATOR); diff --git a/system/bootstrap.php b/system/bootstrap.php index 150881917c05..e014ad05e144 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -87,14 +87,6 @@ define('TESTPATH', realpath($paths->testsDirectory) . DIRECTORY_SEPARATOR); } -/** - * The path to the views directory - */ -if (! defined('VIEWPATH')) -{ - define('VIEWPATH', realpath($paths->viewDirectory) . DIRECTORY_SEPARATOR); -} - /* * --------------------------------------------------------------- * GRAB OUR CONSTANTS & COMMON diff --git a/tests/system/Debug/ExceptionsTest.php b/tests/system/Debug/ExceptionsTest.php index 0a79410b0fe4..25fdf8890cb3 100644 --- a/tests/system/Debug/ExceptionsTest.php +++ b/tests/system/Debug/ExceptionsTest.php @@ -25,18 +25,10 @@ public function dirtyPathsProvider() APPPATH . 'Config' . $ds . 'App.php', 'APPPATH' . $ds . 'Config' . $ds . 'App.php', ], - [ - APPPATH . 'Views' . $ds . 'welcome_message.php', - 'VIEWPATH' . $ds . 'welcome_message.php', - ], [ SYSTEMPATH . 'CodeIgniter.php', 'SYSTEMPATH' . $ds . 'CodeIgniter.php', ], - [ - VIEWPATH . 'errors' . $ds . 'html' . $ds . 'error_exception.php', - 'VIEWPATH' . $ds . 'errors' . $ds . 'html' . $ds . 'error_exception.php', - ], [ VENDORPATH . 'autoload.php', 'VENDORPATH' . $ds . 'autoload.php',