Issues (12)

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.

src/ElasticSearch.php (12 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 Soupmix;
4
/*
5
Elasticsearch Adapter
6
*/
7
Use Elasticsearch\Client;
8
9
final class ElasticSearch implements Base
10
{
11
    private $conn = null;
12
    private $index = null;
13
    private $esMajorVersion = 2;
14
    private static $filteredOrBool = [2 => 'filtered', 5 => 'bool', 6 => 'bool'];
15
16
    private static $operators = [
17
        'range'     => ['gt', 'gte', 'lt', 'lte'],
18
        'standart'  => ['prefix', 'regexp', 'wildcard'],
19
        'BooleanQueryLevel' => ['not'],
20
        'special'   => ['in']
21
    ];
22 3
23
    public function __construct($config, Client $client)
24 3
    {
25 3
        $this->index = $config['db_name'];
26 3
        $this->conn = $client;
27
        $this->esMajorVersion = substr($client->info()['version']['number'],0,1);
0 ignored issues
show
Documentation Bug introduced by
The property $esMajorVersion was declared of type integer, but substr($client->info()['...sion']['number'], 0, 1) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
28 3
    }
29
30 3
31 3
    public function getConnection()
32
    {
33
        return $this->conn;
34
    }
35
36
    public function create(string $collection, array $fields)
37 3
    {
38
        $params = ['index' => $this->index];
39 3
        try {
40
            if (!$this->conn->indices()->exists($params)) {
41 3
                $this->conn->indices()->create($params);
42 3
            }
43
            $params['type'] = $collection;
44 1
            $params['body'] = [$collection => ['properties' => $fields]];
45
            $this->conn->indices()->putMapping($params);
46 2
        } catch (\Exception $e) {
47
            // This ignore the error
48
            return false;
49
        }
50
        return true;
51
    }
52
53
    public function drop(string $collection)
54
    {
55
        $params = ['index' => $this->index, 'type' => $collection];
0 ignored issues
show
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
        try {
0 ignored issues
show
This try statement is empty and can be removed.

This check looks for try blocks that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

If there is nothing in the try then the catch block can never be executed either. Thus, these try statements can be removed completely.

Loading history...
57 2
            //$this->truncate($collection);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
            //$mappingData = $this->conn->indices()->getMapping($params);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59 2
            //$mapping = $mappingData[$this->index]['mappings'][$collection];
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60 2
            //$ma = $this->conn->indices()->deleteMapping($params);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61 2
62 2
        } catch (\Exception $e) {
0 ignored issues
show
catch (\Exception $e) { return false; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
63
            // This ignore the error
64 2
            return false;
65 2
        }
66 2
        return true;
67
    }
68
69
    public function truncate(string $collection)
70
    {
71
        $params = ['index' => $this->index, 'type' => $collection, 'body' => ['query' => ['bool' =>['match_all' => []]]]];
72
        $this->conn->deleteByQuery($params);
73
    }
74 1
75
    public function createIndexes(string $collection, array $indexes)
76 1
    {}
77 1
78 1
    public function insert(string $collection, array $values)
79
    {
80
        $params = [];
81 1
        $params['body'] = $values;
82
        $params['index'] = $this->index;
83
        $params['type'] = $collection;
84
        try {
85
            $result = $this->conn->index($params);
86
        } catch (\Exception $e) {
87
            return;
88
        }
89
        if (
90
            array_key_exists('created', $result)
91
            || (array_key_exists('result', $result) && $result['result'] === 'created')
92
        ) {
93
            return $result['_id'];
94
        }
95
        return null;
96
    }
97
98
    public function get(string $collection, $docId)
99
    {
100
        $params = [];
101 1
        $params['index'] = $this->index;
102 1
        $params['type'] = $collection;
103
104 1
        try {
105 1
            if (is_array($docId)) {
106 1
                $params['body'] = [
107
                    'query' => [
108
                        self::$filteredOrBool[$this->esMajorVersion] => [
109
                            'filter' => [
110
                                'ids' => ['values' => $docId]
111
                            ],
112
                        ],
113
                    ],
114
                ];
115
                $results = $this->conn->search($params);
116
                if ($results['hits']['total'] === 0) {
117
                    return;
118
                }
119
                $result = [];
120
                foreach ($results['hits']['hits'] as $item){
121
                    $result[$item['_id']]=$item['_source'];
122
                }
123
                return $result;
124
            } else {
125
                $params['id'] = $docId;
126
                $result = $this->conn->get($params);
127
            }
128
            if ($result['found']) {
129
                $result['_source']['_id'] = $result['_id'];
130
                return $result['_source'];
131
            } else {
132
                return;
133
            }
134
        } catch (\Exception $e) {
135
            return;
136
        }
137
    }
138
139
    public function update(string $collection, array $filter, array $values)
140
    {
141 2
        $docs = $this->find($collection, $filter, ['_id']);
142
        if ($docs['total'] ===0) {
143 2
            return 0;
144 2
        }
145 2
        $params = [];
146 2
        $params['index'] = $this->index;
147 2
        $params['type'] = $collection;
148
        $modified_count = 0;
149 2
        foreach ($docs['data'] as $doc) {
150 2
            $params['id'] = $doc['_id'];
151
            $params['body']['doc'] = $values;
152
            try {
153 2
                $this->conn->update($params);
154 2
                ++$modified_count;
155
            } catch (\Exception $e) {
156
                // throw new \Exception($e->getMessage());
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
157
            }
158
        }
159
160
        return $modified_count;
161
    }
162
163
    public function delete(string $collection, array $filter)
164
    {
165
        if (isset($filter['_id'])) {
166
            $params = [];
167
            $params['index'] = $this->index;
168
            $params['type'] = $collection;
169
            $params['id'] = $filter['_id'];
170
            try {
171
                $result = $this->conn->delete($params);
172
            } catch (\Exception $e) {
173
                return 0;
174
            }
175
            if (
176
                array_key_exists('found', $result)
177
                || (array_key_exists('result', $result) && $result['result'] === 'deleted')
178
            ) {
179
                return 1;
180
            }
181 1
            return 0;
182
        }
183 1
        $params = [];
184 1
        $params['index'] = $this->index;
185 1
        $params['type'] = $collection;
186 1
        $params['fields'] = '_id';
187 1
        $results = $this->find($collection, $filter, ['_id'], null, 0, 10000);
188 1
        $deleteCount = 0;
189 1
        if ($results['total']>0) {
190
            $dParams = [];
191
            $dParams['index'] = $this->index;
192
            $dParams['type'] = $collection;
193 1
            foreach ($results['data'] as $result) {
194 1
                $dParams['id'] = $result['_id'];
195 1
                try {
196 1
                    $result = $this->conn->delete($dParams);
197
                } catch (\Exception $e) {
198 1
                    return 0;
199 1
                }
200 1
                if (
201
                    array_key_exists('found', $result)
202
                    || (array_key_exists('result', $result) && $result['result'] === 'deleted')
203
                ) {
204 1
                    $deleteCount++;
205
                }
206
            }
207
            return $deleteCount;
208
        }
209
        return 0;
210
    }
211
212
    public function find(
213 1
        string $collection,
214
            ?array $filters,
215
            ?array $fields = null,
216
            ?array $sort = null,
217 1
            int $start = 0,
218 1
            int$limit = 25
219 1
    ) : array
220
    {
221
        $return_type = '_source';
222 1
        $params = [];
223 1
        $params['index'] = $this->index;
224
        $params['type'] = $collection;
225
        $params['body'] = [];
226 1
        if ($filters!==null) {
227
            $filters = self::buildFilter($filters);
228
            $params['body'] = [
229
                'query' => [
230 1
                    self::$filteredOrBool[$this->esMajorVersion] => [
231 1
                        'filter' => [
232 1
                            'bool' => $filters,
233
                        ],
234
                    ],
235
                ],
236
            ];
237 1
        }
238 1
        $count = $this->conn->count($params);
239 1
        if ($sort !== null) {
240 1
            $params['body']['sort'] = [];
241
            foreach ($sort as $sort_key => $sort_dir) {
242
                $params['body']['sort'][] = [$sort_key => $sort_dir];
243
            }
244
        }
245
        if ($fields !== null) {
246
            $params['_source'] = $fields;
247
        }
248 1
        $params['from'] = (int) $start;
249
        $params['size'] = (int) $limit;
250 1
251 1
        $return = $this->conn->search($params);
252 1
253 1
        if ($return['hits']['total'] === 0) {
254 1
            return ['total' => 0, 'data' => null];
255 1
        }
256 1
        elseif ($limit === 1) {
257 1
            $return['hits']['hits'][0][$return_type]['_id'] = $return['hits']['hits'][0]['_id'];
258 1
            return ['total' => 1, 'data' => $return['hits']['hits'][0][$return_type]];
259 1
        }
260
        $result = [];
261 1
        foreach ($return['hits']['hits'] as $item) {
262 1
263
            if (($return_type === 'fields') && ($fields !== ['_id'])) {
264
                foreach ($item[$return_type]as $k => $v) {
265 1
                    $item[$return_type][$k] = $v[0];
266 1
                }
267 1
            }
268 1
            $item[$return_type]['_id'] = $item['_id'];
269 1
            $result[] = $item[$return_type];
270 1
        }
271 1
        return ['total' => $count['count'], 'data' => $result];
272 1
    }
273
274 1
    public function query(string $collection)
275 1
    {
276
        return new ElasticSearchQueryBuilder($collection);
0 ignored issues
show
The call to ElasticSearchQueryBuilder::__construct() misses a required argument $soupmix.

This check looks for function calls that miss required arguments.

Loading history...
277
    }
278 1
279
    public static function buildFilter(array $filter)
280 1
    {
281 1
        $filters = [];
282 1
        foreach ($filter as $key => $value) {
283 1
            $isNot = '';
284
            if (strpos($key, '__')!==false) {
285
                $tmpFilters = self::buildFilterForKeys($key, $value, $isNot);
286
                $filters = self::mergeFilters($filters, $tmpFilters);
287 1
            } elseif ((strpos($key, '__') === false) && is_array($value)) {
288 1
                $filters['should'] = self::buildFilterForOR($value);
289 1
            } else {
290
                $filters['must'][] = ['term' => [$key => $value]];
291 1
            }
292 1
        }
293 1
        return $filters;
294
    }
295
296
    private static function mergeFilters (array $filters, array $tmpFilters){
297
        foreach ($tmpFilters as $fKey => $fVals) {
298
            if (isset($filters[$fKey])) {
299
                foreach ($fVals as $fVal) {
300
                    $filters[$fKey][] = $fVal;
301
                }
302
            } else {
303
                $filters[$fKey] = $fVals;
304
            }
305
        }
306
        return $filters;
307
    }
308
309
    private static function buildFilterForKeys(string $key, $value, string $isNot)
310
    {
311
        $filters = [];
312 1
        preg_match('/__(.*?)$/', $key, $matches);
313 1
        $operator = $matches[1];
314 1
        if (strpos($operator, '!')===0) {
315
            $operator = str_replace('!', '', $operator);
316
            $isNot = '_not';
317 1
        }
318
        $key = str_replace($matches[0], '', $key);
319 1
        foreach (self::$operators as $type => $possibilities) {
320 1
            if (in_array($operator, $possibilities, true)) {
321 1
                switch ($type) {
322 1
                    case 'range':
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
323 1
                        $filters['must'.$isNot][] = ['range' => [$key => [$operator => $value]]];
324 1
                        break;
325 1
                    case 'standard':
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
326 1
                        $filters['must'.$isNot][] = [$type => [$key => $value]];
327
                        break;
328
                    case 'BooleanQueryLevel':
329 1
                        switch ($operator) {
330 1
                            case 'not':
331 1
                                $filters['must_not'][] = ['term' => [$key => $value]];
332
                                break;
333 1
                        }
334 1
                        break;
335 1
                    case 'special':
336
                        switch ($operator) {
337
                            case 'in':
338
                                $filters['must'.$isNot][] = ['terms' => [$key => $value]];
339
                                break;
340
                        }
341
                        break;
342
                }
343
            }
344
        }
345
        return $filters;
346
    }
347 1
348 1
    private static function buildFilterForOR(array $orValues)
349 1
    {
350
        $filters = [];
351
        foreach ($orValues as $orValue) {
352 1
            $subKey = array_keys($orValue)[0];
353 1
            $subValue = $orValue[$subKey];
354
            if (strpos($subKey, '__') !== false) {
355
                preg_match('/__(.*?)$/', $subKey, $subMatches);
356
                $subOperator = $subMatches[1];
357
                if (strpos($subOperator, '!')===0) {
358
                    $subOperator = str_replace('!', '', $subOperator);
359
                }
360
                $subKey = str_replace($subMatches[0], '', $subKey);
361
                foreach (self::$operators as $type => $possibilities) {
362
                    if (in_array($subOperator, $possibilities, true)) {
363
                        switch ($type) {
364
                            case 'range':
365
                                $filters[] = ['range' => [$subKey => [$subOperator => $subValue]]];
366
                                break;
367
                            case 'standard':
368
                                $filters[] = [$type => [$subKey => $subValue]];
369
                                break;
370
                            case 'special':
371
                                switch ($subOperator) {
372
                                    case 'in':
373
                                        $filters[] = ['terms' => [$subKey => $subValue]];
374
                                        break;
375
                                }
376
                                break;
377
                        }
378
                    }
379
                }
380
            } else {
381
                $filters[] = ['term' => [$subKey => $subValue]];
382
            }
383
        }
384
        return $filters;
385
    }
386
}
387