This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sms.php
143 lines (123 loc) · 4.13 KB
/
sms.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
140
141
142
143
<?php
/**
* Twilio SMS Relay and Callback
* Created by PhpStorm.
* User: ross
* Date: 7/12/15
* Time: 1:36 AM
*/
//Libraries
require_once 'vendor/twilio/sdk/Services/Twilio.php';
require_once "lib/AsteriskManager.php";
require_once "lib/VoIPms.php";
require_once "lib/Anveo.php";
require_once "lib/config.inc.php";
//Make sure we have something useful sent to the script
if(!$_REQUEST['From']) {
die("Missing Parameter.");
}
//Let's do this!
receiveMessage();
function receiveMessage()
{
global $users;
$from = $_REQUEST['From'];
$to = $_REQUEST['To'];
$text = $_REQUEST['Body'];
if (($text[0] == "c" || $text[0] == "C") && in_array($from, $users)) {
//Remove 'c' from message to get contact name
$contact = substr($text,2);
//Dial by Number
if (is_numeric($contact)) {
$message = "Callback Requested: " . $contact;
$fromNum = ($_REQUEST['FromCountry'] == "US") ? $twilio_usa : $twilio_france;
sendMessage($from, $message, $fromNum);
callback($from, $contact, $from);
//Dial by Name
} else if (array_key_exists($contact, $recipients)) {
$num = substr($recipients[$contact],1);
$message = "Callback Requested: " . $num;
$fromNum = ($_REQUEST['FromCountry'] == "US") ? $twilio_usa : $twilio_france;
sendMessage($from, $message, $fromNum);
callback($from, $num, $from);
//Something's wrong
} else {
$fromNum = ($_REQUEST['FromCountry'] == "US") ? $twilio_usa : $twilio_france;
$message = "Unknown Contact: " . $contact;
sendMessage($from, $message, $fromNum);
}
} else {
//Shame on them.
$message = "You are not authorized. Contact Ross for access.";
$fromNum = ($_REQUEST['FromCountry'] == "US") ? $twilio_usa : $twilio_france;
sendMessage($from, $message, $fromNum);
//Let me know of the intrusion
$message = "Unauthorized Access: " . $from . " Message: " . $text;
sendMessage($admins['ross'], $message);
}
}
/**
* Send SMS Message using Twilio API
* @param $toNum int SMS Recipient Phone Number (Including +CountryCode)
* @param $text string Message to send to recipient
* @param $fromNum int Force the number to send from on Twilio's side
*/
function sendMessage($toNum, $text, $fromNum = 'foo') {
global $accountSid, $authToken, $twilio_usa, $twilio_france;
$client = new Services_Twilio($accountSid, $authToken);
//Select the appropriate source phone number
//If sending from France to US, should be the US Twilio number.
//If sending from US to France, should be the France Twilio number.
if (!$fromNum || $fromNum == "foo") {
$fromNum = ($_REQUEST['FromCountry'] == "FR") ? $twilio_usa : $twilio_france;
}
try {
$message = $client->account->messages->create(array(
"From" => $fromNum,
"To" => $toNum,
"Body" => $text,
));
} catch (Services_Twilio_RestException $e) {
echo $e->getMessage();
}
}
/**
* Place Callback using Asterisk VoIP Server
* @param $source int Initiator of Callback (SMS Sender)
* @param $dest int Recipient of Callback (Number from SMS)
* @param $cidNum int Caller ID Number (shown on both sides)
*/
function callback($source, $dest, $cidNum) {
global $ami_server, $ami_port, $ami_user, $ami_pass;
$params = array('server' => $ami_server, 'port' => $ami_port);
//Fix Source Number Format
$source = "Local/" . $source . "@outbound-allroutes";
/**
* Instantiate Asterisk object and connect to server
*/
$ast = new Net_AsteriskManager($params);
/**
* Connect to server
*/
try {
$ast->connect();
} catch (PEAR_Exception $e) {
echo $e;
}
/**
* Login to manager API
*/
try {
$ast->login($ami_user, $ami_pass);
} catch(PEAR_Exception $e) {
echo $e;
}
/**
* Place the Call
*/
try {
$ast->originateCall($dest, $source, 'from-internal', $cidNum, '1', '10000');
} catch(PEAR_Exception $e) {
echo $e;
}
}