defapp.docs.php
<?php
// FINAL TWO-TIER: FS + URL - prevents APP_ROOT vs APP_URL bug
if (!defined('APP_ROOT')) {
$BASE_ROOT_FS = '/Users/rbutare/websites';
$SYSTEM_ROOT_FS = $BASE_ROOT_FS . '/system';
$APPS_ROOT_FS = $BASE_ROOT_FS . '/apps';
$BASE_URL = '/'; // rickweb docroot = websites/
$SYSTEM_URL = '/system';
$APPS_URL = '/apps';
if (!function_exists('_load_env')) {
function _load_env($f, &$e)
{
if (!is_file($f) || !is_readable($f)) {
return;
}
$fh = fopen($f, 'r');
if ($fh === false) {
return;
}
while (($l = fgets($fh)) !== false) {
$l = trim($l);
if ($l === '' || $l[0] === '#') {
continue;
}
$eq = strpos($l, '=');
if ($eq === false || $eq === 0) {
continue;
}
$k = trim(substr($l, 0, $eq));
$v = trim(substr($l, $eq + 1), "\"' ");
// Accept uppercase keys with underscores/digits.
if (strspn($k, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789') !== strlen($k)) {
continue;
}
$baseRoot = $e['BASE_ROOT'] ?? '/Users/rbutare/websites';
$v = str_replace(['$BASE_ROOT', '${BASE_ROOT}'], [$baseRoot, $baseRoot], $v);
$e[$k] = $v;
}
fclose($fh);
}
}
$env = [
'BASE_ROOT' => $BASE_ROOT_FS,
'SYSTEM_ROOT' => $SYSTEM_ROOT_FS,
'APPS_ROOT' => $APPS_ROOT_FS,
'APP_ROOT' => $APPS_ROOT_FS . '/defapp',
'APP_HOME' => $APPS_ROOT_FS . '/defapp',
];
_load_env($SYSTEM_ROOT_FS . '/.env', $env);
// Faster than debug_backtrace for normal HTTP/CLI entrypoints.
$caller = $_SERVER['SCRIPT_FILENAME'] ?? __FILE__;
$APP_NAME = $env['APP_NAME'] ?? 'aiwizard';
if (preg_match('#/apps/([^/]+)#', $caller, $m)) {
$APP_NAME = $m[1];
}
$APP_ROOT_FS = $APPS_ROOT_FS . '/' . $APP_NAME;
_load_env($APP_ROOT_FS . '/.env', $env);
$resolvedAppRoot = $APP_ROOT_FS;
$envAppRoot = $env['APP_HOME'] ?? ($env['APP_ROOT'] ?? '');
if (is_string($envAppRoot) && $envAppRoot !== '') {
$envAppRoot = rtrim($envAppRoot, '/');
$expectedRoot = $APPS_ROOT_FS . '/' . $APP_NAME;
if ($envAppRoot === $expectedRoot || str_starts_with($envAppRoot, $expectedRoot . '/')) {
$resolvedAppRoot = $envAppRoot;
}
}
define('BASE_ROOT', $env['BASE_ROOT'] ?? $BASE_ROOT_FS);
define('SYSTEM_ROOT', $SYSTEM_ROOT_FS);
define('APPS_ROOT', $APPS_ROOT_FS);
define('APP_ROOT', $resolvedAppRoot);
define('PROJECT_ROOT', APP_ROOT);
define('APP_NAME', $env['APP_NAME'] ?? $APP_NAME);
define('APP_HOME', APP_ROOT);
define('DEFAULT_APP_VERSION', $env['DEFAULT_APP_VERSION'] ?? 'v1.0.0');
define('DB_HOST', $env['DB_HOST'] ?? 'localhost');
define('DB_NAME', $env['DB_NAME'] ?? '');
define('DB_USER', $env['DB_USER'] ?? '');
define('DB_PASS', $env['DB_PASS'] ?? '');
define('BASE_URL', $BASE_URL);
define('SYSTEM_URL', $SYSTEM_URL);
define('APPS_URL', $APPS_URL);
define('APP_URL', APPS_URL . '/' . APP_NAME);
define('ADMIN_URL', APP_URL . '/admin');
define('PAGES_URL', APP_URL . '/pages');
// Rule #1 enforcement marker: lets auth.php (and anything else
// security-sensitive) refuse to run if it's ever included before this
// file finishes — see GOLDEN_RULES: "No hardcoded paths, ever".
define('BOOTSTRAP_LOADED', true);
// Optional diagnostics file write: disabled by default for performance.
if (($env['PATHS_WRITE_JSON'] ?? 'false') === 'true') {
@file_put_contents(
SYSTEM_ROOT . '/php/paths.json',
json_encode(['BASE_URL' => BASE_URL, 'APP_URL' => APP_URL, 'APP_NAME' => APP_NAME], JSON_PRETTY_PRINT)
);
}
}
if (!function_exists('sys_include')) {
function sys_include($p)
{
$p = ltrim($p, '/');
foreach ([APP_ROOT . '/' . $p, APP_ROOT . '/templates/' . basename($p), SYSTEM_ROOT . '/' . $p] as $c) {
if (file_exists($c)) {
return $c;
}
}
return APP_ROOT . '/' . $p;
}
}
if (!function_exists('app_url')) {
function app_url($path = '')
{
$path = ltrim($path, '/');
return APP_URL . '/' . $path;
}
}
if (!function_exists('app_path')) {
function app_path($path = '')
{
$path = ltrim($path, '/');
return APP_ROOT . '/' . $path;
}
}