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
|
|
|
* useful functions to get quick results |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait CommonBasic |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use CommonPermissions; |
40
|
|
|
|
41
|
|
|
protected function arrayDiffAssocRecursive($array1, $array2) |
42
|
|
|
{ |
43
|
|
|
$difference = []; |
44
|
|
|
foreach ($array1 as $key => $value) { |
45
|
|
|
if (is_array($value)) { |
46
|
|
|
if (!isset($array2[$key]) || !is_array($array2[$key])) { |
47
|
|
|
$difference[$key] = $value; |
48
|
|
|
} else { |
49
|
|
|
$workingDiff = $this->arrayDiffAssocRecursive($value, $array2[$key]); |
50
|
|
|
if (!empty($workingDiff)) { |
51
|
|
|
$difference[$key] = $workingDiff; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) { |
55
|
|
|
$difference[$key] = $value; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return $difference; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the details about Communicator (current) file |
63
|
|
|
* w/o any kind of verification of file existance |
64
|
|
|
* |
65
|
|
|
* @param string $fileGiven |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function getFileDetailsRaw($fileGiven) |
69
|
|
|
{ |
70
|
|
|
$info = new \SplFileInfo($fileGiven); |
71
|
|
|
$aFileBasicDetails = [ |
72
|
|
|
'File Extension' => $info->getExtension(), |
73
|
|
|
'File Group' => $info->getGroup(), |
74
|
|
|
'File Inode' => $info->getInode(), |
75
|
|
|
'File Link Target' => ($info->isLink() ? $info->getLinkTarget() : '-'), |
76
|
|
|
'File Name' => $info->getBasename('.' . $info->getExtension()), |
77
|
|
|
'File Name w. Extension' => $info->getFilename(), |
78
|
|
|
'File Owner' => $info->getOwner(), |
79
|
|
|
'File Path' => $info->getPath(), |
80
|
|
|
'Name' => $info->getRealPath(), |
81
|
|
|
'Type' => $info->getType(), |
82
|
|
|
]; |
83
|
|
|
$aDetails = array_merge($aFileBasicDetails, $this->getFileDetailsRawStatistic($info, $fileGiven)); |
84
|
|
|
ksort($aDetails); |
85
|
|
|
return $aDetails; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getFileDetailsRawStatistic(\SplFileInfo $info, $fileGiven) |
89
|
|
|
{ |
90
|
|
|
return [ |
91
|
|
|
'File is Dir' => $info->isDir(), |
92
|
|
|
'File is Executable' => $info->isExecutable(), |
93
|
|
|
'File is File' => $info->isFile(), |
94
|
|
|
'File is Link' => $info->isLink(), |
95
|
|
|
'File is Readable' => $info->isReadable(), |
96
|
|
|
'File is Writable' => $info->isWritable(), |
97
|
|
|
'File Permissions' => $this->explainPerms($info->getPerms()), |
98
|
|
|
'Size' => $info->getSize(), |
99
|
|
|
'Sha1' => sha1_file($fileGiven), |
100
|
|
|
'Timestamp Accessed' => $this->getFileTimes($info->getATime()), |
101
|
|
|
'Timestamp Changed' => $this->getFileTimes($info->getCTime()), |
102
|
|
|
'Timestamp Modified' => $this->getFileTimes($info->getMTime()), |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getFileTimes($timeAsPhpNumber) |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
|
|
'PHP number' => $timeAsPhpNumber, |
110
|
|
|
'SQL format' => date('Y-m-d H:i:s', $timeAsPhpNumber), |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Moves files into another folder |
116
|
|
|
* |
117
|
|
|
* @param string $sourcePath |
118
|
|
|
* @param string $targetPath |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
protected function moveFilesIntoTargetFolder($sourcePath, $targetPath) |
122
|
|
|
{ |
123
|
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem(); |
124
|
|
|
$filesystem->mirror($sourcePath, $targetPath); |
125
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
126
|
|
|
$iterator = $finder->files()->ignoreUnreadableDirs(true)->followLinks()->in($sourcePath); |
127
|
|
|
$sFiles = []; |
128
|
|
|
foreach ($iterator as $file) { |
129
|
|
|
$relativePathFile = str_replace($sourcePath, '', $file->getRealPath()); |
130
|
|
|
if (!file_exists($targetPath . $relativePathFile)) { |
131
|
|
|
$sFiles[$relativePathFile] = $targetPath . $relativePathFile; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return $this->setArrayToJson($sFiles); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function removeFilesDecision($inputArray) |
138
|
|
|
{ |
139
|
|
|
if (is_array($inputArray)) { |
140
|
|
|
if (!array_key_exists('path', $inputArray)) { |
141
|
|
|
return '`path` has not been provided'; |
142
|
|
|
} elseif (!array_key_exists('dateRule', $inputArray)) { |
143
|
|
|
return '`dateRule` has not been provided'; |
144
|
|
|
} |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Remove files older than given rule |
152
|
|
|
* (both Access time and Modified time will be checked |
153
|
|
|
* and only if both matches removal will take place) |
154
|
|
|
* |
155
|
|
|
* @param array $inputArray |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function removeFilesOlderThanGivenRule($inputArray) |
159
|
|
|
{ |
160
|
|
|
$aFiles = $this->retrieveFilesOlderThanGivenRule($inputArray); |
161
|
|
|
if (is_array($aFiles)) { |
162
|
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem(); |
163
|
|
|
$filesystem->remove($aFiles); |
164
|
|
|
return $this->setArrayToJson($aFiles); |
165
|
|
|
} |
166
|
|
|
return $aFiles; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function retrieveFilesOlderThanGivenRule($inputArray) |
170
|
|
|
{ |
171
|
|
|
$proceedRetrieving = $this->removeFilesDecision($inputArray); |
172
|
|
|
if ($proceedRetrieving === true) { |
173
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
174
|
|
|
$iterator = $finder->files()->ignoreUnreadableDirs(true)->followLinks()->in($inputArray['path']); |
175
|
|
|
$aFiles = []; |
176
|
|
|
foreach ($iterator as $file) { |
177
|
|
|
if ($file->getATime() <= strtotime($inputArray['dateRule'])) { |
178
|
|
|
$aFiles[] = $file->getRealPath(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
return $aFiles; |
182
|
|
|
} |
183
|
|
|
return $proceedRetrieving; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Replace space with break line for each key element |
188
|
|
|
* |
189
|
|
|
* @param array $aElements |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
protected function setArrayToArrayKbr(array $aElements) |
193
|
|
|
{ |
194
|
|
|
$aReturn = []; |
195
|
|
|
foreach ($aElements as $key => $value) { |
196
|
|
|
$aReturn[str_replace(' ', '<br/>', $key)] = $value; |
197
|
|
|
} |
198
|
|
|
return $aReturn; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Converts a single-child array into an parent-child one |
203
|
|
|
* |
204
|
|
|
* @param array $inArray |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function setArrayValuesAsKey(array $inArray) |
208
|
|
|
{ |
209
|
|
|
$outArray = array_combine($inArray, $inArray); |
210
|
|
|
ksort($outArray); |
211
|
|
|
return $outArray; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Converts an array into JSON string |
216
|
|
|
* |
217
|
|
|
* @param array $inArray |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function setArrayToJson(array $inArray) |
221
|
|
|
{ |
222
|
|
|
$rtrn = utf8_encode(json_encode($inArray, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); |
223
|
|
|
$jsonError = $this->setJsonErrorInPlainEnglish(); |
224
|
|
|
if ($jsonError == '') { |
225
|
|
|
$jsonError = $rtrn; |
226
|
|
|
} |
227
|
|
|
return $jsonError; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Provides a list of all known JSON errors and their description |
232
|
|
|
* |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
protected function setJsonErrorInPlainEnglish() |
236
|
|
|
{ |
237
|
|
|
$knownErrors = [ |
238
|
|
|
JSON_ERROR_NONE => '', |
239
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
240
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
241
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
242
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
243
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
244
|
|
|
]; |
245
|
|
|
return $knownErrors[json_last_error()]; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|