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/Adapter/ElasticSearchAdapter.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 NilPortugues\Cache\Adapter;
4
5
use InvalidArgumentException;
6
use NilPortugues\Cache\Adapter\ElasticSearch\Curl;
7
use NilPortugues\Cache\CacheAdapter;
8
9
/**
10
 * Class ElasticSearchAdapter
11
 * @package NilPortugues\Cache\Adapter
12
 */
13
class ElasticSearchAdapter extends Adapter implements CacheAdapter
14
{
15
    /**
16
     * @var \NilPortugues\Cache\Adapter\ElasticSearch\Curl
17
     */
18
    private $curl;
19
20
    /**
21
     * @var string
22
     */
23
    private $base = '';
24
25
    /**
26
     * @var string
27
     */
28
    private $baseUrl = '';
29
30
    /**
31
     * @var array
32
     */
33
    private $createCache = [
34
        "mappings" => [
35
            "cache" => [
36
                "_source"    => [
37
                    "enabled" => false
38
                ],
39
                "_ttl"       => [
40
                    "enabled" => true,
41
                    "default" => "1000ms"
42
                ],
43
                "properties" => [
44
                    "value" => [
45
                        "type"  => "string",
46
                        "index" => "not_analyzed"
47
                    ]
48
                ]
49
            ]
50
        ]
51
    ];
52
53
    /**
54
     * @param string          $baseUrl
55
     * @param string          $indexName
56
     * @param CacheAdapter    $next
57
     *
58
     * @throws InvalidArgumentException
59
     */
60
    public function __construct($baseUrl, $indexName, CacheAdapter $next = null)
61
    {
62
        $baseUrl   = (string)$baseUrl;
63
        $indexName = (string)$indexName;
64
        $this->curl = $this->getCurlClient();
65
66
        $this->base    = \sprintf("%s/%s", $baseUrl, $indexName);
67
        $this->baseUrl = \sprintf("%s/%s/cache", $baseUrl, $indexName);
68
69
        if (false === \filter_var($this->base, FILTER_VALIDATE_URL)) {
70
            throw new InvalidArgumentException('The provided base URL is not a valid URL');
71
        }
72
73
        if (false === $this->curl->cacheIndexExists()) {
74
            $this->curl->createCacheIndex($this->base, $this->createCache);
75
        }
76
77
        $this->nextAdapter = (InMemoryAdapter::getInstance() === $next) ? null : $next;
78
    }
79
80
    /**
81
     * @codeCoverageIgnore
82
     * @return Curl
83
     */
84
    protected function getCurlClient()
85
    {
86
        return new Curl($this->base, $this->baseUrl);
87
    }
88
89
    /**
90
     * Get a value identified by $key.
91
     *
92
     * @param  string $key
93
     *
94
     * @return bool|mixed
95
     */
96 View Code Duplication
    public function get($key)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $key       = (string)$key;
99
        $this->hit = false;
100
101
        $inMemoryValue = InMemoryAdapter::getInstance()->get($key);
102
        if (InMemoryAdapter::getInstance()->isHit()) {
103
            $this->hit = true;
104
            return $inMemoryValue;
105
        }
106
107
        $value = $this->curl->get($key);
108
109
        if (null !== $value) {
110
            $this->hit = true;
111
            InMemoryAdapter::getInstance()->set($key, $value, 0);
112
            return $this->restoreDataStructure($value);
113
        }
114
115
        return (null !== $this->nextAdapter) ? $this->nextAdapter->get($key) : null;
116
    }
117
118
    /**
119
     * Delete a value identified by $key.
120
     *
121
     * @param  string $key
122
     */
123
    public function delete($key)
124
    {
125
        $this->curl->delete($key);
126
        $this->deleteChain($key);
127
    }
128
129
    /**
130
     * Set a value identified by $key and with an optional $ttl.
131
     *
132
     * @param string $key
133
     * @param mixed  $value
134
     * @param int    $ttl
135
     *
136
     * @return $this
137
     */
138
    public function set($key, $value, $ttl = 0)
139
    {
140
        $ttl = $this->fromDefaultTtl($ttl);
141
142
        if ($ttl >= 0) {
143
            $response = $this->curl->set($key, $this->storageDataStructure($value), $ttl);
144
145
            if (false !== $response) {
146
                $response = \json_decode($response, true);
147
148
                if (\array_key_exists('ok', $response) && true === $response['ok']) {
149
                    $this->setChain($key, $value, $ttl);
150
                }
151
            }
152
        }
153
154
        return $this;
155
    }
156
157
    /**
158
     * Checks the availability of the cache service.
159
     *
160
     * @return bool
161
     */
162
    public function isAvailable()
163
    {
164
        return $this->curl->cacheIndexExists();
165
    }
166
167
    /**
168
     * Clears all expired values from cache.
169
     *
170
     * @return mixed
171
     */
172
    public function clear()
173
    {
174
        $this->clearChain();
175
    }
176
177
    /**
178
     * Clears all values from the cache.
179
     *
180
     * @return mixed
181
     */
182
    public function drop()
183
    {
184
        $this->curl->drop($this->base);
185
        $this->curl->createCacheIndex($this->base, $this->createCache);
186
        $this->dropChain();
187
    }
188
}
189