Completed
Push — master ( f22a46...046041 )
by Patrick
02:27 queued 12s
created

WebPage::addWellKnownJS()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
namespace Flipside\Http;
3
4
use \Psr\Http\Message\ServerRequestInterface as Request;
5
use \Psr\Http\Message\ResponseInterface as Response;
6
7
require __DIR__ . '/../vendor/autoload.php';
8
require_once(__DIR__ . '/../static.js_css.php');
9
10
class WebPage
11
{
12
    public $body = '';
13
    protected $templateName = 'main.html';
14
    public $content;
15
    public $user;
16
17
    public function __construct($title)
18
    {
19
        \Sentry\init(['dsn' => 'https://[email protected]/4283882' ]);
20
        $this->settings = \Flipside\Settings::getInstance();
0 ignored issues
show
Bug introduced by
The property settings 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...
21
        $this->loader = new \Twig_Loader_Filesystem(dirname(__FILE__).'/../templates');
0 ignored issues
show
Bug introduced by
The property loader 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...
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...
22
23
        $twigSettings = array();
24
        if(\file_exists('/var/php_cache/twig'))
25
    	{	
26
        	$twigCache = $this->settings->getGlobalSetting('twig_cache', '/var/php_cache/twig');
27
        	$twigSettings['cache'] = $twigCache;
28
		    //$twigSettings = array('cache' => $twigCache, 'debug' => true);
29
        }
30
        //$twigSettings['debug'] = true;
31
32
        $this->wwwUrl = $this->settings->getGlobalSetting('www_url', 'https://www.burningflipside.com');
0 ignored issues
show
Bug introduced by
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...
33
        $this->wikiUrl = $this->settings->getGlobalSetting('wiki_url', 'https://wiki.burningflipside.com');
0 ignored issues
show
Bug introduced by
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...
34
        $this->secureUrl = $this->settings->getGlobalSetting('secure_url', 'https://secure.burningflipside.com');
0 ignored issues
show
Bug introduced by
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...
35
        $this->profilesUrl = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com');
0 ignored issues
show
Bug introduced by
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...
36
        $this->registerUrl = $this->settings->getGlobalSetting('register_url', $this->profilesUrl.'/register.php');
0 ignored issues
show
Bug introduced by
The property registerUrl 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...
37
        $this->resetUrl = $this->settings->getGlobalSetting('reset_url', $this->profilesUrl.'/reset.php');
0 ignored issues
show
Bug introduced by
The property resetUrl 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...
38
        $this->loginUrl = $this->settings->getGlobalSetting('login_url', $this->profilesUrl.'/login.php');
0 ignored issues
show
Bug introduced by
The property loginUrl 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...
39
        $this->logoutUrl = $this->settings->getGlobalSetting('logout_url', $this->profilesUrl.'/logout.php');
0 ignored issues
show
Bug introduced by
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...
40
41
        $this->twig = new \Twig_Environment($this->loader, $twigSettings);
0 ignored issues
show
Bug introduced by
The property twig 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...
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...
42
        //$this->twig->addExtension(new \Twig\Extension\DebugExtension());
43
        $this->content = array('pageTitle' => $title);
44
        $this->user = \Flipside\FlipSession::getUser();
45
        $this->content['user'] = $this->user;
46
        $this->content['header'] = array();
47
        $this->content['header']['sites'] = array();
48
        $this->content['header']['sites']['Profiles'] = $this->profilesUrl;
49
        $this->content['header']['sites']['WWW'] = $this->wwwUrl;
50
        $this->content['header']['sites']['Pyropedia'] = $this->wikiUrl;
51
        $this->content['header']['sites']['Secure'] = $this->secureUrl;
52
53
        $this->aboutUrl = $this->settings->getGlobalSetting('about_url', $this->wwwUrl.'/about');
0 ignored issues
show
Bug introduced by
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...
54
        $this->content['header']['right']['About'] = array(
55
          'url' => $this->aboutUrl,
56
          'menu' => $this->settings->getGlobalSetting('about_menu', array(
57
            'Burning Flipside' => $this->wwwUrl.'/about/event',
58
            'AAR, LLC' => $this->wwwUrl.'/organization/aar',
59
            'Privacy Policy' => $this->wwwUrl.'/about/privacy'
60
        )));
61
62
        $this->content['urls']['wwwUrl'] = $this->wwwUrl;
63
        $this->content['urls']['wikiUrl'] = $this->wikiUrl;
64
        $this->content['urls']['profilesUrl'] = $this->profilesUrl;
65
        $this->content['urls']['secureUrl'] = $this->secureUrl;
66
67
        $this->content['urls']['registerUrl'] = $this->registerUrl;
68
        $this->content['urls']['resetUrl'] = $this->resetUrl;
69
        $this->content['urls']['loginUrl'] = $this->loginUrl;
70
        $this->content['urls']['logoutUrl'] = $this->logoutUrl;
71
72
	if($this->user === false || $this->user === null)
73
        {
74
            if(isset($_SERVER['REQUEST_URI']) && strstr($_SERVER['REQUEST_URI'], 'logout.php') === false)
75
            {
76
                $this->addLink('Login', $this->loginUrl);
77
            }
78
        }
79
        else
80
        {
81
            $this->addLink('Logout', $this->settings->getGlobalSetting('logout_url', $this->profilesUrl.'/logout.php'));
82
            $this->addLinks();
83
        }
84
85
        $this->minified = 'min';
0 ignored issues
show
Bug introduced by
The property minified 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...
86
        $this->cdn      = 'cdn';
0 ignored issues
show
Bug introduced by
The property cdn 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...
87
        if($this->settings->getGlobalSetting('use_minified', true) == false)
88
        {
89
            $this->minified = 'no';
90
        }
91
        if($this->settings->getGlobalSetting('use_cdn', true) == false)
92
        {
93
            $this->cdn = 'no';
94
            $this->content['useCDN'] = false;
95
        }
96
    }
97
98
    public function addTemplateDir($dir, $namespace)
99
    {
100
        $this->loader->addPath($dir, $namespace);
101
    }
102
103
    public function setTemplateName($name)
104
    {
105
        $this->templateName = $name;
106
    }
107
108 View Code Duplication
    public function addCSS($uri)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        if(!isset($this->content['css']))
111
        {
112
            $this->content['css'] = array($uri);
113
            return;
114
        }
115
        array_push($this->content['css'],$uri);
116
    }
