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