summaryrefslogtreecommitdiff
path: root/fs_selfservice/fri/includes/common.php
blob: 87f202638d7e4d46c6c818e031ddc0f342e28232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php

/**
 * @file
 * common functions - core handler
 */

/*
 * Checks if user is set and sets
 */
function checkErrorMessage() {

  if ($_SESSION['ari_error']) {
    $ret .= "<div class='error'>
               " . $_SESSION['ari_error'] . "
             </div>
             <br>";
    unset($_SESSION['ari_error']);
  }

  return $ret;
}

/*
 * Checks modules directory, and configuration, and loaded modules 
 */
function loadModules() {

  global $ARI_ADMIN_MODULES;
  global $ARI_DISABLED_MODULES;

  global $loaded_modules;

  $modules_path = "./modules";
  if (is_dir($modules_path)) {

    $filter = ".module";
    $recursive_max = 1;
    $recursive_count = 0;
    $files = getFiles($modules_path,$filter,$recursive_max,$recursive_count);

    foreach($files as $key => $path) {

      // build module object 
      include_once($path); 
      $path_parts = pathinfo($path);
      list($name,$ext) = split("\.",$path_parts['basename']);

      // check for module and get rank
      if (class_exists($name)) {

        $module = new $name();

        // check if admin module
        $found = 0;
        if ($ARI_ADMIN_MODULES) {
          $admin_modules = split(',',$ARI_ADMIN_MODULES);
          foreach ($admin_modules as $key => $value) {
            if ($name==$value) {
              $found = 1;
              break;
            }
          }
        }

        // check if disabled module
        $disabled = 0;
        if ($ARI_DISABLED_MODULES) {
          $disabled_modules = split(',',$ARI_DISABLED_MODULES);
          foreach ($disabled_modules as $key => $value) {
            if ($name==$value) {
              $disabled = 1;
              break;
            }
          }
        }

        // if not admin module or admin user add to module name to array
        if (!$disabled && (!$found || $_SESSION['ari_user']['admin'])) {
          $loaded_modules[$name] = $module;
        }
      }
    }
  }
  else {
    $_SESSION['ari_error'] = _("$path not a directory or not readable");
  }
}

/**
 * Builds database connections
 */
