array(
'fallback_autoloader' => true,
'namespaces' => array(
'Zend\\' => BASE_PATH . '/lib/ZF2/library/Zend',
),
'prefixes' => array(
'Zend_' => BASE_PATH . '/lib/Zend'
),
),
));
// Loading full config
$config = new Zend_Config(include(CFG_FILE_NAME), true);
if (file_exists(LOCAL_CFG_FILE_NAME)) {
$configLocal = new Zend_Config(include(LOCAL_CFG_FILE_NAME));
$config->merge($configLocal);
}
// autodetect database name if empty
if (('development' === APPLICATION_ENV || 'test' === APPLICATION_ENV) && empty($config->database->params->dbname)) {
$path = str_replace('.', '_', strtolower(WWW_PATH));
$matches = array();
// 1 => project, 2 => subProject, 3 => trunk|branches|tags, 4 => branchPath
if (preg_match('@public_html/([^/]+)(?:/([^/]+))?/(trunk|branches|tags)(?:/([^/]+))?/www@', $path, $matches)) {
$prefix = ('orangecmf' === $matches[1]) ? '' : 'prj_';
$project = (empty($matches[2])) ? $matches[1] : $matches[2];
$branchPath = (empty($matches[4])) ? '' : '_' . $matches[4];
$config->database->params->dbname = $prefix . $project . $branchPath;
$config->session->baseName .= $branchPath;
}
}
$config->setReadOnly();
Zend_Registry::set('config', $config);
unset($config);
// locale settings
setlocale(LC_ALL, Qs_Constant::get('SYSTEM_LOCALE'));
Zend_Registry::set('Zend_Locale', Qs_Constant::get('ZEND_LOCALE'));
// Кешування метаданих таблиць
$cacheDir = BASE_PATH . '/tmp/cache';
if (!file_exists($cacheDir)) {
$umask = umask(0);
if (false === mkdir($cacheDir, 0777)) {
die('Cannot create directory: ' . $cacheDir);
}
umask($umask);
}
$cache = Qs_Cache::factory('Core', 'File', array('automatic_serialization' => true));
Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
Zend_Locale_Data::setCache($cache);
// Визначення CURRENT_PAGE, PARENT_PAGE, CURRENT_LANGUAGE, DEFAULT_LANGUAGE
$currentPage = preg_replace('|^' . WEB_PATH . '/|', '', Frwd_ConfigLite::getServer('REQUEST_URI'));
$currentPage = preg_replace('/\?.*$/', '', $currentPage);
$currentPageParts = explode('/', $currentPage);
$language = array_shift($currentPageParts);
if ($language && $language === Qs_Db_Language::getDefault()) {
$currentPage = preg_replace('/^' . preg_quote($language, '/') . '($|\/)/', '', $currentPage);
Qs_Http::redirect(BASE_URL . '/' . $currentPage);
}
if (!Qs_Db_Language::exists($language)) {
$language = ($language == Qs_Db_Obj::BACKEND_PREFIX)?Qs_Db_Language::getAdminDefault():Qs_Db_Language::getDefault();
}
$currentPage = preg_replace('/^' . preg_quote($language, '/') . '($|\/)/', '', $currentPage);
$currentPageParts = explode('/', $currentPage);
array_pop($currentPageParts);
$parentPage = implode('/', $currentPageParts);
$defaultLanguage = Qs_Db_Language::getDefault();
$adminDefaultLanguage = Qs_Db_Language::getAdminDefault();
// Session
define('SESSION_NAME', __getSessionName($currentPage));
session_save_path(SESSION_SAVE_PATH);
session_name(SESSION_NAME);
Zend_Session::start();
// Debug
$debug = Qs_Debug::getInstance();
Zend_Registry::set('debug', $debug);
$languages = Qs_Db_Language::getList();
define('SOURCE_LANGUAGE', 'en');
define('CURRENT_LANG', $languages[$language]['name']);
define('LANG_LOCALE', $languages[$language]['locale']);
// define('LANG_DIR', isset($languages[$language]['dir']) ? $languages[$language]['dir'] : 'ltr');
// setlocale(LC_TIME, $languages[$language]['locale']);
if (count($languages) > 1) {
$fileCommonMo = BASE_PATH . '/languages/' . CURRENT_LANG . '/common.mo';
if (file_exists($fileCommonMo)) {
$translate = new Zend_Translate('Qs_Translate_Adapter_Gettext', $fileCommonMo, LANG_LOCALE);
if (defined('DEBUG') && DEBUG && defined('LOG_UNTRANSLATED') && (bool)LOG_UNTRANSLATED) {
$fileCommonLog = BASE_PATH . '/languages/' . CURRENT_LANG . '/common.log';
$_logPath = escapeshellarg($fileCommonLog);
exec("test -f $_logPath && cat $_logPath | sort | uniq - $_logPath ");
$writer = new Zend_Log_Writer_Stream($fileCommonLog);
$writer->setFormatter(new Zend_Log_Formatter_Simple("%message%\n"));
$log = new Zend_Log($writer);
$translate->getAdapter()->setOptions(array('log' => $log, 'logMessage' => '%locale% %filename%: "%message%"', 'logUntranslated' => true));
}
Zend_Registry::set('Zend_Translate', $translate);
} elseif (CURRENT_LANG !== SOURCE_LANGUAGE) {
Qs_Debug::log('Translation file not found: ' . $fileCommonMo);
}
}
define('CURRENT_PAGE', $currentPage);
define('PARENT_PAGE', $parentPage);
define('CURRENT_LANGUAGE', $language);
define('DEFAULT_LANGUAGE', $defaultLanguage);
define('ADMIN_DEFAULT_LANGUAGE', $adminDefaultLanguage);
if ($defaultLanguage == $language) {
define('BASE_URL_LANGUAGE', BASE_URL);
define('BASE_URL_LANGUAGE_HTTP', BASE_URL_HTTP);
define('BASE_URL_LANGUAGE_HTTPS', BASE_URL_HTTPS);
} else {
define('BASE_URL_LANGUAGE', BASE_URL . '/' . CURRENT_LANGUAGE);
define('BASE_URL_LANGUAGE_HTTP', BASE_URL_HTTP . '/' . CURRENT_LANGUAGE);
define('BASE_URL_LANGUAGE_HTTPS', BASE_URL_HTTPS . '/' . CURRENT_LANGUAGE);
}
// GLOBAL FUNCTIONS
function __getSessionName($currentPage)
{
$name = '';
/** @var $config Zend_Config */
$config = Zend_Registry::get('config')->session;
if (!empty($config->baseName)) {
$name = $config->baseName;
if (!empty($config->rolePostfix)) {
$currentPageFirstSlice = explode('/', $currentPage);
$currentPageFirstSlice = reset($currentPageFirstSlice);
foreach ($config->rolePostfix as $rolePostfix) {
if ($currentPageFirstSlice == $rolePostfix->currentPagePrefix) {
$name .= $rolePostfix->sessionPostfix;
break;
}
}
}
}
return $name;
}
function __switchSession($sessionName)
{
$sessionName = (string) $sessionName;
if (!$sessionName) {
return false;
}
session_write_close();
session_name($sessionName);
if (ini_get('session.use_cookies') && isset($_COOKIE[$sessionName])) {
session_id($_COOKIE[$sessionName]);
} else if (ini_get('session.use_trans_sid') && !ini_get('session.use_only_cookies') && isset($_REQUEST[$sessionName])) {
session_id($_REQUEST[$sessionName]);
}
session_start();
return true;
}
function __getHttpHeader($header)
{
if (empty($header)) {
return false;
}
// Try to get it from the $_SERVER array first
$temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
if (!empty($_SERVER[$temp])) {
return $_SERVER[$temp];
}
// This seems to be the only way to get the Authorization header on
// Apache
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
if (!empty($headers[$header])) {
return $headers[$header];
}
}
return false;
}
function vdie()
{
header('Content-Type: text/html; charset=utf-8');
$backtrace = debug_backtrace();
ob_start();
echo $backtrace[0]['file'], ' (', $backtrace[0]['line'], '):
' . PHP_EOL . PHP_EOL;
$vars = func_get_args();
foreach ($vars as $var) {
echo '
'; if (is_scalar($var) || is_null($var)) { var_dump($var); } else { echo PHP_EOL; print_r($var); echo PHP_EOL; } echo '' . PHP_EOL; } echo PHP_EOL . '
' . PHP_EOL; print_r($backtrace); echo PHP_EOL . '' . PHP_EOL; $html = ob_get_contents(); ob_end_clean(); if ('XMLHttpRequest' == __getHttpHeader('X_REQUESTED_WITH')) { $html = str_replace(array('
', ''), array("\n", "\n", ''), $html); $html = str_replace(array('
'; if (is_scalar($var) || is_null($var)) { var_dump($var); } else { print_r($var); } echo ''; } return true; } function _ve() { $version = (isset($_SERVER['SERVER_PROTOCOL']) && $_SERVER['SERVER_PROTOCOL'] === '1.0') ? '1.0' : '1.1'; header("HTTP/{$version} 303 See Other"); if ('XMLHttpRequest' == __getHttpHeader('X_REQUESTED_WITH')) { header('Content-Type: text/plain; charset=utf-8'); $delim = "\n"; $prefix = ''; $suffix = "\n"; } else { header('Content-Type: text/html; charset=utf-8'); $delim = "
'; $suffix = "\n"; } $backtrace = debug_backtrace(); echo $backtrace[0]['file'], ':', $backtrace[0]['line'], $delim; foreach (func_get_args() as $var) { echo $prefix; var_export($var); echo $suffix, $delim; } exit; } function logScriptDuration() { global $html; $logsPath = BASE_PATH . '/tmp/script-duration-logs'; if (!is_dir($logsPath)) { $umask = umask(0); if (false === mkdir($logsPath, 0777)) { umask($umask); return false; } umask($umask); } if (constant('MAX_SCRIPT_DURATION') < ($dutation = microtime(true) - SCRIPT_START)) { if (($fp = fopen($logsPath . '/times_' . date('Y-m-d') . '.txt', 'a+'))) { $contentLength = strlen($html); if (!$contentLength) { $contentLength = isset($_SERVER['CONTENT_LENGTH']) ? (int) $_SERVER['CONTENT_LENGTH'] : 0; } $separator = ' '; fwrite( $fp, date('Y-m-d H:i:s') . $separator . round($dutation, 4) . $separator . $_SERVER['REMOTE_ADDR'] . $separator . $_SERVER['REQUEST_METHOD'] . $separator . $contentLength . $separator . str_replace(WEB_PATH . '/', '', $_SERVER['REQUEST_URI']) . "\n" ); fclose($fp); } } // clean old logs if (date('N') == 5) { // every friday $filesList = glob($logsPath . '/times_*.txt'); if (!empty($filesList)) { $date = date('Y-m-d', strtotime(constant('LOG_SCRIPT_DURATION_LIFETIME'))); foreach ($filesList as $file) { if (basename($file) < 'times_' . $date . '.txt') { if (false === unlink($file)) { break; } } } } } return true; } // END OF GLOBAL FUNCTIONS