-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.php
152 lines (140 loc) · 5.45 KB
/
module.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
<?php
@define('NWNADMIN_BASE', dirname(__FILE__));
require_once NWNADMIN_BASE . '/lib/base.php';
require_once NWNADMIN_BASE . '/lib/ModuleForms.php';
// script global variables
global $nwndriver;
$admin = Auth::isAdmin('nwnadmin:admin');
$adminDelete = Auth::isAdmin('nwnadmin:admin', PERMS_DELETE);
$moduleDir = NWNAdmin::getModulePath();
$moduleDirWritable = is_writable($moduleDir);
$serverUp = $nwndriver->serverRunning();
if ($admin && !$serverUp) {
$notification->push(
_("The server is down; module loading is unavailable."));
}
// figure out what to do
$actionId = Util::getFormData('actionId');
$moduleName = Util::getFormData('moduleName');
if (isset($actionId) && !isset($moduleName)) {
$notification->push(_("Invalid options! Try again..."), 'horde.warning');
} else {
switch($actionId) {
case 'delete':
if ($adminDelete &&
strpos($conf['server']['root'], $moduleName) == 0) {
$moduleName = str_replace('../', '', $moduleName);
$result = unlink(escapeshellcmd($moduleName));
if (!$result) {
$notification->push(_("Could not delete the module."),
'horde.error');
} else {
$notification->push(_("Successfully deleted the module."),
'horde.success');
}
}
break;
case 'activate':
if ($admin) {
$result = $nwndriver->useModule($moduleName, true);
if (is_a($result, 'PEAR_Error')) {
$notification->push(
_("Module load failure: ") .
$result->getMessage(), 'horde.error');
} else {
$notification->push(_("Module loaded."), 'horde.success');
}
}
break;
}
}
// form configuration
$vars = &Variables::getDefaultVariables();
$renderer = &new Horde_Form_Renderer();
$form = &Horde_Form::singleton('NewNWNModule', $vars);
$valid = $form->validate($vars);
if ($valid) {
// get the information about the file
$form->getInfo($vars, $info);
if (!empty($info['module']['file'])) {
// as long as it was uploaded and there is information
if (!is_a(Browser::wasFileUploaded('module'), 'PEAR_Error') &&
filesize($info['module']['file'])) {
// check the file for validity
$extension = substr($info['module']['name'], -3);
if ($extension == "mod" || $extension == "MOD") {
$moduleOutputName = escapeshellcmd($moduleDir .
$info['module']['name']);
// do not overwrite files
if (file_exists($moduleOutputName)) {
$notification->push(_("Cannot overwrite existing module!"),
'horde.error');
} else {
// get the file data
$fp = fopen($info['module']['file'], 'r');
$data = fread($fp, filesize($info['module']['file']));
fclose($fp);
// write the data to the output dir
$fp = @fopen($moduleOutputName, 'wb');
@fwrite($fp, $data);
@fclose($fp);
$notification->push(_("Successfully wrote the module: ") .
$info['module']['name'], 'horde.success');
}
} else {
$notification->push(_("The uploaded file does not appear ".
"to be a valid NeverWinter module."), 'horde.error');
}
} else {
// report the error
if (!empty($info['module']['error'])) {
$notification->push(sprintf(_("There was a problem " .
"uploading the module: %s"),
$info['module']['error']), 'horde.error');
} elseif (!filesize($info['module']['file'])) {
$notification->push(_("The uploaded file appears to " .
"be empty. It may not exist on your computer."),
'horde.error');
} else {
$notification->push(_("General failure, please debug!"),
'horde.error');
}
}
}
}
// get the listing of modules
$moduleList = NWNAdmin::getModuleList($moduleDir);
$moduleDone = empty($moduleList);
if ($moduleDone) {
$notification->push(_("No modules were found!"), 'horde.warning');
}
$currentModule = $nwndriver->getModule();
// page setup
$title = _("Modules");
require_once NWNADMIN_TEMPLATES . '/common-header.inc';
require_once NWNADMIN_TEMPLATES . '/menu.inc';
// render the available modules
if (!$moduleDone) {
require NWNADMIN_TEMPLATES . '/module/header.inc';
$style = 'item1';
foreach ($moduleList as $module) {
$currentFlag = false;
$baseModule = substr(basename($module), 0, -4);
if ($style == 'item1') {
$style = 'item0';
} else {
$style = 'item1';
}
if ($currentModule == $baseModule && $serverUp) {
$style = 'selectedRow'; $currentFlag = true;
}
include NWNADMIN_TEMPLATES . '/module/module.inc';
}
require NWNADMIN_TEMPLATES . '/module/footer.inc';
}
// render the upload form
if (isset($form) && $admin && $moduleDirWritable) {
$form->renderActive($renderer, $vars, 'module.php', 'post');
}
// finish up the page
require_once $registry->get('templates', 'horde') . '/common-footer.inc';