Changeset 4429

Show
Ignore:
Timestamp:
07/17/07 22:27:14 (6 years ago)
Author:
p_lindheimer
Message:

add setup_svn.php setup script to svn switch in all of the modules for a release to help development

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • freepbx/branches/2.3/setup_svn.php

    r4417 r4429  
    22<?php 
    33 
    4 require_once ("libfreepbx.install.php"); 
     4/** setup_svn.php 
     5 * 
     6 *  The purpose of this function is to install all the modules associated with a particular FreePBX version 
     7 *  under the same path as install_amp. The setup will be similar to how modules are packaged with the 
     8 *  tarballs when distributed. There are a handful of files that disapear and a few that appear as a 
     9 *  result of this technique, but all of those files are associated with the packaging of distros and 
     10 *  not the actual functioning of FreePBX. 
     11 * 
     12 *  You can not publish modules from a tree like this. However, you can do svn udpates from the top level 
     13 *  and then reinstall, and you can make changes in place and do 'svn ci' commands to get them back into 
     14 *  svn. You will require a 'properly' setup svn pull of the module tree in order to do the final publishing. 
     15 * 
     16 *  however, you can use this mode in conjunction with the --make-links-devel install option to get all of your 
     17 *  modules sym-linked to their final place to make development easier. 
     18 * 
     19 */ 
    520 
    6 # constants 
    7 define("AMP_CONF", "/etc/amportal.conf"); 
    8 define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf"); 
    9 define("UPGRADE_DIR", dirname(__FILE__)."/upgrades"); 
    10 define("MODULE_DIR", dirname(__FILE__)."/amp_conf/htdocs/admin/modules/"); 
     21$MODULE_URL  = "https://amportal.svn.sourceforge.net/svnroot/amportal/modules/branches/2.3"; 
     22$MODULE_PATH = "./amp_conf/htdocs/admin/modules"; 
    1123 
    12 # semi constants 
    13 $webroot  = "/var/www/html/"; 
    14 $fopwebroot = "";       // if blank, will use $webroot/panel 
    15 $ampsbin_dir = "/usr/local/sbin"; // default if not set 
    16 $bin_dir  = "/var/lib/asterisk/bin/"; 
    17 $ampbin_dir = $bin_dir; 
    18 $sbin_dir = "/usr/sbin/"; 
    19 $asterisk_user  = "asteriskuser"; 
    20 $asterisk_pass  = "amp109"; 
     24system("svn switch $MODULE_URL $MODULE_PATH"); 
    2125 
    2226 
    2327/* 
    24 freepbx versions 
    25     '1.10.005', 
    26     '1.10.006', 
    27     '1.10.007beta1', 
    28     '1.10.007beta2', 
    29     '1.10.007', 
    30     '1.10.007a', 
    31     '1.10.008beta1', 
    32     '1.10.008beta2', 
    33     '1.10.008beta3', 
    34     '1.10.008', 
    35     '1.10.009beta1', 
    36     '1.10.009beta2', 
    37     '1.10.009', 
    38     '1.10.010beta1', 
    39     '1.10.010', 
    40     '2.0beta1', 
    41     '2.0beta2', 
    42     '2.0beta3', 
    43     '2.0beta4', 
    44     '2.0beta5', 
    45     '2.0.0', 
    46     '2.0.1', 
    47     '2.1beta1', 
    48     '2.1beta2', 
    49     '2.1beta3', 
    50     '2.1.0', 
    51     '2.1.1', 
    52     '2.1.2', 
    53     '2.1.3', 
    54     '2.2.0beta1', 
    55     '2.2.0beta2', 
    56     '2.2.0beta3', 
    57     '2.2.0rc1', 
    58     '2.2.0rc2', 
    59     '2.2.0rc3', 
    60     '2.2.0', 
    61     '2.2.1', 
    62     '2.2.2', 
    63     '2.2.3', 
    64     '2.3.0beta1', 
     28exec("svn list $MODULE_URL", $modules, $ret); 
     29 
     30if ($ret) { 
     31  echo "ERROR: svn list returned: $ret\n"; 
     32  exit; 
     33} else { 
     34 
     35  foreach ($modules as $module) { 
     36    echo "Checking out $module to $MODULE_PATH/$module\n"; 
     37    system("svn co $MODULE_URL/$module $MODULE_PATH/$module"); 
     38  } 
     39
    6540*/ 
    6641 
    67 /********************************************************************************************************************/ 
    68  
    69 function out($text) { 
    70   echo $text."\n"; 
    71 } 
    72  
    73 function outn($text) { 
    74   echo $text; 
    75 } 
    76  
    77 function error($text) { 
    78   echo "[ERROR] ".$text."\n"; 
    79 } 
    80  
    81 function fatal($text) { 
    82   echo "[FATAL] ".$text."\n"; 
    83   exit(1); 
    84 } 
    85  
    86 function debug($text) { 
    87   global $debug; 
    88    
    89   if ($debug) echo "[DEBUG-preDB] ".$text."\n"; 
    90 } 
    91  
    92 function version_compare_freepbx($version1, $version2) { 
    93   $version1 = str_replace("rc","RC", strtolower($version1)); 
    94   $version2 = str_replace("rc","RC", strtolower($version2)); 
    95   return version_compare($version1, $version2); 
    96 } 
    97  
    98 function showHelp() { 
    99   out("Optional parameters:"); 
    100   out("  --help, -h, -?           Show this help"); 
    101   out("  --dbhost <ip address>    Use a remote database server"); 
    102   out("  --dbname databasename    Use database name specified, instead of 'asterisk'"); 
    103   out("  --username <user>        Use <user> to connect to db and write config"); 
    104   out("  --password <pass>        Use <pass> to connect to db and write config"); 
    105   out("  --fopwebroot <path>      Web path where fop will be installed"); 
    106   out("  --webroot <path>         Web root where freepbx will be installed"); 
    107   out("  --cgibin <path>          Path where cgi-bin's lives"); 
    108   out("  --bin <path>             Path of asterisk binaries"); 
    109   out("  --sbin <path>            Path of system admin binaries"); 
    110   out("  --asteriskuser <user>    Asterisk Manager username"); 
    111   out("  --asteriskpass <pass>    Asterisk Manager password"); 
    112   out("  --systemconfig <path>    System config files"); 
    113   out("  --debug                  Enable debug output"); 
    114   out("  --dry-run                Don't actually do anything"); 
    115   out("  --force-version <ver>    Force upgrade from version <ver>"); 
    116   out("  --skip-module-install    Don't run install scripts for packaged modules, the files are still loaded."); 
    117   out("                           In a development environment you may not want the install scripts run."); 
    118   out("  --no-files               Just run updates without installing files"); 
    119   out("  --install-moh            Install default music-on-hold files (normally doesn't, unless "); 
    120   out("                           it's a new installation)"); 
    121   //out("  --make-links-devel       Make links to files in the source directory instead of copying"); 
    122   //out("                           (intended for developers only)"); 
    123   out("  --my-svn-is-correct      Ignore Asterisk version, assume it is correct"); 
    124   out("  --engine <name>          Use the specified PBX Engine ('asterisk', 'asterisk14' or 'openpbx')"); 
    125 } 
    126  
    127 function install_parse_amportal_conf($filename) { 
    128   $file = file($filename); 
    129   foreach ($file as $line) { 
    130     if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) {  
    131       $conf[ $matches[1] ] = $matches[2]; 
    132     } 
    133   } 
    134  
    135   // use same defaults as function.inc.php 
    136   if ( !isset($conf["AMPDBENGINE"]) || ($conf["AMPDBENGINE"] == "")) { 
    137     $conf["AMPDBENGINE"] = "mysql"; 
    138   } 
    139    
    140   if ( !isset($conf["AMPDBNAME"]) || ($conf["AMPDBNAME"] == "")) { 
    141     $conf["AMPDBNAME"] = "asterisk"; 
    142   } 
    143    
    144   if ( !isset($conf["AMPENGINE"]) || ($conf["AMPENGINE"] == "")) { 
    145     $conf["AMPENGINE"] = "asterisk"; 
    146   } 
    147  
    148   return $conf; 
    149 } 
    150  
    151 function install_parse_asterisk_conf($filename) { 
    152   $file = file($filename); 
    153   foreach ($file as $line) { 
    154     if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) {  
    155       $conf[ $matches[1] ] = $matches[2]; 
    156     } 
    157   } 
    158   // Now set defaults if not set (although asterisk should fail if not set but at least 
    159   // it will get the setup somewhat right 
    160   // 
    161   if (!isset($asterisk_conf['astetcdir']))    { $asterisk_conf['astetcdir']    = "/etc/asterisk"; } 
    162   if (!isset($asterisk_conf['astmoddir']))    { $asterisk_conf['astmoddir']    = "/usr/lib/asterisk/modules"; } 
    163   if (!isset($asterisk_conf['astvarlibdir'])) { $asterisk_conf['astvarlibdir'] = "/var/lib/asterisk"; } 
    164   if (!isset($asterisk_conf['astagidir']))    { $asterisk_conf['astagidir']    = "/var/lib/asterisk/agi-bin"; } 
    165   if (!isset($asterisk_conf['astspooldir']))  { $asterisk_conf['astspooldir']  = "/var/spool/asterisk"; } 
    166   if (!isset($asterisk_conf['astrundir']))    { $asterisk_conf['astrundir']    = "/var/run/asterisk"; } 
    167   if (!isset($asterisk_conf['astlogdir']))    { $asterisk_conf['astlogdir']    = "/var/log/asterisk"; } 
    168  
    169   return $conf; 
    170 } 
    171  
    172 function write_amportal_conf($filename, $conf) { 
    173   $file = file($filename); 
    174   // parse through the file 
    175   foreach (array_keys($file) as $key) { 
    176     if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$file[$key],$matches)) { 
    177       // this is an option=value line 
    178       if (isset($conf[ $matches[1] ])) { 
    179         // rewrite the line, if we have this in $conf 
    180         $file[$key] = $matches[1]."=".$conf[ $matches[1] ]."\n"; 
    181         // unset it so we know what's new 
    182         unset($conf[ $matches[1] ]); 
    183       } 
    184     } 
    185   } 
    186    
    187   // add new entries 
    188   foreach ($conf as $key=>$val) { 
    189     $file[] = $key."=".$val."\n"; 
    190   } 
    191    
    192   // write the file 
    193   if (!$fd = fopen($filename, "w")) { 
    194     fatal("Could not open ".$filename." for writing"); 
    195   } 
    196   fwrite($fd, implode("",$file)); 
    197   fclose($fd); 
    198 } 
    199  
    200 function ask_overwrite($file1, $file2) { 
    201   global $check_md5s; 
    202   do { 
    203     out($file2." has been changed from the original version."); 
    204     outn("Overwrite (y=yes/a=all/n=no/d=diff/s=shell/x=exit)? "); 
    205     $key = fgets(STDIN,1024); 
    206     switch (strtolower($key[0])) { 
    207       case "y": return true;  
    208       case "a": $check_md5s=false; return true; 
    209       case "n": return false; 
    210       case "d":  
    211         out(""); 
    212         // w = ignore whitespace, u = unified 
    213         passthru("diff -wu ".escapeshellarg($file2)." ".escapeshellarg($file1)); 
    214       break; 
    215       case "s": 
    216         if (function_exists("pcntl_fork")) { 
    217           out(""); 
    218           $shell = (isset($_ENV["SHELL"]) ? $_ENV["SHELL"] : "/bin/bash"); 
    219           out("Dropping to shell. Type 'exit' to return"); 
    220           out("-> Original file:  ".$file2); 
    221           out("-> New file:       ".$file1); 
    222            
    223           $pid = pcntl_fork(); 
    224           if ($pid == -1) { 
    225             out("[ERROR] cannot fork"); 
    226           } else if ($pid) { 
    227             // parent 
    228             pcntl_waitpid($pid, $status); 
    229             // we wait till the child exits/dies/whatever 
    230           } else { 
    231             pcntl_exec($shell, array(), $_ENV); 
    232           } 
    233            
    234           out("Returned from shell"); 
    235         } else { 
    236           out("[ERROR] PHP not built with process control (--enable-pcntl) support: cannot spawn shell"); 
    237         } 
    238          
    239       break; 
    240       case "x":  
    241         out("-> Original file:  ".$file2); 
    242         out("-> New file:       ".$file1); 
    243         out("Exiting install program."); 
    244         exit(1); 
    245       break; 
    246     } 
    247     out(""); 
    248   } while(1); 
    249 } 
    250  
    251 /** Write AMP-generated configuration files 
    252  */ 
    253 function generate_configs() { 
    254   global $amp_conf; 
    255   global $dryrun; 
    256   global $debug; 
    257    
    258   out("Generating Configurations.conf, (if Asterisk is not running, you will get an error"); 
    259   out("In case of error, start Asterisk and hit the red bar in the GUI to generate the Configuraions.conf files"); 
    260   if (!$dryrun) 
    261     // added --run-install to make it work like it has been working since retrieve_conf changed to not run module install scripts by default 
    262  
    263     // 
    264     // TODO: Should check if Asterisk is running and/or try to start it. 
    265     // 
    266     passthru("su - asterisk -c \"".trim($amp_conf["AMPBIN"])."/retrieve_conf --run-install ".($debug ? ' --debug' : '').'"'); 
    267 } 
    268  
    269  
    270 /** Set reload flag for AMP admin 
    271  */ 
    272 function install_needreload() { 
    273   global $db; 
    274   $sql = "UPDATE admin SET value = 'true' WHERE variable = 'need_reload'";  
    275   $result = $db->query($sql);  
    276   if(DB::IsError($result)) {      
    277     die($result->getMessage());  
    278   } 
    279 } 
    280  
    281  
    282 /** Collect AMP settings 
    283  */ 
    284 function collect_settings($filename, $dbhost = '', $dbuser = '', $dbpass = '', $dbname = 'asterisk') { 
    285   global $webroot; 
    286   global $fopwebroot; 
    287   global $ampsbin_dir; 
    288   global $bin_dir; 
    289   global $ampbin_dir; 
    290   global $sbin_dir; 
    291   global $asterisk_user; 
    292   global $asterisk_pass; 
    293  
    294   out("Creating new $filename"); 
    295    
    296   outn("Enter your USERNAME to connect to the '$dbname' database:\n [".($dbuser ? $dbuser : $asterisk_user) . "] "); 
    297   $key = trim(fgets(STDIN,1024)); 
    298   if (preg_match('/^$/',$key))  
    299     $amp_conf["AMPDBUSER"] = ($dbuser ? $dbuser : "asteriskuser"); 
    300   else  
    301     $amp_conf["AMPDBUSER"] = $key; 
    302    
    303   outn("Enter your PASSWORD to connect to the '$dbname' database:\n [".($dbpass ? $dbpass : $asterisk_pass)."] "); 
    304   $key = trim(fgets(STDIN,1024)); 
    305   if (preg_match('/^$/',$key))  
    306     $amp_conf["AMPDBPASS"] = ($dbpass ? $dbpass : $asterisk_pas); 
    307   else  
    308     $amp_conf["AMPDBPASS"] = $key; 
    309    
    310   outn("Enter the hostname of the '$dbname' database:\n [".($dbhost ? $dbhost : "localhost")."] "); 
    311   $key = trim(fgets(STDIN,1024)); 
    312   if (preg_match('/^$/',$key))  
    313     $amp_conf["AMPDBHOST"] = ($dbhost ? $dbhost : "localhost"); 
    314   else  
    315     $amp_conf["AMPDBHOST"] = $key; 
    316    
    317   outn("Enter a USERNAME to connect to the Asterisk Manager interface:\n [admin] "); 
    318   $key = trim(fgets(STDIN,1024)); 
    319   if (preg_match('/^$/',$key)) $amp_conf["AMPMGRUSER"] = "admin"; 
    320   else $amp_conf["AMPMGRUSER"] = $key; 
    321    
    322   outn("Enter a PASSWORD to connect to the Asterisk Manager interface:\n [amp111] "); 
    323   $key = trim(fgets(STDIN,1024)); 
    324   if (preg_match('/^$/',$key)) $amp_conf["AMPMGRPASS"] = "amp111"; 
    325   else $amp_conf["AMPMGRPASS"] = $key; 
    326    
    327   do { 
    328     out("Enter the path to use for your AMP web root:\n [$webroot] "); 
    329     $key = trim(fgets(STDIN,1024)); 
    330     if (preg_match('/^$/',$key))  
    331       $amp_conf["AMPWEBROOT"] = "$webroot"; 
    332     else  
    333       $amp_conf["AMPWEBROOT"] = rtrim($key,'/'); 
    334  
    335     if (is_dir($amp_conf["AMPWEBROOT"])) { 
    336       break; 
    337     } else if (amp_mkdir($amp_conf["AMPWEBROOT"],"0755",true)){ 
    338       out("Created ".$amp_conf["AMPWEBROOT"]); 
    339       break; 
    340     } else { 
    341       fatal("Cannot create ".$amp_conf["AMPWEBROOT"]."!"); 
    342     } 
    343   } while(1); 
    344    
    345   // Really no need to ask, is there. 
    346   if (empty($fopwebroot)) 
    347     $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel"; 
    348   else 
    349     $amp_conf["FOPWEBROOT"] = $fopwebroot; 
    350    
    351   outn("Enter the IP ADDRESS or hostname used to access the AMP web-admin:\n [xx.xx.xx.xx] "); 
    352   $key = trim(fgets(STDIN,1024)); 
    353   if (preg_match('/^$/',$key)) $amp_conf["AMPWEBADDRESS"] = "xx.xx.xx.xx"; 
    354   else $amp_conf["AMPWEBADDRESS"] = $key; 
    355    
    356   outn("Enter a PASSWORD to perform call transfers with the Flash Operator Panel:\n [passw0rd] "); 
    357   $key = trim(fgets(STDIN,1024)); 
    358   if (preg_match('/^$/',$key)) $amp_conf["FOPPASSWORD"] = "passw0rd"; 
    359   else $amp_conf["FOPPASSWORD"] = $key; 
    360    
    361   outn("Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?\n [extensions] "); 
    362   $key = trim(fgets(STDIN,1024)); 
    363   if (preg_match('/^$/',$key)) $amp_conf["AMPEXTENSIONS"] = "extensions"; 
    364   else $amp_conf["AMPEXTENSIONS"] = $key; 
    365    
    366   do { 
    367     out("Enter directory in which to store AMP executable scripts:\n [$ampbin_dir] "); 
    368     $key = trim(fgets(STDIN,1024)); 
    369     if (preg_match('/^$/',$key))  
    370       $amp_conf["AMPBIN"] = $ampbin_dir; 
    371     else  
    372       $amp_conf["AMPBIN"] = rtrim($key,'/'); 
    373  
    374     if (is_dir($amp_conf["AMPBIN"])) { 
    375       break; 
    376     } else if (amp_mkdir($amp_conf["AMPBIN"],"0755",true)){ 
    377       out("Created ".$amp_conf["AMPBIN"]); 
    378       break; 
    379     } else { 
    380       fatal("Cannot create ".$amp_conf["AMPBIN"]."!"); 
    381     } 
    382   } while(1); 
    383    
    384   do { 
    385     out("Enter directory in which to store super-user scripts:\n [$ampsbin_dir] "); 
    386     $key = trim(fgets(STDIN,1024)); 
    387     if (preg_match('/^$/',$key))  
    388       $amp_conf["AMPSBIN"] = "$ampsbin_dir"; 
    389     else  
    390       $amp_conf["AMPSBIN"] = rtrim($key,'/'); 
    391  
    392     if (is_dir($amp_conf["AMPSBIN"])) { 
    393       break; 
    394     } else if (amp_mkdir($amp_conf["AMPSBIN"],"0755",true)){ 
    395       out("Created ".$amp_conf["AMPSBIN"]); 
    396       break; 
    397     } else { 
    398       fatal("Cannot create ".$amp_conf["AMPSBIN"]."!"); 
    399     } 
    400   } while(1); 
    401    
    402   // write amportal.conf 
    403   write_amportal_conf($filename, $amp_conf); 
    404   outn(AMP_CONF." written"); 
    405 } 
    406  
    407 /** Set base of packaged modules to the versions packaged in the tarball since they are 
    408  *  getting overwritten with the tarball from anything that may have been updated online. 
    409  * 
    410  */ 
    411 function set_base_version() { 
    412   global $dryrun; 
    413  
    414   // read modules list from MODULE_DIR 
    415   // 
    416   $included_modules = array(); 
    417   $dir = opendir(MODULE_DIR); 
    418   while ($file = readdir($dir)) { 
    419     if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) { 
    420       $included_modules[] = $file; 
    421     } 
    422   } 
    423   closedir($dir); 
    424  
    425   foreach ($included_modules as $up_module) { 
    426     outn("Checking $up_module.. "); 
    427     if (!$dryrun) { 
    428       out(set_module_version($up_module)); 
    429     } else { 
    430       out("Dry Run Not Updated"); 
    431     } 
    432   } 
    433 } 
    434  
    435 /** Install all modules packaged with the install. We use the force flag because the 
    436  *  the assumption is that the dependencies are met and the package is able to have 
    437  *  the modules installed. 
    438  */ 
    439 function install_modules() { 
    440   global $dryrun; 
    441   global $amp_conf; 
    442  
    443   // read modules list from MODULE_DIR 
    444   // 
    445   $included_modules = array(); 
    446   $dir = opendir(MODULE_DIR); 
    447   while ($file = readdir($dir)) { 
    448     if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) { 
    449       $included_modules[] = $file; 
    450     } 
    451   } 
    452   closedir($dir); 
    453  
    454   foreach ($included_modules as $up_module) { 
    455     outn("Checking $up_module.. "); 
    456     if (!$dryrun) { 
    457       // special case framework, it should not be installed just enabled. 
    458       // 
    459       if ($up_module != 'framework') { 
    460         system($amp_conf['AMPBIN']."/module_admin -f install $up_module"); 
    461         out("installed"); 
    462       } else { 
    463         system($amp_conf['AMPBIN']."/module_admin -f enable $up_module"); 
    464         out("enabled"); 
    465       } 
    466     } else { 
    467       out("Dry Run Not Installed"); 
    468     } 
    469   } 
    470 } 
    471  
    472 /** Set the module version number to the packaged version and enable 
    473  *  module must require not install.php or install.sql script 
    474  *  this is primarily to package core and framework with freepbx tarballs 
    475  * 
    476  */ 
    477 function set_module_version($module) { 
    478   global $db; 
    479  
    480   $module_dir = MODULE_DIR; 
    481   $file_path = $module_dir.$module."/module.xml"; 
    482   if (file_exists($file_path)) { 
    483     // TODO: this is bad, there are other version tags (depends on) but this 
    484     //       is equivalnet to what publish.pl does, so it expects this to be 
    485     //       at the top. 
    486     // 
    487     $module_xml = file_get_contents($file_path); 
    488     if (preg_match('/<version>(.+)<\/version>/', $module_xml, $matches)) { 
    489       $version = $matches[1]; 
    490     } else { 
    491       fatal("ERROR: $file_path found but no verison information"); 
    492     } 
    493   } else { 
    494     return  "not packaged, no updating needed"; 
    495   } 
    496  
    497   // If we didn't return above, then we found the package as part of the install 
    498   // tarball and want to update the version info since this might be overwriting 
    499   // and existing install that has a newever version. 
    500   // 
    501   $sql = "SELECT version FROM modules WHERE modulename = '$module'"; 
    502   $result = $db->getCol($sql);  
    503   if(DB::IsError($result)) {      
    504     fatal("error accessing version table: ".$result->getMessage());  
    505   } 
    506   $sql = ""; 
    507   if (count($result) == 0) { 
    508     $sql = "INSERT INTO modules (modulename, version, enabled) VALUES ('$module', '$version', 1)"; 
    509   } else if ($result[0] != $version) { 
    510     $sql = "UPDATE modules SET version = '$version', enabled = 1 WHERE modulename = '$module'"; 
    511   } 
    512   if ($sql) { 
    513     debug($sql); 
    514     $result = $db->query($sql);  
    515     if(DB::IsError($result)) {      
    516       fatal("error writing to version table: ".$result->getMessage());  
    517     } 
    518     return "updated to $version"; 
    519   } else { 
    520     return "already at $version"; 
    521   } 
    522 } 
    523  
    524 /********************************************************************************************************************/ 
    525  
    526 // **** Make sure we have STDIN etc 
    527  
    528 // from  ben-php dot net at efros dot com   at  php.net/install.unix.commandline 
    529 if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) { 
    530   define('STDIN',fopen("php://stdin","r")); 
    531   define('STDOUT',fopen("php://stdout","r")); 
    532   define('STDERR',fopen("php://stderr","r")); 
    533   register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) ); 
    534 } 
    535     
    536 // **** Make sure we have PEAR's DB.php, and include it 
    537  
    538 outn("Checking for PEAR DB.."); 
    539 if (! @ include('DB.php')) { 
    540   out("FAILED"); 
    541   fatal("PEAR must be installed (requires DB.php). Include path: ".ini_get("include_path")); 
    542 } 
    543 out("OK"); 
    544  
    545 // **** Make sure we have PEAR's GetOpts.php, and include it 
    546  
    547 outn("Checking for PEAR Console::Getopt.."); 
    548 if (! @ include("Console/Getopt.php")) { 
    549   out("FAILED"); 
    550   fatal("PEAR must be installed (requires Console/Getopt.php). Include path: ".ini_get("include_path")); 
    551 } 
    552 out("OK"); 
    553  
    554 // **** Parse out command-line options 
    555 $shortopts = "h?u:p:"; 
    556 $longopts = array("help","debug","dry-run","username=","password=","force-version=","dbhost=","no-files","dbname=","my-svn-is-correct","engine=","install-moh","make-links-devel","skip-module-install"); 
    557  
    558 $args = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), $shortopts, $longopts); 
    559 if (is_object($args)) { 
    560   // assume it's PEAR_ERROR 
    561   out($args->message); 
    562   exit(255); 
    563 } 
    564  
    565 $debug = false; 
    566 $dryrun = false; 
    567 $install_files = true; 
    568 $override_astvers = false; 
    569  
    570 $install_moh = false; 
    571 $make_links = false; 
    572 $module_install = true; 
    573  
    574 //initialize variables to avoid php notices 
    575 $dbhost = null;  
    576 $dbname = null;  
    577 $new_username = null; 
    578 $new_password = null; 
    579  
    580 foreach ($args[0] as $arg) { 
    581   switch ($arg[0]) { 
    582     case "--help": case "h": case "?": 
    583       showHelp(); 
    584       exit(10); 
    585     break; 
    586     case "--dry-run": 
    587       out("Dry-run only, nothing will be changed"); 
    588       $dryrun = true; 
    589     break; 
    590     case "--debug": 
    591       $debug = true; 
    592       debug("Debug mode enabled"); 
    593     break; 
    594     case "--username": case "u": 
    595       out("Using username: ".$arg[1]); 
    596       $new_username = $arg[1]; 
    597     break; 
    598     case "--password": case "p": 
    599       out("Using password: ".str_repeat("*",strlen($arg[1]))); 
    600       $new_password = $arg[1]; 
    601     break; 
    602     case "--force-version": 
    603       $version = $arg[1]; 
    604       out("Forcing upgrade from version ".$version); 
    605     break; 
    606     case "--dbhost": 
    607       $dbhost = $arg[1]; 
    608       out("Using remote database server at ".$dbhost); 
    609     break; 
    610     case "--dbname": 
    611       $dbname = $arg[1]; 
    612       out("Using database ".$dbname); 
    613     break; 
    614     case "--no-files": 
    615       $install_files = false; 
    616       out("Running upgrade only, without installing files."); 
    617     break; 
    618     case "--my-svn-is-correct": 
    619       $override_astvers = true; 
    620     break; 
    621     case "--engine": 
    622       if ($arg[1] != 'asterisk' && $arg[1] != 'asterisk14' && $arg[1] != 'openpbx') { 
    623         fatal('Currently only "asterisk", "asterisk14" or "openpbx" are supported as a PBX engine'); 
    624       } 
    625       $pbx_engine = $arg[1]; 
    626     break; 
    627     case "--install-moh": 
    628       $install_moh = true; 
    629     break; 
    630     case "--make-links-devel": 
    631       $make_links = true; 
    632     break; 
    633     case "--skip-module-install": 
    634       $module_install = false; 
    635     break; 
    636     case "--fopwebroot": 
    637       $fopwebroot = $arg[1]; 
    638       out("Using fop at ".$fopwebroot); 
    639     break; 
    640     case "--webroot": 
    641       $webroot = $arg[1]; 
    642       out("Using Webroot at ".$webroot); 
    643     break; 
    644     case "--cgibin": 
    645       $cgibin = $arg[1]; 
    646       out("Using CGI-BIN at ".$cgibin); 
    647     break; 
    648     case "--bin": 
    649       $ampbin_dir = $arg[1]; 
    650       out("Using bin at ".$ampbin_dir); 
    651     break; 
    652     case "--sbin": 
    653       $ampsbin_dir = $arg[1]; 
    654       out("Using sbin ar ".$ampsbin_dir); 
    655     break; 
    656     case "--asteriskuser": 
    657       $asterisk_user = $arg[1]; 
    658       out("Using Asterisk user ".$asterisk_user); 
    659     break; 
    660     case "--asteriskpass": 
    661       $asterisk_pass = $arg[1]; 
    662       out("Using asteriskpass ".str_repeat("*",strlen($arg[1]))); 
    663     break; 
    664  
    665 /*    do we need this ? 
    666     case "--systemconfig": 
    667       $systemconfig = $arg[1]; 
    668       out("Using system config at ". $systemconfig); 
    669     break;  
    670 */ 
    671  
    672   } 
    673 } 
    674  
    675  
    676 // **** Look for user = root 
    677  
    678 outn("Checking user.."); 
    679 //$current_user=(isset($_ENV["USER"]) ? $_ENV["USER"] : exec('whoami',$output)); 
    680 $euid = (posix_getpwuid(posix_geteuid())); 
    681 $current_user = $euid['name']; 
    682 if ($current_user != "root"){ 
    683   out("FAILED"); 
    684   fatal($argv[0]." must be run as root"); 
    685 } 
    686 out("OK"); 
    687  
    688  
    689 // **** Check for amportal.conf, create if necessary 
    690  
    691 outn("Checking for ".AMP_CONF.".."); 
    692 if (!file_exists(AMP_CONF)) { 
    693   out(AMP_CONF." does not exist, copying default"); 
    694   copy("amportal.conf", AMP_CONF); 
    695  
    696   // this file contains password and should not be a+r 
    697   // this addresses http://freepbx.org/trac/ticket/1878 
    698   chown(AMP_CONF, "asterisk");  
    699   chgrp(AMP_CONF, "asterisk");  
    700   chmod(AMP_CONF, 0640);  
    701  
    702   collect_settings(AMP_CONF, $dbhost, $new_username, $new_password, 'asterisk'); 
    703  
    704   out("Assuming new install, --install-moh added to command line"); 
    705   $install_moh = true; 
    706 } 
    707 out("OK"); 
    708  
    709 // **** read amportal.conf 
    710  
    711 outn("Reading ".AMP_CONF.".."); 
    712 $amp_conf = install_parse_amportal_conf(AMP_CONF); 
    713 if (count($amp_conf) == 0) { 
    714   fatal("FAILED"); 
    715 } 
    716 out("OK"); 
    717  
    718 // Ensure our "critical" variables are set.  We absolutely need these to copy in files. 
    719  
    720 if (!array_key_exists("AMPWEBROOT",$amp_conf)) { 
    721   out("Adding AMPWEBROOT option to amportal.conf - using AMP default"); 
    722   $amp_conf["AMPWEBROOT"] = "/var/www/html"; 
    723 } 
    724  
    725 if (!array_key_exists("FOPWEBROOT",$amp_conf)) { 
    726   out("Adding FOPWEBROOT option to amportal.conf - using AMP default"); 
    727   $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel"; 
    728 } 
    729  
    730 if (!array_key_exists("AMPBIN",$amp_conf)) { 
    731   out("Adding AMPBIN option to amportal.conf - using AMP default"); 
    732   $amp_conf["AMPBIN"] = "/var/lib/asterisk/bin"; 
    733 } 
    734  
    735 if (!array_key_exists("AMPSBIN",$amp_conf)) { 
    736   out("Adding AMPSBIN option to amportal.conf - using AMP default"); 
    737   $amp_conf["AMPSBIN"] = "/usr/sbin"; 
    738 } 
    739  
    740 if (!array_key_exists("AMPDBENGINE",$amp_conf)) { 
    741   out("Adding AMPDBENGINE option to amportal.conf - using AMP default"); 
    742   $amp_conf["AMPDBENGINE"] = "mysql"; 
    743 } 
    744 if (!array_key_exists("AMPDBNAME",$amp_conf)) { 
    745   out("Adding AMPDBNAME option to amportal.conf - using AMP default"); 
    746   $amp_conf["AMPDBNAME"] = "asterisk"; 
    747 } 
    748  
    749 if (isset($new_username)) { 
    750   $amp_conf["AMPDBUSER"] = $new_username; 
    751 } 
    752  
    753 if (isset($new_password)) { 
    754   $amp_conf["AMPDBPASS"] = $new_password; 
    755 } 
    756  
    757 if (isset($dbhost)) { 
    758   $amp_conf["AMPDBHOST"] = $dbhost; 
    759 } 
    760  
    761 if (isset($dbname)) { 
    762   $amp_conf["AMPDBNAME"] = $dbname; 
    763 } 
    764    
    765 // write amportal.conf 
    766 write_amportal_conf(AMP_CONF, $amp_conf); 
    767  
    768 // **** Check for amportal.conf, create if necessary 
    769  
    770 outn("Checking for ".ASTERISK_CONF.".."); 
    771 if (!file_exists(ASTERISK_CONF)) { 
    772   out(ASTERISK_CONF." does not exist, copying default"); 
    773   copy("asterisk.conf", ASTERISK_CONF); 
    774 } 
    775 out("OK"); 
    776  
    777 // **** read asterisk.conf 
    778  
    779 outn("Reading ".ASTERISK_CONF.".."); 
    780 $asterisk_conf = install_parse_asterisk_conf(ASTERISK_CONF); 
    781 if (count($asterisk_conf) == 0) { 
    782   fatal("FAILED. Have you installed Asterisk?"); 
    783 } 
    784 out("OK"); 
    785  
    786 /* deprecated on freepbx 2.2, from now pages need to read this information  
    787    from $asterik_conf and not $amp_conf. 
    788     
    789    this code will stay in 2.2, but in 2.3 it will be gone. developers - please 
    790    update your code 
    791  */ 
    792 if (isset($asterisk_conf['astetcdir'])) { $amp_conf['ASTETCDIR'] = $asterisk_conf['astetcdir']; } 
    793 if (isset($asterisk_conf['astmoddir'])) { $amp_conf['ASTMODDIR'] = $asterisk_conf['astmoddir']; } 
    794 if (isset($asterisk_conf['astvarlibdir'])) { $amp_conf['ASTVARLIBDIR'] = $asterisk_conf['astvarlibdir']; } 
    795 if (isset($asterisk_conf['astagidir'])) { $amp_conf['ASTAGIDIR'] = $asterisk_conf['astagidir']; } 
    796 if (isset($asterisk_conf['astspooldir'])) { $amp_conf['ASTSPOOLDIR'] = $asterisk_conf['astspooldir']; } 
    797 if (isset($asterisk_conf['astrundir'])) { $amp_conf['ASTRUNDIR'] = $asterisk_conf['astrundir']; } 
    798 if (isset($asterisk_conf['astlogdir'])) { $amp_conf['ASTLOGDIR'] = $asterisk_conf['astlogdir']; } 
    799  
    800 if (!isset($pbx_engine)) { $pbx_engine='asterisk'; } 
    801 out("Using $pbx_engine as PBX Engine"); 
    802 $amp_conf["AMPENGINE"]=$pbx_engine; 
    803  
    804 write_amportal_conf(AMP_CONF, $amp_conf); 
    805  
    806  
    807 // **** Write asterisk version to ASTETCDIR/version 
    808  
    809 $tmpoutput = ''; 
    810 $tmpout = exec("asterisk -V", $tmpoutput, $exitcode); 
    811 if ($exitcode != 0) { 
    812   fatal("Error executing asterisk: be sure Asterisk is installed and in the path"); 
    813 } 
    814 if (!$fd = fopen($amp_conf['ASTETCDIR'].'/version','w')) { 
    815   fatal('Cannot open '.$amp_conf['ASTETCDIR'].'/version for writing'); 
    816 } 
    817 fwrite($fd, $tmpout); 
    818 fclose($fd); 
    819 // change to read-only 
    820 chmod($amp_conf['ASTETCDIR'].'/version',0444); 
    821  
    822  
    823 // normally this would be the contents of ASTETCDIR/version, but this is for simplicity, as we just read it above 
    824 $verinfo = $tmpout; 
    825  
    826 // **** Check asterisk verison 
    827 //  Set the 'engine' to be 'asterisk14' if using asterisk 1.4, otherwise 
    828 //  'asterisk' 
    829 outn("Checking for Asterisk version.."); 
    830 if ((preg_match('/^Asterisk (\d+(\.\d+)*)(-?(.*))$/', $verinfo, $matches)) || 
    831     (preg_match('/^Asterisk SVN-(\d+(\.\d+)*)(-?(.*))$/', $verinfo, $matches))) { 
    832  
    833   if (version_compare($matches[1], "1.2") < 0) { 
    834     fatal("Asterisk 1.2 or 1.4 is required for this version of FreePBX. Detected version is: ".$matches[1]); 
    835   } 
    836   if (version_compare($matches[1], "1.5", "ge")) { 
    837     fatal("Asterisk 1.2 or 1.4 is required for this version of FreePBX. Detected version is: ".$matches[1]); 
    838   } 
    839 } elseif (preg_match('/^Asterisk SVN.+/', $verinfo)) { 
    840   out("FAIL"); 
    841   out("*** WARNING ***"); 
    842   out("You are not using a released version of Asterisk. We are unable to verify"); 
    843   out("that your Asterisk version is compatible with FreePBX. Whilst this probably"); 
    844   out("won't cause any problems, YOU NEED TO BE CERTAIN that it is compatible"); 
    845   out("with at least the released Asterisk version 1.2" ); 
    846   if ($override_astvers==false) { 
    847     out("If you are SURE that this is compatible, you can re-run ".$argv[0]." with"); 
    848     out("the parameter --my-svn-is-correct"); 
    849     exit; 
    850   } else { 
    851     out("--my-svn-is-correct specified, continuing"); 
    852   } 
    853 } else { 
    854   fatal("Could not determine asterisk version (got: \"".$verinfo."\" please report this)"); 
    855 } 
    856  
    857 // **** Make sure selinux isn't enabled 
    858  
    859 outn("Checking for selinux.."); 
    860 $tmpoutput = ''; 
    861 $tmpout = exec("selinuxenabled 2>&1", $tmpoutput, $sereturn); 
    862 if ($sereturn == 0) { 
    863   fatal("selinux is ENABLED. This is not supported. Please disable selinux before using FreePBX"); 
    864 } 
    865 out("OK"); 
    866  
    867 // **** Connect to database 
    868  
    869 outn("Connecting to database.."); 
    870  
    871 $db_user = $amp_conf["AMPDBUSER"]; 
    872 $db_pass = $amp_conf["AMPDBPASS"]; 
    873 $db_host = $amp_conf["AMPDBHOST"]; 
    874 $db_engine = $amp_conf["AMPDBENGINE"]; 
    875 $db_name = $amp_conf["AMPDBNAME"]; 
    876  
    877 // we still support older configurations,  and fall back  
    878 // into mysql when no other engine is defined 
    879 if ($db_engine == "") 
    880 { 
    881   $db_engine = "mysql"; 
    882 } 
    883    
    884 switch ($db_engine) 
    885 { 
    886   case "pgsql": 
    887   case "mysql": 
    888     // datasource in in this style: dbengine://username:password@host/database  
    889     if (!function_exists($db_engine.'_connect')) { 
    890       out("FAILED"); 
    891       fatal($db_engine." PHP libraries not installed"); 
    892     } 
    893    
    894     $datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name; 
    895     $db = DB::connect($datasource); // attempt connection 
    896     break; 
    897    
    898   case "sqlite": 
    899     if (! @ include('DB/sqlite.php')) 
    900     { 
    901       out("FAILED"); 
    902       fatal( "Your PHP installation lacks SQLite support" ); 
    903     } 
    904    
    905     if (!isset($amp_conf["AMPDBFILE"])) 
    906       die("You must setup properly AMPDBFILE in ".AMP_CONF); 
    907    
    908     if (isset($amp_conf["AMPDBFILE"]) == "") 
    909       die("AMPDBFILE in ".AMP_CONF." cannot be blank"); 
    910    
    911     $DSN = array ( 
    912       "database" => $amp_conf["AMPDBFILE"], 
    913       "mode" => 0666 
    914     ); 
    915    
    916     $db = new DB_sqlite(); 
    917     $db->connect( $DSN ); 
    918     break; 
    919    
    920   case "sqlite3": 
    921     if (!isset($amp_conf["AMPDBFILE"])) 
    922       die("You must setup properly AMPDBFILE in /etc/amportal.conf"); 
    923        
    924     if (isset($amp_conf["AMPDBFILE"]) == "") 
    925       die("AMPDBFILE in /etc/amportal.conf cannot be blank"); 
    926  
    927     require_once('DB/sqlite3.php'); 
    928     $datasource = "sqlite3:///" . $amp_conf["AMPDBFILE"] . "?mode=0666"; 
    929     $db = DB::connect($datasource); 
    930     break; 
    931  
    932   default: 
    933     die( "Unknown SQL engine: [$db_engine]"); 
    934 } 
    935  
    936 if(DB::isError($db)) { 
    937   out("FAILED"); 
    938   debug($db->userinfo); 
    939   out("Try running ".$argv[0]." --username=user --password=pass  (using your own user and pass)"); 
    940   fatal("Cannot connect to database"); 
    941    
    942 } 
    943 out("OK"); 
    944  
    945  
    946 // **** Read DB for version info 
    947  
    948 if (!isset($version)) { 
    949   outn("Checking current version of AMP.."); 
    950   $version = install_getversion(); 
    951   if (!$version) { 
    952     out("no version information"); 
    953     out("Assuming new installation"); 
    954   } else { 
    955     out($version); 
    956   } 
    957 } 
    958  
    959  
    960 // **** Copy files 
    961  
    962 if ($install_files) 
    963 { 
    964   outn("Installing new FreePBX files.."); 
    965   $check_md5s=true; 
    966   $md5sums = read_md5_file(UPGRADE_DIR."/".$version.".md5"); 
    967   list($num_files, $num_copied) = recursive_copy("amp_conf", "", $md5sums); 
    968   if (!is_file("/etc/asterisk/voicemail.conf")) copy("/etc/asterisk/voicemail.conf.template","/etc/asterisk/voicemail.conf"); 
    969   if (!is_dir("/var/spool/asterisk/voicemail/device")) amp_mkdir("/var/spool/asterisk/voicemail/device", "0755", true); 
    970   out("OK (".$num_copied." files copied, ".($num_files-$num_copied)." skipped)"); 
    971 } 
    972  
    973 // **** Apply amportal.conf configuration to files 
    974 debug("Running ".dirname(__FILE__)."/apply_conf.sh"); 
    975 outn("Configuring install for your environment.."); 
    976 if (!$dryrun) { 
    977   if (file_exists($amp_conf["AMPSBIN"]."/amportal")) { 
    978     exec("chmod u+x ".$amp_conf["AMPSBIN"]."/amportal"); 
    979     outn("amportal.."); 
    980   } else { 
    981     outn("no amportal.."); 
    982   } 
    983   if (file_exists($amp_conf["AMPBIN"]."/freepbx_engine")) { 
    984     exec("chmod u+x ".$amp_conf["AMPBIN"]."/freepbx_engine"); 
    985     outn("freepbx_engine.."); 
    986   } else { 
    987     outn("no freepbx_engine.."); 
    988   } 
    989   // edit conf file passwords and then 
    990   // reload manager in asterisk if it is running: 
    991   // 
    992   exec(dirname(__FILE__)."/apply_conf.sh"); 
    993   system("asterisk -rx 'reload manager'"); 
    994 } 
    995 out("OK"); 
    996  
    997 // **** Create spool directories for monitor and fax 
    998 if (!is_dir($asterisk_conf["astspooldir"]."/monitor")) 
    999   amp_mkdir($asterisk_conf["astspooldir"]."/monitor","0766",true); 
    1000 if (!is_dir($asterisk_conf["astspooldir"]."/fax")) 
    1001   amp_mkdir($asterisk_conf["astspooldir"]."/fax","0766",true); 
    1002  
    1003  
    1004 // **** Set permissions all files 
    1005  
    1006 if ($install_files) 
    1007 { 
    1008   outn("Setting permissions on files.."); 
    1009   if (!$dryrun) { 
    1010     exec($amp_conf["AMPSBIN"]."/amportal chown"); 
    1011   } 
    1012   out("OK"); 
    1013 } 
    1014  
    1015 // Run through all the upgrade scripts starting at the specified version 
    1016 // 
    1017 upgrade_all($version); 
    1018  
    1019 // Now force an install for all modules that are packaged with the tarball 
    1020 // directory. 
    1021 // 
    1022 if ($module_install) { 
    1023   install_modules(); 
    1024 } else { 
    1025   out("bypassing packaged module installation because --skip-module-install flag"); 
    1026 } 
    1027  
    1028  
    1029 // **** Generate AMP configs 
    1030 out("Generating AMP configs.."); 
    1031 generate_configs(); 
    1032 out("Generating AMP configs..OK"); 
    1033  
    1034 // **** Bounce FOP 
    1035 outn("Restarting Flash Operator Panel.."); 
    1036 exec('su - asterisk -c "'.$amp_conf["AMPWEBROOT"].'/admin/bounce_op.sh"'); 
    1037 out("OK"); 
    1038  
    1039 $version = install_getversion(); 
    1040 $filename = $amp_conf["AMPWEBROOT"]."/admin/version.txt"; 
    1041 if (!$fd = fopen($filename, "w")) { 
    1042   fatal("Could not open ".$filename." for writing"); 
    1043 } 
    1044 fwrite($fd, $version); 
    1045 fclose($fd); 
    1046  
    1047  
    1048  
    1049 // **** Set reload flag for AMP admin 
    1050 install_needreload(); 
    1051  
    1052 if ($amp_conf["AMPWEBADDRESS"]) 
    1053 { 
    1054   out("Please update your modules and reload Asterisk by visiting http://".$amp_conf["AMPWEBADDRESS"]."/admin"); 
    1055 } 
    1056 else 
    1057 { 
    1058   out("Please update your modules and reload Asterisk by browsing to your server."); 
    1059 } 
    1060  
    1061 out(""); 
    1062 out("*************************************************************************"); 
    1063 out("* Note: It's possible that if you click the red 'Update Now' bar BEFORE *"); 
    1064 out("* updating your modules, your machine will start dropping calls. Ensure *"); 
    1065 out("* that all modules are up to date BEFORE YOU CLICK THE RED BAR. As long *"); 
    1066 out("* as this is observed, your machine will be fully functional whilst the *"); 
    1067 out("* upgrade is in progress.                                               *"); 
    1068 out("*************************************************************************"); 
    106942?>