117
118
    /**
119
     * Add a JavaScript file from its src URI
120
     *
121
     * @param string $uri The webpath to the JavaScript file
122
     */
123 View Code Duplication
    public function addJS($uri)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        if(!isset($this->content['js']))
126
        {
127
            $this->content['js'] = array($uri);
128
            return;
129
        }
130
        array_push($this->content['js'],$uri);
131
    }
132
133
    /**
134
     * Add a JavaScript file from a set of files known to the framework
135
     *
136
     * @param string $jsFileID the ID of the JS file
137
     * @param boolean $async Can the JS file be loaded asynchronously?
0 ignored issues
show
Bug introduced by
There is no parameter named $async. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
138
     */
139
    public function addWellKnownJS($jsFileID)
140
    {
141
        global $jsArray;
142
        $src = $jsArray[$jsFileID][$this->cdn][$this->minified];
143
        if(is_array($src))
144
        {
145
            if(!isset($this->content['securejs']))
146
            {
147
                $this->content['securejs'] = array();
148
            }
149
            array_push($this->content['securejs'], $src);
150
        }
151
        else
152
        {
153
            $this->addJS($src);
154
        }
155
    }
156
157
    /**
158
     * Add a CSS file from a set of files known to the framework
159
     *
160
     * @param string $cssFileID the ID of the CSS file
161
     */
162
    public function addWellKnownCSS($cssFileID)
163
    {
164
        global $cssArray;
165
        $src = $cssArray[$cssFileID][$this->cdn][$this->minified];
166
        $this->addCSS($src);
167
    }
168
169
    /**
170
     * Add a link to the header
171
     *
172
     * @param string $name The name of the link
173
     * @param boolean|string $url The URL to link to
174
     * @param boolean|array $submenu Any submenu items for the dropdown
175
     */
176
    public function addLink($name, $url = false, $submenu = false)
177
    {
178
        $data = array('url' => $url);
179
        if(is_array($submenu))
180
        {
181
            $data['menu'] = $submenu;
182
        }
183
        $this->content['header']['right'] = array($name => $data)+$this->content['header']['right'];
184
    }
185
186
    /** Notification that is green for success */
187
    const NOTIFICATION_SUCCESS = 'alert-success';
188
    /** Notification that is blue for infomrational messages */
189
    const NOTIFICATION_INFO    = 'alert-info';
190
    /** Notification that is yellow for warning */
191
    const NOTIFICATION_WARNING = 'alert-warning';
192
    /** Notification that is red for error */
193
    const NOTIFICATION_FAILED  = 'alert-danger';
194
195
    /**
196
     * Add a notification to the page
197
     *
198
     * @param string $message The message to show in the notifcation
199
     * @param string $severity The severity of the notifcation
200
     * @param boolean $dismissible Can the user dismiss the notificaton?
201
     */
202
    public function addNotification($message, $severity = self::NOTIFICATION_INFO, $dismissible = true)
203
    {
204
        if(!isset($this->content['notifications']))
205
        {
206
          $this->content['notifications'] = array();
207
        }
208
        array_push($this->content['notifications'], array('msg'=>$message, 'sev'=>$severity, 'dismissible'=>$dismissible));
209
    }
210
211
    protected function addLinks()
212
    {
213
    }
214
215
    protected function getContent()
216
    {
217
        if(!isset($this->content['body']))
218
        {
219
          $this->content['body'] = $this->body;
220
        }
221
        //Add page JS just before rednering so it is after any added by the page explicitly
222
        // $this->addJS('js/'.basename($_SERVER['SCRIPT_NAME'], '.php').'.js');
223
        // this code assumes *.php pages have a corresponding *.js file (many don't)
224
        return $this->twig->render($this->templateName, $this->content);
225
    }
226
227
    public function handleRequest($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
228
    {
229
        $body = $response->getBody();
230
        $body->write($this->getContent());
231
        return $response;
232
    }
233
234
    public function printPage()
235
    {
236
        echo $this->getContent();
237
    }
238
239
    /**
240
     * Get the currently requested URL
241
     *
242
     * @return string The full URL of the requested page
243
     *
244
     * @SuppressWarnings("Superglobals")
245
     */
246
    public function currentURL()
247
    {
248
        if(!isset($_SERVER['REQUEST_URI']))
249
        {
250
            return '';
251
        }
252
        $requestURI = $_SERVER['REQUEST_URI'];
253
        if($requestURI[0] === '/')
254
        {
255
            $requestURI = substr($requestURI, 1);
256
        }
257
        return 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].'/'.$requestURI;
258
    }
259
}
260
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
261