Issues (183)

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.

src/Cache/Driver/Memcached.php (2 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
namespace FMUP\Cache\Driver;
3
4
use FMUP\Cache\CacheInterface;
5
use FMUP\Cache\Exception;
6
7
/**
8
 * Class Memcached
9
 * This driver needs MEMCACHED installed on server to work properly
10
 * @package FMUP\Cache\Driver
11
 */
12
class Memcached implements CacheInterface
13
{
14
    const SETTINGS_MEMCACHED = 'SETTINGS_MEMCACHED';
15
    /**
16
     * @see http://php.net/manual/fr/memcached.expiration.php
17
     */
18
    const SETTINGS_TTL_IN_SECOND = 'SETTINGS_TTL_IN_SECOND';
19
    const SETTINGS_CACHE_PREFIX = 'SETTINGS_CACHE_PREFIX';
20
21
    private $isAvailable = null;
22
    private $memcachedInstance = null;
23
    private $settings = array();
24
25
    /**
26
     * Check whether memcached is available
27
     * @return bool
28
     */
29 1
    public function isAvailable()
30
    {
31 1
        if (is_null($this->isAvailable)) {
32 1
            $this->isAvailable = class_exists('\Memcached');
33
        }
34 1
        return $this->isAvailable;
35
    }
36
37
    /**
38
     * constructor of Memcached
39
     * @param array $settings
40
     */
41 15
    public function __construct($settings = array())
42
    {
43 15
        if (isset($settings[self::SETTINGS_MEMCACHED])) {
44 1
            $this->setMemcachedInstance($settings[self::SETTINGS_MEMCACHED]);
45
        }
46 15
        $this->settings = $settings;
47 15
    }
48
49
    /**
50
     * Get the memcached instance
51
     * @return \Memcached
52
     * @throws Exception
53
     */
54 5
    public function getMemcachedInstance()
55
    {
56 5
        if (!$this->memcachedInstance) {
57 2
            if (!$this->isAvailable()) {
58 1
                throw new Exception('Memcached is not available');
59
            }
60 1
            $this->memcachedInstance = $this->createMemcached();
61
        }
62 4
        return $this->memcachedInstance;
63
    }
64
65
    /**
66
     * @return \Memcached
67
     * @codeCoverageIgnore
68
     */
69
    protected function createMemcached()
70
    {
71
        return new \Memcached();
72
    }
73
74
    /**
75
     * Define a memcached instance
76
     * @param \Memcached $memcachedInstance
77
     * @return $this
78
     */
79 3
    public function setMemcachedInstance(\Memcached $memcachedInstance)
80
    {
81 3
        $this->memcachedInstance = $memcachedInstance;
82 3
        return $this;
83
    }
84
85
    /**
86
     * @param string $key
87
     * @return string
88
     */
89 4
    protected function getCacheKey($key)
90
    {
91 4
        $prefix = (string)$this->getSetting(self::SETTINGS_CACHE_PREFIX);
92 4
        return $prefix . $key;
93
    }
94
95
96
    /**
97
     * Check whether a key exists
98
     * @param string $key
99
     * @return bool
100
     * @throws Exception
101
     */
102 2
    public function has($key)
103
    {
104 2
        if (!$this->isAvailable()) {
105 1
            throw new Exception('Memcached is not available');
106
        }
107 1
        $key = $this->getCacheKey($key);
108 1
        $keys = array_flip($this->getMemcachedInstance()->getAllKeys());
109 1
        return isset($keys[$key]);
110
    }
111
112
    /**
113
     * @param string $key
114
     * @return mixed
115
     * @throws Exception
116
     */
117 2 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...
118
    {
119 2
        if (!$this->isAvailable()) {
120 1
            throw new Exception('Memcached is not available');
121
        }
122 1
        $key = $this->getCacheKey($key);
123 1
        return $this->getMemcachedInstance()->get($key);
124
    }
125
126
    /**
127
     * Define a
128
     * @param string $key
129
     * @param mixed $value
130
     * @return $this
131
     * @throws Exception
132
     */
133 3
    public function set($key, $value)
134
    {
135 3
        if (!$this->isAvailable()) {
136 1
            throw new Exception('Memcached is not available');
137
        }
138 2
        $ttl = (int)$this->getSetting(self::SETTINGS_TTL_IN_SECOND);
139 2
        $key = $this->getCacheKey($key);
140 2
        if (!$this->getMemcachedInstance()->set($key, $value, $ttl)) {
141 1
            throw new Exception(
142 1
                'Error while inserting value in memcached : ' . $this->getMemcachedInstance()->getResultMessage(),
143 1
                $this->getMemcachedInstance()->getResultCode()
144
            );
145
        }
146 1
        return $this;
147
    }
148
149
    /**
150
     * Delete a value in memcache
151
     * @param string $key
152
     * @return $this
153
     * @throws Exception
154
     */
155 3 View Code Duplication
    public function remove($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...
156
    {
157 3
        if (!$this->isAvailable()) {
158 1
            throw new Exception('Memcached is not available');
159
        }
160 2
        $key = $this->getCacheKey($key);
161 2
        if (!$this->getMemcachedInstance()->delete($key)) {
162 1
            throw new Exception('Error while deleting key in memcached');
163
        }
164 1
        return $this;
165
    }
166
167
    /**
168
     * Define a setting
169
     * @param string $setting
170
     * @param mixed $value
171
     * @return $this
172
     */
173 1
    public function setSetting($setting, $value)
174
    {
175 1
        $this->settings[(string)$setting] = $value;
176 1
        return $this;
177
    }
178
179
    /**
180
     * Retrieve a defined setting
181
     * @param string $setting
182
     * @return mixed|null
183
     */
184 5
    public function getSetting($setting)
185
    {
186 5
        $setting = (string) $setting;
187 5
        return isset($this->settings[$setting]) ? $this->settings[$setting] : null;
188
    }
189
}
190