57 lines
2.0 KiB
PHP
57 lines
2.0 KiB
PHP
<?php
|
|
/**
|
|
* Global Helper Functions
|
|
*/
|
|
|
|
if (!function_exists('env')) {
|
|
function env(string $key, $default = null) {
|
|
return $_ENV[$key] ?? $default;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('input')) {
|
|
function input(string $key = null, $default = null) {
|
|
static $inputData = null;
|
|
if ($inputData === null) {
|
|
$json = file_get_contents('php://input');
|
|
$inputData = array_merge($_GET, $_POST, json_decode($json, true) ?? []);
|
|
}
|
|
|
|
if ($key === null) return $inputData;
|
|
return $inputData[$key] ?? $default;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('dd')) {
|
|
// M3 Fix: Guard dd() so it never leaks data in production
|
|
function dd(...$vars) {
|
|
if (env('APP_DEBUG', 'false') !== 'true') {
|
|
error_log('dd() called in production — suppressed. Check your code.');
|
|
json_error('Internal Server Error', 500);
|
|
}
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
foreach ($vars as $v) {
|
|
echo "<pre style='background:#1e1e1e;color:#d4d4d4;padding:1rem;border-radius:4px;'>";
|
|
var_dump($v);
|
|
echo "</pre>";
|
|
}
|
|
die();
|
|
}
|
|
}
|
|
|
|
if (!function_exists('safe_error')) {
|
|
/**
|
|
* Log exception details securely and return a safe user-facing message.
|
|
* Full details go to error_log; users only see a generic Arabic message.
|
|
*
|
|
* @param \Throwable $e The caught exception
|
|
* @param string $context Short label for the endpoint (e.g. 'invoices/upload')
|
|
* @param string $userMsg Arabic message shown to the user
|
|
* @param int $code HTTP status code
|
|
*/
|
|
function safe_error(\Throwable $e, string $context, string $userMsg = 'حدث خطأ غير متوقع. يرجى المحاولة مرة أخرى.', int $code = 500): void {
|
|
error_log("[{$context}] " . get_class($e) . ': ' . $e->getMessage() . ' | ' . $e->getFile() . ':' . $e->getLine());
|
|
json_error($userMsg, $code);
|
|
}
|
|
}
|