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 CommonLibLocale, |
40
|
|
|
DomComponentsByDanielGP, |
41
|
|
|
DomComponentsByDanielGPwithCDN, |
42
|
|
|
MySQLiByDanielGPqueries, |
43
|
|
|
MySQLiByDanielGP; |
44
|
|
|
|
45
|
|
|
protected function arrayDiffAssocRecursive($array1, $array2) |
46
|
|
|
{ |
47
|
|
|
$difference = []; |
48
|
|
|
foreach ($array1 as $key => $value) { |
49
|
|
|
if (is_array($value)) { |
50
|
|
|
if (!isset($array2[$key]) || !is_array($array2[$key])) { |
51
|
|
|
$difference[$key] = $value; |
52
|
|
|
} else { |
53
|
|
|
$workingDiff = $this->arrayDiffAssocRecursive($value, $array2[$key]); |
54
|
|
|
if (!empty($workingDiff)) { |
55
|
|
|
$difference[$key] = $workingDiff; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) { |
59
|
|
|
$difference[$key] = $value; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
return $difference; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns an array with meaningfull content of permissions |
67
|
|
|
* |
68
|
|
|
* @param int $permissionNumber |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function explainPermissions($permissionNumber) |
72
|
|
|
{ |
73
|
|
|
if (($permissionNumber & 0xC000) == 0xC000) { |
74
|
|
|
$firstFlag = [ |
75
|
|
|
'code' => 's', |
76
|
|
|
'name' => 'Socket', |
77
|
|
|
]; |
78
|
|
|
} elseif (($permissionNumber & 0xA000) == 0xA000) { |
79
|
|
|
$firstFlag = [ |
80
|
|
|
'code' => 'l', |
81
|
|
|
'name' => 'Symbolic Link', |
82
|
|
|
]; |
83
|
|
|
} elseif (($permissionNumber & 0x8000) == 0x8000) { |
84
|
|
|
$firstFlag = [ |
85
|
|
|
'code' => '-', |
86
|
|
|
'name' => 'Regular', |
87
|
|
|
]; |
88
|
|
|
} elseif (($permissionNumber & 0x6000) == 0x6000) { |
89
|
|
|
$firstFlag = [ |
90
|
|
|
'code' => 'b', |
91
|
|
|
'name' => 'Block special', |
92
|
|
|
]; |
93
|
|
|
} elseif (($permissionNumber & 0x4000) == 0x4000) { |
94
|
|
|
$firstFlag = [ |
95
|
|
|
'code' => 'd', |
96
|
|
|
'name' => 'Directory', |
97
|
|
|
]; |
98
|
|
|
} elseif (($permissionNumber & 0x2000) == 0x2000) { |
99
|
|
|
$firstFlag = [ |
100
|
|
|
'code' => 'c', |
101
|
|
|
'name' => 'Character special', |
102
|
|
|
]; |
103
|
|
|
} elseif (($permissionNumber & 0x1000) == 0x1000) { |
104
|
|
|
$firstFlag = [ |
105
|
|
|
'code' => 'p', |
106
|
|
|
'name' => 'FIFO pipe', |
107
|
|
|
]; |
108
|
|
|
} else { |
109
|
|
|
$firstFlag = [ |
110
|
|
|
'code' => 'u', |
111
|
|
|
'name' => 'FIFO pipe', |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
$permissionsString = substr(sprintf('%o', $permissionNumber), -4); |
115
|
|
|
$numericalPermissions = [ |
116
|
|
|
0 => [ |
117
|
|
|
'code' => '---', |
118
|
|
|
'name' => 'none', |
119
|
|
|
], |
120
|
|
|
1 => [ |
121
|
|
|
'code' => '--x', |
122
|
|
|
'name' => 'execute only', |
123
|
|
|
], |
124
|
|
|
2 => [ |
125
|
|
|
'code' => '-w-', |
126
|
|
|
'name' => 'write only', |
127
|
|
|
], |
128
|
|
|
3 => [ |
129
|
|
|
'code' => '-wx', |
130
|
|
|
'name' => 'write and execute', |
131
|
|
|
], |
132
|
|
|
4 => [ |
133
|
|
|
'code' => 'r--', |
134
|
|
|
'name' => 'read only', |
135
|
|
|
], |
136
|
|
|
5 => [ |
137
|
|
|
'code' => 'r-x', |
138
|
|
|
'name' => 'read and execute', |
139
|
|
|
], |
140
|
|
|
6 => [ |
141
|
|
|
'code' => 'rw-', |
142
|
|
|
'name' => 'read and write', |
143
|
|
|
], |
144
|
|
|
7 => [ |
145
|
|
|
'code' => 'rwx', |
146
|
|
|
'name' => 'read, write and execute', |
147
|
|
|
], |
148
|
|
|
]; |
149
|
|
|
return [ |
150
|
|
|
'Code' => $permissionsString, |
151
|
|
|
'Overall' => implode('', [ |
152
|
|
|
$firstFlag['code'], |
153
|
|
|
$numericalPermissions[substr($permissionsString, 1, 1)]['code'], |
154
|
|
|
$numericalPermissions[substr($permissionsString, 2, 1)]['code'], |
155
|
|
|
$numericalPermissions[substr($permissionsString, 3, 1)]['code'], |
156
|
|
|
]), |
157
|
|
|
'First' => $firstFlag, |
158
|
|
|
'Owner' => $numericalPermissions[substr($permissionsString, 1, 1)], |
159
|
|
|
'Group' => $numericalPermissions[substr($permissionsString, 2, 1)], |
160
|
|
|
'World/Other' => $numericalPermissions[substr($permissionsString, 3, 1)], |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Reads the content of a remote file through CURL extension |
166
|
|
|
* |
167
|
|
|
* @param string $fullURL |
168
|
|
|
* @param array $features |
169
|
|
|
* @return blob |
170
|
|
|
*/ |
171
|
|
|
protected function getContentFromUrlThroughCurl($fullURL, $features = null) |
172
|
|
|
{ |
173
|
|
|
if (!function_exists('curl_init')) { |
174
|
|
|
$aReturn['info'] = $this->lclMsgCmn('i18n_Error_ExtensionNotLoaded'); |
|
|
|
|
175
|
|
|
$aReturn['response'] = ''; |
176
|
|
|
} |
177
|
|
|
if (!filter_var($fullURL, FILTER_VALIDATE_URL)) { |
178
|
|
|
$aReturn['info'] = $this->lclMsgCmn('i18n_Error_GivenUrlIsNotValid'); |
|
|
|
|
179
|
|
|
$aReturn['response'] = ''; |
180
|
|
|
} |
181
|
|
|
$aReturn = []; |
182
|
|
|
$chanel = curl_init(); |
183
|
|
|
curl_setopt($chanel, CURLOPT_USERAGENT, $this->getUserAgentByCommonLib()); |
184
|
|
|
if ((strpos($fullURL, 'https') !== false) || (isset($features['forceSSLverification']))) { |
185
|
|
|
curl_setopt($chanel, CURLOPT_SSL_VERIFYHOST, false); |
186
|
|
|
curl_setopt($chanel, CURLOPT_SSL_VERIFYPEER, false); |
187
|
|
|
} |
188
|
|
|
curl_setopt($chanel, CURLOPT_URL, $fullURL); |
189
|
|
|
curl_setopt($chanel, CURLOPT_HEADER, false); |
190
|
|
|
curl_setopt($chanel, CURLOPT_RETURNTRANSFER, true); |
191
|
|
|
curl_setopt($chanel, CURLOPT_FRESH_CONNECT, true); //avoid a cached response |
192
|
|
|
curl_setopt($chanel, CURLOPT_FAILONERROR, true); |
193
|
|
|
$rspJsonFromClient = curl_exec($chanel); |
194
|
|
|
if (curl_errno($chanel)) { |
195
|
|
|
$aReturn['info'] = $this->setArrayToJson([ |
196
|
|
|
'#' => curl_errno($chanel), |
197
|
|
|
'description' => curl_error($chanel) |
198
|
|
|
]); |
199
|
|
|
$aReturn['response'] = ''; |
200
|
|
|
} else { |
201
|
|
|
$aReturn['info'] = $this->setArrayToJson(curl_getinfo($chanel)); |
202
|
|
|
$aReturn['response'] = $rspJsonFromClient; |
203
|
|
|
} |
204
|
|
|
curl_close($chanel); |
205
|
|
|
$sReturn = ''; |
|
|
|
|
206
|
|
View Code Duplication |
if ($this->isJsonByDanielGP($aReturn['info'])) { |
|
|
|
|
207
|
|
|
$sReturn = '"info": ' . $aReturn['info']; |
208
|
|
|
} else { |
209
|
|
|
$sReturn = '"info": {' . $aReturn['info'] . ' }'; |
210
|
|
|
} |
211
|
|
|
$sReturn .= ', '; |
212
|
|
View Code Duplication |
if ($this->isJsonByDanielGP($aReturn['response'])) { |
|
|
|
|
213
|
|
|
$sReturn .= '"response": ' . $aReturn['response']; |
214
|
|
|
} else { |
215
|
|
|
$sReturn .= '"response": { ' . $aReturn['response'] . ' }'; |
216
|
|
|
} |
217
|
|
|
return '{ ' . $sReturn . ' }'; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Reads the content of a remote file through CURL extension |
222
|
|
|
* |
223
|
|
|
* @param string $fullURL |
224
|
|
|
* @param array $features |
225
|
|
|
* @return blob |
226
|
|
|
*/ |
227
|
|
|
protected function getContentFromUrlThroughCurlAsArrayIfJson($fullURL, $features = null) |
228
|
|
|
{ |
229
|
|
|
$result = $this->setJsonToArray($this->getContentFromUrlThroughCurl($fullURL, $features)); |
230
|
|
View Code Duplication |
if (isset($result['info'])) { |
|
|
|
|
231
|
|
|
if (is_array($result['info'])) { |
232
|
|
|
ksort($result['info']); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
View Code Duplication |
if (isset($result['response'])) { |
|
|
|
|
236
|
|
|
if (is_array($result['response'])) { |
237
|
|
|
ksort($result['response']); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
return $result; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
protected function getFeedbackMySQLAffectedRecords() |
244
|
|
|
{ |
245
|
|
|
if (is_null($this->mySQLconnection)) { |
246
|
|
|
$message = 'No MySQL'; |
247
|
|
|
} else { |
248
|
|
|
$afRows = $this->mySQLconnection->affected_rows; |
249
|
|
|
$message = sprintf($this->lclMsgCmnNumber('i18n_Record', 'i18n_Records', $afRows), $afRows); |
250
|
|
|
} |
251
|
|
|
return '<div>' |
252
|
|
|
. $message |
253
|
|
|
. '</div>'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* returns the details about Communicator (current) file |
258
|
|
|
* |
259
|
|
|
* @param string $fileGiven |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
protected function getFileDetails($fileGiven) |
263
|
|
|
{ |
264
|
|
|
if (!file_exists($fileGiven)) { |
265
|
|
|
return [ |
266
|
|
|
'error' => sprintf($this->lclMsgCmn('i18n_Error_GivenFileDoesNotExist'), $fileGiven) |
267
|
|
|
]; |
268
|
|
|
} |
269
|
|
|
$info = new \SplFileInfo($fileGiven); |
270
|
|
|
$sReturn = [ |
271
|
|
|
'File Extension' => $info->getExtension(), |
272
|
|
|
'File Group' => $info->getGroup(), |
273
|
|
|
'File Inode' => $info->getInode(), |
274
|
|
|
'File Link Target' => ($info->isLink() ? $info->getLinkTarget() : '-'), |
275
|
|
|
'File is Dir' => $info->isDir(), |
276
|
|
|
'File is Executable' => $info->isExecutable(), |
277
|
|
|
'File is File' => $info->isFile(), |
278
|
|
|
'File is Link' => $info->isLink(), |
279
|
|
|
'File is Readable' => $info->isReadable(), |
280
|
|
|
'File is Writable' => $info->isWritable(), |
281
|
|
|
'File Name' => $info->getBasename('.' . $info->getExtension()), |
282
|
|
|
'File Name w. Extension' => $info->getFilename(), |
283
|
|
|
'File Owner' => $info->getOwner(), |
284
|
|
|
'File Path' => $info->getPath(), |
285
|
|
|
'File Permissions' => array_merge([ |
286
|
|
|
'Permissions' => $info->getPerms(), |
287
|
|
|
], $this->explainPermissions($info->getPerms())), |
288
|
|
|
'Name' => $info->getRealPath(), |
289
|
|
|
'Size' => $info->getSize(), |
290
|
|
|
'Sha1' => sha1_file($fileGiven), |
291
|
|
|
'Timestamp Accessed' => [ |
292
|
|
|
'PHP number' => $info->getATime(), |
293
|
|
|
'SQL format' => date('Y-m-d H:i:s', $info->getATime()), |
294
|
|
|
], |
295
|
|
|
'Timestamp Changed' => [ |
296
|
|
|
'PHP number' => $info->getCTime(), |
297
|
|
|
'SQL format' => date('Y-m-d H:i:s', $info->getCTime()), |
298
|
|
|
], |
299
|
|
|
'Timestamp Modified' => [ |
300
|
|
|
'PHP number' => $info->getMTime(), |
301
|
|
|
'SQL format' => date('Y-m-d H:i:s', $info->getMTime()), |
302
|
|
|
], |
303
|
|
|
'Type' => $info->getType(), |
304
|
|
|
]; |
305
|
|
|
return $sReturn; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* returns a multi-dimensional array with list of file details within a given path |
310
|
|
|
* (by using Symfony/Finder package) |
311
|
|
|
* |
312
|
|
|
* @param string $pathAnalised |
313
|
|
|
* @return array |
314
|
|
|
*/ |
315
|
|
|
protected function getListOfFiles($pathAnalised) |
316
|
|
|
{ |
317
|
|
|
if (realpath($pathAnalised) === false) { |
318
|
|
|
$aFiles = [ |
319
|
|
|
'error' => sprintf($this->lclMsgCmn('i18n_Error_GivenPathIsNotValid'), $pathAnalised) |
320
|
|
|
]; |
321
|
|
|
} elseif (!is_dir($pathAnalised)) { |
322
|
|
|
$aFiles = [ |
323
|
|
|
'error' => $this->lclMsgCmn('i18n_Error_GivenPathIsNotFolder') |
324
|
|
|
]; |
325
|
|
|
} else { |
326
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
327
|
|
|
$iterator = $finder |
328
|
|
|
->files() |
329
|
|
|
->sortByName() |
330
|
|
|
->in($pathAnalised); |
331
|
|
|
foreach ($iterator as $file) { |
332
|
|
|
$aFiles[$file->getRealPath()] = $this->getFileDetails($file); |
|
|
|
|
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
return $aFiles; |
|
|
|
|
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Returns a complete list of packages and respective details from a composer.lock file |
340
|
|
|
* |
341
|
|
|
* @param string $fileToRead |
342
|
|
|
* @return array |
343
|
|
|
*/ |
344
|
|
|
protected function getPackageDetailsFromGivenComposerLockFile($fileToRead) |
345
|
|
|
{ |
346
|
|
|
if (!file_exists($fileToRead)) { |
347
|
|
|
return ['error' => $fileToRead . ' was not found']; |
348
|
|
|
} |
349
|
|
|
$dNA = '---'; |
350
|
|
|
$alnfo = []; |
351
|
|
|
$packages = $this->getPkgFileInListOfPackageArrayOut($fileToRead); |
352
|
|
|
foreach ($packages['packages'] as $value) { |
353
|
|
|
$basic = $this->getPkgBasicInfo($value, $dNA); |
354
|
|
|
$atr = $this->getPkgOptAtributeAll($value, $dNA); |
355
|
|
|
$alnfo[$value['name']] = array_merge($basic, $atr); |
356
|
|
|
ksort($alnfo); |
357
|
|
|
} |
358
|
|
|
ksort($alnfo); |
359
|
|
|
return $alnfo; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
private function getPkgAging($timePkg) |
363
|
|
|
{ |
364
|
|
|
$dateTimeToday = new \DateTime(date('Y-m-d', strtotime('today'))); |
365
|
|
|
$dateTime = new \DateTime(date('Y-m-d', strtotime($timePkg))); |
366
|
|
|
$interval = $dateTimeToday->diff($dateTime); |
367
|
|
|
return $interval->format('%a days ago'); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
private function getPkgBasicInfo($value, $defaultNA) |
371
|
|
|
{ |
372
|
|
|
return [ |
373
|
|
|
'Aging' => (isset($value['time']) ? $this->getPkgAging($value['time']) : $defaultNA), |
374
|
|
|
'License' => (isset($value['license']) ? $this->getPkgLcns($value['license']) : $defaultNA), |
375
|
|
|
'Notification URL' => (isset($value['version']) ? $value['notification-url'] : $defaultNA), |
376
|
|
|
'Package Name' => $value['name'], |
377
|
|
|
'PHP required' => (isset($value['require']['php']) ? $value['require']['php'] : $defaultNA), |
378
|
|
|
'Product' => explode('/', $value['name'])[1], |
379
|
|
|
'Time' => (isset($value['time']) ? date('l, d F Y H:i:s', strtotime($value['time'])) : ''), |
380
|
|
|
'Time as PHP no.' => (isset($value['time']) ? strtotime($value['time']) : ''), |
381
|
|
|
'Vendor' => explode('/', $value['name'])[0], |
382
|
|
|
'Version no.' => (isset($value['version']) ? $this->getPkgVerNo($value['version']) : $defaultNA), |
383
|
|
|
]; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
private function getPkgFileInListOfPackageArrayOut($fileToRead) |
387
|
|
|
{ |
388
|
|
|
$handle = fopen($fileToRead, 'r'); |
389
|
|
|
$fileContents = fread($handle, filesize($fileToRead)); |
390
|
|
|
fclose($handle); |
391
|
|
|
return $this->setJsonToArray($fileContents); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
private function getPkgLcns($license) |
395
|
|
|
{ |
396
|
|
|
$lcns = $license; |
397
|
|
|
if (is_array($license)) { |
398
|
|
|
$lcns = implode(', ', $license); |
399
|
|
|
} |
400
|
|
|
return $lcns; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
private function getPkgOptAtributeAll($value, $defaultNA) |
404
|
|
|
{ |
405
|
|
|
$attr = ['description', 'homepage', 'type', 'url', 'version']; |
406
|
|
|
$aReturn = []; |
407
|
|
|
foreach ($attr as $valueA) { |
408
|
|
|
$aReturn[ucwords($valueA)] = $defaultNA; |
409
|
|
|
if (in_array($valueA, $value)) { |
410
|
|
|
$aReturn[ucwords($valueA)] = $valueA; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
return $aReturn; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
private function getPkgVerNo($version) |
417
|
|
|
{ |
418
|
|
|
$vrs = $version; |
419
|
|
|
if (substr($version, 0, 1) == 'v') { |
420
|
|
|
$vrs = substr($version, 1, strlen($version) - 1); |
421
|
|
|
} |
422
|
|
|
if (strpos($vrs, '-') !== false) { |
423
|
|
|
$vrs = substr($v, 0, strpos($vrs, '-')); |
|
|
|
|
424
|
|
|
} |
425
|
|
|
return $vrs; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Returns server Timestamp into various formats |
430
|
|
|
* |
431
|
|
|
* @param string $returnType |
432
|
|
|
* @return string |
433
|
|
|
*/ |
434
|
|
|
protected function getTimestamp($returnType = 'string') |
435
|
|
|
{ |
436
|
|
|
$crtTime = gettimeofday(); |
437
|
|
|
switch ($returnType) { |
438
|
|
|
case 'array': |
439
|
|
|
$sReturn = [ |
440
|
|
|
'float' => ($crtTime['sec'] + $crtTime['usec'] / pow(10, 6)), |
441
|
|
|
'string' => implode('', [ |
442
|
|
|
'<span style="color:black!important;font-weight:bold;">[', |
443
|
|
|
date('Y-m-d H:i:s.', $crtTime['sec']), |
444
|
|
|
substr(round($crtTime['usec'], -3), 0, 3), |
445
|
|
|
']</span> ' |
446
|
|
|
]), |
447
|
|
|
]; |
448
|
|
|
break; |
449
|
|
|
case 'float': |
450
|
|
|
$sReturn = ($crtTime['sec'] + $crtTime['usec'] / pow(10, 6)); |
451
|
|
|
break; |
452
|
|
|
case 'string': |
453
|
|
|
$sReturn = implode('', [ |
454
|
|
|
'<span style="color:black!important;font-weight:bold;">[', |
455
|
|
|
date('Y-m-d H:i:s.', $crtTime['sec']), |
456
|
|
|
substr(round($crtTime['usec'], -3), 0, 3), |
457
|
|
|
']</span> ' |
458
|
|
|
]); |
459
|
|
|
break; |
460
|
|
|
default: |
461
|
|
|
$sReturn = sprintf($this->lclMsgCmn('i18n_Error_UnknownReturnType'), $returnType); |
462
|
|
|
break; |
463
|
|
|
} |
464
|
|
|
return $sReturn; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Tests if given string has a valid Json format |
469
|
|
|
* |
470
|
|
|
* @param string $inputJson |
471
|
|
|
* @return boolean|string |
472
|
|
|
*/ |
473
|
|
|
protected function isJsonByDanielGP($inputJson) |
474
|
|
|
{ |
475
|
|
|
if (is_string($inputJson)) { |
476
|
|
|
json_decode($inputJson); |
477
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
478
|
|
|
} else { |
479
|
|
|
return $this->lclMsgCmn('i18n_Error_GivenInputIsNotJson'); |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Moves files into another folder |
485
|
|
|
* |
486
|
|
|
* @param type $sourcePath |
487
|
|
|
* @param type $targetPath |
488
|
|
|
* @param type $overwrite |
|
|
|
|
489
|
|
|
* @return type |
490
|
|
|
*/ |
491
|
|
|
protected function moveFilesIntoTargetFolder($sourcePath, $targetPath) |
492
|
|
|
{ |
493
|
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem(); |
494
|
|
|
$filesystem->mirror($sourcePath, $targetPath); |
495
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
496
|
|
|
$iterator = $finder |
497
|
|
|
->files() |
498
|
|
|
->ignoreUnreadableDirs(true) |
499
|
|
|
->followLinks() |
500
|
|
|
->in($sourcePath); |
501
|
|
|
$sFiles = []; |
502
|
|
|
foreach ($iterator as $file) { |
503
|
|
|
$relativePathFile = str_replace($sourcePath, '', $file->getRealPath()); |
504
|
|
|
if (!file_exists($targetPath . $relativePathFile)) { |
505
|
|
|
$sFiles[$relativePathFile] = $targetPath . $relativePathFile; |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
return $this->setArrayToJson($sFiles); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Remove files older than given rule |
513
|
|
|
* (both Access time and Modified time will be checked |
514
|
|
|
* and only if both matches removal will take place) |
515
|
|
|
* |
516
|
|
|
* @param array $inputArray |
517
|
|
|
* @return string |
518
|
|
|
*/ |
519
|
|
|
protected function removeFilesOlderThanGivenRule($inputArray) |
520
|
|
|
{ |
521
|
|
|
if (is_array($inputArray)) { |
522
|
|
|
if (!isset($inputArray['path'])) { |
523
|
|
|
$proceedWithDeletion = false; |
524
|
|
|
$error = '`path` has not been provided'; |
525
|
|
|
} elseif (!isset($inputArray['dateRule'])) { |
526
|
|
|
$proceedWithDeletion = false; |
527
|
|
|
$error = '`dateRule` has not been provided'; |
528
|
|
|
} else { |
529
|
|
|
$proceedWithDeletion = true; |
530
|
|
|
} |
531
|
|
|
} else { |
532
|
|
|
$proceedWithDeletion = false; |
533
|
|
|
} |
534
|
|
|
if ($proceedWithDeletion) { |
535
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
536
|
|
|
$iterator = $finder |
537
|
|
|
->files() |
538
|
|
|
->ignoreUnreadableDirs(true) |
539
|
|
|
->followLinks() |
540
|
|
|
->in($inputArray['path']); |
541
|
|
|
$aFiles = null; |
542
|
|
|
foreach ($iterator as $file) { |
543
|
|
|
if ($file->getATime() < strtotime($inputArray['dateRule'])) { |
544
|
|
|
$aFiles[] = $file->getRealPath(); |
545
|
|
|
} |
546
|
|
|
} |
547
|
|
|
if (is_null($aFiles)) { |
548
|
|
|
return null; |
549
|
|
|
} else { |
550
|
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem(); |
551
|
|
|
$filesystem->remove($aFiles); |
552
|
|
|
return $this->setArrayToJson($aFiles); |
553
|
|
|
} |
554
|
|
|
} else { |
555
|
|
|
return $error; |
|
|
|
|
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Send an array of parameters like a form through a POST action |
561
|
|
|
* |
562
|
|
|
* @param string $urlToSendTo |
563
|
|
|
* @param array $params |
564
|
|
|
* @throws \Exception |
565
|
|
|
* @throws \UnexpectedValueException |
566
|
|
|
*/ |
567
|
|
|
protected function sendBackgroundEncodedFormElementsByPost($urlToSendTo, $params = []) |
|
|
|
|
568
|
|
|
{ |
569
|
|
|
try { |
570
|
|
|
$postingUrl = filter_var($urlToSendTo, FILTER_VALIDATE_URL); |
571
|
|
|
if ($postingUrl === false) { |
572
|
|
|
throw new \Exception($exc); |
573
|
|
|
} else { |
574
|
|
|
if (is_array($params)) { |
575
|
|
|
$postingString = $this->setArrayToStringForUrl('&', $params); |
576
|
|
|
$postingUrlParts = parse_url($postingUrl); |
577
|
|
|
$postingPort = (isset($postingUrlParts['port']) ? $postingUrlParts['port'] : 80); |
578
|
|
|
$flPointer = fsockopen($postingUrlParts['host'], $postingPort, $errNo, $errorMessage, 30); |
579
|
|
|
if ($flPointer === false) { |
580
|
|
|
throw new \UnexpectedValueException($this->lclMsgCmn('i18n_Error_FailedToConnect') . ': ' |
581
|
|
|
. $errNo . ' (' . $errorMessage . ')'); |
582
|
|
|
} else { |
583
|
|
|
$out[] = 'POST ' . $postingUrlParts['path'] . ' ' . $_SERVER['SERVER_PROTOCOL']; |
|
|
|
|
584
|
|
|
$out[] = 'Host: ' . $postingUrlParts['host']; |
585
|
|
|
if (isset($_SERVER['HTTP_USER_AGENT'])) { |
586
|
|
|
$out[] = 'User-Agent: ' . filter_var($_SERVER['HTTP_USER_AGENT'], FILTER_SANITIZE_STRING); |
587
|
|
|
} |
588
|
|
|
$out[] = 'Content-Type: application/x-www-form-urlencoded'; |
589
|
|
|
$out[] = 'Content-Length: ' . strlen($postingString); |
590
|
|
|
$out[] = 'Connection: Close' . "\r\n"; |
591
|
|
|
$out[] = $postingString; |
592
|
|
|
fwrite($flPointer, implode("\r\n", $out)); |
593
|
|
|
fclose($flPointer); |
594
|
|
|
} |
595
|
|
|
} else { |
596
|
|
|
throw new \UnexpectedValueException($this->lclMsgCmn('i18n_Error_GivenParameterIsNotAnArray')); |
597
|
|
|
} |
598
|
|
|
} |
599
|
|
|
} catch (\Exception $exc) { |
600
|
|
|
echo '<pre style="color:#f00">' . $exc->getTraceAsString() . '</pre>'; |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Converts an array into JSON string |
606
|
|
|
* |
607
|
|
|
* @param array $inArray |
608
|
|
|
* @return string |
609
|
|
|
*/ |
610
|
|
|
protected function setArrayToJson(array $inArray) |
611
|
|
|
{ |
612
|
|
|
if (!is_array($inArray)) { |
613
|
|
|
return $this->lclMsgCmn('i18n_Error_GivenInputIsNotArray'); |
614
|
|
|
} |
615
|
|
|
$rtrn = utf8_encode(json_encode($inArray, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); |
616
|
|
|
$jsonError = $this->setJsonErrorInPlainEnglish(); |
617
|
|
|
if (is_null($jsonError)) { |
618
|
|
|
return $rtrn; |
619
|
|
|
} else { |
620
|
|
|
return $jsonError; |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* Replace space with break line for each key element |
626
|
|
|
* |
627
|
|
|
* @param array $aElements |
628
|
|
|
* @return array |
629
|
|
|
*/ |
630
|
|
|
protected function setArrayToArrayKbr(array $aElements) |
631
|
|
|
{ |
632
|
|
|
foreach ($aElements as $key => $value) { |
633
|
|
|
$aReturn[str_replace(' ', '<br/>', $key)] = $value; |
|
|
|
|
634
|
|
|
} |
635
|
|
|
return $aReturn; |
|
|
|
|
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Converts a single-child array into an parent-child one |
640
|
|
|
* |
641
|
|
|
* @param type $inArray |
642
|
|
|
* @return type |
643
|
|
|
*/ |
644
|
|
|
protected function setArrayValuesAsKey(array $inArray) |
645
|
|
|
{ |
646
|
|
|
$outArray = array_combine($inArray, $inArray); |
647
|
|
|
ksort($outArray); |
648
|
|
|
return $outArray; |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Returns proper result from a mathematical division in order to avoid Zero division erorr or Infinite results |
653
|
|
|
* |
654
|
|
|
* @param float $fAbove |
655
|
|
|
* @param float $fBelow |
656
|
|
|
* @param mixed $mArguments |
657
|
|
|
* @return decimal |
658
|
|
|
*/ |
659
|
|
|
protected function setDividedResult($fAbove, $fBelow, $mArguments = 0) |
660
|
|
|
{ |
661
|
|
|
// prevent infinite result AND division by 0 |
662
|
|
|
if (($fAbove == 0) || ($fBelow == 0)) { |
663
|
|
|
$nReturn = 0; |
664
|
|
|
} else { |
665
|
|
|
if (is_array($mArguments)) { |
666
|
|
|
$nReturn = $this->setNumberFormat(($fAbove / $fBelow), [ |
667
|
|
|
'MinFractionDigits' => $mArguments[1], |
668
|
|
|
'MaxFractionDigits' => $mArguments[1], |
669
|
|
|
]); |
670
|
|
|
} else { |
671
|
|
|
$nReturn = $this->setNumberFormat(round(($fAbove / $fBelow), $mArguments)); |
672
|
|
|
} |
673
|
|
|
} |
674
|
|
|
return $nReturn; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* Provides a list of all known JSON errors and their description |
679
|
|
|
* |
680
|
|
|
* @return type |
681
|
|
|
*/ |
682
|
|
|
private function setJsonErrorInPlainEnglish() |
683
|
|
|
{ |
684
|
|
|
$knownErrors = [ |
685
|
|
|
JSON_ERROR_NONE => null, |
686
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
687
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
688
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
689
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
690
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
691
|
|
|
]; |
692
|
|
|
$currentError = json_last_error(); |
693
|
|
|
$sReturn = null; |
694
|
|
|
if (in_array($currentError, $knownErrors)) { |
695
|
|
|
$sReturn = $knownErrors[$currentError]; |
696
|
|
|
} |
697
|
|
|
return $sReturn; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* Converts a JSON string into an Array |
702
|
|
|
* |
703
|
|
|
* @param string $inputJson |
704
|
|
|
* @return array |
705
|
|
|
*/ |
706
|
|
|
protected function setJsonToArray($inputJson) |
707
|
|
|
{ |
708
|
|
|
if (!$this->isJsonByDanielGP($inputJson)) { |
709
|
|
|
return [ |
710
|
|
|
'error' => $this->lclMsgCmn('i18n_Error_GivenInputIsNotJson') |
711
|
|
|
]; |
712
|
|
|
} |
713
|
|
|
$sReturn = (json_decode($inputJson, true)); |
714
|
|
|
$jsonError = $this->setJsonErrorInPlainEnglish(); |
715
|
|
|
if (is_null($jsonError)) { |
716
|
|
|
return $sReturn; |
717
|
|
|
} else { |
718
|
|
|
return [ |
719
|
|
|
'error' => $jsonError |
720
|
|
|
]; |
721
|
|
|
} |
722
|
|
|
} |
723
|
|
|
} |
724
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.