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) && false !== ($handle = cDirHandler::read($dirPath))) {
342: $tmp = str_replace(array(
343: '/',
344: '..'
345: ), '', $dirPath);
346: foreach ($handle as $file) {
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: cFileHandler::remove($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)
375: && $keep === false) {
376:
377: $bCanDelete = true;
378: $dirContent = cDirHandler::read($dirPath);
379: foreach ($dirContent as $sContent) {
380: if (in_array($sContent, $this->_dirsExcludedWithFiles)
381: || in_array($dirContent, $this->_dirsExcluded)) {
382: $bCanDelete = false;
383: break;
384: }
385: }
386: if (true === $bCanDelete
387: && in_array($dirName, $this->_dirsExcluded)) {
388: $bCanDelete = false;
389: }
390: }
391:
392: if (true === $bCanDelete) {
393: cDirHandler::remove($dirPath);
394: }
395:
396: return true;
397: } else {
398: return false;
399: }
400: }
401:
402: 403: 404: 405: 406: 407: 408:
409: public function emptyFile($dirPath, $types) {
410: $count = 0;
411: $countCleared = 0;
412:
413: if (is_dir($dirPath) && false !== ($handle = cDirHandler::read($dirPath))) {
414: foreach ($handle as $file) {
415: $fileExt = trim(end(explode('.', $file)));
416:
417: if ($file != '.' && $file != '..' && in_array($fileExt, $types)) {
418: $filePath = $dirPath . '/' . $file;
419:
420: if (cFileHandler::exists($filePath) && cFileHandler::writeable($filePath)) {
421: $count++;
422:
423: if (cFileHandler::truncate($filePath)) {
424: $countCleared++;
425: }
426: }
427: }
428: }
429:
430:
431: return ($count == $countCleared) ? true : false;
432: }
433:
434: return false;
435: }
436:
437: 438: 439: 440: 441: 442:
443: public function getClientDir($clientId) {
444: $cfgClient = cRegistry::getClientConfig();
445:
446: return $cfgClient[$clientId]['path']['frontend'];
447: }
448:
449: 450: 451: 452: 453:
454: public function setLogFileTypes($types) {
455: if (count($types) > 0) {
456: foreach ($types as $type) {
457: $this->_logFileTypes[] = $type;
458: }
459: }
460: }
461:
462: 463: 464: 465: 466:
467: public function setCronjobFileTypes($types) {
468: if (count($types) > 0) {
469: foreach ($types as $type) {
470: $this->_cronjobFileTypes[] = $type;
471: }
472: }
473: }
474:
475: }
476: