root/freepbx/branches/bootstrap/amp_conf/htdocs/recordings/includes/common.php

Revision 10776, 12.2 kB (checked in by mbrevda, 2 years ago)

re #4566 - make are play nicly. also, some amportal freepbx_engein code tweeks

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 <?php
2
3 /**
4  * @file
5  * common functions - core handler
6  */
7
8 /*
9  * Checks if user is set and sets
10  */
11 function checkErrorMessage() {
12
13   if ($_SESSION['ari_error']) {
14     $ret = "<div class='error'>
15                " . $_SESSION['ari_error'] . "
16              </div>
17              <br>";
18     unset($_SESSION['ari_error']);
19   }
20
21   return $ret;
22 }
23
24 /*
25  * Checks modules directory, and configuration, and loaded modules
26  */
27 function loadModules() {
28
29   global $ARI_ADMIN_MODULES;
30   global $ARI_DISABLED_MODULES;
31
32   global $loaded_modules;
33
34   $modules_path = "./modules";
35   if (is_dir($modules_path)) {
36
37     $filter = ".module";
38     $recursive_max = 1;
39     $recursive_count = 0;
40     $files = getFiles($modules_path,$filter,$recursive_max,$recursive_count);
41
42     foreach($files as $key => $path) {
43
44       // build module object
45       include_once($path);
46       $path_parts = pathinfo($path);
47       list($name,$ext) = preg_split("/\./",$path_parts['basename']);
48
49       // check for module and get rank
50       if (class_exists($name)) {
51
52         $module = new $name();
53
54         // check if admin module
55         $found = 0;
56         if ($ARI_ADMIN_MODULES) {
57           $admin_modules = preg_split('/,/',$ARI_ADMIN_MODULES);
58           foreach ($admin_modules as $key => $value) {
59             if ($name==$value) {
60               $found = 1;
61               break;
62             }
63           }
64         }
65
66         // check if disabled module
67         $disabled = 0;
68         if ($ARI_DISABLED_MODULES) {
69           $disabled_modules = preg_split('/,/',$ARI_DISABLED_MODULES);
70           foreach ($disabled_modules as $key => $value) {
71             if ($name==$value) {
72               $disabled = 1;
73               break;
74             }
75           }
76         }
77
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;
81         }
82       }
83     }
84   }
85   else {
86     $_SESSION['ari_error'] = _("$path not a directory or not readable");
87   }
88 }
89
90 /**
91  * Builds database connections
92  */
93 function databaseLogon() {
94   global $STANDALONE;
95
96   global $ASTERISKMGR_DBHOST;
97
98   global $AMP_FUNCTIONS_FILES;
99   global $AMPORTAL_CONF_FILE;
100
101   global $LEGACY_AMP_DBENGINE;
102   global $LEGACY_AMP_DBFILE;
103   global $LEGACY_AMP_DBHOST;
104   global $LEGACY_AMP_DBNAME;
105
106   global $ASTERISKCDR_DBENGINE;
107   global $ASTERISKCDR_DBFILE;
108   global $ASTERISKCDR_DBHOST;
109   global $ASTERISKCDR_DBNAME;
110
111   global $ARI_DISABLED_MODULES;
112
113   global $ARI_ADMIN_USERNAME;
114   global $ARI_ADMIN_PASSWORD;
115   global $ariadminusername;
116   global $ariadminpassword;
117
118   global $loaded_modules;
119
120   // This variable is a global in the FreePBX function.inc.php but needs to be
121   // declared here or the is not seen when parse_amprotaconf() is eventually called
122   // ?php bug?
123   //
124   global $amp_conf_defaults;
125
126   // get user
127   if ($STANDALONE['use']) {
128     $mgrhost = $ASTERISKMGR_DBHOST;
129     $mgruser = $STANDALONE['asterisk_mgruser'];
130     $mgrpass = $STANDALONE['asterisk_mgrpass'];
131     $asteriskcdr_dbengine = $ASTERISKCDR_DBENGINE;
132     $asteriskcdr_dbfile = $ASTERISKCDR_DBFILE;
133     $asteriskcdr_dbuser = $STANDALONE['asteriskcdr_dbuser'];
134     $asteriskcdr_dbpass = $STANDALONE['asteriskcdr_dbpass'];
135     $asteriskcdr_dbhost = $ASTERISKCDR_DBHOST;
136     $asteriskcdr_dbname = $ASTERISKCDR_DBNAME;
137   } else {
138    
139     global $amp_conf, $amp_usedevstate;
140     $ariadminusername = isset($amp_conf["ARI_ADMIN_USERNAME"]) ? $amp_conf["ARI_ADMIN_USERNAME"] : $ARI_ADMIN_USERNAME;
141     $ariadminpassword = isset($amp_conf["ARI_ADMIN_PASSWORD"]) ? $amp_conf["ARI_ADMIN_PASSWORD"] : $ARI_ADMIN_PASSWORD;
142     $mgrhost = $ASTERISKMGR_DBHOST;
143     $mgruser = $amp_conf['AMPMGRUSER'];
144     $mgrpass = $amp_conf['AMPMGRPASS'];
145
146     $amp_dbengine = isset($amp_conf["AMPDBENGINE"]) ? $amp_conf["AMPDBENGINE"] : $LEGACY_AMP_DBENGINE;
147     $amp_dbfile = isset($amp_conf["AMPDBFILE"]) ? $amp_conf["AMPDBFILE"] : $LEGACY_AMP_DBFILE;
148     $amp_dbuser = $amp_conf["AMPDBUSER"];
149     $amp_dbpass = $amp_conf["AMPDBPASS"];
150     $amp_dbhost = isset($amp_conf["AMPDBHOST"]) ? $amp_conf["AMPDBHOST"] : $LEGACY_AMP_DBHOST;
151     $amp_dbname = isset($amp_conf["AMPDBNAME"]) ? $amp_conf["AMPDBNAME"] : $LEGACY_AMP_DBNAME;
152
153     $asteriskcdr_dbengine = $ASTERISKCDR_DBENGINE;
154     $asteriskcdr_dbfile = $ASTERISKCDR_DBFILE;
155     $asteriskcdr_dbuser = $amp_conf["AMPDBUSER"];
156     $asteriskcdr_dbpass = $amp_conf["AMPDBPASS"];
157     $asteriskcdr_dbhost = $ASTERISKCDR_DBHOST;
158     $asteriskcdr_dbhost = isset($amp_conf["AMPDBHOST"]) ? $amp_conf["AMPDBHOST"] : $ASTERISKCDR_DBHOST;
159     $asteriskcdr_dbname = $ASTERISKCDR_DBNAME;
160    
161     $amp_usedevstate = isset($amp_conf["USEDEVSTATE"]) ? strtolower(trim($amp_conf["USEDEVSTATE"])) : 0;
162     if ($amp_usedevstate == 'yes' || $amp_usedevstate == 'true' || $amp_usedevstate == 'on' || $amp_usedevstate == '1') {
163       $amp_usedevstate = 1;
164     } else {
165       $amp_usedevstate = 0;
166     }
167
168   }
169
170   // asterisk manager interface (berkeley database I think)
171   global $asterisk_manager_interface;
172   $asterisk_manager_interface = new AsteriskManagerInterface();
173
174   $success = $asterisk_manager_interface->Connect($mgrhost,$mgruser,$mgrpass);
175   if (!$success) {
176     $_SESSION['ari_error'] = 
177       _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" .
178       _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" .
179       _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" .
180       _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver.");
181     return FALSE;
182   }
183
184
185   global $astman;
186   if (!isset($astman) || !$astman) {
187     // couldn't connect to astman
188         $_SESSION['ari_error'] =
189         _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" .
190         _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" .
191         _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" .
192         _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver.");
193   }
194
195
196   global $db;
197   if (isset($db) && $db) {
198     $_SESSION['dbh_asterisk'] = $db;
199   } else {
200     $_SESSION['ari_error'] .= _("Cannot connect to the $amp_dbname database") . "<br>" .
201                               _("Check AMP installation, asterisk, and ARI main.conf");
202      return FALSE;
203 }
204
205   // cdr database
206   if (in_array('callmonitor',array_keys($loaded_modules))) {
207    $cdrdb = new Database();
208     $_SESSION['dbh_cdr'] = $cdrdb->logon($asteriskcdr_dbengine,
209                                       $asteriskcdr_dbfile,
210                                       $asteriskcdr_dbuser,
211                                       $asteriskcdr_dbpass,
212                                       $asteriskcdr_dbhost,
213                                       $asteriskcdr_dbname);
214     if (!isset($_SESSION['dbh_cdr'])) {
215       $_SESSION['ari_error'] .= sprintf(_("Cannot connect to the $asteriskcdr_dbname database"),$asteriskcdr_dbname) . "<br>" .
216                                _("Check AMP installation, asterisk, and ARI main.conf");
217       return FALSE;
218     }
219   }
220
221   return TRUE;
222 }
223
224 /**
225  * Logout if needed for any databases
226  */
227 function databaseLogoff() {
228
229   global $asterisk_manager_interface;
230   global $astman;
231
232   $asterisk_manager_interface->Disconnect();
233
234   if (is_object($astman))
235   {
236     $astman->logoff();
237     $astman->disconnect();
238   }
239   unset($astman);
240 }
241
242 /*
243  * Checks if user is set and sets
244  */
245 function loginBlock() {
246
247   $login = new Login();
248
249   if (isset($_REQUEST['logout'])) {
250     $login->Unauth();
251   }
252
253   if (!isset($_SESSION['ari_user'])) {
254     $login->Auth();
255
256   }
257
258   if (!isset($_SESSION['ari_user'])) {
259
260     // login form
261     $ret .= $login->GetForm();
262
263     return $ret;
264   }
265 }
266
267 /*
268  * Main handler for website
269  */
270 function handleBlock() {
271
272   global $ARI_NO_LOGIN;
273
274   global $loaded_modules;
275
276   // check errors here and in login block
277   $content .= checkErrorMessage();
278
279   // check logout
280   if ($_SESSION['ari_user'] && !$ARI_NO_LOGIN) {
281     $logout = 1;
282   }
283
284   // if nothing set goto user default page
285   if (!isset($_REQUEST['m'])) {
286     $_REQUEST['m'] = $_SESSION['ari_user']['default_page'];
287   }
288   // if not function specified then use display page function
289   if (!isset($_REQUEST['f'])) {
290     $_REQUEST['f'] = 'display';
291   }
292
293   $m = $_REQUEST['m'];     // module
294   $f = $_REQUEST['f'];     // function
295   $a = $_REQUEST['a'];     // action
296
297   // set arguments
298   $args = array();
299   foreach($_REQUEST as $key => $value) {
300     $args[$key] = $value;
301   }
302
303   // set rank
304   $ranked_modules = array();
305   foreach ($loaded_modules as $module) {
306
307     $module_methods = get_class_methods($module);    // note that PHP4 returns all lowercase
308     while (list($index, $value) = each($module_methods)) {
309       $module_methods[strtolower($index)] = strtolower($value);
310     }
311     reset($module_methods);
312        
313     $rank = 99999;
314     $rank_function = "rank";
315     if (in_array(strtolower($rank_function), $module_methods)) {
316       $rank = $module->$rank_function();
317     }
318
319     $ranked_modules[$rank][] = $module;
320   }
321   ksort($ranked_modules);
322
323   // process modules
324   foreach ($ranked_modules as $rank => $modules) {
325   $rankloaded = false; //wether this rank has any menu items
326   foreach ($modules as $module)     {
327     $nmenu = false; //text/link that goes in the menu
328       // process module
329       $name = get_class($module);    // note PHP4 returns all lowercase
330       $module_methods = get_class_methods($module);    // note PHP4 returns all lowercase
331       while (list($index, $value) = each($module_methods)) {
332           $module_methods[strtolower($index)] = strtolower($value);
333       }
334       reset($module_methods);
335
336       // init module
337       $module->init();
338
339       // add nav menu items
340       $nav_menu_function = "navMenu";
341       if (in_array(strtolower($nav_menu_function), $module_methods)) {
342       $nmenu = $module->$nav_menu_function($args);
343           //$nav_menu .= $module->$nav_menu_function($args);
344       $nav_menu .= $nmenu;
345       }     
346
347       if (strtolower($m)==strtolower($name)) {
348
349         // build sub menu
350         $subnav_menu_function = "navSubMenu";
351         if (in_array(strtolower($subnav_menu_function), $module_methods)) {
352           $subnav_menu .= $module->$subnav_menu_function($args);
353         }
354
355         // execute function (usually to build content)
356         if (in_array(strtolower($f), $module_methods)) {
357           $content .= $module->$f($args);
358         }
359     }
360  
361    if ($nmenu != false){
362     $nav_menu .= '<br />';
363     $rankloaded = true;
364   }
365 }
366   if ($rankloaded) {
367     $nav_menu .= '<br />';
368   }
369   }
370
371   // add logout link
372   if ($logout != '') {
373     $nav_menu .= "<small><small><a href='" . $_SESSION['ARI_ROOT'] . "?logout=1'>" . _("Logout") . "</a></small></small>";
374   }
375
376   // error message if no content
377   if (!$content) {
378     $content .= _("Page Not Found.");
379   }
380
381   return array($nav_menu,$subnav_menu,$content);
382 }
383
384 /*
385  * Main handler for website
386  */
387 function handler() {
388
389   global $ARI_VERSION;
390
391   // version
392   $ari_version = $ARI_VERSION;
393
394   // check error
395   $error = $_SESSION['ari_error'];
396
397   // load modules
398   loadModules();
399
400   // login to database
401   $success = databaseLogon();
402
403   if ($success) {
404
405     // check if login is needed
406     $content = loginBlock();
407     if (!isset($content)) {
408         list($nav_menu,$subnav_menu,$content) = handleBlock();
409     }
410   }
411   else {
412
413     $display = new Display();
414  
415   $content = '';
416     $content .= $display->displayHeaderText("ARI");
417     $content .= $display->displayLine();
418     $content .= checkErrorMessage();
419   }
420
421   // log off any databases needed
422   databaseLogoff();
423
424   // check for ajax request and refresh or if not build the page
425   if (isset($_REQUEST['ajax_refresh']) ) {
426
427     echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
428       <response>
429         <nav_menu><![CDATA[" . $nav_menu . "]]></nav_menu>
430         <subnav_menu><![CDATA[" . $subnav_menu . "]]></subnav_menu>
431         <content><![CDATA[" . $content . "]]></content>
432       </response>";
433   }
434   else {
435
436     // build the page
437     include_once("./theme/page.tpl.php");
438   }
439 }
440
441 /**
442  * Includes and run functions
443  */ 
444
445 // create asterisk manager interface singleton
446 $asterisk_manager_interface = '';
447
448 // array to keep track of loaded modules
449 $loaded_modules = array();
450
451 include_once("./includes/asi.php");
452 include_once("./includes/database.php");
453 include_once("./includes/display.php");
454 include_once("./includes/ajax.php");
455 include_once("./includes/callme.php");
456 include_once("../admin/functions.inc.php");
457 include_once("../admin/libraries/php-asmanager.php");
458
459 ?>
Note: See TracBrowser for help on using the browser.