diff --git a/.gitignore b/.gitignore index 4956d3b..ec2815e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor/ /cache/ +/system/logs/ /app/extensions/ composer.lock diff --git a/README.md b/README.md index 8581e1d..493eae1 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@

Ridiculously small PHP framework.

- +

Wolff is a ridiculously small and lightweight PHP framework with useful functions and utilities like a template, route, extensions and language system. diff --git a/composer.json b/composer.json index 31515d4..cc728b6 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "mvc" ], "type": "project", - "version": "0.9.9.3", + "version": "0.9.9.4", "license": "MIT", "support": { "issues": "https://github.com/Usbac/Wolff/issues", diff --git a/system/cli/Create.php b/system/cli/Create.php index 02ade34..fcbbb03 100644 --- a/system/cli/Create.php +++ b/system/cli/Create.php @@ -3,6 +3,7 @@ namespace Cli; use Core\{Extension, Maintenance}; +use Utilities\Str; class Create { @@ -92,10 +93,13 @@ private function controller() $file = fopen($file_dir, 'w') or die("WARNING: Cannot create Controller file \n"); + $values = [ + 'namespace' => $namespace, + 'classname' => $file_name + ]; + $content = file_get_contents(self::TEMPLATE_PATH . 'controller.txt'); - $original = array('{namespace}', '{classname}'); - $replacement = array($namespace, $file_name); - $content = str_replace($original, $replacement, $content); + $content = Str::interpolate($content, $values); fwrite($file, $content); fclose($file); @@ -130,7 +134,7 @@ private function view() $file = fopen($file_dir, 'w') or die("WARNING: Cannot create view file \n"); $content = file_get_contents(self::TEMPLATE_PATH . 'view.txt'); - $content = str_replace('{title}', $this->argv[3], $content); + $content = Str::interpolate($content, ['title' => $this->argv[3]]); fwrite($file, $content); fclose($file); @@ -148,7 +152,7 @@ private function extension() } $file_dir = getExtensionDirectory() . $this->argv[3] . '.php'; - Extension::makeFolder(); + Extension::mkdir(); if (file_exists($file_dir)) { echo "\e[1;31m WARNING: extension " . $this->argv[3] . " already exists!\e[0m \n"; @@ -165,11 +169,16 @@ private function extension() $version = readline("Version -> "); $author = readline("Author -> "); - $original = array('{classname}', '{name}', '{description}', '{version}', '{author}'); - $replacement = array($this->argv[3], $name, $description, $version, $author); + $values = [ + 'classname' => $this->argv[3], + 'name' => $name, + 'description' => $description, + 'version' => $version, + 'author' => $author, + ]; $content = file_get_contents(self::TEMPLATE_PATH . 'extension.txt'); - $content = str_replace($original, $replacement, $content); + $content = Str::interpolate($content, $values); fwrite($file, $content); fclose($file); diff --git a/system/config.php b/system/config.php index fcd8a6b..1f7472d 100644 --- a/system/config.php +++ b/system/config.php @@ -11,13 +11,14 @@ //Directories define('WOLFF_ROOT_DIR', dirname(__DIR__) . '/'); +define('WOLFF_SYS_DIR', WOLFF_ROOT_DIR . 'system/'); define('WOLFF_APP_DIR', WOLFF_ROOT_DIR . 'app/'); define('WOLFF_PUBLIC_DIR', WOLFF_ROOT_DIR . 'public/'); define('WOLFF_EXTENSION_DIR', WOLFF_APP_DIR . 'extensions/'); define('WOLFF_CACHE_DIR', WOLFF_ROOT_DIR . 'cache/'); //General -define('WOLFF_VERSION', '0.9.9.3'); +define('WOLFF_VERSION', '0.9.9.4'); define('WOLFF_START', microtime(true)); define('WOLFF_PAGE_TITLE', 'Wolff'); define('WOLFF_MAIN_PAGE', 'main_page'); diff --git a/system/core/Cache.php b/system/core/Cache.php index b680dc3..8b4a93d 100644 --- a/system/core/Cache.php +++ b/system/core/Cache.php @@ -39,19 +39,32 @@ public static function isEnabled() } + /** + * Return the specified cache file path + * + * @param string $dir the cache filename + * + * @return string return the specified cache file path + */ + public static function get(string $dir) + { + return getCacheDirectory() . self::getFilename($dir); + } + + /** * Create the cache file if doesn't exists and return its path * - * @param string $dir the view directory + * @param string $dir the cache filename * @param string $content the original file content * * @return string the cache file path */ - public static function get(string $dir, string $content) + public static function set(string $dir, string $content) { - $file_path = getCacheDirectory() . self::getFilename($dir); + $file_path = self::get($dir); - self::createFolder(); + self::mkdir(); if (!file_exists($file_path)) { $file = fopen($file_path, 'w'); @@ -83,7 +96,7 @@ public static function expired($dir) /** * Create the cache folder if it doesn't exists */ - public static function createFolder() + public static function mkdir() { $folder_path = getCacheDirectory(); if (!file_exists($folder_path)) { @@ -99,40 +112,43 @@ public static function createFolder() * * @return bool true if the cache exists, false otherwise */ - public static function exists(string $dir = '') + public static function has(string $dir) { - $folder_path = getCacheDirectory(); - - if (!empty($dir)) { - $file_path = $folder_path . self::getFilename($dir); - - return is_file($file_path); - } + $file_path = getCacheDirectory() . self::getFilename($dir); - return !empty(glob($folder_path . '*')); + return is_file($file_path); } /** - * Delete all the cache files or the specified one + * Delete the specified cache file * * @param string $dir the cache to delete, if its empty all the cache will be deleted + * + * @return bool true if the item was successfully removed, false otherwise */ - public static function clear(string $dir = '') + public static function delete(string $dir) { - $folder_path = getCacheDirectory(); + $file_path = getCacheDirectory() . self::getFilename($dir); - if (empty($dir)) { - deleteFilesInDir($folder_path); + if (is_file($file_path)) { + unlink($file_path); - return; + return true; } - $file_path = $folder_path . self::getFilename($dir); + return false; + } - if (is_file($file_path)) { - unlink($file_path); - } + + /** + * Delete all the cache files + * + * @return bool true if the item was successfully removed, false otherwise + */ + public static function clear() + { + return deleteFilesInDir(getCacheDirectory()); } diff --git a/system/core/DB.php b/system/core/DB.php index 5b12ae7..1174f9e 100644 --- a/system/core/DB.php +++ b/system/core/DB.php @@ -53,7 +53,7 @@ public function __construct() self::$connection = new PDO(strtolower(getDBMS()) . ':host=' . getServer() . '; dbname=' . getDB() . '', getDBUser(), getDBPass(), array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); } catch (PDOException $e) { - error_log($e->getMessage()); + Log::critical($e->getMessage()); } } diff --git a/system/core/Extension.php b/system/core/Extension.php index 7966c21..423ddc3 100644 --- a/system/core/Extension.php +++ b/system/core/Extension.php @@ -35,7 +35,7 @@ public static function load(string $type, $loader = null) return false; } - self::makeFolder(); + self::mkdir(); foreach (self::$extensions as $extension) { if ($extension['type'] !== $type || !self::matchesRoute($extension['route'])) { @@ -125,7 +125,7 @@ public static function folderExists() /** * Make the extension folder directory if it doesn't exists */ - public static function makeFolder() + public static function mkdir() { if (!self::folderExists()) { mkdir(getExtensionDirectory()); diff --git a/system/core/Factory.php b/system/core/Factory.php index ee96499..8d8f46e 100644 --- a/system/core/Factory.php +++ b/system/core/Factory.php @@ -28,7 +28,7 @@ public static function controller(string $dir = null) $class = self::NAMESPACE_CONTROLLER . Str::pathToNamespace($dir); if (!class_exists($class)) { - error_log("Warning: The controller class '" . $class . "' doesn't exists"); + Log::error("The controller class '$class' doesn't exists"); return false; } @@ -49,7 +49,7 @@ public static function extension(string $name) $class = self::NAMESPACE_EXTENSION . $name; if (!class_exists($class)) { - error_log("Warning: The Extension class '" . $class . "' doesn't exists"); + Log::error("The extension class '$class' doesn't exists"); return false; } diff --git a/system/core/Loader.php b/system/core/Loader.php index af91074..f3a3401 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -69,6 +69,7 @@ public function getUpload() */ public function controller(string $dir) { + $controller_path = $dir; $dir = Str::sanitizePath($dir); //load controller default function and return it @@ -104,6 +105,8 @@ public function controller(string $dir) return $controller; } + Log::error("Controller '$controller_path' doesn't exists"); + return false; } @@ -152,10 +155,14 @@ public function language(string $dir, string $language = WOLFF_LANGUAGE) if (file_exists($file_path)) { include_once($file_path); + } else { + Log::warning("The $language language for '$dir' doesn't exists"); + + return false; } if (!isset($data)) { - error_log("Warning: The " . $language . " language for '" . $dir . "' doesn't exists"); + Log::warning("The $language language content for '$dir' is empty"); return false; } @@ -174,6 +181,14 @@ public function language(string $dir, string $language = WOLFF_LANGUAGE) public function view(string $dir, array $data = [], bool $cache = true) { $dir = Str::sanitizePath($dir); + $file_path = getAppDirectory() . 'views/' . $dir; + + if (!file_exists($file_path . '.php') && !file_exists($file_path . '.html')) { + Log::error("View '$dir' doesn't exists"); + + return; + } + $this->template->get($dir, $data, $cache); } @@ -189,6 +204,13 @@ public function view(string $dir, array $data = [], bool $cache = true) public function getView(string $dir, array $data = []) { $dir = Str::sanitizePath($dir); + $file_path = getAppDirectory() . 'views/' . $dir; + + if (!file_exists($file_path . '.php') && !file_exists($file_path . '.html')) { + Log::error("View '$dir' doesn't exists"); + + return; + } return $this->template->getView($dir, $data); } diff --git a/system/core/Log.php b/system/core/Log.php new file mode 100644 index 0000000..b15f7fc --- /dev/null +++ b/system/core/Log.php @@ -0,0 +1,155 @@ + date('m-d-Y H:i:s'), + 'ip' => getClientIP(), + 'level' => $level, + 'message' => $message + ]; + + $data = Str::interpolate(self::FORMAT, $values); + self::writeToFile($data); + } + + + /** + * Write content to the current log file + * + * @param string $data the content to append + */ + private static function writeToFile(string $data) + { + self::mkdir(); + $filename = getSystemDirectory() . self::FOLDER_NAME . '/' . date("m-d-Y") . '.log'; + file_put_contents($filename, $data . "\n", FILE_APPEND); + } + + + /** + * Create the logs folder if it doesn't exists + */ + private static function mkdir() + { + $folder_path = getSystemDirectory() . self::FOLDER_NAME; + + if (!file_exists($folder_path)) { + mkdir($folder_path, 0755, true); + } + } +} diff --git a/system/core/Maintenance.php b/system/core/Maintenance.php index 3c1028e..202dfdb 100644 --- a/system/core/Maintenance.php +++ b/system/core/Maintenance.php @@ -15,7 +15,7 @@ class Maintenance private static $file = WOLFF_ROOT_DIR . 'system/definitions/maintenance_whitelist.txt'; - const READ_ERROR = "Warning: Couldn't read the maintenance whitelist file"; + const READ_ERROR = "Couldn't read the maintenance whitelist file"; /** @@ -38,7 +38,7 @@ public static function getAllowedIPs() } if (!$content = file_get_contents(self::$file)) { - error_log(self::READ_ERROR); + Log::warning(self::READ_ERROR); return false; } @@ -68,7 +68,7 @@ public static function addAllowedIP(string $ip) return true; } - error_log(self::READ_ERROR); + Log::warning(self::READ_ERROR); return false; } @@ -99,7 +99,7 @@ public static function removeAllowedIP(string $ip) self::createFile(); if (!$content = file_get_contents(self::$file)) { - error_log(self::READ_ERROR); + Log::warning(self::READ_ERROR); return false; } diff --git a/system/core/Template.php b/system/core/Template.php index 4ebf412..121046c 100644 --- a/system/core/Template.php +++ b/system/core/Template.php @@ -54,11 +54,17 @@ public function get(string $dir, array $data, bool $cache) unset($data); } - $content = $this->replaceAll($this->getContent($dir)); + $content = $this->getContent($dir); + + if ($content === false) { + return false; + } + + $content = $this->replaceAll($content); //Cache system if ($cache && Cache::isEnabled()) { - include(Cache::get($dir, $content)); + include(Cache::set($dir, $content)); } else { $temp = tmpfile(); fwrite($temp, $content); @@ -76,7 +82,7 @@ public function get(string $dir, array $data, bool $cache) * @param string $dir the view directory * @param array $data the data array present in the view * - * @return string the view content + * @return string|bool the view content or false if it doesn't exists */ public function getView(string $dir, array $data) { @@ -86,7 +92,13 @@ public function getView(string $dir, array $data) unset($data); } - return $this->replaceAll($this->getContent($dir)); + $content = $this->getContent($dir); + + if ($content === false) { + return false; + } + + return $this->replaceAll($content); } @@ -95,7 +107,7 @@ public function getView(string $dir, array $data) * * @param string $dir the view directory * - * @return string the view content + * @return string|bool the view content or false if it doesn't exists */ private function getContent($dir) { @@ -106,9 +118,9 @@ private function getContent($dir) } elseif (file_exists($file_path . '.html')) { return file_get_contents($file_path . '.html'); } else { - error_log("Error: View '" . $dir . "' doesn't exists"); + Log::error("View '" . $dir . "' doesn't exists"); - return null; + return false; } } diff --git a/system/stdlib/config.php b/system/stdlib/config.php index 6a27370..50719be 100644 --- a/system/stdlib/config.php +++ b/system/stdlib/config.php @@ -71,6 +71,16 @@ function getDirectory() } + /** + * Returns the system directory of the project + * @return string the system directory of the project + */ + function getSystemDirectory() + { + return WOLFF_SYS_DIR; + } + + /** * Returns the app directory of the project * @return string the app directory of the project diff --git a/system/utilities/Upload.php b/system/utilities/Upload.php index 5871288..33d8b72 100644 --- a/system/utilities/Upload.php +++ b/system/utilities/Upload.php @@ -2,6 +2,8 @@ namespace Utilities; +use Core\Log; + class Upload { @@ -110,14 +112,14 @@ public function file(string $filename) $file = $_FILES[$filename]; if ($this->maxSize > 0 && $file['size'] > $this->maxSize) { - error_log("Error: file '" . $file['name'] . "' exceeds maximum upload size"); + Log::notice("File '" . $file['name'] . "' exceeds maximum upload size"); return false; } $dir = getPublicDirectory() . $this->directory; if (!move_uploaded_file($file['tmp_name'], $dir . '/' . $file['name'])) { - error_log("Error: Upload of '" . $file['name'] . "' failed"); + Log::notice("Upload of '" . $file['name'] . "' failed"); return false; } @@ -130,7 +132,7 @@ public function file(string $filename) 'size' => $file['size'], 'directory' => $dir, 'uploader_ip' => $_SERVER['REMOTE_ADDR'], - 'date' => date('Y-m-d H:i:s'), + 'date' => date('Y-m-d H:i:s') ); return true;