Completed
Push — master ( b029a9...ae7e79 )
by Patrick
03:01
created

class.FlipPage.php (4 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
/**
3
 * A WebPage class specific to this framework
4
 *
5
 * This file describes an abstraction for creating a webpage with JQuery, Bootstrap,
6
 * and other framework specific abilities
7
 *
8
 * PHP version 5 and 7
9
 *
10
 * @author Patrick Boyd / [email protected]
11
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
12
 * @license http://www.apache.org/licenses/ Apache 2.0 License
13
 */
14
15
/**
16
 * We use the FlipsideSettings class for a list of sites and settings
17
 * about CDNs and minified JS/CSS
18
 */
19
if(isset($GLOBALS['FLIPSIDE_SETTINGS_LOC']))
20
{
21
    require_once($GLOBALS['FLIPSIDE_SETTINGS_LOC'].'/class.FlipsideSettings.php');
22
}
23
else
24
{
25
    require_once('/var/www/secure_settings/class.FlipsideSettings.php');
26
}
27
28
/**
29
 * We need the parent class
30
 */
31
require_once('class.WebPage.php');
32
33
define('JS_JQUERY', 0);
34
define('JS_JQUERY_UI', 1);
35
define('JS_BOOTSTRAP', 2);
36
define('JQUERY_VALIDATE', 3);
37
define('JQUERY_TOUCH', 4);
38
define('JS_TINYNAV', 5);
39
define('JS_BOOTSTRAP_FH', 6);
40
define('JS_BOOTSTRAP_SW', 7);
41
define('JS_DATATABLE', 8);
42
define('JS_CHART', 9);
43
define('JS_METISMENU', 10);
44
define('JS_BOOTBOX', 11);
45
define('JS_DATATABLE_ODATA', 12);
46
define('JS_CRYPTO_MD5_JS', 13);
47
define('JS_JCROP', 14);
48
define('JS_TYPEAHEAD', 15);
49
define('JS_FLIPSIDE', 20);
50
define('JS_LOGIN', 21);
51
52
define('CSS_JQUERY_UI', 0);
53
define('CSS_BOOTSTRAP', 1);
54
define('CSS_BOOTSTRAP_FH', 2);
55
define('CSS_BOOTSTRAP_SW', 3);
56
define('CSS_DATATABLE', 4);
57
define('CSS_JCROP', 5);
58
define('CSS_FONTAWESOME', 6);
59
60
global $jsArray;
61
$jsArray = array(
62
     JS_JQUERY => array(
63
         'no' => array(
64
             'no'  => '/js/common/jquery.js',
65
             'min' => '/js/common/jquery.min.js'
66
         ),
67
         'cdn' => array(
68
             'no'  => '//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.js',
69
             'min' => '//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'
70
         )
71
     ),
72
     JS_JQUERY_UI => array(
73
         'no' => array(
74
             'no'  => '/js/common/jquery-ui.js',
75
             'min' => '/js/common/jquery-ui.min.js'
76
         ),
77
         'cdn' => array(
78
             'no'  => '//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js',
79
             'min' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js'
80
         )
81
     ),
82
     JS_BOOTSTRAP => array(
83
         'no' => array(
84
             'no'  => '/js/common/bootstrap.js',
85
             'min' => '/js/common/bootstrap.min.js'
86
         ),
87
         'cdn' => array(
88
             'no'  => '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.js',
89
             'min' => '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js'
90
         )
91
     ),
92
     JQUERY_VALIDATE => array(
93
         'no' => array(
94
             'no'  => '/js/common/jquery.validate.js',
95
             'min' => '/js/common/jquery.validate.min.js'
96
         ),
97
         'cdn' => array(
98
             'no'  => '//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js',
99
             'min' => '//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js'
100
         )
101
     ),
102
     JQUERY_TOUCH => array(
103
         'no' => array(
104
             'no'  => '/js/common/jquery.ui.touch-punch.min.js',
105
             'min' => '/js/common/jquery.ui.touch-punch.min.js'
106
         ),
107
         'cdn' => array(
108
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js',
109
             'min' => '//cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js'
110
         )
111
     ),
112
     JS_TINYNAV => array(
113
         'no' => array(
114
             'no'  => '/js/common/tinynav.js',
115
             'min' => '/js/common/tinynav.min.js'
116
         ),
117
         'cdn' => array(
118
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/TinyNav.js/1.2.0/tinynav.js',
119
             'min' => '//cdnjs.cloudflare.com/ajax/libs/TinyNav.js/1.2.0/tinynav.min.js'
120
         )
121
     ),
122
     JS_BOOTSTRAP_FH => array(
123
         'no' => array(
124
             'no'  => '/js/common/bootstrap-formhelpers.js',
125
             'min' => '/js/common/bootstrap-formhelpers.min.js'
126
         ),
127
         'cdn' => array(
128
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/bootstrap-formhelpers/2.3.0/js/bootstrap-formhelpers.js',
129
             'min' => '//cdnjs.cloudflare.com/ajax/libs/bootstrap-formhelpers/2.3.0/js/bootstrap-formhelpers.min.js'
130
         )
131
     ),
132
     JS_BOOTSTRAP_SW => array(
133
         'no' => array(
134
             'no'  => '/js/common/bootstrap-switch.js',
135
             'min' => '/js/common/bootstrap-switch.min.js'
136
         ),
137
         'cdn' => array(
138
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.js',
139
             'min' => '//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js'
140
         )
141
     ),
142
     JS_DATATABLE => array(
143
         'no' => array(
144
             'no'  => '/js/common/jquery.dataTables.js',
145
             'min' => '/js/common/jquery.dataTables.min.js'
146
         ),
147
         'cdn' => array(
148
             'no'  => '//cdn.datatables.net/1.10.7/js/jquery.dataTables.js',
149
             'min' => '//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js'
150
         )
151
     ),
152
     JS_CHART => array(
153
         'no' => array(
154
             'no'  => '/js/common/Chart.js',
155
             'min' => '/js/common/Chart.min.js'
156
         ),
157
         'cdn' => array(
158
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js',
159
             'min' => '//cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js'
160
         )
161
     ),
162
     JS_METISMENU => array(
163
         'no' => array(
164
             'no'  => '/js/common/metisMenu.js',
165
             'min' => '/js/common/metisMenu.min.js'
166
         ),
167
         'cdn' => array(
168
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/metisMenu/2.0.2/metisMenu.js',
169
             'min' => '//cdnjs.cloudflare.com/ajax/libs/metisMenu/2.0.2/metisMenu.min.js'
170
         )
171
     ),
172
     JS_BOOTBOX => array(
173
         'no' => array(
174
             'no'  => '/js/common/bootbox.js',
175
             'min' => '/js/common/bootbox.min.js'
176
         ),
177
         'cdn' => array(
178
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.3.0/bootbox.js',
179
             'min' => '//cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.3.0/bootbox.min.js'
180
         )
181
     ),
182
     JS_DATATABLE_ODATA => array(
183
         'no' => array(
184
             'no'  => '/js/common/jquery.dataTables.odata.js',
185
             'min' => '/js/common/jquery.dataTables.odata.js',
186
         ),
187
         'cdn' => array(
188
             'no'  => '/js/common/jquery.dataTables.odata.js',
189
             'min' => '/js/common/jquery.dataTables.odata.js',
190
         )
191
     ),
192
     JS_CRYPTO_MD5_JS => array(
193
         'no' => array(
194
             'no'  => '/js/common/md5.js',
195
             'min' => '/js/common/md5.js',
196
         ),
197
         'cdn' => array(
198
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/md5.js',
199
             'min' => '//cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/md5.js',
200
         )
201
     ),
202
     JS_JCROP => array(
203
         'no' => array(
204
             'no'  => '/js/common/jquery.Jcrop.min.js',
205
             'min' => '/js/common/jquery.Jcrop.min.js'
206
         ),
207
         'cdn' => array(
208
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.12/js/jquery.Jcrop.min.js',
209
             'min' => '//cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.12/js/jquery.Jcrop.min.js'
210
         )
211
     ),
212
     JS_TYPEAHEAD => array(
213
         'no' => array(
214
             'no'  => '/js/common/typeahead.bundle.js',
215
             'min' => '/js/common/typeahead.bundle.min.js'
216
         ),
217
         'cdn' => array(
218
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.js',
219
             'min' => '//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js'
220
         )
221
     ),
222
     JS_FLIPSIDE => array(
223
         'no' => array(
224
             'no'  => '/js/common/flipside.js',
225
             'min' => '/js/common/flipside.min.js'
226
         ),
227
         'cdn' => array(
228
             'no'  => '/js/common/flipside.js',
229
             'min' => '/js/common/flipside.min.js'
230
         )
231
     ),
232
     JS_LOGIN => array(
233
         'no' => array(
234
             'no'  => '/js/common/login.js',
235
             'min' => '/js/common/login.min.js'
236
         ),
237
         'cdn' => array(
238
             'no'  => '/js/common/login.js',
239
             'min' => '/js/common/login.min.js'
240
         )
241
     )
242
);
243
244
global $cssArray;
245
$cssArray = array(
246
    CSS_JQUERY_UI => array(
247
        'no' => array(
248
             'no'  => '/css/common/jquery-ui.css',
249
             'min' => '/css/common/jquery-ui.min.css'
250
         ),
251
         'cdn' => array(
252
             'no'  => '//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css',
253
             'min' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.min.css'
254
         )
255
    ),
256
    CSS_BOOTSTRAP => array(
257
         'no' => array(
258
             'no'  => '/css/common/bootstrap.css',
259
             'min' => '/css/common/bootstrap.min.css'
260
         ),
261
         'cdn' => array(
262
             'no'  => '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css',
263
             'min' => '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'
264
         )
265
    ),
266
    CSS_BOOTSTRAP_FH => array(
267
        'no' => array(
268
             'no'  => '/css/common/bootstrap-formhelpers.css',
269
             'min' => '/css/common/bootstrap-formhelpers.min.css'
270
         ),
271
         'cdn' => array(
272
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/bootstrap-formhelpers/2.3.0/css/bootstrap-formhelpers.css',
273
             'min' => '//cdnjs.cloudflare.com/ajax/libs/bootstrap-formhelpers/2.3.0/css/bootstrap-formhelpers.min.css'
274
         )
275
    ),
276
    CSS_BOOTSTRAP_SW => array(
277
         'no' => array(
278
             'no'  => '/css/common/bootstrap-switch.css',
279
             'min' => '/css/common/bootstrap-switch.min.css'
280
         ),
281
         'cdn' => array(
282
             'no'  => '//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.css',
283
             'min' => '//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css'
284
         )
285
    ),
286
    CSS_DATATABLE => array(
287
        'no' => array(
288
             'no'  => '/css/common/jquery.dataTables.css',
289
             'min' => '/css/common/jquery.dataTables.min.css'
290
         ),
291
         'cdn' => array(
292
             'no'  => '//cdn.datatables.net/1.10.7/css/jquery.dataTables.css',
293
             'min' => '//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css'
294
         )
295
    ),
296
    CSS_JCROP => array(
297
        'no'  => array(
298
            'no'  => '/css/common/jquery.Jcrop.min.css',
299
            'min' => '/css/common/jquery.Jcrop.min.css'
300
        ),
301
        'cdn' => array(
302
            'no'  => '//cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.12/css/jquery.Jcrop.min.css',
303
            'min' => '//cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.12/css/jquery.Jcrop.min.css'
304
        ) 
305
    ),
306
    CSS_FONTAWESOME => array(
307
        'no'  => array(
308
            'no'  => '/css/common/font-awesome.min.css',
309
            'min' => '/css/common/font-awesome.min.css'
310
        ),
311
        'cdn' => array(
312
            'no'  => '//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css',
313
            'min' => '//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css'
314
        )
315
    )
316
);
317
318
/**
319
 * A framework specific webpage abstraction layer
320
 *
321
 * This class abstracts out some basic webpage creation with JQuery, Bootstrap, and other helpers
322
 */
323
class FlipPage extends WebPage
324
{
325
    /** The currently logged in user or null if no user is logged in */
326
    public $user;
327
    /** An array of links to put in the header */
328
    public $links;
329
    /** An array of notifications to draw on the page */
330
    public $notifications;
331
    /** Should we draw the header? */
332
    public $header;
333
    /** The login page URL */
334
    public $loginUrl;
335
    /** The logout page URL */
336
    public $logoutUrl;
337
    /** Should we use minified JS/CSS? */
338
    protected $minified = null;
339
    /** Should we use local JS/CSS or Content Delivery Networks? */
340
    protected $cdn = null;
341
    /** Draw the analytics scripts */
342
    protected $analytics = true;
343
344
    /**
345
     * Create a webpage with JQuery, Bootstrap, etc
346
     *
347
     * @param string $title The webpage title
348
     * @param boolean $header Draw the header bar?
349
     *
350
     * @SuppressWarnings("StaticAccess")
351
     */
352
    public function __construct($title, $header = true)
353
    {
354
        parent::__construct($title);
355
        $this->setupVars();
356
        $this->add_js(JS_JQUERY, false);
357
        $this->add_js(JS_FLIPSIDE, false);
358
        $this->addBootstrap();
359
        $this->header = $header;
360
        $this->links = array();
361
        $this->notifications = array();
362
        $this->loginUrl = 'login.php';
363
        $this->logoutUrl = 'logout.php';
364
        if(isset(FlipsideSettings::$global))
365
        {
366
            if(isset(FlipsideSettings::$global['login_url']))
367
            {
368
                $this->loginUrl = FlipsideSettings::$global['login_url'];
369
            }
370
            if(isset(FlipsideSettings::$global['logout_url']))
371
            {
372
                $this->logoutUrl = FlipsideSettings::$global['logout_url'];
373
            }
374
        }
375
        $this->user = FlipSession::getUser();
376
        $this->addAllLinks();
377
    }
378
379
    /**
380
     * Get the external site links for this page 
381
     *
382
     */
383
    protected function getSites()
384
    {
385
        if(isset(FlipsideSettings::$sites))
386
        {
387
            return FlipsideSettings::$sites;
388
        }
389
        return array();
390
    }
391
392
    /**
393
     * Add the links to be used in the header
394
     *
395
     * @SuppressWarnings("Superglobals")
396
     *
397
     * @todo Consider pulling the about menu from the settings file or a DB
398
     */
399
    protected function addAllLinks()
400
    {
401
        if($this->user === false || $this->user === null)
402
        {
403
            if(isset($_SERVER['REQUEST_URI']) && strstr($_SERVER['REQUEST_URI'], 'logout.php') === false)
404
            {
405
                $this->addLink('Login', $this->loginUrl);
406
            }
407
        }
408
        else
409
        {
410
            $this->add_links();
411
            $this->addLink('Logout', $this->logoutUrl);
412
        }
413
        $about_menu = array(
414
            'Burning Flipside'=>'https://www.burningflipside.com/about/event',
415
            'AAR, LLC'=>'https://www.burningflipside.com/organization/aar',
416
            'Privacy Policy'=>'https://www.burningflipside.com/about/privacy'
417
        );
418
        $this->addLink('About', 'http://www.burningflipside.com/about', $about_menu);
0 ignored issues
show
$about_menu is of type array<string,string,{"Bu...vacy Policy":"string"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
419
    }
420
421
    /**
422
     * Setup minified and cdn class varibles from defaults or the settings file
423
     */
424
    private function setupVars()
425
    {
426
        if($this->minified !== null && $this->cdn !== null)
427
        {
428
            return;
429
        }
430
        $this->minified = 'min';
431
        $this->cdn      = 'cdn';
432
        if(isset(FlipsideSettings::$global))
433
        {
434
            if(isset(FlipsideSettings::$global['use_minified']) && !FlipsideSettings::$global['use_minified'])
435
            {
436
                $this->minified = 'no';
437
            }
438
            if(isset(FlipsideSettings::$global['use_cdn']) && !FlipsideSettings::$global['use_cdn'])
439
            {
440
                $this->cdn = 'no';
441
            }
442
        }
443
    }
444
445
    /**
446
     * Add a JavaScript file from its src URI
447
     *
448
     * @param string $src The webpath to the JavaScript file
449
     * @param boolean $async Can the JavaScript be loaded asynchronously?
450
     *
451
     * @deprecated 2.0.0 Please use addJSByURI() instead
452
     */
453
    public function add_js_from_src($src, $async = true)
454
    {
455
        $this->addJSByURI($src, $async);
456
    }
457
458
    /**
459
     * Add a JavaScript file from its src URI
460
     *
461
     * @param string $uri The webpath to the JavaScript file
462
     * @param boolean $async Can the JavaScript be loaded asynchronously?
463
     */
464
    public function addJSByURI($uri, $async = true)
465
    {
466
        $attributes = array('src'=>$uri, 'type'=>'text/javascript');
467
        if($async === true)
468
        {
469
            $attributes['async'] = true;
470
        }
471
        $jsTag = $this->createOpenTag('script', $attributes);
472
        $closeTag = $this->createCloseTag('script');
473
        $this->addHeadTag($jsTag);
474
        $this->addHeadTag($closeTag);
475
    }
476
477
    /**
478
     * Add a Cascading Style Sheet file from its src URI
479
     *
480
     * @param string $src The webpath to the Cascading Style Sheet file
481
     * @param boolean $import Can the CSS be loaded asynchronously?
482
     *
483
     * @deprecated 2.0.0 Please use addCSSByURI() instead
484
     */
485
    public function add_css_from_src($src, $import = false)
486
    {
487
        $this->addCSSByURI($src, $import);
488
    }
489
490
    /**
491
     * Add a Cascading Style Sheet file from its src URI
492
     *
493
     * @param string $uri The webpath to the Cascading Style Sheet file
494
     * @param boolean $async Can the CSS be loaded asynchronously?
495
     */
496
    public function addCSSByURI($uri, $async = false)
497
    {
498
        $attributes = array('rel'=>'stylesheet', 'href'=>$uri, 'type'=>'text/css');
499
        if($async === true && $this->importSupport === true)
500
        {
501
            $attributes['rel'] = 'import';
502
        }
503
        $cssTag = $this->createOpenTag('link', $attributes, true);
504
        $this->addHeadTag($cssTag);
505
    }
506
507
    /**
508
     * Add a JavaScript file from a set of files known to the framework
509
     *
510
     * @param string $type the ID of the JS file
511
     * @param boolean $async Can the JS file be loaded asynchronously?
512
     *
513
     * @deprecated 2.0.0 Please use addWellKnownJS() instead
514
     */
515
    public function addJS($type, $async = true)
516
    {
517
        $this->addWellKnownJS($type, $async);
518
    }
519
520
    /**
521
     * Add a JavaScript file from a set of files known to the framework
522
     *
523
     * @param string $type the ID of the JS file
524
     * @param boolean $async Can the JS file be loaded asynchronously?
525
     *
526
     * @deprecated 2.0.0 Please use addWellKnownJS() instead
527
     */
528
    public function add_js($type, $async = true)
529
    {
530
        $this->addWellKnownJS($type, $async);
531
    }
532
533
    /**
534
     * Add a JavaScript file from a set of files known to the framework
535
     *
536
     * @param string $jsFileID the ID of the JS file
537
     * @param boolean $async Can the JS file be loaded asynchronously?
538
     */
539
    public function addWellKnownJS($jsFileID, $async = true)
540
    {
541
        global $jsArray;
542
        $this->setupVars();
543
        $src = $jsArray[$jsFileID][$this->cdn][$this->minified];
544
        $this->addJSByURI($src, $async);
545
    }
546
547
    /**
548
     * Add a CSS file from a set of files known to the framework
549
     *
550
     * @param string $type the ID of the CSS file
551
     * @param boolean $import Can the CSS file be loaded asynchronously?
552
     *
553
     * @deprecated 2.0.0 Please use addWellKnownCSS() instead
554
     */
555
    public function add_css($type, $import = false)
556
    {
557
        $this->addWellKnownCSS($type, $import);
558
    }
559
560
    /**
561
     * Add a CSS file from a set of files known to the framework
562
     *
563
     * @param string $cssFileID the ID of the CSS file
564
     * @param boolean $async Can the CSS file be loaded asynchronously?
565
     */
566
    public function addWellKnownCSS($cssFileID, $async = true)
567
    {
568
        global $cssArray;
569
        $this->setupVars();
570
        $src = $cssArray[$cssFileID][$this->cdn][$this->minified];
571
        $this->addCSSByURI($src, $async);
572
    }
573
574
    /**
575
     * Add files needed by the Bootstrap framework
576
     */
577
    private function addBootstrap()
578
    {
579
        $this->add_js(JS_BOOTSTRAP, false);
580
        $this->addWellKnownCSS(CSS_BOOTSTRAP);
581
        $this->addWellKnownCSS(CSS_FONTAWESOME);
582
    }
583
584
    protected function getSiteLinksForHeader()
585
    {
586
        $sites = $this->getSites();
587
        $names = array_keys($sites);
588
        $ret = '';
589
        foreach($names as $name)
590
        {
591
            $ret .= '<li>'.$this->createLink($name, $sites[$name]).'</li>';
592
        }
593
        return $ret;
594
    }
595
596
    /**
597
     * Get the link for the HREF
598
     *
599
     * @return string The HREF for the dropdown
600
     */
601
    protected function getHrefForDropdown(&$link)
602
    {
603
        if(isset($link['_']))
604
        {
605
            $ret = $link['_'];
606
            unset($link['_']);
607
            return $ret;
608
        }
609
        return '#';
610
    }
611
612
    protected function getDropdown($link, $name)
613
    {
614
        $ret = '<li class="dropdown">';
615
        $href = $this->getHrefForDropdown($link);
616
        $ret .= '<a href="'.$href.'" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">'.$name.' <span class="caret"></span></a>';
617
        $ret .= '<ul class="dropdown-menu">';
618
        $subNames = array_keys($link);
619
        foreach($subNames as $subName)
620
        {
621
            $ret .= $this->getLinkByName($subName, $link);
622
        }
623
        $ret .= '</ul></li>';
624
        return $ret;
625
    }
626
627
    protected function getLinkByName($name, $links)
628
    {
629
        if(is_array($links[$name]))
630
        {
631
            return $this->getDropdown($links[$name], $name);
632
        }
633
        if($links[$name] === false)
634
        {
635
            return '<li>'.$name.'</li>';
636
        }
637
        return '<li>'.$this->createLink($name, $links[$name]).'</li>';
638
    }
639
640
    protected function getLinksMenus()
641
    {
642
        $names = array_keys($this->links);
643
        $ret = '';
644
        foreach($names as $name)
645
        {
646
            $ret .= $this->getLinkByName($name, $this->links);
647
        }
648
        return $ret;
649
    }
650
651
    /**
652
     * Draw the header for the page
653
     */
654
    protected function addHeader()
655
    {
656
        $sites = $this->getSiteLinksForHeader();
657
        $links = $this->getLinksMenus();
658
        $header = '<nav class="navbar navbar-default navbar-fixed-top">
659
                      <div class="container-fluid">
660
                          <!-- Brand and toggle get grouped for better mobile display -->
661
                          <div class="navbar-header">
662
                          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
663
                              <span class="sr-only">Toggle navigation</span>
664
                              <span class="icon-bar"></span>
665
                              <span class="icon-bar"></span>
666
                              <span class="icon-bar"></span>
667
                          </button>
668
                          <a class="navbar-brand" href="#">
669
                              <picture>
670
                                  <source srcset="/img/common/logo.svg" style="width: 30px; height:30px"/>
671
                                  <img alt="Burning Flipside" src="/img/common/logo.png" style="width: 30px; height:30px"/>
672
                              </picture>
673
                          </a>
674
                          </div>
675
                          <!-- Collect the nav links, forms, and other content for toggling -->
676
                          <div class="collapse navbar-collapse" id="navbar-collapse">
677
                              <ul id="site_nav" class="nav navbar-nav">
678
                              '.$sites.'
679
                              </ul>
680
                              <ul class="nav navbar-nav navbar-right">
681
                              '.$links.'
682
                              </ul>
683
                          </div>
684
                      </div>
685
                  </nav>';
686
        $this->body = $header.$this->body;
687
        $this->body_tags .= 'style="padding-top: 60px;"';
688
    }
689
690
    /** Notification that is green for success */
691
    const NOTIFICATION_SUCCESS = 'alert-success';
692
    /** Notification that is blue for infomrational messages */
693
    const NOTIFICATION_INFO    = 'alert-info';
694
    /** Notification that is yellow for warning */
695
    const NOTIFICATION_WARNING = 'alert-warning';
696
    /** Notification that is red for error */
697
    const NOTIFICATION_FAILED  = 'alert-danger';
698
699
    /**
700
     * Add a notification to the page
701
     *
702
     * @param string $msg The message to show in the notifcation
703
     * @param string $sev The severity of the notifcation
704
     * @param boolean $dismissible Can the user dismiss the notificaton?
705
     *
706
     * @deprecated 2.0.0 Use the addNotification function instead 
707
     */
708
    public function add_notification($msg, $sev = self::NOTIFICATION_INFO, $dismissible = true)
709
    {
710
        $notice = array('msg'=>$msg, 'sev'=>$sev, 'dismissible'=>$dismissible);
711
        array_push($this->notifications, $notice);
712
    }
713
714
    /**
715
     * Add a notification to the page
716
     *
717
     * @param string $message The message to show in the notifcation
718
     * @param string $severity The severity of the notifcation
719
     * @param boolean $dismissible Can the user dismiss the notificaton?
720
     */
721
    public function addNotification($message, $severity = self::NOTIFICATION_INFO, $dismissible = true)
722
    {
723
        array_push($this->notifications, array('msg'=>$message, 'sev'=>$severity, 'dismissible'=>$dismissible)); 
724
    }
725
726
    /**
727
     * Draw all notifications to the page
728
     */
729
    private function renderNotifications()
730
    {
731
        $count = count($this->notifications);
732
        if($count === 0)
733
        {
734
            return;
735
        }
736
        for($i = 0; $i < $count; $i++)
737
        {
738
            $class = 'alert '.$this->notifications[$i]['sev'];
739
            $button = '';
740
            if($this->notifications[$i]['dismissible'])
741
            {
742
                $class .= ' alert-dismissible';
743
                $button = '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>';
744
            }
745
            $prefix = '';
746
            switch($this->notifications[$i]['sev'])
747
            {
748
                case self::NOTIFICATION_INFO:
749
                    $prefix = '<strong>Notice:</strong> '; 
750
                    break;
751
                case self::NOTIFICATION_WARNING:
752
                    $prefix = '<strong>Warning!</strong> ';
753
                    break;
754
                case self::NOTIFICATION_FAILED:
755
                    $prefix = '<strong>Warning!</strong> ';
756
                    break;
757
            }
758
            $style = '';
759
            if(($i + 1) < count($this->notifications))
760
            {
761
                //Not the last notification, remove the end margin
762
                $style = 'style="margin: 0px;"';
763
            }
764
            $this->body = '
765
                <div class="'.$class.'" role="alert" '.$style.'>
766
                    '.$button.$prefix.$this->notifications[$i]['msg'].'
767
                </div>
768
            '.$this->body;
769
        }
770
    }
771
772
    /**
773
     * Add the no script block
774
     */
775
    private function addNoScript()
776
    {
777
        $this->body = '<noscript>
778
                <div class="alert alert-danger alert-dismissible" role="alert">
779
                    <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
780
                    <strong>Error!</strong> This site makes extensive use of JavaScript. Please enable JavaScript or this site will not function.
781
                </div>
782
            </noscript>'.$this->body;
783
    }
784
785
    /**
786
     * Add the analytics script block
787
     */
788
    private function addAnalyticsBlock()
789
    {
790
        if($this->analytics === false)
791
        {
792
            return;
793
        }
794
        $this->body = $this->body.'<script>
795
  (function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
796
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
797
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
798
  })(window,document,\'script\',\'//www.google-analytics.com/analytics.js\',\'ga\');
799
800
  ga(\'create\', \'UA-64901342-1\', \'auto\');
801
  ga(\'send\', \'pageview\');
802
803
</script>';
804
    }
805
806
    /**
807
     * Draw the page
808
     *
809
     * @param boolean $header Draw the header
810
     */
811
    public function printPage($header = true)
812
    {
813
        $this->renderNotifications();
814
        $this->addNoScript();
815
        $this->addAnalyticsBlock();
816
        if($this->header || $header)
817
        {
818
            $this->addHeader();
819
        }
820
        parent::printPage();
821
    }
822
823
    /**
824
     * Add a link to the header
825
     *
826
     * @param string $name The name of the link
827
     * @param boolean|string $url The URL to link to
828
     * @param boolean|array $submenu Any submenu items for the dropdown
829
     *
830
     * @deprecated 1.0.0 Use addLink instead
831
     */
832
    public function add_link($name, $url = false, $submenu = false)
833
    {
834
        $this->addLink($name, $url, $submenu);
0 ignored issues
show
It seems like $url defined by parameter $url on line 832 can also be of type boolean; however, FlipPage::addLink() does only seem to accept false|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
It seems like $submenu defined by parameter $submenu on line 832 can also be of type array; however, FlipPage::addLink() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
835
    }
836
837
    /**
838
     * Add a link to the header
839
     *
840
     * @param string $name The name of the link
841
     * @param false|string $url The URL to link to
842
     * @param false|array $subment Any submenu items for the dropdown
0 ignored issues
show
There is no parameter named $subment. 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...
843
     */
844
    public function addLink($name, $url = false, $submenu = false)
845
    {
846
        if(is_array($submenu))
847
        {
848
            $submenu['_'] = $url;
849
            $this->links[$name] = $submenu;
850
        }
851
        else
852
        {
853
            $this->links[$name] = $url;
854
        }
855
    }
856
857
    /**
858
     * Add the login form to the page
859
     *
860
     * @SuppressWarnings("StaticAccess")
861
     */
862
    public function add_login_form()
863
    {
864
        $auth = \AuthProvider::getInstance();
865
        $authLinks = $auth->getSupplementaryLinks();
866
        $authLinksStr = '';
867
        $count = count($authLinks);
868
        for($i = 0; $i < $count; $i++)
869
        {
870
            $authLinksStr .= $authLinks[$i];
871
        }
872
        if($count > 0)
873
        {
874
            $authLinksStr = 'Sign in with '.$authLinksStr;
875
        }
876
        $this->body .= '<div class="modal fade" role="dialog" id="login-dialog" title="Login" aria-hidden="true">
877
                            <div class="modal-dialog">
878
                                <div class="modal-content">
879
                                    <div class="modal-header">
880
                                        <button type="button" class="close" data-dismiss="modal">
881
                                            <span aria-hidden="true">&times;</span>
882
                                            <span class="sr-only">Close</span>
883
                                        </button>
884
                                        <h4 class="modal-title">Login</h4>
885
                                    </div>
886
                                    <div class="modal-body">
887
                                        <form id="login_dialog_form" role="form">
888
                                            <input class="form-control" type="text" name="username" placeholder="Username or Email" required autofocus/>
889
                                            <input class="form-control" type="password" name="password" placeholder="Password" required/>
890
                                            <input type="hidden" name="return" value="'.$this->currentUrl().'"/>
891
                                            <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
892
                                        </form>
893
                                        '.$authLinksStr.'
894
                                    </div>
895
                                </div>
896
                            </div>
897
                        </div>';
898
    }
899
900
    /**
901
     * Add additional links
902
     */
903
    public function add_links()
904
    {
905
    }
906
}
907
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
908