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(array('_framework', '.'), array('', '_'), 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 = ('quick_site' === $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;
}
}
// detect mobile
$mobile = (bool)preg_match(
'/android|ipad|ipod|iphone|playbook|blackberry|kindle/i',
$_SERVER['HTTP_USER_AGENT']
);
define('IS_MOBILE', $mobile);
$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 . '/|', '', $_SERVER['REQUEST_URI']);
$currentPage = preg_replace('/\?.*$/', '', $currentPage);
$currentPageParts = explode('/', $currentPage);
$language = array_shift($currentPageParts);
if (!Qs_Db_Language::exists($language)) {
$language = 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();
// 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('CURRENT_LOCALE', $languages[$language]['locale']);
if (count($languages) > 1) {
$fileCommonMo = BASE_PATH . '/languages/' . CURRENT_LOCALE . '/common.mo';
if (file_exists($fileCommonMo)) {
$translate = new Zend_Translate('Qs_Translate_Adapter_Gettext', $fileCommonMo, CURRENT_LOCALE);
if (defined('DEBUG') && DEBUG && defined('LOG_UNTRANSLATED') && (bool)LOG_UNTRANSLATED) {
$fileCommonLog = BASE_PATH . '/languages/' . CURRENT_LOCALE . '/common.log';
exec($fileCommonLog . ' | sort | unique > ' . $fileCommonLog);
$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, 'logUntranslated' => true));
}
Zend_Registry::set('Zend_Translate', $translate);
} else {
Qs_Debug::log(' translation file not found: ' . 'languages/' . CURRENT_LOCALE . '/common.mo');
}
}
define('CURRENT_PAGE', $currentPage);
define('PARENT_PAGE', $parentPage);
define('CURRENT_LANGUAGE', $language);
define('DEFAULT_LANGUAGE', $defaultLanguage);
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