Asternic CDR Reports. Listening to calls on FreePBX with access restrictions

Software version


FreePBX 2.11.0.41
Asternic CDR Reports 1.5.1

Introduction


Task: it is necessary to give a person the opportunity to listen to the recordings of conversations, but strictly on a certain range of internal extensions. We are trying to create a new administrator and even prescribe the Extension Range to him, hoping in this way to give him limited access to listening.



But then, going under the created account and going to the call reports, we realize that they failed - CDR Reports ignores the specified Extension Range and displays information on all numbers.

After scratching in the back of the head, we are looking for an alternative reporting module - and, lo and behold, we find it: Asternic CDR Reports. It is wonderful and works only with numbers whose range is limited to an authorized administrator.



But what is it? No listening to calls? Does the Listen column display voicemail? No, that won’t work ...

We correct


Information about calls in Asterisk is stored in Mysql in the asteriskcdrdb database, table cdr. This can be seen from the following code taken from the page.cdr.php file of regular CDR Reports

$resultscdr = $dbcdr->getAll($query, DB_FETCHMODE_ASSOC);
...
foreach($resultscdr as $row) 
...
        if ($row['recordingfile']) {
            $rec_parts = explode('-',$row['recordingfile']);
            $fyear = substr($rec_parts[3],0,4);
            $fmonth = substr($rec_parts[3],4,2);
            $fday = substr($rec_parts[3],6,2);
            $monitor_base = $amp_conf['MIXMON_DIR'] ? $amp_conf['MIXMON_DIR'] : $amp_conf['ASTSPOOLDIR'] . '/monitor';
            $recordingfile = "$monitor_base/$fyear/$fmonth/$fday/" . $row['recordingfile'];
            if (!file_exists($recordingfile)) {
                $recordingfile = '';
            }
        } else {
            $recordingfile = '';
        }

So, all we need is to read the value of the recordingfile cell, where the string "filename.wav" is stored and add the full path to this file, which, by the way, is taken from the same file name (year, month, day).

Open the file /var/www/html/admin/modules/asternic_cdr/functions.inc.php of the Asternic module.

Looking for line 154

$query.= "billsec,duration,duration-billsec as ringtime,src,";

and replace with

$query.= "billsec,duration,duration-billsec as ringtime,src,recordingfile,";

So in the query to the database we added the recordingfile field, the value of which, accordingly, will now also be in the resulting array, from which we then take it to form a link to the file.

Looking for line 208

$detail[$row['chan1']].= "\n";
          $uni = $row['uniqueid'];
          $uni = str_replace(".","",$uni);
             if($row['userfield']<>"") {
              $detail[$row['chan1']].="";
              $detail[$row['chan1']].="
"; $detail[$row['chan1']].="pixel"; $detail[$row['chan1']].="
"; $detail[$row['chan1']].=""; $detail[$row['chan1']].="
"; $detail[$row['chan1']].="pixel"; $detail[$row['chan1']].="
"; } else { $detail[$row['chan1']].= " "; } $detail[$row['chan1']].= "\n";


replace with

if ($row['recordingfile']) {
            $rec_parts = explode('-',$row['recordingfile']);
            $fyear = substr($rec_parts[3],0,4);
            $fmonth = substr($rec_parts[3],4,2);
            $fday = substr($rec_parts[3],6,2);
            $monitor_base = $amp_conf['MIXMON_DIR'] ? $amp_conf['MIXMON_DIR'] : $amp_conf['ASTSPOOLDIR'] . '/monitor';
            $recordingfile = "$monitor_base/$fyear/$fmonth/$fday/" . $row['recordingfile'];
            if (!file_exists($recordingfile)) {
                $recordingfile = '';
                $detail[$row['chan1']].= "\n";
            }
            else {
            $detail[$row['chan1']].= "\n\"Call";
            }
        } else {
            $recordingfile = '';
            $detail[$row['chan1']].= "\n";
        } 
          $detail[$row['chan1']].= "\n";

Thus, instead of outputting voicemail in a table cell, we checked for the presence of this call record and, if found, output a small “Play” icon with a link encoded in base64 to the audio recording file itself.



An attentive reader noticed that there is a GET request variable called getRec in the generated link. Since the web server does not have access to the folder with audio, we give the file via php, and to do this at the end of the file functions.inc.php we do check for variable getRec, in which case we refer to the presence of features, bestowing file

Adding code at the very end of the file before, of course, closing the php tag "?>"

function recordfile_uri($path) {
    $size = filesize($path);
    $name = basename($path);
    $extension = strtolower(substr(strrchr($name,"."),1));
    // This will set the Content-Type to the appropriate setting for the file
    $ctype ='';
    switch( $extension ) {
        case "WAV":
            $ctype="audio/x-wav";
            break;
        case "wav":
            $ctype="audio/x-wav";
            break;
        case "ulaw":
            $ctype="audio/basic";
            break;
        case "alaw":
            $ctype="audio/x-alaw-basic";
            break;
        case "sln":
            $ctype="audio/x-wav";
            break;
        case "gsm":
            $ctype="audio/x-gsm";
            break;
        case "g729":
            $ctype="audio/x-g729";
            break;
        default: //not downloadable
            // echo ("404 File not found! foo");
            // TODO: what to do if none of the above work?
        break ;
    }
  $fp=fopen($path, "rb");
  if ($size && $ctype && $fp) {
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: audio file");
    header("Content-Type: " . $ctype);
    header("Content-Disposition: attachment; filename=" . $name);
    header("Content-Transfer-Encoding: binary");
    header("Content-length: " . $size);
    $chunksize = 1*(1024*1024);
    while (!feof($fp)) {
        $buffer = fread($fp, $chunksize);
        echo $buffer;
        ob_flush();
        flush();
    }
    fclose($fp);
  }
}
if(isset($_GET['getRec'])){
    recordfile_uri(base64_decode($_GET['getRec']));
    die();
}


Done!


Everyone is happy and happy - because now you can easily download the recording of the conversation and listen to it locally in the audio player.

Also popular now: