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/MediaController.php (7 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\MediaUploadHandler;
11
use common\models\Media;
12
use common\models\Option;
13
use common\models\search\Media as MediaSearch;
14
use Yii;
15
use yii\filters\AccessControl;
16
use yii\filters\VerbFilter;
17
use yii\helpers\ArrayHelper;
18
use yii\web\Controller;
19
use yii\web\ForbiddenHttpException;
20
use yii\web\NotFoundHttpException;
21
22
/**
23
 * MediaController, controlling the actions for for Media model.
24
 *
25
 * @author Agiel K. Saputra <[email protected]>
26
 * @since 0.1.0
27
 */
28
class MediaController extends Controller
29
{
30
    /**
31
     * @inheritdoc
32
     */
33
    public function behaviors()
34
    {
35
        return [
36
            'access' => [
37
                'class' => AccessControl::className(),
38
                'rules' => [
39
                    [
40
                        'actions' => [
41
                            'index',
42
                            'create',
43
                            'update',
44
                            'delete',
45
                            'bulk-action',
46
                            'ajax-upload',
47
                            'ajax-update',
48
                            'ajax-delete',
49
                        ],
50
                        'allow' => true,
51
                        'roles' => ['author'],
52
                    ],
53
                ],
54
            ],
55
            'verbs' => [
56
                'class' => VerbFilter::className(),
57
                'actions' => [
58
                    'delete' => ['post'],
59
                    'bulk-action' => ['post'],
60
                    'ajax-upload' => ['post'],
61
                    'ajax-update' => ['post'],
62
                    'ajax-delete' => ['post'],
63
                ],
64
            ],
65
        ];
66
    }
67
68
    /**
69
     * Lists all Media models.
70
     *
71
     * @return mixed
72
     */
73 View Code Duplication
    public function actionIndex()
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...
74
    {
75
        $searchModel = new MediaSearch();
76
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
77
78
        return $this->render('index', [
79
            'searchModel' => $searchModel,
80
            'dataProvider' => $dataProvider,
81
        ]);
82
    }
83
84
85
    /**
86
     * Creates a new Media model.
87
     * If creation is successful, the browser will display thumbnail and options to the 'create' page.
88
     *
89
     * @return mixed
90
     */
91
    public function actionCreate()
92
    {
93
        return $this->render('create', ['model' => new Media(['scenario' => 'upload'])]);
94
    }
95
96
    /**
97
     * Updates an existing Media model.
98
     * If update is successful, the browser will be redirected to the 'view' page.
99
     *
100
     * @param integer $id
101
     * @return mixed
102
     * @throws ForbiddenHttpException
103
     * @throws NotFoundHttpException
104
     */
105
    public function actionUpdate($id)
106
    {
107
        $model = $this->findModel($id);
108
        $this->getPermission($model);
109
        $metadata = $model->getMeta('metadata');
0 ignored issues
show
The method getMeta 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...
110
111
        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...
112
            $model->date = date('Y-m-d H:i:s', strtotime($model->date));
113
            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...
114
                Yii::$app->getSession()->setFlash('success', Yii::t('writesdown', 'Media 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...
115
116
                return $this->redirect(['update', 'id' => $id]);
117
            }
118
        }
119
120
        return $this->render('update', [
121
            'model' => $model,
122
            'metadata' => $metadata,
123
        ]);
124
    }
125
126
    /**
127
     * Deletes an existing Media model.
128
     * If deletion is successful, the browser will be redirected to the 'index' page.
129
     *
130
     * @param integer $id
131
     * @return mixed
132
     * @throws ForbiddenHttpException
133
     * @throws NotFoundHttpException
134
     */
135
    public function actionDelete($id)
136
    {
137
        $this->getPermission($this->findModel($id));
138
        $uploadHandler = new MediaUploadHandler(null, false);
139
        $uploadHandler->delete($id, MediaUploadHandler::NOT_PRINT_RESPONSE);
140
141
        return $this->redirect(['index']);
142
    }
143
144
    /**
145
     * Bulk action for Media triggered when button 'Apply' clicked.
146
     * The action depends on the value of the dropdown next to the button.
147
     * Only accept POST HTTP method.
148
     */
149
    public function actionBulkAction()
150
    {
151
        if (Yii::$app->request->post('action') == 'delete') {
152
            foreach (Yii::$app->request->post('ids', []) as $id) {
153
                $this->getPermission($this->findModel($id));
154
                $uploadHandler = new MediaUploadHandler(null, false);
155
                $uploadHandler->delete($id, MediaUploadHandler::NOT_PRINT_RESPONSE);
156
            }
157
        }
158
    }
159
160
    /**
161
     * Upload media file and store it to database.
162
     * Media versions can be set from application params.
163
     *
164
     * @return array
165
     */
166
    public function actionAjaxUpload()
167
    {
168
        $versions = [
169
            'large' => [
170
                'max_width' => Option::get('large_width'),
171
                'max_height' => Option::get('large_height'),
172
            ],
173
            'medium' => [
174
                'max_width' => Option::get('medium_width'),
175
                'max_height' => Option::get('medium_height'),
176
            ],
177
            'thumbnail' => [
178
                'max_width' => Option::get('thumbnail_width'),
179
                'max_height' => Option::get('thumbnail_height'),
180
                'crop' => 1,
181
            ],
182
        ];
183
184 View Code Duplication
        if($userVersions = ArrayHelper::getValue(Yii::$app->params, 'media.versions', [])){
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...
185
            $versions = ArrayHelper::merge($versions, $userVersions);
186
        }
187
188
        $uploadHandler = new MediaUploadHandler([
189
            'versions' => $versions,
190
            'user_dirs' => Option::get('uploads_username_based'),
191
            'year_month_dirs' => Option::get('uploads_yearmonth_based'),
192
        ], MediaUploadHandler::NOT_PRINT_RESPONSE);
193
        $uploadHandler->post();
194
    }
195
196
    /**
197
     * Update attributes of Media model via AJAX request.
198
     */
199
    public function actionAjaxUpdate()
200
    {
201
        if ($model = $this->findModel(Yii::$app->request->post('id'))) {
202
            $this->getPermission($model);
203
            $model->{Yii::$app->request->post('attribute')} = Yii::$app->request->post('value');
204
            $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...
205
        }
206
    }
207
208
    /**
209
     * Delete Media model and its files based on media primary key.
210
     *
211
     * @param $id
212
     * @return array
213
     * @throws ForbiddenHttpException
214
     * @throws NotFoundHttpException
215
     */
216
    public function actionAjaxDelete($id)
217
    {
218
        $this->getPermission($this->findModel($id));
219
        $uploadHandler = new MediaUploadHandler(null, MediaUploadHandler::NOT_PRINT_RESPONSE);
220
        $uploadHandler->delete($id);
221
    }
222
223
    /**
224
     * Get permission to access model by current user.
225
     * If the user does not obtain the permission, a 403 exeption will be thrown.
226
     *
227
     * @param $model Media
228
     * @throws ForbiddenHttpException
229
     */
230
    public function getPermission($model)
231
    {
232
        if (!$model->getPermission()) {
233
            throw new ForbiddenHttpException(Yii::t('writesdown', 'You are not allowed to perform this action.'));
234
        }
235
    }
236
237
    /**
238
     * Finds the Media model based on its primary key value.
239
     * If the model is not found, a 404 HTTP exception will be thrown.
240
     *
241
     * @param integer $id
242
     * @return Media the loaded model
243
     * @throws NotFoundHttpException if the model cannot be found
244
     */
245
    protected function findModel($id)
246
    {
247
        if (($model = Media::findOne($id)) !== null) {
248
            return $model;
249
        }
250
251
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
252
    }
253
}
254