Changeset 13391

Show
Ignore:
Timestamp:
02/02/12 11:03:59 (1 year ago)
Author:
mbrevda
Message:

re #4909 - reuse cdrdb object. Thanks jdccdevel. Please test this and report back!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • freepbx/branches/2.10/amp_conf/htdocs/recordings/includes/bootstrap.php

    r12950 r13391  
    33/** 
    44 * @file 
    5  * Functions that need to be loaded on every request. 
    6  */ 
    7  
    8 /** 
    9  * Sets doc root 
    10  */ 
    11 function setARIRoot() { 
    12  
    13   $found = 0; 
    14   if (isset($_SERVER['PHP_SELF'])) { 
    15     if ($_SERVER['PHP_SELF']!='') { 
    16       $_SESSION['ARI_ROOT'] = $_SERVER['PHP_SELF']; 
    17     } 
    18   } 
    19    
    20   if (!$found) { 
    21     $_SESSION['ARI_ROOT'] = "index.php"; 
    22   } 
    23 
    24  
    25 /** 
    26  * Return a arguments. 
    27  * 
    28  * @param $args 
    29  *   The name of the array being acted upon. 
    30  * @param $name 
    31  *   The name of the variable to return. 
    32  * @return 
    33  *   The value of the variable. 
    34  */ 
    35 function getArgument($args, $name) { 
    36  
    37   return isset($args[$name]) ? $args[$name] : ''; 
    38 
     5 * common functions - core handler 
     6 */ 
    397 
    408/* 
    41  * Gets top level directory names  
    42  * 
    43  * @param $path 
    44  *   directory to search 
    45  * @param $filter 
    46  *   string to use as a filter to match files to return 
    47  * @return $directories 
    48  *   directories found 
    49  */ 
    50 function getDirectories($path,$filter) { 
    51  
    52   $directories = array(); 
    53  
    54   if (is_dir($path)) { 
    55  
    56     $dh = opendir($path); 
    57     while (false!== ($item = readdir($dh))) { 
    58       if($item!="." && $item!="..") { 
    59  
    60         $path = fixPathSlash($path); 
    61         $directory = $path; 
    62         $directory = appendPath($directory,$item); 
    63  
    64         if (is_dir($directory)) { 
    65  
    66           $found = 0; 
    67           if ($filter) { 
    68             if (strpos($directory,$filter)) { 
     9 * Checks if user is set and sets 
     10 */ 
     11function checkErrorMessage() { 
     12  $ret = ''; 
     13 
     14  if ($_SESSION['ari_error']) { 
     15    $ret = "<div class='error'> 
     16               " . $_SESSION['ari_error'] . " 
     17             </div> 
     18             <br>"; 
     19    unset($_SESSION['ari_error']); 
     20  } 
     21 
     22  return $ret; 
     23
     24 
     25/* 
     26 * Checks modules directory, and configuration, and loaded modules  
     27 */ 
     28function loadModules() { 
     29 
     30  global $ARI_ADMIN_MODULES; 
     31  global $ARI_DISABLED_MODULES; 
     32 
     33  global $loaded_modules; 
     34 
     35  $modules_path = "./modules"; 
     36  if (is_dir($modules_path)) { 
     37 
     38    $filter = ".module"; 
     39    $recursive_max = 1; 
     40    $recursive_count = 0; 
     41    $files = getFiles($modules_path,$filter,$recursive_max,$recursive_count); 
     42 
     43    foreach($files as $key => $path) { 
     44 
     45      // build module object  
     46      include_once($path);  
     47      $path_parts = pathinfo($path); 
     48      list($name,$ext) = preg_split("/\./",$path_parts['basename']); 
     49 
     50      // check for module and get rank 
     51      if (class_exists($name)) { 
     52 
     53        $module = new $name(); 
     54 
     55        // check if admin module 
     56        $found = 0; 
     57        if ($ARI_ADMIN_MODULES) { 
     58          $admin_modules = preg_split('/,/',$ARI_ADMIN_MODULES); 
     59          foreach ($admin_modules as $key => $value) { 
     60            if ($name==$value) { 
    6961              $found = 1; 
     62              break; 
    7063            } 
    71           } else { 
    72             $found = 1; 
    73           } 
    74           if ($found) { 
    75             $directories[count($directories) + 1] = $directory; 
    7664          } 
    7765        } 
    78       } 
    79     }  
    80   } 
    81  
    82   return $directories; 
    83 
    84  
    85 /* 
    86  * Gets file names recursively 6 folders deep 
    87  * 
    88  * @param $path 
    89  *   directory to search 
    90  * @param $filter 
    91  *   string to use as a filter to match files to return 
    92  * @param $recursive_max 
    93  *   max number of sub folders to search 
    94  * @param $recursive_count 
    95  *   current sub folder count 
    96  * @return $files 
    97  *   files found 
    98  */ 
    99 function getFiles($path,$filter,$recursive_max,$recursive_count) { 
    100   global $SETTINGS_MAX_FILES; 
    101   $SETTINGS_MAX_FILES = isset($SETTINGS_MAX_FILES) ? $SETTINGS_MAX_FILES : 3000; 
    102   $fileCount=0; 
    103   $files = array(); 
    104  
    105   if (@is_dir($path) && @is_readable($path)) { 
    106     $dh = opendir($path); 
    107     while (false!== ($item = readdir($dh))) { 
    108       if($item[0]!=".") { 
    109  
    110         $path = fixPathSlash($path); 
    111         $msg_path = appendPath($path,$item); 
    112  
    113         $fileCount++; 
    114         if ($fileCount>$SETTINGS_MAX_FILES) { 
    115           $_SESSION['ari_error']  
    116             .= sprintf(_("Too many files in %s. Not all files processed"),$msg_path) . "<br>"; 
    117           return; 
    118         } 
    119  
    120         if ($recursive_count<$recursive_max && is_dir($msg_path)) { 
    121  
    122           $dirCount++; 
    123           if ($dirCount>40) { 
    124             $_SESSION['ari_error']  
    125               .= sprintf(_("Too many directories in %s. Not all files processed"),$msg_path) . "<br>"; 
    126             return; 
    127           } 
    128  
    129           $count = $recursive_count + 1; 
    130           $path_files = getFiles($msg_path,$filter,$recursive_max,$count); 
    131           $files = array_merge($files,$path_files); 
    132         }  
    133         else { 
    134           $found = 0; 
    135           if ($filter) { 
    136             if (strpos($msg_path,$filter)) { 
    137               $found = 1; 
     66 
     67        // check if disabled module 
     68        $disabled = 0; 
     69        if ($ARI_DISABLED_MODULES) { 
     70          $disabled_modules = preg_split('/,/',$ARI_DISABLED_MODULES); 
     71          foreach ($disabled_modules as $key => $value) { 
     72            if ($name==$value) { 
     73              $disabled = 1; 
     74              break; 
    13875            } 
    139           } else { 
    140             $found = 1; 
    141           } 
    142           if ($found) { 
    143             $files[count($files) + 1] = $msg_path; 
    14476          } 
    14577        } 
     78 
     79        // if not admin module or admin user add to module name to array 
     80        if (!$disabled && (!$found || $_SESSION['ari_user']['admin'])) { 
     81          $loaded_modules[$name] = $module; 
     82        } 
    14683      } 
    147     }  
    148   } 
    149  
    150   return $files; 
    151 
    152  
    153 /* Utilities */ 
     84    } 
     85  } 
     86  else { 
     87    $_SESSION['ari_error'] = _("$path not a directory or not readable"); 
     88  } 
     89
    15490 
    15591/** 
    156  * Fixes the path for a trailing slash 
    157  * 
    158  * @param $path 
    159  *   path to append 
    160  * @return $ret 
    161  *   path to returned 
    162  */ 
    163 function fixPathSlash($path) { 
    164  
    165   $ret = $path; 
    166  
    167   $slash = ''; 
    168   if (!preg_match('/\/$/',$path)) { 
    169     $slash = '/'; 
     92 * Builds database connections 
     93 */ 
     94function databaseLogon() { 
     95  global $AMPORTAL_CONF_FILE, 
     96      $AMP_FUNCTIONS_FILES, 
     97      $ARI_ADMIN_PASSWORD, 
     98      $ARI_ADMIN_USERNAME, 
     99      $ARI_DISABLED_MODULES, 
     100      $ASTERISKMGR_DBHOST, 
     101      $LEGACY_AMP_DBENGINE, 
     102      $LEGACY_AMP_DBFILE, 
     103      $LEGACY_AMP_DBHOST, 
     104      $LEGACY_AMP_DBNAME, 
     105      $amp_conf,  
     106      $amp_conf_defaults,  
     107      $amp_usedevstate, 
     108      $ariadminpassword, 
     109      $ariadminusername, 
     110      $asterisk_manager_interface,  
     111      $astman,  
     112      $cdrdb,  
     113      $db,  
     114      $loaded_modules, 
     115      $STANDALONE; 
     116 
     117 
     118  // get user 
     119  if ($STANDALONE['use']) { 
     120    $mgrhost = $ASTERISKMGR_DBHOST; 
     121    $mgruser = $STANDALONE['asterisk_mgruser']; 
     122    $mgrpass = $STANDALONE['asterisk_mgrpass']; 
     123  } else { 
     124 
     125    $ariadminusername = isset($amp_conf["ARI_ADMIN_USERNAME"]) ? $amp_conf["ARI_ADMIN_USERNAME"] : $ARI_ADMIN_USERNAME; 
     126    $ariadminpassword = isset($amp_conf["ARI_ADMIN_PASSWORD"]) ? $amp_conf["ARI_ADMIN_PASSWORD"] : $ARI_ADMIN_PASSWORD; 
     127    $mgrhost = $ASTERISKMGR_DBHOST; 
     128    $mgruser = $amp_conf['AMPMGRUSER']; 
     129    $mgrpass = $amp_conf['AMPMGRPASS']; 
     130     
     131    $amp_usedevstate = isset($amp_conf["USEDEVSTATE"]) ? strtolower(trim($amp_conf["USEDEVSTATE"])) : 0; 
     132    if ($amp_usedevstate == 'yes' || $amp_usedevstate == 'true' || $amp_usedevstate == 'on' || $amp_usedevstate == '1') { 
     133      $amp_usedevstate = 1; 
     134    } else { 
     135      $amp_usedevstate = 0; 
     136    } 
     137 
     138  } 
     139 
     140  $asterisk_manager_interface = new AsteriskManagerInterface(); 
     141 
     142  $success = $asterisk_manager_interface->Connect($mgrhost,$mgruser,$mgrpass); 
     143  if (!$success) { 
     144    $_SESSION['ari_error'] =   
     145      _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" .  
     146      _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" .  
     147      _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" . 
     148      _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver."); 
     149    return FALSE; 
     150  } 
     151 
     152  if (!isset($astman) || !$astman) { 
     153    // couldn't connect to astman 
     154    $_SESSION['ari_error'] = 
     155      _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" . 
     156      _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" . 
     157      _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" . 
     158      _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver."); 
     159  } 
     160 
     161  if (isset($db) && $db) { 
     162    $_SESSION['dbh_asterisk'] = $db; 
     163  } 
     164 
     165  // cdr database 
     166  if (isset($cdrdb) && $cdrdb) { 
     167    $_SESSION['dbh_cdr'] = $cdrdb; 
     168  } 
     169   
     170  return TRUE; 
     171
     172 
     173/** 
     174 * Logout if needed for any databases 
     175 */ 
     176function databaseLogoff() { 
     177 
     178  global $asterisk_manager_interface; 
     179  global $astman; 
     180 
     181  $asterisk_manager_interface->Disconnect(); 
     182 
     183  if (is_object($astman)) 
     184  { 
     185    $astman->logoff(); 
     186    $astman->disconnect(); 
     187  } 
     188  unset($astman); 
     189
     190 
     191/* 
     192 * Checks if user is set and sets 
     193 */ 
     194function loginBlock() { 
     195 
     196  $login = new Login(); 
     197 
     198  if (isset($_REQUEST['logout'])) { 
     199    $login->Unauth(); 
     200  } 
     201 
     202  if (!isset($_SESSION['ari_user'])) { 
     203    $login->Auth(); 
     204 
     205  } 
     206 
     207  if (!isset($_SESSION['ari_user'])) { 
     208 
     209    // login form 
     210    $ret .= $login->GetForm(); 
     211 
     212    return $ret; 
     213  } 
     214
     215 
     216/* 
     217 * Main handler for website 
     218 */ 
     219function handleBlock() { 
     220 
     221  global $ARI_NO_LOGIN; 
     222 
     223  global $loaded_modules; 
     224 
     225  $nav_menu = ''; 
     226  $subnav_menu = ''; 
     227 
     228  // check errors here and in login block 
     229  $content = checkErrorMessage(); 
     230 
     231  // check logout 
     232  if ($_SESSION['ari_user'] && !$ARI_NO_LOGIN) { 
     233    $logout = 1; 
     234  } 
     235 
     236  // if nothing set goto user default page 
     237  if (!isset($_REQUEST['m'])) { 
     238    $_REQUEST['m'] = $_SESSION['ari_user']['default_page']; 
     239  } 
     240  // if not function specified then use display page function 
     241  if (!isset($_REQUEST['f'])) { 
     242    $_REQUEST['f'] = 'display'; 
     243  } 
     244 
     245  $m = $_REQUEST['m'];     // module 
     246  $f = $_REQUEST['f'];     // function 
     247  $a = isset($_REQUEST['a']) ? $_REQUEST['a'] : '';     // action 
     248 
     249  // set arguments 
     250  $args = array(); 
     251  foreach($_REQUEST as $key => $value) { 
     252    $args[$key] = $value; 
     253  } 
     254 
     255  // set rank 
     256  $ranked_modules = array(); 
     257  ksort($loaded_modules); 
     258  foreach ($loaded_modules as $module) { 
     259 
     260    $module_methods = get_class_methods($module);    // note that PHP4 returns all lowercase 
     261    while (list($index, $value) = each($module_methods)) { 
     262      $module_methods[strtolower($index)] = strtolower($value); 
     263    } 
     264    reset($module_methods); 
     265         
     266    $rank = 99999; 
     267    $rank_function = "rank"; 
     268    if (in_array(strtolower($rank_function), $module_methods)) { 
     269      $rank = $module->$rank_function();  
     270    } 
     271 
     272    $ranked_modules[$rank][] = $module; 
     273  } 
     274  ksort($ranked_modules); 
     275 
     276  // process modules 
     277  foreach ($ranked_modules as $rank => $modules) { 
     278  $rankloaded = false; //wether this rank has any menu items 
     279  foreach ($modules as $module)     { 
     280    $nmenu = false; //text/link that goes in the menu 
     281      // process module 
     282      $name = get_class($module);    // note PHP4 returns all lowercase 
     283      $module_methods = get_class_methods($module);    // note PHP4 returns all lowercase 
     284      while (list($index, $value) = each($module_methods)) { 
     285          $module_methods[strtolower($index)] = strtolower($value); 
     286      } 
     287      reset($module_methods); 
     288 
     289      // init module 
     290      $module->init(); 
     291 
     292      // add nav menu items 
     293      $nav_menu_function = "navMenu"; 
     294      if (in_array(strtolower($nav_menu_function), $module_methods)) { 
     295      $nmenu = $module->$nav_menu_function($args); 
     296          //$nav_menu .= $module->$nav_menu_function($args);  
     297      $nav_menu .= $nmenu; 
     298      }       
     299 
     300      if (strtolower($m)==strtolower($name)) { 
     301 
     302        // build sub menu  
     303        $subnav_menu_function = "navSubMenu"; 
     304        if (in_array(strtolower($subnav_menu_function), $module_methods)) { 
     305          $subnav_menu .= $module->$subnav_menu_function($args);  
     306        } 
     307 
     308        // execute function (usually to build content) 
     309        if (in_array(strtolower($f), $module_methods)) { 
     310          $content .= $module->$f($args); 
     311        } 
     312    } 
     313   
     314   if ($nmenu != false){ 
     315    $nav_menu .= '<br />'; 
     316    $rankloaded = true; 
     317  } 
     318
     319  if ($rankloaded) { 
     320    $nav_menu .= '<br />'; 
     321  } 
     322  } 
     323 
     324  // add logout link 
     325  if ($logout != '') {  
     326    $nav_menu .= "<small><small><a href='" . $_SESSION['ARI_ROOT'] . "?logout=1'>" . _("Logout") . "</a></small></small>"; 
    170327  }  
    171   $ret .= $slash; 
    172  
    173   return $ret;  
    174 
    175  
    176 /** 
    177  * Appends folder to end of path 
    178  * 
    179  * @param $path 
    180  *   path to append 
    181  * @param $folder 
    182  *   folder to append to path 
    183  * @return $ret 
    184  *   path to returned 
    185  */ 
    186 function appendPath($path,$folder) { 
    187  
    188   $ret = $path; 
    189  
    190   $m = ''; 
    191   if (!preg_match('/\/$/',$path)) { 
    192     $m = '/'; 
     328 
     329  // error message if no content 
     330  if (!$content) { 
     331    $content .= _("Page Not Found."); 
    193332  }  
    194   $ret .= $m . $folder;  
    195  
    196   return $ret; 
    197 
    198  
    199 /** 
    200  * Get Date format  
    201  * 
    202  * @param $timestamp 
    203  *   timestamp to be converted 
    204  */ 
    205 function getDateFormat($timestamp) { 
    206   return date('Y-m-d', $timestamp); 
    207 
    208  
    209 /** 
    210  * Get time format  
    211  * 
    212  * @param $timestamp 
    213  *   timestamp to be converted 
    214  */ 
    215 function getTimeFormat($timestamp) {    
    216   return date('G:i:s', $timestamp); 
    217 
    218  
    219 /* */ 
    220  
    221 /** 
    222  * Checks ARI dependencies 
    223  */ 
    224 function checkDependencies() { 
    225  
    226   // check for PHP 
    227   if (!version_compare(phpversion(), '4.3', '>=')) { 
    228     echo _("ARI requires a version of PHP 4.3 or later"); 
    229     exit(); 
    230   } 
    231  
    232   // check for PEAR 
    233   $include_path = ini_get('include_path'); 
    234   $buf = preg_split('/:|,/',$include_path); 
    235  
    236   $found = 0; 
    237   foreach ($buf as $path) { 
    238     $path = fixPathSlash($path); 
    239     $pear_check_path = $path . "DB.php"; 
    240     if (is_file($pear_check_path)) { 
    241       $found = 1; 
    242       break; 
     333 
     334  return array($nav_menu,$subnav_menu,$content); 
     335
     336 
     337/* 
     338 * Main handler for website 
     339 */ 
     340function handler() { 
     341 
     342  global $ARI_VERSION, $amp_conf; 
     343 
     344  // version 
     345  $ari_version = $ARI_VERSION; 
     346 
     347  // check error 
     348  $error = $_SESSION['ari_error']; 
     349 
     350  // load modules 
     351  loadModules(); 
     352 
     353  // login to database 
     354  $success = databaseLogon(); 
     355 
     356  if ($success) { 
     357 
     358    // check if login is needed 
     359    $content = loginBlock(); 
     360    if (!isset($content)) { 
     361        list($nav_menu,$subnav_menu,$content) = handleBlock(); 
    243362    } 
    244363  } 
    245  
    246   if (!$found) { 
    247     echo _("PHP PEAR must be installed.  Visit http://pear.php.net for help with installation."); 
    248     exit(); 
    249   } 
    250 
    251  
    252 /** 
    253  * Starts the session 
    254  */ 
    255 function startARISession() { 
    256  
    257   if (!isset($_SESSION['ari_user']) ) { 
    258  
    259     // start a new session for the user  
    260     ini_set('session.name', 'ARI');             // prevent session name clashes 
    261     ini_set('session.gc_maxlifetime', '3900');  // make the session timeout a long time 
    262     set_time_limit(360); 
    263     session_start(); 
    264   $_SESSION['ari_error'] = ''; 
    265   } 
    266 
    267  
    268 /** 
    269  * Bootstrap 
    270  * 
    271  * Loads critical variables needed for every page request 
    272  * 
    273  */ 
    274 function bootstrap() { 
    275  
    276   // set error reporting 
    277 //  error_reporting (E_ALL & ~ E_NOTICE);   
    278 
    279  
    280 /** 
    281  * Set HTTP headers in preparation for a page response. 
    282  * 
    283  * TODO: Figure out caching 
    284  */ 
    285 function ariPageHeader() { 
    286 header('Content-type: text/html; charset=utf-8'); 
    287   bootstrap(); 
    288 
    289  
    290 /** 
    291  * Perform end-of-request tasks. 
    292  * 
    293  * This function sets the page cache if appropriate, and allows modules to 
    294  * react to the closing of the page by calling hook_exit(). 
    295  */ 
    296 function ariPageFooter() { 
    297  
    298 
    299  
    300 /** 
    301  * Initiate FreePBX bootstrap environment 
    302  */ 
    303 $bootstrap_settings['freepbx_auth'] = false; 
    304 if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) { 
    305   include_once('/etc/asterisk/freepbx.conf'); 
     364  else { 
     365 
     366    $display = new Display(); 
     367   
     368  $content = ''; 
     369    $content .= $display->displayHeaderText("ARI"); 
     370    $content .= $display->displayLine(); 
     371    $content .= checkErrorMessage(); 
     372  } 
     373 
     374  // log off any databases needed 
     375  databaseLogoff(); 
     376 
     377  // check for ajax request and refresh or if not build the page 
     378  if (isset($_REQUEST['ajax_refresh']) ) { 
     379 
     380    echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?> 
     381      <response> 
     382        <nav_menu><![CDATA[" . $nav_menu . "]]></nav_menu> 
     383        <subnav_menu><![CDATA[" . $subnav_menu . "]]></subnav_menu> 
     384        <content><![CDATA[" . $content . "]]></content> 
     385      </response>"; 
     386  } 
     387  else { 
     388 
     389    // build the page 
     390    include_once("./theme/page.tpl.php");  
     391  } 
    306392} 
    307393 
    308394/** 
    309395 * Includes and run functions 
    310  */ 
    311  
    312 include_once("./includes/lang.php"); 
    313 $language = new Language(); 
    314 $language->set(); 
    315  
    316 checkDependencies(); 
    317 startARISession(); 
    318 setARIRoot(); 
    319  
    320 include_once("./includes/common.php"); 
    321 include_once("./includes/main.conf.php"); 
    322 include_once("./version.php"); 
    323 include_once("./includes/crypt.php");  
    324 include_once("./includes/login.php"); 
     396 */   
     397 
     398// create asterisk manager interface singleton 
     399$asterisk_manager_interface = ''; 
     400 
     401// array to keep track of loaded modules 
     402$loaded_modules = array(); 
     403 
     404include_once("./includes/asi.php"); 
     405include_once("./includes/database.php"); 
     406include_once("./includes/display.php");  
     407include_once("./includes/ajax.php"); 
     408include_once("./includes/callme.php"); 
    325409 
    326410?>