Completed
Push — develop ( 4a389e...d0bb9b )
by Maxim
15s
created

preload.functions.inc.php ➔ evolutionCMS()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 11
rs 9.4285
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
if( ! function_exists('evolutionCMS')) {
8
    /**
9
     * @return DocumentParser
10
     */
11
    function evolutionCMS()
12
    {
13
        if( ! defined('MODX_CLASS')) {
14
            if( ! class_exists('DocumentParser')) {
15
                throw new RuntimeException('MODX_CLASS not defined and DocumentParser class not exists');
16
            }
17
            define('MODX_CLASS', 'DocumentParser');
18
        }
19
        $obj = new ReflectionClass(MODX_CLASS);
20
        return $obj->newInstanceWithoutConstructor()->getInstance();
21
    }
22
}
23
24
/**
25
 * @return string
26
 */
27
function genEvoSessionName()
28
{
29
    $_ = 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...
30
    $_ = sprintf('%u', $_);
31
32
    return 'evo' . base_convert($_, 10, 36);
33
}
34
35
/**
36
 * @return void
37
 */
38
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...
39
{
40
41
    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...
42
43
    session_name($site_sessionname);
44
    removeInvalidCmsSessionIds($site_sessionname);
45
    $cookieExpiration = 0;
46
    $secure = ((isset ($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') || $_SERVER['SERVER_PORT'] == $https_port);
47
    $cookiePath = !empty($session_cookie_path) ? $session_cookie_path : MODX_BASE_URL;
48
    $cookieDomain = !empty($session_cookie_domain) ? $session_cookie_domain : '';
49
    session_set_cookie_params($cookieExpiration, $cookiePath, $cookieDomain, $secure, true);
50
    session_start();
51
    $key = "modx.mgr.session.cookie.lifetime";
52
    if (isset($_SESSION[$key]) && is_numeric($_SESSION[$key])) {
53
        $cookieLifetime = (int)$_SESSION[$key];
54
        if ($cookieLifetime) {
55
            $cookieExpiration = $_SERVER['REQUEST_TIME'] + $cookieLifetime;
56
        }
57
        setcookie(session_name(), session_id(), $cookieExpiration, $cookiePath, $cookieDomain, $secure, true);
58
    }
59
    if (!isset($_SESSION['modx.session.created.time'])) {
60
        $_SESSION['modx.session.created.time'] = $_SERVER['REQUEST_TIME'];
61
    }
62
}
63
64
/**
65
 * @param $storage
66
 * @param $session_name
67
 * @return void
68
 */
69
function removeInvalidCmsSessionFromStorage(&$storage, $session_name)
70
{
71
    if (isset($storage[$session_name]) && ($storage[$session_name] === '' || $storage[$session_name] === 'deleted')) {
72
        unset($storage[$session_name]);
73
    }
74
}
75
76
/**
77
 * @param $session_name
78
 * @return void
79
 */
80
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...
81
{
82
    // session ids is invalid iff it is empty string
83
    // storage priorioty can see in PHP source ext/session/session.c
84
    removeInvalidCmsSessionFromStorage($_COOKIE, $session_name);
85
    removeInvalidCmsSessionFromStorage($_GET, $session_name);
86
    removeInvalidCmsSessionFromStorage($_POST, $session_name);
87
}
88