-
Notifications
You must be signed in to change notification settings - Fork 2
/
freepbx-click-to-call.php
140 lines (119 loc) · 5.64 KB
/
freepbx-click-to-call.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
# This script is an adapted version of Alissons Pelizaro's version found here: https://github.com/alissonpelizaro/Asterisk-Click-to-Call
# Script adapted to provide input validation and JSON results
# Asterisk host details
$strHost = "127.0.0.1";
$strUser = "admin"; #specify the asterisk manager username you want to login with
$strSecret = "MYSECRETPASS"; #specify the password for the above user, can be fetched from /etc/asterisk/manager.conf
# Config
$strCallerId = "CTR Plugin (%s)"; #specify caller id with number to be called in %s placeholder
$strContext = "from-internal";
$strWaitTime = "30"; #specify the amount of time you want to try calling the specified channel before hangin up
$strPriority = "1"; #specify the priority you wish to place on making this call
$strMaxRetry = "2"; #specify the maximum amount of retries
# Request data
$strExten = $_REQUEST['exten'];
$strNumber = strtolower($_REQUEST['number']);
#
# Script execution
#
$result = array('Success' => true, 'ValidInput' => true, 'Description' => '');
# Data validation
if(!preg_match('/^\\+?[0-9]+$/i', $strNumber))
{
$result['ValidInput'] = false;
$result['Success'] = false;
$result['Description'] = sprintf("Error, number to call must be provided and may only contain numeric characters and a leading plus symbol, number called was: %s", $strNumber);
}
if(!preg_match('/^[0-9]+$/i', $strExten))
{
$result['ValidInput'] = false;
$result['Success'] = false;
$result['Description'] = sprintf("Error, extension to call from must be provided and may only contain numeric characters, extension provided was: %s", $strExten);
}
# Exit if not a local request
if (filter_var($_SERVER["REMOTE_ADDR"], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) )
{
$result['Success'] = false;
$result['Description'] = sprintf("Error, %s is not a local IP",$_SERVER["REMOTE_ADDR"]);
}
# Open socket
if($result['Success'] === true)
{
$strCallerId = sprintf($strCallerId, $strNumber);
# Create socket
$oSocket = stream_socket_client("tcp://$strHost:$strPort/ws");
if (!$oSocket)
{
$result['Success'] = false;
$result['Description'] = sprintf("Error while creating socket to [%s]:%s, %s (%s)", $strHost, $intPort, $errstr, $errno);
}
# Authenticate
if($result['Success'] === true)
{
// Prepare authentication request
$authenticationRequest = "Action: Login\r\n";
$authenticationRequest .= "Username: $strUser\r\n";
$authenticationRequest .= "Secret: $strSecret\r\n";
$authenticationRequest .= "Events: off\r\n\r\n";
// Send authentication request
$authenticate = stream_socket_sendto($oSocket, $authenticationRequest);
if($authenticate < 0)
{
$result['Success'] = false;
$result['Description'] = sprintf("Error while writing login request to tcp socket");
}
else
{
// Wait for server response
usleep(200000);
// Read server response
$authenticateResponse = fread($oSocket, 4096);
// Check if authentication was successful
if(strpos($authenticateResponse, 'Success') === false)
{
$result['Success'] = false;
$result['Description'] = sprintf("Error, could not authenticate to Asterisk Manager Interface");
}
}
}
# Originate call
if($result['Success'] === true)
{
// Prepare originate request
$originateRequest = "Action: Originate\r\n";
$originateRequest .= "Channel: SIP/$strExten\r\n";
$originateRequest .= "WaitTime: $strWaitTime\r\n";
$originateRequest .= "CallerId: $strCallerId\r\n";
$originateRequest .= "Exten: $strNumber\r\n";
$originateRequest .= "Context: $strContext\r\n";
$originateRequest .= "Priority: $strPriority\r\n";
$originateRequest .= "Async: yes\r\n\r\n";
$originate = stream_socket_sendto($oSocket, $originateRequest);
if($originate < 0)
{
$result['Success'] = false;
$result['Description'] = sprintf("Error, could not write call initiation request to socket.");
}
else
{
// Wait for server response
usleep(200000);
// Read server response
$originateResponse = fread($oSocket, 4096);
// Check if originate was successful
if(strpos($originateResponse, 'Success') !== false)
{
$result['Description'] = sprintf("Extension %s should be calling %s.", $strExten, $strNumber);
}
else
{
$result['Success'] = false;
$result['Description'] = sprintf("Error, could not initiate call");
}
}
}
# Deauth
stream_socket_sendto($oSocket, "Action: Logoff\r\n\r\n");
}
printf(json_encode($result, JSON_PRETTY_PRINT));