Completed
Pull Request — develop (#92)
by Patrick
02:39
created

Http/class.WebPage.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
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');
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/');
0 ignored issues
show
The property wwwUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $this->wikiUrl = $this->settings->getGlobalSetting('wiki_url', 'https://wiki.burningflipside.com/');
0 ignored issues
show
The property wikiUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->secureUrl = $this->settings->getGlobalSetting('secure_url', 'https://secure.burningflipside.com/');
0 ignored issues
show
The property secureUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
        $this->profilesUrl = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com/');
0 ignored issues
show
The property profilesUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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');
0 ignored issues
show
The property logoutUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
32
        $this->twig = new \Twig_Environment($this->loader, $twigSettings);
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');
0 ignored issues
show
The property aboutUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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 View Code Duplication
	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
        $this->addJS($src);
133
    }
134
135
    /**
136
     * Add a CSS file from a set of files known to the framework
137
     *
138
     * @param string $cssFileID the ID of the CSS file
139
     */
140
    public function addWellKnownCSS($cssFileID)
141
    {
142
        global $cssArray;
143
        $src = $cssArray[$cssFileID][$this->cdn][$this->minified];
144
        $this->addCSS($src);
145
    }
146
147
    /**
148
     * Add a link to the header
149
     *
150
     * @param string $name The name of the link
151
     * @param boolean|string $url The URL to link to
152
     * @param boolean|array $submenu Any submenu items for the dropdown
153
     */
154
    public function addLink($name, $url = false, $submenu = false)
155
    {
156
        $data = array('url' => $url);
157
        if(is_array($submenu))
158
        {
159
            $data['menu'] = $submenu;
160
        }
161
        $this->content['header']['right'] = array($name => $data)+$this->content['header']['right'];
162
    }
163
164
    /** Notification that is green for success */
165
    const NOTIFICATION_SUCCESS = 'alert-success';
166
    /** Notification that is blue for infomrational messages */
167
    const NOTIFICATION_INFO    = 'alert-info';
168
    /** Notification that is yellow for warning */
169
    const NOTIFICATION_WARNING = 'alert-warning';
170
    /** Notification that is red for error */
171
    const NOTIFICATION_FAILED  = 'alert-danger';
172
173
    /**
174
     * Add a notification to the page
175
     *
176
     * @param string $message The message to show in the notifcation
177
     * @param string $severity The severity of the notifcation
178
     * @param boolean $dismissible Can the user dismiss the notificaton?
179
     */
180
    public function addNotification($message, $severity = self::NOTIFICATION_INFO, $dismissible = true)
181
    {
182
        if(!isset($this->content['notifications']))
183
        {
184
          $this->content['notifications'] = array();
185
        }
186
        array_push($this->content['notifications'], array('msg'=>$message, 'sev'=>$severity, 'dismissible'=>$dismissible));
187
    }
188
189
    protected function addLinks()
190
    {
191
    }
192
193
    protected function getContent()
194
    {
195
        if(!isset($this->content['body']))
196
        {
197
          $this->content['body'] = $this->body;
198
        }
199
	//Add page JS just before rednering so it is after any added by the page explicitly
200
        $this->addJS('js/'.basename($_SERVER['SCRIPT_NAME'], '.php').'.js');
201
        return $this->twig->render($this->templateName, $this->content);
202
    }
203
204
    public function handleRequest($request, $response, $args)
205
    {
206
        $body = $response->getBody();
207
        $body->write($this->getContent());
208
        return $response;
209
    }
210
211
    public function printPage()
212
    {
213
        echo $this->getContent();
214
    }
215
216
    /**
217
     * Get the currently requested URL
218
     *
219
     * @return string The full URL of the requested page
220
     *
221
     * @SuppressWarnings("Superglobals")
222
     */
223 View Code Duplication
    public function currentURL()
224
    {
225
        if(!isset($_SERVER['REQUEST_URI']))
226
        {
227
            return '';
228
        }
229
        $requestURI = $_SERVER['REQUEST_URI'];
230
        if($requestURI[0] === '/')
231
        {
232
            $requestURI = substr($requestURI, 1);
233
        }
234
        return 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].'/'.$requestURI;
235
    }
236
}
237
238