Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/LimeSurvey/LimeSurvey.git
Browse files Browse the repository at this point in the history
…into master_13513
  • Loading branch information
Shnoulle committed Jun 4, 2018
2 parents d1a5d3f + c3d2521 commit 08f2a9f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 34 deletions.
2 changes: 1 addition & 1 deletion application/config/version.php
Expand Up @@ -13,7 +13,7 @@


$config['versionnumber'] = '3.9.0';
$config['dbversionnumber'] = 350;
$config['dbversionnumber'] = 349;
$config['buildnumber'] = '';
$config['updatable'] = true;
$config['assetsversionnumber'] = '30036';
Expand Down
2 changes: 0 additions & 2 deletions application/controllers/admin/themeoptions.php
Expand Up @@ -84,8 +84,6 @@ public function update($id)
$model->attributes = $_POST['TemplateConfiguration'];
if ($model->save()) {
Yii::app()->user->setFlash('success', gT('Theme options saved.'));
/* If one TemplateConfiguration_files_* updated, we need to update asset */
Template::model()->findByPk($model->template_name)->resetAsset();
$this->getController()->redirect(array('admin/themeoptions/sa/update/id/'.$model->id));
}
}
Expand Down
6 changes: 4 additions & 2 deletions application/controllers/admin/themes.php
Expand Up @@ -346,6 +346,7 @@ public function uploadfile()
$uploadresult = gT("An error occurred uploading your file. This may be caused by incorrect permissions for the application /tmp folder.");
} else {
$uploadresult = sprintf(gT("File %s uploaded"), $filename);
Template::model()->findByPk($templatename)->resetAssetVersion(); // Upload a files, asset need to be resetted (maybe)
$status = 'success';
}
}
Expand Down Expand Up @@ -461,6 +462,7 @@ public function templatefiledelete()

