-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCallHandler.class.php
More file actions
160 lines (135 loc) · 3.74 KB
/
CallHandler.class.php
File metadata and controls
160 lines (135 loc) · 3.74 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* User telephone number verification
* @author Michael Peacock <mkpeacock@gmail.com>
* @copyright Michael Peacock www.michaelpeacock.co.uk
*/
class CallHandler{
/**
* Callback code - secret key used to verify the requests are coming from twilio
* @var String
*/
private $callbackCode = 'SECRET-YOU-DEFINE-HERE';
/**
* Name of our site
*/
private $siteName = 'ACME';
/**
* Our user adapter
* @var UserAdapter
*/
private $user;
/**
* Twilio
* @var Service_Twilio
*/
private $twilio;
/**
* Caller ID
* @var int
*/
private $caller;
/**
* Verification site URL
* @var String
*/
private $url;
/**
* Constructor
* @param Services_Twilio $twilio
* @param UserAdapter $user
* @param String $code
* @return void
*/
public function __construct( Services_Twilio $twilio )
{
$this->twilio = $twilio;
}
public function setUser( UserAdapter $user )
{
$this->user = $user;
return $this;
}
public function setCode( $code=null )
{
$this->user->setVerificationCode( is_null( $code ) ? $this->generateRandomCode() : $code );
return $this;
}
public function setCaller( $tn )
{
$this->caller = $tn;
return $this;
}
public function setSiteName( $sn )
{
$this->siteName = $sn;
return $this;
}
public function setURL( $url )
{
$this->url = $url;
return $this;
}
private function generateRandomCode()
{
return '1234';
}
public function callUser( $countryCode, $telephoneNumber )
{
$call = $this->twilio->account->calls->create(
$this->caller,
$countryCode . $telephoneNumber,
$this->url . '?&callbackCode=' . $this->callbackCode . '&user=' . $this->user->getUserID()
);
return $this;
}
/**
* Process a call / phone verification
* @param String $action
* @return void
*/
public function processCall( $action='', $callbackCode='' )
{
$response = new Services_Twilio_Twiml();
if( $callbackCode != $this->callbackCode )
{
// looks like someone is trying to trick us!
$response->say('Sorry, we were not able to process your request');
print $response;
}
else
{
switch( $action )
{
case 'repeat':
$gather = $response->gather( array( 'numDigits' => 4, 'action' => $this->url . '?&action=verify_again&user=' . $this->user->getUserID() . '&callbackCode=' . $this->callbackCode ) );
$gather->say('Sorry, the code you entered was incorrect, please try again.');
print $response;
break;
case 'verify':
$code = $_REQUEST['Digits'];
$this->user->verify( $code ) ? $this->processCall('verified', $this->callbackCode ) : $this->processCall('repeat', $this->callbackCode );
break;
case 'verify_again':
$code = $_REQUEST['Digits'];
$this->user->verify( $code ) ? $this->processCall('verified', $this->callbackCode ) : $this->processCall('sorry', $this->callbackCode );
break;
case 'verified':
$response->say('Thank you, your phone number has been verified');
print $response;
break;
case 'sorry':
$response->say('Sorry, you have entered an incorrect code again, we have not been able to verify your phone number');
print $response;
break;
default:
$gather = $response->gather( array( 'numDigits' => 4, 'action' => $this->url . '?&action=verify&user=' . $this->user->getUserID() . '&callbackCode=' . $this->callbackCode ) );
$gather->say('This is the ' . $this->siteName . ' telephone verification service. To verify your telephone number, please enter the four digit code shown on your screen now.');
print $response;
break;
}
}
exit();
}
}
?>