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