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:
312: public function clearArticleCache($idartlang) {
313: $cfgClient = cRegistry::getClientConfig();
314: $client = cRegistry::getClientId();
315:
316: $artLang = new cApiArticleLanguage($idartlang);
317: $idlang = $artLang->get('idlang');
318: $idart = $artLang->get('idart');
319: $art = new cApiArticle($idart);
320: $idclient = $art->get('idclient');
321:
322: $catArtColl = new cApiCategoryArticleCollection();
323: $catArtColl->select('idart=' . $idart);
324: while (($item = $catArtColl->next()) !== false) {
325: $filename = $cfgClient[$client]['code']['path'] . $idclient . '.' . $idlang . '.' . $item->get('idcatart') . '.php';
326: if (cFileHandler::exists($filename)) {
327: cFileHandler::remove($filename);
328: }
329: }
330: }
331:
332: 333: 334: 335: 336: 337: 338: 339: 340:
341: public function clearDir($dirPath, $tmpDirPath, $keep = false, &$tmpFileList = array()) {
342: if (is_dir($dirPath) && ($handle = opendir($dirPath))) {
343: $tmp = str_replace(array(
344: '/',
345: '..'
346: ), '', $dirPath);
347: while (false !== ($file = readdir($handle))) {
348: if (!in_array($file, $this->_dirsExcludedWithFiles)) {
349: $filePath = $dirPath . '/' . $file;
350: $filePath = str_replace('//', '/', $filePath);
351: if (is_dir($filePath)) {
352: $this->clearDir($filePath, $tmpDirPath, $keep, $tmpFileList);
353: } else {
354: if ($keep === false) {
355: unlink($filePath);
356: } else {
357: $tmpFileList[$tmp][] = $filePath;
358: }
359: }
360: }
361: }
362:
363: $dirs = explode('/', $dirPath);
364: if (end($dirs) == '') {
365: array_pop($dirs);
366: }
367: $dirName = end($dirs);
368:
369: if (str_replace(array(
370: '/',
371: '..'
372: ), '', $dirPath) != str_replace(array(
373: '/',
374: '..'
375: ), '', $tmpDirPath) && $keep === false && !in_array($dirName, $this->_dirsExcludedWithFiles) && !in_array($dirName, $this->_dirsExcluded)) {
376: rmdir($dirPath);
377: }
378:
379: closedir($handle);
380:
381: return true;
382: } else {
383: return false;
384: }
385: }
386:
387: 388: 389: 390: 391: 392: 393:
394: public function emptyFile($dirPath, $types) {
395: $count = 0;
396: $countCleared = 0;
397: if (is_dir($dirPath) && ($handle = opendir($dirPath))) {
398: while (false !== ($file = readdir($handle))) {
399: $fileExt = trim(end(explode('.', $file)));
400:
401: if ($file != '.' && $file != '..' && in_array($fileExt, $types)) {
402: $filePath = $dirPath . '/' . $file;
403:
404: if (cFileHandler::exists($filePath) && is_writable($filePath)) {
405: $count++;
406:
407: if (cFileHandler::truncate($filePath)) {
408: $countCleared++;
409: }
410: }
411: }
412: }
413:
414:
415: return ($count == $countCleared)? true : false;
416: }
417:
418: return false;
419: }
420:
421: 422: 423: 424: 425: 426:
427: public function getClientDir($clientId) {
428: $cfgClient = cRegistry::getClientConfig();
429:
430: return $cfgClient[$clientId]['path']['frontend'];
431: }
432:
433: 434: 435: 436: 437:
438: public function setLogFileTypes($types) {
439: if (count($types) > 0) {
440: foreach ($types as $type) {
441: $this->_logFileTypes[] = $type;
442: }
443: }
444: }
445:
446: 447: 448: 449: 450:
451: public function setCronjobFileTypes($types) {
452: if (count($types) > 0) {
453: foreach ($types as $type) {
454: $this->_cronjobFileTypes[] = $type;
455: }
456: }
457: }
458:
459: }
460: