1<?php if (!defined('BASEPATH')) {
2    exit('No direct script access allowed');
3}
4/*
5   * LimeSurvey
6   * Copyright (C) 2013 The LimeSurvey Project Team / Carsten Schmitz
7   * All rights reserved.
8   * License: GNU/GPL License v2 or later, see LICENSE.php
9   * LimeSurvey is free software. This version may have been modified pursuant
10   * to the GNU General Public License, and as distributed it includes or
11   * is derivative of works licensed under the GNU General Public License or
12   * other free or open source software licenses.
13   * See COPYRIGHT.php for copyright notices and details.
14   *
15     *	Files Purpose: lots of common functions
16*/
17
18/**
19 * Class SettingGlobal
20 *
21 * @property string $stg_name Setting name
22 * @property string $stg_value Setting value
23 *
24 */
25class SettingGlobal extends LSActiveRecord
26{
27    /**
28     * @inheritdoc
29     * @return CActiveRecord
30     */
31    public static function model($class = __CLASS__)
32    {
33        return parent::model($class);
34    }
35
36    /** @inheritdoc */
37    public function tableName()
38    {
39        return '{{settings_global}}';
40    }
41
42    /** @inheritdoc */
43    public function primaryKey()
44    {
45        return 'stg_name';
46    }
47
48    /** @inheritdoc */
49    public function rules()
50    {
51        /* settings that must only comme from php files */
52        $disableByDb = array(
53            'versionnumber', // Come and leave it in version.php
54            'dbversionnumber', // Must keep it out of DB
55            'updatable', // If admin with ftp access disable updatable : leave it
56            'debug', // Currently not accessible, seem better
57            'debugsql', // Currently not accessible, seem better
58            'forcedsuperadmin', // This is for security
59            'defaultfixedtheme', // Because updating can broke instance
60            'ssl_emergency_override', // security related
61            'ssl_disable_alert', // security related
62        );
63        /* Specific disable settings for demo mode */
64        if (Yii::app()->getConfig("demoMode")) {
65            $disableByDb = array_merge($disableByDb,array('sitename','defaultlang','defaulthtmleditormode','filterxsshtml'));
66        }
67        $aRules = array(
68            array('stg_name', 'required'),
69            array('stg_name', 'unique'),
70            array('stg_value', 'default', 'value' => ''),
71            array('stg_name', 'in', 'not'=>true,'range' => $disableByDb),
72        );
73
74        return $aRules;
75    }
76
77    /**
78     * Update or set a setting in DB and update current app config if no error happen
79     * Return self : then other script can use if(!$oSetting) { $oSetting->getErrors; }
80     * @param string $settingname
81     * @param string $settingvalue
82     * @return self
83     */
84    public static function setSetting($settingname, $settingvalue)
85    {
86        $setting = self::model()->findByPk($settingname);
87        if(empty($setting)) {
88            $setting = new self;
89            $setting->stg_name = $settingname;
90        }
91        $setting->stg_value = $settingvalue;
92        if($setting->save()) {
93            Yii::app()->setConfig($settingname,$settingvalue);
94        }
95        return $setting;
96    }
97
98    /**
99     * Increase the custom asset version number in DB
100     * This will force the refresh of the assets folders content
101     */
102    static public function increaseCustomAssetsversionnumber()
103    {
104        $iCustomassetversionnumber = getGlobalSetting('customassetversionnumber');
105        $iCustomassetversionnumber++;
106        self::setSetting('customassetversionnumber', $iCustomassetversionnumber);
107        return;
108    }
109
110
111    /**
112     * Increase the asset version number in version.php
113     * This will force the refresh of the assets folders content
114     */
115    static public function increaseAssetsversionnumber()
116    {
117        @ini_set('auto_detect_line_endings', '1');
118        $sRootdir      = Yii::app()->getConfig("rootdir");
119        $versionlines = file($sRootdir.DIRECTORY_SEPARATOR.'application'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'version.php');
120        $handle       = fopen($sRootdir.DIRECTORY_SEPARATOR.'application'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'version.php', "w");
121        $iAssetNumber = self::generateAssetVersionNumber(Yii::app()->getConfig("assetsversionnumber"));
122
123        foreach ($versionlines as $line) {
124            if (strpos($line, 'assetsversionnumber') !== false) {
125                $line = '$config[\'assetsversionnumber\'] = \''.$iAssetNumber.'\';'."\r\n";
126            }
127            fwrite($handle, $line);
128        }
129        fclose($handle);
130        Yii::app()->setConfig("assetsversionnumber", $iAssetNumber);
131        return;
132    }
133
134    /**
135     * with comfortUpate, we increase the asset number by one.
136     * so to be sure that the asset number from comfortUpdate will be different from the one generated here, we index it by 100000
137     *
138     * @param int $iAssetNumber the current asset number
139     * @return int the new asset number
140     */
141    static public function generateAssetVersionNumber($iAssetNumber)
142    {
143        while ( $iAssetNumber == Yii::app()->getConfig("assetsversionnumber")) {
144            if ($iAssetNumber > 100000){
145                $iAssetNumber++;
146            }else{
147                $iAssetNumber = Yii::app()->getConfig("assetsversionnumber") + 100000;
148            }
149        }
150        return $iAssetNumber;
151    }
152}
153