CurlComm::CommReturnHello()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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 {
0 ignored issues
show
Deprecated Code introduced by
The class Curl has been deprecated with message: Use Fwlib\Net\Curl

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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ar is correct as $this->CommReceive() (which targets CurlComm::CommReceive()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$ar is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$ar_req is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function StrUnderline2Ucfirst() has been deprecated with message: Use Fwlib\Util\StringUtil::toStudlyCaps()

This function 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 function will be removed from the class and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function McryptSmplIvDecrypt() has been deprecated with message: Use Fwlib\Util\McryptSmplIv::decrypt()

This function 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 function will be removed from the class and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function McryptSmplIvEncrypt() has been deprecated with message: Use Fwlib\Util\McryptSmplIv::encrypt()

This function 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 function will be removed from the class and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function ArrayRead() has been deprecated with message: Use Fwlib\Util\ArrayUtil::getIdx(), getEdx()

This function 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 function will be removed from the class and what other function to use instead.

Loading history...
266
			$this->sCryptKey = ArrayRead($ar_cfg, 'crypt_key', '');
0 ignored issues
show
Deprecated Code introduced by
The function ArrayRead() has been deprecated with message: Use Fwlib\Util\ArrayUtil::getIdx(), getEdx()

This function 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 function will be removed from the class and what other function to use instead.

Loading history...
267
			$this->sUrlRemote = ArrayRead($ar_cfg, 'url_remote', '');
0 ignored issues
show
Deprecated Code introduced by
The function ArrayRead() has been deprecated with message: Use Fwlib\Util\ArrayUtil::getIdx(), getEdx()

This function 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 function will be removed from the class and what other function to use instead.

Loading history...
268
		}
269
		return $this;
270
	} // end of func SetCfg
271
272
273
} // end of class CurlComm
274
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
275