#2 | Phalcon\Mvc\Application->handle
/srv/ChatHispanoEngine/releases/98/apps/Application.php (150) <?php
// namespace ChatHispanoEngine;
/**
* Application driver class to initialize Phalcon and
* other resources.
*/
class Application extends \Phalcon\Mvc\Application
{
private static $mode = 'dev';
private static $DEFAULT_MODULE = 'api';
public const MODE_PRODUCTION = 'prod';
public const MODE_STAGING = 'staging';
public const MODE_TEST = 'test';
public const MODE_DEVELOPMENT = 'dev';
/**
* Set application mode and error reporting level.
*/
public function __construct($defaultModule, $env = 'dev')
{
$this->modules = array(
'core' => array(
'className' => 'ChatHispanoEngine\Core\Module',
'path' => __DIR__.'/Core/Module.php',
),
'api' => array(
'className' => 'ChatHispanoEngine\Api\Module',
'path' => __DIR__.'/Api/Module.php',
),
'login' => array(
'className' => 'ChatHispanoEngine\Login\Module',
'path' => __DIR__.'/Login/Module.php',
),
'oidc' => array(
'className' => 'ChatHispanoEngine\Oidc\Module',
'path' => __DIR__.'/Oidc/Module.php',
),
'web' => array(
'className' => 'ChatHispanoEngine\Web\Module',
'path' => __DIR__.'/Web/Module.php',
),
'backoffice' => array(
'className' => 'ChatHispanoEngine\Backoffice\Module',
'path' => __DIR__.'/Backoffice/Module.php',
),
'movil' => array(
'className' => 'ChatHispanoEngine\Movil\Module',
'path' => __DIR__.'/Movil/Module.php',
),
'regwebexternal' => array(
'className' => 'ChatHispanoEngine\RegWebExternal\Module',
'path' => __DIR__.'/Regwebexternal/Module.php',
),
'cdn' => array(
'className' => 'ChatHispanoEngine\Cdn\Module',
'path' => __DIR__.'/Cdn/Module.php',
),
'shorten' => array(
'className' => 'ChatHispanoEngine\Shorten\Module',
'path' => __DIR__.'/Shorten/Module.php',
),
);
static::$DEFAULT_MODULE = $defaultModule;
self::$mode = $env;
self::$mode = trim(file_get_contents(__DIR__.'/../config/environment.txt'));
define('ENVIRONMENT', self::$mode);
if (!defined('PHALCON_MODE')) {
$mode = getenv('PHALCON_MODE');
$mode = $mode ? $mode : self::$mode;
define('PHALCON_MODE', $mode);
}
switch (self::getMode()) {
case self::MODE_PRODUCTION:
case self::MODE_STAGING:
error_reporting(0);
break;
case self::MODE_TEST:
case self::MODE_DEVELOPMENT:
ini_set('display_errors', 'On');
error_reporting(E_ALL);
break;
}
}
/**
* Register the services here to make them general or register in
* the ModuleDefinition to make them module-specific.
*/
protected function _registerServices()
{
$defaultModule = self::$DEFAULT_MODULE;
$modules = $this->modules;
$config = include __DIR__.'/../config/config.php';
$env_config = include __DIR__.'/../config/config_'.ENVIRONMENT.'.php';
$config->merge($env_config);
$di = new \Phalcon\DI\FactoryDefault();
include __DIR__.'/../config/loader.php';
include __DIR__.'/../config/services.php';
include __DIR__.'/../config/routing.php';
$this->setDI($di);
}
/**
* Run the application.
*/
public function main()
{
if (static::MODE_PRODUCTION === static::getMode()) {
$this->mainProd();
} else {
$this->mainDev();
}
}
private function getRequestUri()
{
if (!isset($_SERVER)) {
return "/";
}
if (!is_array($_SERVER)) {
return "/";
}
if (!isset($_SERVER['REQUEST_URI'])) {
return "/";
}
return $_SERVER['REQUEST_URI'];
}
/**
* Run the development environment.
*/
private function mainDev()
{
(new \Phalcon\Support\Debug())->listen();
$this->_registerServices();
$this->registerModules($this->modules);
$response = $this->handle($this->getRequestUri());
$response->send();
}
/**
* Run the production environment.
*/
private function mainProd()
{
try {
$this->registerModules($this->modules);
$this->_registerServices();
$response = $this->handle($this->getRequestUri());
$response->send();
} catch (\Exception $e) {
$logger = new \Phalcon\Logger\Adapter\Stream(__DIR__.'/../logs/'.date('Y-m-d').'.log');
$msg = "[".$_SERVER['SERVER_NAME']."] [".$_SERVER['REQUEST_URI']."] [".$e->getCode()."] ".$e->getMessage()." at ".$e->getFile()." (".$e->getLine().")";
$msg .= "\n".$e->getTraceAsString();
$logger->process(new \Phalcon\Logger\Item($msg, "error", 100));
$logger->close();
// remove view contents from buffer
ob_clean();
$errorCode = 500;
$errorView = __DIR__.'/../public/errors/error.html';
if (401 === $e->getCode()) {
// 401 UNAUTHORIZED
$errorCode = 401;
} elseif (403 === $e->getCode()) {
// 403 FORBIDDEN
$errorCode = 403;
} elseif (404 === $e->getCode()
|| $e instanceof Phalcon\Mvc\View\Exception
|| $e instanceof Phalcon\Mvc\Dispatcher\Exception) {
// 404 NOT FOUND
$errorCode = 404;
}
// Get error view contents. Since we are including the view
// file here you can use PHP and local vars inside the error view.
ob_start();
include_once $errorView;
$contents = ob_get_contents();
ob_end_clean();
// send view to header
$response = $this->getDI()->getShared('response');
$response->resetHeaders()
->setStatusCode($errorCode, null)
->setContent($contents)
->send()
;
/**
* We try to register in MongoDB the error to be able to
* track it in backoffice and/or receive emails
*/
try {
$system_log_manager = $this->getDI()->get('system_log_manager');
if ($errorCode == 500) {
$system_log_manager->createError([
'ip' => $_SERVER['SERVER_ADDR'],
'host' => $_SERVER['SERVER_NAME'],
'process' => 'php-fpm',
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
} else {
$system_log_manager->createWarning([
'ip' => $_SERVER['SERVER_ADDR'],
'host' => $_SERVER['SERVER_NAME'],
'process' => 'php-fpm',
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
}
} catch (\Exception $e) {
}
}
}
public function slowLog($t)
{
$config = $this->getDI()->get('config');
$irc_manager = $this->getDI()->get('inspircd_irc_manager');
$server = gethostname() ? gethostname() : 'unknown';
$uri = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if (substr($_SERVER['REQUEST_URI'], 0, 10) == '/historias') {
return;
}
$msg = "\x02[SLOWLOG] [".str_pad($server, 6, " ", STR_PAD_LEFT)."] [".str_pad(round($t, 1), 5, " ", STR_PAD_LEFT)."s] PATH=\x02 ".$_SERVER['REQUEST_URI']
."\x02 TYPE=\x02 ".$_SERVER['REQUEST_METHOD']."\x02 URI=\x02 ".$uri;
$irc_manager->privmsgOrderEnqueue('AAAAAI', $config->irc->debug_channel, $msg);
}
/**
* Get the current mode.
*
* @return string
*/
public static function getMode()
{
return self::$mode;
}
}
|
#3 | Application->mainDev
/srv/ChatHispanoEngine/releases/98/apps/Application.php (122) <?php
// namespace ChatHispanoEngine;
/**
* Application driver class to initialize Phalcon and
* other resources.
*/
class Application extends \Phalcon\Mvc\Application
{
private static $mode = 'dev';
private static $DEFAULT_MODULE = 'api';
public const MODE_PRODUCTION = 'prod';
public const MODE_STAGING = 'staging';
public const MODE_TEST = 'test';
public const MODE_DEVELOPMENT = 'dev';
/**
* Set application mode and error reporting level.
*/
public function __construct($defaultModule, $env = 'dev')
{
$this->modules = array(
'core' => array(
'className' => 'ChatHispanoEngine\Core\Module',
'path' => __DIR__.'/Core/Module.php',
),
'api' => array(
'className' => 'ChatHispanoEngine\Api\Module',
'path' => __DIR__.'/Api/Module.php',
),
'login' => array(
'className' => 'ChatHispanoEngine\Login\Module',
'path' => __DIR__.'/Login/Module.php',
),
'oidc' => array(
'className' => 'ChatHispanoEngine\Oidc\Module',
'path' => __DIR__.'/Oidc/Module.php',
),
'web' => array(
'className' => 'ChatHispanoEngine\Web\Module',
'path' => __DIR__.'/Web/Module.php',
),
'backoffice' => array(
'className' => 'ChatHispanoEngine\Backoffice\Module',
'path' => __DIR__.'/Backoffice/Module.php',
),
'movil' => array(
'className' => 'ChatHispanoEngine\Movil\Module',
'path' => __DIR__.'/Movil/Module.php',
),
'regwebexternal' => array(
'className' => 'ChatHispanoEngine\RegWebExternal\Module',
'path' => __DIR__.'/Regwebexternal/Module.php',
),
'cdn' => array(
'className' => 'ChatHispanoEngine\Cdn\Module',
'path' => __DIR__.'/Cdn/Module.php',
),
'shorten' => array(
'className' => 'ChatHispanoEngine\Shorten\Module',
'path' => __DIR__.'/Shorten/Module.php',
),
);
static::$DEFAULT_MODULE = $defaultModule;
self::$mode = $env;
self::$mode = trim(file_get_contents(__DIR__.'/../config/environment.txt'));
define('ENVIRONMENT', self::$mode);
if (!defined('PHALCON_MODE')) {
$mode = getenv('PHALCON_MODE');
$mode = $mode ? $mode : self::$mode;
define('PHALCON_MODE', $mode);
}
switch (self::getMode()) {
case self::MODE_PRODUCTION:
case self::MODE_STAGING:
error_reporting(0);
break;
case self::MODE_TEST:
case self::MODE_DEVELOPMENT:
ini_set('display_errors', 'On');
error_reporting(E_ALL);
break;
}
}
/**
* Register the services here to make them general or register in
* the ModuleDefinition to make them module-specific.
*/
protected function _registerServices()
{
$defaultModule = self::$DEFAULT_MODULE;
$modules = $this->modules;
$config = include __DIR__.'/../config/config.php';
$env_config = include __DIR__.'/../config/config_'.ENVIRONMENT.'.php';
$config->merge($env_config);
$di = new \Phalcon\DI\FactoryDefault();
include __DIR__.'/../config/loader.php';
include __DIR__.'/../config/services.php';
include __DIR__.'/../config/routing.php';
$this->setDI($di);
}
/**
* Run the application.
*/
public function main()
{
if (static::MODE_PRODUCTION === static::getMode()) {
$this->mainProd();
} else {
$this->mainDev();
}
}
private function getRequestUri()
{
if (!isset($_SERVER)) {
return "/";
}
if (!is_array($_SERVER)) {
return "/";
}
if (!isset($_SERVER['REQUEST_URI'])) {
return "/";
}
return $_SERVER['REQUEST_URI'];
}
/**
* Run the development environment.
*/
private function mainDev()
{
(new \Phalcon\Support\Debug())->listen();
$this->_registerServices();
$this->registerModules($this->modules);
$response = $this->handle($this->getRequestUri());
$response->send();
}
/**
* Run the production environment.
*/
private function mainProd()
{
try {
$this->registerModules($this->modules);
$this->_registerServices();
$response = $this->handle($this->getRequestUri());
$response->send();
} catch (\Exception $e) {
$logger = new \Phalcon\Logger\Adapter\Stream(__DIR__.'/../logs/'.date('Y-m-d').'.log');
$msg = "[".$_SERVER['SERVER_NAME']."] [".$_SERVER['REQUEST_URI']."] [".$e->getCode()."] ".$e->getMessage()." at ".$e->getFile()." (".$e->getLine().")";
$msg .= "\n".$e->getTraceAsString();
$logger->process(new \Phalcon\Logger\Item($msg, "error", 100));
$logger->close();
// remove view contents from buffer
ob_clean();
$errorCode = 500;
$errorView = __DIR__.'/../public/errors/error.html';
if (401 === $e->getCode()) {
// 401 UNAUTHORIZED
$errorCode = 401;
} elseif (403 === $e->getCode()) {
// 403 FORBIDDEN
$errorCode = 403;
} elseif (404 === $e->getCode()
|| $e instanceof Phalcon\Mvc\View\Exception
|| $e instanceof Phalcon\Mvc\Dispatcher\Exception) {
// 404 NOT FOUND
$errorCode = 404;
}
// Get error view contents. Since we are including the view
// file here you can use PHP and local vars inside the error view.
ob_start();
include_once $errorView;
$contents = ob_get_contents();
ob_end_clean();
// send view to header
$response = $this->getDI()->getShared('response');
$response->resetHeaders()
->setStatusCode($errorCode, null)
->setContent($contents)
->send()
;
/**
* We try to register in MongoDB the error to be able to
* track it in backoffice and/or receive emails
*/
try {
$system_log_manager = $this->getDI()->get('system_log_manager');
if ($errorCode == 500) {
$system_log_manager->createError([
'ip' => $_SERVER['SERVER_ADDR'],
'host' => $_SERVER['SERVER_NAME'],
'process' => 'php-fpm',
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
} else {
$system_log_manager->createWarning([
'ip' => $_SERVER['SERVER_ADDR'],
'host' => $_SERVER['SERVER_NAME'],
'process' => 'php-fpm',
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
}
} catch (\Exception $e) {
}
}
}
public function slowLog($t)
{
$config = $this->getDI()->get('config');
$irc_manager = $this->getDI()->get('inspircd_irc_manager');
$server = gethostname() ? gethostname() : 'unknown';
$uri = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if (substr($_SERVER['REQUEST_URI'], 0, 10) == '/historias') {
return;
}
$msg = "\x02[SLOWLOG] [".str_pad($server, 6, " ", STR_PAD_LEFT)."] [".str_pad(round($t, 1), 5, " ", STR_PAD_LEFT)."s] PATH=\x02 ".$_SERVER['REQUEST_URI']
."\x02 TYPE=\x02 ".$_SERVER['REQUEST_METHOD']."\x02 URI=\x02 ".$uri;
$irc_manager->privmsgOrderEnqueue('AAAAAI', $config->irc->debug_channel, $msg);
}
/**
* Get the current mode.
*
* @return string
*/
public static function getMode()
{
return self::$mode;
}
}
|