Completed
Branch master (6a6544)
by Pierre-Henry
33:43
created

_install/library/Controller.class.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 15 and the first side effect is on line 13.

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-2017, 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
defined('PH7') or die('Restricted access');
14
15
abstract class Controller implements Controllable
16
{
17
    const
18
    SOFTWARE_NAME = 'pH7CMS',
19
    SOFTWARE_PREFIX_COOKIE_NAME = 'pH7',
20
    SOFTWARE_WEBSITE = 'http://ph7cms.com',
21
    SOFTWARE_LICENSE_URL = 'http://ph7cms.com/legal/license',
22
    SOFTWARE_HELP_URL = 'http://clients.hizup.com/support', // Help Desk URL
23
    SOFTWARE_LICENSE_KEY_URL = 'http://ph7cms.com/web/buysinglelicense',
24
    SOFTWARE_DOWNLOAD_URL = 'http://download.hizup.com/',
25
    SOFTWARE_REQUIREMENTS_URL = 'http://ph7cms.com/doc/en/requirements',
26
    SOFTWARE_HOSTING_LIST_URL = 'http://ph7cms.com/hosting',
27
    SOFTWARE_HOSTING_LIST_FR_URL = 'http://ph7cms.com/doc/fr/h%C3%A9bergement-web',
28
    SOFTWARE_EMAIL = '[email protected]',
29
    SOFTWARE_AUTHOR = 'Pierre-Henry Soria',
30
    SOFTWARE_LICENSE = 'GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.',
31
    SOFTWARE_COPYRIGHT = '© (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.',
32
    // 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", 5.* was "pCO" and 6.* is "WoW"
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
    SOFTWARE_VERSION_NAME = 'WoW',
34
    SOFTWARE_VERSION = '6.0.1',
35
    SOFTWARE_BUILD = '1',
36
    DEFAULT_LANG = 'en',
37
    DEFAULT_THEME = 'base';
38
39
    protected $oView, $sCurrentLang;
0 ignored issues
show
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
40
41
    public function __construct()
42
    {
43
        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...
44
45
        // Initialize PHP session
46
        $this->initializePHPSession();
47
48
        // Verify and correct the time zone if necessary
49
        $this->checkTimezone();
50
51
        // Language initialization
52
        $this->sCurrentLang = (new Language)->get();
53
        include_once PH7_ROOT_INSTALL . 'langs/' . $this->sCurrentLang . '/install.lang.php';
54
55
        /* Smarty initialization */
56
        $this->oView = new \Smarty;
57
        $this->oView->use_sub_dirs = true;
58
        $this->oView->setTemplateDir(PH7_ROOT_INSTALL . 'views/' . self::DEFAULT_THEME);
59
        $this->oView->setCompileDir(PH7_ROOT_INSTALL . 'data/caches/smarty_compile');
60
        $this->oView->setCacheDir(PH7_ROOT_INSTALL  . 'data/caches/smarty_cache');
61
        $this->oView->setPluginsDir(PH7_ROOT_INSTALL . 'library/Smarty/plugins');
62
        // Smarty Cache
63
        $this->oView->caching = 0; // 0 = Cache disabled |  1 = Cache never expires | 2 = Set the cache duration at "cache_lifetime" attribute
0 ignored issues
show
Documentation Bug introduced by
The property $caching was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
64
        $this->oView->cache_lifetime = 86400; // 86400 seconds = 24h
65
66
        $this->oView->assign('LANG', $LANG);
67
        $this->oView->assign('software_name', self::SOFTWARE_NAME);
68
        $this->oView->assign('software_version', self::SOFTWARE_VERSION . ' Build ' . self::SOFTWARE_BUILD . ' - ' . self::SOFTWARE_VERSION_NAME);
69
        $this->oView->assign('software_website', self::SOFTWARE_WEBSITE);
70
        $this->oView->assign('software_license_url', self::SOFTWARE_LICENSE_URL);
71
        $this->oView->assign('software_help_url', self::SOFTWARE_HELP_URL);
72
        $this->oView->assign('software_license_key_url', self::SOFTWARE_LICENSE_KEY_URL);
73
        $this->oView->assign('software_author', self::SOFTWARE_AUTHOR);
74
        $this->oView->assign('software_copyright', self::SOFTWARE_COPYRIGHT);
75
        $this->oView->assign('software_email', self::SOFTWARE_EMAIL);
76
        $this->oView->assign('tpl_name', self::DEFAULT_THEME);
77
        $this->oView->assign('current_lang', $this->sCurrentLang);
78
    }
79
80
    /**
81
     * Check if the session is already initialized (thanks "session_status()" PHP >= 5.4)
82
     * And initialize it if it isn't the case.
83
     *
84
     * @return void
85
     */
86
    protected function initializePHPSession()
87
    {
88
        if (session_status() !== PHP_SESSION_ACTIVE)
89
            @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...
90
    }
91
92
    /**
93
     * Set a default timezone if it is not already configured.
94
     *
95
     * @return void
96
     */
97
    protected function checkTimezone()
98
    {
99
        if (!ini_get('date.timezone'))
100
            date_default_timezone_set(PH7_DEFAULT_TIMEZONE);
101
    }
102
}
103