root/freepbx/trunk/amp_conf/htdocs/recordings/includes/common.php

Revision 12167, 12.2 kB (checked in by p_lindheimer, 2 years ago)

Merged revisions 11124-11125,11128-11133,11135,11137,11145-11147,11149,11166,11168,11216,11228-11229,11240-11241,11243-11244,11255,11263-11264,11272,11278,11280,11282,11285,11291-11293,11300,11302,11321,11334,11339-11343,11352,11363-11364,11366,11368,11382-11384,11388-11390,11399,11405-11406,11421,11449,11453-11460,11462-11464,11471,11482,11485-11487,11493-11494,11498,11502,11504,11528,11530,11535,11543,11555,11558,11574-11576,11579,11581,11583,11585-11587,11589,11591-11592,11599,11622,11629,11631,11633,11637-11639,11641,11652,11662-11663,11666,11668,11671-11672,11675-11676,11704,11711,11714,11719,11721,11723,11725-11726,11732,11755,11760,11762-11764,11767,11769-11770,11772-11774,11796,11799-11800,11805,11807,11810,11812,11818,11822,11826,11828,11830,11841-11842,11853-11855,11891-11892,11900,11928,11931,11937-11940,11944,11954,11960,11968,11971-11974,11986-11987,12016,12021-12022,12024,12036-12037,12044-12046,12056,12061,12066,12068,12073,12076,12154-12156 via svnmerge from

Merged 2.9 branch to trunk.

  • 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 = isset($amp_conf["AMPDBHOST"]) ? $amp_conf["AMPDBHOST"] : $ASTERISKCDR_DBHOST;
