* @link https://TheTempusProject.com/libraries/Bedrock * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Bedrock\Bin; use TheTempusProject\Canary\Bin\Canary as Debug; use TheTempusProject\Hermes\Functions\Route as Routes; use TheTempusProject\Bedrock\Functions\Input; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Bedrock\Functions\Token; use TheTempusProject\Bedrock\Functions\Sanitize; use TheTempusProject\Bedrock\Classes\Config; use TheTempusProject\Hermes\Classes\Autoloader; use TheTempusProject\Houdini\Classes\Template; use TheTempusProject\Houdini\Classes\Components; class Bedrock { public static $controllerName = ''; public static $methodName = ''; public static $activeConfig = []; protected $controllerObject = null; protected $controllerClass = ''; protected $params = []; protected $controllerError = false; /** * The constructor handles the entire process of parsing the url, * finding the controller/method, and calling the appropriate * class/function for the application. * * @param string $url - A custom URL to be parsed to determine * controller/method. (GET) url is used by * default if none is provided */ public function __construct( $url = '' ) { Debug::group( 'Bedrock Application' ); ob_start(); self::$activeConfig = new Config( CONFIG_JSON ); set_error_handler( [ BEDROCK_DEFAULT_ERROR_HANDLER, 'handle_error' ] ); set_exception_handler( [ BEDROCK_DEFAULT_EXCEPTION_HANDLER, 'handle_exception' ] ); self::$controllerName = DEFAULT_CONTROLLER_CLASS; self::$methodName = DEFAULT_CONTROLLER_METHOD; $this->setUrl( $url ); } public function setUrl( $url ) { if ( empty( $url ) ) { Debug::log( 'Using GET url.' ); $url = Input::get( 'url' ); } $trimmedUrl = trim( $url, '/' ); $url = str_ireplace( '.php', '', $trimmedUrl ); $urlArray = explode( '/', Sanitize::url( $url ) ); $this->setVarsFromUrlArray( $urlArray ); } protected function setVarsFromUrlArray( $urlArray ) { if ( !empty( $urlArray[0] ) ) { $urlPart = array_shift( $urlArray ); if ( $urlPart == 'admin' ) { $namespace = APP_SPACE . '\\Controllers\\Admin\\'; if ( empty( $urlArray[0] ) ) { // if there is no second param, assume they want the home controller $urlPart = 'home'; } else { $urlPart = array_shift( $urlArray ); // to drop the admin } if ( $urlPart == 'index' ) { // if its admin/index, again, assume the home controller $urlPart = 'home'; } } elseif ( $urlPart == 'api' ) { $namespace = APP_SPACE . '\\Controllers\\Api\\'; if ( empty( $urlArray[0] ) ) { // if there is no second param, assume they want the home controller $urlPart = 'home'; } else { $urlPart = array_shift( $urlArray ); // to drop the admin } if ( $urlPart == 'index' ) { // if its admin/index, again, assume the home controller $urlPart = 'home'; } } else { $namespace = APP_SPACE . '\\Controllers\\'; } $this->setController( $urlPart, $namespace ); } if ( !empty( $urlArray[0] ) ) { $urlPart = array_shift( $urlArray ); $this->setPage( $urlPart ); } if ( !empty( $urlArray ) ) { $this->params = array_values( $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' ) ?? 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' ) ); } protected function printDebug() { echo '
'; echo '

PHP Info

