root/freepbx/branches/2.1/install_amp

Revision 2609, 27.1 kB (checked in by qldrob, 2 years ago)

Forgot to bump the version number. Sigh.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/php -q
2 <?php
3
4 // define versions. latest version must be last
5 $versions = array(
6                 '1.10.005',
7                 '1.10.006',
8                 '1.10.007beta1',
9                 '1.10.007beta2',
10                 '1.10.007',
11                 '1.10.007a',
12                 '1.10.008beta1',
13                 '1.10.008beta2',
14                 '1.10.008beta3',
15                 '1.10.008',
16                 '1.10.009beta1',
17                 '1.10.009beta2',
18                 '1.10.009',
19                 '1.10.010beta1',
20                 '1.10.010',
21                 '2.0beta1',
22                 '2.0beta2',
23                 '2.0beta3',
24                 '2.0beta4',
25                 '2.0beta5',
26                 '2.0.0',
27                 '2.0.1',
28                 '2.1beta1',
29                 '2.1beta2',
30                 '2.1beta3',
31                 '2.1.0',
32                 '2.1.1',
33                 '2.1.2',
34                 '2.1.3'
35         );
36
37 define("AMP_CONF", "/etc/amportal.conf");
38
39 define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf");
40
41 define("UPGRADE_DIR", dirname(__FILE__)."/upgrades");
42
43 /********************************************************************************************************************/
44
45 function out($text) {
46         echo $text."\n";
47 }
48
49 function outn($text) {
50         echo $text;
51 }
52
53 function error($text) {
54         echo "[ERROR] ".$text."\n";
55 }
56
57 function fatal($text) {
58         echo "[FATAL] ".$text."\n";
59         exit(1);
60 }
61
62 function debug($text) {
63         global $debug;
64        
65         if ($debug) echo "[DEBUG] ".$text."\n";
66 }
67
68 function showHelp() {
69         out("Optional parameters:");
70         out("  --help, -h, -?           Show this help");
71         out("  --username <user>        Use <user> to connect to db and write config");
72         out("  --password <pass>        Use <pass> to connect to db and write config");
73         out("  --debug                  Enable debug output");
74         out("  --dry-run                Don't actually do anything");
75         out("  --force-version <ver>    Force upgrade from version <ver>");
76         out("  --dbhost <ip address>    Use a remote database server");
77         out("  --no-files               Just run updates without installing files");
78 }
79
80 function install_parse_amportal_conf($filename) {
81         $file = file($filename);
82         foreach ($file as $line) {
83                 if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) {
84                         $conf[ $matches[1] ] = $matches[2];
85                 }
86         }
87         return $conf;
88 }
89
90 function install_parse_asterisk_conf($filename) {
91         $file = file($filename);
92         foreach ($file as $line) {
93                 if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) {
94                         $conf[ $matches[1] ] = $matches[2];
95                 }
96         }
97         return $conf;
98 }
99
100 //get the version number
101 function install_getversion() {
102         global $db;
103         $sql = "SELECT value FROM admin WHERE variable = 'version'";
104         $results = $db->getAll($sql);
105         if(DB::IsError($results)) {
106                 return false;
107         }
108         return $results[0][0];
109 }
110
111 //set the version number
112 function setversion($version) {
113         global $db;
114         $sql = "UPDATE admin SET value = '".$version."' WHERE variable = 'version'";
115         debug($sql);
116         $result = $db->query($sql);
117         if(DB::IsError($result)) {     
118                 die($result->getMessage());
119         }
120 }
121
122 function write_amportal_conf($filename, $conf) {
123         $file = file($filename);
124         // parse through the file
125         foreach (array_keys($file) as $key) {
126                 if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$file[$key],$matches)) {
127                         // this is an option=value line
128                         if (isset($conf[ $matches[1] ])) {
129                                 // rewrite the line, if we have this in $conf
130                                 $file[$key] = $matches[1]."=".$conf[ $matches[1] ]."\n";
131                                 // unset it so we know what's new
132                                 unset($conf[ $matches[1] ]);
133                         }
134                 }
135         }
136        
137         // add new entries
138         foreach ($conf as $key=>$val) {
139                 $file[] = $key."=".$val."\n";
140         }
141        
142         // write the file
143         if (!$fd = fopen($filename, "w")) {
144                 fatal("Could not open ".$filename." for writing");
145         }
146         fwrite($fd, implode("",$file));
147         fclose($fd);
148 }
149
150 function ask_overwrite($file1, $file2) {
151         global $check_md5s;
152         do {
153                 out($file2." has been changed from the original version.");
154                 outn("Overwrite (y=yes/a=all/n=no/d=diff/s=shell/x=exit)? ");
155                 $key = fgets(STDIN,1024);
156                 switch (strtolower($key[0])) {
157                         case "y": return true;
158                         case "a": $check_md5s=false; return true;
159                         case "n": return false;
160                         case "d":
161                                 out("");
162                                 passthru("diff -u ".$file2." ".$file1);
163                         break;
164                         case "s":
165                                 if (function_exists("pcntl_fork")) {
166                                         out("");
167                                         $shell = (isset($_ENV["SHELL"]) ? $_ENV["SHELL"] : "/bin/bash");
168                                         out("Dropping to shell. Type 'exit' to return");
169                                         out("-> Original file:  ".$file2);
170                                         out("-> New file:       ".$file1);
171                                        
172                                         $pid = pcntl_fork();
173                                         if ($pid == -1) {
174                                                 out("[ERROR] cannot fork");
175                                         } else if ($pid) {
176                                                 // parent
177                                                 pcntl_waitpid($pid, $status);
178                                                 // we wait till the child exits/dies/whatever
179                                         } else {
180                                                 pcntl_exec($shell, array(), $_ENV);
181                                         }
182                                        
183                                         out("Returned from shell");
184                                 } else {
185                                         out("[ERROR] PHP not built with process control (--enable-pcntl) support: cannot spawn shell");
186                                 }
187                                
188                         break;
189                         case "x":
190                                 out("-> Original file:  ".$file2);
191                                 out("-> New file:       ".$file1);
192                                 out("Exiting install program.");
193                                 exit(1);
194                         break;
195                 }
196                 out("");
197         } while(1);
198 }
199
200 function amp_mkdir($directory, $mode = "0755", $recursive = false) {
201         debug("mkdir ".$directory.", ".$mode);
202         $ntmp = sscanf($mode,"%o",$modenum); //assumes all inputs are octal
203         if (version_compare(phpversion(), 5.0) < 0) {
204                 // php <5 can't recursively create directories
205                 if ($recursive) {
206                         $output = false;
207                         $return_value = false;
208                         exec("mkdir -m ".$mode." -p ".$directory,  $output, $return_value);
209                         return ($return_value == 0);
210                 } else {
211                         return mkdir($directory, $modenum);
212                 }
213         } else {
214                 return mkdir($directory, $modenum, $recursive);
215         }
216 }
217
218 /** Recursively copy a directory
219  */
220 function recursive_copy($dirsourceparent, $dirdest, &$md5sums, $dirsource = "") {
221         global $dryrun;
222         global $check_md5s;
223         global $amp_conf;
224         global $asterisk_conf;
225        
226         if ($dirsource && ($dirsource[0] != "/")) $dirsource = "/".$dirsource;
227        
228         if (is_dir($dirsourceparent.$dirsource)) $dir_handle = opendir($dirsourceparent.$dirsource);
229        
230         /*
231         echo "dirsourceparent: "; var_dump($dirsourceparent);
232         echo "dirsource: "; var_dump($dirsource);
233         echo "dirdest: "; var_dump($dirdest);
234         */
235        
236         while (isset($dir_handle) && ($file = readdir($dir_handle))) {
237                 if (($file!=".") && ($file!="..") && ($file != "CVS") && ($file != ".svn")) {
238                         $source = $dirsourceparent.$dirsource."/".$file;
239                         $destination =  $dirdest.$dirsource."/".$file;
240                        
241                         // configurable in amportal.conf
242                         if (strpos($destination,"htdocs_panel")) {
243                                 $destination=str_replace("/htdocs_panel",trim($amp_conf["FOPWEBROOT"]),$destination);
244                         } else {
245                                 $destination=str_replace("/htdocs",trim($amp_conf["AMPWEBROOT"]),$destination);
246                         }
247                         $destination=str_replace("/htdocs_panel",trim($amp_conf["FOPWEBROOT"]),$destination);
248                         $destination=str_replace("/cgi-bin",trim($amp_conf["AMPCGIBIN"]),$destination);
249                         $destination=str_replace("/bin",trim($amp_conf["AMPBIN"]),$destination);
250                         $destination=str_replace("/sbin",trim($amp_conf["AMPSBIN"]),$destination);
251                        
252                         // the following are configurable in asterisk.conf
253                         $destination=str_replace("/astetc",trim($asterisk_conf["astetcdir"]),$destination);
254                         $destination=str_replace("/mohmp3",trim($asterisk_conf["astvarlibdir"])."/mohmp3",$destination);
255                         $destination=str_replace("/astvarlib",trim($asterisk_conf["astvarlibdir"]),$destination);
256                         $destination=str_replace("/agi-bin",trim($asterisk_conf["astagidir"]),$destination);
257                         $destination=str_replace("/sounds",trim($asterisk_conf["astvarlibdir"])."/sounds",$destination);
258                        
259                         // if this is a directory, ensure destination exists
260                         if (is_dir($source)) {
261                                 if (!file_exists($destination)) {
262                                         if ((!$dryrun) && ($destination != "")) {
263                                                 amp_mkdir($destination, "0750", true);
264                                         }
265                                 }
266                         }
267                        
268                         if (!is_dir($source)) {
269                                 if ($check_md5s && file_exists($destination) && isset($md5sums[$destination]) && (md5_file($destination) != $md5sums[$destination])) {
270                                         $overwrite = ask_overwrite($source, $destination, $md5sums);
271                                 } else {
272                                         $overwrite = true;
273                                 }
274                                
275                                 if ($overwrite) {
276                                         debug("copy ".$source." -> ".$destination);
277                                         if (!$dryrun) {
278                                                 copy($source, $destination);
279                                         }
280                                 } else {
281                                         debug("not overwriting ".$destination);
282                                 }
283                         } else {
284                                 //echo "recursive_copy($dirsourceparent, $dirdest, $md5sums, $dirsource/$file)";
285                                 recursive_copy($dirsourceparent, $dirdest, $md5sums, $dirsource."/".$file);
286                         }
287                 }
288         }
289        
290         if (isset($dir_handle)) closedir($dir_handle);
291        
292         return true;
293 }
294
295 function read_md5_file($filename) {
296         $md5 = array();
297         if (file_exists($filename)) {
298                 foreach (file($filename) as $line) {
299                         if (preg_match("/^([a-f0-9]{32})\s+(.*)$/", $line, $matches)) {
300                                 $md5[ "/".$matches[2] ] = $matches[1];
301                         }
302                 }
303         }
304         return $md5;
305 }
306
307 /** Include a .php file
308  * This is a function just to keep a seperate context
309  */
310 function run_included($file) {
311         global $db;
312         global $amp_conf;
313        
314         include($file);
315 }
316
317 /** Install a particular version
318  */
319 function install_upgrade($version) {
320         global $db;
321         global $dryrun;
322        
323         if (is_dir(UPGRADE_DIR."/".$version)) {
324                 // sql scripts first
325                 $dir = opendir(UPGRADE_DIR."/".$version);
326                 while ($file = readdir($dir)) {
327                         if (($file[0] != ".") && is_file(UPGRADE_DIR."/".$version."/".$file)) {
328                                 if (strtolower(substr($file,-4)) == ".sql") {
329                                         out("-> Running SQL script ".UPGRADE_DIR."/".$version."/".$file);
330                                         // run sql script
331                                         $fd = fopen(UPGRADE_DIR."/".$version."/".$file, "r");
332                                         $data = "";
333                                         while (!feof($fd)) {
334                                                 $data .= fread($fd, 1024);
335                                         }
336                                         fclose($fd);
337
338                                         preg_match_all("/((SELECT|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER).*);\s*\n/Us", $data, $matches);
339                                        
340                                         foreach ($matches[1] as $sql) {
341                                                 debug($sql);
342                                                 if (!$dryrun) {
343                                                         $result = $db->query($sql);
344                                                         if(DB::IsError($result)) {     
345                                                                 fatal($result->getDebugInfo()."\" while running ".$file."\n");
346                                                         }
347                                                 }
348                                         }
349                                 }
350                         }
351                 }
352
353                 // now non sql scripts
354                 $dir = opendir(UPGRADE_DIR."/".$version);
355                 while ($file = readdir($dir)) {
356                         if (($file[0] != ".") && is_file(UPGRADE_DIR."/".$version."/".$file)) {
357                                 if (strtolower(substr($file,-4)) == ".sql") {
358                                         // sql scripts were dealt with first
359                                 } else if (strtolower(substr($file,-4)) == ".php") {
360                                         out("-> Running PHP script ".UPGRADE_DIR."/".$version."/".$file);
361                                         if (!$dryrun) {
362                                                 run_included(UPGRADE_DIR."/".$version."/".$file);
363                                         }
364
365                                 } else if (is_executable(UPGRADE_DIR."/".$version."/".$file)) {
366                                         out("-> Executing ".UPGRADE_DIR."/".$version."/".$file);
367                                         if (!$dryrun) {
368                                                 exec(UPGRADE_DIR."/".$version."/".$file);
369                                         }
370                                 } else {
371                                         error("-> Don't know what to do with ".UPGRADE_DIR."/".$version."/".$file);
372                                 }
373                         }
374                 }
375
376         }
377 }
378
379 /** Invoke upgrades
380  * @param $versions array       The version upgrade scripts to run
381  */
382 function run_upgrade($versions) {
383         global $dryrun;
384        
385         foreach ($versions as $version) {
386                 out("Upgrading to ".$version."..");
387                 install_upgrade($version);
388                 if (!$dryrun) {
389                         setversion($version);
390                 }
391                 out("Upgrading to ".$version."..OK");
392         }
393 }
394
395 /** Write AMP-generated configuration files
396  */
397 function generate_configs() {
398         global $amp_conf;
399         global $dryrun;
400        
401         out("Generating Configurations.conf..");
402         if (!$dryrun)
403                 passthru("su - asterisk -c ".trim($amp_conf["AMPBIN"])."/retrieve_conf");
404 }
405
406
407 /** Set reload flag for AMP admin
408  */
409 function install_needreload() {
410         global $db;
411         $sql = "UPDATE admin SET value = 'true' WHERE variable = 'need_reload'";
412         $result = $db->query($sql);
413         if(DB::IsError($result)) {     
414                 die($result->getMessage());
415         }
416 }
417
418
419 /** Collect AMP settings
420  */
421 function collect_settings($filename, $dbhost = '', $dbuser = '', $dbpass = '') {
422         out("Creating new /etc/amportal.conf");
423        
424         outn("Enter your USERNAME to connect to the 'asterisk' database:\n [".($dbuser ? $dbuser : "asteriskuser")."] ");
425         $key = trim(fgets(STDIN,1024));
426         if (preg_match('/^$/',$key)) $amp_conf["AMPDBUSER"] = ($dbuser ? $dbuser : "asteriskuser");
427         else $amp_conf["AMPDBUSER"] = $key;
428        
429         outn("Enter your PASSWORD to connect to the 'asterisk' database:\n [".($dbpass ? $dbpass : "amp109")."] ");
430         $key = trim(fgets(STDIN,1024));
431         if (preg_match('/^$/',$key)) $amp_conf["AMPDBPASS"] = ($dbpass ? $dbpass : "amp109");
432         else $amp_conf["AMPDBPASS"] = $key;
433        
434         outn("Enter the hostname of the 'asterisk' database:\n [".($dbhost ? $dbhost : "localhost")."] ");
435         $key = trim(fgets(STDIN,1024));
436         if (preg_match('/^$/',$key)) $amp_conf["AMPDBHOST"] = ($dbhost ? $dbhost : "localhost");
437         else $amp_conf["AMPDBHOST"] = $key;
438        
439         outn("Enter a USERNAME to connect to the Asterisk Manager interface:\n [admin] ");
440         $key = trim(fgets(STDIN,1024));
441         if (preg_match('/^$/',$key)) $amp_conf["AMPMGRUSER"] = "admin";
442         else $amp_conf["AMPMGRUSER"] = $key;
443        
444         outn("Enter a PASSWORD to connect to the Asterisk Manager interface:\n [amp111] ");
445         $key = trim(fgets(STDIN,1024));
446         if (preg_match('/^$/',$key)) $amp_conf["AMPMGRPASS"] = "amp111";
447         else $amp_conf["AMPMGRPASS"] = $key;
448        
449         do {
450                 out("Enter the path to use for your AMP web root:\n [/var/www/html] ");
451                 $key = trim(fgets(STDIN,1024));
452                 if (preg_match('/^$/',$key)) $amp_conf["AMPWEBROOT"] = "/var/www/html";
453                 else $amp_conf["AMPWEBROOT"] = rtrim($key,'/');
454                 if (is_dir($amp_conf["AMPWEBROOT"])) {
455                         break;
456                 } else if (amp_mkdir($amp_conf["AMPWEBROOT"],"0755",true