-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCorezoid.php
More file actions
220 lines (189 loc) · 4.59 KB
/
Corezoid.php
File metadata and controls
220 lines (189 loc) · 4.59 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
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
<?php
/**
* Corezoid Module
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Corezoid
* @package Corezoid/Corezoid
* @version 1.0
* @author corezoid.com
* @copyright Copyright (c) 2013 corezoid.com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* EXTENSION INFORMATION
*
* Corezoid API http://www.corezoid.com/how_to_use/api/en/
*
*/
/**
* Corezoid Class
*
* @author Corezoid <support@mcorezoid.com>
*/
class Corezoid
{
/**
* host Corezoid
*/
private $_host = 'https://www.corezoid.com';
/**
* Version API
*/
private $_version = '1';
/**
* Format API
*/
private $_format = 'json';
/**
* User API
*/
private $_api_login;
/**
* API secret key
*/
private $_api_secret;
/**
* Array tasks
*/
private $_tasks = array();
/**
* Constructor.
*
* @param string $api_login
* @param string $api_secret
*
* @throws InvalidArgumentException
*/
public function __construct($api_login, $api_secret)
{
if (empty($api_login)) {
throw new InvalidArgumentException('api_login is empty');
}
if (empty($api_secret)) {
throw new InvalidArgumentException('api_secret is empty');
}
$this->_api_login = $api_login;
$this->_api_secret = $api_secret;
}
public function changeHost($host)
{
$this->_host = $host;
}
/**
* Add new task
*
* @param string $ref External id for the task
* @param string $conv_id Corezoid process id
* @param array $data Sending data
*
* @throws InvalidArgumentException
*/
public function add_task($ref, $conv_id, $data = array())
{
if (empty($ref)) {
throw new InvalidArgumentException('ref is empty');
}
if (empty($conv_id)) {
throw new InvalidArgumentException('conv_id is empty');
}
$this->_tasks[] = array(
'ref' => $ref,
'type' => 'create',
'obj' => 'task',
'conv_id' => $conv_id,
'data' => $data
);
}
/**
* Send tasks to Corezoid
*
* @param bool $clear_tasks Clears current tasks
* @return string
*/
public function send_tasks($clear_tasks = true)
{
$content = json_encode(array('ops' => $this->_tasks));
if ($clear_tasks) {
$this->clear_tasks();
}
$time = time();
$url = $this->make_url($time, $content);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$server_output = curl_exec($ch);
curl_close($ch);
return $server_output;
}
public function clear_tasks()
{
$this->_tasks = array();
}
/**
* Check Signature
*
* @param string $sign
* @param string $time
* @param string $content
*
* @return string
*/
public function check_sign($sign, $time, $content)
{
$make_sign = $this->make_sign($time, $content);
return ($sign == $make_sign) ? true : false;
}
/**
* Create URL to Corezoid
*
* @param string $time
* @param string $content
*
* @return string
*/
private function make_url($time, $content)
{
$sign = $this->make_sign($time, $content);
return $this->_host . '/api/'
. $this->_version . '/'
. $this->_format . '/'
. $this->_api_login . '/'
. $time . '/'
. $sign;
}
/**
* Create Signature
*
* @param string $time
* @param string $content
*
* @return string
*/
private function make_sign($time, $content)
{
return $this->str2hex(sha1($time . $this->_api_secret . $content . $this->_api_secret, 1));
}
/**
* String to HEX
*
* @param string $str
*
* @return string
*/
private function str2hex($str)
{
$r = unpack('H*', $str);
return array_shift($r);
}
}