monster-hunter /
yii2-settings
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @link http://phe.me |
||
| 4 | * @copyright Copyright (c) 2014 Pheme |
||
| 5 | * @license MIT http://opensource.org/licenses/MIT |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace pheme\settings\models; |
||
| 9 | |||
| 10 | use Yii; |
||
| 11 | use yii\helpers\Json; |
||
| 12 | use yii\db\Expression; |
||
| 13 | use yii\db\ActiveRecord; |
||
| 14 | use yii\helpers\ArrayHelper; |
||
| 15 | use yii\base\InvalidParamException; |
||
| 16 | use yii\behaviors\TimestampBehavior; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * This is the model class for table "settings". |
||
| 20 | * |
||
| 21 | * @property integer $id |
||
| 22 | * @property string $type |
||
| 23 | * @property string $section |
||
| 24 | * @property string $key |
||
| 25 | * @property string $value |
||
| 26 | * @property boolean $active |
||
| 27 | * @property string $created |
||
| 28 | * @property string $modified |
||
| 29 | * |
||
| 30 | * @author Aris Karageorgos <[email protected]> |
||
| 31 | */ |
||
| 32 | class BaseSetting extends ActiveRecord implements SettingInterface |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @inheritdoc |
||
| 36 | */ |
||
| 37 | 24 | public static function tableName() |
|
| 38 | { |
||
| 39 | 24 | return '{{%settings}}'; |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @inheritdoc |
||
| 44 | */ |
||
| 45 | 6 | public function rules() |
|
| 46 | { |
||
| 47 | return [ |
||
| 48 | 6 | [['value'], 'string'], |
|
| 49 | 6 | [['section', 'key'], 'string', 'max' => 255], |
|
| 50 | [ |
||
| 51 | 6 | ['key'], |
|
| 52 | 6 | 'unique', |
|
| 53 | 6 | 'targetAttribute' => ['section', 'key'], |
|
| 54 | 6 | ], |
|
| 55 | 6 | ['type', 'in', 'range' => ['string', 'integer', 'boolean', 'float', 'double', 'object', 'null']], |
|
| 56 | 6 | [['created', 'modified'], 'safe'], |
|
| 57 | 6 | [['active'], 'boolean'], |
|
| 58 | 6 | ]; |
|
| 59 | } |
||
| 60 | |||
| 61 | 22 | public function afterSave($insert, $changedAttributes) |
|
| 62 | { |
||
| 63 | 22 | parent::afterSave($insert, $changedAttributes); |
|
| 64 | 22 | Yii::$app->settings->clearCache(); |
|
| 65 | 22 | } |
|
| 66 | |||
| 67 | 4 | public function afterDelete() |
|
| 68 | { |
||
| 69 | 4 | parent::afterDelete(); |
|
| 70 | 4 | Yii::$app->settings->clearCache(); |
|
| 71 | 4 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | 24 | public function behaviors() |
|
| 77 | { |
||
| 78 | return [ |
||
| 79 | 'timestamp' => [ |
||
| 80 | 24 | 'class' => TimestampBehavior::className(), |
|
| 81 | 'attributes' => [ |
||
| 82 | 24 | ActiveRecord::EVENT_BEFORE_INSERT => 'created', |
|
| 83 | 24 | ActiveRecord::EVENT_BEFORE_UPDATE => 'modified', |
|
| 84 | 24 | ], |
|
| 85 | 24 | 'value' => new Expression('NOW()'), |
|
| 86 | 24 | ], |
|
| 87 | 24 | ]; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @inheritdoc |
||
| 92 | */ |
||
| 93 | 4 | public function getSettings() |
|
| 94 | { |
||
| 95 | 4 | $settings = static::find()->where(['active' => true])->asArray()->all(); |
|
| 96 | 4 | return array_merge_recursive( |
|
| 97 | 4 | ArrayHelper::map($settings, 'key', 'value', 'section'), |
|
| 98 | 4 | ArrayHelper::map($settings, 'key', 'type', 'section') |
|
| 99 | 4 | ); |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @inheritdoc |
||
| 104 | */ |
||
| 105 | 2 | public function setSetting($section, $key, $value, $type = null) |
|
| 106 | { |
||
| 107 | 2 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
| 108 | |||
| 109 | 2 | if ($model === null) { |
|
| 110 | 1 | $model = new static(); |
|
| 111 | 1 | $model->active = 1; |
|
| 112 | 1 | } |
|
| 113 | 2 | $model->section = $section; |
|
| 114 | 2 | $model->key = $key; |
|
| 115 | 2 | $model->value = strval($value); |
|
| 116 | |||
| 117 | 2 | if ($type !== null) { |
|
| 118 | 1 | $model->type = $type; |
|
| 119 | 1 | } else { |
|
| 120 | 1 | $t = gettype($value); |
|
| 121 | 1 | if ($t == 'string') { |
|
| 122 | 1 | $error = false; |
|
| 123 | try { |
||
| 124 | 1 | Json::decode($value); |
|
| 125 | 1 | } catch (InvalidParamException $e) { |
|
| 126 | 1 | $error = true; |
|
| 127 | } |
||
| 128 | 1 | if (!$error) { |
|
| 129 | $t = 'object'; |
||
| 130 | } |
||
| 131 | 1 | } |
|
| 132 | 1 | $model->type = $t; |
|
| 133 | } |
||
| 134 | |||
| 135 | 2 | return $model->save(); |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @inheritdoc |
||
| 140 | */ |
||
| 141 | 6 | public function activateSetting($section, $key) |
|
| 142 | { |
||
| 143 | 6 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
| 144 | |||
| 145 | 6 | if ($model && $model->active == 0) { |
|
| 146 | 6 | $model->active = 1; |
|
| 147 | 6 | return $model->save(); |
|
| 148 | } |
||
| 149 | 1 | return false; |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @inheritdoc |
||
| 154 | */ |
||
| 155 | 3 | public function deactivateSetting($section, $key) |
|
| 156 | { |
||
| 157 | 3 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
| 158 | |||
| 159 | 3 | if ($model && $model->active == 1) { |
|
| 160 | 3 | $model->active = 0; |
|
| 161 | 3 | return $model->save(); |
|
| 162 | } |
||
| 163 | 1 | return false; |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @inheritdoc |
||
| 168 | */ |
||
| 169 | 2 | public function deleteSetting($section, $key) |
|
| 170 | { |
||
| 171 | 2 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
| 172 | |||
| 173 | 2 | if ($model) { |
|
| 174 | 2 | return $model->delete(); |
|
|
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
| 175 | } |
||
| 176 | return true; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritdoc |
||
| 181 | */ |
||
| 182 | 2 | public function deleteAllSettings() |
|
| 183 | { |
||
| 184 | 2 | return static::deleteAll(); |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritdoc |
||
| 189 | */ |
||
| 190 | 1 | public function findSetting($key, $section = null) |
|
| 191 | { |
||
| 192 | 1 | if (is_null($section)) { |
|
| 193 | $pieces = explode('.', $key, 2); |
||
| 194 | if (count($pieces) > 1) { |
||
| 195 | $section = $pieces[0]; |
||
| 196 | $key = $pieces[1]; |
||
| 197 | } else { |
||
| 198 | $section = ''; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | 1 | return $this->find()->where(['section' => $section, 'key' => $key])->limit(1)->one(); |
|
| 202 | } |
||
| 203 | } |
||
| 204 |