#!/usr/bin/php -q
<?php
/*
This Callback script takes 3 arguments:
1- number to dial
2- context.exten.priority to dump number into
3- time in seconds to sleep before calling back

eg: callback 14032448089 ext-meetme.200.1
*/
//Copyright (C) 2004 Coalescent Systems Inc. (info@coalescentsystems.ca)
//This file is part of FreePBX.
//
//    FreePBX is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    FreePBX is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with FreePBX.  If not, see <http://www.gnu.org/licenses/>.
?>

<?php
define("AMP_CONF", "/etc/amportal.conf");

//sleep(10);

/**************************************************************/

// from  ben-php dot net at efros dot com   at  php.net/install.unix.commandline
if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) {
	define('STDIN',fopen("php://stdin","r"));
	define('STDOUT',fopen("php://stdout","r"));
	define('STDERR',fopen("php://stderr","r"));
	register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) );
}

// **** Make sure we have PEAR's GetOpts.php, and include it

outn("Checking for PEAR Console::Getopt..");
if (! @ include("Console/Getopt.php")) {
	out("FAILED");
	fatal("PEAR must be installed (requires Console/Getopt.php). Include path: ".ini_get("include_path"));
}
out("OK");

outn("Reading ".AMP_CONF."..");
$amp_conf = parse_amportal_conf_bootstrap(AMP_CONF);
if (count($amp_conf) == 0) {
	fatal("FAILED");
}
out("OK");

// include manager functions
require_once($amp_conf['AMPWEBROOT']."/admin/functions.inc.php");
include $amp_conf['AMPWEBROOT'].'/admin/common/php-asmanager.php';

// **** Parse out command-line args
// context, extension, and number of voicemails
out("Getting passed arguments:");
$con  = new Console_Getopt;
$args = $con->readPHPArgv();
array_shift($args);
//system("echo \"".$args[0].substr($args[1],0,strpos($args[1],"@")).$args[2]."\" > on.txt"); ;
print_r($args);

$callback_number = $args[0];
$callback_destination = $args[1];
$pause_seconds = $args[2];

if($pause_seconds)
	sleep($pause_seconds);
	
// figure out context, exten, priority
$dest = explode(".",$callback_destination);
$callback_context = $dest[0];
$callback_exten = $dest[1];
$callback_priority = $dest[2];

//define the args for Originate
$channel = "Local/".$callback_number."@from-internal";
//$channel = "zap/g0/".$uservm[$vmcontext][$vmextension]['options']['callme'];
$exten = $callback_exten;
$context = $callback_context;
$priority = $callback_priority;
$timeout = "15000";
$callerid = "Callback";
$variable = "";
$account = "";
$application = "";
$data = "";

//connect to manager and dial
$astman = new AGI_AsteriskManager();
if ($res = $astman->connect("127.0.0.1", $amp_conf["AMPMGRUSER"] , $amp_conf["AMPMGRPASS"])) {
	$astman->Originate($channel, $exten, $context, $priority, $timeout, $callerid, $variable, $account, $application, $data);
} else {
	fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
}
$astman->disconnect();


function parse_amportal_conf_bootstrap($filename) {
	$file = file($filename);
	foreach ($file as $line) {
		if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) { 
			$conf[ $matches[1] ] = $matches[2];
		}
	}
	return $conf;
}

function out($text) {
	echo $text."\n";
}

function outn($text) {
	echo $text;
}

function error($text) {
	echo "[ERROR] ".$text."\n";
}

function fatal($text) {
	echo "[FATAL] ".$text."\n";
	exit(1);
}

?>