function databaseLogon() {

  global $STANDALONE;

  global $ASTERISKMGR_DBHOST;

  global $AMP_FUNCTIONS_FILES;
  global $AMPORTAL_CONF_FILE;

  global $LEGACY_AMP_DBENGINE;
  global $LEGACY_AMP_DBFILE;
  global $LEGACY_AMP_DBHOST;
  global $LEGACY_AMP_DBNAME;

  global $ASTERISKCDR_DBENGINE;
  global $ASTERISKCDR_DBFILE;
  global $ASTERISKCDR_DBHOST;
  global $ASTERISKCDR_DBNAME;

  global $ARI_DISABLED_MODULES;

  global $loaded_modules;

	// This variable is a global in the FreePBX function.inc.php but needs to be
	// declared here or the is not seen when parse_amprotaconf() is eventually called
	// ?php bug?
	//
	global $amp_conf_defaults;

  // get user
  if ($STANDALONE['use']) {

    $mgrhost = $ASTERISKMGR_DBHOST;
    $mgruser = $STANDALONE['asterisk_mgruser'];
    $mgrpass = $STANDALONE['asterisk_mgrpass'];

    $asteriskcdr_dbengine = $ASTERISKCDR_DBENGINE;
    $asteriskcdr_dbfile = $ASTERISKCDR_DBFILE;
    $asteriskcdr_dbuser = $STANDALONE['asteriskcdr_dbuser'];
    $asteriskcdr_dbpass = $STANDALONE['asteriskcdr_dbpass'];
    $asteriskcdr_dbhost = $ASTERISKCDR_DBHOST;
    $asteriskcdr_dbname = $ASTERISKCDR_DBNAME;
  } 
  else {

    $include = 0;
    $files = split(';',$AMP_FUNCTIONS_FILES);
    foreach ($files as $file) {
      if (is_file($file)) {
        include_once($file);
        $include = 1;
      }
    }

    if ($include) {
      $amp_conf = parse_amportal_conf($AMPORTAL_CONF_FILE);

      $mgrhost = $ASTERISKMGR_DBHOST;
      $mgruser = $amp_conf['AMPMGRUSER'];
      $mgrpass = $amp_conf['AMPMGRPASS'];

      $amp_dbengine = isset($amp_conf["AMPDBENGINE"]) ? $amp_conf["AMPDBENGINE"] : $LEGACY_AMP_DBENGINE;
      $amp_dbfile = isset($amp_conf["AMPDBFILE"]) ? $amp_conf["AMPDBFILE"] : $LEGACY_AMP_DBFILE;
      $amp_dbuser = $amp_conf["AMPDBUSER"];
      $amp_dbpass = $amp_conf["AMPDBPASS"];
      $amp_dbhost = isset($amp_conf["AMPDBHOST"]) ? $amp_conf["AMPDBHOST"] : $LEGACY_AMP_DBHOST;
      $amp_dbname = isset($amp_conf["AMPDBNAME"]) ? $amp_conf["AMPDBNAME"] : $LEGACY_AMP_DBNAME;

      $asteriskcdr_dbengine = $ASTERISKCDR_DBENGINE;
      $asteriskcdr_dbfile = $ASTERISKCDR_DBFILE;
      $asteriskcdr_dbuser = $amp_conf["AMPDBUSER"];
      $asteriskcdr_dbpass = $amp_conf["AMPDBPASS"];
      $asteriskcdr_dbhost = $ASTERISKCDR_DBHOST;
      $asteriskcdr_dbhost = isset($amp_conf["AMPDBHOST"]) ? $amp_conf["AMPDBHOST"] : $ASTERISKCDR_DBHOST;
      $asteriskcdr_dbname = $ASTERISKCDR_DBNAME;

      unset($amp_conf);
    } 
  }

  // asterisk manager interface (berkeley database I think)
  global $asterisk_manager_interface;
  $asterisk_manager_interface = new AsteriskManagerInterface();

  $success = $asterisk_manager_interface->Connect($mgrhost,$mgruser,$mgrpass);
  if (!$success) {
    $_SESSION['ari_error'] =  
      _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" . 
      _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" . 
      _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" .
      _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver.");
    return FALSE;
  }

  // pear interface databases
  $db = new Database();

  // AMP asterisk database
  if (!$STANDALONE['use']) {
    $_SESSION['dbh_asterisk'] = $db->logon($amp_dbengine,
                                           $amp_dbfile,
                                           $amp_dbuser, 
                                           $amp_dbpass,
                                           $amp_dbhost,
                                           $amp_dbname);
    if (!isset($_SESSION['dbh_asterisk'])) {
      $_SESSION['ari_error'] .= _("Cannot connect to the $amp_dbname database") . "<br>" .
                               _("Check AMP installation, asterisk, and ARI main.conf");
      return FALSE;
    }
  }

  // cdr database
  if (in_array('callmonitor',array_keys($loaded_modules))) {
    $_SESSION['dbh_cdr'] = $db->logon($asteriskcdr_dbengine,
                                      $asteriskcdr_dbfile,
                                      $asteriskcdr_dbuser, 
                                      $asteriskcdr_dbpass,
                                      $asteriskcdr_dbhost,
                                      $asteriskcdr_dbname);
    if (!isset($_SESSION['dbh_cdr'])) {
      $_SESSION['ari_error'] .= sprintf(_("Cannot connect to the $asteriskcdr_dbname database"),$asteriskcdr_dbname) . "<br>" .
                               _("Check AMP installation, asterisk, and ARI main.conf");
      return FALSE;
    }
  }

  return TRUE;
}

/**
 * Logout if needed for any databases
 */
function databaseLogoff() {

  global $asterisk_manager_interface;

  $asterisk_manager_interface->Disconnect();
}

/*
 * Checks if user is set and sets
 */
function loginBlock() {

  $login = new Login();

  if (isset($_REQUEST['logout'])) {
    $login->Unauth();
  }

  if (!isset($_SESSION['ari_user'])) {
    $login->Auth();

  }

  if (!isset($_SESSION['ari_user'])) {

    // login form
    $ret .= $login->GetForm();

    return $ret;
  }
}

/*
 * Main handler for website
 */
