From 3152d180c056753646e25092bf15c189ccb4c059 Mon Sep 17 00:00:00 2001 From: Joey Kimsey Date: Tue, 21 Jan 2025 20:35:24 -0500 Subject: [PATCH] Updates Adjusted log severity Improved error reporting Improved token handling Improved Config Form handling --- Bin/Bedrock.php | 109 +++++++++++++++++++++++------------------- Classes/Config.php | 15 +++++- Classes/Database.php | 2 +- Config/constants.php | 4 ++ Functions/Check.php | 10 ++-- Functions/Input.php | 6 +-- Functions/Session.php | 12 ++--- 7 files changed, 93 insertions(+), 65 deletions(-) diff --git a/Bin/Bedrock.php b/Bin/Bedrock.php index e07c7ed..da8295a 100644 --- a/Bin/Bedrock.php +++ b/Bin/Bedrock.php @@ -32,6 +32,7 @@ class Bedrock { protected $controllerObject = null; protected $controllerClass = ''; protected $params = []; + protected $controllerError = false; /** * The constructor handles the entire process of parsing the url, @@ -64,55 +65,6 @@ class Bedrock { $this->setVarsFromUrlArray( $urlArray ); } - public function load() { - $this->loadController(); - $this->loadPage(); - } - - public static function getUrl() { - return Routes::getAddress() . Input::get( 'url' ); - } - - protected function loadPage() { - if ( !method_exists( $this->controllerClass, self::$methodName ) ) { - return false; - } - Components::set( 'META_IMAGE', Routes::getAddress() . Config::getValue( 'main/logoLarge' ) ); - Components::set( 'CURRENT_URL', self::getCurrentUrl() ); - Components::set( 'SITENAME', Config::getValue( 'main/name' ) ); - Components::set( 'AUTHOR', '' ); - call_user_func_array( [ $this->controllerObject, self::$methodName ], $this->params ); - Components::set( 'TITLE', Template::parse( $this->controllerObject::$title ) ); - Components::set( 'PAGE_DESCRIPTION', Template::parse( $this->controllerObject::$pageDescription ) ); - Template::render(); - Debug::closeAllGroups(); - // self::$session->updatePage( self::getUrl() ); // where did this method go? - } - - protected function loadController() { - if ( empty( $this->controllerClass ) ) { - $this->controllerClass = (string) APP_SPACE . '\\Controllers\\' . self::$controllerName; - } - $this->controllerObject = new $this->controllerClass; - } - - protected function setController( $name, $namespace ) { - $controllerClass = $namespace . ucfirst( $name ); - if ( Autoloader::testLoad( $controllerClass ) ) { - $this->controllerClass = $controllerClass; - self::$controllerName = $name; - } - } - - protected function setPage( $name ) { - $name = strtolower( $name ); - if ( !method_exists( $this->controllerClass, $name ) ) { - Debug::info( 'setPage - Method not found: ' . $name ); - return false; - } - self::$methodName = $name; - } - protected function setVarsFromUrlArray( $urlArray ) { if ( !empty( $urlArray[0] ) ) { $urlPart = array_shift( $urlArray ); @@ -154,6 +106,65 @@ class Bedrock { } } + public function load() { + $this->loadController(); + $this->loadPage(); + } + + public static function getUrl() { + return Routes::getAddress() . Input::get( 'url' ); + } + + protected function loadPage() { + if ( !method_exists( $this->controllerClass, self::$methodName ) ) { + return false; + } + Components::set( 'META_IMAGE', Routes::getAddress() . Config::getValue( 'main/logoLarge' ) ); + Components::set( 'CURRENT_URL', self::getCurrentUrl() ); + Components::set( 'SITENAME', Config::getValue( 'main/name' ) ?? APP_NAME ); + Components::set( 'AUTHOR', '' ); + call_user_func_array( [ $this->controllerObject, self::$methodName ], $this->params ); + Components::set( 'TITLE', Template::parse( $this->controllerObject::$title ) ); + Components::set( 'PAGE_DESCRIPTION', Template::parse( $this->controllerObject::$pageDescription ) ); + Template::render(); + Debug::closeAllGroups(); + // self::$session->updatePage( self::getUrl() ); // where did this method go? + } + + protected function loadController() { + if ( empty( $this->controllerClass ) ) { + $this->controllerClass = (string) APP_SPACE . '\\Controllers\\' . self::$controllerName; + } + if ( empty( $this->controllerError ) ) { + Components::set( 'TOKEN', Token::generate() ); + } + $this->controllerObject = new $this->controllerClass; + } + + protected function setController( $name, $namespace ) { + $controllerClass = $namespace . ucfirst( $name ); + if ( Autoloader::testLoad( $controllerClass ) ) { + $this->controllerClass = $controllerClass; + self::$controllerName = $name; + } else { + $this->controllerError = 'setController - Controller not found. Name: ' . $name . ' Namespace: ' . $namespace; + Debug::info( $this->controllerError ); + } + } + + protected function setPage( $name ) { + $name = strtolower( $name ); + if ( empty( $this->controllerClass ) ) { + Debug::info( 'setPage - controllerClass Empty' ); + return false; + } + if ( ! method_exists( $this->controllerClass, $name ) ) { + Debug::info( 'setPage - Method not found. Controller: ' . $this->controllerClass . ' Method: ' . $name ); + return false; + } + self::$methodName = $name; + } + public static function getCurrentUrl() { return Sanitize::url( Input::get( 'url' ) ); } diff --git a/Classes/Config.php b/Classes/Config.php index 2976fb3..84a20a7 100644 --- a/Classes/Config.php +++ b/Classes/Config.php @@ -14,6 +14,7 @@ namespace TheTempusProject\Bedrock\Classes; use TheTempusProject\Canary\Bin\Canary as Debug; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Bedrock\Functions\Input; +use TheTempusProject\Bedrock\Functions\Upload; class Config { public static $config = false; @@ -145,7 +146,19 @@ class Config { } $fieldname = str_ireplace( '/', '-', $name ); if ( Input::exists( $fieldname ) ) { - $this->update( $category, $field, Input::post( $fieldname ) ); + if ( 'file' == $node['type'] ) { + $upload = Upload::image( $fieldname, IMAGE_DIRECTORY ); + if ( $upload ) { + $route = str_replace( APP_ROOT_DIRECTORY, '', IMAGE_DIRECTORY ); + $this->update( $category, $field, $route . Upload::last() ); + } else { + Debug::error( 'There was an error with your upload.'); + } + } else { + $this->update( $category, $field, Input::post( $fieldname ) ); + } + } elseif ( 'radio' == $node['type'] ) { + $this->update( $category, $field, false ); } } } diff --git a/Classes/Database.php b/Classes/Database.php index abc4dd6..71e6e79 100644 --- a/Classes/Database.php +++ b/Classes/Database.php @@ -274,7 +274,7 @@ class Database { foreach ( $params as $param ) { $x++; if ( is_array( $param ) ) { - dv( $param ); + // dv( $param ); } $this->query->bindValue( $x, $param ); } diff --git a/Config/constants.php b/Config/constants.php index e760edd..8501143 100644 --- a/Config/constants.php +++ b/Config/constants.php @@ -173,6 +173,10 @@ if (!defined('TOKEN_ENABLED')) { define('TOKEN_ENABLED', true); } +// dUH + if (!defined('APP_NAME')) { + define('APP_NAME', 'Bedrock Application'); + } # Tell the app all constants have been loaded. if ( ! defined('BEDROCK_CONSTANTS_LOADED' ) ) { define( 'BEDROCK_CONSTANTS_LOADED', true ); diff --git a/Functions/Check.php b/Functions/Check.php index 7240acd..184b170 100644 --- a/Functions/Check.php +++ b/Functions/Check.php @@ -100,24 +100,24 @@ class Check { */ public static function imageUpload( $imageName ) { if ( !Config::getValue( 'uploads/images' ) ) { - self::addError( 'Image uploads are disabled.' ); + self::addUserError( 'Image uploads are disabled.' ); return false; } if ( !isset( $_FILES[$imageName] ) ) { - self::addError( 'File not found.', $imageName ); + self::addUserError( 'File not found.', $imageName ); return false; } if ( $_FILES[$imageName]['error'] != 0 ) { - self::addError( 'File error:' . $_FILES[$imageName]['error'] ); + self::addUserError( 'File error:' . $_FILES[$imageName]['error'] ); return false; } if ( $_FILES[$imageName]['size'] > Config::getValue( 'uploads/maxImageSize' ) ) { - self::addError( 'Image is too large.' ); + self::addUserError( 'Image is too large.' ); return false; } $fileType = strrchr( $_FILES[$imageName]['name'], '.' ); if ( !( in_array( $fileType, ALLOWED_IMAGE_UPLOAD_EXTENTIONS ) ) ) { - self::addError( 'Invalid image type', $fileType ); + self::addUserError( 'Invalid image type', $fileType ); return false; } return true; diff --git a/Functions/Input.php b/Functions/Input.php index 03cdb09..c5b1cc8 100644 --- a/Functions/Input.php +++ b/Functions/Input.php @@ -29,7 +29,7 @@ class Input { } elseif ( self::file( $data ) ) { return true; } else { - Debug::info( 'Input::exists: No input Found: '. $data ); + Debug::log( 'Input::exists: No input Found: '. $data ); return false; } } @@ -43,11 +43,11 @@ class Input { */ public static function file( $data ) { if ( !isset( $_FILES[$data] ) ) { - Debug::debug( "Input - file : $data not found." ); + Debug::log( "Input - file : $data not found." ); return false; } if ( $_FILES[$data]['tmp_name'] == '' ) { - Debug::debug( "Input - file : $data empty." ); + Debug::log( "Input - file : $data empty." ); return false; } return $_FILES[$data]; diff --git a/Functions/Session.php b/Functions/Session.php index 0f9bb77..c61aac6 100644 --- a/Functions/Session.php +++ b/Functions/Session.php @@ -31,7 +31,7 @@ class Session { if ( isset( $_SESSION[ $sessionName ] ) ) { return true; } - Debug::info( "Session::exists - Session not found: $sessionName" ); + Debug::log( "Session::exists - Session not found: $sessionName" ); return false; } @@ -49,7 +49,7 @@ class Session { if ( self::exists( $name ) ) { return $_SESSION[ $sessionName ]; } - Debug::info( "Session::get - Session not found: $sessionName" ); + Debug::log( "Session::get - Session not found: $sessionName" ); return false; } @@ -69,7 +69,7 @@ class Session { } $sessionName = DEFAULT_SESSION_PREFIX . $name; $_SESSION[ $sessionName ] = $data; - Debug::info( "Session::get - Created/Updated: $sessionName" ); + Debug::log( "Session::get - Created/Updated: $sessionName" ); return true; } @@ -107,7 +107,7 @@ class Session { return false; } if ( self::exists( $name ) ) { - Debug::info("Session::flash - Exists"); + Debug::log("Session::flash - Exists"); $session = self::get( $name ); self::delete( $name ); return $session; @@ -121,11 +121,11 @@ class Session { } if ( ! empty( $data ) ) { self::put( $name, $data ); - Debug::info("Session::flash - Session created."); + Debug::log("Session::flash - Session created."); return true; } if ( self::exists( $name ) ) { - Debug::info("Session::flash - Exists"); + Debug::log("Session::flash - Exists"); $session = self::get( $name ); self::delete( $name ); return $session;