Issues (17)

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/SearchAbstract.php (3 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
/*
4
 * This file is part of gpupo/search
5
 *
6
 * (c) Gilmar Pupo <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gpupo\Search;
13
14
use Gpupo\Search\Result\CountableCollection;
15
16
/**
17
 * Comunicacao com o SphinxClient.
18
 */
19
abstract class SearchAbstract
20
{
21
    /**
22
     * Facade para Single query, obtendo apenas os resultados (matches).
23
     *
24
     * @param string $index
25
     */
26
    public function search($index, array $filters = null,
27
        array $queries = null, array $fieldWeights = null,
28
        $limit = 20, $offset = 0
29
    ) {
30
        $result = $this->query($index, $filters, $queries,
31
            $fieldWeights, $limit, $offset);
32
33
        if (!isset($result['matches'])) {
34
            return [];
35
        }
36
37
        return $result['matches'];
38
    }
39
40
    /**
41
     * Executa Queries.
42
     *
43
     * @see \Gpupo\Search\Query\FiltersAbstract::toArray()          Filter Array Sintaxe
44
     * @see \Gpupo\Search\Query\QueryAbstract::getQueries()         Query Array Sintaxe
45
     * @see \Gpupo\Search\Query\QueryAbstract::getFieldWeights()    Query Array Sintaxe
46
     *
47
     * @param array $filter       Search filter
0 ignored issues
show
There is no parameter named $filter. Did you maybe mean $filters?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
48
     * @param array $queries      Search query
49
     * @param array $fieldWeights Field weights array
50
     * @param int   $limit
51
     * @param int   $offset
52
     *
53
     * @return array Results
54
     */
55
    public function query($index, array $filters = null,
56
        array $queries = null, array $fieldWeights = null,
57
        $limit = 20, $offset = 0
58
    ) {
59
        $sphinxClient = $this->getSphinxClient();
60
        $sphinxClient->SetLimits($offset, $limit);
61
        if (null !== $filters) {
62
            foreach ($filters as $filter) {
63
                if (!isset($filter['key'])) {
0 ignored issues
show
This if statement is empty and can be removed.

This check looks for the bodies of if statements 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.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
64
                    // Filtro existe mas sem key
65
                }
66
                if (
67
                    array_key_exists('min', $filter) &&
68
                    array_key_exists('max', $filter)
69
                ) {
70
                    $sphinxClient->SetFilterRange(
71
                                  $filter['key'],
72
                        (integer) $filter['min'],
73
                        (integer) $filter['max']
74
                    );
75
                } else {
76
                    if (!isset($filter['values']) || !is_array($filter['values'])) {
0 ignored issues
show
This if statement is empty and can be removed.

This check looks for the bodies of if statements 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.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
77
                        //Filtro existe mas sem valor;
78
                    }
79
                    $sphinxClient->SetFilter(
80
                        $filter['key'],
81
                        $filter['values']
82
                    );
83
                }
84
            }
85
        }
86
        if (null !== $queries) {
87
            foreach ($queries as $key => $queryInfo) {
88
                $query = $this->implodeQueryValues($queryInfo);
89
90
                if (array_key_exists('countableAttributes', $queryInfo)) {
91
                    $array = $queryInfo['countableAttributes'];
92
                    if (!is_array($array)) {
93
                        $array = [$array];
94
                    }
95
96
                    $sphinxClient->addFacetedQuery($query, $index, $array);
97
                } else {
98
                    $sphinxClient->AddQuery($query, $index);
99
                }
100
            }
101
        }
102
103
        if (null !== $fieldWeights) {
104
            $sphinxClient->SetFieldWeights($fieldWeights);
105
        }
106
107
        $result = $this->getResult($sphinxClient);
108
109
        return $result;
110
    }
111
112
    /**
113
     * RunQueries() + validate.
114
     *
115
     * - Single Query: Resultados da Query
116
     *
117
     * - Multi Query:  Array de Resultados das Querys
118
     *
119
     * Formato de cada Resultado:
120
     *
121
     * <code>
122
     * //Results
123
     * array(
124
     *     array(
125
     *         'id'     => 12345,
126
     *         'weight' => 30,
127
     *         'attrs'  => array(...)
128
     *     ),
129
     *     array(
130
     *         'id'     => 23456,
131
     *         'weight' => 20,
132
     *         'attrs'  => array(...)
133
     *     ),
134
     *     ...
135
     * );
136
     * </code>
137
     *
138
     * @param \SphinxClient $sphinxClient
139
     *
140
     * @throws \Exception
141
     *
142
     * @return array
143
     */
144
    protected function getResult(\SphinxClient $sphinxClient)
145
    {
146
        $result = $sphinxClient->RunQueries();
147
148
        if (false === $result) {
149
            throw new \Exception(
150
                $sphinxClient->getLastError()
151
            );
152
        }
153
        if ($sphinxClient->GetLastWarning()) {
154
            throw new \Exception(
155
                $sphinxClient->GetLastWarning()
156
            );
157
        }
158
159
        if (false === $result) {
160
            throw new \Exception(
161
                $sphinxClient->getLastError()
162
            );
163
        }
164
        if ($sphinxClient->GetLastWarning()) {
165
            throw new \Exception(
166
                $sphinxClient->GetLastWarning()
167
            );
168
        }
169
170
        //Suporte ao formato inicial de unica query
171
        if (count($result) === 1) {
172
            return current($result);
173
        }
174
175
        return $result;
176
    }
177
178
    /**
179
     * Transforma uma query array em uma string usada na
180
     * query do Client Sphinx Search.
181
     *
182
     * @param array $queryInfo
183
     *
184
     * @return string
185
     */
186
    protected function implodeQueryValues(array $queryInfo)
187
    {
188
        $query = "@{$queryInfo['key']} "
189
            .(
190
                '*'.implode('* *', $queryInfo['values'])
191
                .'*'
192
            ).PHP_EOL;
193
194
        return $query;
195
    }
196
197
    /**
198
     * Facade para query, obtendo resultados em objeto.
199
     */
200
    public function getCollection($index, array $filters = null,
201
        array $queries = null, array $fieldWeights = null,
202
        $limit = 20, $offset = 0, $countableAttributes = null
203
    ) {
204
        $result = $this->query($index, $filters, $queries,
205
            $fieldWeights, $limit, $offset);
206
207
        if (is_array($result)) {
208
            $i = 0;
209
210
            if ($countableAttributes) {
211
                foreach ($countableAttributes as $attributeName) {
212
                    $i++;
213
                    $result[0]['attributes']['countable'][$attributeName] = new CountableCollection($result[$i], $attributeName);
214
                }
215
            }
216
217
            for ($l = 1; $l <= $i; $l++) {
218
                unset($result[$l]);
219
            }
220
221
            $collection = $this->factoryCollection($result);
222
223
            return $collection;
224
        }
225
    }
226
}
227