Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenaka <benakamoorthi@fastmail.fm>2014-10-28 08:52:43 +0300
committerBenaka <benakamoorthi@fastmail.fm>2014-10-28 08:52:43 +0300
commit52f47a9fc38016e99097af81dad6012e4451903e (patch)
tree81d60a11232c2ef89a1404afcceb32ef1dffc8c7 /core/FrontController.php
parent9c73ca6d97665b278deb76a45b797e59d2881b67 (diff)
parent9e3d90ce579c706d13a630123c003164b60d28e7 (diff)
Merge pull request #6541 from piwik/remove_exitwithmessage
Use exceptions instead of calls to Piwik_ExitWithMessage. Also includes refactoring Piwik_ExitWithMessage so no echo-ing or exit-ing is done by the function (function is renamed), modification to Log.php so it will work even if config is empty, and, includes event that allows modification of error page contents.
Diffstat (limited to 'core/FrontController.php')
-rw-r--r--core/FrontController.php263
1 files changed, 142 insertions, 121 deletions
diff --git a/core/FrontController.php b/core/FrontController.php
index cf1981c48a..c8ce328cda 100644
--- a/core/FrontController.php
+++ b/core/FrontController.php
@@ -12,6 +12,7 @@ namespace Piwik;
use Exception;
use Piwik\API\Request;
use Piwik\API\ResponseBuilder;
+use Piwik\Exceptions\HtmlMessageException;
use Piwik\Http\Router;
use Piwik\Plugin\Controller;
use Piwik\Plugin\Report;
@@ -106,10 +107,6 @@ class FrontController extends Singleton
* @param \Piwik\NoAccessException $exception The exception that was caught.
*/
Piwik::postEvent('User.isNotAuthorized', array($exception), $pending = true);
- } catch (Exception $e) {
- $debugTrace = $e->getTraceAsString();
- $message = Common::sanitizeInputValue($e->getMessage());
- Piwik_ExitWithMessage($message, $debugTrace, true, true);
}
}
@@ -223,7 +220,8 @@ class FrontController extends Singleton
{
try {
if (class_exists('Piwik\\Profiler')
- && !SettingsServer::isTrackerApiRequest()) {
+ && !SettingsServer::isTrackerApiRequest()
+ ) {
// in tracker mode Piwik\Tracker\Db\Pdo\Mysql does currently not implement profiling
Profiler::displayDbProfileReport();
Profiler::printQueryCount();
@@ -308,152 +306,142 @@ class FrontController extends Singleton
}
$initialized = true;
- try {
- Registry::set('timer', new Timer);
-
- $directoriesToCheck = array(
- '/tmp/',
- '/tmp/assets/',
- '/tmp/cache/',
- '/tmp/logs/',
- '/tmp/tcpdf/',
- '/tmp/templates_c/',
- );
+ Registry::set('timer', new Timer);
- Filechecks::dieIfDirectoriesNotWritable($directoriesToCheck);
+ $directoriesToCheck = array(
+ '/tmp/',
+ '/tmp/assets/',
+ '/tmp/cache/',
+ '/tmp/logs/',
+ '/tmp/tcpdf/',
+ '/tmp/templates_c/',
+ );
- Translate::loadEnglishTranslation();
+ Translate::loadEnglishTranslation();
- $exceptionToThrow = self::createConfigObject();
+ Filechecks::dieIfDirectoriesNotWritable($directoriesToCheck);
- $this->handleMaintenanceMode();
- $this->handleProfiler();
- $this->handleSSLRedirection();
+ $exceptionToThrow = self::createConfigObject();
- Plugin\Manager::getInstance()->loadPluginTranslations('en');
- Plugin\Manager::getInstance()->loadActivatedPlugins();
-
- if ($exceptionToThrow) {
- throw $exceptionToThrow;
- }
+ $this->handleMaintenanceMode();
+ $this->handleProfiler();
+ $this->handleSSLRedirection();
- // try to connect to the database
- try {
- Db::createDatabaseObject();
- Db::fetchAll("SELECT DATABASE()");
- } catch (Exception $exception) {
- if (self::shouldRethrowException()) {
- throw $exception;
- }
-
- Log::debug($exception);
-
- /**
- * Triggered when Piwik cannot connect to the database.
- *
- * This event can be used to start the installation process or to display a custom error
- * message.
- *
- * @param Exception $exception The exception thrown from creating and testing the database
- * connection.
- */
- Piwik::postEvent('Db.cannotConnectToDb', array($exception), $pending = true);
+ Plugin\Manager::getInstance()->loadPluginTranslations('en');
+ Plugin\Manager::getInstance()->loadActivatedPlugins();
- throw $exception;
- }
-
- // try to get an option (to check if data can be queried)
- try {
- Option::get('TestingIfDatabaseConnectionWorked');
- } catch (Exception $exception) {
- if (self::shouldRethrowException()) {
- throw $exception;
- }
-
- Log::debug($exception);
-
- /**
- * Triggered when Piwik cannot access database data.
- *
- * This event can be used to start the installation process or to display a custom error
- * message.
- *
- * @param Exception $exception The exception thrown from trying to get an option value.
- */
- Piwik::postEvent('Config.badConfigurationFile', array($exception), $pending = true);
+ if ($exceptionToThrow) {
+ throw $exceptionToThrow;
+ }
+ // try to connect to the database
+ try {
+ Db::createDatabaseObject();
+ Db::fetchAll("SELECT DATABASE()");
+ } catch (Exception $exception) {
+ if (self::shouldRethrowException()) {
throw $exception;
}
- // Init the Access object, so that eg. core/Updates/* can enforce Super User and use some APIs
- Access::getInstance();
+ Log::debug($exception);
/**
- * Triggered just after the platform is initialized and plugins are loaded.
+ * Triggered when Piwik cannot connect to the database.
*
- * This event can be used to do early initialization.
+ * This event can be used to start the installation process or to display a custom error
+ * message.
*
- * _Note: At this point the user is not authenticated yet._
+ * @param Exception $exception The exception thrown from creating and testing the database
+ * connection.
*/
- Piwik::postEvent('Request.dispatchCoreAndPluginUpdatesScreen');
+ Piwik::postEvent('Db.cannotConnectToDb', array($exception), $pending = true);
- \Piwik\Plugin\Manager::getInstance()->installLoadedPlugins();
+ throw $exception;
+ }
- // ensure the current Piwik URL is known for later use
- if (method_exists('Piwik\SettingsPiwik', 'getPiwikUrl')) {
- SettingsPiwik::getPiwikUrl();
+ // try to get an option (to check if data can be queried)
+ try {
+ Option::get('TestingIfDatabaseConnectionWorked');
+ } catch (Exception $exception) {
+ if (self::shouldRethrowException()) {
+ throw $exception;
}
+ Log::debug($exception);
+
/**
- * Triggered before the user is authenticated, when the global authentication object
- * should be created.
+ * Triggered when Piwik cannot access database data.
*
- * Plugins that provide their own authentication implementation should use this event
- * to set the global authentication object (which must derive from {@link Piwik\Auth}).
+ * This event can be used to start the installation process or to display a custom error
+ * message.
*
- * **Example**
- *
- * Piwik::addAction('Request.initAuthenticationObject', function() {
- * Piwik\Registry::set('auth', new MyAuthImplementation());
- * });
+ * @param Exception $exception The exception thrown from trying to get an option value.
*/
- Piwik::postEvent('Request.initAuthenticationObject');
- try {
- $authAdapter = Registry::get('auth');
- } catch (Exception $e) {
- throw new Exception("Authentication object cannot be found in the Registry. Maybe the Login plugin is not activated?
- <br />You can activate the plugin by adding:<br />
- <code>Plugins[] = Login</code><br />
- under the <code>[Plugins]</code> section in your config/config.ini.php");
- }
- Access::getInstance()->reloadAccess($authAdapter);
+ Piwik::postEvent('Config.badConfigurationFile', array($exception), $pending = true);
- // Force the auth to use the token_auth if specified, so that embed dashboard
- // and all other non widgetized controller methods works fine
- if (Common::getRequestVar('token_auth', false, 'string') !== false) {
- Request::reloadAuthUsingTokenAuth();
- }
- SettingsServer::raiseMemoryLimitIfNecessary();
+ throw $exception;
+ }
- Translate::reloadLanguage();
- \Piwik\Plugin\Manager::getInstance()->postLoadPlugins();
+ // Init the Access object, so that eg. core/Updates/* can enforce Super User and use some APIs
+ Access::getInstance();
- /**
- * Triggered after the platform is initialized and after the user has been authenticated, but
- * before the platform has handled the request.
- *
- * Piwik uses this event to check for updates to Piwik.
- */
- Piwik::postEvent('Platform.initialized');
- } catch (Exception $e) {
+ /**
+ * Triggered just after the platform is initialized and plugins are loaded.
+ *
+ * This event can be used to do early initialization.
+ *
+ * _Note: At this point the user is not authenticated yet._
+ */
+ Piwik::postEvent('Request.dispatchCoreAndPluginUpdatesScreen');
- if (self::shouldRethrowException()) {
- throw $e;
- }
+ \Piwik\Plugin\Manager::getInstance()->installLoadedPlugins();
- $debugTrace = $e->getTraceAsString();
- Piwik_ExitWithMessage($e->getMessage(), $debugTrace, true);
+ // ensure the current Piwik URL is known for later use
+ if (method_exists('Piwik\SettingsPiwik', 'getPiwikUrl')) {
+ SettingsPiwik::getPiwikUrl();
}
+
+ /**
+ * Triggered before the user is authenticated, when the global authentication object
+ * should be created.
+ *
+ * Plugins that provide their own authentication implementation should use this event
+ * to set the global authentication object (which must derive from {@link Piwik\Auth}).
+ *
+ * **Example**
+ *
+ * Piwik::addAction('Request.initAuthenticationObject', function() {
+ * Piwik\Registry::set('auth', new MyAuthImplementation());
+ * });
+ */
+ Piwik::postEvent('Request.initAuthenticationObject');
+ try {
+ $authAdapter = Registry::get('auth');
+ } catch (Exception $e) {
+ throw new HtmlMessageException("Authentication object cannot be found in the Registry. Maybe the Login plugin is not activated?
+ <br />You can activate the plugin by adding:<br />
+ <code>Plugins[] = Login</code><br />
+ under the <code>[Plugins]</code> section in your config/config.ini.php");
+ }
+ Access::getInstance()->reloadAccess($authAdapter);
+
+ // Force the auth to use the token_auth if specified, so that embed dashboard
+ // and all other non widgetized controller methods works fine
+ if (Common::getRequestVar('token_auth', false, 'string') !== false) {
+ Request::reloadAuthUsingTokenAuth();
+ }
+ SettingsServer::raiseMemoryLimitIfNecessary();
+
+ Translate::reloadLanguage();
+ \Piwik\Plugin\Manager::getInstance()->postLoadPlugins();
+
+ /**
+ * Triggered after the platform is initialized and after the user has been authenticated, but
+ * before the platform has handled the request.
+ *
+ * Piwik uses this event to check for updates to Piwik.
+ */
+ Piwik::postEvent('Platform.initialized');
}
protected function prepareDispatch($module, $action, $parameters)
@@ -612,4 +600,37 @@ class FrontController extends Singleton
Piwik::postEvent('Request.dispatch.end', array(&$result, $module, $action, $parameters));
return $result;
}
+
+ /**
+ * Returns HTML that displays an exception's error message (and possibly stack trace).
+ * The result of this method is echo'd by dispatch.php.
+ *
+ * @param Exception $ex The exception to use when generating the error page's HTML.
+ * @return string The HTML to echo.
+ */
+ public function getErrorResponse(Exception $ex)
+ {
+ $debugTrace = $ex->getTraceAsString();
+
+ if (method_exists($ex, 'getHtmlMessage')) {
+ $message = $ex->getHtmlMessage();
+ } else {
+ $message = Common::sanitizeInputValue($ex->getMessage());
+ }
+
+ $result = Piwik_GetErrorMessagePage($message, $debugTrace, true, true);
+
+ /**
+ * Triggered before a Piwik error page is displayed to the user.
+ *
+ * This event can be used to modify the content of the error page that is displayed when
+ * an exception is caught.
+ *
+ * @param string &$result The HTML of the error page.
+ * @param Exception $ex The Exception displayed in the error page.
+ */
+ Piwik::postEvent('FrontController.modifyErrorPage', array(&$result, $ex));
+
+ return $result;
+ }
}