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