Controller
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 104
c 0
b 0
f 0
wmc 0
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 38 1
A initializePHPSession() 0 6 2
A checkTimezone() 0 6 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @title            Controller Core Class
4
 *
5
 * @author           Pierre-Henry Soria <[email protected]>
6
 * @copyright        (c) 2012-2019, Pierre-Henry Soria. All Rights Reserved.
7
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
8
 * @link             http://ph7cms.com
9
 * @package          PH7 / Install / Library
10
 */
11
12
namespace PH7;
13
14
defined('PH7') or die('Restricted access');
15
16
use Smarty;
17
18
abstract class Controller implements Controllable
19
{
20
    const PHP_TIMEZONE_DIRECTIVE = 'date.timezone';
21
    const VIEW_CACHE_LIFETIME = 24 * 3600; //thanks PHP5.6 for scalar expr in consts
22
23
    const SOFTWARE_NAME = 'pH7CMS';
24
    const DEFAULT_SITE_NAME = 'My Dating WebApp';
25
    const DEFAULT_ADMIN_USERNAME = 'administrator';
26
    const SOFTWARE_PREFIX_COOKIE_NAME = 'pH7';
27
    const SOFTWARE_WEBSITE = 'http://ph7cms.com';
28
    const SOFTWARE_REQUIREMENTS_URL = 'http://ph7cms.com/doc/en/requirements';
29
    const PAYPAL_DONATE_URL = 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=X457W3L7DAPC6';
30
    const PATREON_URL = 'https://www.patreon.com/bePatron?u=3534366';
31
    const SOFTWARE_AUTHOR = 'Pierre-Henry Soria';
32
    const AUTHOR_URL = 'https://github.com/pH-7';
33
    const SOFTWARE_GIT_REPO_URL = 'https://github.com/pH7Software/pH7-Social-Dating-CMS';
34
    const SOFTWARE_TWITTER = '@pH7Soft';
35
    const SOFTWARE_LICENSE = 'GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.';
36
    const SOFTWARE_COPYRIGHT = '© (c) 2012-%s, Pierre-Henry Soria. All Rights Reserved.';
37
    const TOTAL_INSTALL_STEPS = 7;
38
39
    /**
40
     * VERSION NAMES:
41
     *
42
     * 1.0, 1.1 branches were "pOH", 1.2 was "pOW", 1.3, 1.4 were "p[H]", 2.* was "H2O", 3.* was "H3O", 4.* was "HCO",
43
     * 5.* was "pCO", 6.* was "WoW", 7.*, 8.* were "NaOH", 10.* was "pKa", 12.* was "PHS", 14.* was "pKb" amd 15.* is ABSOLUTE™
44
     */
45
    const SOFTWARE_VERSION_NAME = 'ABSOLUTE™';
46
    const SOFTWARE_VERSION = '15.0.0';
47
    const SOFTWARE_BUILD = '1';
48
49
    const DEFAULT_LANG = 'en';
50
    const DEFAULT_THEME = 'base';
51
52
    /** @var Smarty */
53
    protected $oView;
54
55
    /** @var string */
56
    protected $sCurrentLang;
57
58
    public function __construct()
59
    {
60
        global $LANG;
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...
61
62
        // Initialize PHP session
63
        $this->initializePHPSession();
64
65
        // Verify and correct the time zone if necessary
66
        $this->checkTimezone();
67
68
        // Language initialization
69
        $this->sCurrentLang = (new Language)->get();
70
        include_once PH7_ROOT_INSTALL . Language::LANG_FOLDER_NAME . $this->sCurrentLang . PH7_DS . Language::LANG_FILENAME;
71
72
        /* Smarty initialization */
73
        $this->oView = new Smarty;
74
        $this->oView->setUseSubDirs(true);
75
        $this->oView->setTemplateDir(PH7_ROOT_INSTALL . 'views/' . self::DEFAULT_THEME);
76
        $this->oView->setCompileDir(PH7_ROOT_INSTALL . 'data/caches/smarty_compile');
77
        $this->oView->setCacheDir(PH7_ROOT_INSTALL . 'data/caches/smarty_cache');
78
        $this->oView->setPluginsDir(PH7_ROOT_INSTALL . 'library/Smarty/plugins');
79
80
        // Smarty Cache
81
        $this->oView->setCaching(Smarty::CACHING_OFF);
82
        $this->oView->setCacheLifetime(self::VIEW_CACHE_LIFETIME);
83
84
        $this->oView->assign('LANG', $LANG);
85
        $this->oView->assign('software_name', self::SOFTWARE_NAME);
86
        $this->oView->assign('software_version', self::SOFTWARE_VERSION . ' ' . self::SOFTWARE_VERSION_NAME . ' - Build ' . self::SOFTWARE_BUILD);
87
        $this->oView->assign('software_website', self::SOFTWARE_WEBSITE);
88
        $this->oView->assign('paypal_donate_url', self::PAYPAL_DONATE_URL);
89
        $this->oView->assign('patreon_url', self::PATREON_URL);
90
        $this->oView->assign('software_author', self::SOFTWARE_AUTHOR);
91
        $this->oView->assign('software_copyright', sprintf(self::SOFTWARE_COPYRIGHT, date('Y')));
92
        $this->oView->assign('tpl_name', self::DEFAULT_THEME);
93
        $this->oView->assign('current_lang', $this->sCurrentLang);
94
        $this->oView->assign('total_install_steps', self::TOTAL_INSTALL_STEPS);
95
    }
96
97
    /**
98
     * Check if the session is already initialized (thanks "session_status()" PHP >= 5.4)
99
     * And initialize it if it isn't the case.
100
     *
101
     * @return void
102
     */
103
    protected function initializePHPSession()
104
    {
105
        if (session_status() !== PHP_SESSION_ACTIVE) {
106
            @session_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
107
        }
108
    }
109
110
    /**
111
     * Set a default timezone if it is not already configured.
112
     *
113
     * @return void
114
     */
115
    protected function checkTimezone()
116
    {
117
        if (!ini_get(self::PHP_TIMEZONE_DIRECTIVE)) {
118
            date_default_timezone_set(PH7_DEFAULT_TIMEZONE);
119
        }
120
    }
121
}
122