diff --git a/.env b/.env index dde155b..3802b23 100644 --- a/.env +++ b/.env @@ -1,5 +1,4 @@ #DATABASE CONFIGURATION - DB_HOST=127.0.0.1 DB_NAME=wepesi DB_USER=root @@ -9,15 +8,15 @@ DB_PORT=3306 #EMAIL CONFIGURATION SERVER_HOST= -# EMAIL INFOS +#INFOS EMAIL INFO_EMAIL= INFO_PASSWORD= -#contact +#CONTACT EMAIL CONTACT_EMAIL= CONTACT_PASSWORD= -#contact +#NO-REPLY EMAIL NOREPLY_EMAIL= NOREPLY_PASSWORD= @@ -26,4 +25,6 @@ APP_ENV=dev #APP DEFAULT LANG LANG=fr + +#APP TIME ZONE TIME_ZONE=Africa/Kigali \ No newline at end of file diff --git a/cache/index_folder.txt b/cache/index_folder.txt deleted file mode 100644 index 87bb235..0000000 --- a/cache/index_folder.txt +++ /dev/null @@ -1 +0,0 @@ -["C:\/wamp\/www\/wepesi","C:\/wamp\/www\/wepesi\/assets","C:\/wamp\/www\/wepesi\/assets\/css","C:\/wamp\/www\/wepesi\/assets\/css\/font","C:\/wamp\/www\/wepesi\/assets\/css\/font-awesome-4.7.0","C:\/wamp\/www\/wepesi\/assets\/css\/font-awesome-4.7.0\/css","C:\/wamp\/www\/wepesi\/assets\/css\/font-awesome-4.7.0\/fonts","C:\/wamp\/www\/wepesi\/assets\/icon","C:\/wamp\/www\/wepesi\/assets\/img","C:\/wamp\/www\/wepesi\/assets\/js","C:\/wamp\/www\/wepesi\/cache","C:\/wamp\/www\/wepesi\/config","C:\/wamp\/www\/wepesi\/controller","C:\/wamp\/www\/wepesi\/helpers","C:\/wamp\/www\/wepesi\/lang","C:\/wamp\/www\/wepesi\/lang\/en","C:\/wamp\/www\/wepesi\/lang\/fr","C:\/wamp\/www\/wepesi\/middleware","C:\/wamp\/www\/wepesi\/middleware\/Validation","C:\/wamp\/www\/wepesi\/models","C:\/wamp\/www\/wepesi\/route","C:\/wamp\/www\/wepesi\/src","C:\/wamp\/www\/wepesi\/src\/Core","C:\/wamp\/www\/wepesi\/src\/Core\/Event","C:\/wamp\/www\/wepesi\/src\/Core\/Orm","C:\/wamp\/www\/wepesi\/src\/Core\/Routing","C:\/wamp\/www\/wepesi\/src\/Core\/Validation","C:\/wamp\/www\/wepesi\/views"] \ No newline at end of file diff --git a/config/autoload.php b/config/autoload.php index 69a9978..23ce0c6 100644 --- a/config/autoload.php +++ b/config/autoload.php @@ -14,10 +14,7 @@ if (isset($config['autoload'])) { $autoload = is_string($config['autoload']) ? [$config['autoload']] : $config['autoload']; } -// check project is still on development. -if (!APP_DEV) { - autoIndexFolder(); -} + // builtin autoload spl_autoload_register(/** * @param $class diff --git a/config/constant.php b/config/constant.php index 07f1a72..1a5d42c 100644 --- a/config/constant.php +++ b/config/constant.php @@ -9,22 +9,58 @@ * they declare as global tho to be accessible from anywhere in the project */ -//web root configuration +/** + * web app file path + */ define('WEB_ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_NAME'])); +/** + * os system absolute file path + */ define('ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME'])); /** + * Get App domain * define default domain */ -$server_name = $_SERVER['SERVER_NAME'] ?? 'wepesi.com'; -$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? strtolower(explode('/', $_SERVER['SERVER_PROTOCOL'])[0]) : 'http'; -$domain = $_SERVER['REMOTE_ADDR'] == '::1' ? "$protocol://$server_name" : $server_name; -define('DEFAULT_DOMAIN', "$protocol://$server_name"); -define('APP_DOMAIN', $domain); +/** + * Get host domain ip address + * @return string + */ +function getDomainIP(): string +{ + $ip = $_SERVER['REMOTE_ADDR']; -// default timezone -const TIMEZONE = 'Africa/Kigali'; + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { + $ip = $_SERVER['HTTP_CLIENT_IP']; + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + } elseif ($ip == '::1') { + $ip = gethostbyname(getHostName()); + } + return $ip; +} -// define in witch cycle are you are working on. -// in case you are in dev, indexing file will not be generated, but in prod fase, it will be generated -define('APP_DEV', true); \ No newline at end of file +/** + * Get server information's + * @return object + */ +function serverDomain(): object +{ + $server_name = $_SERVER['SERVER_NAME']; + $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? strtolower(explode('/', $_SERVER['SERVER_PROTOCOL'])[0]) : 'http'; + $domain = getDomainIp() === '127.0.0.1' ? "$protocol://$server_name" : $server_name; + return (object)[ + 'server_name' => $server_name, + 'protocol' => $protocol, + 'domain' => $domain + ]; +} + +/** + * Define default domain + */ +define('DEFAULT_DOMAIN', serverDomain()->protocol . "://" . serverDomain()->server_name); +/** + * Define Application host domain + */ +define('APP_DOMAIN', serverDomain()->domain); diff --git a/config/function.php b/config/function.php index 29bf15e..b3b03db 100644 --- a/config/function.php +++ b/config/function.php @@ -43,24 +43,20 @@ function checkFileExtension($fileName) function autoIndexFolder(array $exclude_folder = []) { $app_root = appDirSeparator(dirname(__DIR__)); - // check if cache directory exists before processing - $cash_file_dir = appDirSeparator($app_root . '/cache'); - if (!file_exists($cash_file_dir)) { - mkdir($cash_file_dir, 0777, true); - } - // define exclude folder to not be affected by the situation. + + // define folder to be excluded to not be affected by the process. $exclude = ['vendor', 'test']; if (count($exclude_folder)) $exclude = array_merge($exclude, $exclude_folder); $implode = implode('|', $exclude); $folder_struct = getSubDirectories($app_root); + $filter = array_filter($folder_struct, function ($folder_name) use ($implode) { $pattern = "/$implode/i"; if (!preg_match($pattern, strtolower(trim($folder_name)))) { return $folder_name; } }); - - if (!checkCacheContent($cash_file_dir, $filter)) { + if (!checkCacheContent($filter,$app_root)) { foreach ($filter as $subFolder) { if (!is_file($subFolder . '/index.php')) { copy(__DIR__ . '/index.php', $subFolder . '/index.php'); @@ -70,29 +66,31 @@ function autoIndexFolder(array $exclude_folder = []) } /** - * @param string $cash_file_dir + * check content from cache file * @param array $filter + * @param string $app_root * @return bool */ -function checkCacheContent(string $cash_file_dir, array $filter): bool +function checkCacheContent(array $filter,string $app_root): bool { $status = true; - $cash_file_path = appDirSeparator($cash_file_dir . '/index_folder.txt'); - sort($filter); - $file_content = json_encode($filter, true); - $cache_file = fOpen($cash_file_path, 'a+'); - if (!is_file($cash_file_path) || filesize($cash_file_path) < 1) { - fwrite($cache_file, $file_content); + // check if cache directory exists before processing + $cash_file_dir = appDirSeparator($app_root . '/cache'); + if (!file_exists($cash_file_dir)) { + mkdir($cash_file_dir, 0777, true); + } + $cash_file_path = appDirSeparator($cash_file_dir . '/index_folder'); + + if (!file_exists($cash_file_path)){ + file_put_contents($cash_file_path, var_export($filter, true)); } else { - $content = fread($cache_file, filesize($cash_file_path)); - if ($content != $file_content) { - $cache_file = fOpen($cash_file_path, 'w'); - fwrite($cache_file, $file_content); + $old_content = file_get_contents($cash_file_path); + if (json_encode($old_content, true) != json_encode($filter, true)) { + file_put_contents($cash_file_path, var_export($filter, true)); } else { $status = false; } } - fclose($cache_file); return $status; } @@ -106,15 +104,3 @@ function appDirSeparator(string $path): string if ((substr(PHP_OS, 0, 3)) === 'WIN') $new_path = str_replace("\\", '/', $path); return $new_path; } - -/** - * @param $ex - * @return void - */ -function dumper($ex) -{ - print('
'); - print_r($ex); - print(''); - exit(); -} \ No newline at end of file diff --git a/controller/exampleController.php b/controller/exampleController.php index 440f104..3789bad 100644 --- a/controller/exampleController.php +++ b/controller/exampleController.php @@ -10,8 +10,9 @@ namespace Wepesi\Controller; -use Wepesi\Core\Input; -use Wepesi\Core\Redirect; + +use Wepesi\Core\Http\Input; +use Wepesi\Core\Http\Redirect; use Wepesi\Core\Session; class exampleController diff --git a/index.php b/index.php index 68878da..0429f5f 100644 --- a/index.php +++ b/index.php @@ -16,6 +16,13 @@ (new DotEnv($ROOT_DIR . '/.env'))->load(); +/** + * Generate and index file for redirection (protection) while APP_DEV in production + */ +if (getenv('APP_ENV') === 'prod') { + autoIndexFolder(['assets']); +} + $appConfiguration = new AppConfiguration(); $configuration = $appConfiguration diff --git a/middleware/Validation/exampleValidation.php b/middleware/Validation/exampleValidation.php index 63bc0fa..c5c4ad3 100644 --- a/middleware/Validation/exampleValidation.php +++ b/middleware/Validation/exampleValidation.php @@ -2,6 +2,7 @@ namespace Wepesi\Middleware\Validation; +use Wepesi\Core\Application; use Wepesi\Core\MiddleWare; class exampleValidation extends MiddleWare @@ -10,8 +11,8 @@ function changeLang() { $rules = [ "token" => $this->schema->string("token") - ->min(1) - ->max(2) + ->min(5) + ->max(30) ->required(), "lang" => $this->schema->string("lang") ->min(1) @@ -21,8 +22,7 @@ function changeLang() $this->validate->check($_POST, $rules); if (!$this->validate->passed()) { - dumper($this->validate->errors()); - exit(); + Application::dumper($this->validate->errors()); } } } \ No newline at end of file diff --git a/src/Core/Application.php b/src/Core/Application.php index a60af14..3cf85a0 100644 --- a/src/Core/Application.php +++ b/src/Core/Application.php @@ -8,7 +8,7 @@ use Wepesi\Core\Routing\Router; /** - * + * Application root */ class Application { @@ -55,7 +55,7 @@ public function __construct(string $path, AppConfiguration $config) { self::$ROOT_DIR = str_replace("\\", '/', $path); - self::$APP_DOMAIN = $this->domainSetup()->app_domain; + self::$APP_DOMAIN = serverDomain()->domain; self::$params = $config->generate(); self::$APP_TEMPLATE = self::$params['app_template'] ?? null; self::$APP_LANG = self::$params['lang'] ?? 'fr'; @@ -63,39 +63,6 @@ public function __construct(string $path, AppConfiguration $config) self::$LAYOUT_CONTENT = 'layout_content'; } - /** - * @return object - */ - private function domainSetup(): object - { - $server_name = $_SERVER['SERVER_NAME']; - $protocol = strtolower(explode('/', $_SERVER['SERVER_PROTOCOL'])[0]); - $domain = self::getDomainIp() === '127.0.0.1' ? "$protocol://$server_name" : $server_name; - return (object)[ - 'server_name' => $server_name, - 'protocol' => $protocol, - 'app_domain' => $domain, - ]; - } - - /** - * use method to get domain ip - * @return string - */ - public static function getDomainIp(): string - { - $ip = $_SERVER['REMOTE_ADDR']; - - if (!empty($_SERVER['HTTP_CLIENT_IP'])) { - $ip = $_SERVER['HTTP_CLIENT_IP']; - } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { - $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; - } elseif ($ip == '::1') { - $ip = gethostbyname(getHostName()); - } - return $ip; - } - /** * simple builtin dumper for dump data * @param $ex @@ -134,4 +101,4 @@ public function run() { $this->router->run(); } -} \ No newline at end of file +} diff --git a/views/home.php b/views/home.php index 1c8e082..da04799 100644 --- a/views/home.php +++ b/views/home.php @@ -6,6 +6,7 @@ use Wepesi\Core\Bundles; use Wepesi\Core\I18n; use Wepesi\Core\Session; +use Wepesi\Core\Token; $lang = Session::exists("lang") ? Session::get("lang") : "en"; $errors = Session::exists("errors") ? Session::flash("errors") : null; @@ -44,7 +45,7 @@