forked from sentora/sentora-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.class.php
326 lines (297 loc) · 12.5 KB
/
module.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* @copyright 2014-2015 Sentora Project (http://www.sentora.org/)
* Sentora is a GPL fork of the ZPanel Project whose original header follows:
*
* Main module interface class.
* @package zpanelx
* @subpackage dryden -> ui
* @version 1.0.0
* @author Bobby Allen (ballen@bobbyallen.me)
* @copyright ZPanel Project (http://www.zpanelcp.com/)
* @link http://www.zpanelcp.com/
* @license GPL (http://www.gnu.org/licenses/gpl.html)
*/
class ui_module {
function __construct() {
}
/**
* Checks that the module exists.
* @author Bobby Allen (ballen@bobbyallen.me)
* @param string $name Name of the module to check that exists.
* @return boolean
*/
static function CheckModuleExists($name) {
$user = ctrl_users::GetUserDetail();
if (file_exists("modules/" . $name . "/module.zpm"))
if (ctrl_groups::CheckGroupModulePermissions($user['usergroupid'], self::GetModuleID()))
return true;
return false;
}
/**
* Returns the module template code.
* @author Bobby Allen (ballen@bobbyallen.me)
* @param string $name Name of the module.
* @return string
*/
static function GetModuleContent($name) {
if (self::CheckModuleExists($name)) {
return fs_filehandler::ReadFileContents("modules/" . $name . "/module.zpm");
}
}
/**
* Handles the GetModule control, if unable to load the module will handle the error too!
* @author Bobby Allen (ballen@bobbyallen.me)
* @param string $module The name of the module to load.
* @return string
*/
static function GetModule($module) {
if (self::CheckModuleExists($module)) {
$retval = self::GetModuleContent($module);
} else {
runtime_hook::Execute('OnFailedModuleLoad');
$retval = "Unable to find requested module!";
}
return $retval;
}
/**
* Gathers module infomation from the modules XML file and adds the details to the DB.
* @author Bobby Allen (ballen@bobbyallen.me)
* @param string $module The name of the module (folder) of which to import the module infomation in for.
* @return boolean
*/
static function ModuleInfoToDB($module) {
global $zdbh;
global $zlo;
runtime_hook::Execute('OnBeforeModuleInfoToDB');
$mod_xml = "modules/$module/module.xml";
try {
$mod_config = new xml_reader(fs_filehandler::ReadFileContents($mod_xml));
$mod_config->Parse();
$module_name = $mod_config->document->name[0]->tagData;
$module_version = $mod_config->document->version[0]->tagData;
$module_description = $mod_config->document->desc[0]->tagData;
$module_defaultcat = $mod_config->document->defaultcat[0]->tagData;
$module_type = $mod_config->document->type[0]->tagData;
if ($module_type != ("user" || "system" || "modadmin")) {
$module_type = "user";
}
$sql = $zdbh->prepare("SELECT mc_id_pk FROM x_modcats WHERE mc_name_vc = :module_defaultcat");
$sql->bindParam(':module_defaultcat', $module_defaultcat);
$status = $sql->execute();
$result = $sql->fetch();
if ($result) {
$cat_fk = $result['mc_id_pk'];
} else {
$cat_fk = 2;
}
$sql = $zdbh->prepare("INSERT INTO x_modules (mo_name_vc, mo_category_fk, mo_version_in, mo_folder_vc, mo_installed_ts, mo_type_en, mo_desc_tx) VALUES (:module_name, :cat_fk, :module_version, :module, " . time() . ", :module_type, :module_description)");
$sql->bindParam(':module_name', $module_name);
$sql->bindParam(':cat_fk', $cat_fk);
$sql->bindParam(':module_version', $module_version);
$sql->bindParam(':module', $module);
$sql->bindParam(':module_type', $module_type);
$sql->bindParam(':module_description', $module_description);
$sql->execute();
return true;
} catch (Exception $e) {
return false;
}
runtime_hook::Execute('OnAfterModuleInfoToDB');
}
/**
* This class scans the module directory and will return an array of new modules that are not yet in the database.
* @author Bobby Allen (ballen@bobbyallen.me)
* @global db_driver $zdbh The ZPX database handle.
* @param boolean $init Upon finding modules that don't exist in the database, add them!
* @return array List of all new modules.
*/
static function ScanForNewModules($init = false) {
global $zdbh;
$new_module_list = array();
$rootdir = ctrl_options::GetSystemOption('sentora_root') . 'modules';
$handle = @opendir($rootdir);
$chkdir = $rootdir . '/';
if ($handle) {
while ($file = readdir($handle)) {
if ($file != "." && $file != "..") {
if (is_dir($chkdir . $file)) {
$sql = $zdbh->prepare("SELECT mo_id_pk FROM x_modules WHERE mo_folder_vc = :file");
$sql->bindParam(':file', $file);
$status = $sql->execute();
$match_module = $sql->fetch();
if (!$match_module) {
array_push($new_module_list, $file);
}
}
}
}
closedir($handle);
}
if ($init == true) {
foreach ($new_module_list as $modules_to_import) {
ui_module::ModuleInfoToDB($modules_to_import);
}
}
return $new_module_list;
}
/**
* Checks to see if the specified module is enabled.
* @author Bobby Allen (ballen@bobbyallen.me)
* @global db_driver $zdbh The ZPX database handle.
* @param string $modulename The name of the module of which to check.
* @return boolean
*/
static function CheckModuleEnabled($modulename) {
global $zdbh;
$numrows = $zdbh->prepare("SELECT mo_name_vc, mo_enabled_en FROM x_modules WHERE mo_name_vc = :module");
$numrows->bindParam(':module', $modulename);
$numrows->execute();
$retval = $numrows->fetch();
if ($retval['mo_enabled_en'] == "true") {
$retvalEnd = true;
} else {
$retvalEnd = false;
}
return $retvalEnd;
}
/**
* Returns the name of the current module.
* @author Bobby Allen (ballen@bobbyallen.me)
* @global obj $controller The controller object.
* @global db_driver $zdbh The ZPX database handle.
* @return string The name of the currently loaded (active) module.
*/
public static function GetModuleName() {
global $controller;
global $zdbh;
$bindArray = array(
':module' => $controller->GetControllerRequest('URL', 'module'),
);
$retval = $zdbh->bindQuery("SELECT mo_name_vc FROM x_modules WHERE mo_folder_vc = :module", $bindArray);
$moduleInfo = $zdbh->returnRow();
return $moduleInfo['mo_name_vc'];
}
/**
* This class returns the database ID of the currently loaded (active) module.
* @author Bobby Allen (ballen@bobbyallen.me)
* @global obj $controller The controller object.
* @global db_driver $zdbh The ZPX database handle.
* @return int The module ID of the currently loaded (active) module.
*/
static function GetModuleID() {
global $controller;
global $zdbh;
$bindArray = array(
':module' => $controller->GetControllerRequest('URL', 'module'),
);
$retval = $zdbh->bindQuery("SELECT mo_id_pk FROM x_modules WHERE mo_folder_vc = :module", $bindArray);
$moduleInfo = $zdbh->returnRow();
return $moduleInfo['mo_id_pk'];
}
/**
* This class returns the folder name of the current module.
* @author Bobby Allen (ballen@bobbyallen.me)
* @global obj $controller The controller object.
* @global db_driver $zdbh The ZPX database handle.
* @return string The modules folder name as it appears in panel/modules/.
*/
static function GetModuleFolderName() {
global $controller;
global $zdbh;
$bindArray = array(
':module' => $controller->GetControllerRequest('URL', 'module'),
);
$retval = $zdbh->bindQuery("SELECT mo_folder_vc FROM x_modules WHERE mo_folder_vc = :module", $bindArray);
$moduleInfo = $zdbh->returnRow();
return $moduleInfo['mo_folder_vc'];
}
/**
* This class returns the description of the current module.
* @author Bobby Allen (ballen@bobbyallen.me)
* @global obj $controller The controller object.
* @global db_driver $zdbh The ZPX database handle.
* @return string The module description from the database (originally improted from the module.xml file).
*/
static function GetModuleDescription() {
global $controller;
global $zdbh;
$bindArray = array(
':module' => $controller->GetControllerRequest('URL', 'module'),
);
$retval = $zdbh->bindQuery("SELECT mo_desc_tx FROM x_modules WHERE mo_folder_vc = :module", $bindArray);
$moduleInfo = $zdbh->returnRow();
return $moduleInfo['mo_desc_tx'];
}
/**
* Returns the current module's Name and Category Name.
* @since 10.1.0
* @author Jason Davis (jason.davis.fl@gmail.com)
* @global obj $controller The controller object.
* @global db_driver $zdbh The ZPX database handle.
* @return array Module name and category name.
*/
public static function GetModuleCategoryName() {
global $controller;
global $zdbh;
$bindArray = array(
':module' => $controller->GetControllerRequest('URL', 'module'),
);
$sql = 'SELECT x_modules.mo_category_fk , x_modules.mo_name_vc , x_modcats.mc_name_vc
FROM x_modcats
LEFT JOIN x_modules
ON ( x_modcats.mc_id_pk = x_modules.mo_category_fk )
WHERE x_modules.mo_folder_vc = :module';
$retval = $zdbh->bindQuery($sql , $bindArray);
$moduleRow = $zdbh->returnRow();
return $moduleRow;
}
/**
* Checks to see if the specified module has updates.
* @author Bobby Allen (ballen@bobbyallen.me)
* @global db_driver $zdbh The ZPX database handle.
* @param string $modulefolder The module folder of which to check updates for.
* @return mixed If updates are avaliable will return an array with the new version and download URL otherwise will return 'false'.
*/
static function GetModuleHasUpdates($modulefolder) {
global $zdbh;
$bindArray = array(
':module' => $modulefolder,
);
$retvalQuery = $zdbh->bindQuery("SELECT mo_updateurl_tx, mo_updatever_vc FROM x_modules WHERE mo_folder_vc = :module", $bindArray);
$retval = $zdbh->returnRow();
if ($retval['mo_updatever_vc'] <> "") {
$retval = array($retval['mo_updatever_vc'], $retval['mo_updateurl_tx']);
} else {
$retval = false;
}
return $retval;
}
/**
* Returns an array of the XML tags from the module.xml file.
* @author Bobby Allen (ballen@bobbyallen.me)
* @global obj $zlo The Generic ZPX logging object.
* @param string $modulefolder The module folder name of which to import the XML data from.
* @return mixed Will an array of the module XML data if the parsing of the document is successful otherwise will return 'false'.
*/
static function GetModuleXMLTags($modulefolder) {
global $zlo;
$mod_xml = "modules/$modulefolder/module.xml";
$info = array();
try {
$mod_config = new xml_reader(fs_filehandler::ReadFileContents($mod_xml));
$mod_config->Parse();
$info['name'] = $mod_config->document->name[0]->tagData;
$info['version'] = $mod_config->document->version[0]->tagData;
$info['desc'] = $mod_config->document->desc[0]->tagData;
$info['authorname'] = $mod_config->document->authorname[0]->tagData;
$info['authoremail'] = $mod_config->document->authoremail[0]->tagData;
$info['authorurl'] = $mod_config->document->authorurl[0]->tagData;
$info['updateurl'] = $mod_config->document->updateurl[0]->tagData;
return $info;
} catch (Exception $e) {
return false;
}
}
}
?>