1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* usefull functions to get quick results |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait CommonCode |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use CommonBasic, |
40
|
|
|
DomComponentsByDanielGP, |
41
|
|
|
MySQLiByDanielGPqueries, |
42
|
|
|
MySQLiByDanielGP; |
43
|
|
|
|
44
|
|
|
private function buildArrayForCurlInterogation($chanel, $rspJsonFromClient) |
45
|
|
|
{ |
46
|
|
|
if (curl_errno($chanel)) { |
47
|
|
|
return [ |
48
|
|
|
'info' => $this->setArrayToJson(['#' => curl_errno($chanel), 'description' => curl_error($chanel)]), |
49
|
|
|
'response' => '', |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
return ['info' => $this->setArrayToJson(curl_getinfo($chanel)), 'response' => $rspJsonFromClient]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Reads the content of a remote file through CURL extension |
57
|
|
|
* |
58
|
|
|
* @param string $fullURL |
59
|
|
|
* @param array $features |
60
|
|
|
* @return blob |
61
|
|
|
*/ |
62
|
|
|
protected function getContentFromUrlThroughCurl($fullURL, $features = null) |
63
|
|
|
{ |
64
|
|
|
if (!function_exists('curl_init')) { |
65
|
|
|
$aReturn = ['info' => $this->lclMsgCmn('i18n_Error_ExtensionNotLoaded'), 'response' => '']; |
66
|
|
|
return $this->setArrayToJson($aReturn); |
67
|
|
|
} |
68
|
|
|
if (!filter_var($fullURL, FILTER_VALIDATE_URL)) { |
69
|
|
|
$aReturn = ['info' => $this->lclMsgCmn('i18n_Error_GivenUrlIsNotValid'), 'response' => '']; |
70
|
|
|
return $this->setArrayToJson($aReturn); |
71
|
|
|
} |
72
|
|
|
$aReturn = $this->getContentFromUrlThroughCurlRawArray($fullURL, $features); |
73
|
|
|
return '{ ' . $this->packIntoJson($aReturn, 'info') . ', ' . $this->packIntoJson($aReturn, 'response') . ' }'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Reads the content of a remote file through CURL extension |
78
|
|
|
* |
79
|
|
|
* @param string $fullURL |
80
|
|
|
* @param array $features |
81
|
|
|
* @return blob |
82
|
|
|
*/ |
83
|
|
|
protected function getContentFromUrlThroughCurlAsArrayIfJson($fullURL, $features = null) |
84
|
|
|
{ |
85
|
|
|
$result = $this->setJsonToArray($this->getContentFromUrlThroughCurl($fullURL, $features)); |
86
|
|
|
if (is_array($result['info'])) { |
87
|
|
|
ksort($result['info']); |
88
|
|
|
} |
89
|
|
|
if (is_array($result['response'])) { |
90
|
|
|
ksort($result['response']); |
91
|
|
|
} |
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function getContentFromUrlThroughCurlRawArray($fullURL, $features = null) |
96
|
|
|
{ |
97
|
|
|
$chanel = curl_init(); |
98
|
|
|
curl_setopt($chanel, CURLOPT_USERAGENT, $this->getUserAgentByCommonLib()); |
99
|
|
|
if ((strpos($fullURL, 'https') !== false) || (isset($features['forceSSLverification']))) { |
100
|
|
|
curl_setopt($chanel, CURLOPT_SSL_VERIFYHOST, false); |
101
|
|
|
curl_setopt($chanel, CURLOPT_SSL_VERIFYPEER, false); |
102
|
|
|
} |
103
|
|
|
curl_setopt($chanel, CURLOPT_URL, $fullURL); |
104
|
|
|
curl_setopt($chanel, CURLOPT_HEADER, false); |
105
|
|
|
curl_setopt($chanel, CURLOPT_RETURNTRANSFER, true); |
106
|
|
|
curl_setopt($chanel, CURLOPT_FRESH_CONNECT, true); //avoid a cached response |
107
|
|
|
curl_setopt($chanel, CURLOPT_FAILONERROR, true); |
108
|
|
|
$rspJsonFromClient = curl_exec($chanel); |
109
|
|
|
$aReturn = $this->buildArrayForCurlInterogation($chanel, $rspJsonFromClient); |
110
|
|
|
curl_close($chanel); |
111
|
|
|
return $aReturn; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function getFeedbackMySQLAffectedRecords() |
115
|
|
|
{ |
116
|
|
|
if (is_null($this->mySQLconnection)) { |
117
|
|
|
return '<div>No MySQL connection detected</div>'; |
118
|
|
|
} |
119
|
|
|
$afRows = $this->mySQLconnection->affected_rows; |
120
|
|
|
return '<div>' . sprintf($this->lclMsgCmnNumber('i18n_Record', 'i18n_Records', $afRows), $afRows) . '</div>'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* returns the details about Communicator (current) file |
125
|
|
|
* |
126
|
|
|
* @param string $fileGiven |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function getFileDetails($fileGiven) |
130
|
|
|
{ |
131
|
|
|
if (!file_exists($fileGiven)) { |
132
|
|
|
return ['error' => sprintf($this->lclMsgCmn('i18n_Error_GivenFileDoesNotExist'), $fileGiven)]; |
133
|
|
|
} |
134
|
|
|
return $this->getFileDetailsRaw($fileGiven); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* returns a multi-dimensional array with list of file details within a given path |
139
|
|
|
* (by using Symfony/Finder package) |
140
|
|
|
* |
141
|
|
|
* @param string $pathAnalised |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
protected function getListOfFiles($pathAnalised) |
145
|
|
|
{ |
146
|
|
|
if (realpath($pathAnalised) === false) { |
147
|
|
|
return ['error' => sprintf($this->lclMsgCmn('i18n_Error_GivenPathIsNotValid'), $pathAnalised)]; |
148
|
|
|
} elseif (!is_dir($pathAnalised)) { |
149
|
|
|
return ['error' => $this->lclMsgCmn('i18n_Error_GivenPathIsNotFolder')]; |
150
|
|
|
} |
151
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
152
|
|
|
$iterator = $finder->files()->sortByName()->in($pathAnalised); |
153
|
|
|
$aFiles = null; |
154
|
|
|
foreach ($iterator as $file) { |
155
|
|
|
$aFiles[$file->getRealPath()] = $this->getFileDetails($file); |
156
|
|
|
} |
157
|
|
|
return $aFiles; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns server Timestamp into various formats |
162
|
|
|
* |
163
|
|
|
* @param string $returnType |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function getTimestamp($returnType = 'string') |
167
|
|
|
{ |
168
|
|
|
$crtTime = gettimeofday(); |
169
|
|
|
$knownReturnTypes = ['array', 'float', 'string']; |
170
|
|
|
if (in_array($returnType, $knownReturnTypes)) { |
171
|
|
|
return call_user_func([$this, 'getTimestamp' . ucfirst($returnType)], $crtTime); |
172
|
|
|
} |
173
|
|
|
return sprintf($this->lclMsgCmn('i18n_Error_UnknownReturnType'), $returnType); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private function getTimestampArray($crtTime) |
177
|
|
|
{ |
178
|
|
|
return ['float' => $this->getTimestampFloat($crtTime), 'string' => $this->getTimestampString($crtTime)]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private function getTimestampFloat($crtTime) |
182
|
|
|
{ |
183
|
|
|
return ($crtTime['sec'] + $crtTime['usec'] / pow(10, 6)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
private function getTimestampString($crtTime) |
187
|
|
|
{ |
188
|
|
|
return implode('', [ |
189
|
|
|
'<span style="color:black!important;font-weight:bold;">[', |
190
|
|
|
date('Y-m-d H:i:s.', $crtTime['sec']), |
191
|
|
|
substr(round($crtTime['usec'], -3), 0, 3), |
192
|
|
|
']</span> ' |
193
|
|
|
]); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Tests if given string has a valid Json format |
198
|
|
|
* |
199
|
|
|
* @param string $inputJson |
200
|
|
|
* @return boolean|string |
201
|
|
|
*/ |
202
|
|
|
protected function isJsonByDanielGP($inputJson) |
203
|
|
|
{ |
204
|
|
|
if (is_string($inputJson)) { |
205
|
|
|
json_decode($inputJson); |
206
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
207
|
|
|
} |
208
|
|
|
return $this->lclMsgCmn('i18n_Error_GivenInputIsNotJson'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
private function packIntoJson($aReturn, $keyToWorkWith) |
212
|
|
|
{ |
213
|
|
|
if ($this->isJsonByDanielGP($aReturn[$keyToWorkWith])) { |
214
|
|
|
return '"' . $keyToWorkWith . '": ' . $aReturn[$keyToWorkWith]; |
215
|
|
|
} |
216
|
|
|
return '"' . $keyToWorkWith . '": {' . $aReturn[$keyToWorkWith] . ' }'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Send an array of parameters like a form through a POST action |
221
|
|
|
* |
222
|
|
|
* @param string $urlToSendTo |
223
|
|
|
* @param array $params |
224
|
|
|
* @throws \Exception |
225
|
|
|
*/ |
226
|
|
|
protected function sendBackgroundEncodedFormElementsByPost($urlToSendTo, $params = []) |
227
|
|
|
{ |
228
|
|
|
$postingUrl = filter_var($urlToSendTo, FILTER_VALIDATE_URL); |
229
|
|
|
if ($postingUrl === false) { |
230
|
|
|
throw new \Exception('Invalid URL in ' . __FUNCTION__); |
231
|
|
|
} |
232
|
|
|
if (is_array($params)) { |
233
|
|
|
$cntFailErrMsg = $this->lclMsgCmn('i18n_Error_FailedToConnect'); |
234
|
|
|
$this->sendBackgroundPostData($postingUrl, $params, $cntFailErrMsg); |
235
|
|
|
return ''; |
236
|
|
|
} |
237
|
|
|
throw new \Exception($this->lclMsgCmn('i18n_Error_GivenParameterIsNotAnArray')); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
private function sendBackgroundPostData($postingUrl, $params, $cntFailErrMsg) |
241
|
|
|
{ |
242
|
|
|
$postingString = $this->setArrayToStringForUrl('&', $params); |
243
|
|
|
$pUrlParts = parse_url($postingUrl); |
244
|
|
|
$postingPort = 80; |
245
|
|
|
if (isset($pUrlParts['port'])) { |
246
|
|
|
$postingPort = $pUrlParts['port']; |
247
|
|
|
} |
248
|
|
|
$flPointer = fsockopen($pUrlParts['host'], $postingPort, $erN, $erM, 30); |
249
|
|
|
if ($flPointer === false) { |
250
|
|
|
throw new \Exception($cntFailErrMsg . ': ' . $erN . ' (' . $erM . ')'); |
251
|
|
|
} |
252
|
|
|
fwrite($flPointer, $this->sendBackgroundPrepareData($pUrlParts, $postingString)); |
253
|
|
|
fclose($flPointer); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
private function sendBackgroundPrepareData($pUrlParts, $postingString) |
257
|
|
|
{ |
258
|
|
|
$this->initializeSprGlbAndSession(); |
259
|
|
|
$out = []; |
260
|
|
|
$out[] = 'POST ' . $pUrlParts['path'] . ' ' . $this->tCmnSuperGlobals->server->get['SERVER_PROTOCOL']; |
261
|
|
|
$out[] = 'Host: ' . $pUrlParts['host']; |
262
|
|
|
$out[] = 'User-Agent: ' . $this->getUserAgentByCommonLib(); |
263
|
|
|
$out[] = 'Content-Type: application/x-www-form-urlencoded'; |
264
|
|
|
$out[] = 'Content-Length: ' . strlen($postingString); |
265
|
|
|
$out[] = 'Connection: Close' . "\r\n"; |
266
|
|
|
$out[] = $postingString; |
267
|
|
|
return implode("\r\n", $out); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Converts a JSON string into an Array |
272
|
|
|
* |
273
|
|
|
* @param string $inputJson |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
|
|
protected function setJsonToArray($inputJson) |
277
|
|
|
{ |
278
|
|
|
if (!$this->isJsonByDanielGP($inputJson)) { |
279
|
|
|
return ['error' => $this->lclMsgCmn('i18n_Error_GivenInputIsNotJson')]; |
280
|
|
|
} |
281
|
|
|
$sReturn = (json_decode($inputJson, true)); |
282
|
|
|
$jsonError = $this->setJsonErrorInPlainEnglish(); |
283
|
|
|
if (is_null($jsonError)) { |
284
|
|
|
return $sReturn; |
285
|
|
|
} else { |
286
|
|
|
return ['error' => $jsonError]; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|