Issues (4)

Security Analysis    no request data  

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.

controllers/commands/Alias.php (1 issue)

Labels
Severity

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
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\cli\controllers\commands;
11
12
use gplcart\core\interfaces\Crud as CrudInterface;
13
use gplcart\core\models\Alias as AliasModel;
14
use gplcart\modules\cli\controllers\Command;
15
16
17
/**
18
 * Handles commands related to URL aliases
19
 */
20
class Alias extends Command
21
{
22
23
    /**
24
     * Alias model instance
25
     * @var \gplcart\core\models\Alias $alias
26
     */
27
    protected $alias;
28
29
    /**
30
     * @param AliasModel $alias
31
     */
32
    public function __construct(AliasModel $alias)
33
    {
34
        parent::__construct();
35
36
        $this->alias = $alias;
37
    }
38
39
    /**
40
     * Callback for "alias-get" command
41
     */
42
    public function cmdGetAlias()
43
    {
44
        $result = $this->getListAlias();
45
        $this->outputFormat($result);
46
        $this->outputFormatTableAlias($result);
47
        $this->output();
48
    }
49
50
    /**
51
     * Callback for "alias-add" command
52
     */
53
    public function cmdAddAlias()
54
    {
55
        $params = $this->getParam();
56
57
        if (count($params) != 3) {
58
            $this->errorAndExit($this->text('Invalid command'));
59
        }
60
61
        list($entity, $entity_id, $alias) = $params;
62
63
        if (empty($entity) || empty($entity_id) || empty($alias) || !is_numeric($entity_id)) {
64
            $this->errorAndExit($this->text('Invalid argument'));
65
        }
66
67
        if (!$this->alias->loadEntity($entity, $entity_id)) {
68
            $this->errorAndExit($this->text('Invalid argument'));
69
        }
70
71
        $result = $this->addAlias($entity, $entity_id, $alias);
72
73
        if (empty($result)) {
74
            $this->errorAndExit($this->text('Unexpected result'));
75
        }
76
77
        $this->line($result);
78
        $this->output();
79
    }
80
81
    /**
82
     * Callback for "alias-generate" command
83
     */
84
    public function cmdGenerateAlias()
85
    {
86
        $entity = $this->getParam(0);
87
        $entity_id = $this->getParam(1);
88
        $all = $this->getParam('all');
89
90
        if (empty($entity)) {
91
            $this->errorAndExit($this->text('Invalid command'));
92
        }
93
94
        if (!isset($entity_id) && empty($all)) {
95
            $this->errorAndExit($this->text('Invalid command'));
96
        }
97
98
        $result = false;
99
100
        if (isset($entity_id)) {
101
102
            if (!is_numeric($entity_id)) {
103
                $this->errorAndExit($this->text('Invalid argument'));
104
            }
105
106
            $data = $this->alias->loadEntity($entity, $entity_id);
107
108
            if (empty($data)) {
109
                $this->errorAndExit($this->text('Invalid argument'));
110
            }
111
112
            $alias = $this->alias->generateEntity($entity, $data);
113
114
            if (empty($alias)) {
115
                $this->errorAndExit($this->text('Unexpected result'));
116
            }
117
118
            $result = $this->addAlias($entity, $entity_id, $alias);
119
120
        } else if (!empty($all)) {
121
            $result = $this->generateListAlias($entity);
122
        }
123
124
        if (empty($result)) {
125
            $this->errorAndExit($this->text('Unexpected result'));
126
        }
127
128
        $this->output();
129
    }
130
131
    /** Add a URL alias
132
     * @param string $entity
133
     * @param int $entity_id
134
     * @param string $alias
135
     * @return int
136
     */
137
    protected function addAlias($entity, $entity_id, $alias)
138
    {
139
        $this->alias->delete(array('entity' => $entity, 'entity_id' => $entity_id));
140
141
        $data = array(
142
            'alias' => $alias,
143
            'entity' => $entity,
144
            'entity_id' => $entity_id
145
        );
146
147
        return $this->alias->add($data);
148
    }
149
150
    /**
151
     * Mass generate URL aliases for the given entity
152
     * @param string $entity
153
     * @return bool
154
     */
155
    protected function generateListAlias($entity)
156
    {
157
        if (!$this->alias->isSupportedEntity($entity)) {
158
            $this->errorAndExit($this->text('Invalid argument'));
159
        }
160
161
        $model = gplcart_instance_model($entity);
162
163
        if (!$model instanceof CrudInterface) {
0 ignored issues
show
The class gplcart\core\interfaces\Crud does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
164
            $this->errorAndExit($this->text('Invalid argument'));
165
        }
166
167
        ini_set('memory_limit', '-1');
168
169
        $added = $count = 0;
170
171
        foreach ($model->getList() as $item) {
172
173
            if (empty($item["{$entity}_id"])) {
174
                continue;
175
            }
176
177
            $count++;
178
            $entity_id = $item["{$entity}_id"];
179
180
            $this->alias->delete(array('entity' => $entity, 'entity_id' => $entity_id));
181
182
            $data = array(
183
                'entity' => $entity,
184
                'entity_id' => $entity_id,
185
                'alias' => $this->alias->generateEntity($entity, $item)
186
            );
187
188
            if ($this->alias->add($data)) {
189
                $added++;
190
            }
191
        }
192
193
        return $count && $count == $added;
194
    }
195
196
197
    /**
198
     * Callback for "alias-delete" command
199
     */
200
    public function cmdDeleteAlias()
201
    {
202
        $id = $this->getParam(0);
203
        $all = $this->getParam('all');
204
205
        if (!isset($id) && !$all) {
206
            $this->errorAndExit($this->text('Invalid command'));
207
        }
208
209
        $options = $result = null;
210
211
        if (isset($id)) {
212
213
            if ($this->getParam('entity')) {
214
                $options = array('entity' => $id);
215
            }
216
217
        } else if (!empty($all)) {
218
            $options = array();
219
        }
220
221
        if (isset($options)) {
222
223
            $deleted = $count = 0;
224
            foreach ($this->alias->getList($options) as $item) {
225
                $count++;
226
                $deleted += (int) $this->alias->delete($item['alias_id']);
227
            }
228
229
            $result = $count && $count == $deleted;
230
231
        } else if (isset($id)) {
232
233
            if (!is_numeric($id)) {
234
                $this->errorAndExit($this->text('Invalid argument'));
235
            }
236
237
            $result = $this->alias->delete($id);
238
        }
239
240
        if (empty($result)) {
241
            $this->errorAndExit($this->text('Unexpected result'));
242
        }
243
244
        $this->output();
245
    }
246
247
    /**
248
     * Returns an array of URL aliases
249
     * @return array
250
     */
251
    protected function getListAlias()
252
    {
253
        $id = $this->getParam(0);
254
255
        if (!isset($id)) {
256
            return $this->alias->getList(array('limit' => $this->getLimit()));
257
        }
258
259
        if ($this->getParam('entity')) {
260
            return $this->alias->getList(array('entity' => $id, 'limit' => $this->getLimit()));
261
        }
262
263
        if (!is_numeric($id)) {
264
            $this->errorAndExit($this->text('Invalid argument'));
265
        }
266
267
        $result = $this->alias->get($id);
268
269
        if (empty($result)) {
270
            $this->errorAndExit($this->text('Unexpected result'));
271
        }
272
273
        return array($result);
274
    }
275
276
    /**
277
     * Output table format
278
     * @param array $items
279
     */
280
    protected function outputFormatTableAlias(array $items)
281
    {
282
        $header = array(
283
            $this->text('ID'),
284
            $this->text('Alias'),
285
            $this->text('Entity'),
286
            $this->text('Entity ID')
287
        );
288
289
        $rows = array();
290
291
        foreach ($items as $item) {
292
            $rows[] = array(
293
                $item['alias_id'],
294
                $item['alias'],
295
                $item['entity'],
296
                $item['entity_id']
297
            );
298
        }
299
300
        $this->outputFormatTable($rows, $header);
301
    }
302
303
}
304