Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #14875: No error is shown at debug=0 if DB is broken #1276

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions application/controllers/SurveysController.php
Expand Up @@ -61,6 +61,10 @@ public function actionPublicList($lang = null)
*/
public function actionError()
{
if($this->getConfig('DBVersion') < 200) {
/* Activate since DBVersion for 2.50 and up */
return;
}
$oTemplate = Template::model()->getInstance(getGlobalSetting('defaulttheme'));

$this->sTemplate = $oTemplate->sTemplateName;
Expand Down
46 changes: 30 additions & 16 deletions application/core/LSYii_Application.php
Expand Up @@ -48,6 +48,12 @@ class LSYii_Application extends CWebApplication
*/
protected $plugin;

/**
* The DB version, used to check if setup is all OK
* @var integer|null
*/
protected $dbVersion;

/**
*
* Initiates the application
Expand Down Expand Up @@ -132,32 +138,39 @@ public function setConfigs() {
if (file_exists( $configdir . '/config.php')) {
$userConfigs = require( $configdir .'/config.php');
if (is_array($userConfigs['config'])) {

$this->config = array_merge($this->config, $userConfigs['config']);

}
}


if(!file_exists(__DIR__.'/../config/config.php')) {
/* Set up not done : then no other part to update */
return;
}/* User file config */
}
/* User file config */
$userConfigs = require(__DIR__.'/../config/config.php');
if (is_array($userConfigs['config'])) {
$this->config = array_merge($this->config, $userConfigs['config']);
}
/* Database config */
/* Check DB : let throw error if DB is broken issue #14875 */
Yii::app()->db->getConnectionStatus();
/* DB checked : Database config */
try {
/* Since DB exist : think we must not use try/catch ? */
$settingsTableExist = Yii::app()->db->schema->getTable('{{settings_global}}');
if (is_object($settingsTableExist)) {
$dbConfig = CHtml::listData(SettingGlobal::model()->findAll(), 'stg_name', 'stg_value');
$this->config = array_merge($this->config, $dbConfig);
/* According to updatedb_helper : no update can be done before settings_global->DBVersion > 183, then set it only if upper to 183 */
if(!empty($dbConfig['DBVersion']) && $dbConfig['DBVersion'] > 183) {
$this->dbVersion = $dbConfig['DBVersion'];
}
}
} catch (Exception $exception) {
/* Even if database can exist : don't throw exception, */
/* @todo : find when settings_global was created with stg_name and stg_value, maybe can Throw Exception ? */
Yii::log("Table settings_global not found");// Log it as LEVEL_INFO , application category
/* settings_global was created before 1.80 */
Yii::log("Table settings_global not found",'error');
throw new CHttpException(500, "Table settings_global not found");
}
/* Add some specific config using exiting other configs */
$this->setConfig('globalAssetsVersion', /* Or create a new var ? */
Expand Down Expand Up @@ -356,19 +369,20 @@ public function getPlugin()
public function onException($event)
{
if (Yii::app() instanceof CWebApplication) {
$configExists = file_exists(__DIR__.'/../config/config.php');
$usingTestEnv = defined('PHP_ENV') && PHP_ENV == 'test';
if ($usingTestEnv || !$configExists) {
if($usingTestEnv) {
// If run from phpunit, die with exception message.
die($event->exception->getMessage());
} else {
/* Here we have different possibility : maybe 400/401/403/404 with debug < 2 except for forced superadmin (currently : the 4 part don't show intersting information with debug*/
/* 500 always if debug and for (forced) superadmin even if no debug is set (and try to show complete error in this case (see issue by olle about white page and log */
if ((Yii::app()->getConfig('debug')<1 /* || Permission::hasGlobalPermission('superadmin') */) || (isset($event->exception->statusCode) &&$event->exception->statusCode=='404')) {
Yii::app()->setComponent('errorHandler', array(
'errorAction'=>'surveys/error',
));
}
}
if(!$this->dbVersion) {
return; // DB not set , don't try to manage exception
}
/* Here we have different possibility : maybe 400/401/403/404 with debug < 2 except for forced superadmin (currently : the 4 part don't show intersting information with debug*/
/* 500 always if debug and for (forced) superadmin even if no debug is set (and try to show complete error in this case (see issue by olle about white page and log */
if ((Yii::app()->getConfig('debug')<1 /* || Permission::hasGlobalPermission('superadmin') */) || (isset($event->exception->statusCode) && $event->exception->statusCode=='404')) {
Yii::app()->setComponent('errorHandler', array(
'errorAction'=>'surveys/error',
));
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions application/core/LSYii_Controller.php
Expand Up @@ -35,8 +35,9 @@ public function __construct($id, $module = null)

//Yii::app()->session->init();
$this->loadLibrary('LS.LS');
// This will setConfig from database
// Usage of old function
$this->loadHelper('globalsettings');
// Some functiion : traceVar
$this->loadHelper('common');
$this->loadHelper('expressions.em_manager');
$this->loadHelper('replacements');
Expand Down Expand Up @@ -171,7 +172,7 @@ public function createAbsoluteUrl($route, $params = array(), $schema = '', $ampe
}
return trim($sPublicUrl, "/").$url;
} else {
return parent::createAbsoluteUrl($route, $params, $schema, $ampersand);
return parent::createAbsoluteUrl($route, $params, $schema, $ampersand);
}
}

Expand Down
37 changes: 1 addition & 36 deletions application/helpers/globalsettings_helper.php
Expand Up @@ -13,22 +13,7 @@
* See COPYRIGHT.php for copyright notices and details.
*
*/
//Ensure script is not run directly, avoid path disclosure
//if (!isset($homedir) || isset($_REQUEST['$homedir'])) {die("Cannot run this script directly");}
injectglobalsettings();


function injectglobalsettings()
{
$settings = SettingGlobal::model()->findAll();

//if ($dbvaluearray!==false)
if (count($settings) > 0) {
foreach ($settings as $setting) {
Yii::app()->setConfig($setting->getAttribute('stg_name'), $setting->getAttribute('stg_value'));
}
}
}
/**
* Returns a global setting
* @deprecated : use App()->getConfig($settingname)
Expand All @@ -39,25 +24,5 @@ function injectglobalsettings()
*/
function getGlobalSetting($settingname)
{
$dbvalue = Yii::app()->getConfig($settingname);

if ($dbvalue === false) {
$dbvalue = SettingGlobal::model()->findByPk($settingname);

if ($dbvalue === null) {
Yii::app()->setConfig($settingname, null);
$dbvalue = '';
} else {
$dbvalue = $dbvalue->getAttribute('stg_value');
}

if (Yii::app()->getConfig($settingname) !== false) {
// If the setting was not found in the setting table but exists as a variable (from config.php)
// get it and save it to the table
SettingGlobal::setSetting($settingname, Yii::app()->getConfig($settingname));
$dbvalue = Yii::app()->getConfig($settingname);
}
}

return $dbvalue;
return Yii::app()->getConfig($settingname);
}
35 changes: 25 additions & 10 deletions application/models/SettingGlobal.php
Expand Up @@ -24,6 +24,20 @@
*/
class SettingGlobal extends LSActiveRecord
{

/**
* @var string[] settings that must only come from php files
*/
private $disableByDb = array(
'versionnumber', // Come and leave it in version.php
'dbversionnumber', // Must keep it out of DB
'updatable', // If admin with ftp access disable updatable : leave it
'debug', // Currently not accessible, seem better
'debugsql', // Currently not accessible, seem better
'forcedsuperadmin', // This is for security
'defaultfixedtheme', // Because updating can broke instance
'demoMode', // No demoMode update via GUI
);
/**
* @inheritdoc
* @return CActiveRecord
Expand All @@ -48,16 +62,7 @@ public function primaryKey()
/** @inheritdoc */
public function rules()
{
/* settings that must only comme from php files */
$disableByDb = array(
'versionnumber', // Come and leave it in version.php
'dbversionnumber', // Must keep it out of DB
'updatable', // If admin with ftp access disable updatable : leave it
'debug', // Currently not accessible, seem better
'debugsql', // Currently not accessible, seem better
'forcedsuperadmin', // This is for security
'defaultfixedtheme', // Because updating can broke instance
);
$disableByDb = $this->disableByDb;
/* Specific disable settings for demo mode */
if (Yii::app()->getConfig("demoMode")) {
$disableByDb = array_merge($disableByDb,array('sitename','defaultlang','defaulthtmleditormode','filterxsshtml'));
Expand All @@ -72,6 +77,16 @@ public function rules()
return $aRules;
}

/** @inheritdoc
* Allways update of current application config after sucessfull save
**/

protected function afterSave()
{
parent::afterSave();
Yii::app()->setConfig($this->stg_name,$this->stg_value);
}

/**
* Update or set a setting in DB and update current app config if no error happen
* Return self : then other script can use if(!$oSetting) { $oSetting->getErrors; }
Expand Down