Issues (443)

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.

backend/controllers/MenuController.php (14 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://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace backend\controllers;
9
10
use common\components\Json;
11
use common\models\Menu;
12
use common\models\MenuItem;
13
use common\models\Post;
14
use common\models\PostType;
15
use common\models\Taxonomy;
16
use common\models\Term;
17
use Yii;
18
use yii\filters\AccessControl;
19
use yii\filters\VerbFilter;
20
use yii\helpers\ArrayHelper;
21
use yii\web\Controller;
22
use yii\web\NotFoundHttpException;
23
24
/**
25
 * MenuController, controlling the actions for Menu and MenuItem model.
26
 *
27
 * @author Agiel K. Saputra <[email protected]>
28
 * @since 0.1.0
29
 */
30
class MenuController extends Controller
31
{
32
    /**
33
     * @inheritdoc
34
     */
35 View Code Duplication
    public function behaviors()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        return [
38
            'access' => [
39
                'class' => AccessControl::className(),
40
                'rules' => [
41
                    [
42
                        'actions' => ['index', 'update', 'create', 'delete', 'create-menu-item', 'delete-menu-item'],
43
                        'allow' => true,
44
                        'roles' => ['administrator'],
45
                    ],
46
                ],
47
            ],
48
            'verbs' => [
49
                'class' => VerbFilter::className(),
50
                'actions' => [
51
                    'delete' => ['post'],
52
                    'update' => ['post'],
53
                    'create-menu-item' => ['post'],
54
                    'delete-menu-item' => ['post'],
55
                ],
56
            ],
57
        ];
58
    }
59
60
    /**
61
     * Displays menu page consists of CRUD for Menu and MenuItem model.
62
     *
63
     * @param null $id
64
     * @return mixed
65
     */
66
    public function actionIndex($id = null)
67
    {
68
        $model = new Menu();
69
        $postTypes = PostType::find()->where(['menu_builder' => PostType::MENU_BUILDER])->all();
70
        $taxonomies = Taxonomy::find()->where(['menu_builder' => Taxonomy::MENU_BUILDER])->all();
71
72
        if ($available = ArrayHelper::map(Menu::find()->all(), 'id', 'title')) {
73
            if ($id === null && $available) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $available of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
74
                reset($available);
75
                $id = key($available);
76
            }
77
            $selected = $this->findModel($id);
78
        }
79
80
        return $this->render('index', [
81
            'model' => $model,
82
            'available' => $available,
83
            'selected' => isset($selected) ? $selected : null,
84
            'postTypes' => $postTypes,
85
            'taxonomies' => $taxonomies,
86
        ]);
87
    }
88
89
    /**
90
     * Creates a new Menu model.
91
     * If creation is successful, the browser will be redirected to the 'index' page.
92
     *
93
     * @return mixed
94
     */
95
    public function actionCreate()
96
    {
97
        $model = new Menu();
98
99
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
100
            return $this->redirect(['index', 'id' => $model->id]);
101
        }
102
103
        return $this->redirect(['index']);
104
    }
105
106
    /**
107
     * Updates an existing Menu model.
108
     * If update is successful, the browser will be redirected to the 'view' page.
109
     *
110
     * @param integer $id
111
     * @return mixed
112
     */
113
    public function actionUpdate($id)
114
    {
115
        $model = $this->findModel($id);
116
117
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
0 ignored issues
show
The method load cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
The method save cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
118
            if ($menuOrder = Yii::$app->request->post('MenuOrder')) {
119
                $this->saveMenuItem(Json::decode($menuOrder));
120
            }
121
        }
122
123
        Yii::$app->getSession()->setFlash('success', Yii::t('writesdown', 'Menu successfully saved.'));
0 ignored issues
show
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
124
125
        return $this->redirect(['/menu/index', 'id' => $id]);
126
    }
127
128
    /**
129
     * Deletes an existing Menu model.
130
     * If deletion is successful, the browser will be redirected to the 'index' page.
131
     *
132
     * @return mixed
133
     * @throws NotFoundHttpException
134
     * @throws \Exception
135
     */
136
    public function actionDelete()
