Skip to content

Commit

Permalink
Merge pull request #1107 from josaphatim/enh-to-lib
Browse files Browse the repository at this point in the history
Small improvement to code in lib folder
  • Loading branch information
kroky authored Jul 8, 2024
2 parents 50b4966 + 8aab934 commit 1a62fb4
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 82 deletions.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/* show all warnings in debug mode */
if (DEBUG_MODE) {
error_reporting(E_ALL | E_STRICT);
error_reporting(E_ALL);
}

/* don't let anything output content until we are ready */
Expand Down
6 changes: 2 additions & 4 deletions lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,16 +464,14 @@ class Hm_Site_Config_File extends Hm_Config {
public $user_defaults = [];

/**
* Load data based on source
* @param string $source source location for site configuration
* @param array $all_configs
*/
public function __construct($all_configs = []) {
$this->load(empty($all_configs) ? merge_config_files(APP_PATH.'config') : $all_configs, false);
}

/**
* Load site data from a file
* @param string $source file path to the site configuration
* @param string $all_configs
* @param string $key encryption key (unsued in this class)
* @return void
*/
Expand Down
23 changes: 13 additions & 10 deletions lib/dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,21 @@ private function get_pages($filters) {
* @return boolean
*/
public function validate_ajax_request($request, $filters) {
if (array_key_exists('hm_ajax_hook', $request->get)) {
if (in_array($request->get['hm_ajax_hook'], $this->get_pages($filters), true)) {
return true;
} else {
Hm_Functions::cease(json_encode(['status' => 'not callable']));;
}
}
if (array_key_exists('hm_ajax_hook', $request->post)) {
if (in_array($request->post['hm_ajax_hook'], $this->get_pages($filters), true)) {
return $this->validate_hook($request->get, $filters) || $this->validate_hook($request->post, $filters);
}

/**
* Validates ajax hook
* @param array $input POST or GET data
* @param array $filters list of filters
* @return boolean
*/
private function validate_hook($input, $filters) {
if (array_key_exists('hm_ajax_hook', $input)) {
if (in_array($input['hm_ajax_hook'], $this->get_pages($filters), true)) {
return true;
} else {
Hm_Functions::cease(json_encode(['status' => 'not callable']));;
Hm_Functions::cease(json_encode(['status' => 'not callable']));
}
}
return false;
Expand Down
68 changes: 26 additions & 42 deletions lib/environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@ public function load() {
if (method_exists($dotenvLoader, 'usePutenv')) {
$dotenvLoader->usePutenv(true);
}
$envDistFile = APP_PATH . '.env';
if (!file_exists($envDistFile)) {
Hm_Msgs::add('ERR.env file not found at: "' . $envDistFile . '"');
return;
}

$envFile = static::get('CYPHT_DOTENV');
$dotenvLoader->load($envDistFile);
if ($envFile) {
$dotenvLoader->loadEnv($envFile);
if (!file_exists($envFile)) {
Hm_Msgs::add('ERR.env file not found at: "' . $envFile . '"');
return;
}
$dotenvLoader->load($envFile);
}

public static function get($key, $default = null) {
Expand Down Expand Up @@ -91,50 +86,39 @@ function merge_config_files($folder_path) {
$files = glob($folder_path . '/*.php');

foreach ($files as $file) {
// Use require to include the file
$fileArray = require $file;

if(is_array($fileArray)) {
// Check if values are boolean and convert if necessary
$fileArray = array_map(function ($value) {
return is_array($value) ? $value : (
is_string($value) && strtolower($value) === 'true' ? true : (
is_string($value) && strtolower($value) === 'false' ? false : $value
)
);
}, $fileArray);

// Merge the arrays
$configArray = array_merge($configArray, $fileArray);
}
$fileArray = process_config_array($file);
$configArray = array_merge($configArray, $fileArray);
}
return $configArray;
}
}

if (!function_exists('config')) {
/**
* Merge configuration arrays from PHP files in the specified folder.
*
* This function includes each PHP file in the specified folder and retrieves its array.
* It then merges these arrays into a single configuration array, applying boolean conversion
* for values that are represented as "true" or "false" strings.
* Get configuration data for a single file
*
* @param string $folder_path The path to the folder containing PHP configuration files.
* @param string $file_name The path to PHP configuration file.
*
* @return array The merged configuration array.
* @return array The configuration array.
*/
function config($file_name) {
// Use require to include the file
$fileArray = require CONFIG_PATH.$file_name.'.php';
$path = CONFIG_PATH.$file_name.'.php';
return process_config_array($path);
}
}

// Check if values are boolean and convert if necessary
return array_map(function ($value) {
return is_array($value) ? $value : (
is_string($value) && strtolower($value) === 'true' ? true : (
is_string($value) && strtolower($value) === 'false' ? false : $value
)
);
}, $fileArray);
if (!function_exists('process_config_array')) {
function process_config_array($filename) {
$array = require $filename;
if (is_array($array)) {
return array_map(function ($value) {
return is_array($value) ? $value : (
is_string($value) && strtolower($value) === 'true' ? true : (
is_string($value) && strtolower($value) === 'false' ? false : $value
)
);
}, $array);
}
return [];
}
}
27 changes: 13 additions & 14 deletions lib/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,14 @@ class Hm_Functions {
public static function setcookie($name, $value, $lifetime = 0, $path = '', $domain = '', $secure = false, $html_only = false, $same_site = 'Strict') {
$prefix = ($lifetime != 0 && $lifetime < time()) ? 'Deleting' : 'Setting';
Hm_Debug::add(sprintf('%s cookie: name: %s, lifetime: %s, path: %s, domain: %s, secure: %s, html_only %s',$prefix, $name, $lifetime, $path, $domain, $secure, $html_only));
if (version_compare(PHP_VERSION, '7.3', '>=')) {
return setcookie($name, $value, [
'expires' => $lifetime,
'path' => $path,
'domain' => $domain,
'secure' => $secure,
'httponly' => $html_only,
'samesite' => $same_site
]
);
} else {
return setcookie($name, $value, $lifetime, $path, $domain, $secure, $html_only);
}
return setcookie($name, $value, [
'expires' => $lifetime,
'path' => $path,
'domain' => $domain,
'secure' => $secure,
'httponly' => $html_only,
'samesite' => $same_site
]);
}

/**
Expand Down Expand Up @@ -211,7 +206,11 @@ public static function filter_input_array($type, $filters) {
if ($type === INPUT_SERVER) {
$value = array();
foreach ($filters as $var => $flag) {
$value[$var] = filter_var($_SERVER[$var], $flag);
if (isset($_SERVER[$var])) {
$value[$var] = filter_var($_SERVER[$var], $flag);
} else {
$value[$var] = null;
}
}
return $value;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/modules_exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,9 @@ public function setup_debug_modules() {
'allowed_post' => array(), 'allowed_server' => array(), 'allowed_pages' => array());
$modules = $this->site_config->get_modules();
foreach ($modules as $name) {
if (is_readable(sprintf(APP_PATH."modules/%s/setup.php", $name))) {
$filters = self::merge_filters($filters, require sprintf(APP_PATH."modules/%s/setup.php", $name));
$path = sprintf(APP_PATH."modules/%s/setup.php", $name);
if (is_readable($path)) {
$filters = self::merge_filters($filters, require $path);
}
}
$this->filters = $filters;
Expand Down
13 changes: 4 additions & 9 deletions lib/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ private function get_other_request_details() {
* @return void
*/
private function empty_super_globals() {
if (version_compare(PHP_VERSION, '8.1', '>=')) {
return;
}
$_POST = [];
$_SERVER = [];
$_GET = [];
Expand All @@ -151,9 +148,7 @@ private function empty_super_globals() {
$_ENV = [];

foreach (array_keys($GLOBALS) as $key) {
if (isset($GLOBALS)) {
unset($GLOBALS[$key]);
}
unset($GLOBALS[$key]);
}
}

Expand Down Expand Up @@ -192,9 +187,9 @@ private function is_mobile() {
* @return void
*/
private function is_tls() {
if (array_key_exists('HTTPS', $this->server) && strtolower($this->server['HTTPS']) == 'on') {
if (!empty($this->server['HTTPS']) && strtolower($this->server['HTTPS']) == 'on') {
$this->tls = true;
} elseif (array_key_exists('REQUEST_SCHEME', $this->server) && strtolower($this->server['REQUEST_SCHEME']) == 'https') {
} elseif (!empty($this->server['REQUEST_SCHEME']) && strtolower($this->server['REQUEST_SCHEME']) == 'https') {
$this->tls = true;
}
}
Expand All @@ -218,7 +213,7 @@ private function get_request_type() {
* @return bool true if the request is from an AJAX call
*/
public function is_ajax() {
return array_key_exists('HTTP_X_REQUESTED_WITH', $this->server) && strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
return !empty($this->server['HTTP_X_REQUESTED_WITH']) && strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
}

/**
Expand Down

0 comments on commit 1a62fb4

Please sign in to comment.