158     $asteriskcdr_dbname = $ASTERISKCDR_DBNAME;
159    
160     $amp_usedevstate = isset($amp_conf["USEDEVSTATE"]) ? strtolower(trim($amp_conf["USEDEVSTATE"])) : 0;
161     if ($amp_usedevstate == 'yes' || $amp_usedevstate == 'true' || $amp_usedevstate == 'on' || $amp_usedevstate == '1') {
162       $amp_usedevstate = 1;
163     } else {
164       $amp_usedevstate = 0;
165     }
166
167   }
168
169   // asterisk manager interface (berkeley database I think)
170   global $asterisk_manager_interface;
171   $asterisk_manager_interface = new AsteriskManagerInterface();
172
173   $success = $asterisk_manager_interface->Connect($mgrhost,$mgruser,$mgrpass);
174   if (!$success) {
175     $_SESSION['ari_error'] = 
176       _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" .
177       _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" .
178       _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" .
179       _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver.");
180     return FALSE;
181   }
182
183
184   global $astman;
185   if (!isset($astman) || !$astman) {
186     // couldn't connect to astman
187         $_SESSION['ari_error'] =
188         _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" .
189         _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" .
190         _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" .
191         _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver.");
192   }
193
194
195   global $db;
196   if (isset($db) && $db) {
197     $_SESSION['dbh_asterisk'] = $db;
198   } else {
199     $_SESSION['ari_error'] .= _("Cannot connect to the $amp_dbname database") . "<br>" .
200                               _("Check AMP installation, asterisk, and ARI main.conf");
201      return FALSE;
202 }
203
204   // cdr database
205   if (in_array('callmonitor',array_keys($loaded_modules))) {
206    $cdrdb = new Database();
207     $_SESSION['dbh_cdr'] = $cdrdb->logon($asteriskcdr_dbengine,
208                                       $asteriskcdr_dbfile,
209                                       $asteriskcdr_dbuser,
210                                       $asteriskcdr_dbpass,
211                                       $asteriskcdr_dbhost,
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");
216       return FALSE;
217     }
218   }
219
220   return TRUE;
221 }
222
223 /**
224  * Logout if needed for any databases
225  */
226 function databaseLogoff() {
227
228   global $asterisk_manager_interface;
229   global $astman;
230
231   $asterisk_manager_interface->Disconnect();
232
233   if (is_object($astman))
234   {
235     $astman->logoff();
236     $astman->disconnect();
237   }
238   unset($astman);
239 }
240
241 /*
242  * Checks if user is set and sets
243  */
244 function loginBlock() {
245
246   $login = new Login();
247
248   if (isset($_REQUEST['logout'])) {
249     $login->Unauth();
250   }
251
252   if (!isset($_SESSION['ari_user'])) {
253     $login->Auth();
254
255   }
256
257   if (!isset($_SESSION['ari_user'])) {
258
259     // login form
260     $ret .= $login->GetForm();
261
262     return $ret;
263   }
264 }
265
266 /*
267  * Main handler for website
268  */
269 function handleBlock() {
270
271   global $ARI_NO_LOGIN;
272
273   global $loaded_modules;
274
275   // check errors here and in login block
276   $content .= checkErrorMessage();
277
278   // check logout
279   if ($_SESSION['ari_user'] && !$ARI_NO_LOGIN) {
280     $logout = 1;
281   }
282
283   // if nothing set goto user default page
284   if (!isset($_REQUEST['m'])) {
285     $_REQUEST['m'] = $_SESSION['ari_user']['default_page'];
286   }
287   // if not function specified then use display page function
288   if (!isset($_REQUEST['f'])) {
289     $_REQUEST['f'] = 'display';
290   }
291
292   $m = $_REQUEST['m'];     // module
293   $f = $_REQUEST['f'];     // function
294   $a = $_REQUEST['a'];     // action
295
296   // set arguments
297   $args = array();
298   foreach($_REQUEST as $key => $value) {
299     $args[$key] = $value;
300   }
301
302   // set rank
303   $ranked_modules = array();
304   foreach ($loaded_modules as $module) {
305
306     $module_methods = get_class_methods($module);    // note that PHP4 returns all lowercase
307     while (list($index, $value) = each($module_methods)) {
308       $module_methods[strtolower($index)] = strtolower($value);
309     }
310     reset($module_methods);
311        
312     $rank = 99999;
313     $rank_function = "rank";
314     if (in_array(strtolower($rank_function), $module_methods)) {
315       $rank = $module->$rank_function();
316     }
317
318     $ranked_modules[$rank][] = $module;
319   }
320   ksort($ranked_modules);
321
322   // process modules
323   foreach ($ranked_modules as $rank => $modules) {
324   $rankloaded = false; //wether this rank has any menu items
325   foreach ($modules as $module)     {
326     $nmenu = false; //text/link that goes in the menu
327       // process module
328       $name = get_class($module);    // note PHP4 returns all lowercase
329       $module_methods = get_class_methods($module);    // note PHP4 returns all lowercase
330       while (list($index, $value) = each($module_methods)) {
331           $module_methods[strtolower($index)] = strtolower($value);
332       }
333       reset($module_methods);
334
335       // init module
336       $module->init();
337
338       // add nav menu items
339       $nav_menu_function = "navMenu";
340       if (in_array(strtolower($nav_menu_function), $module_methods)) {
341       $nmenu = $module->$nav_menu_function($args);
342           //$nav_menu .= $module->$nav_menu_function($args);
343       $nav_menu .= $nmenu;
344       }     
345
346       if (strtolower($m)==strtolower($name)) {
347
348         // build sub menu
349         $subnav_menu_function = "navSubMenu";
350         if (in_array(strtolower($subnav_menu_function), $module_methods)) {
351           $subnav_menu .= $module->$subnav_menu_function($args);
352         }
353
354         // execute function (usually to build content)
355         if (in_array(strtolower($f), $module_methods)) {
356           $content .= $module->$f($args);
357         }
358     }
359  
360    if ($nmenu != false){
361     $nav_menu .= '<br />';
362     $rankloaded = true;
363   }
364 }
365   if ($rankloaded) {
366     $nav_menu .= '<br />';
367   }
368   }
369
370   // add logout link
371   if ($logout != '') {
372     $nav_menu .= "<small><small><a href='" . $_SESSION['ARI_ROOT'] . "?logout=1'>" . _("Logout") . "</a></small></small>";
373   }
374
375   // error message if no content
376   if (!$content) {
377     $content .= _("Page Not Found.");
378   }
379
380   return array($nav_menu,$subnav_menu,$content);
381 }
382
383 /*
384  * Main handler for website
385  */
386 function handler() {
387
388   global $ARI_VERSION;
389
390   // version
391   $ari_version = $ARI_VERSION;
392
393   // check error
394   $error = $_SESSION['ari_error'];
395
396   // load modules
397   loadModules();
398
399   // login to database
400   $success = databaseLogon();
401
402   if ($success) {
403
404     // check if login is needed
405     $content = loginBlock();
406     if (!isset($content)) {
407         list($nav_menu,$subnav_menu,$content) = handleBlock();
408     }
409   }
410   else {
411
412     $display = new Display();
413  
414   $content = '';
415     $content .= $display->displayHeaderText("ARI");
416     $content .= $display->displayLine();
417     $content .= checkErrorMessage();
418   }
419
420   // log off any databases needed
421   databaseLogoff();
422
423   // check for ajax request and refresh or if not build the page
424   if (isset($_REQUEST['ajax_refresh']) ) {
425
426     echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
427       <response>
428         <nav_menu><![CDATA[" . $nav_menu . "]]></nav_menu>
429         <subnav_menu><![CDATA[" . $subnav_menu . "]]></subnav_menu>
430         <content><![CDATA[" . $content . "]]></content>
431       </response>";
432   }
433   else {
434
435     // build the page
436     include_once("./theme/page.tpl.php");
437   }
438 }
439
440 /**
441  * Includes and run functions
442  */ 
443
444 // create asterisk manager interface singleton
445 $asterisk_manager_interface = '';
446
447 // array to keep track of loaded modules
448 $loaded_modules = array();
449
450 include_once("./includes/asi.php");
451 include_once("./includes/database.php");
452 include_once("./includes/display.php");
453 include_once("./includes/ajax.php");
454 include_once("./includes/callme.php");
455 include_once("../admin/functions.inc.php");
456 include_once("../admin/libraries/php-asmanager.php");
457
458 ?>
Note: See TracBrowser for help on using the browser.