1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cSystemPurge {
25:
26: 27: 28: 29: 30:
31: private $_dirsExcluded = array(
32: 'code',
33: 'templates_c'
34: );
35:
36: 37: 38: 39: 40:
41: private $_dirsExcludedWithFiles = array(
42: '.',
43: '..',
44: '.svn',
45: '.cvs',
46: '.htaccess',
47: '.git',
48: '.gitignore',
49: '.keep',
50: );
51:
52: 53: 54: 55:
56: private $_logFileTypes = array(
57: 'txt'
58: );
59:
60: 61: 62: 63:
64: private $_cronjobFileTypes = array(
65: 'job'
66: );
67:
68: 69: 70:
71: public function __construct() {
72:
73: $dirsToExcludeWithFiles = getSystemProperty('system', 'purge-dirstoexclude-withfiles');
74: $aDirsToExcludeWithFiles = array_map('trim', explode(',', $dirsToExcludeWithFiles));
75: if (count($aDirsToExcludeWithFiles) < 1 || empty($aDirsToExcludeWithFiles[0])) {
76: $aDirsToExcludeWithFiles = $this->_dirsExcludedWithFiles;
77: setSystemProperty('system', 'purge-dirstoexclude-withfiles', implode(',', $aDirsToExcludeWithFiles));
78: }
79:
80: $this->_dirsExcludedWithFiles = $aDirsToExcludeWithFiles;
81: }
82:
83: 84: 85: 86: 87: 88:
89: public function resetClientConCode($clientId) {
90: global $perm, $currentuser;
91: $cfgClient = cRegistry::getClientConfig();
92:
93: if (cFileHandler::exists($cfgClient[$clientId]['cache']['path'] . 'code/') === false) {
94: return false;
95: }
96:
97: if ($perm->isClientAdmin($clientId, $currentuser) === false && $perm->isSysadmin($currentuser) === false) {
98: return false;
99: }
100:
101:
102: foreach (new DirectoryIterator($cfgClient[$clientId]['code']['path']) as $file) {
103: if ($file->isFile() === false) {
104: continue;
105: }
106:
107: $extension = substr($file, strrpos($file->getBasename(), '.') + 1);
108: if ($extension != 'php') {
109: continue;
110: }
111:
112: if (cFileHandler::remove($cfgClient[$clientId]['code']['path'] . '/' . $file->getFilename()) === false) {
113: return false;
114: }
115: }
116:
117: return true;
118: }
119:
120: 121: 122: 123: 124: 125:
126: public function resetClientConCatArt($clientId) {
127: global $perm, $currentuser;
128: $db = cRegistry::getDb();
129: $cfg = cRegistry::getConfig();
130:
131: if ($perm->isClientAdmin($clientId, $currentuser) || $perm->isSysadmin($currentuser)) {
132: $sSql = ' UPDATE ' . $cfg['tab']['cat_art'] . ' cca, ' . $cfg['tab']['cat'] . ' cc, ' . $cfg['tab']['art'] . ' ca ' . ' SET cca.createcode=1 ' . ' WHERE cc.idcat = cca.idcat ' . ' AND ca.idart = cca.idart ' . ' AND cc.idclient = ' . (int) $clientId . ' AND ca.idclient = ' . (int) $clientId;
133: $db->query($sSql);
134:
135: return ($db->getErrorMessage() == '') ? true : false;
136: } else {
137: return false;
138: }
139: }
140:
141: 142: 143: 144: 145:
146: public function resetConInuse() {
147: global $perm, $currentuser;
148: $db = cRegistry::getDb();
149: $cfg = cRegistry::getConfig();
150:
151: if ($perm->isSysadmin($currentuser)) {
152: $sql = 'DELETE FROM ' . $cfg['tab']['inuse'];
153: $db->query($sql);
154:
155: return ($db->getErrorMessage() == '') ? true : false;
156: } else {
157: return false;
158: }
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function clearClientCache($clientId) {
168: global $perm, $currentuser;
169: $cfgClient = cRegistry::getClientConfig();
170:
171: if ($perm->isClientAdmin($clientId, $currentuser) || $perm->isSysadmin($currentuser)) {
172: $cacheDir = $cfgClient[$clientId]['cache']['path'];
173: if (is_dir($cacheDir)) {
174: return ($this->clearDir($cacheDir, $cacheDir) ? true : false);
175: }
176: return false;
177: } else {
178: return false;
179: }
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: public function clearClientHistory($clientId, $keep, $fileNumber) {
191: global $perm, $currentuser;
192: $cfgClient = cRegistry::getClientConfig();
193:
194: if ($perm->isClientAdmin($clientId, $currentuser) || $perm->isSysadmin($currentuser)) {
195: $versionDir = $cfgClient[$clientId]['version']['path'];
196: if (is_dir($versionDir)) {
197: $tmpFile = array();
198: $this->clearDir($versionDir, $versionDir, $keep, $tmpFile);
199: if (count($tmpFile) > 0) {
200: foreach ($tmpFile as $sKey => $aFiles) {
201:
202: array_multisort($tmpFile[$sKey]);
203:
204: $count = count($tmpFile[$sKey]);
205:
206: $countDelete = ($count <= $fileNumber) ? 0 : ($count - $fileNumber);
207:
208: for ($i = 0; $i < $countDelete; $i++) {
209: if (cFileHandler::exists($tmpFile[$sKey][$i]) && is_writable($tmpFile[$sKey][$i])) {
210: unlink($tmpFile[$sKey][$i]);
211: }
212: }
213: }
214: }
215:
216: return true;
217: }
218: return false;
219: } else {
220: return false;
221: }
222: }
223:
224: 225: 226: 227: 228: 229:
230: public function clearClientLog($clientId) {
231: global $perm, $currentuser;
232: $cfgClient = cRegistry::getClientConfig();
233:
234: if ($perm->isClientAdmin($clientId, $currentuser) || $perm->isSysadmin($currentuser)) {
235: $logDir = $cfgClient[$clientId]['log']['path'];
236: if (is_dir($logDir)) {
237: return $this->emptyFile($logDir, $this->_logFileTypes);
238: }
239: return false;
240: } else {
241: return false;
242: }
243: }
244:
245: 246: 247: 248: 249:
250: public function clearConLog() {
251: global $perm, $currentuser;
252: $cfg = cRegistry::getConfig();
253:
254: $logDir = $cfg['path']['contenido_logs'];
255: if ($perm->isSysadmin($currentuser)) {
256: if (is_dir($logDir)) {
257: return $this->emptyFile($logDir, $this->_logFileTypes);
258: }
259: return false;
260: } else {
261: return false;
262: }
263: }
264:
265: 266: 267: 268: 269:
270: public function clearConCronjob() {
271: global $perm, $currentuser;
272: $cfg = cRegistry::getConfig();
273:
274: $cronjobDir = $cfg['path']['contenido_cronlog'];
275: if ($perm->isSysadmin($currentuser)) {
276: if (is_dir($cronjobDir)) {
277: return $this->emptyFile($cronjobDir, $this->_cronjobFileTypes);
278: }
279: return false;
280: } else {
281: return false;
282: }
283: }
284:
285: 286: 287: 288: 289:
290: public function clearConCache() {
291: global $perm, $currentuser;
292: $cfg = cRegistry::getConfig();
293:
294: $cacheDir = $cfg['path']['contenido_cache'];
295: if ($perm->isSysadmin($currentuser)) {
296: if (is_dir($cacheDir)) {
297: return ($this->clearDir($cacheDir, $cacheDir) ? true : false);
298: }
299: return false;
300: } else {
301: return false;
302: }
303: }
304:
305: 306: 307: 308: 309: 310:
311: public function clearArticleCache($idartlang) {
312: $cfgClient = cRegistry::getClientConfig();
313: $client = cRegistry::getClientId();
314:
315: $artLang = new cApiArticleLanguage($idartlang);
316: $idlang = $artLang->get('idlang');
317: $idart = $artLang->get('idart');
318: $art = new cApiArticle($idart);
319: $idclient = $art->get('idclient');
320:
321: $catArtColl = new cApiCategoryArticleCollection();
322: $catArtColl->select('idart=' . $idart);
323: while (($item = $catArtColl->next()) !== false) {
324: $filename = $cfgClient[$client]['code']['path'] . $idclient . '.' . $idlang . '.' . $item->get('idcatart') . '.php';
325: if (cFileHandler::exists($filename)) {
326: cFileHandler::remove($filename);
327: }
328: }
329: }
330:
331: 332: 333: 334: 335: 336: 337: 338: 339:
340: public function clearDir($dirPath, $tmpDirPath, $keep = false, &$tmpFileList = array()) {
341: if (is_dir($dirPath) && ($handle = opendir($dirPath))) {
342: $tmp = str_replace(array(
343: '/',
344: '..'
345: ), '', $dirPath);
346: while (false !== ($file = readdir($handle))) {
347: if (!in_array($file, $this->_dirsExcludedWithFiles)) {
348: $filePath = $dirPath . '/' . $file;
349: $filePath = str_replace('//', '/', $filePath);
350: if (is_dir($filePath)) {
351: $this->clearDir($filePath, $tmpDirPath, $keep, $tmpFileList);
352: } else {
353: if ($keep === false) {
354: unlink($filePath);
355: } else {
356: $tmpFileList[$tmp][] = $filePath;
357: }
358: }
359: }
360: }
361:
362: $dirs = explode('/', $dirPath);
363: if (end($dirs) == '') {
364: array_pop($dirs);
365: }
366: $dirName = end($dirs);
367:
368: if (str_replace(array(
369: '/',
370: '..'
371: ), '', $dirPath) != str_replace(array(
372: '/',
373: '..'
374: ), '', $tmpDirPath) && $keep === false && !in_array($dirName, $this->_dirsExcludedWithFiles) && !in_array($dirName, $this->_dirsExcluded)) {
375: rmdir($dirPath);
376: }
377:
378: closedir($handle);
379:
380: return true;
381: } else {
382: return false;
383: }
384: }
385:
386: 387: 388: 389: 390: 391: 392:
393: public function emptyFile($dirPath, $types) {
394: $count = 0;
395: $countCleared = 0;
396: if (is_dir($dirPath) && ($handle = opendir($dirPath))) {
397: while (false !== ($file = readdir($handle))) {
398: $fileExt = trim(end(explode('.', $file)));
399:
400: if ($file != '.' && $file != '..' && in_array($fileExt, $types)) {
401: $filePath = $dirPath . '/' . $file;
402:
403: if (cFileHandler::exists($filePath) && is_writable($filePath)) {
404: $count++;
405:
406: if (cFileHandler::truncate($filePath)) {
407: $countCleared++;
408: }
409: }
410: }
411: }
412:
413:
414: return ($count == $countCleared) ? true : false;
415: }
416:
417: return false;
418: }
419:
420: 421: 422: 423: 424: 425:
426: public function getClientDir($clientId) {
427: $cfgClient = cRegistry::getClientConfig();
428:
429: return $cfgClient[$clientId]['path']['frontend'];
430: }
431:
432: 433: 434: 435: 436:
437: public function setLogFileTypes($types) {
438: if (count($types) > 0) {
439: foreach ($types as $type) {
440: $this->_logFileTypes[] = $type;
441: }
442: }
443: }
444:
445: 446: 447: 448: 449:
450: public function setCronjobFileTypes($types) {
451: if (count($types) > 0) {
452: foreach ($types as $type) {
453: $this->_cronjobFileTypes[] = $type;
454: }
455: }
456: }
457:
458: }
459: