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: public function resetClientConCode($clientId) {
88: global $perm, $currentuser;
89: $cfgClient = cRegistry::getClientConfig();
90:
91: if (cFileHandler::exists($cfgClient[$clientId]['cache']['path'] . 'code/') === false) {
92: return false;
93: }
94:
95: if ($perm->isClientAdmin($clientId, $currentuser) === false && $perm->isSysadmin($currentuser) === false) {
96: return false;
97: }
98:
99:
100: foreach (new DirectoryIterator($cfgClient[$clientId]['code']['path']) as $file) {
101: if ($file->isFile() === false) {
102: continue;
103: }
104:
105: $extension = substr($file, strrpos($file->getBasename(), '.') + 1);
106: if ($extension != 'php') {
107: continue;
108: }
109:
110: if (cFileHandler::remove($cfgClient[$clientId]['code']['path'] . '/' . $file->getFilename()) === false) {
111: return false;
112: }
113: }
114:
115: return true;
116: }
117:
118: 119: 120: 121: 122: 123:
124: public function resetClientConCatArt($clientId) {
125: global $perm, $currentuser;
126: $db = cRegistry::getDb();
127: $cfg = cRegistry::getConfig();
128:
129: if ($perm->isClientAdmin($clientId, $currentuser) || $perm->isSysadmin($currentuser)) {
130: $db->query('
131: UPDATE
132: ' . $cfg['tab']['cat_art'] . ' cca,
133: ' . $cfg['tab']['cat'] . ' cc,
134: ' . $cfg['tab']['art'] . ' ca
135: SET
136: cca.createcode=1
137: WHERE
138: cc.idcat = cca.idcat
139: AND ca.idart = cca.idart
140: AND cc.idclient = ' . (int) $clientId . '
141: AND ca.idclient = ' . (int) $clientId);
142:
143: return ($db->getErrorMessage() == '') ? true : false;
144: } else {
145: return false;
146: }
147: }
148:
149: 150: 151: 152: 153:
154: public function resetConInuse() {
155: global $perm, $currentuser;
156: $db = cRegistry::getDb();
157: $cfg = cRegistry::getConfig();
158:
159: if ($perm->isSysadmin($currentuser)) {
160: $sql = 'DELETE FROM ' . $cfg['tab']['inuse'];
161: $db->query($sql);
162:
163: return ($db->getErrorMessage() == '') ? true : false;
164: } else {
165: return false;
166: }
167: }
168:
169: 170: 171: 172: 173: 174:
175: public function clearClientCache($clientId) {
176: global $perm, $currentuser;
177: $cfgClient = cRegistry::getClientConfig();
178:
179: if ($perm->isClientAdmin($clientId, $currentuser) || $perm->isSysadmin($currentuser)) {
180: $cacheDir = $cfgClient[$clientId]['cache']['path'];
181: if (cDirHandler::exists($cacheDir)) {
182: return ($this->clearDir($cacheDir, $cacheDir) ? true : false);
183: }
184: return false;
185: } else {
186: return false;
187: }
188: }
189:
190: 191: 192: 193: 194: 195: 196: 197:
198: public function clearClientHistory($clientId, $keep, $fileNumber) {
199: global $perm, $currentuser;
200: $cfgClient = cRegistry::getClientConfig();
201:
202: if ($perm->isClientAdmin($clientId, $currentuser) || $perm->isSysadmin($currentuser)) {
203: $versionDir = $cfgClient[$clientId]['version']['path'];
204: if (cDirHandler::exists($versionDir)) {
205: $tmpFile = array();
206: $this->clearDir($versionDir, $versionDir, $keep, $tmpFile);
207: if (count($tmpFile) > 0) {
208: foreach ($tmpFile as $sKey => $aFiles) {
209:
210: array_multisort($tmpFile[$sKey]);
211:
212: $count = count($tmpFile[$sKey]);
213:
214: $countDelete = ($count <= $fileNumber) ? 0 : ($count - $fileNumber);
215:
216: for ($i = 0; $i < $countDelete; $i++) {
217: if (cFileHandler::exists($tmpFile[$sKey][$i]) && is_writable($tmpFile[$sKey][$i])) {
218: unlink($tmpFile[$sKey][$i]);
219: }
220: }
221: }
222: }
223:
224: return true;
225: }
226: return false;
227: } else {
228: return false;
229: }
230: }
231:
232: 233: 234: 235: 236: 237:
238: public function clearClientContentVersioning($idclient) {
239: global $perm, $currentuser;
240:
241: if ($perm->isClientAdmin($idclient, $currentuser) || $perm->isSysadmin($currentuser)) {
242:
243: $artLangVersionColl = new cApiArticleLanguageVersionCollection();
244: $artLangVersionColl->deleteByWhereClause('idartlangversion != 0');
245:
246: $contentVersionColl = new cApiContentVersionCollection();
247: $contentVersionColl->deleteByWhereClause('idcontentversion != 0');
248:
249: $metaTagVersionColl = new cApiMetaTagVersionCollection();
250: $metaTagVersionColl->deleteByWhereClause('idmetatagversion != 0');
251:
252: return true;
253: } else {
254: return false;
255: }
256: }
257:
258: 259: 260: 261: 262: 263:
264: public function clearClientLog($idclient) {
265: global $perm, $currentuser;
266: $cfgClient = cRegistry::getClientConfig();
267:
268: if ($perm->isClientAdmin($idclient, $currentuser) || $perm->isSysadmin($currentuser)) {
269: $logDir = $cfgClient[$idclient]['log']['path'];
270: if (cDirHandler::exists($logDir)) {
271: return $this->emptyFile($logDir, $this->_logFileTypes);
272: }
273: return false;
274: } else {
275: return false;
276: }
277: }
278:
279: 280: 281: 282: 283:
284: public function clearConLog() {
285: global $perm, $currentuser;
286: $cfg = cRegistry::getConfig();
287:
288: $logDir = $cfg['path']['contenido_logs'];
289: if ($perm->isSysadmin($currentuser)) {
290: if (cDirHandler::exists($logDir)) {
291: return $this->emptyFile($logDir, $this->_logFileTypes);
292: }
293: return false;
294: } else {
295: return false;
296: }
297: }
298:
299: 300: 301: 302: 303:
304: public function clearConCronjob() {
305: global $perm, $currentuser;
306: $cfg = cRegistry::getConfig();
307:
308: $cronjobDir = $cfg['path']['contenido_cronlog'];
309: if ($perm->isSysadmin($currentuser)) {
310: if (cDirHandler::exists($cronjobDir)) {
311: return $this->emptyFile($cronjobDir, $this->_cronjobFileTypes);
312: }
313: return false;
314: } else {
315: return false;
316: }
317: }
318:
319: 320: 321: 322: 323:
324: public function clearConCache() {
325: global $perm, $currentuser;
326: $cfg = cRegistry::getConfig();
327:
328: $cacheDir = $cfg['path']['contenido_cache'];
329: if ($perm->isSysadmin($currentuser)) {
330: if (cDirHandler::exists($cacheDir)) {
331: return ($this->clearDir($cacheDir, $cacheDir) ? true : false);
332: }
333: return false;
334: } else {
335: return false;
336: }
337: }
338:
339: 340: 341: 342: 343: 344: 345:
346: public function clearArticleCache($idartlang) {
347: $cfgClient = cRegistry::getClientConfig();
348: $client = cRegistry::getClientId();
349:
350: $artLang = new cApiArticleLanguage($idartlang);
351: $idlang = $artLang->get('idlang');
352: $idart = $artLang->get('idart');
353: $art = new cApiArticle($idart);
354: $idclient = $art->get('idclient');
355:
356: $catArtColl = new cApiCategoryArticleCollection();
357: $catArtColl->select('idart=' . $idart);
358: while (($item = $catArtColl->next()) !== false) {
359: $filename = $cfgClient[$client]['code']['path'] . $idclient . '.' . $idlang . '.' . $item->get('idcatart') . '.php';
360: if (cFileHandler::exists($filename)) {
361: cFileHandler::remove($filename);
362: }
363: }
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376:
377: public function clearDir($dirPath, $tmpDirPath, $keep = false, &$tmpFileList = array()) {
378: if (cDirHandler::exists($dirPath) && false !== ($handle = cDirHandler::read($dirPath))) {
379: $tmp = str_replace(array(
380: '/',
381: '..'
382: ), '', $dirPath);
383: foreach ($handle as $file) {
384: if (!in_array($file, $this->_dirsExcludedWithFiles)) {
385: $filePath = $dirPath . '/' . $file;
386: $filePath = str_replace('//', '/', $filePath);
387: if (cDirHandler::exists($filePath)) {
388: $this->clearDir($filePath, $tmpDirPath, $keep, $tmpFileList);
389: } else {
390: if ($keep === false) {
391: cFileHandler::remove($filePath);
392: } else {
393: $tmpFileList[$tmp][] = $filePath;
394: }
395: }
396: }
397: }
398:
399: $dirs = explode('/', $dirPath);
400: if (end($dirs) == '') {
401: array_pop($dirs);
402: }
403: $dirName = end($dirs);
404:
405: if (str_replace(array(
406: '/',
407: '..'
408: ), '', $dirPath) != str_replace(array(
409: '/',
410: '..'
411: ), '', $tmpDirPath)
412: && $keep === false) {
413:
414: $bCanDelete = true;
415: $dirContent = cDirHandler::read($dirPath);
416: foreach ($dirContent as $sContent) {
417: if (in_array($sContent, $this->_dirsExcludedWithFiles)
418: || in_array($dirContent, $this->_dirsExcluded)) {
419: $bCanDelete = false;
420: break;
421: }
422: }
423: if (true === $bCanDelete
424: && in_array($dirName, $this->_dirsExcluded)) {
425: $bCanDelete = false;
426: }
427: }
428:
429: if (true === $bCanDelete) {
430: cDirHandler::remove($dirPath);
431: }
432:
433: return true;
434: } else {
435: return false;
436: }
437: }
438:
439: 440: 441: 442: 443: 444: 445:
446: public function emptyFile($dirPath, $types) {
447: $count = 0;
448: $countCleared = 0;
449:
450: if (cDirHandler::exists($dirPath) && false !== ($handle = cDirHandler::read($dirPath))) {
451: foreach ($handle as $file) {
452: $fileExt = trim(end(explode('.', $file)));
453:
454: if ($file != '.' && $file != '..' && in_array($fileExt, $types)) {
455: $filePath = $dirPath . '/' . $file;
456:
457: if (cFileHandler::exists($filePath) && cFileHandler::writeable($filePath)) {
458: $count++;
459:
460: if (cFileHandler::truncate($filePath)) {
461: $countCleared++;
462: }
463: }
464: }
465: }
466:
467:
468: return ($count == $countCleared) ? true : false;
469: }
470:
471: return false;
472: }
473:
474: 475: 476: 477: 478: 479:
480: public function getClientDir($clientId) {
481: $cfgClient = cRegistry::getClientConfig();
482:
483: return $cfgClient[$clientId]['path']['frontend'];
484: }
485:
486: 487: 488: 489: 490:
491: public function setLogFileTypes($types) {
492: if (count($types) > 0) {
493: foreach ($types as $type) {
494: $this->_logFileTypes[] = $type;
495: }
496: }
497: }
498:
499: 500: 501: 502: 503:
504: public function setCronjobFileTypes($types) {
505: if (count($types) > 0) {
506: foreach ($types as $type) {
507: $this->_cronjobFileTypes[] = $type;
508: }
509: }
510: }
511:
512: }
513: