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/PostCommentController.php (11 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\models\Post;
11
use common\models\PostComment;
12
use common\models\PostType;
13
use common\models\search\PostComment as PostCommentSearch;
14
use Yii;
15
use yii\filters\AccessControl;
16
use yii\filters\VerbFilter;
17
use yii\web\Controller;
18
use yii\web\NotFoundHttpException;
19
20
/**
21
 * PostCommentController, controlling the actions for PostComment model.
22
 *
23
 * @author Agiel K. Saputra <[email protected]>
24
 * @since 0.1.0
25
 */
26
class PostCommentController extends Controller
27
{
28
    /**
29
     * @inheritdoc
30
     */
31 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...
32
    {
33
        return [
34
            'access' => [
35
                'class' => AccessControl::className(),
36
                'rules' => [
37
                    [
38
                        'actions' => ['index', 'update', 'delete', 'bulk-action', 'reply'],
39
                        'allow' => true,
40
                        'roles' => ['editor'],
41
                    ],
42
                ],
43
            ],
44
            'verbs' => [
45
                'class' => VerbFilter::className(),
46
                'actions' => [
47
                    'delete' => ['post'],
48
                    'bulk-action' => ['post'],
49
                ],
50
            ],
51
        ];
52
    }
53
54
55
    /**
56
     * Lists all PostComment models on specific post type.
57
     * If there is post_id the action will generate list of all PostComment models based on post_id.
58
     *
59
     * @param integer $posttype Post type ID
60
     * @param null|integer $post Post ID
61
     * @throws \yii\web\NotFoundHttpException
62
     * @return string
63
     */
64
    public function actionIndex($posttype, $post = null)
65
    {
66
        $postId = null;
67
        $postType = $this->findPostType($posttype);
68
69
        if ($post) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $post of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
70
            $post = $this->findPost($post);
71
            $postId = $post->id;
72
        }
73
74
        $searchModel = new PostCommentSearch();
75
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $posttype, $postId);
76
77
        return $this->render('index', [
78
            'post' => $post,
79
            'postType' => $postType,
80
            'searchModel' => $searchModel,
81
            'dataProvider' => $dataProvider,
82
        ]);
83
    }
84
85
86
    /**
87
     * Updates an existing PostComment model.
88
     * If update is successful, the browser will be redirected to the 'view' page.
89
     *
90
     * @param integer $id
91
     * @return mixed
92
     */
93
    public function actionUpdate($id)
94
    {
95
        $model = $this->findModel($id);
96
97
        if ($model->load(Yii::$app->request->post())) {
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...
98
            $model->date = date('Y-m-d H:i:s', strtotime($model->date));
99
            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...
100
                return $this->redirect(['update', 'id' => $model->id]);
101
            }
102
        }
103
104
        return $this->render('update', [
105
            'model' => $model,
106
        ]);
107
    }
108
109
    /**
110
     * Deletes an existing PostComment model.
111
     * If deletion is successful, the browser will be redirected to the 'index' page.
112
     *
113
     * @param integer $id
114
     * @return mixed
115
     */
116 View Code Duplication
    public function actionDelete($id)
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...
117
    {
118
        $model = $this->findModel($id);
119
        $post = $model->commentPost;
120
121
        if ($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...
122
            if (!$model->parent) {
123
                $post->updateAttributes(['comment_count', --$post->comment_count]);
124
            }
125
            PostComment::deleteAll(['parent' => $model->id]);
126
        }
127
128
        return $this->redirect(['index', 'posttype' => $post->type]);
129
    }
130
131
    /**
132
     * Bulk action for PostComment triggered when button 'Apply' clicked.
133
     * The action depends on the value of the dropdown next to the button.
134
     * Only accept POST HTTP method.
135
     */
136 View Code Duplication
    public function actionBulkAction()
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...
137
    {
138
        if (Yii::$app->request->post('action') === PostComment::STATUS_APPROVED) {
139
            foreach (Yii::$app->request->post('ids', []) as $id) {
140
                $this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_APPROVED]);
0 ignored issues
show
The method updateAttributes 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...
141
            }
142
        } elseif (Yii::$app->request->post('action') === PostComment::STATUS_NOT_APPROVED) {
143
            foreach (Yii::$app->request->post('ids', []) as $id) {
144
                $this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_NOT_APPROVED]);
0 ignored issues
show
The method updateAttributes 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...
145
            }
146
        } elseif (Yii::$app->request->post('action') === PostComment::STATUS_TRASHED) {
147
            foreach (Yii::$app->request->post('ids', []) as $id) {
148
                $this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_TRASHED]);
0 ignored issues
show
The method updateAttributes 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...
149
            }
150
        } elseif (Yii::$app->request->post('action') === 'delete') {
151
            foreach (Yii::$app->request->post('ids', []) as $id) {
152
                $model = $this->findModel($id);
153
                $post = $model->commentPost;
154
                if ($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...
155
                    if (!$model->parent) {
156
                        $post->updateAttributes(['comment_count', --$post->comment_count]);
157
                    }
158
                    PostComment::deleteAll(['parent' => $model->id]);
159
                }
160
            }
161
        }
162
    }
163
164
    /**
165
     * Reply an existing PostComment model.
166
     * If reply is successful, the browser will be redirected to 'update' page.
167
     *
168
     * @param int $id Find PostComment model based on id as parent.
169
     * @return string
170
     */
171
    public function actionReply($id)
172
    {
173
        $commentParent = $this->findModel($id);
174
        $model = new PostComment(['scenario' => 'reply']);
175
176
        if ($model->load(Yii::$app->request->post())) {
177
            $model->post_id = $commentParent->post_id;
178
            $model->parent = $commentParent->id;
179
            if ($model->save()) {
180
                $this->redirect(['post-comment/update', 'id' => $model->id]);
181
            }
182
        }
183
184
        return $this->render('reply', [
185
            'commentParent' => $commentParent,
186
            'model' => $model,
187
        ]);
188
    }
189
190
    /**
191
     * Finds the PostComment model based on its primary key value.
192
     * If the model is not found, a 404 HTTP exception will be thrown.
193
     *
194
     * @param integer $id
195
     * @return PostComment the loaded model
196
     * @throws NotFoundHttpException if the model cannot be found
197
     */
198
    protected function findModel($id)
199
    {
200
        if (($model = PostComment::findOne($id)) !== null) {
201
            return $model;
202
        }
203
204
        throw new NotFoundHttpException('The requested page does not exist.');
205
    }
206
207
    /**
208
     * Finds the PostType model based on its primary key value.
209
     * If the model is not found, a 404 HTTP exception will be thrown.
210
     *
211
     * @param integer $id
212
     * @return PostType the loaded model
213
     * @throws NotFoundHttpException if the model cannot be found
214
     */
215
    protected function findPostType($id)
216
    {
217
        if (($model = PostType::findOne($id)) !== null) {
218
            return $model;
219
        }
220
221
        throw new NotFoundHttpException('The requested page does not exist.');
222
    }
223
224
225
    /**
226
     * Finds the Post model based on its primary key value.
227
     * If the model is not found, a 404 HTTP exception will be thrown.
228
     *
229
     * @param integer $id
230
     * @return Post the loaded model
231
     * @throws NotFoundHttpException if the model cannot be found
232
     */
233
    protected function findPost($id)
234
    {
235
        if (($model = Post::findOne($id)) !== null) {
236
            return $model;
237
        }
238
239
        throw new NotFoundHttpException('The requested page does not exist.');
240
    }
241
}
242