root/freepbx/trunk/amp_conf/bin/retrieve_conf

Revision 2090, 8.9 kB (checked in by qldrob, 7 years ago)

Fix hardcoded executable paths, use /usr/bin/env, as per #759

  • 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/env php
2 <?php
3
4 define("AMP_CONF", "/etc/amportal.conf");
5 define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf");
6
7 function out($text) {
8   echo $text."\n";
9 }
10
11 function outn($text) {
12   echo $text;
13 }
14
15 function error($text) {
16   echo "[ERROR] ".$text."\n";
17 }
18
19 function fatal($text) {
20   echo "[FATAL] ".$text."\n";
21   exit(1);
22 }
23
24 function debug($text) {
25   global $debug;
26  
27   if ($debug) echo "[DEBUG] ".$text."\n";
28 }
29
30 function showHelp() {
31   out("Optional parameters:");
32   out("  --help, -h, -?           Show this help");
33   out("  --debug                  Enable debug output");
34   out("  --dry-run                Don't actually do anything");
35 }
36
37
38 function parse_amportal_conf2($filename) {
39   $file = file($filename);
40   foreach ($file as $line) {
41     if (preg_match("/^\s*([\w]+)\s*=\s*\"?([\w\/\:\.\%-]*)\"?\s*([;#].*)?/",$line,$matches)) {
42       $conf[ $matches[1] ] = $matches[2];
43     }
44   }
45   return $conf;
46 }
47
48 function parse_asterisk_conf2($filename) {
49   $file = file($filename);
50   foreach ($file as $line) {
51     if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) {
52       $conf[ $matches[1] ] = $matches[2];
53     }
54   }
55   return $conf;
56 }
57
58
59 /********************************************************************************************************************/
60
61 // **** Make sure we have STDIN etc
62
63 // from  ben-php dot net at efros dot com   at  php.net/install.unix.commandline
64 if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) {
65   define('STDIN',fopen("php://stdin","r"));
66   define('STDOUT',fopen("php://stdout","r"));
67   define('STDERR',fopen("php://stderr","r"));
68   register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) );
69 }
70    
71 // **** Make sure we have PEAR's DB.php, and include it
72
73 outn("Checking for PEAR DB..");
74 if (! @ include('DB.php')) {
75   out("FAILED");
76   fatal("PEAR must be installed (requires DB.php). Include path: ".ini_get("include_path"));
77 }
78 out("OK");
79
80
81 // **** Make sure we have PEAR's GetOpts.php, and include it
82
83 outn("Checking for PEAR Console::Getopt..");
84 if (! @ include("Console/Getopt.php")) {
85   out("FAILED");
86   fatal("PEAR must be installed (requires Console/Getopt.php). Include path: ".ini_get("include_path"));
87 }
88 out("OK");
89
90
91 // **** Parse out command-line options
92
93 $shortopts = "h?u:p:";
94 $longopts = array("help","debug","dry-run");
95
96 $args = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), $shortopts, $longopts);
97 if (is_object($args)) {
98   // assume it's PEAR_ERROR
99   out($args->message);
100   exit(255);
101 }
102
103 $debug = false;
104 $dryrun = false;
105
106 foreach ($args[0] as $arg) {
107   switch ($arg[0]) {
108     case "--help": case "h": case "?":
109       showHelp();
110       exit(10);
111     break;
112     case "--dry-run":
113       out("Dry-run only, no files will be written");
114       $dryrun = true;
115     break;
116     case "--debug":
117       $debug = true;
118       debug("Debug mode enabled");
119     break;
120   }
121 }
122
123 // **** Check for amportal.conf
124
125 outn("Checking for ".AMP_CONF."..");
126 if (!file_exists(AMP_CONF)) {
127   fatal("Does not exist, or is inaccessible");
128 }
129 out("OK");
130
131 // **** read amportal.conf
132
133 outn("Reading ".AMP_CONF."..");
134 $amp_conf = parse_amportal_conf2(AMP_CONF);
135 if (count($amp_conf) == 0) {
136   fatal("FAILED");
137 }
138 out("OK");
139
140 outn("Reading ".ASTERISK_CONF."..");
141 $asterisk_conf = parse_asterisk_conf2(ASTERISK_CONF);
142 if (count($asterisk_conf) == 0) {
143   fatal("FAILED");
144 }
145 out("OK");
146
147 // **** Connect to database
148
149 outn("Connecting to database..");
150
151 # the engine to be used for the SQL queries,
152 # if none supplied, backfall to mysql
153 $db_engine = "mysql";
154 if (isset($amp_conf["AMPDBENGINE"])){
155   $db_engine = $amp_conf["AMPDBENGINE"];
156 }
157
158 switch ($db_engine)
159 {
160   case "pgsql":
161   case "mysql":
162     /* datasource in in this style:
163     dbengine://username:password@host/database */
164  
165     $db_user = $amp_conf["AMPDBUSER"];
166     $db_pass = $amp_conf["AMPDBPASS"];
167     $db_host = $amp_conf["AMPDBHOST"];
168     $db_name = $amp_conf["AMPDBNAME"];
169  
170     $datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name;
171     $db = DB::connect($datasource); // attempt connection
172     break;
173  
174   case "sqlite":
175     require_once('DB/sqlite.php');
176  
177     if (!isset($amp_conf["AMPDBFILE"]))
178       die("You must setup properly AMPDBFILE in /etc/amportal.conf");
179  
180     if (isset($amp_conf["AMPDBFILE"]) == "")
181       die("AMPDBFILE in /etc/amportal.conf cannot be blank");
182  
183     $DSN = array (
184       "database" => $amp_conf["AMPDBFILE"],
185       "mode" => 0666
186     );
187  
188     $db = new DB_sqlite();
189     $db->connect( $DSN );
190     break;
191  
192   default:
193     die( "Unknown SQL engine: [$db_engine]");
194 }
195
196 if(DB::isError($db)) {
197   out("FAILED");
198   debug($db->userinfo);
199   fatal("Cannot connect to database");
200  
201 }
202 out("OK");
203
204 //include common functions
205 require_once($amp_conf['AMPWEBROOT']."/admin/extensions.class.php");
206 require_once($amp_conf['AMPWEBROOT']."/admin/functions.inc.php");
207
208 // query for our modules
209 $modules = find_allmodules();
210
211 //Putting the core module last, to move outbound-allroutes
212 // last in from-internals-additional
213 if (array_key_exists('core', $modules)) {
214         $core_tmp = $modules['core'];
215         unset($modules['core']);
216         $modules['core'] = $core_tmp;
217 }
218
219 // include any module global functions
220 if(is_array($modules)){
221   foreach($modules as $key => $module) {
222     //only use this module if it's enabled (status=2)
223     if (isset($module['status']) && $module['status'] == 2) {
224       // Make sure the module is installed and up to date
225       runModuleSql($key, 'install');
226       // active_modules array used in genConf function
227       $active_modules[] = $key;
228       //include module functions
229       if (is_file($amp_conf['AMPWEBROOT']."/admin/modules/{$key}/functions.inc.php")) {
230         require_once($amp_conf['AMPWEBROOT']."/admin/modules/{$key}/functions.inc.php");
231       }
232     }
233   }
234 }
235
236 // create an object of the extensions class
237 require_once($amp_conf['AMPWEBROOT']."/admin/extensions.class.php");
238 $ext = new extensions;
239
240 // create objects for any module classes
241 // currently only 1 class can be declared per module, not sure if that will be an issue
242 if(isset($active_modules) && is_array($active_modules)){
243   foreach($active_modules as $active_module) {
244     $classname = $active_module."_conf";
245     if(class_exists($classname)) {
246       ${$classname} = new $classname;
247     }
248   }
249 }
250
251
252 // run all of the *_get_config functions, which will populate the appropriate objects
253 $engine = "asterisk";
254 if(isset($active_modules) && is_array($active_modules)){
255   foreach($active_modules as $module) {
256     $funcname = $module."_get_config";
257     if (function_exists($funcname)) {
258       $funcname($engine);
259     }
260   }
261 }
262
263 // now run all of the *_hookGet_config functions, which allows the generated dialplan to be modified (ie: $ext->splice())
264 $engine = "asterisk";
265 if(isset($active_modules) && is_array($active_modules)){
266   foreach($active_modules as $module) {
267     $funcname = $module."_hookGet_config";
268     if (function_exists($funcname)) {
269       $funcname($engine);
270     }
271   }
272 }
273
274 // extensions_additional.conf
275 // create the from-internal-additional context so other can add to it
276 $ext->add('from-internal-additional', 'h', '', new ext_hangup(''));
277 //echo $ext->get_filename();
278 //echo $ext->generateConf();
279 write_file($ext->get_filename(),$ext->generateConf());
280
281 // now we write out our conf files for modules
282 // check for any objects for each of the active modules
283 // ** conferences is an example of a module that write a conf
284 if(isset($active_modules) && is_array($active_modules)){
285   foreach($active_modules as $active_module) {
286     $classname = $active_module."_conf";
287     if(class_exists($classname) && get_class(${$classname}) !== false) {
288       //echo ${$classname}->get_filename();
289       //echo ${$classname}->generateConf();
290      
291       // if the module returns an array, it wants to write multiple files
292       // ** pinsets is an example of a module that does this
293       if (is_array(${$classname}->get_filename())) {
294         foreach(${$classname}->get_filename() as $modconf) {
295           write_file($modconf,${$classname}->generateConf($modconf));
296         }
297       } else {
298         write_file(${$classname}->get_filename(),${$classname}->generateConf());
299       }
300     }
301 }
302 }
303
304 function write_file($filename,$contents) {
305   global $asterisk_conf;
306   if (isset($filename) && !empty($filename)) {
307     if ($fd = fopen($asterisk_conf['astetcdir'].'/'.$filename, "w")) {
308       fwrite($fd, $contents);
309       fclose($fd);
310     }
311   }
312 }
313
314
315 // run legacy retrieve scripts
316 // TODO these legacy retrieve scripts should be obsoleted, in favor of classes like extensions class
317
318 //script to write op_server.cfg file from mysql
319 $script = $amp_conf['AMPBIN'].'/retrieve_op_conf_from_mysql.pl';
320 exec($script);
321
322 //script to write sip conf file from mysql
323 $script = $amp_conf['AMPBIN'].'/retrieve_sip_conf_from_mysql.pl';
324 exec($script);
325
326 //script to write iax2 conf file from mysql
327 $script = $amp_conf['AMPBIN'].'/retrieve_iax_conf_from_mysql.pl';
328 exec($script);
329
330 //script to write zap conf file from mysql
331 $script = $amp_conf['AMPBIN'].'/retrieve_zap_conf_from_mysql.pl';
332 exec($script);
333
334 //script to write queues conf file from mysql
335 $script = $amp_conf['AMPBIN'].'/retrieve_queues_conf_from_mysql.pl';
336 exec($script);
337  
338 // **** Set reload flag for AMP admin
339 needreload();
340 out("Please Reload Asterisk by visiting http://".$amp_conf["AMPWEBADDRESS"]."/admin");
341 ?>
Note: See TracBrowser for help on using the browser.