1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Module |
4
|
|
|
* |
5
|
|
|
* @package Intraface |
6
|
|
|
* @author Lars Olesen <[email protected]> |
7
|
|
|
* @author Sune Jensen <[email protected]> |
8
|
|
|
* @since 0.1.0 |
9
|
|
|
* @version @package-version@ |
10
|
|
|
*/ |
11
|
|
|
class Intraface_ModuleHandler |
12
|
|
|
{ |
13
|
|
|
private $user; |
14
|
|
|
private $internet; |
|
|
|
|
15
|
|
|
private $modules = array(); |
16
|
|
|
private $primary_module_name; |
17
|
|
|
private $primary_module_object; |
18
|
|
|
private $shared = array(); |
19
|
|
|
|
20
|
94 |
|
public function __construct($intranet = '', $user = '') |
21
|
|
|
{ |
22
|
94 |
|
$this->user = $user; |
23
|
94 |
|
$this->intranet = $intranet; |
|
|
|
|
24
|
94 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns the primary module |
28
|
|
|
* |
29
|
|
|
* Used for instance in Page to give the correct submenu. |
30
|
|
|
* |
31
|
|
|
* @return module object or false |
32
|
|
|
*/ |
33
|
1 |
|
function getPrimaryModule() |
34
|
|
|
{ |
35
|
1 |
|
if (!empty($this->modules[$this->primary_module_name]) and is_object($this->modules[$this->primary_module_name])) { |
|
|
|
|
36
|
1 |
|
return($this->modules[$this->primary_module_name]); |
37
|
|
|
} else { |
38
|
1 |
|
return false; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
28 |
|
function setPrimaryModule($module_name) |
43
|
|
|
{ |
44
|
28 |
|
if (!empty($this->primary_module_object) and is_object($this->primary_module_object)) { |
|
|
|
|
45
|
|
|
throw new Exception('Primary module has been set'); |
46
|
|
|
} else { |
47
|
28 |
|
$module = $this->useModule($module_name); |
48
|
|
|
|
49
|
27 |
|
if (is_object($module)) { |
50
|
27 |
|
$this->primary_module_name = $module_name; |
51
|
|
|
|
52
|
|
|
// Finds dependent modules |
53
|
|
|
// @todo Maybe this should be moved to useModule() |
54
|
27 |
|
$dependent_modules = $module->getDependentModules(); |
55
|
|
|
|
56
|
27 |
|
foreach ($dependent_modules as $dependent) { |
57
|
|
|
$no_use = $this->useModule($dependent); |
|
|
|
|
58
|
27 |
|
} |
59
|
|
|
|
60
|
27 |
|
return $module; |
61
|
|
|
} else { |
62
|
|
|
// @todo Den fejlmeddelse er egentlig irrelevant, da useModul ikke enten returnere et objekt eller throw new Exception. |
63
|
|
|
throw new Exception('Du har ikke adgang til modulet'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* useModule() |
70
|
|
|
* |
71
|
|
|
* @todo Need to handle access |
72
|
|
|
* |
73
|
|
|
* @param string $module_name |
74
|
|
|
* @param boolean $ignore_user_access |
75
|
|
|
* |
76
|
|
|
* @return object $module |
77
|
|
|
*/ |
78
|
90 |
|
public function useModule($module_name, $ignore_user_access = false) |
79
|
|
|
{ |
80
|
90 |
|
if (!self::isValidModuleName($module_name)) { |
81
|
1 |
|
throw new Exception($module_name . ' is not a valid module name'); |
82
|
|
|
} |
83
|
|
|
|
84
|
89 |
|
if (!empty($this->modules[$module_name]) and is_object($this->modules[$module_name])) { |
|
|
|
|
85
|
25 |
|
return $this->modules[$module_name]; |
86
|
|
|
} |
87
|
|
|
|
88
|
89 |
|
$this->modules[$module_name] = $module_name; |
89
|
|
|
|
90
|
|
|
// @todo check whether a module has any limitations in access here? - we have not asked for shared |
91
|
|
|
|
92
|
|
|
// access control here |
93
|
89 |
|
$access = false; |
94
|
|
|
|
95
|
89 |
|
if (!is_object($this->user)) { |
96
|
9 |
|
if (!is_object($this->intranet)) { |
97
|
2 |
|
throw new Exception('Cannot use a module when no intranet is available'); |
98
|
|
|
} |
99
|
|
|
// Det er et weblogin. |
100
|
7 |
|
if ($this->intranet->hasModuleAccess($module_name)) { |
101
|
6 |
|
$access = true; |
102
|
6 |
|
} |
103
|
87 |
|
} elseif ($ignore_user_access) { |
104
|
|
|
if (!is_object($this->intranet)) { |
105
|
|
|
throw new Exception('Cannot use a module when no intranet is available'); |
106
|
|
|
} |
107
|
|
|
// Skal kun kontrollere om intranettet har adgang, for at benytte modullet |
108
|
|
|
if ($this->intranet->hasModuleAccess($module_name)) { |
109
|
|
|
$access = true; |
110
|
|
|
} |
111
|
|
|
} else { |
112
|
|
|
// Almindelig login |
113
|
80 |
|
if ($this->user->hasModuleAccess($module_name)) { |
114
|
79 |
|
$access = true; |
115
|
79 |
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
87 |
|
if ($access !== true) { |
119
|
2 |
|
throw new Exception('You need access to a required module to see this page, maybe it is ' . $module_name); |
120
|
|
|
} |
121
|
|
|
|
122
|
85 |
|
$main_class_name = "Main" . ucfirst($module_name); |
123
|
85 |
|
$main_class_path = PATH_INCLUDE_MODULE . $module_name . "/" . $main_class_name . ".php"; |
124
|
|
|
|
125
|
85 |
|
if (file_exists($main_class_path)) { |
126
|
85 |
|
require_once($main_class_path); |
127
|
85 |
|
$object = new $main_class_name; |
128
|
85 |
|
$object->load($this); |
129
|
85 |
|
$this->modules[$module_name] = $object; |
130
|
85 |
|
return $object; |
131
|
|
|
} else { |
132
|
|
|
// @TODO this should not fail as hard - but what should happen then? |
133
|
|
|
throw new Exception('ModuleHandler: ' . $main_class_path . ' does not exist'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gives access to a shared module |
139
|
|
|
* |
140
|
|
|
* @param string $shared_name Name on module to load |
141
|
|
|
* |
142
|
|
|
* @return object or false |
143
|
|
|
*/ |
144
|
63 |
|
public function useShared($shared_name) |
145
|
|
|
{ |
146
|
63 |
|
if (!self::isValidModuleName($shared_name)) { |
147
|
|
|
throw new Exception($shared_name. ' is not a valid shared module name'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Tjekker om shared allerede er loaded |
151
|
63 |
|
if (!empty($this->shared[$shared_name]) and is_object($this->shared[$shared_name])) { |
|
|
|
|
152
|
36 |
|
return $this->shared[$shared_name]; |
153
|
|
|
} |
154
|
|
|
|
155
|
63 |
|
$main_shared_name = 'Shared' . ucfirst($shared_name); |
156
|
63 |
|
$main_shared_path = PATH_INCLUDE_SHARED . $shared_name . '/' . $main_shared_name . '.php'; |
157
|
|
|
|
158
|
63 |
|
if (file_exists($main_shared_path)) { |
159
|
63 |
|
require_once $main_shared_path; |
160
|
63 |
|
$object = new $main_shared_name; |
161
|
63 |
|
$object->load(); |
162
|
63 |
|
$this->shared[$shared_name] = $object; |
163
|
63 |
|
return $object; |
164
|
|
|
} else { |
165
|
|
|
throw new Exception($shared_name . ' cannot be found on ' . $main_shared_path . ' with PATH_INCLUDE_SHARED: ' . PATH_INCLUDE_SHARED); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* getModule() |
171
|
|
|
* |
172
|
|
|
* @param string $module_name |
|
|
|
|
173
|
|
|
* |
174
|
|
|
* @return object |
175
|
|
|
*/ |
176
|
2 |
|
function getModule($name) |
177
|
|
|
{ |
178
|
2 |
|
if (is_object($this->modules[$name])) { |
179
|
2 |
|
return($this->modules[$name]); |
180
|
|
|
} else { |
181
|
|
|
throw new Exception('getModule() module ' . $name . ' not loaded'); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* getModules() |
187
|
|
|
* |
188
|
|
|
* @param string $order_by |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
2 |
|
public static function getModules($db, $order_by = 'frontpage_index') |
193
|
|
|
{ |
194
|
2 |
|
$modules = array(); |
195
|
|
|
|
196
|
2 |
|
if ($order_by != '') { |
197
|
2 |
|
$order_by = "ORDER BY ".$db->quoteIdentifier($order_by, 'text'); |
198
|
2 |
|
} |
199
|
|
|
|
200
|
2 |
|
$i = 0; |
201
|
2 |
|
$result = $db->query("SELECT id, menu_label, name, show_menu FROM module WHERE active = 1 ".$order_by); |
202
|
2 |
|
if (PEAR::isError($result)) { |
203
|
|
|
throw new Exception($result->getUserInfo()); |
204
|
|
|
} |
205
|
2 |
|
while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { |
206
|
2 |
|
$modules[$i]['id'] = $row['id']; |
207
|
2 |
|
$modules[$i]['name'] = $row['name']; |
208
|
2 |
|
$modules[$i]['menu_label'] = $row['menu_label']; |
209
|
2 |
|
$modules[$i]['show_menu'] = $row['show_menu']; |
210
|
|
|
|
211
|
2 |
|
$j = 0; |
212
|
2 |
|
$result_sub = $db->query("SELECT id, description FROM module_sub_access WHERE active = 1 AND module_id = ".$db->quote($row["id"], 'integer')." ORDER BY description"); |
213
|
2 |
|
if (PEAR::isError($result_sub)) { |
214
|
|
|
throw new Exception($result_sub->getUserInfo()); |
215
|
|
|
} |
216
|
|
|
|
217
|
2 |
|
while ($row_sub = $result_sub->fetchRow(MDB2_FETCHMODE_ASSOC)) { |
218
|
2 |
|
$modules[$i]['sub_access'][$j]['id'] = $row_sub['id']; |
219
|
2 |
|
$modules[$i]['sub_access'][$j]['description'] = $row_sub['description']; |
220
|
2 |
|
$j++; |
221
|
2 |
|
} |
222
|
|
|
|
223
|
2 |
|
$i++; |
224
|
2 |
|
} |
225
|
2 |
|
return $modules; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Function to check whether the module has been registered |
230
|
|
|
* Made temporarily for /main/index.php |
231
|
|
|
*/ |
232
|
|
|
public static function exists($db, $module_id) |
233
|
|
|
{ |
234
|
|
|
if (is_numeric($module_id)) { |
235
|
|
|
throw new Exception("Not yet implemented!"); |
236
|
|
|
} else { |
237
|
|
|
$result = $db->query('SELECT id FROM module WHERE name = '.$db->quote($module_id, 'text')); |
238
|
|
|
if (PEAR::isError($result)) { |
239
|
|
|
throw new Exception('Error in query: '.$result->getUserInfo()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ($result->numRows() > 0) { |
243
|
|
|
return true; |
244
|
|
|
} else { |
245
|
|
|
return false; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
return false; |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
92 |
|
private static function isValidModuleName($name) |
252
|
|
|
{ |
253
|
92 |
|
return preg_match("/^[a-z0-9]+$/", $name); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.