ZapMachine/conf/init.php

110 lines
No EOL
2.7 KiB
PHP
Executable file

<?php
include_once 'conf.php';
// Load gitignored local overrides (API keys, debug toggle) if present
$localConf = __DIR__ . '/conf.local.php';
if (file_exists($localConf)) {
require_once $localConf;
}
// Fallback: try environment variable if still not defined
if (!defined('BRAVE_API_KEY')) {
define('BRAVE_API_KEY', getenv('BRAVE_API_KEY') ?: 'your api key');
}
/**
* Define some app-wide constants
*/
define('ZAP_APP_BASE_DIR', realpath(__DIR__ . DIRECTORY_SEPARATOR . '..'));
define('ZAP_WEB_DIR', 'www');
define('ZAP_RESOURCE_DIR', 'resource');
define('ZAP_UTILS_DIR', 'utils');
define('ZAP_SESSIONS_DIR', 'sessions');
define('ZAP_LIB_DIR', 'lib');
define('ZAP_CONF_DIR', 'conf');
define('ZAP_TPL_DIR', 'tpl');
/**
* Error decoration
*/
define('ERROR_PREFIX', 'ZAP Error: ');
/**
* Set include path for application
*/
function setZAPIncludePath() {
$app_dirs = array(ZAP_RESOURCE_DIR, ZAP_UTILS_DIR, ZAP_SESSIONS_DIR,
ZAP_LIB_DIR, ZAP_TPL_DIR, ZAP_CONF_DIR);
$app_include_path = '';
foreach ($app_dirs as $app_dir) {
$app_include_path .= PATH_SEPARATOR . ZAP_APP_BASE_DIR . DIRECTORY_SEPARATOR . $app_dir;
}
if (set_include_path(get_include_path() . $app_include_path) == false) {
throw new Exception(ERROR_PREFIX . 'Error while creating include path...');
}
define('ZAP_INCLUDE_PATH_SET', true);
return;
}
/**
* Initiate autoload function for application
*/
spl_autoload_register(function ($class_name) {
if (!defined('ZAP_INCLUDE_PATH_SET') || ZAP_INCLUDE_PATH_SET === false) {
try {
setZAPIncludePath();
} catch (Exception $e) {
echo $e->getMessage();
die();
}
}
$include_path = get_include_path();
$include_path_tokens = explode(PATH_SEPARATOR, $include_path);
foreach ($include_path_tokens as $prefix) {
$path[0] = $prefix . DIRECTORY_SEPARATOR . $class_name . '.interface.php';
$path[1] = $prefix . DIRECTORY_SEPARATOR . $class_name . '.class.php';
foreach ($path as $thisPath) {
if (file_exists($thisPath)) {
require_once $thisPath;
return;
}
}
}
});
/**
* Define remaining app-wide constants
*/
define('ZAP_MOMENT', date("Y-m-d-H_i_s"));
define('ZAP_VERSION', '0.9.0');
define('ZAP_SESSION_NAME', 'ZAP');
// Only start session in web mode (not CLI)
if (php_sapi_name() !== 'cli') {
Session::start(ZAP_SESSION_NAME);
}
/**
* returns string of path of template file
* @return string
*/
function getZAPTemplate($tplname) {
if (!defined('ZAP_APP_BASE_DIR')) {
throw new Exception(ERROR_PREFIX . 'No base directory defined?!');
}
$tplfile = $tplname . '.tpl';
$tplfilepath = ZAP_APP_BASE_DIR . DIRECTORY_SEPARATOR
. ZAP_TPL_DIR . DIRECTORY_SEPARATOR . $tplfile;
if (file_exists($tplfilepath)) {
return $tplfilepath;
} else {
throw new Exception(ERROR_PREFIX . 'No template');
}
}
?>