-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.php
228 lines (180 loc) · 9.79 KB
/
install.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
<?php
// Get some standard global variables
global $base, $conf, $self, $onadb;
// Gather various bits of information about the plugin
$onainstalldir = dirname($base);
$plugindir = str_replace($onainstalldir.'/www', '', dirname(__FILE__));
$installfile = __FILE__;
$stat = 0;
// Check permissions
// if (!auth('advanced')) {
// $window['js'] = "alert('Permission denied!'); removeElement('{$window_name}');";
// return;
// }
//----------------------------Change these values for your plugin---------------------------
// Define this plugins name, must be same as the directory it will live in
$plugin_name = 'rack_maint';
// Set a title
$window['title'] = "Rack Maint Install";
// Add any DCM module names related to this plugin
// each new module requires a description and a file path name
// the dcm module name is the first field in the array
//
// EXAMPLE
// $pmodules['rack_del']['desc'] = 'Delete a rack';
// $pmodules['rack_del']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
//
// If you do not specify a file entry, it will default to the path listed in the example
//
$pmodules = array();
$pmodules['rack_assignment_add']['desc'] = 'Assign a device within a rack';
//$pmodules['rack_assignment_add']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
$pmodules['rack_assignment_modify']['desc'] = 'Modify a rack assignment';
//$pmodules['rack_assignment_modify']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
$pmodules['rack_assignment_del']['desc'] = 'Delete a rack assignment';
//$pmodules['rack_assignment_del']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
$pmodules['rack_add']['desc'] = 'Add a rack';
//$pmodules['rack_add']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
$pmodules['rack_modify']['desc'] = 'Modify a rack';
$pmodules['rack_modify']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
$pmodules['rack_del']['desc'] = 'Delete a rack';
$pmodules['rack_del']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
$ppermissions['rack_add']['desc'] = 'view rack';
$ppermissions['rack_mod']['desc'] = 'Add and modify rack';
$ppermissions['rack_del']['desc'] = 'Delete rack';
//------------------------------------------------------------------------------------------
// Provide basic javascript for the new popup window
$window['js'] .= <<<EOL
/* Put a minimize icon in the title bar */
el('{$window_name}_title_r').innerHTML =
' <a onClick="toggle_window(\'{$window_name}\');" title="Minimize window" style="cursor: pointer;"><img src="{$images}/icon_minimize.gif" border="0" /></a>' +
el('{$window_name}_title_r').innerHTML;
/* Put a help icon in the title bar */
el('{$window_name}_title_r').innerHTML =
' <a href="{$_ENV['help_url']}{$window_name}" target="null" title="Help" style="cursor: pointer;"><img src="{$images}/silk/help.png" border="0" /></a>' +
el('{$window_name}_title_r').innerHTML;
EOL;
$window['html'] .= "<div style='max-height: 500px;max-width:750;overflow: auto;padding: 5px;'>";
if (!is_writable($conf['plugin_dir'])) {
$window['html'] .= "<br><img src='{$images}/silk/error.png' border='0'><font color=\"red\"> ERROR=> The plugin directory '{$conf['plugin_dir']}' is not writable by the web server!</font><br> You might execute the command: <font color='orange'>chown -R {$_ENV['APACHE_RUN_USER']} {$conf['plugin_dir']}</font><br>";
$stat++;
}
// If we have defined modules, process them
if (count($pmodules) > 0 ) {
$window['html'] .= <<<EOL
<br><b>Installing new DCM modules:</b><br>
EOL;
// Get list of existing DCM modules to see if they are already installed, Use cache if possible
if (!is_array($self['cache']['modules']) or !array_key_exists('get_module_list', $self['cache']['modules'])) {
require_once($conf['dcm_module_dir'] . '/get_module_list.inc.php');
list($status, $self['cache']['modules']) = get_module_list('type=array');
}
// If the new module does not already exist, add it
foreach ($pmodules as $modname => $attributes) {
if (!array_key_exists($modname,$self['cache']['modules'])) {
// default the file location if it is not set to use the main lugin file
if (!$attributes['file']) $attributes['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
list($status, $output) = run_module('add_module', array('name' => $modname, 'desc' => $attributes['desc'], 'file' => $attributes['file']));
if ($status) {
$stat++;
$window['html'] .= " <img src='{$images}/silk/error.png' border='0'> {$modname} failed to install.<br>";
} else {
printmsg("DEBUG => Plugin install for {$plugin_name} created new DCM module {$modname}.",2);
$window['html'] .= " <img src='{$images}/silk/accept.png' border='0'> {$modname}<br>";
}
} else {
$window['html'] .= " <img src='{$images}/silk/accept.png' border='0'> {$modname}, already installed.<br>";
}
}
}
// If we have defined permissions, process them
if (count($ppermissions) > 0 ) {
$window['html'] .= <<<EOL
<br><b>Installing new plugin permissions:</b><br>
EOL;
// Get list of existing permissions to see if they are already installed, Use cache if possible
if (!is_array($self['cache']['permissions'])) {
list($status, $rows, $perms) = db_get_records($onadb, 'permissions', 'name IS NOT NULL');
}
foreach ($perms as $perm) {
$self['cache']['permissions'][$perm['name']] = $perm['description'];
}
// If the new permission does not already exist, add it
foreach ($ppermissions as $permname => $attributes) {
if (!array_key_exists($permname,$self['cache']['permissions'])) {
list($status, $output) = run_module('add_permission', array('name' => $permname, 'desc' => $attributes['desc']));
if ($status) {
$stat++;
$window['html'] .= " <img src='{$images}/silk/error.png' border='0'> {$permname} failed to install.<br>";
} else {
printmsg("DEBUG => Plugin install for {$plugin_name} created new permission {$permname}.",2);
$window['html'] .= " <img src='{$images}/silk/accept.png' border='0'> {$permname}<br>";
}
} else {
$window['html'] .= " <img src='{$images}/silk/accept.png' border='0'> {$permname}, already installed.<br>";
}
}
}
// If there is a SQL file to process. lets do that
$sqlfile = dirname(__FILE__)."/install.sql";
if (file_exists($sqlfile)) {
$sqlcontent = file_get_contents($sqlfile);
$statements = preg_split("/;/", $sqlcontent);
//print_r($statement);
$has_trans = $onadb->BeginTrans();
if (!$has_trans) printmsg("WARNING => Transactions support not available on this database, this can cause problems!", 1);
// If begintrans worked and we support transactions, do the smarter "starttrans" function
if ($has_trans) {
printmsg("DEBUG => Starting transaction", 2);
$onadb->StartTrans();
}
// Run the SQL
printmsg("DEBUG => Installing {$modname} plugin SQL statements.", 4);
$i = 0;
while ($i < count($statements)-1) {
// The SQL statements are split above based on a ; character.
// This may not always work but should cover most things, just be aware.
//$window['html'] .= $statements[$i].'---<br><br>';
$ok = $onadb->Execute($statements[$i].';');
$error = $onadb->ErrorMsg();
if ($ok === false or $error) {
if ($has_trans) {
printmsg("INFO => There was a module error, marking transaction for a Rollback!", 1);
$onadb->FailTrans();
}
break;
}
$i++;
}
// Report any errors
if ($ok === false or $error) {
$window['html'] .= <<<EOL
<br><b>Installing database updates:</b><br>
<img src='{$images}/silk/error.png' border='0'> <font color="red">ERROR => SQL statements failed:</font><br><pre>{$error}</pre>
<br><img src='{$images}/silk/error.png' border='0'> Unable to automatically process SQL statements<br>
<font color="orange">Please try again, or add the following SQL statements manually:</font>
<pre>
{$sqlcontent}
</pre>
<br>
<font color="orange">Possibly use the following command:<br>
mysql -u {$self['db_login']} -p{$self['db_passwd']} {$self['db_database']} < {$sqlfile}</font><br><br>
EOL;
$stat++;
} else {
$window['html'] .= <<<EOL
<br><b>Installing database updates:</b><br>
<img src='{$images}/silk/accept.png' border='0'> All SQL updates were successful.<br>
EOL;
if ($has_trans) { $onadb->CompleteTrans(); }
}
}
$window['html'] .= "<br><b>Disabling install script:</b><br>";
// If there were no errors, move this install file out of the way.
if (!$stat) {
$window['html'] .= @rename(__FILE__, __FILE__.'.completed') ? " <img src='{$images}/silk/accept.png' border='0'>Moved install files.<br><br><center><b>Install complete.</b><br><a onclick=\"removeElement('{$window_name}');\">CLOSE WINDOW</a></center>" : "<br> <img src='{$images}/silk/error.png' border='0'> <font color=\"red\">ERROR=> Unable to rename install file, do it manually then close this window:</font><br><br><font color=\"orange\">mv {$installfile} {$installfile}.completed</font><br><br>";
} else {
$window['html'] .= " <img src='{$images}/silk/error.png' border='0'> Not disabling install script due to previous errors.<br><br><center><a onclick=\"removeElement('{$window_name}');toggle_window('{$window_name}');\">Fix the errors and then click to TRY AGAIN</a></center>";
}
$window['html'] .= "<br><br><center><font color='green'>END OF INSTALL</font></center></div>";
?>