Completed
Push — master ( 04748f...585c72 )
by Marco
07:11
created

SecureCookie::encryptKey()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 1
crap 12
1
<?php namespace Comodojo\Cookies;
2
3
/**
4
 * AES-encrypted cookie
5
 *
6
 * @package     Comodojo Spare Parts
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
21
class SecureCookie extends EncryptedCookie {
22
23
    /**
24
     * Create a client-specific key using provided key,
25
     * the client remote address and (in case) the value of
26
     * HTTP_X_FORWARDED_FOR header
27
     *
28
     * @param   string   $key
29
     *
30
     * @return  string
31
     */
32
    protected static function encryptKey($key) {
0 ignored issues
show
Coding Style introduced by
encryptKey uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
33
34
        if ( isset($_SERVER['REMOTE_ADDR']) ) {
35
36
            $client_hash = md5($_SERVER['REMOTE_ADDR'].(isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : ''), true);
37
38
            $server_hash = md5($key, true);
39
40
            $cookie_key = $client_hash.$server_hash;
41
42
        } else {
43
44
            $cookie_key = hash('sha256', $key);
45
46
        }
47
48
        return $cookie_key;
49
50
    }
51
52
}
53