-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoolsms.php
368 lines (337 loc) · 9.43 KB
/
coolsms.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
*
* Copyright (C) 2008-2015 NURIGO
* http://www.coolsms.co.kr
*
**/
class coolsms
{
private $api_key;
private $api_secret;
private $host = "http://api.coolsms.co.kr/";
private $resource;
private $version = "1.5";
private $sdk_version = "1.1";
private $path;
private $method;
private $timestamp;
private $salt;
private $result;
private $basecamp;
private $user_agent;
private $error;
public function __construct($api_key, $api_secret, $basecamp=false)
{
if($basecamp)
{
$this->coolsms_user = $api_key;
$this->basecamp = true;
}
else
{
$this->api_key = $api_key;
}
$this->api_secret = $api_secret;
$this->user_agent = $_SERVER['HTTP_USER_AGENT'];
}
/**
* process curl
*/
public function curlProcess()
{
$ch = curl_init();
// Set host. 1 = POST , 0 = GET
if($this->method==1)
{
$host = sprintf("%s%s/%s/%s", $this->host, $this->resource, $this->version, $this->path);
}
else if($this->method==0)
{
$host = sprintf("%s%s/%s/%s?%s", $this->host, $this->resource, $this->version, $this->path, $this->content);
}
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION,3); // SSL 버젼 (https 접속시에 필요)
curl_setopt($ch, CURLOPT_HEADER, 0); // 헤더 출력 여부
curl_setopt($ch, CURLOPT_POST, $this->method); // Post Get 접속 여부
// Set POST DATA
if($this->method)
{
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->content);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // TimeOut 값
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 결과값을 받을것인지
$this->result = json_decode(curl_exec($ch));
// Check coolsms error code
if($this->result->code) $this->error = 'Code : ' . $this->result->code . ' Message : ' . $this->result->message;
// Check connect errors
if(curl_errno($ch)) $this->error = curl_error($ch);
curl_close ($ch);
}
/**
* set http body content
*/
private function setContent($options)
{
if($this->method)
{
$this->content = array();
foreach($options as $key => $val)
{
if($key != "image")
$this->content[$key] = sprintf("\0%s", $val);
else
$this->content[$key] = "@".realpath("./$val");
}
}
else
{
foreach($options as $key => $val)
$this->content .= $key."=".urlencode($val)."&";
}
}
/**
* make a signature with hash_hamac then return the signature
*/
private function getSignature()
{
return hash_hmac('md5', (string)$this->timestamp.$this->salt, $this->api_secret);
}
/**
* set authenticate information
*/
private function addInfos($options)
{
$this->salt = uniqid();
$this->timestamp = (string)time();
if(!$options->User_Agent) $options->User_Agent = sprintf("PHP REST API %s", $this>version);
if(!$options->os_platform) $options->os_platform = $this->getOS();
if(!$options->dev_lang) $options->dev_lang = sprintf("PHP %s", phpversion());
if(!$options->sdk_version) $options->sdk_version = sprintf("PHP SDK %s", $this->sdk_version);
$options->salt = $this->salt;
$options->timestamp = $this->timestamp;
if($this->basecamp)
{
$options->coolsms_user = $this->coolsms_user;
}
else
{
$options->api_key = $this->api_key;
}
$options->signature = $this->getSignature();
$this->setContent($options);
$this->curlProcess();
}
/**
* $resource
* 'sms', 'senderid'
* $method
* GET = 0, POST, 1
* $path
* 'send' 'sent' 'cancel' 'balance'
*/
private function setMethod($resource, $path, $method, $version="1.5")
{
$this->resource = $resource;
$this->path = $path;
$this->method = $method;
$this->version = $version;
}
/**
* return result
*/
public function getResult()
{
return $this->result;
}
/**
* @POST send method
* @param $options (options must contain api_key, salt, signature, to, from, text)
* @type, image, refname, country, datetime, mid, gid, subject, charset (optional)
* @returns an object(recipient_number, group_id, message_id, result_code, result_message)
*/
public function send($options)
{
$this->setMethod('sms', 'send', 1);
$this->addInfos($options);
return $this;
}
/**
* @GET sent method
* @param $options (options can be optional)
* @count, page, s_rcpt, s_start, s_end, mid, gid (optional)
* @returns an object(total count, list_count, page, data['type', 'accepted_time', 'recipient_number', 'group_id', 'message_id', 'status', 'result_code', 'result_message', 'sent_time', 'text'])
*/
public function sent($options=null)
{
if(!$options)
$options = new stdClass();
$this->setMethod('sms', 'sent', 0);
$this->addInfos($options);
return $this;
}
/**
* @POST cancel method
* @options must contain api_key, salt, signature
* @mid, gid (either one must be entered.)
*/
public function cancel($options)
{
$this->setMethod('sms', 'cancel', 1);
$this->addInfos($options);
return $this;
}
/**
* @GET balance method
* @options must contain api_key, salt, signature
* @return an object(cash, point)
*/
public function balance()
{
$this->setMethod('sms', 'balance', 0);
$this->addInfos($options = new stdClass());
return $this;
}
/**
* @GET status method
* @options must contain api_key, salt, signature
* @return an object(registdate, sms_average, sms_sk_average, sms_kt_average, sms_lg_average, mms_average, mms_sk_average, mms_kt_average, mms_lg_average)
* this method is made for Coolsms inc. internal use
*/
public function status($options)
{
$this->setMethod('sms', 'status', 0);
$this->addInfos($options);
return $this;
}
/**
* @POST register method
* @options must contains api_key, salt, signature, phone, site_user(optional)
* @return json object(handle_key, ars_number)
*/
public function register($options)
{
$this->setMethod('senderid', 'register', 1, "1.1");
$this->addInfos($options);
return $this;
}
/**
* @POST verify method
* @options must contains api_key, salt, signature, handle_key
* return nothing
*/
public function verify($options)
{
$this->setMethod('senderid', 'verify', 1, "1.1");
$this->addInfos($options);
return $this;
}
/**
* POST delete method
* $options must contains api_key, salt, signature, handle_key
* return nothing
*/
public function delete($options)
{
$this->setMethod('senderid', 'delete', 1, "1.1");
$this->addInfos($options);
return $this;
}
/**
* GET list method
* $options must conatins api_key, salt, signature, site_user(optional)
* return json object(idno, phone_number, flag_default, updatetime, regdate)
*/
public function get_senderid_list($options)
{
$this->setMethod('senderid', 'list', 0, "1.1");
$this->addInfos($options);
return $this;
}
/**
* POST set_default
* $options must contains api_key, salt, signature, handle_key, site_user(optional)
* return nothing
*/
public function set_default($options)
{
$this->setMethod('senderid', 'set_default', 1, "1.1");
$this->addInfos($options);
return $this;
}
/**
* GET get_default
* $options must conatins api_key, salt, signature, site_user(optional)
* return json object(handle_key, phone_number)
*/
public function get_default($options)
{
$this->setMethod('senderid', 'get_default', 0, "1.1");
$this->addInfos($options);
return $this;
}
/**
* return user's current OS
*/
function getOS() {
$user_agent = $this->user_agent;
$os_platform = "Unknown OS Platform";
$os_array = array(
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);
foreach ($os_array as $regex => $value) {
if (preg_match($regex, $user_agent)) {
$os_platform = $value;
}
}
return $os_platform;
}
/**
* return user's current browser
*/
function getBrowser() {
$user_agent = $this->user_agent;
$browser = "Unknown Browser";
$browser_array = array(
'/msie/i' => 'Internet Explorer',
'/firefox/i' => 'Firefox',
'/safari/i' => 'Safari',
'/chrome/i' => 'Chrome',
'/opera/i' => 'Opera',
'/netscape/i' => 'Netscape',
'/maxthon/i' => 'Maxthon',
'/konqueror/i' => 'Konqueror',
'/mobile/i' => 'Handheld Browser'
);
foreach ($browser_array as $regex => $value) {
if (preg_match($regex, $user_agent)) {
$browser = $value;
}
}
return $browser;
}
}