Issues (7)

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/traits/XEditableTrait.php (4 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
 * X-editable extension for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-x-editable
6
 * @package   yii2-x-editable
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\xeditable\traits;
12
13
use hipanel\helpers\ArrayHelper;
14
use hiqdev\xeditable\assets\XEditableAsset;
15
use vova07\select2\Select2Asset;
16
use Yii;
17
use yii\helpers\Html;
18
use yii\helpers\Json;
19
use yii\helpers\Url;
20
21
/**
22
 * Class XEditableTrait.
23
 */
24
trait XEditableTrait
25
{
26
    /**
27
     * @var array
28
     */
29
    public $pluginOptions = [];
30
31
    /**
32
     * @var string library to be used: bootstrap, jqueryui, plain jquery
33
     */
34
    public $library;
35
36
    /**
37
     * @var array will be passed to [[Html::a]] method as options
38
     */
39
    public $linkOptions = [];
40
41
    public function registerAssets()
42
    {
43
        $asset = new XEditableAsset();
44
        if ($this->library) {
45
            $asset->library = $this->library;
46
        }
47
        $asset->register(Yii::$app->view);
48
        if ($this->pluginOptions['type'] === 'select2') {
49
            Select2Asset::register(Yii::$app->view);
50
        }
51
    }
52
53
    public function init()
54
    {
55
        parent::init();
56
        $this->registerAssets();
57
    }
58
59
    public function registerMyJs($data)
60
    {
61
        if (!$this->pluginOptions['title']) {
62
            $this->pluginOptions['title'] = $data['model']->getAttributeLabel($data['attribute']);
63
        }
64
        if ($this->pluginOptions['url']) {
65
            $this->pluginOptions['url'] = Url::to($this->pluginOptions['url']);
66
        }
67
        $selector = ArrayHelper::remove($this->pluginOptions, 'selector', ".editable[data-pk={$data['model']->primaryKey}][data-name={$data['attribute']}]");
68
        Yii::$app->view->registerJs('$("' . $selector . '").editable(' . Json::htmlEncode($this->pluginOptions) . ');');
69
    }
70
71
    public function prepareValue($data)
72
    {
73
        if ($data['value'] !== null) {
74
            if ($data['value'] instanceof \Closure) {
75
                $value = call_user_func($data['value'], $this->model, $this);
0 ignored issues
show
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76
            } else {
77
                $value = $data['value'];
78
            }
79
        } else {
80
            $value = $data['model']->getAttribute($data['attribute']);
81
            if (is_array($value)) {
82
                if (ArrayHelper::isAssociative($value)) {
83
                    $value = array_keys($value);
84
                }
85
            } else {
86
                $value = [$value];
87
            }
88
        }
89
90
        $this->pluginOptions['value'] = $value;
91
    }
92
93
    protected function prepareDataValue($data)
94
    {
95 View Code Duplication
        if (isset($this->pluginOptions['data-value'])) {
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...
96
            if ($this->pluginOptions['data-value'] instanceof \Closure) {
97
                $result = call_user_func($this->pluginOptions['data-value'], $this, $data);
98
            } else {
99
                $result = $this->pluginOptions['data-value'];
100
            }
101
        } else {
102
            $result = $this->pluginOptions['value'];
103
        }
104
105
        $this->pluginOptions['data-value'] = $result;
106
    }
107
108
    protected function prepareDisplayValue($data)
109
    {
110 View Code Duplication
        if (isset($this->pluginOptions['data-display-value'])) {
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...
111
            if ($this->pluginOptions['data-display-value'] instanceof \Closure) {
112
                $result = call_user_func($this->pluginOptions['data-display-value'], $this, $data);
113
            } else {
114
                $result = $this->pluginOptions['data-display-value'];
115
            }
116
        } else {
117
            $result = $this->pluginOptions['value'];
118
        }
119
120
        if (is_array($result)) {
121
            if ($this->pluginOptions['source']) {
122
                $result = ArrayHelper::getValues($this->pluginOptions['source'], $result);
123
            }
124
125
            $result = implode('<br />', $result);
126
        }
127
128
        $this->pluginOptions['data-display-value'] = $result;
129
    }
130
131
    public function prepareHtml($data)
132
    {
133
        $this->prepareValue($data);
134
        $this->prepareDataValue($data);
135
        $this->prepareDisplayValue($data);
136
        $this->registerMyJs($data);
137
138
        $params = ArrayHelper::merge([
139
            'id' => $this->getId(),
0 ignored issues
show
It seems like getId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
140
            'class' => 'editable',
141
            'data-pk' => $data['model']->primaryKey,
142
            'data-name' => $data['attribute'],
143
            'data-value' => $this->pluginOptions['data-value'],
144
        ], $this->linkOptions);
145
146
        return Html::a($this->pluginOptions['data-display-value'], '#', $params);
147
    }
148
}
149