Issues (13)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

models/BaseSetting.php (3 issues)

Upgrade to new PHP Analysis Engine

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
The expression $model->delete(); of type false|integer adds the type integer to the return on line 174 which is incompatible with the return type declared by the interface pheme\settings\models\Se...nterface::deleteSetting of type boolean.
Loading history...
175
        }
176
        return true;
177
    }
178
179
    /**
180
     * @inheritdoc
181
     */
182 2
    public function deleteAllSettings()
183
    {
184 2
        return static::deleteAll();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return static::deleteAll(); (integer) is incompatible with the return type declared by the interface pheme\settings\models\Se...face::deleteAllSettings of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->find()->wh...key))->limit(1)->one(); (yii\db\ActiveRecord|array|null) is incompatible with the return type declared by the interface pheme\settings\models\Se...gInterface::findSetting of type pheme\settings\models\SettingInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
202
    }
203
}
204