1
|
|
|
<?php |
|
|
|
|
2
|
|
|
require_once('class.FlipPage.php'); |
3
|
|
|
require_once('class.FlipSession.php'); |
4
|
|
|
require_once('class.SecurePlugin.php'); |
5
|
|
|
|
6
|
|
|
trait SecureWebPage |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected function getSecureRoot() |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
$root = $_SERVER['DOCUMENT_ROOT']; |
11
|
|
|
$script_dir = dirname(__FILE__); |
12
|
|
|
$ret = substr($script_dir, strlen($root)); |
13
|
|
|
|
14
|
|
|
if($ret === false || strlen($ret) === 0) |
15
|
|
|
{ |
16
|
|
|
return '/'; |
17
|
|
|
} |
18
|
|
|
else if($ret[strlen($ret)-1] !== '/') |
19
|
|
|
{ |
20
|
|
|
$ret .= '/'; |
21
|
|
|
} |
22
|
|
|
return $ret; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function addSecureCss() |
26
|
|
|
{ |
27
|
|
|
$this->addCSSByURI($this->secure_root.'css/secure.css'); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function addSecureScript() |
31
|
|
|
{ |
32
|
|
|
$this->addWellKnownJS(JS_LOGIN); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function loadAndGetPlugins() |
36
|
|
|
{ |
37
|
|
|
$script_dir = dirname(__FILE__); |
38
|
|
|
$plugin_files = glob($script_dir.'/*/plugin.php'); |
39
|
|
|
$count = count($plugin_files); |
40
|
|
|
for($i = 0; $i < $count; $i++) |
41
|
|
|
{ |
42
|
|
|
include($plugin_files[$i]); |
43
|
|
|
} |
44
|
|
|
$ret = array(); |
45
|
|
|
foreach(get_declared_classes() as $class) |
46
|
|
|
{ |
47
|
|
|
if(is_subclass_of($class, 'SecurePlugin')) |
48
|
|
|
{ |
49
|
|
|
$ret[] = new $class(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return $ret; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function addPluginLinks($count, $plugins) |
56
|
|
|
{ |
57
|
|
|
$secure_menu = array(); |
58
|
|
|
for($i = 0; $i < $count; $i++) |
59
|
|
|
{ |
60
|
|
|
$ret = $plugins[$i]->get_secure_menu_entries($this, $this->user); |
|
|
|
|
61
|
|
|
if($ret !== false) |
62
|
|
|
{ |
63
|
|
|
$ret["<hr id='hr_$i'/>"] = false; |
64
|
|
|
$secure_menu = array_merge($secure_menu, $ret); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
array_pop($secure_menu); |
68
|
|
|
$this->addLink('Secure', $this->secureUrl, $secure_menu); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
class SecurePage extends FlipPage |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
use SecureWebPage; |
75
|
|
|
|
76
|
|
|
public $secure_root; |
77
|
|
|
protected $plugins; |
78
|
|
|
protected $plugin_count; |
79
|
|
|
|
80
|
|
View Code Duplication |
function __construct($title) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
parent::__construct($title, true); |
83
|
|
|
$this->secure_root = $this->getSecureRoot(); |
84
|
|
|
$this->addSecureCss(); |
85
|
|
|
$this->addSecureScript(); |
86
|
|
|
$this->add_login_form(); |
87
|
|
|
$this->body_tags='data-login-url="'.$this->secure_root.'api/v1/login"'; |
88
|
|
|
$this->plugins = $this->loadAndGetPlugins(); |
89
|
|
|
$this->plugin_count = count($this->plugins); |
90
|
|
|
$this->add_links(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function add_links() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
if($this->user !== false) |
96
|
|
|
{ |
97
|
|
|
$this->addPluginLinks($this->plugin_count, $this->plugins); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
function get_secure_child_entry_points() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$entry_points = ''; |
104
|
|
|
for($i = 0; $i < $this->plugin_count; $i++) |
105
|
|
|
{ |
106
|
|
|
$ret = $this->plugins[$i]->get_plugin_entry_point(); |
107
|
|
|
if($ret !== false) |
108
|
|
|
{ |
109
|
|
|
$entry_points .= '<li>'.$this->createLink($ret['name'],$ret['link']).'</li>'; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return $entry_points; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
?> |
|
|
|
|
116
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.