-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcallback.php
More file actions
68 lines (58 loc) · 1.71 KB
/
callback.php
File metadata and controls
68 lines (58 loc) · 1.71 KB
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
<?php
/*
* This file handles callback events
*/
if (empty($_GET['user'])) {
die('Error: Missing user query parameter.');
}
// load needed functions
require_once(__DIR__."/helpers.php");
// debug
$file = false;
function debug($message) {
global $file;
if ($file === false)
$file = fopen('debug.log', 'a');
fwrite($file, $message."\n\n");
}
// load user
$User = getUser($_GET['user'], false);
// initialize objects
$client = new Catapult\Client;
$account = new Catapult\Account;
// parse the event
$event = json_decode(file_get_contents('php://input'));
if (empty($event->eventType)) {
die('Error: Missing event type.');
}
// set callback URL
$callbackUrl = callbackURL($User->userName);
debug("User: " . json_encode($User));
debug("Event: " . json_encode($event));
// We only need to act on the answer event
if ($event->eventType == 'answer') {
// Only the 1st leg doesn't have a tag
if (empty($event->tag)) {
if ($User->phoneNumber == $event->to) {
// incoming call
debug("Handle incoming call: call to sip " . $User->endpoint->sipUri);
$Call = new Catapult\Call($event->callId);
return $Call->transfer($User->endpoint->sipUri, array(
'transferCallerId' => $event->from,
'callbackUrl' => $callbackUrl,
'tag' => 'transferred'
));
}
if (strpos($User->endpoint->sipUri, trim($event->from)) !== false) {
// outgoing call
debug("Handle outgoing sip call: call to " . $event->to);
$Call = new Catapult\Call($event->callId);
return $Call->transfer($event->to, array(
'transferCallerId' => $event->from,
'callbackUrl' => $callbackUrl,
'tag' => 'transferred'
));
}
}
debug("Nothing to do!");
}