Issues (62)

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.

src/helpers/CommentsHelper.php (10 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
namespace ogheo\comments\helpers;
4
5
use Yii;
6
use yii\helpers\Url;
7
use yii\helpers\Json;
8
use ogheo\comments\Module as CommentsModule;
9
use yii\web\BadRequestHttpException;
10
11
/**
12
 * Class CommentsHelper
13
 * @package ogheo\comments\helpers
14
 */
15
class CommentsHelper
16
{
17
    /**
18
     * Encode comment id
19
     * @param $id
20
     * @return string
21
     */
22
    public static function encodeId($id)
23
    {
24
        return base_convert($id, 10, 36);
25
    }
26
27
    /**
28
     * Decode comment id
29
     * @param $id
30
     * @return string
31
     */
32
    public static function decodeId($id)
33
    {
34
        return base_convert($id, 36, 10);
35
    }
36
37
    /**
38
     * Encrypt data
39
     * @param $decryptedData
40
     * @return string
41
     */
42
    public static function encryptData($decryptedData)
43
    {
44
        return utf8_encode(
45
            Yii::$app->getSecurity()->encryptByKey(
46
                Json::encode($decryptedData), CommentsModule::getInstance()->id
47
            )
48
        );
49
    }
50
51
    /**
52
     * Decrypt data
53
     * @param $encryptedData
54
     * @return mixed
55
     * @throws BadRequestHttpException
56
     */
57
    public static function decryptData($encryptedData)
58
    {
59
        $decryptedData = Yii::$app->getSecurity()->decryptByKey(
60
            utf8_decode($encryptedData), CommentsModule::getInstance()->id
61
        );
62
63
        if ($decryptedData !== false) {
64
            return Json::decode($decryptedData);
65
        }
66
67
        throw new BadRequestHttpException(Yii::t('comments', 'Sorry, something went wrong. Please try again later.'));
68
    }
69
70
    /**
71
     * Set username in session and cookies
72
     * @param $username
73
     */
74 View Code Duplication
    public static function setUsername($username)
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...
75
    {
76
        Yii::$app->session[CommentsModule::getInstance()->guestUsernameSessionKey] = $username;
77
        if (CommentsModule::getInstance()->guestCookieDuration) {
78
            $cookies = Yii::$app->response->cookies;
79
            $cookies->add(new \yii\web\Cookie([
80
                'name' => CommentsModule::getInstance()->guestUsernameCookieName,
81
                'expire' => time() + (int)CommentsModule::getInstance()->guestCookieDuration,
82
                'value' => $username,
83
            ]));
84
        }
85
    }
86
87
    /**
88
     * Get username from session or cookies
89
     * @return mixed|null
90
     */
91 View Code Duplication
    public static function getUsername()
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...
92
    {
93
        $username = Yii::$app->session->get(CommentsModule::getInstance()->guestUsernameSessionKey);
94
95
        if ($username === null) {
96
            $cookies = Yii::$app->request->cookies;
97
            if ($username === null && $cookies->has(CommentsModule::getInstance()->guestUsernameCookieName)) {
98
                $username = !empty($cookies[CommentsModule::getInstance()->guestUsernameCookieName]->value) ?
99
                    $cookies[CommentsModule::getInstance()->guestUsernameCookieName]->value : null;
100
            }
101
        }
102
103
        return $username;
104
    }
105
106
    /**
107
     * Set email in session and cookies
108
     * @param $email
109
     */
110 View Code Duplication
    public static function setEmail($email)
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...
111
    {
112
        Yii::$app->session[CommentsModule::getInstance()->guestEmailSessionKey] = $email;
113
        if (CommentsModule::getInstance()->guestCookieDuration) {
114
            $cookies = Yii::$app->response->cookies;
115
            $cookies->add(new \yii\web\Cookie([
116
                'name' => CommentsModule::getInstance()->guestEmailCookieName,
117
                'expire' => time() + (int)CommentsModule::getInstance()->guestCookieDuration,
118
                'value' => $email,
119
            ]));
120
        }
121
    }
122
123
    /**
124
     * Get email from session or cookies
125
     * @return mixed|null
126
     */
127 View Code Duplication
    public static function getEmail()
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...
128
    {
129
        $email = Yii::$app->session->get(CommentsModule::getInstance()->guestEmailSessionKey);
130
131
        if ($email === null) {
132
            $cookies = Yii::$app->request->cookies;
133
            if ($email === null && $cookies->has(CommentsModule::getInstance()->guestEmailCookieName)) {
134
                $email = !empty($cookies[CommentsModule::getInstance()->guestEmailCookieName]->value) ?
135
                    $cookies[CommentsModule::getInstance()->guestEmailCookieName]->value : null;
136
            }
137
        }
138
139
        return $email;
140
    }
141
142
    /**
143
     * Get uprated comments
144
     * @return null|string
145
     */
146 View Code Duplication
    public static function getUprated()
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...
147
    {
148
        $cookies = Yii::$app->request->cookies;
149
        if ($cookies->has(CommentsModule::getInstance()->upRatedCookieName)) {
150
            return !empty($cookies[CommentsModule::getInstance()->upRatedCookieName]->value) ?
151
                $cookies[CommentsModule::getInstance()->upRatedCookieName]->value : null;
152
        }
153
154
        return null;
155
    }
156
157
    /**
158
     * Check if comment is rated by user
159
     * @param $id
160
     * @return bool
161
     */
162
    public static function isUprated($id)
163
    {
164
        $uprated = self::getUprated();
165
        $uprated_arr = explode(',', $uprated);
166
        if (in_array($id, $uprated_arr)) {
167
            return true;
168
        }
169
170
        return false;
171
    }
172
173
    /**
174
     * Set uprated comment
175
     * @param $id
176
     */
177 View Code Duplication
    public static function setUprated($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...
178
    {
179
        if (CommentsModule::getInstance()->ratingCookieDuration) {
180
            $cookies = Yii::$app->response->cookies;
181
            $uprated = self::getUprated();
182
183
            if ($uprated === null) {
184
                $uprated = $id;
185
            } else {
186
                $uprated_arr = explode(',', $uprated);
187
                if (!in_array($id, $uprated_arr)) {
188
                    array_push($uprated_arr, $id);
189
                }
190
191
                $uprated = implode(',', $uprated_arr);
192
            }
193
194
            $cookies->add(new \yii\web\Cookie([
195
                'name' => CommentsModule::getInstance()->upRatedCookieName,
196
                'expire' => time() + (int)CommentsModule::getInstance()->ratingCookieDuration,
197
                'value' => $uprated,
198
            ]));
199
        }
200
    }
201
202
    /**
203
     * Delete uprated comment
204
     * @param $id
205
     */
206 View Code Duplication
    public static function deleteUprated($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...
207
    {
208
        if (CommentsModule::getInstance()->ratingCookieDuration) {
209
            $cookies = Yii::$app->response->cookies;
210
            $uprated = self::getUprated();
211
212
            $uprated_arr = explode(',', $uprated);
213
            if (($key = array_search($id, $uprated_arr)) !== false) {
214
                unset($uprated_arr[$key]);
215
            }
216
217
            $uprated = implode(',', $uprated_arr);
218
            $cookies->add(new \yii\web\Cookie([
219
                'name' => CommentsModule::getInstance()->upRatedCookieName,
220
                'expire' => time() + (int)CommentsModule::getInstance()->ratingCookieDuration,
221
                'value' => $uprated,
222
            ]));
223
        }
224
    }
225
226
    /**
227
     * Get downrated comments
228
     * @return null|string
229
     */
230 View Code Duplication
    public static function getDownrated()
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...
231
    {
232
        $cookies = Yii::$app->request->cookies;
233
        if ($cookies->has(CommentsModule::getInstance()->downRatedCookieName)) {
234
            return !empty($cookies[CommentsModule::getInstance()->downRatedCookieName]->value) ?
235
                $cookies[CommentsModule::getInstance()->downRatedCookieName]->value : null;
236
        }
237
238
        return null;
239
    }
240
241
    /**
242
     * Check if comment is downrated by user
243
     * @param $id
244
     * @return bool
245
     */
246
    public static function isDownrated($id)
247
    {
248
        $downrated = self::getDownrated();
249
        $downrated_arr = explode(',', $downrated);
250
        if (in_array($id, $downrated_arr)) {
251
            return true;
252
        }
253
254
        return false;
255
    }
256
257
    /**
258
     * Set downrated comment
259
     * @param $id
260
     */
261 View Code Duplication
    public static function setDownrated($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...
262
    {
263
        if (CommentsModule::getInstance()->ratingCookieDuration) {
264
            $cookies = Yii::$app->response->cookies;
265
            $downrated = self::getDownrated();
266
267
            if ($downrated === null) {
268
                $downrated = $id;
269
            } else {
270
                $downrated_arr = explode(',', $downrated);
271
                if (!in_array($id, $downrated_arr)) {
272
                    array_push($downrated_arr, $id);
273
                }
274
275
                $downrated = implode(',', $downrated_arr);
276
            }
277
278
            $cookies->add(new \yii\web\Cookie([
279
                'name' => CommentsModule::getInstance()->downRatedCookieName,
280
                'expire' => time() + (int)CommentsModule::getInstance()->ratingCookieDuration,
281
                'value' => $downrated,
282
            ]));
283
        }
284
    }
285
286
    /**
287
     * Delete downrated comment
288
     * @param $id
289
     */
290 View Code Duplication
    public static function deleteDownrated($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...
291
    {
292
        if (CommentsModule::getInstance()->ratingCookieDuration) {
293
            $cookies = Yii::$app->response->cookies;
294
            $downrated = self::getDownrated();
295
296
            $downrated_arr = explode(',', $downrated);
297
            if (($key = array_search($id, $downrated_arr)) !== false) {
298
                unset($downrated_arr[$key]);
299
            }
300
301
            $downrated = implode(',', $downrated_arr);
302
            $cookies->add(new \yii\web\Cookie([
303
                'name' => CommentsModule::getInstance()->downRatedCookieName,
304
                'expire' => time() + (int)CommentsModule::getInstance()->ratingCookieDuration,
305
                'value' => $downrated,
306
            ]));
307
        }
308
    }
309
310
    /**
311
     * Build comments tree
312
     * @param $comments
313
     * @param int $parentId
314
     * @return array
315
     */
316
    public static function buildCommentsTree(&$comments, $parentId = 0)
317
    {
318
        $tree = [];
319
320
        foreach ($comments as &$comment) {
321
            if ($comment->parent_id == $parentId) {
322
                $children = self::buildCommentsTree($comments, $comment->id);
323
                if ($children) {
324
                    $comment->children = $children;
325
                }
326
                $tree[$comment->id] = $comment;
327
                unset($comment);
328
            }
329
        }
330
331
        return $tree;
332
    }
333
334
    /**
335
     * Get cache properties
336
     * @param $tag
337
     * @param int $duration
338
     * @return array
339
     */
340
    public static function getCacheProperties($tag, $duration = 3600)
341
    {
342
        return [
343
            'duration' => $duration,
344
            'variations' => [
345
                Yii::$app->language,
346
                Url::current()
347
            ],
348
            'dependency' => [
349
                'class' => 'yii\caching\TagDependency',
350
                'tags' => $tag
351
            ]
352
        ];
353
    }
354
}