-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathClient.php
251 lines (214 loc) · 4.82 KB
/
Client.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php // -->
/**
* GoIP Client/Server Package based on
* GoIP SMS Gateway Interface.
*
* (c) 2014-2016 Openovate Labs
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/
namespace GoIP;
/**
* Client Class
*
* @package GoIP
* @author Charles Zamora <czamora@openovate.com>
* @standard PSR-2
*/
class Client extends Event
{
/**
* Default socket.
*
* @var object
*/
protected $socket = null;
/**
* Default host.
*
* @var string
*/
protected $host = null;
/**
* Default port / channel.
*
* @var int
*/
protected $port = null;
/**
* Default password.
*
* @var string
*/
protected $password = null;
/**
* Current send id.
*
* @var int | null
*/
protected $id = null;
/**
* Debug flag.
*
* @var bool
*/
protected $debug = false;
/**
* Initialize the client socket
* given the host and the port,
* default GoIP port for it's
* SMS interface is 9991 which
* also serve as the GSM Module
* channel.
*
* @param string
* @param int
* @return $this
*/
public function __construct($host, $port = 9991)
{
// set the host
$this->host = $host;
// set the port
$this->port = $port;
// initialize our socket
// - ipv4 connection
// - socket datagram type
// - over UDP connection
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
// if socket failed to be created
if($this->socket < 0) {
// set event message
$message = 'Failed to create socket: ' . socket_strerror($this->socket) . PHP_EOL;
// throw an event
$this->trigger('client-error', $message);
// exit
exit();
}
}
/**
* Set host.
*
* @param string
* @return $this
*/
public function setHost($host)
{
// set the host
$this->host = $host;
return $this;
}
/**
* Set port.
*
* @param int
* @return $this
*/
public function setPort($port = 9991)
{
// set the port / channel
$this->port = $port;
return $this;
}
/**
* Close the socket connection.
*
* @return $this
*/
public function close()
{
// close the socket
socket_close($this->socket);
return $this;
}
/**
* Set password.
*
* @param string
* @return $this
*/
public function setPassword($password)
{
// set the password
$this->password = $password;
return $this;
}
/**
* Set send id.
*
* @param int
* @return $this
*/
public function setId($id)
{
// set the id
$this->id = $id;
return $this;
}
/**
* Set debugging.
*
* @param bool
* @return $this
*/
public function setDebug($debug = false)
{
// set debug flag
$this->debug = $debug;
return $this;
}
/**
* Send sms request.
*
* @param string
* @param string
* @return $this
*/
public function sendSms($content, $receiver)
{
// initialize request object
$request = new Request(
$this->socket,
$this->host,
$this->port,
$this->password);
// if debug
if($this->debug) {
// set request debugging
$request->setDebug(true);
}
// is id set?
if(!isset($this->id)) {
// set a random number
$this->id = rand(100, 1000);
}
// send bulk sms request
$bulk = $request->bulkSmsRequest($this->id, $content);
// if bulk request failed
if($bulk !== true) {
throw new \Exception('An error occured in BulkSmsRequest.');
}
// send auth request
$auth = $request->authenticationRequest($this->password);
// if auth request failed
if($auth !== true) {
throw new \Exception('An error occured in AuthenticationRequest.');
}
// send submit number request
$submit = $request->submitNumberRequest($receiver);
// if submit number request failed
if($submit !== true) {
throw new \Exception('An error occured in SubmitNumberRequest.');
}
// send end request
$end = $request->endRequest();
// if end request is not done
if($end !== true) {
throw new \Exception('An error occured in EndRequest.');
}
// close socket
$this->close();
return true;
}
}