first try at single sign-on
[freeside.git] / fs_selfservice / fri / includes / common.php
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) = 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 = 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 = 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
95   global $STANDALONE;
96
97   global $ASTERISKMGR_DBHOST;
98
99   global $AMP_FUNCTIONS_FILES;
100   global $AMPORTAL_CONF_FILE;
101
102   global $LEGACY_AMP_DBENGINE;
103   global $LEGACY_AMP_DBFILE;
104   global $LEGACY_AMP_DBHOST;
105   global $LEGACY_AMP_DBNAME;
106
107   global $ASTERISKCDR_DBENGINE;
108   global $ASTERISKCDR_DBFILE;
109   global $ASTERISKCDR_DBHOST;
110   global $ASTERISKCDR_DBNAME;
111
112   global $ARI_DISABLED_MODULES;
113
114   global $loaded_modules;
115
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
118         // ?php bug?
119         //
120         global $amp_conf_defaults;
121
122   // get user
123   if ($STANDALONE['use']) {
124
125     $mgrhost = $ASTERISKMGR_DBHOST;
126     $mgruser = $STANDALONE['asterisk_mgruser'];
127     $mgrpass = $STANDALONE['asterisk_mgrpass'];
128
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;
135   } 
136   else {
137
138     $include = 0;
139     $files = split(';',$AMP_FUNCTIONS_FILES);
140     foreach ($files as $file) {
141       if (is_file($file)) {
142         include_once($file);
143         $include = 1;
144       }
145     }
146
147     if ($include) {
148       $amp_conf = parse_amportal_conf($AMPORTAL_CONF_FILE);
149
150       $mgrhost = $ASTERISKMGR_DBHOST;
151       $mgruser = $amp_conf['AMPMGRUSER'];
152       $mgrpass = $amp_conf['AMPMGRPASS'];
153
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;
160
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;
168
169       unset($amp_conf);
170     } 
171   }
172
173   // asterisk manager interface (berkeley database I think)
174   global $asterisk_manager_interface;
175   $asterisk_manager_interface = new AsteriskManagerInterface();
176
177   $success = $asterisk_manager_interface->Connect($mgrhost,$mgruser,$mgrpass);
178   if (!$success) {
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.");
184     return FALSE;
185   }
186
187   // pear interface databases
188   $db = new Database();
189
190   // AMP asterisk database
191   if (!$STANDALONE['use']) {
192     $_SESSION['dbh_asterisk'] = $db->logon($amp_dbengine,
193                                            $amp_dbfile,
194                                            $amp_dbuser, 
195                                            $amp_dbpass,
196                                            $amp_dbhost,
197                                            $amp_dbname);
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");
201       return FALSE;
202     }
203   }
204
205   // cdr database
206   if (in_array('callmonitor',array_keys($loaded_modules))) {
207     $_SESSION['dbh_cdr'] = $db->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
230   $asterisk_manager_interface->Disconnect();
231 }
232
233 /*
234  * Checks if user is set and sets
235  */
236 function loginBlock() {
237
238   $login = new Login();
239
240   if (isset($_REQUEST['logout'])) {
241     $login->Unauth();
242   }
243
244   if (!isset($_SESSION['ari_user'])) {
245     $login->Auth();
246
247   }
248
249   if (!isset($_SESSION['ari_user'])) {
250
251     // login form
252     $ret .= $login->GetForm();
253
254     return $ret;
255   }
256 }
257
258 /*
259  * Main handler for website
260  */
261 function handleBlock() {
262
263   global $ARI_NO_LOGIN;
264
265   global $loaded_modules;
266
267   // check errors here and in login block
268   $content .= checkErrorMessage();
269
270   // check logout
271   if ($_SESSION['ari_user'] && !$ARI_NO_LOGIN) {
272     $logout = 1;
273   }
274
275   // if nothing set goto user default page
276   if (!isset($_REQUEST['m'])) {
277     $_REQUEST['m'] = $_SESSION['ari_user']['default_page'];
278   }
279   // if not function specified then use display page function
280   if (!isset($_REQUEST['f'])) {
281     $_REQUEST['f'] = 'display';
282   }
283
284   $m = $_REQUEST['m'];     // module
285   $f = $_REQUEST['f'];     // function
286   $a = $_REQUEST['a'];     // action
287
288   // set arguments
289   $args = array();
290   foreach($_REQUEST as $key => $value) {
291     $args[$key] = $value;
292   }
293
294   // set rank
295   $ranked_modules = array();
296   foreach ($loaded_modules as $module) {
297
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);
301     }
302     reset($module_methods);
303         
304     $rank = 99999;
305     $rank_function = "rank";
306     if (in_array(strtolower($rank_function), $module_methods)) {
307       $rank = $module->$rank_function(); 
308     }
309
310     $ranked_modules[$rank] = $module;
311   }
312   ksort($ranked_modules);
313
314   // process modules
315   foreach ($ranked_modules as $module) {
316
317     // process 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);
322     }
323     reset($module_methods);
324
325     // init module
326     $module->init();
327
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); 
332     }      
333
334     if (strtolower($m)==strtolower($name)) {
335
336       // build sub menu 
337       $subnav_menu_function = "navSubMenu";
338       if (in_array(strtolower($subnav_menu_function), $module_methods)) {
339         $subnav_menu .= $module->$subnav_menu_function($args); 
340       }
341
342       // execute function (usually to build content)
343       if (in_array(strtolower($f), $module_methods)) {
344         $content .= $module->$f($args);
345       }
346     }
347   }
348
349   // add logout link
350   if ($logout != '') { 
351     $nav_menu .= "<p><small><small><a href='" . $_SESSION['ARI_ROOT'] . "?logout=1'>" . _("Logout") . "</a></small></small></p>";
352   } 
353
354   // error message if no content
355   if (!$content) {
356     $content .= _("Page Not Found.");
357   } 
358
359   return array($nav_menu,$subnav_menu,$content);
360 }
361
362 /*
363  * Main handler for website
364  */
365 function handler() {
366
367   global $ARI_VERSION;
368
369   // version
370   $ari_version = $ARI_VERSION;
371
372   // check error
373   $error = $_SESSION['ari_error'];
374
375   // load modules
376   loadModules();
377
378   // login to database
379   $success = databaseLogon();
380   if ($success) {
381
382     // check if login is needed
383     $content = loginBlock();
384     if (!isset($content)) {
385         list($nav_menu,$subnav_menu,$content) = handleBlock();
386     }
387   }
388   else {
389
390     $display = new Display();
391
392     $content .= $display->displayHeaderText("ARI");
393     $content .= $display->displayLine();
394     $content .= checkErrorMessage();
395   }
396
397   // log off any databases needed
398   databaseLogoff();
399
400   // check for ajax request and refresh or if not build the page
401   if (isset($_REQUEST['ajax_refresh']) ) {
402
403     echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
404       <response>
405         <nav_menu><![CDATA[" . $nav_menu . "]]></nav_menu>
406         <subnav_menu><![CDATA[" . $subnav_menu . "]]></subnav_menu>
407         <content><![CDATA[" . $content . "]]></content>
408       </response>";
409   }
410   else {
411
412     // build the page
413     include_once("./theme/page.tpl.php"); 
414   }
415 }
416
417 /**
418  * Includes and run functions
419  */  
420
421 // create asterisk manager interface singleton
422 $asterisk_manager_interface = '';
423
424 // array to keep track of loaded modules
425 $loaded_modules = array();
426
427 include_once("./includes/asi.php");
428 include_once("./includes/database.php");
429 include_once("./includes/display.php"); 
430 include_once("./includes/ajax.php");
431
432 include_once("./includes/freeside.class.php");
433
434 ?>