Completed
Push — develop ( 0c91f3...21b315 )
by Agel_Nash
10:05 queued 04:14
created

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

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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