Issues (116)

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.

Search/Adapter/Algolia/AlgoliaSearchAdapter.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
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Component\Search\Adapter\Algolia;
14
15
use AlgoliaSearch\Client;
16
use Doctrine\Common\Collections\Collection;
17
use WellCommerce\Component\Search\Adapter\AdapterInterface;
18
use WellCommerce\Component\Search\Adapter\QueryBuilderInterface;
19
use WellCommerce\Component\Search\Adapter\SearchAdapterConfiguratorInterface;
20
use WellCommerce\Component\Search\Model\DocumentInterface;
21
use WellCommerce\Component\Search\Model\FieldInterface;
22
use WellCommerce\Component\Search\Request\SearchRequestInterface;
23
24
/**
25
 * Class AlgoliaSearchAdapter
26
 *
27
 * @author  Adam Piotrowski <[email protected]>
28
 */
29
final class AlgoliaSearchAdapter implements AdapterInterface
30
{
31
    /**
32
     * @var SearchAdapterConfiguratorInterface
33
     */
34
    private $configurator;
35
    
36
    /**
37
     * @var Client
38
     */
39
    private $client;
40
    
41
    public function __construct(SearchAdapterConfiguratorInterface $configurator)
42
    {
43
        $this->configurator = $configurator;
44
    }
45
    
46
    public function search(SearchRequestInterface $request): array
47
    {
48
        $indexName = $this->getIndexName($request->getLocale(), $request->getType()->getName());
49
        $index     = $this->getClient()->initIndex($indexName);
50
        $body      = $this->createQueryBuilder($request)->getQuery();
51
        $results   = $index->search($request->getPhrase(), [
52
            'attributesToRetrieve' => $body,
53
            'hitsPerPage'          => $this->getOption('maxResults'),
54
        ]);
55
        
56
        return $this->processResults($results);
57
    }
58
    
59
    public function addDocument(DocumentInterface $document)
60
    {
61
        $indexName = $this->getIndexName($document->getLocale(), $document->getType()->getName());
62
        $index     = $this->getClient()->initIndex($indexName);
63
        
64
        $index->addObject($this->createDocumentBody($document), $document->getIdentifier());
65
    }
66
    
67
    public function addDocuments(Collection $documents, string $locale, string $type)
68
    {
69
        $request = [];
70
        
71
        $documents->map(function (DocumentInterface $document) use (&$request) {
72
            $body             = $this->createDocumentBody($document);
73
            $body['objectID'] = $document->getIdentifier();
74
            $request[]        = $body;
75
        });
76
        
77
        $indexName = $this->getIndexName($locale, $type);
78
        $index     = $this->getClient()->initIndex($indexName);
79
        
80
        if (!empty($request)) {
81
            $index->addObjects($request);
82
        }
83
    }
84
    
85
    public function removeDocument(DocumentInterface $document)
86
    {
87
        $indexName = $this->getIndexName($document->getLocale(), $document->getType()->getName());
88
        $index     = $this->getClient()->initIndex($indexName);
89
        
90
        $index->deleteObject($document->getIdentifier());
91
    }
92
    
93
    public function updateDocument(DocumentInterface $document)
94
    {
95
        $indexName        = $this->getIndexName($document->getLocale(), $document->getType()->getName());
96
        $index            = $this->getClient()->initIndex($indexName);
97
        $body             = $this->createDocumentBody($document);
98
        $body['objectID'] = $document->getIdentifier();
99
        
100
        $index->saveObject($body);
101
    }
102
    
103
    public function getIndexName(string $locale, string $type): string
104
    {
105
        return sprintf('%s%s_%s', $this->getOption('indexPrefix'), $locale, $type);
106
    }
107
    
108
    public function createIndex(string $locale, string $type)
109
    {
110
    }
111
    
112
    public function removeIndex(string $locale, string $type)
113
    {
114
        $indexName = $this->getIndexName($locale, $type);
115
        
116
        return $this->getClient()->deleteIndex($indexName);
117
    }
118
    
119
    public function flushIndex(string $locale, string $type)
120
    {
121
        $indexName = $this->getIndexName($locale, $type);
122
        $index     = $this->getClient()->initIndex($indexName);
123
        
124
        return $index->clearIndex();
125
    }
126
    
127
    public function getStats(string $locale)
0 ignored issues
show
The parameter $locale is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        return $this->getClient()->getLogs(0, 100);
130
    }
131
    
132
    private function createQueryBuilder(SearchRequestInterface $request): QueryBuilderInterface
133
    {
134
        $queryBuilderClass = $this->getOption('builderClass');
135
        
136
        return new $queryBuilderClass($request, $this->getOption('termMinLength'));
137
    }
138
    
139
    private function createDocumentBody(DocumentInterface $document): array
140
    {
141
        $body = [];
142
        
143
        $document->getFields()->map(function (FieldInterface $field) use (&$body) {
144
            $body[$field->getName()] = $field->getValue();
145
        });
146
        
147
        return $body;
148
    }
149
    
150
    private function getClient(): Client
151
    {
152
        if (null === $this->client) {
153
            $this->client = new Client($this->getOption('appId'), $this->getOption('apiKey'));
154
        }
155
        
156
        return $this->client;
157
    }
158
    
159
    private function processResults(array $results): array
160
    {
161
        $identifiers = [];
162
        
163
        foreach ($results['hits'] as $hit) {
164
            $identifiers[] = $hit['objectID'];
165
        }
166
        
167
        return $identifiers;
168
    }
169
    
170
    private function getOption(string $name)
171
    {
172
        return $this->configurator->getSearchAdapterOptions()[$name];
173
    }
174
}
175