5 * common functions - core handler
9 * Checks if user is set and sets
11 function checkErrorMessage() {
13 if ($_SESSION['ari_error']) {
14 $ret .= "<div class='error'>
15 " . $_SESSION['ari_error'] . "
18 unset($_SESSION['ari_error']);
25 * Checks modules directory, and configuration, and loaded modules
27 function loadModules() {
29 global $ARI_ADMIN_MODULES;
30 global $ARI_DISABLED_MODULES;
32 global $loaded_modules;
34 $modules_path = "./modules";
35 if (is_dir($modules_path)) {
40 $files = getFiles($modules_path,$filter,$recursive_max,$recursive_count);
42 foreach($files as $key => $path) {
44 // build module object
46 $path_parts = pathinfo($path);
47 list($name,$ext) = split("\.",$path_parts['basename']);
49 // check for module and get rank
50 if (class_exists($name)) {
52 $module = new $name();
54 // check if admin module
56 if ($ARI_ADMIN_MODULES) {
57 $admin_modules = split(',',$ARI_ADMIN_MODULES);
58 foreach ($admin_modules as $key => $value) {
66 // check if disabled module
68 if ($ARI_DISABLED_MODULES) {
69 $disabled_modules = split(',',$ARI_DISABLED_MODULES);
70 foreach ($disabled_modules as $key => $value) {
78 // if not admin module or admin user add to module name to array
79 if (!$disabled && (!$found || $_SESSION['ari_user']['admin'])) {
80 $loaded_modules[$name] = $module;
86 $_SESSION['ari_error'] = _("$path not a directory or not readable");
91 * Builds database connections
93 function databaseLogon() {
97 global $ASTERISKMGR_DBHOST;
99 global $AMP_FUNCTIONS_FILES;
100 global $AMPORTAL_CONF_FILE;
102 global $LEGACY_AMP_DBENGINE;
103 global $LEGACY_AMP_DBFILE;
104 global $LEGACY_AMP_DBHOST;
105 global $LEGACY_AMP_DBNAME;
107 global $ASTERISKCDR_DBENGINE;
108 global $ASTERISKCDR_DBFILE;
109 global $ASTERISKCDR_DBHOST;
110 global $ASTERISKCDR_DBNAME;
112 global $ARI_DISABLED_MODULES;
114 global $loaded_modules;
116 // This variable is a global in the FreePBX function.inc.php but needs to be
117 // declared here or the is not seen when parse_amprotaconf() is eventually called
120 global $amp_conf_defaults;
123 if ($STANDALONE['use']) {
125 $mgrhost = $ASTERISKMGR_DBHOST;
126 $mgruser = $STANDALONE['asterisk_mgruser'];
127 $mgrpass = $STANDALONE['asterisk_mgrpass'];
129 $asteriskcdr_dbengine = $ASTERISKCDR_DBENGINE;
130 $asteriskcdr_dbfile = $ASTERISKCDR_DBFILE;
131 $asteriskcdr_dbuser = $STANDALONE['asteriskcdr_dbuser'];
132 $asteriskcdr_dbpass = $STANDALONE['asteriskcdr_dbpass'];
133 $asteriskcdr_dbhost = $ASTERISKCDR_DBHOST;
134 $asteriskcdr_dbname = $ASTERISKCDR_DBNAME;
139 $files = split(';',$AMP_FUNCTIONS_FILES);
140 foreach ($files as $file) {
141 if (is_file($file)) {
148 $amp_conf = parse_amportal_conf($AMPORTAL_CONF_FILE);
150 $mgrhost = $ASTERISKMGR_DBHOST;
151 $mgruser = $amp_conf['AMPMGRUSER'];
152 $mgrpass = $amp_conf['AMPMGRPASS'];
154 $amp_dbengine = isset($amp_conf["AMPDBENGINE"]) ? $amp_conf["AMPDBENGINE"] : $LEGACY_AMP_DBENGINE;
155 $amp_dbfile = isset($amp_conf["AMPDBFILE"]) ? $amp_conf["AMPDBFILE"] : $LEGACY_AMP_DBFILE;
156 $amp_dbuser = $amp_conf["AMPDBUSER"];
157 $amp_dbpass = $amp_conf["AMPDBPASS"];
158 $amp_dbhost = isset($amp_conf["AMPDBHOST"]) ? $amp_conf["AMPDBHOST"] : $LEGACY_AMP_DBHOST;
159 $amp_dbname = isset($amp_conf["AMPDBNAME"]) ? $amp_conf["AMPDBNAME"] : $LEGACY_AMP_DBNAME;
161 $asteriskcdr_dbengine = $ASTERISKCDR_DBENGINE;
162 $asteriskcdr_dbfile = $ASTERISKCDR_DBFILE;
163 $asteriskcdr_dbuser = $amp_conf["AMPDBUSER"];
164 $asteriskcdr_dbpass = $amp_conf["AMPDBPASS"];
165 $asteriskcdr_dbhost = $ASTERISKCDR_DBHOST;
166 $asteriskcdr_dbhost = isset($amp_conf["AMPDBHOST"]) ? $amp_conf["AMPDBHOST"] : $ASTERISKCDR_DBHOST;
167 $asteriskcdr_dbname = $ASTERISKCDR_DBNAME;
173 // asterisk manager interface (berkeley database I think)
174 global $asterisk_manager_interface;
175 $asterisk_manager_interface = new AsteriskManagerInterface();
177 $success = $asterisk_manager_interface->Connect($mgrhost,$mgruser,$mgrpass);
179 $_SESSION['ari_error'] =
180 _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" .
181 _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" .
182 _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" .
183 _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver.");
187 // pear interface databases
188 $db = new Database();
190 // AMP asterisk database
191 if (!$STANDALONE['use']) {
192 $_SESSION['dbh_asterisk'] = $db->logon($amp_dbengine,
198 if (!isset($_SESSION['dbh_asterisk'])) {
199 $_SESSION['ari_error'] .= _("Cannot connect to the $amp_dbname database") . "<br>" .
200 _("Check AMP installation, asterisk, and ARI main.conf");
206 if (in_array('callmonitor',array_keys($loaded_modules))) {
207 $_SESSION['dbh_cdr'] = $db->logon($asteriskcdr_dbengine,
212 $asteriskcdr_dbname);
213 if (!isset($_SESSION['dbh_cdr'])) {
214 $_SESSION['ari_error'] .= sprintf(_("Cannot connect to the $asteriskcdr_dbname database"),$asteriskcdr_dbname) . "<br>" .
215 _("Check AMP installation, asterisk, and ARI main.conf");
224 * Logout if needed for any databases
226 function databaseLogoff() {
228 global $asterisk_manager_interface;
230 $asterisk_manager_interface->Disconnect();
234 * Checks if user is set and sets
236 function loginBlock() {
238 $login = new Login();
240 if (isset($_REQUEST['logout'])) {
244 if (!isset($_SESSION['ari_user'])) {
249 if (!isset($_SESSION['ari_user'])) {
252 $ret .= $login->GetForm();
259 * Main handler for website
261 function handleBlock() {
263 global $ARI_NO_LOGIN;
265 global $loaded_modules;
267 // check errors here and in login block
268 $content .= checkErrorMessage();
271 if ($_SESSION['ari_user'] && !$ARI_NO_LOGIN) {
275 // if nothing set goto user default page
276 if (!isset($_REQUEST['m'])) {
277 $_REQUEST['m'] = $_SESSION['ari_user']['default_page'];
279 // if not function specified then use display page function
280 if (!isset($_REQUEST['f'])) {
281 $_REQUEST['f'] = 'display';
284 $m = $_REQUEST['m']; // module
285 $f = $_REQUEST['f']; // function
286 $a = $_REQUEST['a']; // action
290 foreach($_REQUEST as $key => $value) {
291 $args[$key] = $value;
295 $ranked_modules = array();
296 foreach ($loaded_modules as $module) {
298 $module_methods = get_class_methods($module); // note that PHP4 returns all lowercase
299 while (list($index, $value) = each($module_methods)) {
300 $module_methods[strtolower($index)] = strtolower($value);
302 reset($module_methods);
305 $rank_function = "rank";
306 if (in_array(strtolower($rank_function), $module_methods)) {
307 $rank = $module->$rank_function();
310 $ranked_modules[$rank] = $module;
312 ksort($ranked_modules);
315 foreach ($ranked_modules as $module) {
318 $name = get_class($module); // note PHP4 returns all lowercase
319 $module_methods = get_class_methods($module); // note PHP4 returns all lowercase
320 while (list($index, $value) = each($module_methods)) {
321 $module_methods[strtolower($index)] = strtolower($value);
323 reset($module_methods);
328 // add nav menu items
329 $nav_menu_function = "navMenu";
330 if (in_array(strtolower($nav_menu_function), $module_methods)) {
331 $nav_menu .= $module->$nav_menu_function($args);
334 if (strtolower($m)==strtolower($name)) {
337 $subnav_menu_function = "navSubMenu";
338 if (in_array(strtolower($subnav_menu_function), $module_methods)) {
339 $subnav_menu .= $module->$subnav_menu_function($args);
342 // execute function (usually to build content)
343 if (in_array(strtolower($f), $module_methods)) {
344 $content .= $module->$f($args);
351 $nav_menu .= "<p><small><small><a href='" . $_SESSION['ARI_ROOT'] . "?logout=1'>" . _("Logout") . "</a></small></small></p>";
354 // error message if no content
356 $content .= _("Page Not Found.");
359 return array($nav_menu,$subnav_menu,$content);
363 * Main handler for website
370 $ari_version = $ARI_VERSION;
373 $error = $_SESSION['ari_error'];
379 $success = databaseLogon();
382 // check if login is needed
383 $content = loginBlock();
384 if (!isset($content)) {
385 list($nav_menu,$subnav_menu,$content) = handleBlock();
390 $display = new Display();
392 $content .= $display->displayHeaderText("ARI");
393 $content .= $display->displayLine();
394 $content .= checkErrorMessage();
397 // log off any databases needed
400 // check for ajax request and refresh or if not build the page
401 if (isset($_REQUEST['ajax_refresh']) ) {
403 echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
405 <nav_menu><![CDATA[" . $nav_menu . "]]></nav_menu>
406 <subnav_menu><![CDATA[" . $subnav_menu . "]]></subnav_menu>
407 <content><![CDATA[" . $content . "]]></content>
413 include_once("./theme/page.tpl.php");
418 * Includes and run functions
421 // create asterisk manager interface singleton
422 $asterisk_manager_interface = '';
424 // array to keep track of loaded modules
425 $loaded_modules = array();
427 include_once("./includes/asi.php");
428 include_once("./includes/database.php");
429 include_once("./includes/display.php");
430 include_once("./includes/ajax.php");
432 include_once("./includes/freeside.class.php");