Issues (1)

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/ScoutElasticEngine.php (1 issue)

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 Alhoqbani\Elastic;
4
5
use Elasticsearch\Client;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Support\Arr;
8
use Laravel\Scout\Builder;
9
use Laravel\Scout\Engines\Engine;
10
11
class ScoutElasticEngine extends Engine
12
{
13
14
    /**
15
     * The Elasticsearch client.
16
     *
17
     * @var \Elasticsearch\Client
18
     */
19
    private $client;
20
21
    /**
22
     * ElasticSearchScoutEngine constructor.
23
     *
24
     * @param \Elasticsearch\Client $client
25
     */
26 39
    public function __construct(Client $client)
27
    {
28 39
        $this->client = $client;
29 39
    }
30
31
    /**
32
     * Update the given model in the index.
33
     *
34
     * @param  \Illuminate\Database\Eloquent\Collection $models
35
     *
36
     * @return void
37
     */
38 3
    public function update($models)
39
    {
40 3
        $index = $models->first()->searchableAs();
41
42 3
        $params = ['body' => []];
43 3
        foreach ($models as $model) {
44 3
            $array = $model->toSearchableArray();
45
46 3
            if (empty($array)) {
47
                return;
48
            }
49
50 3
            $params['body'][] = [
51
                'index' => [
52 3
                    '_index' => $index,
53 3
                    '_type'  => $index,
54 3
                    '_id'    => $model->getKey(),
55
                ],
56
            ];
57 3
            $params['body'][] = $array;
58
        }
59
60 3
        $this->client->bulk($params);
61 3
    }
62
63
    /**
64
     * Remove the given model from the index.
65
     *
66
     * @param  \Illuminate\Database\Eloquent\Collection $models
67
     *
68
     * @return void
69
     */
70 3
    public function delete($models)
71
    {
72 3
        $index = $models->first()->searchableAs();
73
74 3
        $params = ['body' => []];
75 3
        foreach ($models as $model) {
76 3
            $params['body'][] = [
77
                'delete' => [
78 3
                    '_index' => $index,
79 3
                    '_type'  => $index,
80 3
                    '_id'    => $model->getKey(),
81
                ],
82
            ];
83
        }
84
85 3
        $this->client->bulk($params);
86 3
    }
87
88
    /**
89
     * Perform the given search on the engine.
90
     *
91
     * @param  \Laravel\Scout\Builder $builder
92
     *
93
     * @return mixed
94
     */
95 15
    public function search(Builder $builder)
96
    {
97 15
        $params = $this->prepareParams($builder);
98
99 15
        return $this->client->search($params);
100
    }
101
102
    /**
103
     * Perform the given search on the engine.
104
     *
105
     * @param  \Laravel\Scout\Builder $builder
106
     * @param  int                    $perPage
107
     * @param  int                    $page
108
     *
109
     * @return mixed
110
     */
111 15
    public function paginate(Builder $builder, $perPage, $page)
112
    {
113 15
        $params = $this->prepareParams($builder);
114
115 15
        $params['size'] = $perPage;
116 15
        $params['from'] = ($page - 1) * $perPage;
117 15
        unset($params['body']['size'], $params['body']['from']);
118
119 15
        return $this->client->search($params);
120
    }
121
122
    /**
123
     * Pluck and return the primary keys of the given results.
124
     *
125
     * @param  mixed $results
126
     *
127
     * @return \Illuminate\Support\Collection
128
     */
129
    public function mapIds($results)
130
    {
131
        return collect($results['hits']['hits'])->pluck('_id')->values();
132
    }
133
134
    /**
135
     * Map the given results to instances of the given model.
136
     *
137
     * @param  mixed                               $results
138
     * @param  \Illuminate\Database\Eloquent\Model $model
139
     *
140
     * @return \Illuminate\Database\Eloquent\Collection
141
     */
142 3
    public function map($results, $model)
143
    {
144 3
        if ($results['hits']['total'] === 0) {
145
            return Collection::make();
146
        }
147
148 3
        $keys = collect($results['hits']['hits'])
149 3
            ->pluck('_id')->values()->all();
150
151 3
        $models = $model->whereIn(
152 3
            $model->getQualifiedKeyName(),
153 3
            $keys
154 3
        )->get()->keyBy($model->getKeyName());
155
156 3
        return Collection::make($results['hits']['hits'])->map(function ($hit) use ($model, $models) {
157 3
            $key = $hit['_id'];
158
159 3
            if (isset($models[$key])) {
160 3
                return $models[$key];
161
            }
162 3
        })->filter()->values();
163
    }
164
165
    /**
166
     * Get the total count from a raw result returned by the engine.
167
     *
168
     * @param  mixed $results
169
     *
170
     * @return int
171
     */
172
    public function getTotalCount($results)
173
    {
174
        return $results['hits']['total'];
175
    }
176
177
    /**
178
     * Parse and prepare the params to send to Elasticsearch client
179
     *
180
     * @param \Laravel\Scout\Builder $builder
181
     *
182
     * @return array
183
     */
184 30
    protected function prepareParams(Builder $builder)
185
    {
186
        $params = [
187 30
            'index' => $builder->index ?? $builder->model->searchableAs(),
188
//            'type'  => $builder->index ?? $builder->model->searchableAs(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
189 30
            'size'  => $builder->limit ?? $builder->model->getPerPage(),
190
            'body'  => [
191
                'query' => [
192
                    'multi_match' => [
193 30
                        'query'  => $builder->query,
194 30
                        'fields' => '_all',
195
                    ],
196
                ],
197
            ],
198
        ];
199
200 30
        if ($builder->callback) {
201 12
            $params = array_merge($params,
202 12
                call_user_func(
203 12
                    $builder->callback,
204 12
                    $this->client,
205 12
                    $builder->query
206
                ));
207
        }
208
209 30
        return $params;
210
    }
211
}
212