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

_doc/inc/scripts/fns.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 18 and the first side effect is on line 11.

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
 * @author      Pierre-Henry Soria
4
 * @email       [email protected]
5
 * @link        http://github.com/pH-7/Nav-Doc-Script-V2
6
 * @copyright   (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
7
 * @license     CC-BY - http://creativecommons.org/licenses/by/3.0/
8
 */
9
10
namespace PH7\Doc;
11
defined('PH7') or exit('Restricted access');
12
13
/**
14
 * Detect the user's preferred language.
15
 *
16
 * @return string The first two lowercase letter of the browser language.
17
 */
18
function get_browser_lang()
0 ignored issues
show
get_browser_lang 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...
19
{
20
    $aLang = explode(',' ,@$_SERVER['HTTP_ACCEPT_LANGUAGE']);
21
    return escape(strtolower(substr(chop($aLang[0]), 0, 2)));
22
}
23
24
/**
25
 * Display a page if the file exists, otherwise displays a 404.
26
 *
27
 * @param string $sPage The page.
28
 * @return void
29
 */
30
function get_page($sPage)
31
{
32
    if (is_file($sPage))
33
    {
34
        $sPage = file_get_contents($sPage);
35
        echo parse_var($sPage);
36
    }
37
    else
38
    {   // Set the Not Found page
39
        error_404();
40
    }
41
}
42
43
/**
44
 * Parse the text to transform variable.
45
 *
46
 * @param string $sContent The text.
47
 * @return string The text parsed.
48
 */
49
function parse_var($sContent)
50
{
51
    $sContent = str_replace('{site_url}', RELATIVE, $sContent);
52
    $sContent = str_replace('{static_url}', STATIC_URL, $sContent);
53
    $sContent = str_replace('{lang}', LANG, $sContent);
54
    $sContent = str_replace('{tpl_name}', TPL, $sContent);
55
    $sContent = str_replace('{site_name}', SITE_NAME, $sContent);
56
    $sContent = str_replace('{page_name}', get_page_name(), $sContent);
57
    $sContent = str_replace('{menu_links}', get_links_html(), $sContent);
58
    $sContent = str_replace('{menu_langs}', get_langs_html(), $sContent);
59
    $sContent = str_replace('{year}', date('Y'), $sContent);
60
61
    return $sContent;
62
}
63
64
/**
65
 * Get the page name.
66
 *
67
 * @return string
68
 */
69
function get_page_name()
0 ignored issues
show
get_page_name 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...
70
{
71
    if (empty($_GET['p']))
72
    {
73
        $sName = SITE_SLOGAN;
74
    }
75
    else
76
    {
77
        $sPageName = str_replace(array('-','_'), ' ', $_GET['p']);
78
        $sName = ucfirst($sPageName);
79
    }
80
81
    return $sName;
82
}
83
84
/**
85
 * @see set_lang()
86
 * @return string The language available.
87
 */
88
function get_lang()
89
{
90
    return set_lang();
91
}
92
93
/**
94
 * @param string $sDir The directory.
95
 * @return string The list of the folder that is in the directory.
96
 */
97
function get_dir_list($sDir)
98
{
99
    $aDirList = array();
100
101
    if ($rHandle = opendir($sDir))
102
    {
103
        while(false !== ($sFile = readdir($rHandle)))
104
        {
105
            if ($sFile != '.' && $sFile != '..' && is_dir($sDir . '/' . $sFile))
106
                $aDirList[] = $sFile;
107
        }
108
        closedir($rHandle);
109
        asort($aDirList);
110
        reset($aDirList);
111
    }
112
113
    return $aDirList;
114
}
115
116
/**
117
 * @return string The current URL.
118
 */
119
function get_current_url()
0 ignored issues
show
get_current_url 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...
120
{
121
    return PROT_URL . escape($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
122
}
123
124
/**
125
 * Check if the language folder and the language core folder exists.
126
 *
127
 * @return string The language available.
128
 */
129
function set_lang()
0 ignored issues
show
set_lang 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...
set_lang 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...
130
{
131
    if (!empty($_GET['l']) && is_file(DATA_PATH . $_GET['l'] . '/core/welcome.tpl') && is_file(DATA_PATH . $_GET['l'] . '/core/404-error.tpl'))
132
    {
133
        setcookie('pH7_doc_lang', $_GET['l'], time()+60*60*24*365, null, null, false, true);
134
        $sLang = $_GET['l'];
135
    }
136
    elseif (isset($_COOKIE['pH7_doc_lang']) && is_dir(DATA_PATH . $_COOKIE['pH7_doc_lang'] . '/core/'))
137
    {
138
        $sLang = $_COOKIE['pH7_doc_lang'];
139
    }
140
    elseif (is_dir(DATA_PATH . get_browser_lang() . '/core/'))
141
    {
142
        $sLang = get_browser_lang();
143
    }
144
    else
145
    {
146
        $sLang = DEF_LANG;
147
    }
148
149
    return $sLang;
150
}
151
152
/**
153
 * Escape string with htmlspecialchars() PHP function.
154
 *
155
 * @param string $sVal
156
 * @return string
157
 */
158
function escape($sVal)
159
{
160
    return htmlspecialchars($sVal, ENT_QUOTES);
161
}
162
163
/**
164
 * Sets an error 404 page with HTTP 404 code status.
165
 *
166
 * @return void
167
 */
168
function error_404()
169
{
170
    header('HTTP/1.1 404 Not Found');
171
    get_page(DATA_PATH . LANG . '/core/404-error.tpl');
172
}
173