137
    {
138
        $id = Yii::$app->request->post('id');
139
        $this->findModel($id)->delete();
0 ignored issues
show
The method delete cannot be called on $this->findModel($id) (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
140
141
        return $this->redirect(['index']);
142
    }
143
144
    /**
145
     * Create MenuItem models.
146
     *
147
     * @param int $id
148
     * @return string
149
     */
150
    public function actionCreateMenuItem($id)
151
    {
152
        $items = '';
153
154
        if (Yii::$app->request->post('type') === 'link') {
155
            $model = new MenuItem();
156
            $model->menu_id = $id;
157
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
158
                $items .= $this->renderPartial('_render-item', ['item' => $model, 'wrap' => 'true']);
159
            }
160
        }
161
162 View Code Duplication
        if (Yii::$app->request->post('type') === 'post' && $postIds = Yii::$app->request->post('postIds')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
            foreach ($postIds as $postId) {
164
                if ($post = $this->findPost($postId)) {
165
                    $model = new MenuItem([
166
                        'menu_id' => $id,
167
                        'label' => $post->title,
168
                        'url' => $post->getUrl(),
0 ignored issues
show
The method getUrl cannot be called on $post (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
169
                    ]);
170
                    if ($model->save()) {
171
                        $items .= $this->renderPartial('_render-item', ['item' => $model, 'wrap' => 'true']);
172
                    }
173
                }
174
            }
175
        }
176
177 View Code Duplication
        if (Yii::$app->request->post('type') === 'taxonomy' && $termIds = Yii::$app->request->post('termIds')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
            foreach ($termIds as $termId) {
179
                if ($term = $this->findTerm($termId)) {
180
                    $model = new MenuItem([
181
                        'menu_id' => $id,
182
                        'label' => $term->name,
183
                        'url' => $term->getUrl(),
0 ignored issues
show
The method getUrl cannot be called on $term (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
184
                    ]);
185
                    if ($model->save()) {
186
                        $items .= $this->renderPartial('_render-item', ['item' => $model, 'wrap' => 'true']);
187
                    }
188
                }
189
            }
190
        }
191
192
        return $items;
193
    }
194
195
    /**
196
     * Delete an existing MenuItem model via AJAX request.
197
     *
198
     * @throws NotFoundHttpException
199
     * @throws \Exception
200
     */
201
    public function actionDeleteMenuItem()
202
    {
203
        /* @var $children \common\models\MenuItem[] */
204
        if ($id = Yii::$app->request->post('id')) {
205
            $model = $this->findMenuItem($id);
206
            if ($model && $model->delete()) {
0 ignored issues
show
The method delete cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
207
                $children = MenuItem::find()->where(['parent' => $model->id])->all();
208
                foreach ($children as $child) {
209
                    $child->updateAttributes(['parent' => $model->parent]);
210
                }
211
            } else {
212
                throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
213
            }
214
        }
215
    }
216
217
    /**
218
     * Save menu item recursively based on parent and child.
219
     *
220
     * @param array $menuOrder
221
     * @param int $menuParent
222
     */
223
    protected function saveMenuItem($menuOrder, $menuParent = 0)
224
    {
225
        foreach ($menuOrder as $key => $order) {
226
            $menuItem = Yii::$app->request->post('MenuItem')[$order['id']];
227
            if ($model = $this->findMenuItem($order['id'])) {
228
                $model->setAttributes($menuItem);
0 ignored issues
show
The method setAttributes cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
229
                $model->setAttributes([
0 ignored issues
show
The method setAttributes cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
230
                    'parent' => $menuParent,
231
                    'order' => $key,
232
                ]);
233
                if ($model->save()) {
0 ignored issues
show
The method save cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
234
                    if ($orderItems = ArrayHelper::getValue($order, 'items')) {
235
                        $this->saveMenuItem($orderItems, $model->id);
236
                    }
237
                }
238
            }
239
        }
240
    }
241
242
    /**
243
     * Finds the Menu model based on its primary key value.
244
     * If the model is not found, a 404 HTTP exception will be thrown.
245
     *
246
     * @param integer $id
247
     * @return Menu the loaded model
248
     * @throws NotFoundHttpException if the model cannot be found
249
     */
250
    protected function findModel($id)
251
    {
252
        if (($model = Menu::findOne($id)) !== null) {
253
            return $model;
254
        }
255
256
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
257
    }
258
259
    /**
260
     * Finds the MenuItem model based on its primary key value.
261
     * If the model is not found, it will return false.
262
     *
263
     * @param integer $id
264
     * @return MenuItem the loaded model
265
     * @throws NotFoundHttpException if the model cannot be found
266
     */
267
    protected function findMenuItem($id)
268
    {
269
        if (($model = MenuItem::findOne($id)) !== null) {
270
            return $model;
271
        }
272
273
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
274
    }
275
276
    /**
277
     * Finds the Post model based on its primary key value.
278
     * If the model is not found, it will return false.
279
     *
280
     * @param integer $id
281
     * @return Post the loaded model
282
     * @throws NotFoundHttpException if the model cannot be found
283
     */
284
    protected function findPost($id)
285
    {
286
        if (($model = Post::findOne($id)) !== null) {
287
            return $model;
288
        }
289
290
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
291
    }
292
293
    /**
294
     * Finds the Term model based on its primary key value.
295
     * If the model is not found, it will return false.
296
     *
297
     * @param integer $id
298
     * @return Term the loaded model
299
     * @throws NotFoundHttpException if the model cannot be found
300
     */
301
    protected function findTerm($id)
302
    {
303
        if (($model = Term::findOne($id)) !== null) {
304
            return $model;
305
        }
306
307
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
308
    }
309
}
310