Completed
Push — master ( c9e75c...9f5802 )
by Patrick
12:31 queued 10:30
created

Http/class.WebPage.php (2 issues)

Severity

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
2
namespace Http;
3
4
use \Psr\Http\Message\ServerRequestInterface as Request;
5
use \Psr\Http\Message\ResponseInterface as Response;
6
7
require 'vendor/autoload.php';
8
require_once('static.js_css.php');
9
10
class WebPage
11
{
12
    public $body = '';
13
    protected $templateName = 'main.html';
14
15
    public function __construct($title)
16
    {
17
        $this->settings = \Settings::getInstance();
18
        $this->loader = new \Twig_Loader_Filesystem(dirname(__FILE__).'/../templates');
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_Filesystem has been deprecated with message: since Twig 2.7, use "Twig\Loader\FilesystemLoader" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
        
20
        $twigCache = $this->settings->getGlobalSetting('twig_cache', '/var/php_cache/twig');
21
        $twigSettings = array('cache' => $twigCache);
22
23
        $this->wwwUrl = $this->settings->getGlobalSetting('www_url', 'https://www.burningflipside.com');
24
        $this->wikiUrl = $this->settings->getGlobalSetting('wiki_url', 'https://wiki.burningflipside.com');
25
        $this->secureUrl = $this->settings->getGlobalSetting('secure_url', 'https://secure.burningflipside.com');
26
        $this->profilesUrl = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com');
27
        $this->registerUrl = $this->settings->getGlobalSetting('register_url', $this->profilesUrl.'/register.php');
28
        $this->resetUrl = $this->settings->getGlobalSetting('reset_url', $this->profilesUrl.'/reset.php');
29
        $this->loginUrl = $this->settings->getGlobalSetting('login_url', $this->profilesUrl.'/login.php');
30
        $this->logoutUrl = $this->settings->getGlobalSetting('logout_url', $this->profilesUrl.'/logout.php');
31
32
        $this->twig = new \Twig_Environment($this->loader, $twigSettings);
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Environment has been deprecated with message: since Twig 2.7, use "Twig\Environment" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
33
        $this->content = array('pageTitle' => $title);
34
        $this->user = \FlipSession::getUser();
35
        $this->content['header'] = array();
36
        $this->content['header']['sites'] = array();
37
        $this->content['header']['sites']['Profiles'] = $this->profilesUrl;
38
        $this->content['header']['sites']['WWW'] = $this->wwwUrl;
39
        $this->content['header']['sites']['Pyropedia'] = $this->wikiUrl;
40
        $this->content['header']['sites']['Secure'] = $this->secureUrl;
41
42
        $this->aboutUrl = $this->settings->getGlobalSetting('about_url', $this->wwwUrl.'/about');
43
        $this->content['header']['right']['About'] = array(
44
          'url' => $this->aboutUrl,
45
          'menu' => $this->settings->getGlobalSetting('about_menu', array(
46
            'Burning Flipside' => $this->wwwUrl.'/about/event',
47
            'AAR, LLC' => $this->wwwUrl.'/organization/aar',
48
            'Privacy Policy' => $this->wwwUrl.'/about/privacy'
49
        )));
50
51
        $this->content['urls']['wwwUrl'] = $this->wwwUrl;
52
        $this->content['urls']['wikiUrl'] = $this->wikiUrl;
53
        $this->content['urls']['profilesUrl'] = $this->profilesUrl;
54
        $this->content['urls']['secureUrl'] = $this->secureUrl;
55
56
        $this->content['urls']['registerUrl'] = $this->registerUrl;
57
        $this->content['urls']['resetUrl'] = $this->resetUrl;
58
        $this->content['urls']['loginUrl'] = $this->loginUrl;
59
        $this->content['urls']['logoutUrl'] = $this->logoutUrl;
60
61
	if($this->user === false || $this->user === null)
62
        {
63
            if(isset($_SERVER['REQUEST_URI']) && strstr($_SERVER['REQUEST_URI'], 'logout.php') === false)
64
            {
65
                $this->addLink('Login', $this->loginUrl);
66
            }
67
        }
68
        else
69
        {
70
            $this->addLink('Logout', $this->settings->getGlobalSetting('logout_url', $this->profilesUrl.'/logout.php'));
71
            $this->addLinks();
72
        }
73
74
        $this->minified = 'min';
75
        $this->cdn      = 'cdn';
76
        if($this->settings->getGlobalSetting('use_minified', true) == false)
77
        {
78
            $this->minified = 'no';
79
        }
80
        if($this->settings->getGlobalSetting('use_cdn', true) == false)
81
        {
82
            $this->cdn = 'no';
83
            $this->content['useCDN'] = false;
84
        }
85
    }
86
87
    public function addTemplateDir($dir, $namespace)
88
    {
89
        $this->loader->addPath($dir, $namespace);
90
    }
91
92
    public function setTemplateName($name)
93
    {
94
        $this->templateName = $name;
95
    }
96
97 View Code Duplication
    public function addCSS($uri)
98
    {
99
        if(!isset($this->content['css']))
100
        {
101
            $this->content['css'] = array($uri);
102
            return;
103
        }
104
        array_push($this->content['css'],$uri);
105
    }
106
107
    /**
108
     * Add a JavaScript file from its src URI
109
     *
110
     * @param string $uri The webpath to the JavaScript file
111
     */
112 View Code Duplication
    public function addJS($uri)
113
    {
114
        if(!isset($this->content['js']))
115
        {
116
            $this->content['js'] = array($uri);
117
            return;
118
        }
119
        array_push($this->content['js'],$uri);
120
    }
121
122
    /**
123
     * Add a JavaScript file from a set of files known to the framework
124
     *
125
     * @param string $jsFileID the ID of the JS file
126
     * @param boolean $async Can the JS file be loaded asynchronously?
127
     */
128
    public function addWellKnownJS($jsFileID)
129
    {
130
        global $jsArray;
131
        $src = $jsArray[$jsFileID][$this->cdn][$this->minified];
132
        if(is_array($src))
133
        {
134
            if(!isset($this->content['securejs']))
135
            {
136
                $this->content['securejs'] = array();
137
            }
138
            array_push($this->content['securejs'], $src);
139
        }
140
        else
141
        {
142
            $this->addJS($src);
143
        }
144
    }
145
146
    /**
147
     * Add a CSS file from a set of files known to the framework
148
     *
149
     * @param string $cssFileID the ID of the CSS file
150
     */
151
    public function addWellKnownCSS($cssFileID)
152
    {
153
        global $cssArray;
154
        $src = $cssArray[$cssFileID][$this->cdn][$this->minified];
155
        $this->addCSS($src);
156
    }
157
158
    /**
159
     * Add a link to the header
160
     *
161
     * @param string $name The name of the link
162
     * @param boolean|string $url The URL to link to
163
     * @param boolean|array $submenu Any submenu items for the dropdown
164
     */
165
    public function addLink($name, $url = false, $submenu = false)
166
    {
167
        $data = array('url' => $url);
168
        if(is_array($submenu))
169
        {
170
            $data['menu'] = $submenu;
171
        }
172
        $this->content['header']['right'] = array($name => $data)+$this->content['header']['right'];
173
    }
174
175
    /** Notification that is green for success */
176
    const NOTIFICATION_SUCCESS = 'alert-success';
177
    /** Notification that is blue for infomrational messages */
178
    const NOTIFICATION_INFO    = 'alert-info';
179
    /** Notification that is yellow for warning */
180
    const NOTIFICATION_WARNING = 'alert-warning';
181
    /** Notification that is red for error */
182
    const NOTIFICATION_FAILED  = 'alert-danger';
183
184
    /**
185
     * Add a notification to the page
186
     *
187
     * @param string $message The message to show in the notifcation
188
     * @param string $severity The severity of the notifcation
189
     * @param boolean $dismissible Can the user dismiss the notificaton?
190
     */
191
    public function addNotification($message, $severity = self::NOTIFICATION_INFO, $dismissible = true)
192
    {
193
        if(!isset($this->content['notifications']))
194
        {
195
          $this->content['notifications'] = array();
196
        }
197
        array_push($this->content['notifications'], array('msg'=>$message, 'sev'=>$severity, 'dismissible'=>$dismissible));
198
    }
199
200
    protected function addLinks()
201
    {
202
    }
203
204
    protected function getContent()
205
    {
206
        if(!isset($this->content['body']))
207
        {
208
          $this->content['body'] = $this->body;
209
        }
210
        //Add page JS just before rednering so it is after any added by the page explicitly
211
        // $this->addJS('js/'.basename($_SERVER['SCRIPT_NAME'], '.php').'.js');
212
        // this code assumes *.php pages have a corresponding *.js file (many don't)
213
        return $this->twig->render($this->templateName, $this->content);
214
    }
215
216
    public function handleRequest($request, $response, $args)
217
    {
218
        $body = $response->getBody();
219
        $body->write($this->getContent());
220
        return $response;
221
    }
222
223
    public function printPage()
224
    {
225
        echo $this->getContent();
226
    }
227
228
    /**
229
     * Get the currently requested URL
230
     *
231
     * @return string The full URL of the requested page
232
     *
233
     * @SuppressWarnings("Superglobals")
234
     */
235
    public function currentURL()
236
    {
237
        if(!isset($_SERVER['REQUEST_URI']))
238
        {
239
            return '';
240
        }
241
        $requestURI = $_SERVER['REQUEST_URI'];
242
        if($requestURI[0] === '/')
243
        {
244
            $requestURI = substr($requestURI, 1);
245
        }
246
        return 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].'/'.$requestURI;
247
    }
248
}
249
250