function handleBlock() {

  global $ARI_NO_LOGIN;

  global $loaded_modules;

  // check errors here and in login block
  $content .= checkErrorMessage();

  // check logout
  if ($_SESSION['ari_user'] && !$ARI_NO_LOGIN) {
    $logout = 1;
  }

  // if nothing set goto user default page
  if (!isset($_REQUEST['m'])) {
    $_REQUEST['m'] = $_SESSION['ari_user']['default_page'];
  }
  // if not function specified then use display page function
  if (!isset($_REQUEST['f'])) {
    $_REQUEST['f'] = 'display';
  }

  $m = $_REQUEST['m'];     // module
  $f = $_REQUEST['f'];     // function
  $a = $_REQUEST['a'];     // action

  // set arguments
  $args = array();
  foreach($_REQUEST as $key => $value) {
    $args[$key] = $value;
  }

  // set rank
  $ranked_modules = array();
  foreach ($loaded_modules as $module) {

    $module_methods = get_class_methods($module);    // note that PHP4 returns all lowercase
    while (list($index, $value) = each($module_methods)) {
      $module_methods[strtolower($index)] = strtolower($value);
    }
    reset($module_methods);
        
    $rank = 99999;
    $rank_function = "rank";
    if (in_array(strtolower($rank_function), $module_methods)) {
      $rank = $module->$rank_function(); 
    }

    $ranked_modules[$rank] = $module;
  }
  ksort($ranked_modules);

  // process modules
  foreach ($ranked_modules as $module) {

    // process module
    $name = get_class($module);    // note PHP4 returns all lowercase
    $module_methods = get_class_methods($module);    // note PHP4 returns all lowercase
    while (list($index, $value) = each($module_methods)) {
      $module_methods[strtolower($index)] = strtolower($value);
    }
    reset($module_methods);

    // init module
    $module->init();

    // add nav menu items
    $nav_menu_function = "navMenu";
    if (in_array(strtolower($nav_menu_function), $module_methods)) {
      $nav_menu .= $module->$nav_menu_function($args); 
    }      

    if (strtolower($m)==strtolower($name)) {

      // build sub menu 
      $subnav_menu_function = "navSubMenu";
      if (in_array(strtolower($subnav_menu_function), $module_methods)) {
        $subnav_menu .= $module->$subnav_menu_function($args); 
      }

      // execute function (usually to build content)
      if (in_array(strtolower($f), $module_methods)) {
        $content .= $module->$f($args);
      }
    }
  }

  // add logout link
  if ($logout != '') { 
    $nav_menu .= "<p><small><small><a href='" . $_SESSION['ARI_ROOT'] . "?logout=1'>" . _("Logout") . "</a></small></small></p>";
  } 

  // error message if no content
  if (!$content) {
    $content .= _("Page Not Found.");
  } 

  return array($nav_menu,$subnav_menu,$content);
}

/*
 * Main handler for website
 */
function handler() {

  global $ARI_VERSION;

  // version
  $ari_version = $ARI_VERSION;

  // check error
  $error = $_SESSION['ari_error'];

  // load modules
  loadModules();

  // login to database
  $success = databaseLogon();
  if ($success) {

    // check if login is needed
    $content = loginBlock();
    if (!isset($content)) {
        list($nav_menu,$subnav_menu,$content) = handleBlock();
    }
  }
  else {

    $display = new Display();

    $content .= $display->displayHeaderText("ARI");
    $content .= $display->displayLine();
    $content .= checkErrorMessage();
  }

  // log off any databases needed
  databaseLogoff();

  // check for ajax request and refresh or if not build the page
  if (isset($_REQUEST['ajax_refresh']) ) {

    echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
      <response>
        <nav_menu><![CDATA[" . $nav_menu . "]]></nav_menu>
        <subnav_menu><![CDATA[" . $subnav_menu . "]]></subnav_menu>
        <content><![CDATA[" . $content . "]]></content>
      </response>";
  }
  else {

    // build the page
    include_once("./theme/page.tpl.php"); 
  }
}

/**
 * Includes and run functions
 */  

// create asterisk manager interface singleton
$asterisk_manager_interface = '';

// array to keep track of loaded modules
$loaded_modules = array();

include_once("./includes/asi.php");
include_once("./includes/database.php");
include_once("./includes/display.php"); 
include_once("./includes/ajax.php");

include_once("./includes/freeside.class.php");

?>