$arrDetail) { $strOut .= $i . ' '; if (isset($arrDetail['file'])) { $strOut .= $arrDetail['file'] . ' #' . $arrDetail['line'] . ' '; } if (isset($arrDetail['class'])) { $strOut .= $arrDetail['class'] . $arrDetail['type']; } if (isset($arrDetail['function'])) { $strOut .= $arrDetail['function'] . '()'; } $strOut .= PHP_EOL; } } $strOut .= PHP_EOL . PHP_EOL; $strLogFileName = LOG_PATH . '/php_error.' . APPLICATION_ENV . '.' . date('Y-m-d') . '.log'; error_log($strOut, 3, $strLogFileName); chmod($strLogFileName, 0660); } /* Don't execute PHP internal error handler */ return true; } function myAutoLoader($strClass) { $strClassFile = str_replace(array('.', '/'), array('', ''), $strClass); $strClassFile = str_replace('_', '/', $strClassFile) . '.php'; $strClassFullFile = APPLICATION_PATH . '/../library/' . $strClassFile; if (file_exists($strClassFullFile) && is_file($strClassFullFile)) { require_once $strClassFullFile; if (class_exists($strClass) || interface_exists($strClass)) { return true; } } } function myShutDownFunction() { if ((!defined('IS_WEBSERVICE') || !IS_WEBSERVICE) && !defined('NO_TIMEOUT_ERROR') && MAX_EXECUTION_TIME > 0) { $dblScriptTime = round(microtime(true)-PT_ST, 0); if ($dblScriptTime >= MAX_EXECUTION_TIME) { if (LOG_REDIRECTS || 'production' != APPLICATION_ENV) { error_log('[' . date('r') . '] :: Page timeout :: ' . $_SERVER['REQUEST_URI'] . PHP_EOL, 3, REDIRECT_LOG . '/' . Utilities::getUserIpAddress() . '.log'); } Header('Location: /error/timeout'); die(); } } } register_shutdown_function('myShutDownFunction'); /** * Application environment (production / staging / development) * @var string */ define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing')); $strHost = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST']; define('STATIC_CONTENT', (substr($strHost, 0, 7) == 'static.') || (substr($_SERVER['REQUEST_URI'], 0, 9) == '/resource')); define('WS_CONTENT', (substr($strHost, 0, 3) == 'ws.')); if (STATIC_CONTENT) { if (substr($_SERVER['REQUEST_URI'], 0, 10) == '/resourceb') { define('RESOURCE_CONTROLLER', 'resourceb'); if ($strHost == 'static.portal.avd') { $strHost = 'portal.avd'; } } else if (substr($_SERVER['REQUEST_URI'], 0, 9) == '/resource') { define('RESOURCE_CONTROLLER', 'resource'); if (APPLICATION_ENV == 'production') { if ($strHost != 'payments.tripayserv.com' && $strHost != 'paytrader.hyphen.co.za') { $strHost = 'www.hyphen.co.za'; } } else { $strHost = substr($strHost, 7); } } else if (APPLICATION_ENV == 'production') { if ($strHost != 'payments.tripayserv.com' && $strHost != 'paytrader.hyphen.co.za') { $strHost = 'www.hyphen.co.za'; } } else { $strHost = substr($strHost, 7); } } define('DYNAMIC_HOST', $strHost); define('SECURE_SITE', (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')); define('CERT_AUTH', (SECURE_SITE && isset($_SERVER['SSL_CLIENT_S_DN_CN']))); if (CERT_AUTH) { define('CERT_CN', $_SERVER['SSL_CLIENT_S_DN_CN']); if ( DYNAMIC_HOST != $_SERVER['SSL_SERVER_S_DN_CN'] || $_SERVER['SSL_CLIENT_I_DN_CN'] != $_SERVER['SSL_SERVER_I_DN_CN'] || $_SERVER['SSL_CLIENT_V_REMAIN'] < 1 ) { if (LOG_REDIRECTS || 'production' != APPLICATION_ENV) { error_log('[' . date('r') . '] :: Certificate invalid :: ' . $_SERVER['REQUEST_URI'] . PHP_EOL, 3, REDIRECT_LOG . '/' . Utilities::getUserIpAddress() . '.log'); } Header('HTTP/1.1 403 Forbidden'); echo 'The certificate used for authentication is not valid'; die(); } } $strController = ''; // set new error handler if ('production' == APPLICATION_ENV && DYNAMIC_HOST == 'www.hyphen.co.za') { define('STATIC_HOST', (SECURE_SITE ? 'https' : 'http') . '://static.hyphen.co.za'); if (!defined('RESOURCE_CONTROLLER')) { define('RESOURCE_CONTROLLER', 'resource'); } } else if (DYNAMIC_HOST == 'payments.tripayserv.com') { define('STATIC_HOST', 'https://payments.tripayserv.com'); if (!defined('RESOURCE_CONTROLLER')) { define('RESOURCE_CONTROLLER', 'resource'); } } else if (DYNAMIC_HOST == 'paytrader.hyphen.co.za') { define('STATIC_HOST', 'https://static.hyphen.co.za'); if (!defined('RESOURCE_CONTROLLER')) { define('RESOURCE_CONTROLLER', 'resource'); } } else { define('STATIC_HOST', ''); if (!defined('RESOURCE_CONTROLLER')) { define('RESOURCE_CONTROLLER', 'resourceb'); } } /** * Path where application lives on the server * @var string */ define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application')); defined('LOG_PATH') || define('LOG_PATH', APPLICATION_PATH . '/../logs'); if (! file_exists(LOG_PATH) || ! is_dir(LOG_PATH)) { if (! mkdir(LOG_PATH)) { echo "Unable to create " . LOG_PATH . '
'; die(); } if (! chmod(LOG_PATH, 0700)) { echo "Unable to set permissions on " . LOG_PATH . '
'; die(); } } ini_set("error_log", LOG_PATH . '/php_error.' . APPLICATION_ENV . '.' . date('Y-m-d') . '.log'); $oldErrorHandler = set_error_handler('myErrorHandler', E_ALL); if (LOG_REDIRECTS || 'production' != APPLICATION_ENV) { defined('REDIRECT_LOG') || define('REDIRECT_LOG', LOG_PATH . '/redirects'); if (! file_exists(REDIRECT_LOG) || ! is_dir(REDIRECT_LOG)) { if (! mkdir(REDIRECT_LOG)) { echo "Unable to create " . REDIRECT_LOG . '
'; die(); } if (! chmod(REDIRECT_LOG, 0700)) { echo "Unable to set permissions on " . REDIRECT_LOG . '
'; die(); } } } /** * Path where TTF font files are stored * @var string */ defined('FONT_PATH') || define('FONT_PATH', APPLICATION_PATH . '/../desktop/fonts'); /** * Path where message files are stored * @var string */ defined('MESSAGES_PATH') || define('MESSAGES_PATH', APPLICATION_PATH . '/../desktop/messages'); /** * Path where foyer page content are stored * @var string */ defined('FOYER_PAGES') || define('FOYER_PAGES', APPLICATION_PATH . '/../foyer_pages'); // make sure foyer_pages dir exists if (! file_exists(FOYER_PAGES) || ! is_dir(FOYER_PAGES)) { if (! mkdir(FOYER_PAGES)) { echo "Unable to create " . FOYER_PAGES . '
'; die(); } if (! chmod(FOYER_PAGES, 0700)) { echo "Unable to set permissions on " . FOYER_PAGES . '
'; die(); } } /** * Path where File Transfer files are held temporarliy * @var string */ defined('FTS_PATH') || define('FTS_PATH', APPLICATION_PATH . '/../fts'); /** * Path where resources are stored * @var string */ defined('RESOURCE_PATH') || define('RESOURCE_PATH', APPLICATION_PATH . '/../resources'); // make sure resource_path dir exists if (! file_exists(RESOURCE_PATH) || ! is_dir(RESOURCE_PATH)) { if (! mkdir(RESOURCE_PATH)) { echo "Unable to create " . RESOURCE_PATH . '
'; die(); } if (! chmod(RESOURCE_PATH, 0700)) { echo "Unable to set permissions on " . RESOURCE_PATH . '
'; die(); } } if (!file_exists(RESOURCE_PATH . '/captcha') || !is_dir(RESOURCE_PATH . '/captcha')) { if (! mkdir(RESOURCE_PATH . '/captcha')) { echo "Unable to create " . RESOURCE_PATH . '/captcha' . '
'; die(); } if (! chmod(RESOURCE_PATH . '/captcha', 0700)) { echo "Unable to set permissions on " . RESOURCE_PATH . '/captcha' . '
'; die(); } } if (!file_exists(RESOURCE_PATH . '/graphs') || !is_dir(RESOURCE_PATH . '/graphs')) { if (! mkdir(RESOURCE_PATH . '/graphs')) { echo "Unable to create " . RESOURCE_PATH . '/graphs' . '
'; die(); } if (! chmod(RESOURCE_PATH . '/graphs', 0700)) { echo "Unable to set permissions on " . RESOURCE_PATH . '/graphs' . '
'; die(); } } if (!file_exists(RESOURCE_PATH . '/cache') || !is_dir(RESOURCE_PATH . '/cache')) { if (! mkdir(RESOURCE_PATH . '/cache')) { echo "Unable to create " . RESOURCE_PATH . '/cache' . '
'; die(); } if (! chmod(RESOURCE_PATH . '/cache', 0700)) { echo "Unable to set permissions on " . RESOURCE_PATH . '/cache' . '
'; die(); } } // cache handler $strCacheHandler = 'FILE'; if (!STATIC_CONTENT && class_exists('Memcache')) { $bTryingMemCache = true; $objMemCache = new Memcache(); $bRes = $objMemCache->pconnect('localhost', 11211); if ($bRes !== false) { $strCacheHandler = 'MEMCACHE'; } $bTryingMemCache = false; } define('CACHE_HANDLER', $strCacheHandler); /** * MAGIC.MIME database path * Full path including file name without the extension. eg /etc/magic.mime becomes /etc/magic * @var string */ if ('sandbox' == APPLICATION_ENV) { defined('MAGIC_MIME_PATH') || define('MAGIC_MIME_PATH', 'C:\Program Files\Zend\Apache2\conf\magic'); } else { defined('MAGIC_MIME_PATH') || define('MAGIC_MIME_PATH', '/httpd/conf/magic'); } // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), get_include_path() ))); require_once 'Utilities.php'; $arrRequest = explode('/', $_SERVER['REQUEST_URI']); $strController = $arrRequest[1]; if (strlen($strController) !== 0) { $arrModules = Utilities::retrieveCacheItem('GLOBAL_ALLOWED_MODULES'); if ($arrModules === false) { $arrModules = array(); if (file_exists(APPLICATION_PATH . '/configs/available_controllers.ini') && is_readable(APPLICATION_PATH . '/configs/available_controllers.ini')) { $resFile = fopen(APPLICATION_PATH . '/configs/available_controllers.ini', 'r'); $strModules = fread($resFile, filesize(APPLICATION_PATH . '/configs/available_controllers.ini')); fclose($resFile); $arrModules = explode("\n", $strModules); } Utilities::storeCacheItem('GLOBAL_ALLOWED_MODULES', $arrModules); } if (count($arrModules) !== 0) { if (!(count($arrModules) === 1 && in_array('', $arrModules))) { if (!in_array($strController, $arrModules)) { Utilities::logBadRequest(); } } } } $strIp = Utilities::getUserIpAddress(true); $strBadIpsFile = RESOURCE_PATH . '/badIps.txt'; $arrBlockIps = array(); if (file_exists($strBadIpsFile) && is_readable($strBadIpsFile)) { $resFile = fopen($strBadIpsFile, 'r'); if ($resFile) { $arrBlockIps = unserialize(fread($resFile, filesize($strBadIpsFile))); fclose($resFile); } } $bDie = false; if (isset($arrBlockIps[$strIp])) { $arrBlockDetail = $arrBlockIps[$strIp]; // more than 3 errors in last 60 seconds if ($arrBlockDetail['c'] >= 5 && $arrBlockDetail['l'] >= (time() - 30)) { $bDie = true; } else if ($arrBlockDetail['l'] < (time() - 300)) { unset($arrBlockIps[$strIp]); $resFile = fopen($strBadIpsFile, 'w'); if ($resFile) { fwrite($resFile, serialize($arrBlockIps)); fclose($resFile); } } } if (isset($_SERVER['HTTP_USER_AGENT'])) { $strBrowser = $_SERVER['HTTP_USER_AGENT']; $strBadBrowsersFile = RESOURCE_PATH . '/badBrowsers.txt'; $arrBlockBrowsers = array(); if (file_exists($strBadBrowsersFile) && is_readable($strBadBrowsersFile)) { $resFile = fopen($strBadBrowsersFile, 'r'); if ($resFile) { $arrBlockBrowsers = unserialize(fread($resFile, filesize($strBadBrowsersFile))); fclose($resFile); } } if (isset($arrBlockBrowsers[$strBrowser])) { $arrBlockDetail = $arrBlockBrowsers[$strBrowser]; // more than 3 errors in last 60 seconds if ($arrBlockDetail['c'] >= 5 && $arrBlockDetail['l'] >= (time() - 30)) { $bDie = true; } else if ($arrBlockDetail['l'] < (time() - 300)) { unset($arrBlockBrowsers[$strBrowser]); $resFile = fopen($strBadBrowsersFile, 'w'); if ($resFile) { fwrite($resFile, serialize($arrBlockBrowsers)); fclose($resFile); } } } } if ($bDie) { Utilities::logBadRequest(false); if (LOG_REDIRECTS || 'production' != APPLICATION_ENV) { error_log('[' . date('r') . '] :: Bad request - forbidden :: ' . $_SERVER['REQUEST_URI'] . PHP_EOL, 3, REDIRECT_LOG . '/' . Utilities::getUserIpAddress() . '.log'); } Header('HTTP/1.1 403 Forbidden'); Header('Content-Type: text/plain'); echo 'Your IP address have been used for abuse on this server. We have therefore blocked access for a while.'; die(); } if (APPLICATION_ENV == 'sandbox' && isset($_GET['forcemobile'])) { define('MOBILE_DEVICE', true); setcookie('forcemobile', 1, 0, '/', null, (SECURE_SITE), true); } else if (APPLICATION_ENV == 'sandbox' && isset($_COOKIE['forcemobile'])) { define('MOBILE_DEVICE', true); } else { define('MOBILE_DEVICE', false); } /* else { require_once 'Tera-WURFL/TeraWurfl.php'; $wurflObj = new TeraWurfl(); $wurflObj->getDeviceCapabilitiesFromAgent(); // see if this client is on a wireless device define('MOBILE_DEVICE', ($wurflObj->getDeviceCapability("is_wireless_device"))); } */ $strTemplatePath = (MOBILE_DEVICE) ? 'mobile' : 'templates'; /** * Path where templates are stored * @var string */ defined('TEMPLATE_PATH') || define('TEMPLATE_PATH', APPLICATION_PATH . '/../desktop/' . $strTemplatePath); /** * Zend_Application * */ #require_once 'Zend/Application.php'; if (WS_CONTENT && substr($_SERVER['REQUEST_URI'], 0, 11) != '/webservice' && substr($_SERVER['REQUEST_URI'], 0, 6) != '/error' && substr($_SERVER['REQUEST_URI'], 0, 5) != '/rest') { $_SERVER['REQUEST_URI'] = '/webservice' . $_SERVER['REQUEST_URI']; } else if (DYNAMIC_HOST == 'payments.tripayserv.com') { define('NO_TRACK_CHECK', true); if (substr($_SERVER['REQUEST_URI'], 0, 6) != '/error' && substr($_SERVER['REQUEST_URI'], 0, 5) != '/ajax' && substr($_SERVER['REQUEST_URI'], 0, 11) != '/webservice' && substr($_SERVER['REQUEST_URI'], 0, 9) != '/resource' && substr($_SERVER['REQUEST_URI'], 0, 11) != '/tradequest') { $_SERVER['REQUEST_URI'] = '/tradequest' . $_SERVER['REQUEST_URI']; } } else if ($_SERVER['REQUEST_URI'] == '/sitemap.xml') { $_SERVER['REQUEST_URI'] = '/foyer/sitemap.xml'; } else if ($_SERVER['REQUEST_URI'] == '/sitemap.html') { $_SERVER['REQUEST_URI'] = '/foyer/sitemap'; } // Create application, bootstrap, and run //$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini'); //$application->bootstrap()->run();