Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active March 12, 2019 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgaetz/73984457008c3769f0af4c7d668d16c3 to your computer and use it in GitHub Desktop.
Save lgaetz/73984457008c3769f0af4c7d668d16c3 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
*
* Script: check-cust-id.php
*
* Latest version: https://gist.github.com/lgaetz/73984457008c3769f0af4c7d668d16c3
*
* Usage: Check provided arg1 against a csv file (arg2) and set channel var (arg3) to true|false
*
* AGI(check-cust-id.php,<cust_id>,<file.ext>,<channel_var>)
*
* cust_id - customer id number
* file.ext - full path and file name or URL for csv file, one record per line, two columns, i.e.:
* 15331,0
* 15334,0
* 15340,1
* channel_var - name of asterisk channel variable to store lookup result, will be true or false
*
* use with FreePBX Custom Destintion with a gosub string of "check-customer-id,s,1(600)" without quotes
* and something like the following in extensions_custom.conf
*
* [check-customer-id]
* exten => s,1,NooP(entering custom defined context check-customer-id in extensions_custom.conf)
* exten => s,n,Read(cust-id,please-enter-your&user&number&then-press-pound)
* exten => s,n,AGI(check-cust-id.php,${cust-id},/tmp/data.csv,customer_status)
* exten => s,n,Noop(Returning from AGI with customer_status = ${customer_status})
* exten => s,n,GotoIf($["${customer_status}"!="true"]?from-internal,${ARG1},1) ; send fails to arg passed via gosub
* exten => s,n,return
*
* License: GNU GPL/2
*
* History:
* 2019-00-12 Rough and working
*
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***/
// FreePBX Bootstrap environment
include '/etc/freepbx.conf';
$FreePBX = FreePBX::Create();
// AGI Class
require_once "phpagi.php";
$AGI = new AGI();
if (!$AGI || !$FreePBX) {
// something is wrong, exit
exit;
}
// script requires 3 args - exit if not provided
if (isset($argv[3]) & strlen($argv[3])!=0) {
$cust_id = $argv[1];
$csv_file = $argv[2];
$channel_var = $argv[3];
} else {
agi_verbose("Insufficient arguments, exiting");
echo "Insufficient arguments, exiting";
exit;
}
// initialize vars
$status = false;
// parse csv file into simple key=>value array
$data=array();
$fileHandle = fopen($csv_file, "r");
//Loop through the CSV rows.
while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) {
$data[$row[0]]=$row[1];
}
// print_r($data); // for debug
agi_verbose("Parsing data from csv");
if (empty($data)) {
agi_verbose("No CSV data, exiting");
} else {
agi_verbose("Searchig CSV data for $cust_id");
}
if ( isset($data[$cust_id]) & $data[$cust_id] == 1 ) {
// cutomer ID matches and is enabled
echo "Customer ID located and confirmed enabled\n";
agi_verbose("Customer ID located and confirmed enabled");
$status = true;
} else {
// cutomer ID not valid
echo "Customer ID disabled\n";
agi_verbose("Customer ID disabled");
$status = false;
}
if ($status) {
agi_verbose("Setting Asterisk channel var $channel_var to true");
$AGI->set_variable($channel_var, 'true');
} else {
agi_verbose("Setting Asterisk channel var $channel_var to false");
$AGI->set_variable($channel_var, 'false');
}
function agi_verbose($string, $level=3) {
global $AGI;
$AGI->verbose($string, $level);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment