ona  18.1.1
About: OpenNetAdmin provides a database managed inventory of your IP network (with Web and CLI interface).
  Fossies Dox: ona-18.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

install.php
Go to the documentation of this file.
1 <?php
2 
3 // Get some standard global variables
4 global $base, $conf, $self, $onadb;
5 
6 // Gather various bits of information about the plugin
7 $onainstalldir = dirname($base);
8 $plugindir = str_replace($onainstalldir.'/www', '', dirname(__FILE__));
9 $installfile = __FILE__;
10 $installdir = dirname(__FILE__);
11 $stat = 0;
12 
13 // Check permissions
14 if (!auth('advanced')) {
15  $window['js'] = "alert('Permission denied!'); removeElement('{$window_name}');";
16  return;
17 }
18 
19 
20 //----------------------------Change these values for your plugin---------------------------
21 
22 // Define this plugins name, must be same as the directory it will live in
23 $plugin_name = 'ona_nmap_scans';
24 
25 // Set a title
26 $window['title'] = "ONA Nmap Scans Install";
27 
28 // Add any DCM module names related to this plugin
29 // each new module requires a description and a file path name
30 // the dcm module name is the first field in the array
31 //
32 // EXAMPLE
33 // $pmodules['rack_del']['desc'] = 'Delete a rack';
34 // $pmodules['rack_del']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
35 //
36 // If you do not specify a file entry, it will default to the path listed in the example
37 //
38 $pmodules = array();
39 //$pmodules['puppet_fact_add']['desc'] = 'Add puppet facts to a host';
40 //$pmodules['puppet_fact_add']['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
41 
42 //------------------------------------------------------------------------------------------
43 
44 
45 
46 
47 // Provide basic javascript for the new popup window
48 $window['js'] .= <<<EOL
49  /* Put a minimize icon in the title bar */
50  el('{$window_name}_title_r').innerHTML =
51  '&nbsp;<a onClick="toggle_window(\'{$window_name}\');" title="Minimize window" style="cursor: pointer;"><img src="{$images}/icon_minimize.gif" border="0" /></a>' +
52  el('{$window_name}_title_r').innerHTML;
53 
54  /* Put a help icon in the title bar */
55  el('{$window_name}_title_r').innerHTML =
56  '&nbsp;<a href="{$_ENV['help_url']}{$window_name}" target="null" title="Help" style="cursor: pointer;"><img src="{$images}/silk/help.png" border="0" /></a>' +
57  el('{$window_name}_title_r').innerHTML;
58 
60 
61 $window['html'] .= "<div style='max-height: 500px;max-width:750;overflow: auto;padding: 5px;'>";
62 
63 
64 if (!is_writable($conf['plugin_dir'])) {
65  $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You might execute the command: <font color='orange'>chown -R {$_ENV['APACHE_RUN_USER']} {$conf['plugin_dir']}</font><br>";
66  $stat++;
67 }
68 
69 // If we have defined modules, process them
70 if (count($pmodules) > 0 ) {
71  $window['html'] .= <<<EOL
72 <br><b>Installing new DCM modules:</b><br>
73 EOL;
74 
75 
76 
77  // Get list of existing DCM modules to see if they are already installed, Use cache if possible
78  if (!is_array($self['cache']['modules']) or !array_key_exists('get_module_list', $self['cache']['modules'])) {
79  require_once($conf['dcm_module_dir'] . '/get_module_list.inc.php');
80  list($status, $self['cache']['modules']) = get_module_list('type=array');
81  }
82 
83  // If the new module does not already exist, add it
84  foreach ($pmodules as $modname => $attributes) {
85  if (!array_key_exists($modname,$self['cache']['modules'])) {
86  // default the file location if it is not set to use the main lugin file
87  if (!$attributes['file']) $attributes['file'] = "..{$plugindir}/{$plugin_name}.inc.php";
88  list($status, $output) = run_module('add_module', array('name' => $modname, 'desc' => $attributes['desc'], 'file' => $attributes['file']));
89  if ($status) {
90  $stat++;
91  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/error.png' border='0'> {$modname} failed to install.<br>";
92  } else {
93  printmsg("DEBUG => Plugin install for {$plugin_name} created new DCM module {$modname}.",2);
94  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> {$modname}<br>";
95  }
96  } else {
97  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> {$modname}, already installed.<br>";
98  }
99  }
100 }
101 
102 // If there is a SQL file to process. lets do that
103 $sqlfile = dirname(__FILE__)."/{$plugin_name}.sql";
104 if (file_exists($sqlfile)) {
105 
106  $sqlcontent = file_get_contents($sqlfile);
107  $statements = preg_split("/;/", $sqlcontent);
108 //print_r($statement);
109 
110  $has_trans = $onadb->BeginTrans();
111  if (!$has_trans) printmsg("WARNING => Transactions support not available on this database, this can cause problems!", 1);
112 
113  // If begintrans worked and we support transactions, do the smarter "starttrans" function
114  if ($has_trans) {
115  printmsg("DEBUG => Starting transaction", 2);
116  $onadb->StartTrans();
117  }
118 
119 
120  // Run the SQL
121  printmsg("DEBUG => Installing {$modname} plugin SQL statements.", 4);
122  $i = 0;
123  while ($i < count($statements)-1) {
124 
125  // The SQL statements are split above based on a ; character.
126  // This may not always work but should cover most things, just be aware.
127  //$window['html'] .= $statements[$i].'---<br><br>';
128  $ok = $onadb->Execute($statements[$i].';');
129  $error = $onadb->ErrorMsg();
130 
131  if ($ok === false or $error) {
132  if ($has_trans) {
133  printmsg("INFO => There was a module error, marking transaction for a Rollback!", 1);
134  $onadb->FailTrans();
135  }
136  break;
137  }
138  $i++;
139  }
140 
141  // Report any errors
142  if ($ok === false or $error) {
143  $window['html'] .= <<<EOL
144  <br><b>Installing database updates:</b><br>
145  <img src='{$images}/silk/error.png' border='0'> <font color="red">ERROR => SQL statements failed:</font><br><pre>{$error}</pre>
146  <br><img src='{$images}/silk/error.png' border='0'> Unable to automatically process SQL statements<br>
147  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="orange">Please try again, or add the following SQL statements manually:</font>
148  <pre>
149  {$sqlcontent}
150  </pre>
151  <br>
152  <font color="orange">Possibly use the following command:<br>
153  mysql -u {$self['db_login']} -p{$self['db_passwd']} {$self['db_database']} < {$sqlfile}</font><br><br>
154 EOL;
155  $stat++;
156  } else {
157  $window['html'] .= <<<EOL
158  <br><b>Installing database updates:</b><br>
159  &nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> All SQL updates were successful.<br>
160 EOL;
161  if ($has_trans) { $onadb->CompleteTrans(); }
162  }
163 
164 }
165 
166 // Lets check for files that should be installed.
167 $window['html'] .= "<br><b>Checking required files:</b><br>";
168 
169 $reqfile = $onainstalldir.'/www/local/nmap_scans';
170 if (is_dir($reqfile)) {
171  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> {$reqfile}<br>";
172 } else {
173  // make the dir
174  if (@mkdir($reqfile)) {
175  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> {$reqfile}<br>";
176  @mkdir($reqfile.'/subnets');
177  } else {
178  $stat++;
179  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/error.png' border='0'> <font color=\"red\">ERROR=> Unable to make required directory, try the following:</font><br>";
180  // Provide some information to the user on how to fix the problem manually
181  $window['html'] .= "<font color=\"orange\">Execute shell command:<br>mkdir {$onainstalldir}/www/local/nmap_scans</font><br><br>";
182  $window['html'] .= "<font color=\"orange\">Execute shell command:<br>mkdir {$onainstalldir}/www/local/nmap_scans/subnets</font><br><br>";
183  }
184 }
185 
186 $reqfile = $onainstalldir.'/www/local/nmap_scans/subnets/nmap.xsl';
187 if (file_exists($reqfile)) {
188  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> {$reqfile}<br>";
189 } else {
190  $stat++;
191  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/error.png' border='0'> <font color=\"red\">ERROR=> Unable to find required file, try the following:</font><br>";
192  // Provide some information to the user on how to fix the problem manually
193  $window['html'] .= "<font color=\"orange\">Execute shell command:<br>cp {$installdir}/nmap.xsl {$reqfile}</font><br><br>";
194 }
195 
196 $reqfile = $onainstalldir.'/bin/nmap_scan_cron';
197 if (file_exists($reqfile)) {
198  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> {$reqfile}<br>";
199 } else {
200  $stat++;
201  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/error.png' border='0'> <font color=\"red\">ERROR=> Unable to find required file, try the following:</font><br>";
202  // Provide some information to the user on how to fix the problem manually
203  $window['html'] .= "<font color=\"orange\">Execute shell command:<br>ln -s {$installdir}/nmap_scan_cron {$reqfile}</font><br><br>";
204 }
205 
206 $reqfile = $onainstalldir.'/bin/dcm.pl';
207 if (file_exists($reqfile)) {
208  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> {$reqfile}<br>";
209 } else {
210  $stat++;
211  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/error.png' border='0'> <font color=\"red\">ERROR=> Unable to find required file, try the following:</font><br>";
212  // Provide some information to the user on how to fix the problem manually
213  $window['html'] .= "<font color=\"orange\">Please install <a href=\"http://opennetadmin.com/docs/utils/dcm.pl\">dcm.pl</a>:<br>Make a symlink or install into {$onainstalldir}/bin</font><br><br>";
214 }
215 
216 $reqfile = '/usr/bin/nmap';
217 if (file_exists($reqfile)) {
218  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/accept.png' border='0'> {$reqfile}<br>";
219 } else {
220  //$stat++;
221  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<img src='{$images}/silk/error.png' border='0'> <font color=\"red\">ERROR=> Unable to find required file, try the following:</font><br>";
222  // Provide some information to the user on how to fix the problem manually
223  $window['html'] .= "<font color=\"orange\">Install nmap package or adjust {$installdir}/nmap_scan_cron to point to proper nmap locaiton</font><br><br>";
224 }
225 
226 $window['html'] .= "<br><b>Disabling install script:</b><br>";
227 // If there were no errors, move this install file out of the way.
228 if (!$stat) {
229  $window['html'] .= @rename(__FILE__, __FILE__.'.completed') ? "&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;<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>";
230 } else {
231  $window['html'] .= "&nbsp;&nbsp;&nbsp;&nbsp;<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>";
232 
233 }
234 
235 $window['html'] .= "<br><br><center><font color='green'>END OF INSTALL</font></center></div>";
236 
237 ?>
$plugindir
$plugindir
Definition: install.php:8
$plugin_name
if(!auth('advanced')) $plugin_name
Definition: install.php:23
$base
$base
Definition: install.php:8
$sqlfile
if(!is_writable($conf['plugin_dir'])) if(count($pmodules) > 0) $sqlfile
Definition: install.php:103
$status
$status
Definition: install.php:12
$onainstalldir
$onainstalldir
Definition: install.php:7
$window
$window['title']
Definition: install.php:26
$conf
global $conf
Definition: install.php:4
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
$stat
$stat
Definition: install.php:11
get_module_list
get_module_list($options="type=string")
Definition: get_module_list.inc.php:23
run_module
run_module($module='', $options='', $transaction=1)
Definition: functions_general.inc.php:1468
$onadb
global $onadb
Definition: install.php:4
EOL
< a style="text-decoration: none;" href="/">< input class='edit' type="button" value="I don't like free stuff?" onclick=""/></a ></center ></div > EOL
Definition: install.php:40
$installdir
$installdir
Definition: install.php:10
$reqfile
$reqfile
Definition: install.php:169
nbsp
& nbsp
Definition: install.php:36
$output
$output
Definition: dcm.php:16
$self
global $self
Definition: install.php:4
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
$pmodules
$pmodules
Definition: install.php:38
$installfile
$installfile
Definition: install.php:9