root/freepbx/branches/2.10/amp_conf/htdocs/admin/bootstrap.php

Revision 13801, 9.8 kB (checked in by p_lindheimer, 1 year ago)

add logging when AMI connection can't be made

Line 
1 <?php
2 /*
3  * Copyright 2010 by Moshe Brevda (mbrevda => gmail ! com)
4  * and Schmooze Com, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *
20  * Bootstrap Settings:
21  *
22  * bootstrap_settings['skip_astman']           - legacy $skip_astman, default false
23  *
24  * bootstrap_settings['astman_config']         - default null, config arguemnt when creating new Astman
25  * bootstrap_settings['astman_options']        - default array(), config options creating new Astman
26  *                                               e.g. array('cachemode' => true), see astman documentation
27  * bootstrap_settings['astman_events']         - default 'off' used when connecting, Astman defaults to 'on'
28  *
29  * bootstrap_settings['freepbx_error_handler'] - false don't set it, true use default, named use what is passed
30  *
31  * bootstrap_settings['freepbx_auth']          - true (default) - authorize, false - bypass authentication
32  *
33  * $restrict_mods: false means include all modules functions.inc.php, true skip all modules
34  *                 array of hashes means each module where there is a hash
35  *                 e.g. $restrict_mods = array('core' => true, 'dashboard' => true)
36  *
37  * Settings that are set by bootstrap to indicate the results of what was setup and not:
38  *
39  * $bootstrap_settings['framework_functions_included'] = true/false;
40  * $bootstrap_settings['amportal_conf_initialized'] = true/false;
41  * $bootstrap_settings['astman_connected'] = false/false;
42  * $bootstrap_settings['function_modules_included'] = true/false true if one or more were included, false if all were skipped;
43  */
44
45 // we should never re-run this file, something is wrong if we do.
46 //
47 //enable error reporting and start benchmarking
48 error_reporting(E_ALL & ~E_STRICT);
49 date_default_timezone_set(@date_default_timezone_get());
50 function microtime_float() { list($usec,$sec) = explode(' ',microtime()); return ((float)$usec+(float)$sec); }
51 $benchmark_starttime = microtime_float();
52
53 global $amp_conf;
54 if (empty($amp_conf['AMPWEBROOT'])) {
55     $amp_conf['AMPWEBROOT'] = dirname(dirname(__FILE__));
56 }
57 $dirname = $amp_conf['AMPWEBROOT'] . '/admin';
58
59
60 if (isset($bootstrap_settings['bootstrapped'])) {
61   freepbx_log(FPBX_LOG_ERROR,"Bootstrap has already been called once, bad code somewhere");
62   return;
63 } else {
64   $bootstrap_settings['bootstrapped'] = true;
65 }
66
67 if (!isset($bootstrap_settings['skip_astman'])) {
68   $bootstrap_settings['skip_astman'] = isset($skip_astman) ? $skip_astman : false;
69 }
70 $bootstrap_settings['astman_config'] = isset($bootstrap_settings['astman_config']) ? $bootstrap_settings['astman_config'] : null;
71 $bootstrap_settings['astman_options'] = isset($bootstrap_settings['astman_options']) && is_array($bootstrap_settings['astman_options']) ? $bootstrap_settings['astman_options'] : array();
72 $bootstrap_settings['astman_events'] = isset($bootstrap_settings['astman_events']) ? $bootstrap_settings['astman_events'] : 'off';
73
74 $bootstrap_settings['freepbx_error_handler'] = isset($bootstrap_settings['freepbx_error_handler']) ? $bootstrap_settings['freepbx_error_handler'] : true;
75 $bootstrap_settings['freepbx_auth'] = isset($bootstrap_settings['freepbx_auth']) ? $bootstrap_settings['freepbx_auth'] : true;
76 $bootstrap_settings['cdrdb'] = isset($bootstrap_settings['cdrdb']) ? $bootstrap_settings['cdrdb'] : false;
77
78 $restrict_mods = isset($restrict_mods) ? $restrict_mods : false;
79
80           
81 // include base functions
82 require_once($dirname . '/libraries/utility.functions.php');
83 $bootstrap_settings['framework_functions_included'] = false;
84 require_once($dirname . '/functions.inc.php');
85 $bootstrap_settings['framework_functions_included'] = true;
86           
87 //now that its been included, use our own error handler as it tends to be much more verbose.
88 if ($bootstrap_settings['freepbx_error_handler']) {
89   $error_handler = $bootstrap_settings['freepbx_error_handler'] === true ? 'freepbx_error_handler' : $bootstrap_settings['freepbx_error_handler'];
90   if (function_exists($error_handler)) {
91     set_error_handler($error_handler, E_ALL & ~E_STRICT);
92   }
93 }
94
95 // bootstrap.php should always be called from freepbx.conf so
96 // database conifguration already included, connect to database:
97 //
98 require_once($dirname . '/libraries/db_connect.php'); //PEAR must be installed
99
100 // get settings
101 $freepbx_conf =& freepbx_conf::create();
102
103 // passing by reference, this means that the $amp_conf available to everyone is the same one as present
104 // within the class, which is probably a direction we want to go to use the class.
105 //
106 $bootstrap_settings['amportal_conf_initialized'] = false;
107 $amp_conf =& $freepbx_conf->parse_amportal_conf("/etc/amportal.conf",$amp_conf);
108 $asterisk_conf =& $freepbx_conf->get_asterisk_conf();
109 $bootstrap_settings['amportal_conf_initialized'] = true;
110
111 //connect to cdrdb if requestes
112 if ($bootstrap_settings['cdrdb']) {
113     $dsn = array(
114         'phptype'  => $amp_conf['CDRDBTYPE'] ? $amp_conf['CDRDBTYPE'] : $amp_conf['AMPDBENGINE'],
115         'hostspec' => $amp_conf['CDRDBHOST'] ? $amp_conf['CDRDBHOST'] : $amp_conf['AMPDBHOST'],
116         'username' => $amp_conf['CDRDBUSER'] ? $amp_conf['CDRDBUSER'] : $amp_conf['AMPDBUSER'],
117         'password' => $amp_conf['CDRDBPASS'] ? $amp_conf['CDRDBPASS'] : $amp_conf['AMPDBPASS'],
118         'port'     => $amp_conf['CDRDBPORT'] ? $amp_conf['CDRDBPORT'] : '3306',
119         //'socket'   => $amp_conf['CDRDBTYPE'] ? $amp_conf['CDRDBTYPE'] : 'mysql',
120         'database' => $amp_conf['CDRDBNAME'] ? $amp_conf['CDRDBNAME'] : 'asteriskcdrdb',
121     );
122     $cdrdb = DB::connect($dsn);
123 }
124
125 $bootstrap_settings['astman_connected'] = false;
126 if (!$bootstrap_settings['skip_astman']) {
127     require_once($dirname . '/libraries/php-asmanager.php');
128   $astman    = new AGI_AsteriskManager($bootstrap_settings['astman_config'], $bootstrap_settings['astman_options']);
129     // attempt to connect to asterisk manager proxy
130     if (!$amp_conf["ASTMANAGERPROXYPORT"] || !$res = $astman->connect($amp_conf["ASTMANAGERHOST"] . ":" . $amp_conf["ASTMANAGERPROXYPORT"], $amp_conf["AMPMGRUSER"] , $amp_conf["AMPMGRPASS"], $bootstrap_settings['astman_events'])) {
131         // attempt to connect directly to asterisk, if no proxy or if proxy failed
132         if (!$res = $astman->connect($amp_conf["ASTMANAGERHOST"] . ":" . $amp_conf["ASTMANAGERPORT"], $amp_conf["AMPMGRUSER"] , $amp_conf["AMPMGRPASS"], $bootstrap_settings['astman_events'])) {
133             // couldn't connect at all
134             unset( $astman );
135             freepbx_log(FPBX_LOG_CRITICAL,"Connection attmempt to AMI failed");
136         } else {
137             $bootstrap_settings['astman_connected'] = true;
138         }
139     }
140 } else {
141     $bootstrap_settings['astman_connected'] = true;
142 }
143
144 //include gui functions + auth if nesesarry
145 // If set to freepbx_auth but we are in a cli mode, then don't bother authenticating either way.
146 // TODO: is it ever possible through an apache or httplite configuration to run a web launched php script
147 //       as 'cli' ? Also, from a security perspective, should we just require this always be set to false
148 //       if we want to bypass authentication and not try to be automatic about it?
149 //
150 if (!$bootstrap_settings['freepbx_auth'] || (php_sapi_name() == 'cli')) {
151     if (!defined('FREEPBX_IS_AUTH')) {
152         define('FREEPBX_IS_AUTH', 'TRUE');
153     }
154 } else {
155     require($dirname . '/libraries/gui_auth.php');
156     frameworkPasswordCheck();
157 }
158 if (!isset($no_auth) && !defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }//we should never need this, just another line of defence
159
160 $restrict_mods_local = $restrict_mods;
161 // I'm pretty sure if this is == true then there is no need to even pull all the module info as we are going down a path
162 // such as an ajax path that this is just overhead. (We'll know soon enough if this is too restrcitive).
163 //
164 if ($restrict_mods_local !== true && !isset($no_auth)) {
165   $active_modules = module_getinfo(false, MODULE_STATUS_ENABLED);
166
167   if(is_array($active_modules)){
168
169         $force_autoload = false;
170       foreach($active_modules as $key => $module) {
171           //include module functions if there not dissabled
172       if ((!$restrict_mods_local || (is_array($restrict_mods_local) && isset($restrict_mods_local[$key]))) && is_file($amp_conf['AMPWEBROOT']."/admin/modules/{$key}/functions.inc.php")) {
173         require_once($amp_conf['AMPWEBROOT']."/admin/modules/{$key}/functions.inc.php");
174
175                 // Zend appears to break class auto-loading. Therefore, if we detect there is a module that requires Zend
176                 // we will include all the potential classes at this point.
177                 //
178                 if (!$force_autoload && isset($module['depends']['phpcomponent']) && stristr($module['depends']['phpcomponent'], 'zend')) {
179                     fpbx_framework_autoloader(true);
180                     $force_autoload = true;
181                 }
182       }
183           //create an array of module sections to display
184           // stored as [items][$type][$category][$name] = $displayvalue
185           if (isset($module['items']) && is_array($module['items'])) {
186               // loop through the types
187               foreach($module['items'] as $itemKey => $item) {
188                 
189                   //if asterisk isnt running, mark moduels that depend on asterisk as disbaled
190                   if (!isset($astman) || !$astman) {
191                       if (( isset($item['needsenginedb']) && strtolower($item['needsenginedb']) == 'yes')
192                       || (isset($item['needsenginerunning']) && strtolower($item['needsenginerunning']) == 'yes')) {
193                           $active_modules[$key]['items'][$itemKey]['disabled'] = true;
194                       }
195                   }
196               }
197           }
198       }
199       $bootstrap_settings['function_modules_included'] = true;
200   }
201 } else {
202     $bootstrap_settings['function_modules_included'] = false;
203 }
204 ?>
205
Note: See TracBrowser for help on using the browser.