Completed
Push — develop ( 053968...47dc8d )
by Maxim
12s
created

preload.functions.inc.php ➔ startCMSSession()   D

Complexity

Conditions 9
Paths 72

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 72
nop 0
dl 0
loc 25
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
global $site_sessionname;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
4
$site_sessionname = genEvoSessionName(); // For legacy extras not using startCMSSession
5
6
/**
7
 * @return string
8
 */
9
function genEvoSessionName()
10
{
11
    $_ = crc32(__FILE__);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $_. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
12
    $_ = sprintf('%u', $_);
13
14
    return 'evo' . base_convert($_, 10, 36);
15
}
16
17
/**
18
 * @return void
19
 */
20
function startCMSSession()
0 ignored issues
show
Coding Style introduced by
startCMSSession 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...
Coding Style introduced by
startCMSSession uses the super-global variable $_SESSION 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...
21
{
22
23
    global $site_sessionname, $https_port, $session_cookie_path, $session_cookie_domain;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
24
25
    session_name($site_sessionname);
26
    removeInvalidCmsSessionIds($site_sessionname);
27
    $cookieExpiration = 0;
28
    $secure = ((isset ($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') || $_SERVER['SERVER_PORT'] == $https_port);
29
    $cookiePath = !empty($session_cookie_path) ? $session_cookie_path : MODX_BASE_URL;
30
    $cookieDomain = !empty($session_cookie_domain) ? $session_cookie_domain : '';
31
    session_set_cookie_params($cookieExpiration, $cookiePath, $cookieDomain, $secure, true);
32
    session_start();
33
    $key = "modx.mgr.session.cookie.lifetime";
34
    if (isset($_SESSION[$key]) && is_numeric($_SESSION[$key])) {
35
        $cookieLifetime = (int)$_SESSION[$key];
36
        if ($cookieLifetime) {
37
            $cookieExpiration = $_SERVER['REQUEST_TIME'] + $cookieLifetime;
38
        }
39
        setcookie(session_name(), session_id(), $cookieExpiration, $cookiePath, $cookieDomain, $secure, true);
40
    }
41
    if (!isset($_SESSION['modx.session.created.time'])) {
42
        $_SESSION['modx.session.created.time'] = $_SERVER['REQUEST_TIME'];
43
    }
44
}
45
46
/**
47
 * @param $storage
48
 * @param $session_name
49
 * @return void
50
 */
51
function removeInvalidCmsSessionFromStorage(&$storage, $session_name)
52
{
53
    if (isset($storage[$session_name]) && ($storage[$session_name] === '' || $storage[$session_name] === 'deleted')) {
54
        unset($storage[$session_name]);
55
    }
56
}
57
58
/**
59
 * @param $session_name
60
 * @return void
61
 */
62
function removeInvalidCmsSessionIds($session_name)
0 ignored issues
show
Coding Style introduced by
removeInvalidCmsSessionIds uses the super-global variable $_COOKIE 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...
Coding Style introduced by
removeInvalidCmsSessionIds uses the super-global variable $_GET 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...
Coding Style introduced by
removeInvalidCmsSessionIds uses the super-global variable $_POST 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...
63
{
64
    // session ids is invalid iff it is empty string
65
    // storage priorioty can see in PHP source ext/session/session.c
66
    removeInvalidCmsSessionFromStorage($_COOKIE, $session_name);
67
    removeInvalidCmsSessionFromStorage($_GET, $session_name);
68
    removeInvalidCmsSessionFromStorage($_POST, $session_name);
69
}
70