root/freepbx/branches/2.3/libfreepbx.install.php

Revision 4417, 11.5 kB (checked in by p_lindheimer, 1 year ago)

- moved version_compare_freepbx back to install_amp from libfreepbx.install.php
- added module_admin call to install packaged modules, uses force flag, all

dependencies need to be pre-confirmed.

- added skip-module-install flag to skip the above
- made default variables global in collect_settings() (was giving warnings)
- run asterisk -rx 'reload manager' after apply_conf.sh is run in case

settings changed so retrieve_conf can get to it later.

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 /********************************************************************************************************************/
4 /* freepbxlib.install.php
5  *
6  * These are used by install_amp and the framework install script to run updates
7  *
8  * These variables are required to be defined outside of this library. The purpose
9  * of this is to allow the library to be used by both install_amp as well as the
10  * framework which would potentially be accessing these from different locations.
11  *
12  * Examples:
13  *
14  * UPGRADE_DIR     dirname(__FILE__)."/upgrades"
15  * MODULE_DIR      dirname(__FILE__)."/amp_conf/htdocs/admin/modules/"
16  *
17  * or (in framework for instance)
18  *
19  * MODULE_DIR      dirname(__FILE__)."/htdocs/admin/modules/"
20  *
21  * $debug = false;
22  * $dryrun = false;
23  */
24
25 function upgrade_all($version) {
26
27         // **** Read upgrades/ directory
28
29         outn("Checking for upgrades..");
30
31         // read versions list from ugprades/
32         $versions = array();
33         $dir = opendir(UPGRADE_DIR);
34         while ($file = readdir($dir)) {
35                 if (($file[0] != ".") && is_dir(UPGRADE_DIR."/".$file)) {
36                         $versions[] = $file;
37                 }
38         }
39         closedir($dir);
40
41         // callback to use php's version_compare() to sort
42         usort($versions, "version_compare_freepbx");
43
44
45         // find versions that are higher than the current version
46         $starting_version = false;
47         foreach ($versions as $check_version) {
48                 if (version_compare_freepbx($check_version, $version) > 0) { // if check_version < version
49                         $starting_version = $check_version;
50                         break;
51                 }
52         }
53
54         // run all upgrades from the list of higher versions
55         if ($starting_version) {
56                 $pos = array_search($starting_version, $versions);
57                 $upgrades = array_slice($versions, $pos); // grab the list of versions, starting at $starting_version
58                 out(count($upgrades)." found");
59                 run_upgrade($upgrades);
60
61                 /* Set the base version of key modules, currently core and framework, to the
62                  * Version packaged with this tarball, if any. The expectation is that the
63                  * packaging scripts will make these module version numbers the same as the
64                  * release plus a '.0' which can be incremented for bug fixes delivered through
65                  * the online system between main releases.
66                  *
67                  * added if function_exists because if this is being run from framework there is no
68                  * need to reset the base version.
69                  */
70                 if (function_exists('set_base_version')) {
71                         set_base_version();
72                 }
73
74         } else {
75                 out("No upgrades found");
76         }
77
78 }
79
80 //----------------------------------
81 // dependencies for upgrade_all
82
83
84 /** Invoke upgrades
85  * @param $versions array       The version upgrade scripts to run
86  */
87 function run_upgrade($versions) {
88         global $dryrun;
89        
90         foreach ($versions as $version) {
91                 out("Upgrading to ".$version."..");
92                 install_upgrade($version);
93                 if (!$dryrun) {
94                         setversion($version);
95                 }
96                 out("Upgrading to ".$version."..OK");
97         }
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 /** Install a particular version
123  */
124 function install_upgrade($version) {
125         global $db;
126         global $dryrun;
127         global $amp_conf;
128        
129         $db_engine = $amp_conf["AMPDBENGINE"];
130        
131         if (is_dir(UPGRADE_DIR."/".$version)) {
132                 // sql scripts first
133                 $dir = opendir(UPGRADE_DIR."/".$version);
134                 while ($file = readdir($dir)) {
135                         if (($file[0] != ".") && is_file(UPGRADE_DIR."/".$version."/".$file)) {
136                                 if ( (strtolower(substr($file,-4)) == ".sqlite") && ($db_engine == "sqlite") ) {
137                                         install_sqlupdate( $version, $file );
138                                 }
139                                 elseif ((strtolower(substr($file,-4)) == ".sql") &&
140                                                 ( ($db_engine  == "mysql")  ||  ($db_engine  == "pgsql") || ($db_engine == "sqlite3") ) ) {
141                                         install_sqlupdate( $version, $file );
142                                 }
143                         }
144                 }
145
146                 // now non sql scripts
147                 $dir = opendir(UPGRADE_DIR."/".$version);
148                 while ($file = readdir($dir)) {
149                         if (($file[0] != ".") && is_file(UPGRADE_DIR."/".$version."/".$file)) {
150                                 if ((strtolower(substr($file,-4)) == ".sql") || (strtolower(substr($file,-7)) == ".sqlite")) {
151                                         // sql scripts were dealt with first
152                                 } else if (strtolower(substr($file,-4)) == ".php") {
153                                         out("-> Running PHP script ".UPGRADE_DIR."/".$version."/".$file);
154                                         if (!$dryrun) {
155                                                 run_included(UPGRADE_DIR."/".$version."/".$file);
156                                         }
157
158                                 } else if (is_executable(UPGRADE_DIR."/".$version."/".$file)) {
159                                         out("-> Executing ".UPGRADE_DIR."/".$version."/".$file);
160                                         if (!$dryrun) {
161                                                 exec(UPGRADE_DIR."/".$version."/".$file);
162                                         }
163                                 } else {
164                                         error("-> Don't know what to do with ".UPGRADE_DIR."/".$version."/".$file);
165                                 }
166                         }
167                 }
168
169         }
170 }
171
172
173 function checkDiff($file1, $file2) {
174         // diff, ignore whitespace and be quiet
175         exec("diff -wq ".escapeshellarg($file2)." ".escapeshellarg($file1), $output, $retVal);
176         return ($retVal != 0);
177 }
178
179 function amp_mkdir($directory, $mode = "0755", $recursive = false) {
180         debug("mkdir ".$directory.", ".$mode);
181         $ntmp = sscanf($mode,"%o",$modenum); //assumes all inputs are octal
182         if (version_compare(phpversion(), 5.0) < 0) {
183                 // php <5 can't recursively create directories
184                 if ($recursive) {
185                         $output = false;
186                         $return_value = false;
187                         exec("mkdir -m ".$mode." -p ".$directory,  $output, $return_value);
188                         return ($return_value == 0);
189                 } else {
190                         return mkdir($directory, $modenum);
191                 }
192         } else {
193                 return mkdir($directory, $modenum, $recursive);
194         }
195 }
196
197 /** Recursively copy a directory
198  */
199 function recursive_copy($dirsourceparent, $dirdest, &$md5sums, $dirsource = "") {
200         global $dryrun;
201         global $check_md5s;
202         global $amp_conf;
203         global $asterisk_conf;
204         global $install_moh;
205         global $make_links;
206
207         // total # files, # actually copied
208         $num_files = $num_copied = 0;
209        
210         if ($dirsource && ($dirsource[0] != "/")) $dirsource = "/".$dirsource;
211        
212         if (is_dir($dirsourceparent.$dirsource)) $dir_handle = opendir($dirsourceparent.$dirsource);
213        
214         /*
215         echo "dirsourceparent: "; var_dump($dirsourceparent);
216         echo "dirsource: "; var_dump($dirsource);
217         echo "dirdest: "; var_dump($dirdest);
218         */
219        
220         while (isset($dir_handle) && ($file = readdir($dir_handle))) {
221                 if (($file!=".") && ($file!="..") && ($file != "CVS") && ($file != ".svn")) {
222                         $source = $dirsourceparent.$dirsource."/".$file;
223                         $destination =  $dirdest.$dirsource."/".$file;
224                        
225                         if ($dirsource == "" && $file == "mohmp3" && !$install_moh) {
226                                 // skip to the next dir
227                                 continue;
228                         }
229
230                        
231                         // configurable in amportal.conf
232                         if (strpos($destination,"htdocs_panel")) {
233                                 $destination=str_replace("/htdocs_panel",trim($amp_conf["FOPWEBROOT"]),$destination);
234                         } else {
235                                 $destination=str_replace("/htdocs",trim($amp_conf["AMPWEBROOT"]),$destination);
236                         }
237                         $destination=str_replace("/htdocs_panel",trim($amp_conf["FOPWEBROOT"]),$destination);
238 //                      $destination=str_replace("/cgi-bin",trim($amp_conf["AMPCGIBIN"]),$destination);
239                         if(strpos($dirsource, 'modules') === false) $destination=str_replace("/bin",trim($amp_conf["AMPBIN"]),$destination);
240                         $destination=str_replace("/sbin",trim($amp_conf["AMPSBIN"]),$destination);
241                        
242                         // the following are configurable in asterisk.conf
243                         $destination=str_replace("/astetc",trim($asterisk_conf["astetcdir"]),$destination);
244                         $destination=str_replace("/mohmp3",trim($asterisk_conf["astvarlibdir"])."/mohmp3",$destination);
245                         $destination=str_replace("/astvarlib",trim($asterisk_conf["astvarlibdir"]),$destination);
246                         if(strpos($dirsource, 'modules') === false) $destination=str_replace("/agi-bin",trim($asterisk_conf["astagidir"]),$destination);
247                         if(strpos($dirsource, 'modules') === false) $destination=str_replace("/sounds",trim($asterisk_conf["astvarlibdir"])."/sounds",$destination);
248
249                         // if this is a directory, ensure destination exists
250                         if (is_dir($source)) {
251                                 if (!file_exists($destination)) {
252                                         if ((!$dryrun) && ($destination != "")) {
253                                                 amp_mkdir($destination, "0750", true);
254                                         }
255                                 }
256                         }
257                        
258                         //var_dump($md5sums);
259                         if (!is_dir($source)) {
260                                 $md5_source = preg_replace("|^/?amp_conf/|", "/", $source);
261
262                                 if ($check_md5s && file_exists($destination) && isset($md5sums[$md5_source]) && (md5_file($destination) != $md5sums[$md5_source])) {
263                                         // double check using diff utility (and ignoring whitespace)
264                                         // This is a somewhat edge case (eg, the file doesn't match
265                                         // it's md5 sum from the previous version, but no substantial
266                                         // changes exist compared to the current version), but it
267                                         // pervents a useless prompt to the user.
268                                         if (checkDiff($source, $destination)) {
269                                                 $overwrite = ask_overwrite($source, $destination);
270                                         } else {
271                                                 debug("NOTE: MD5 for ".$destination." was different, but `diff` did not detect any (non-whitespace) changes: overwriting");
272                                                 $overwrite = true;
273                                         }
274                                 } else {
275                                         $overwrite = true;
276                                 }
277                                
278                                 $num_files++;
279                                 if ($overwrite) {
280                                         debug(($make_links ? "link" : "copy")." ".$source." -> ".$destination);
281                                         if (!$dryrun) {
282                                                 if ($make_links) {
283                                                         // symlink, unlike copy, doesn't overwrite - have to delete first
284                                                         if (is_link($destination) || file_exists($destination)) {
285                                                                 unlink($destination);
286                                                         }
287                                                         symlink($_ENV["PWD"]."/".$source, $destination);
288                                                 } else {
289                                                         copy($source, $destination);
290                                                 }
291                                                 $num_copied++;
292                                         }
293                                 } else {
294                                         debug("not overwriting ".$destination);
295                                 }
296                         } else {
297                                 //echo "recursive_copy($dirsourceparent, $dirdest, $md5sums, $dirsource/$file)";
298                                 list($tmp_num_files, $tmp_num_copied) = recursive_copy($dirsourceparent, $dirdest, $md5sums, $dirsource."/".$file);
299                                 $num_files += $tmp_num_files;
300                                 $num_copied += $tmp_num_copied;
301                         }
302                 }
303         }
304        
305         if (isset($dir_handle)) closedir($dir_handle);
306        
307         return array($num_files, $num_copied);
308 }
309
310 function read_md5_file($filename) {
311         $md5 = array();
312         if (file_exists($filename)) {
313                 foreach (file($filename) as $line) {
314                         if (preg_match("/^([a-f0-9]{32})\s+(.*)$/", $line, $matches)) {
315                                 $md5[ "/".$matches[2] ] = $matches[1];
316                         }
317                 }
318         }
319         return $md5;
320 }
321
322 /** Include a .php file
323  * This is a function just to keep a seperate context
324  */
325 function run_included($file) {
326         global $db;
327         global $amp_conf;
328        
329         include($file);
330 }
331
332 function install_sqlupdate( $version, $file )
333 {
334         global $db;
335         global $dryrun;
336
337         out("-> Running SQL script ".UPGRADE_DIR."/".$version."/".$file);
338         // run sql script
339         $fd = fopen(UPGRADE_DIR."/".$version."/".$file, "r");
340         $data = "";
341         while (!feof($fd)) {
342                 $data .= fread($fd, 1024);
343         }
344         fclose($fd);
345
346         preg_match_all("/((SELECT|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER).*);\s*\n/Us", $data, $matches);
347        
348         foreach ($matches[1] as $sql) {
349                 debug($sql);
350                 if (!$dryrun) {
351                         $result = $db->query($sql);
352                         if(DB::IsError($result)) {     
353                                 fatal($result->getDebugInfo()."\" while running ".$file."\n");
354                         }
355                 }
356         }
357 }
358
359 /********************************************************************************************************************/
360
361 ?>
Note: See TracBrowser for help on using the browser.