From b1683de32141e80fb17d5ff1a6f460186e22d961 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 26 May 2015 23:51:59 +0200 Subject: [PATCH] Introduced FileNotFoundException MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../file_directory/class_FileIoException.php | 3 +- .../class_FileNotFoundException.php | 42 +++++++++++++++++++ .../main/class_BaseFrameworkSystem.php | 2 +- .../main/console/class_ConsoleTools.php | 2 +- .../backend/class_CachedLocalFileDatabase.php | 7 +--- .../class_FrameworkRawFileInputPointer.php | 10 +++-- .../io_stream/class_FileIoStream.php | 2 +- inc/classes/main/menu/class_BaseMenu.php | 2 +- .../template/class_BaseTemplateEngine.php | 6 +-- index.php | 2 +- 10 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 inc/classes/exceptions/file_directory/class_FileNotFoundException.php diff --git a/inc/classes/exceptions/file_directory/class_FileIoException.php b/inc/classes/exceptions/file_directory/class_FileIoException.php index 9dea76f0..85744cbc 100644 --- a/inc/classes/exceptions/file_directory/class_FileIoException.php +++ b/inc/classes/exceptions/file_directory/class_FileIoException.php @@ -1,6 +1,7 @@ * @version 0.0.0 diff --git a/inc/classes/exceptions/file_directory/class_FileNotFoundException.php b/inc/classes/exceptions/file_directory/class_FileNotFoundException.php new file mode 100644 index 00000000..3117bfaf --- /dev/null +++ b/inc/classes/exceptions/file_directory/class_FileNotFoundException.php @@ -0,0 +1,42 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class FileNotFoundException extends FrameworkException { + /** + * The constructor + * + * @param $fqfn Full-qualified file name of (maybe) missing file + * @param $code Code number for the exception + * @return void + */ + public function __construct ($fqfn, $code) { + // Add a message around the missing class + $message = sprintf('File %s not found.', $fqfn); + + // Call parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 3ea5bcf5..85aeda63 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -3274,7 +3274,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { $isReadable = FALSE; // Is within parameters, so check if it is a file and readable - $isReadable = ((self::isReachableFilePath($fileName)) && (is_file($fileName)) && (is_readable($fileName))); + $isReadable = ((self::isReachableFilePath($fileName)) && (file_exists($fileName)) && (is_file($fileName)) && (is_readable($fileName))); // Return status return $isReadable; diff --git a/inc/classes/main/console/class_ConsoleTools.php b/inc/classes/main/console/class_ConsoleTools.php index 0d269417..7b690ef5 100644 --- a/inc/classes/main/console/class_ConsoleTools.php +++ b/inc/classes/main/console/class_ConsoleTools.php @@ -220,7 +220,7 @@ class ConsoleTools extends BaseFrameworkSystem { // Resolve the IP number $ip = $helperInstance->resolveIpAddress($hostname); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // Fall-back to 'SESSION_SVR' which found on my Sun Station if (isset($_SERVER['SESSION_SVR'])) { // Resolve it diff --git a/inc/classes/main/database/backend/class_CachedLocalFileDatabase.php b/inc/classes/main/database/backend/class_CachedLocalFileDatabase.php index a81dd3cb..935cf27f 100644 --- a/inc/classes/main/database/backend/class_CachedLocalFileDatabase.php +++ b/inc/classes/main/database/backend/class_CachedLocalFileDatabase.php @@ -228,7 +228,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac // Get the file contents try { $infoArray = $this->getDataArrayFromFile($fqfn); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // Not found, so ignore it here } @@ -279,9 +279,6 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac * @return void */ private function updateTableInfoFile (StoreableCriteria $dataSetInstance) { - // "Cache" table name - $tableName = $dataSetInstance->getTableName(); - // Create FQFN for creating the table information file $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info'); @@ -290,7 +287,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac $this->tableInfo[$tableName]['last_updated'] = time(); // Write the data to the file - $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$tableName]); + $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]); } /** diff --git a/inc/classes/main/file_directories/input/raw/class_FrameworkRawFileInputPointer.php b/inc/classes/main/file_directories/input/raw/class_FrameworkRawFileInputPointer.php index a3b37020..9fb6e1db 100644 --- a/inc/classes/main/file_directories/input/raw/class_FrameworkRawFileInputPointer.php +++ b/inc/classes/main/file_directories/input/raw/class_FrameworkRawFileInputPointer.php @@ -40,6 +40,7 @@ class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer { * @throws FileIsEmptyException If the provided file name is empty. * @throws FileIoException If the file is not reachable * @throws FileReadProtectedException If the file is not found or cannot be read + * @throws FileNotFoundException If the file does not exist * @return void */ public static final function createFrameworkRawFileInputPointer ($fileName) { @@ -48,11 +49,14 @@ class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer { // No filename given throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING); } elseif (!BaseFrameworkSystem::isReachableFilePath($fileName)) { - // File does not exist! + // File cannot be accessed (due to open_basedir restriction) throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE); - } elseif (!BaseFrameworkSystem::isReadableFile($fileName)) { - // File does not exist! + } elseif ((!BaseFrameworkSystem::isReadableFile($fileName)) && (file_exists($fileName))) { + // File exists but cannot be read from throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ); + } elseif ((!BaseFrameworkSystem::isReadableFile($fileName)) && (!file_exists($fileName))) { + // File does not exist + throw new FileNotFoundException($fileName, self::EXCEPTION_FILE_NOT_FOUND); } // Try to open a handler diff --git a/inc/classes/main/file_directories/io_stream/class_FileIoStream.php b/inc/classes/main/file_directories/io_stream/class_FileIoStream.php index f51884fd..d0ff5490 100644 --- a/inc/classes/main/file_directories/io_stream/class_FileIoStream.php +++ b/inc/classes/main/file_directories/io_stream/class_FileIoStream.php @@ -82,7 +82,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil // Get a file output pointer try { $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_output_class', array($fileName, 'wb')); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // Bail out ApplicationEntryPoint::app_exit('The application has made a fatal error. Exception: ' . $e->__toString() . ' with message: ' . $e->getMessage()); } diff --git a/inc/classes/main/menu/class_BaseMenu.php b/inc/classes/main/menu/class_BaseMenu.php index 3e37de7d..7bb97e16 100644 --- a/inc/classes/main/menu/class_BaseMenu.php +++ b/inc/classes/main/menu/class_BaseMenu.php @@ -61,7 +61,7 @@ class BaseMenu extends BaseFrameworkSystem { // Load the menu template for this page try { $templateInstance->loadMenuTemplate($command . '_menu_entries'); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // Log exception @TODO Maybe to intrusive? self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Exception caught: ' . $e->__toString() . ', with message: ' . $e->getMessage()); } diff --git a/inc/classes/main/template/class_BaseTemplateEngine.php b/inc/classes/main/template/class_BaseTemplateEngine.php index a447eb57..68511ad1 100644 --- a/inc/classes/main/template/class_BaseTemplateEngine.php +++ b/inc/classes/main/template/class_BaseTemplateEngine.php @@ -702,7 +702,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { try { // Load the raw template data $this->loadRawTemplateData($fqfn); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // If we shall load a code-template we need to switch the file extension if (($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('html_template_type')) && (empty($extOther))) { // Switch over to the code-template extension and try it again @@ -846,7 +846,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Remember this template for recursion detection // RECURSIVE PROTECTION! array_push($this->loadedTemplates, $template); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // Even this is not done... :/ array_push($this->rawTemplates, $template); } @@ -945,7 +945,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Remember this template for recursion detection // RECURSIVE PROTECTION! array_push($this->loadedTemplates, $template); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // This template was never found. We silently ignore it unset($this->rawTemplates[$key]); } diff --git a/index.php b/index.php index 51eb50ef..0a3ef741 100644 --- a/index.php +++ b/index.php @@ -150,7 +150,7 @@ final class ApplicationEntryPoint { // Flush the response $responseInstance->flushBuffer(); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // Even the template 'emergency_exit' wasn't found so output both message exit($message . ', exception: ' . $e->getMessage()); } -- 2.39.2