1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package fwolflib |
4
|
|
|
* @subpackage class |
5
|
|
|
* @copyright Copyright 2010, Fwolf |
6
|
|
|
* @author Fwolf <[email protected]> |
7
|
|
|
* @since 2010-05-19 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
require_once(dirname(__FILE__) . '/fwolflib.php'); |
12
|
|
|
require_once(FWOLFLIB . 'class/curl.php'); |
13
|
|
|
require_once(FWOLFLIB . 'func/crypt.php'); |
14
|
|
|
require_once(FWOLFLIB . 'func/ecl.php'); |
15
|
|
|
require_once(FWOLFLIB . 'func/string.php'); |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Commucate with server via http using Curl. |
20
|
|
|
* |
21
|
|
|
* Msg commucated is json to string then encrypted. |
22
|
|
|
* |
23
|
|
|
* Msg send/post format(encrypted json string): |
24
|
|
|
* array( |
25
|
|
|
* action |
26
|
|
|
* msg Various action may have diff type msg, array or string. |
27
|
|
|
* msg_extra |
28
|
|
|
* ) |
29
|
|
|
* |
30
|
|
|
* Msg return format(after decrypted): |
31
|
|
|
* array( |
32
|
|
|
* code |
33
|
|
|
* msg |
34
|
|
|
* If code=0, no error, msg is data array. |
35
|
|
|
* If code<>0, error, msg is string error msg. |
36
|
|
|
* ) |
37
|
|
|
* |
38
|
|
|
* |
39
|
|
|
* Roadmap: |
40
|
|
|
* |
41
|
|
|
* 1.1 2010-06-08 Msg extra can be added when send to remote. |
42
|
|
|
* 1.0 2010-06-01 Basic communicate feature. |
43
|
|
|
* |
44
|
|
|
* @package fwolflib |
45
|
|
|
* @subpackage class |
46
|
|
|
* @copyright Copyright 2010, Fwolf |
47
|
|
|
* @author Fwolf <[email protected]> |
48
|
|
|
* @since 2010-05-19 |
49
|
|
|
*/ |
50
|
|
|
class CurlComm extends Curl { |
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Extra msg will be added when comm |
54
|
|
|
* Notice: Avoid conflict with other msg. |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
public $aMsgExtra = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Algorithm of crypt |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
public $sCryptAlgo = 'blowfish'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Key of crypt |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
public $sCryptKey = ''; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Url of remote site |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
public $sUrlRemote = ''; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Constructor |
80
|
|
|
* |
81
|
|
|
* @param array $ar_cfg |
82
|
|
|
*/ |
83
|
|
|
public function __construct($ar_cfg = array()) { |
84
|
|
|
// For auto-call of func NewObjXXX() |
85
|
|
|
//unset($this->oCurl); |
86
|
|
|
parent::__construct(); |
87
|
|
|
|
88
|
|
|
$this->Log('Begin', 3); |
89
|
|
|
if (!empty($ar_cfg)) |
90
|
|
|
$this->SetCfg($ar_cfg); |
91
|
|
|
|
92
|
|
|
if (!empty($_POST) && isset($_POST['msg'])) { |
93
|
|
|
// Act as server |
94
|
|
|
$ar = $this->CommReceive(); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} // end of func __construct |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Destructor |
101
|
|
|
*/ |
102
|
|
|
public function __destruct() { |
103
|
|
|
// Useless, dummy item, log will not be recorded. |
104
|
|
|
$this->Log('End', 3); |
105
|
|
|
|
106
|
|
|
parent::__destruct(); |
107
|
|
|
} // end of func __destruct |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Server receive msg from client, call treat func |
112
|
|
|
* and return msg to client. |
113
|
|
|
*/ |
114
|
|
|
protected function CommReceive() { |
115
|
|
|
// Init result array |
116
|
|
|
$rs = array( |
117
|
|
|
'code' => 0, |
118
|
|
|
'msg' => '', |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
if (empty($_POST) || empty($_POST['msg'])) { |
122
|
|
|
$rs['code'] = 1; |
123
|
|
|
$rs['msg'] = 'Empty input msg.'; |
124
|
|
|
} else { |
125
|
|
|
$ar_req = $this->MsgDecrypt($_POST['msg']); |
126
|
|
|
// Check input msg format |
127
|
|
|
if (empty($ar_req['action'])) { |
128
|
|
|
$rs['code'] = 2; |
129
|
|
|
$rs['msg'] = 'Empty action.'; |
130
|
|
|
} else { |
131
|
|
|
$rs = $this->CommReturn($ar_req); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Response to client |
136
|
|
|
echo $this->MsgEncrypt($rs); |
137
|
|
|
} // end of func CommReceive |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Call action func, return result |
142
|
|
|
* |
143
|
|
|
* @param array $ar_req |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
protected function CommReturn($ar_req) { |
147
|
|
|
$s = 'CommReturn' |
148
|
|
|
. StrUnderline2Ucfirst($ar_req['action'], true); |
|
|
|
|
149
|
|
|
if (method_exists($this, $s)) { |
150
|
|
|
return $this->$s($ar_req); |
151
|
|
|
} |
152
|
|
|
else { |
153
|
|
|
$rs = array(); |
154
|
|
|
$rs['code'] = 3; |
155
|
|
|
$rs['msg'] = 'Action "' . $ar_req['action'] |
156
|
|
|
. '" is not implemented.'; |
157
|
|
|
return $rs; |
158
|
|
|
} |
159
|
|
|
} // end of func CommReturn |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return hello msg to CommSendTest |
164
|
|
|
* |
165
|
|
|
* @see CommSendTest() |
166
|
|
|
* @param array $ar_req |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
protected function CommReturnHello($ar_req = array()) { |
170
|
|
|
return array( |
171
|
|
|
'code' => 0, |
172
|
|
|
'msg' => json_encode(array( |
173
|
|
|
'request' => var_export($ar_req, true), |
174
|
|
|
'math 1 + 1 = 2', |
175
|
|
|
'people 1 + 1 > 2', |
176
|
|
|
)), |
177
|
|
|
); |
178
|
|
|
} // end of func CommReturnHello |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Send msg to server, got result |
183
|
|
|
* |
184
|
|
|
* @param array $msg |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
public function CommSend($msg) { |
188
|
|
|
// Adding msg extra |
189
|
|
|
if (!empty($this->aMsgExtra)) |
190
|
|
|
$msg = array_merge($msg, $this->aMsgExtra); |
191
|
|
|
|
192
|
|
|
$s = $this->MsgEncrypt($msg); |
193
|
|
|
$s = $this->Post($this->sUrlRemote, array('msg' => $s)); |
194
|
|
|
// Decrypt result |
195
|
|
|
if (!empty($s)) |
196
|
|
|
$ar = $this->MsgDecrypt($s); |
197
|
|
|
else |
198
|
|
|
$ar = array(); |
199
|
|
|
return $ar; |
200
|
|
|
} // end of func CommSend |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Send signal to server to test remote url readable |
205
|
|
|
* |
206
|
|
|
* @return int 0/ok, other error. |
207
|
|
|
*/ |
208
|
|
|
public function CommSendTest() { |
209
|
|
|
$this->Log('Say hello to server.', 1); |
210
|
|
|
$ar = array('action' => 'hello'); |
211
|
|
|
$this->Log('Sending: ' . json_encode($ar), 1); |
212
|
|
|
$ar = $this->CommSend($ar); |
213
|
|
|
$this->Log('Server http code: ' |
214
|
|
|
. $this->GetLastCode() . ', raw msg length ' |
215
|
|
|
. strlen($this->mRs), 1); |
216
|
|
|
//$this->Log('Server raw msg: ' . $this->mRs); |
217
|
|
|
//$this->Log('Server raw msg decrypted: ' . var_export($ar, true)); |
218
|
|
|
if (isset($ar['code'])) { |
219
|
|
|
$this->Log('Server code: ' . $ar['code'], 1); |
220
|
|
|
$this->Log('Server msg: ' . $ar['msg'], 1); |
221
|
|
|
$this->Log('Comm send test ok.', 3); |
222
|
|
|
return 0; |
223
|
|
|
} else { |
224
|
|
|
$this->Log('No valid server return msg.', 1); |
225
|
|
|
$this->Log('Comm send test fail.', 5); |
226
|
|
|
return 1; |
227
|
|
|
} |
228
|
|
|
} // end of func CommSendTest |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Decrypt msg, include json treat |
233
|
|
|
* |
234
|
|
|
* @param string $msg |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
protected function MsgDecrypt($msg) { |
238
|
|
|
$s = McryptSmplIvDecrypt($msg, $this->sCryptKey, $this->sCryptAlgo); |
|
|
|
|
239
|
|
|
$ar = json_decode($s, true); |
240
|
|
|
return $ar; |
241
|
|
|
} // end of func MsgDecrypt |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Encrypt msg, include json treat |
246
|
|
|
* |
247
|
|
|
* @param array $ar_msg Array |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
protected function MsgEncrypt($ar_msg) { |
251
|
|
|
$s = json_encode($ar_msg); |
252
|
|
|
$s = McryptSmplIvEncrypt($s, $this->sCryptKey, $this->sCryptAlgo); |
|
|
|
|
253
|
|
|
return $s; |
254
|
|
|
} // end of func MsgEncrypt |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Read and set config |
259
|
|
|
* |
260
|
|
|
* @param array $ar_cfg |
261
|
|
|
* @return $this |
262
|
|
|
*/ |
263
|
|
|
public function SetCfg($ar_cfg = array()) { |
264
|
|
|
if (!empty($ar_cfg)) { |
265
|
|
|
$this->sCryptAlgo = ArrayRead($ar_cfg, 'crypt_algo', $this->sCryptAlgo); |
|
|
|
|
266
|
|
|
$this->sCryptKey = ArrayRead($ar_cfg, 'crypt_key', ''); |
|
|
|
|
267
|
|
|
$this->sUrlRemote = ArrayRead($ar_cfg, 'url_remote', ''); |
|
|
|
|
268
|
|
|
} |
269
|
|
|
return $this; |
270
|
|
|
} // end of func SetCfg |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
} // end of class CurlComm |
274
|
|
|
?> |
|
|
|
|
275
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.