if (@unlink($the_full_file_path)) {
Yii::app()->user->setFlash('success', sprintf(gT("The file %s was deleted."), htmlspecialchars($sFileToDelete)));
Template::model()->findByPk($sTemplateName)->resetAssetVersion(); // Delete a files, asset need to be resetted (maybe)
} else {
Yii::app()->user->setFlash('error', sprintf(gT("File %s couldn't be deleted. Please check the permissions on the /upload/themes folder"), htmlspecialchars($sFileToDelete)));
}
Expand Down Expand Up @@ -583,8 +585,8 @@ public function delete($templatename)
if (Template::checkIfTemplateExists($templatename) && !Template::isStandardTemplate($templatename)) {

if (!Template::hasInheritance($templatename)) {

if (rmdirr(Yii::app()->getConfig('userthemerootdir')."/".$templatename)) {
Template::model()->findByPk($templatename)->deleteAssetVersion();
$surveys = Survey::model()->findAllByAttributes(array('template' => $templatename));

// The default template could be the same as the one we're trying to remove
Expand Down Expand Up @@ -699,7 +701,7 @@ public function templatesavechanges()
// If the file is an asset file, we refresh asset number
if (in_array($relativePathEditfile, $cssfiles) || in_array($relativePathEditfile, $jsfiles)){
//SettingGlobal::increaseCustomAssetsversionnumber();
Template::model()->findByPk($sTemplateName)->resetAsset();
Template::model()->findByPk($sTemplateName)->resetAssetVersion();
}

fclose($handle);
Expand Down
2 changes: 1 addition & 1 deletion application/core/LSYii_Application.php
Expand Up @@ -142,7 +142,7 @@ public function setConfigs() {
/* @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
}
/* Add some specific config using existing other configs */
/* Add some specific config using exiting other configs */
$this->setConfig('globalAssetsVersion', /* Or create a new var ? */
$this->getConfig('assetsversionnumber',0).
$this->getConfig('versionnumber',0).
Expand Down
4 changes: 2 additions & 2 deletions application/core/LSYii_AssetManager.php
Expand Up @@ -26,9 +26,9 @@ protected function hash($path)
protected function generatePath($file,$hashByName=false)
{
if (is_file($file)) {
$pathForHashing=$hashByName ? dirname($file) : dirname($file).filemtime($file).AssetVersion::getAssetVersion($file);
$pathForHashing=$hashByName ? dirname($file) : dirname($file).".".filemtime($file).".".AssetVersion::getAssetVersion($file);
} else {
$pathForHashing=$hashByName ? $file : $file.filemtime($file).AssetVersion::getAssetVersion($file);
$pathForHashing=$hashByName ? $file : $file.".".filemtime($file).".".AssetVersion::getAssetVersion($file);
}
return $this->hash($pathForHashing);
}
Expand Down
10 changes: 3 additions & 7 deletions application/helpers/update/updatedb_helper.php
Expand Up @@ -2203,28 +2203,24 @@ function db_upgrade_all($iOldDBVersion, $bSilent = false)
$oTransaction->commit();
}



if ($iOldDBVersion < 349) {
$oTransaction = $oDB->beginTransaction();
dropColumn('{{users}}','one_time_pw');
addColumn('{{users}}','one_time_pw','text');
$oDB->createCommand()->update('{{settings_global}}', ['stg_value'=>349], "stg_name='DBVersion'");
$oTransaction->commit();
}

/**
* Adding asset version to allow to reset asset without write inside
*/
if ($iOldDBVersion < 350) {
$oTransaction = $oDB->beginTransaction();
$oDB->createCommand()->createTable('{{asset_version}}',array(
'hash' => 'string(64)',
'path' => 'text',
'id' => 'pk',
'path' => 'text NOT NULL',
'version' => 'integer NOT NULL',
));
/* Create index on hash */
$oDB->createCommand()->addPrimaryKey('{{asset_version_pk}}', '{{asset_version}}', ['hash']);
$oDB->createCommand()->update('{{settings_global}}', ['stg_value'=>350], "stg_name='DBVersion'");
$oTransaction->commit();
}
Expand Down
35 changes: 20 additions & 15 deletions application/models/AssetVersion.php
Expand Up @@ -15,8 +15,8 @@
/**
* Class AssetVersion
*
* @property string $hash identifier of path
* @property string $path for reminder
* @property integer $id pk
* @property string $path
* @property integer $version number
*/
class AssetVersion extends LSActiveRecord
Expand All @@ -43,18 +43,12 @@ public function tableName()
return '{{asset_version}}';
}

/** @inheritdoc */
public function primaryKey()
{
return array('path');
}

/** @inheritdoc */
public function rules()
{
return array(
array('hash', 'required'),
array('path', 'required'),
array('path', 'unique'),
array('version', 'required'),
array('version', 'numerical', 'integerOnly'=>true),
);
Expand All @@ -70,8 +64,7 @@ public static function getAssetVersion($path)
if(Yii::app()->getConfig('DBVersion') < 349) {
return 0;
}
$hash = hash('sha256', $path);
$oAssetVersion = self::model()->findByPk($hash);
$oAssetVersion = self::model()->find('path = :path',array(":path"=>$path));
if(!$oAssetVersion) {
return 0;
}
Expand All @@ -88,16 +81,28 @@ public static function incrementAssetVersion($path)
if(Yii::app()->getConfig('DBVersion') < 349) {
return 0;
}
$hash = hash('sha256', $path);
$oAssetVersion = self::model()->findByPk($hash);
/* This increment case insensitivity , (extend_vanilla at same time than Extend_Vanilla) no real issue (update 2 assets in one) , but … */
$oAssetVersion = self::model()->find('path = :path',array(":path"=>$path));
if(!$oAssetVersion) {
$oAssetVersion = new self;
$oAssetVersion->hash = $hash;
$oAssetVersion->path = $path;
$oAssetVersion->version = 0;
}
$oAssetVersion->version++;
$oAssetVersion->save(); // Not need to test : can break rules. DB error can happen ?
$oAssetVersion->save(); // Not need to test : can not break rules. DB error can happen ?
return $oAssetVersion->version;
}

/**
* delete assets version related to path
* @param string $path
* @return integer (0|1)
*/
public static function deleteAssetVersion($path)
{
if(Yii::app()->getConfig('DBVersion') < 349) {
return 0;
}
return self::model()->deleteAll('path = :path',array(":path"=>$path));
}
}
13 changes: 12 additions & 1 deletion application/models/Template.php
Expand Up @@ -514,11 +514,21 @@ public static function resetInstance()
* Using DB only
* @return void
*/
public function resetAsset()
public function resetAssetVersion()
{
AssetVersion::incrementAssetVersion(self::getTemplatePath($this->name));
}

/**
* Delete asset related to this template
* Using DB only
* @return integer (0|1)
*/
public function deleteAssetVersion()
{
return AssetVersion::deleteAssetVersion(self::getTemplatePath($this->name));
}

/**
* Return the standard template list
* @return string[]
Expand Down Expand Up @@ -592,6 +602,7 @@ public static function getTemplateInFolder($sFolder)
public function renameTo($sNewName)
{
Yii::import('application.helpers.sanitize_helper', true);
$this->deleteAssetVersion();
Survey::model()->updateAll(array('template' => $sNewName), "template = :oldname", array(':oldname'=>$this->name));
Template::model()->updateAll(array('name' => $sNewName, 'folder' => $sNewName), "name = :oldname", array(':oldname'=>$this->name));
TemplateConfiguration::rename($this->name, $sNewName);
Expand Down
5 changes: 2 additions & 3 deletions installer/create-database.php
Expand Up @@ -811,11 +811,10 @@ function createDatabase($oDB){

// asset version
$oDB->createCommand()->createTable('{{asset_version}}',array(
'hash' => 'string(64)',
'path' => 'text',
'id' => 'pk',
'path' => 'text NOT NULL',
'version' => 'integer NOT NULL',
));
$oDB->createCommand()->addPrimaryKey('{{asset_version_pk}}', '{{asset_version}}', ['hash']);

// Set database version
$oDB->createCommand()->insert("{{settings_global}}", ['stg_name'=> 'DBVersion' , 'stg_value' => $databaseCurrentVersion]);
Expand Down

0 comments on commit 08f2a9f

Please sign in to comment.