'; echo ''; echo ''; echo ''; echo ''; echo '
PHP version: ' . phpversion() . '
PDO Loaded version: ' . extension_loaded( 'pdo' ) . '
PHP extensions:
';
        foreach ( get_loaded_extensions() as $i => $ext ) {
            echo $ext . ' => ' . phpversion( $ext ) . '
'; } echo '

'; echo '

Tempus Core Info

'; echo ''; // Just in case echo ''; // Checks echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; // Routes echo ''; echo ''; echo ''; echo ''; echo ''; // Debugging echo ''; echo ''; echo ''; echo ''; echo ''; // Main echo ''; echo ''; echo ''; echo ''; echo ''; // Constants echo ''; // Debugging echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; // Tempus Debugger echo ''; echo ''; echo ''; // Tokens echo ''; echo ''; echo ''; // Cookies echo ''; echo ''; echo ''; // Directories echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; // other echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
_SERVER:
';
        echo var_export( $_SERVER, true );
        echo '

Checks

Uploads work?: ' . var_export( Check::uploads(), true ) . '
PHP: ' . var_export( Check::php(), true ) . '
Mail works?: ' . var_export( Check::mail(), true ) . '
Is safe mode?: ' . var_export( Check::safe(), true ) . '
Sessions work?: ' . var_export( Check::sessions(), true ) . '
Cookies work?: ' . var_export( Check::cookies(), true ) . '
is Apache?: ' . var_export( Check::isApache(), true ) . '
is Nginx?: ' . var_export( Check::isNginx(), true ) . '
Is token enabled?: ' . var_export( Token::isTokenEnabled(), true ) . '

Routes

Root: ' . var_export( Routes::getRoot(), true ) . '
Address: ' . var_export( Routes::getAddress(), true ) . '
Protocol: ' . var_export( Routes::getProtocol(), true ) . '
App URL: ' . var_export( self::getUrl(), true ) . '

Debugging

Console Debugging Enabled: ' . var_export( Debug::status( 'console' ), true ) . '
Debug Trace Enabled: ' . var_export( Debug::status( 'trace' ), true ) . '
Debugging Enabled: ' . var_export( Debug::status( 'debug' ), true ) . '
Rendering Enabled: ' . var_export( Debug::status( 'render' ), true ) . '

Main App Variables

Template Location: ' . var_export( Template::getLocation(), true ) . '
Configuration:
' . var_export( Config::$config, true ) . '
Check Errors:
' . var_export( Check::systemErrors(), true ) . '
GET:
' . var_export( $_GET, true ) . '

Constants

Debugging:
HERMES_REDIRECTS_ENABLED: ' . var_export( HERMES_REDIRECTS_ENABLED, true ) . '
CANARY_TRACE_ENABLED: ' . var_export( CANARY_TRACE_ENABLED, true ) . '
CANARY_ENABLED: ' . var_export( CANARY_ENABLED, true ) . '
CANARY_DEBUG_TO_CONSOLE: ' . var_export( CANARY_DEBUG_TO_CONSOLE, true ) . '
CANARY_DEBUG_TO_FILE: ' . var_export( CANARY_DEBUG_TO_FILE, true ) . '
Tempus Debugger:
CANARY_SECURE_HASH: ' . var_export( CANARY_SECURE_HASH, true ) . '
CANARY_SHOW_LINES: ' . var_export( CANARY_SHOW_LINES, true ) . '
Tokens:
DEFAULT_TOKEN_NAME: ' . var_export( DEFAULT_TOKEN_NAME, true ) . '
TOKEN_ENABLED: ' . var_export( TOKEN_ENABLED, true ) . '
Cookies:
DEFAULT_COOKIE_EXPIRATION: ' . var_export( DEFAULT_COOKIE_EXPIRATION, true ) . '
DEFAULT_COOKIE_PREFIX: ' . var_export( DEFAULT_COOKIE_PREFIX, true ) . '
Directories:
APP_ROOT_DIRECTORY: ' . var_export( APP_ROOT_DIRECTORY, true ) . '
BIN_DIRECTORY: ' . var_export( BIN_DIRECTORY, true ) . '
UPLOAD_DIRECTORY: ' . var_export( UPLOAD_DIRECTORY, true ) . '
IMAGE_UPLOAD_DIRECTORY: ' . var_export( IMAGE_UPLOAD_DIRECTORY, true ) . '
CLASSES_DIRECTORY: ' . var_export( CLASSES_DIRECTORY, true ) . '
CONFIG_DIRECTORY: ' . var_export( CONFIG_DIRECTORY, true ) . '
FUNCTIONS_DIRECTORY: ' . var_export( FUNCTIONS_DIRECTORY, true ) . '
TEMPLATE_DIRECTORY: ' . var_export( TEMPLATE_DIRECTORY, true ) . '
VIEW_DIRECTORY: ' . var_export( VIEW_DIRECTORY, true ) . '
ERRORS_DIRECTORY: ' . var_export( ERRORS_DIRECTORY, true ) . '
RESOURCES_DIRECTORY: ' . var_export( RESOURCES_DIRECTORY, true ) . '
BEDROCK_ROOT_DIRECTORY: ' . var_export( BEDROCK_ROOT_DIRECTORY, true ) . '
BEDROCK_BIN_DIRECTORY: ' . var_export( BEDROCK_BIN_DIRECTORY, true ) . '
BEDROCK_CLASSES_DIRECTORY: ' . var_export( BEDROCK_CLASSES_DIRECTORY, true ) . '
BEDROCK_CONFIG_DIRECTORY: ' . var_export( BEDROCK_CONFIG_DIRECTORY, true ) . '
BEDROCK_FUNCTIONS_DIRECTORY: ' . var_export( BEDROCK_FUNCTIONS_DIRECTORY, true ) . '
BEDROCK_RESOURCES_DIRECTORY: ' . var_export( BEDROCK_RESOURCES_DIRECTORY, true ) . '
BEDROCK_VIEW_DIRECTORY: ' . var_export( BEDROCK_VIEW_DIRECTORY, true ) . '
BEDROCK_ERRORS_DIRECTORY: ' . var_export( BEDROCK_ERRORS_DIRECTORY, true ) . '
Other:
MINIMUM_PHP_VERSION: ' . var_export( MINIMUM_PHP_VERSION, true ) . '
DATA_TITLE_PREG: ' . var_export( DATA_TITLE_PREG, true ) . '
PATH_PREG_REQS: ' . var_export( PATH_PREG_REQS, true ) . '
SIMPLE_NAME_PREG: ' . var_export( SIMPLE_NAME_PREG, true ) . '
ALLOWED_IMAGE_UPLOAD_EXTENTIONS: ' . var_export( ALLOWED_IMAGE_UPLOAD_EXTENTIONS, true ) . '
MAX_RESULTS_PER_PAGE: ' . var_export( MAX_RESULTS_PER_PAGE, true ) . '
DEFAULT_RESULTS_PER_PAGE: ' . var_export( DEFAULT_RESULTS_PER_PAGE, true ) . '
DEFAULT_SESSION_PREFIX: ' . var_export( DEFAULT_SESSION_PREFIX, true ) . '
DEFAULT_CONTROLLER_CLASS: ' . var_export( DEFAULT_CONTROLLER_CLASS, true ) . '
'; } }