$timeout = 10;
$host = "127.0.0.1";
$port = 5038;
// Check if required parameters are present and are numbers
if (
is_numeric($_GET['local_extension']) &&
is_numeric($_GET['a2b_extension']) &&
isset($_GET['action'])
) {
$local_extension = intval($_GET['local_extension']);
$a2b_extension = intval($_GET['a2b_extension']);
$action = preg_replace('/\s+/', '', $_GET['action']); // Remove spaces from action
// Define username and secret
$username = "admin";
$secret = "6a16e190abd1qeee7d";
$context_1 = "from-internal"; // Variable for out1
$context_2 = "monitor"; // Variable for out2
// Attempt to establish connection
$socket = @fsockopen("$host", $port, $errno, $errstr, $timeout);
if (!$socket) {
echo "Error connecting to Asterisk Manager Interface: $errstr ($errno)";
exit;
}
// Send AMI login command
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: $username\r\n"); // Use username variable
fputs($socket, "Secret: $secret\r\n\r\n"); // Use secret variable
$wrets = fgets($socket, 128);
// Send Originate command
fputs($socket, "Action: Originate\r\n");
fputs($socket, "Channel: Local/$local_extension@$context_1/n\r\n");
fputs($socket, "Exten: $action\r\n");
fputs($socket, "Context: $context_2\r\n");
fputs($socket, "Priority: 1\r\n");
fputs($socket, "CallerID: \r\n");
fputs($socket, "Variable: __action=$action\r\n"); // Use sanitized action variable
fputs($socket, "Variable: __a2b_extension=$a2b_extension\r\n"); // Add a2b_extension variable
fputs($socket, "Async: yes\r\n\r\n");
fputs($socket, "Action: Logoff\r\n\r\n");
sleep(1);
$wrets = fgets($socket, 128);
echo $wrets;
// Close the socket connection
fclose($socket);
} else {
echo "Missing or invalid parameters.";
}