Changeset 6833

Show
Ignore:
Timestamp:
09/22/08 10:10:27 (2 years ago)
Author:
sasargen
Message:

ARI: update callme feature to use phpagi-asmanager for Asterisk Manager Interface

Files:

Legend:

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

    r6421 r6833  
    99 
    1010/* 
     11 * 
     12 * Include admin/functions.inc.php for our call to parse_amportal_conf() 
     13 * Include admin/common/php-asmanager.php to use the AGI_AsteriskManager class for accessing the AMI 
     14 * 
     15 */ 
     16require_once('../admin/functions.inc.php'); 
     17require_once('../admin/common/php-asmanager.php'); 
     18 
     19$amp_conf       = parse_amportal_conf("/etc/amportal.conf"); 
     20$asm            = new AGI_AsteriskManager(); 
     21// attempt to connect to asterisk manager proxy 
     22if (!isset($amp_conf["ASTMANAGERPROXYPORT"]) || !$res = $asm->connect("127.0.0.1:".$amp_conf["ASTMANAGERPROXYPORT"], $amp_conf["AMPMGRUSER"] , $amp_conf["AMPMGRPASS"], 'off')) 
     23{ 
     24        // attempt to connect directly to asterisk, if no proxy or if proxy failed 
     25        if (!$res = $asm->connect("127.0.0.1:".$amp_conf["ASTMANAGERPORT"], $amp_conf["AMPMGRUSER"] , $amp_conf["AMPMGRPASS"], 'off')) 
     26        { 
     27                // couldn't connect at all 
     28                unset( $asm ); 
     29                $_SESSION['ari_error'] = 
     30                _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" . 
     31                _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" . 
     32                _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" . 
     33                _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver."); 
     34        } 
     35} 
     36 
     37function callme_close() 
     38{ 
     39        global $asm; 
     40        if (is_object($asm)) 
     41        { 
     42                $asm->logoff(); 
     43                $asm->disconnect(); 
     44        } 
     45        unset($asm); 
     46} 
     47/* 
    1148 * Call Me functions 
    1249 */ 
    13 /* Return the call me number stored in the database. */ 
     50 /* Return the call me number stored in the database. */ 
    1451function callme_getnum($exten) 
    1552{ 
    16         global $asterisk_manager_interface; 
     53        global $asm; 
     54        $cmd            = "database get AMPUSER $exten/callmenum"; 
     55        $callme_num     = ''; 
     56        $results        = $asm->Command($cmd); 
    1757 
    18         $action = "Action: Command\r\nCommand: database get AMPUSER $exten/callmenum\r\n\r\n"; 
    19         $callme_num = $asterisk_manager_interface->Command($action); 
     58        if (is_array($results)) 
     59        { 
     60                foreach ($results as $results_elem) 
     61                { 
     62                        if (preg_match('/Value: [^\s]*/', $results_elem, $matches) != 0) 
     63                        { 
     64                                $parts = split(' ', trim($matches[0])); 
     65                                $callme_num = $parts[1]; 
     66                        } 
     67 
     68                } 
     69        } 
     70 
    2071        return $callme_num; 
    2172} 
     
    2475function callme_setnum($exten, $callme_num) 
    2576{ 
    26         global $asterisk_manager_interface
     77        global $asm
    2778 
    28         $action = "Action: Command\r\nCommand: database put AMPUSER $exten/callmenum $callme_num\r\n\r\n"; 
    29         $asterisk_manager_interface->Command($action); 
     79        $cmd = "database put AMPUSER $exten/callmenum $callme_num"; 
     80        $asm->Command($cmd); 
    3081        return; 
    31 } 
    32  
    33 /* Send an action directly to the ami.  No return value.*/ 
    34 function callme_sendcmd($action) 
    35 { 
    36         global $asterisk_manager_interface; 
    37         $socket = $asterisk_manager_interface->socket; 
    38          
    39         fwrite($socket, $action); 
    40         fflush($socket); 
    41         return; 
    4282} 
    4383 
     
    4686function callme_startcall($to, $from, $new_path) 
    4787{ 
    48         $action = "Action: Originate\r\n"; 
    49         $action .= "Channel: Local/$to@from-internal/n\r\n"; 
    50         $action .= "Context: custom-callme\r\n"; 
    51         $action .= "Exten: s\r\n"; 
    52         $action .= "Priority: 1\r\n"; 
    53         $action .= "CallerID: VMAIL/$from\r\n"; 
    54         $action .= "Variable: MSG=$new_path\r\n"; 
    55         $action .= "Variable: MBOX=$from\r\n\r\n"; 
    56  
    57         $status = callme_originate($action); 
    58         return $status; 
     88        global $asm; 
     89        $channel        = "Local/$to@from-internal/n"; 
     90        $context        = "custom-callme"; 
     91        $extension      = "s"; 
     92        $priority       = "1"; 
     93        $callerid       = "VMAIL/$from"; 
     94        $variable       = "MSG=$new_path|MBOX=$from"; 
     95        /* Arguments to Originate: channel, extension, context, priority, timeout, callerid, variable, account, application, data */ 
     96        $status = $asm->Originate($channel, $extension, $context, $priority, NULL, $callerid, $variable, NULL, NULL, NULL); 
     97        if (is_array($status)) 
     98        { 
     99                foreach ($status as $status_elem) 
     100                { 
     101                        if (preg_match('/Originate successfully queued/', $status_elem, $matches) != 0) 
     102                        { 
     103                                return CALLME_SUCCESS; 
     104                        } 
     105                } 
     106        }  
     107        return CALLME_FAILURE; 
    59108} 
    60109 
    61 /* Turns event monitoring off for our ami instance, thereby     */ 
    62 /* facilitating retrieval of responses to actions sent to       */ 
    63 /* the ami (with events off, we can safely expect to only       */ 
    64 /* see results from our actions over the ami and no more).      */ 
    65 /* Called by callme_originate().  Assumed to have been          */ 
    66 /* called BEFORE callme_hangup() is called (see callme_hangup). */ 
    67110function callme_eventsoff() 
    68111{ 
    69         global $asterisk_manager_interface; 
    70         $socket = $asterisk_manager_interface->socket; 
    71         $action = "Action: Events\r\n"; 
    72         $action .= "Eventmask: off\r\n\r\n"; 
    73  
    74         callme_sendcmd($action); 
    75          
    76         /* Take the action's response out of the socket's buffer; do nothing with the response. */ 
    77         while (($buffer=fgets($socket)) && (!preg_match('/Response: Events Off/', $buffer))); 
     112        global $asm; 
     113        $asm->Events("off"); 
    78114        return; 
    79 } 
    80  
    81 /* Helper function called by callme_startcall().  Sends the ORIGINATE action */ 
    82 /* passed to it (this *must* be an Originate action) to the ami via          */ 
    83 /* callme_sendcmd().  Retrieves the Originate response and parses that       */ 
    84 /* response in order to determine state of call (success/failure/error).     */ 
    85 /* Returns state of call.                                                    */ 
    86 function callme_originate($action) 
    87 { 
    88         global $asterisk_manager_interface; 
    89         $socket = $asterisk_manager_interface->socket; 
    90  
    91         $error_pat = '/Response: Error[\s]*/'; 
    92         $success_pat = '/Response: Success[\s]*/'; 
    93         $r = ""; 
    94         $buffer = ""; 
    95         callme_eventsoff(); 
    96         callme_sendcmd($action); 
    97          
    98         /* Continue polling for a result from the action until we get it. */ 
    99         /* (For example, it may take a long time to get a result back     */ 
    100         /* from the Originate action in the case where the phone rings    */ 
    101         /* until the call times out.)  Pull the entire Originate response */ 
    102         /* from the socket's buffer.                                      */ 
    103         while (!preg_match('/Message: Originate[a-zA-Z\s]*/', $buffer)) 
    104         { 
    105                 $buffer = fgets($socket); 
    106                 $r .= $buffer;           
    107         } 
    108  
    109         if (preg_match($error_pat, $r)) 
    110         { 
    111                 return CALLME_FAILURE; 
    112         } else if (preg_match($success_pat, $r)) 
    113         { 
    114                 return CALLME_SUCCESS; 
    115         } else 
    116         { 
    117                 return CALLME_ERROR; 
    118         } 
    119          
    120 } 
    121  
    122 /* Logs us off the ami. */ 
    123 function callme_logoff() 
    124 { 
    125         $action = "Action: Logoff\r\n\r\n"; 
    126         callme_sendcmd($action); 
    127         return; 
    128115} 
    129116 
     
    140127function callme_hangup($exten) 
    141128{ 
    142         global $asterisk_manager_interface; 
    143         $socket = $asterisk_manager_interface->socket; 
    144         $action = "Action: Command\r\nCommand: local show channels\r\n\r\n"; 
    145         $chan_pat = '/[\s]*Local\/' . trim($exten) . '@from\-internal\-[a-zA-Z0-9]*,(1|2)[\s]*/'; 
    146         $matches[0] = ""; 
    147         $response = ""; 
    148         /* Events must be off for callme_sendcmd() to work, but         */ 
    149         /* callme_eventsoff() is called in callme_startcall(), and      */ 
    150         /* callme_hangup() should not be called unless                  */ 
    151         /* callme_startcall() was called first!                         */ 
    152         callme_sendcmd($action); 
    153  
    154         /* Read in entire response belonging to our ami command action. */ 
    155         while (($buffer=fgets($socket)) && (!preg_match('/\-\-END COMMAND\-\-/', $buffer))) 
    156                 $response .= $buffer; 
     129        global $asm; 
     130        $cmd            = "local show channels"; 
     131        $chan_pat       = '/[\s]*Local\/' . trim($exten) . '@from\-internal\-[a-zA-Z0-9]*,(1|2)[\s]*/'; 
     132        $matches[0]     = ""; 
     133        $response       = ""; 
     134        $channel        = ""; 
     135        $local_channels = $asm->Command($cmd); 
    157136 
    158137        /* Look for our local channel. */ 
    159         preg_match($chan_pat, $response, $matches); 
     138        if (is_array($local_channels)) 
     139        { 
     140                foreach ($local_channels as $local_channels_elem) 
     141                { 
     142                        preg_match($chan_pat, $local_channels_elem, $matches); 
     143                        if ($matches[0] != "") 
     144                        { 
     145                                $channel = $matches[0]; 
     146                                break; 
     147                        } 
     148                } 
     149        } else 
     150        { 
     151                $channel = ""; 
     152        } 
    160153 
    161154        /* If the channel was still up, hang it up. */  
    162         if ($matches[0] != "") 
     155        if ($channel != "") 
    163156        { 
    164                 $action = "Action: Hangup\r\nChannel: " . trim($matches[0]) . "\r\n\r\n"; 
    165                 callme_sendcmd($action); 
    166         }        
    167  
     157                $asm->Hangup(trim($channel)); 
     158        } 
    168159        return;  
    169  
    170160} 
    171161?> 
  • freepbx/branches/ari_changes/amp_conf/htdocs/recordings/misc/callme_page.php

    r6650 r6833  
    1010include_once("./includes/common.php"); 
    1111 
    12 function cmpage_ami_open() { 
    13         global $asterisk_manager_interface; 
    14         global $ASTERISKMGR_DBHOST; 
    15         global $STANDALONE; 
    16         global $AMP_FUNCTIONS_FILES; 
    17         global $AMPORTAL_CONF_FILE; 
    18         global $amp_conf_defaults; 
    19         $asterisk_manager_interface = new AsteriskManagerInterface(); 
    20         /* get login credentials */ 
    21         if ($STANDALONE['use']) { 
    22                 $mgrhost = $ASTERISKMGR_DBHOST; 
    23                 $mgruser = $STANDALONE['asterisk_mgruser']; 
    24                 $mgrpass = $STANDALONE['asterisk_mgrpass']; 
    25         } else { 
    26                 $include = 0; 
    27                 $files = split(';',$AMP_FUNCTIONS_FILES); 
    28                 foreach ($files as $file) { 
    29                         if (is_file($file)) { 
    30                                 include_once($file); 
    31                                 $include = 1; 
    32                         } 
    33                 } 
    34  
    35                 if ($include) { 
    36                         $amp_conf = parse_amportal_conf($AMPORTAL_CONF_FILE); 
    37                         $mgrhost = $ASTERISKMGR_DBHOST; 
    38                         $mgruser = $amp_conf['AMPMGRUSER']; 
    39                         $mgrpass = $amp_conf['AMPMGRPASS']; 
    40                         unset($amp_conf); 
    41                 } 
    42         } 
    43  
    44         $success = $asterisk_manager_interface->Connect($mgrhost,$mgruser,$mgrpass); 
    45         if (!$success) { 
    46                 $_SESSION['ari_error'] = 
    47                 _("ARI does not appear to have access to the Asterisk Manager.") . " ($errno)<br>" . 
    48                 _("Check the ARI 'main.conf.php' configuration file to set the Asterisk Manager Account.") . "<br>" . 
    49                 _("Check /etc/asterisk/manager.conf for a proper Asterisk Manager Account") . "<br>" . 
    50                 _("make sure [general] enabled = yes and a 'permit=' line for localhost or the webserver."); 
    51                 return FALSE; /* exit */ 
    52         } 
    53 } 
    54  
    55 function cmpage_ami_close() { 
    56  
    57         global $asterisk_manager_interface; 
    58         $action = "Action: Logoff\r\n\r\n"; 
    59         //$asterisk_manager_interface->Command($action); 
    60         callme_sendcmd($action); 
    61         $asterisk_manager_interface->Disconnect(); 
    62         unset($asterisk_manager_interface); 
    63 } 
    6412?> 
    6513 
     
    7321 
    7422<?php 
    75         global $ARI_CRYPT_PASSWORD; 
    76         $crypt = new Crypt(); 
    77         $path           = $crypt->decrypt($_REQUEST['recording'],$ARI_CRYPT_PASSWORD); 
    78         $to             = $crypt->decrypt($_REQUEST['callmenum'],$ARI_CRYPT_PASSWORD); 
    79         $pageaction     = $_REQUEST['action']; 
    80         $msgFrom        = $crypt->decrypt($_REQUEST['msgFrom'],$ARI_CRYPT_PASSWORD); 
    81         $new_path = substr($path, 0, -4);               /* Without the sound file extension. */ 
     23        $path       = $_SESSION['ari_user']['recfiles'][$_REQUEST['recindex']]; 
     24        $pageaction = $_REQUEST['action']; 
     25        $to         = $_REQUEST['callmenum']; 
     26        $msgFrom    = $_REQUEST['msgFrom']; 
     27        $new_path   = substr($path, 0, -4);             /* Without the sound file extension. */ 
    8228        $matches[0] = ''; /* init the $matches array. */ 
    8329        /* Either start or end the call me call */ 
     
    8632                case "c": 
    8733                        /* Call me. */ 
    88                         cmpage_ami_open(); 
    8934                        $call_status = callme_startcall($to, $msgFrom, $new_path); 
    9035                        echo("<table class='voicemail' style='width: 100%; height: 100%; margin: 0 0 0 0; border: 0px; padding: 0px'><tr><td valign='middle' style='border: 0px'>"); 
     
    9237                        if (callme_succeeded($call_status)) 
    9338                        { 
    94                                 echo("<a href='callme_page.php?action=h&callmenum=" . urlencode($crypt->encrypt($to, $ARI_CRYPT_PASSWORD)) . "'>Click here to hang up.</a>"); 
     39                                echo("<a href='callme_page.php?action=h&callmenum=" . $to . "'>Click here to hang up.</a>"); 
    9540                        } 
    9641                        echo("</td></tr></table>"); 
    9742                        echo("<script language='javascript'>parent.document.getElementById('callme_status').innerHTML = '" . _("$call_status") . "';</script>"); 
    98                         cmpage_ami_close(); 
    9943                        echo("<script language='javascript'>parent.document.getElementById('pb_load_inprogress').value='false';</script>"); 
    10044                        echo("<script language='javascript'>parent.document.getElementById('callme_status').parentNode.style.backgroundColor = 'white';</script>"); 
     
    10246                case "h": 
    10347                        /* Hang up. */ 
    104                         cmpage_ami_open(); 
    10548                        /* Find the channel and hang it up if it still exists. */ 
    10649                        callme_hangup($to); 
    107                         cmpage_ami_close(); 
    10850                        echo("<script language='javascript'>parent.document.getElementById('callme_status').innerHTML = '" . _("The call was terminated.") . "';</script>"); 
    10951                        break;