Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version boss #159

Merged
merged 7 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#DATABASE CONFIGURATION

DB_HOST=127.0.0.1
DB_NAME=wepesi
DB_USER=root
Expand All @@ -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=

Expand All @@ -26,4 +25,6 @@ APP_ENV=dev

#APP DEFAULT LANG
LANG=fr

#APP TIME ZONE
TIME_ZONE=Africa/Kigali
1 change: 0 additions & 1 deletion cache/index_folder.txt

This file was deleted.

5 changes: 1 addition & 4 deletions config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 47 additions & 11 deletions config/constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
bim-g marked this conversation as resolved.
Show resolved Hide resolved
{
$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);
/**
* 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);
52 changes: 19 additions & 33 deletions config/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}

Expand All @@ -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('<pre>');
print_r($ex);
print('</pre>');
exit();
}
5 changes: 3 additions & 2 deletions controller/exampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions middleware/Validation/exampleValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wepesi\Middleware\Validation;

use Wepesi\Core\Application;
use Wepesi\Core\MiddleWare;

class exampleValidation extends MiddleWare
Expand All @@ -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)
Expand All @@ -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());
}
}
}
39 changes: 3 additions & 36 deletions src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Wepesi\Core\Routing\Router;

/**
*
* Application root
*/
class Application
{
Expand Down Expand Up @@ -55,47 +55,14 @@ 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';
$this->router = new Router();
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
Expand Down Expand Up @@ -134,4 +101,4 @@ public function run()
{
$this->router->run();
}
}
}
3 changes: 2 additions & 1 deletion views/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +45,7 @@
<div class="w3-card w3-border w3-round-large " style="width: 300px;overflow: hidden">
<h3 class="w3-text-blue-gray w3-padding"><?= $language->translate("Change the language") ?></h3>
<form action="<?= WEB_ROOT . "changelang" ?>" method="post">
<input type="hidden" name="token" value="<?= $lang ?>">
<input type="hidden" name="token" value="<?= Token::generate() ?>">
<select name="lang" id="lang_id" class="w3-select w3-center">
<option value="" class="w3-center w3-large" disabled selected><?= $lang ?></option>
<option value="fr">Francais</option>
Expand Down