Completed
Push — master ( 40b8dd...0c2ef0 )
by Patrick
08:55
created

WebPage::addTemplateDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
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();
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...
18
        $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...
19
        $twigSettings = array('cache' => '/var/php_cache/twig');
20
        if(isset($GLOBALS['TWIG_CACHE']))
21
        {
22
            $twigSettings = $twigSettings = array('cache' => $GLOBALS['TWIG_CACHE']);
23
        }
24
        $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...
25
        $this->content = array('pageTitle' => $title);
0 ignored issues
show
Bug introduced by
The property content 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->user = \FlipSession::getUser();
0 ignored issues
show
Bug introduced by
The property user 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->content['header'] = array();
28
        $this->content['header']['sites'] = array();
29
        $wwwUri = $this->settings->getGlobalSetting('www_url', 'https://www.burningflipside.com');
30
        $this->content['header']['sites']['Profiles'] = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com/');
31
        $this->content['header']['sites']['WWW'] = $wwwUri;
32
        $this->content['header']['sites']['Pyropedia'] = $this->settings->getGlobalSetting('wiki_url', 'https://wiki.burningflipside.com');
33
        $this->content['header']['sites']['Secure'] = $this->settings->getGlobalSetting('secure_url', 'https://secure.burningflipside.com/');
34
35
        $aboutUrl = $this->settings->getGlobalSetting('about_url', $wwwUri.'/about');
36
        $this->content['header']['right']['About'] = array(
37
          'url' => $aboutUrl,
38
          'menu' => $this->settings->getGlobalSetting('about_menu', array(
39
            'Burning Flipside' => $wwwUri.'/about/event',
40
            'AAR, LLC' => $wwwUri.'/organization/aar',
41
            'Privacy Policy' => $wwwUri.'/about/privacy'
42
        )));
43
44
        $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...
45
        $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...
46
        $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...
47
        $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...
48
49 View Code Duplication
	if($this->user === false || $this->user === null)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
50
        {
51
            if(isset($_SERVER['REQUEST_URI']) && strstr($_SERVER['REQUEST_URI'], 'logout.php') === false)
52
            {
53
                $this->addLink('Login', $this->loginUrl);
54
            }
55
        }
56
        else
57
        {
58
            $this->addLink('Logout', $this->settings->getGlobalSetting('logout_url', $this->profilesUrl.'/logout.php'));
59
            $this->addLinks();
60
        }
61
        $this->content['js'] = array('js/'.basename($_SERVER['SCRIPT_NAME'], '.php').'.js');
62
63
        $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...
64
        $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...
65
        if($this->settings->getGlobalSetting('use_minified', true) == false)
66
        {
67
            $this->minified = 'no';
68
        }
69
        if($this->settings->getGlobalSetting('use_cdn', true) == false)
70
        {
71
            $this->cdn = 'no';
72
            $this->content['useCDN'] = false;
73
        }
74
    }
75
76
    public function addTemplateDir($dir, $namespace)
77
    {
78
        $this->loader->addPath($dir, $namespace);
79
    }
80
81
    public function setTemplateName($name)
82
    {
83
        $this->templateName = $name;
84
    }
85
86 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...
87
    {
88
        if(!isset($this->content['css']))
89
        {
90
            $this->content['css'] = array($uri);
91
            return;
92
        }
93
        array_push($this->content['css'],$uri);
94
    }
95
96
    /**
97
     * Add a JavaScript file from its src URI
98
     *
99
     * @param string $uri The webpath to the JavaScript file
100
     */
101 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...
102
    {
103
        if(!isset($this->content['js']))
104
        {
105
            $this->content['js'] = array($uri);
106
            return;
107
        }
108
        array_push($this->content['js'],$uri);
109
    }
110
111
    /**
112
     * Add a JavaScript file from a set of files known to the framework
113
     *
114
     * @param string $jsFileID the ID of the JS file
115
     * @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...
116
     */
117
    public function addWellKnownJS($jsFileID)
118
    {
119
        global $jsArray;
120
        $src = $jsArray[$jsFileID][$this->cdn][$this->minified];
121
        $this->addJS($src);
122
    }
123
124
    /**
125
     * Add a CSS file from a set of files known to the framework
126
     *
127
     * @param string $cssFileID the ID of the CSS file
128
     */
129
    public function addWellKnownCSS($cssFileID)
130
    {
131
        global $cssArray;
132
        $src = $cssArray[$cssFileID][$this->cdn][$this->minified];
133
        $this->addCSS($src);
134
    }
135
136
    /**
137
     * Add a link to the header
138
     *
139
     * @param string $name The name of the link
140
     * @param boolean|string $url The URL to link to
141
     * @param boolean|array $submenu Any submenu items for the dropdown
142
     */
143
    public function addLink($name, $url = false, $submenu = false)
144
    {
145
        $data = array('url' => $url);
146
        if(is_array($submenu))
147
        {
148
            $data['menu'] = $submenu;
149
        }
150
        $this->content['header']['right'] = array($name => $data)+$this->content['header']['right'];
151
    }
152
153
    protected function addLinks()
154
    {
155
    }
156
157
    protected function getContent()
158
    {
159
        if(!isset($this->content['body']))
160
        {
161
          $this->content['body'] = $this->body;
162
        }
163
        return $this->twig->render($this->templateName, $this->content);
164
    }
165
166
    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...
167
    {
168
        $body = $response->getBody();
169
        $body->write($this->getContent());
170
        return $response;
171
    }
172
173
    public function printPage()
174
    {
175
        echo $this->getContent();
176
    }
177
178
    /**
179
     * Get the currently requested URL
180
     *
181
     * @return string The full URL of the requested page
182
     *
183
     * @SuppressWarnings("Superglobals")
184
     */
185 View Code Duplication
    public function currentURL()
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...
186
    {
187
        if(!isset($_SERVER['REQUEST_URI']))
188
        {
189
            return '';
190
        }
191
        $requestURI = $_SERVER['REQUEST_URI'];
192
        if($requestURI[0] === '/')
193
        {
194
            $requestURI = substr($requestURI, 1);
195
        }
196
        return 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].'/'.$requestURI;
197
    }
198
}
199
200