diff --git a/system/database/autoload.php b/system/database/autoload.php index 5ba3855..856272f 100644 --- a/system/database/autoload.php +++ b/system/database/autoload.php @@ -8,7 +8,7 @@ namespace Phacil\Framework; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Interfaces\Databases as DatabaseInterface; /** * Principal class to load databases drivers @@ -16,12 +16,14 @@ use Phacil\Framework\Interfaces\Databases; * @package Phacil\Framework */ final class Database { /** + * Loaded class db driver * - * @var Databases + * @var DatabaseInterface */ private $driver; /** + * Prefix for query cache * * @var string */ @@ -42,7 +44,7 @@ final class Database { $driverClass = "\\Phacil\\Framework\\Databases\\".$driver; try { - $this->driver = new $driverClass($hostname, $username, $password, $database); + $this->createDriver(new $driverClass($hostname, $username, $password, $database)); } catch (Exception $th) { throw new Exception('Error: Could not load database file ' . $driver . '!'); //exit('Error: Could not load database file ' . $driver . '!'); @@ -50,6 +52,21 @@ final class Database { } + /** + * @param DatabaseInterface $driverObject + * @return never + * @throws Exception + */ + private function createDriver(DatabaseInterface $driverObject) + { + try { + $this->driver = $driverObject; + } catch (Exception $th) { + throw new Exception('Error: Could not load database file ' . $driver . '!'); + //exit('Error: Could not load database file ' . $driver . '!'); + } + } + /** * Check is connected on database * @return bool */ @@ -162,7 +179,7 @@ final class Database { * @throws PhpfastcacheInvalidArgumentException */ private function Cache($sql) { - if(class_exists('Caches')) { + if(class_exists('\Phacil\Framework\Caches')) { $cache = new Caches(); if (stripos($sql, "select") !== false) { diff --git a/system/encryption/autoload.php b/system/encryption/autoload.php new file mode 100644 index 0000000..2ea2739 --- /dev/null +++ b/system/encryption/autoload.php @@ -0,0 +1,155 @@ +key = $this->hash($key); + + if(function_exists('openssl_encrypt')) { + $this->method = 'openssl'; + $this->cipher = $opensslCipher; + } else { + $this->method = 'base64'; + } + + } + + /** + * @param string $value + * @return string|false + */ + public function hash($value) { + return hash('sha256', $value, true); + } + + /** + * + * @param mixed $value + * @param mixed|null $key + * @return string + */ + public function encrypt ($value, $key = NULL) { + $this->key = ($key != NULL) ? $key : $this->key; + + if($this->method == 'openssl') { + return $this->opensslEncrypt($value); + } else { + return $this->base64Encrypt($value); + } + } + + /** + * + * @param mixed $value + * @param mixed|null $key + * @return string|false + */ + public function decrypt ($value, $key = NULL) { + $this->key = ($key != NULL) ? $key : $this->key; + + if($this->method == 'openssl') { + return $this->opensslDecrypt($value); + } else { + return $this->base64Decrypt($value); + } + } + + /** + * @param string $value + * @return string + */ + function base64Encrypt($value) { + if (!$this->key) { + return $value; + } + + $output = ''; + + for ($i = 0; $i < strlen($value); $i++) { + $char = substr($value, $i, 1); + $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1); + $char = chr(ord($char) + ord($keychar)); + + $output .= $char; + } + + return base64_encode($output); + } + + /** + * @param string $value + * @return string|false + */ + function base64Decrypt($value) { + if (!$this->key) { + return $value; + } + + $output = ''; + + $value = base64_decode($value); + + for ($i = 0; $i < strlen($value); $i++) { + $char = substr($value, $i, 1); + $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1); + $char = chr(ord($char) - ord($keychar)); + + $output .= $char; + } + + return $output; + } + + /** + * @param string $value + * @return string + */ + private function opensslEncrypt ($value) { + + $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipher)); + + $ciphertext_raw = strtr(base64_encode(openssl_encrypt($value, $this->cipher, $this->hash( $this->key), 0, $iv)), '+/=', '-_,'); + + //$hmac = hash_hmac('sha256', $ciphertext_raw, $this->key, true); + + $output = strtr($this->base64Encrypt( $iv.$ciphertext_raw ), '+/=', '-_,'); + + return $output; + + } + + /** + * @param string $value + * @return string|false + */ + private function opensslDecrypt ($value) { + + $c = $this->base64Decrypt(strtr($value, '-_,', '+/=')); + $ivlen = openssl_cipher_iv_length($this->cipher); + $iv = substr($c, 0, $ivlen); + //$hmac = substr($c, $ivlen, $sha2len=32); + $ciphertext_raw = substr($c, $ivlen); + + + $output = trim(openssl_decrypt(base64_decode(strtr($ciphertext_raw, '-_,', '+/=')), $this->cipher, $this->hash($this->key), 0, $iv)); + + return $output; + } +} diff --git a/system/engine/registry.php b/system/engine/registry.php index fe6365c..1edd7b0 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -10,8 +10,16 @@ /** @package Phacil\Framework */ final class Registry { + /** + * data Objects + * @var array + */ private $data = array(); + /** + * Original route for childs + * @var string + */ public $routeOrig; /** @@ -19,7 +27,8 @@ final class Registry { * @return mixed */ public function get($key) { - return (isset($this->data[$key]) ? $this->data[$key] : NULL); + + return (isset($this->$key) ? $this->$key : $this->engine->checkRegistry($key)); } /** @@ -28,7 +37,7 @@ final class Registry { * @return void */ public function set($key, $value) { - $this->data[$key] = $value; + $this->$key = $value; } /** @@ -36,6 +45,20 @@ final class Registry { * @return bool */ public function has($key) { - return isset($this->data[$key]); + return isset($this->$key); } + + /** + * UnSet + * + * Unsets registry value by key. + * + * @param string $key + * @return void + */ + public function destroy(string $key) { + if (isset($this->$key)) { + unset($this->$key); + } + } } diff --git a/system/library/encryption.php b/system/library/encryption.php index 2ea2739..1aa46c4 100644 --- a/system/library/encryption.php +++ b/system/library/encryption.php @@ -6,150 +6,5 @@ * Phacil PHP Framework - https://github.com/exacti/phacil-framework */ -namespace Phacil\Framework; -/** @package Phacil\Framework */ -final class Encryption { - private $key; - private $method; - private $cipher; - - /** - * @param string $key - * @param string $opensslCipher - * @return void - */ - function __construct($key, $opensslCipher = 'aes-128-cbc') { - $this->key = $this->hash($key); - - if(function_exists('openssl_encrypt')) { - $this->method = 'openssl'; - $this->cipher = $opensslCipher; - } else { - $this->method = 'base64'; - } - - } - - /** - * @param string $value - * @return string|false - */ - public function hash($value) { - return hash('sha256', $value, true); - } - - /** - * - * @param mixed $value - * @param mixed|null $key - * @return string - */ - public function encrypt ($value, $key = NULL) { - $this->key = ($key != NULL) ? $key : $this->key; - - if($this->method == 'openssl') { - return $this->opensslEncrypt($value); - } else { - return $this->base64Encrypt($value); - } - } - - /** - * - * @param mixed $value - * @param mixed|null $key - * @return string|false - */ - public function decrypt ($value, $key = NULL) { - $this->key = ($key != NULL) ? $key : $this->key; - - if($this->method == 'openssl') { - return $this->opensslDecrypt($value); - } else { - return $this->base64Decrypt($value); - } - } - - /** - * @param string $value - * @return string - */ - function base64Encrypt($value) { - if (!$this->key) { - return $value; - } - - $output = ''; - - for ($i = 0; $i < strlen($value); $i++) { - $char = substr($value, $i, 1); - $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1); - $char = chr(ord($char) + ord($keychar)); - - $output .= $char; - } - - return base64_encode($output); - } - - /** - * @param string $value - * @return string|false - */ - function base64Decrypt($value) { - if (!$this->key) { - return $value; - } - - $output = ''; - - $value = base64_decode($value); - - for ($i = 0; $i < strlen($value); $i++) { - $char = substr($value, $i, 1); - $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1); - $char = chr(ord($char) - ord($keychar)); - - $output .= $char; - } - - return $output; - } - - /** - * @param string $value - * @return string - */ - private function opensslEncrypt ($value) { - - $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipher)); - - $ciphertext_raw = strtr(base64_encode(openssl_encrypt($value, $this->cipher, $this->hash( $this->key), 0, $iv)), '+/=', '-_,'); - - //$hmac = hash_hmac('sha256', $ciphertext_raw, $this->key, true); - - $output = strtr($this->base64Encrypt( $iv.$ciphertext_raw ), '+/=', '-_,'); - - return $output; - - } - - /** - * @param string $value - * @return string|false - */ - private function opensslDecrypt ($value) { - - $c = $this->base64Decrypt(strtr($value, '-_,', '+/=')); - $ivlen = openssl_cipher_iv_length($this->cipher); - $iv = substr($c, 0, $ivlen); - //$hmac = substr($c, $ivlen, $sha2len=32); - $ciphertext_raw = substr($c, $ivlen); - - - $output = trim(openssl_decrypt(base64_decode(strtr($ciphertext_raw, '-_,', '+/=')), $this->cipher, $this->hash($this->key), 0, $iv)); - - return $output; - } -} +class_alias('\Phacil\Framework\Encryption', 'Encryption'); \ No newline at end of file diff --git a/system/system.php b/system/system.php index 2340e13..b5540de 100644 --- a/system/system.php +++ b/system/system.php @@ -15,7 +15,7 @@ use TypeError; * * @package Phacil\Framework */ -class startEngineExacTI { +final class startEngineExacTI { /** * @@ -92,7 +92,7 @@ class startEngineExacTI { /** @return string|false */ private function checkPHPversion() { - if (version_compare(phpversion(), '5.4.0', '>') == false) { + if (version_compare(phpversion(), '5.4.40', '>') == false) { exit('PHP 5.4+ Required'); } else { return phpversion(); @@ -227,6 +227,29 @@ class startEngineExacTI { return (isset($this->preActions) && is_array($this->preActions)) ? $this->preActions : []; } + public function checkRegistry($key){ + //mail + if(!isset($this->registry->$key) && $key == 'mail'){ + $this->mail = new Mail(); + $this->mail->protocol = $this->config->get('config_mail_protocol'); + if($this->config->get('config_mail_protocol') == 'smtp'){ + $this->mail->parameter = $this->config->get('config_mail_parameter'); + $this->mail->hostname = $this->config->get('config_smtp_host'); + $this->mail->username = $this->config->get('config_smtp_username'); + $this->mail->password = $this->config->get('config_smtp_password'); + $this->mail->port = $this->config->get('config_smtp_port'); + $this->mail->timeout = $this->config->get('config_smtp_timeout'); + } + } + + // Translate + if(!isset($this->registry->$key) && $key == 'translate'){ + $this->translate = new Translate(); + } + + return (isset($this->registry->$key)) ? $this->registry->$key : NULL; + } + } global $engine; @@ -236,27 +259,25 @@ $engine = new startEngineExacTI(); // Registry /** @var \Phacil\Framework\startEngineExacTI $engine */ -$engine->registry->set('engine', $engine); +$engine->engine = $engine; // Loader /** * @var Loader */ -$loader = new Loader($engine->registry); -$engine->registry->set('load', $loader); +$engine->load = new Loader($engine->registry); // Config /** @var Config */ -$config = new Config(); -$engine->registry->set('config', $config); +$engine->config = new Config(); if(defined('DB_DRIVER')) - $engine->registry->set('db', new Database(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE)); + $engine->db = new Database(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); // Settings if(!empty($configs)){ foreach ($configs as $key => $confValue) { - $config->set($key, $confValue); + $engine->config->set($key, $confValue); } } @@ -266,43 +287,44 @@ if(USE_DB_CONFIG === true) { foreach ($query->rows as $setting) { if (!$setting['serialized']) { - $config->set($setting['key'], $setting['value']); + $engine->config->set($setting['key'], $setting['value']); } else { - $config->set($setting['key'], unserialize($setting['value'])); + $engine->config->set($setting['key'], unserialize($setting['value'])); } } } -$config->set('config_url', HTTP_URL); -$config->set('config_ssl', HTTPS_URL); +$engine->config->set('config_url', HTTP_URL); +$engine->config->set('config_ssl', HTTPS_URL); //timezone -if($config->get('date_timezone')){ - $engine->setTimezone($config->get('date_timezone')); +if($engine->config->get('date_timezone')){ + $engine->setTimezone($engine->config->get('date_timezone')); } // Site Title -if($config->get('PatternSiteTitle') == true) { - define('PATTERSITETITLE', $config->get('PatternSiteTitle')); +if($engine->config->get('PatternSiteTitle') == true) { + define('PATTERSITETITLE', $engine->config->get('PatternSiteTitle')); } else { define('PATTERSITETITLE', false); } // Url -$url = new Url($config->get('config_url'), $config->get('config_use_ssl') ? $config->get('config_ssl') : $config->get('config_url')); -$engine->registry->set('url', $url); +$engine->url = new Url($engine->config->get('config_url'), $engine->config->get('config_use_ssl') ? $engine->config->get('config_ssl') : $engine->config->get('config_url')); // Log -if(!$config->get('config_error_filename')){ - $config->set('config_error_filename', 'error.log'); +if(!$engine->config->get('config_error_filename')){ + $engine->config->set('config_error_filename', 'error.log'); } -$log = new Log($config->get('config_error_filename')); -$engine->registry->set('log', $log); +/** + * @var Log + */ +$engine->log = new Log($engine->config->get('config_error_filename')); // Error Handler -set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($log, $config, $engine){ +set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($engine){ switch ($errno) { case E_NOTICE: @@ -326,94 +348,82 @@ set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($log, $con break; } - if ($config->get('config_error_display')) { + if ($engine->config->get('config_error_display')) { echo '' . $error . ': ' . $errstr . ' in ' . $errfile . ' on line ' . $errline . ''; } - if ($config->get('config_error_log')) { - $log->write( $error . ': ' . $errstr . ' in ' . $errfile . ' on line ' . $errline.' | Phacil '.$engine->version(). ' on PHP '.$engine->phpversion); + if ($engine->config->get('config_error_log')) { + $engine->log->write( $error . ': ' . $errstr . ' in ' . $errfile . ' on line ' . $errline.' | Phacil '.$engine->version(). ' on PHP '.$engine->phpversion); } return true; }); -set_exception_handler(function($e) use ($log, $config) { - if ($config->get('config_error_display')) { +set_exception_handler(function($e) use ($engine) { + if ($engine->config->get('config_error_display')) { echo '' . get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . ''; } - if ($config->get('config_error_log')) { - $log->write(get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine()); + if ($engine->config->get('config_error_log')) { + $engine->log->write(get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine()); } }); -//Caches -$caches = new Caches(); -$engine->registry->set('cache', $caches); +/** + * Caches + * @var Caches + */ +$engine->cache = new Caches(); -// Request -$request = new Request(); -$engine->registry->set('request', $request); +/** + * Request + * @var Request + */ +$engine->request = new Request(); // Response -$response = new Response(); +/* $response = new Response(); $response->addHeader('Content-Type: text/html; charset=utf-8'); -$response->setCompression($config->get('config_compression')); -$engine->registry->set('response', $response); +$response->setCompression($engine->config->get('config_compression')); */ +$engine->response = new Response(); + +if($engine->config->get('config_compression')) + $engine->response->setCompression($engine->config->get('config_compression')); // Session -$session = new Session(); -$engine->registry->set('session', $session); - -// Translate -$translate = new Translate(); -$engine->registry->set('translate', $translate); - -// E-Mail Config -$mail = new Mail(); -$mail->protocol = $config->get('config_mail_protocol'); -if($config->get('config_mail_protocol') == 'smtp'){ - $mail->parameter = $config->get('config_mail_parameter'); - $mail->hostname = $config->get('config_smtp_host'); - $mail->username = $config->get('config_smtp_username'); - $mail->password = $config->get('config_smtp_password'); - $mail->port = $config->get('config_smtp_port'); - $mail->timeout = $config->get('config_smtp_timeout'); -} -$engine->registry->set('mail', $mail); +$engine->session = new Session(); // Document -$document = new Document(); -$engine->registry->set('document', $document); +$engine->document = new Document(); // Custom registrations $engine->extraRegistrations(); // Front Controller -$controller = new Front($engine->registry); +$frontController = new Front($engine->registry); // SEO URL's -$controller->addPreAction(new ActionSystem((string) 'url/seo_url')); +$frontController->addPreAction(new ActionSystem((string) 'url/seo_url')); //extraPreactions if($engine->controllerPreActions()){ foreach ($engine->controllerPreActions() as $action){ - $controller->addPreAction(new Action($action)); + $frontController->addPreAction(new Action($action)); } } // Router -if (isset($request->get['route'])) { - $action = new Action($request->get['route']); +if (isset($engine->request->get['route'])) { + $action = new Action($engine->request->get['route']); } else { $default = (defined('DEFAULT_ROUTE')) ? DEFAULT_ROUTE : 'common/home'; - $request->get['route'] = $default; + $engine->request->get['route'] = $default; $action = new Action($default); } // Dispatch $not_found = (defined('NOT_FOUND')) ? NOT_FOUND : 'error/not_found'; -$controller->dispatch($action, ($not_found)); +$frontController->dispatch($action, ($not_found)); // Output -$response->output(); +$engine->response->output(); diff --git a/system/token/autoload.php b/system/token/autoload.php index ad5fb84..c9937f2 100644 --- a/system/token/autoload.php +++ b/system/token/autoload.php @@ -1,31 +1,49 @@