1<?php
2
3  global $filedels, $dirdels;
4  $cachedir = DOKU_INC. str_replace("./", "", $conf['savedir'])."/cache";
5  $time_older = time() - $conf['cachetime']; // purge only files older than cachetime
6
7  $params = 0x01 + 0x02 + 0x04 + 0x08 + 0x10 + 0x20 + 0x40;
8
9  $filedels = 0;
10  $dirdels = 0;
11	rmeverything_cache($cachedir, $cachedir, $params, $time_older);
12  logMessage(" Cache:: files deleted: ".$filedels.", folders deleted: ".$dirdels);
13
14  $lockdir  = DOKU_INC. str_replace("./", "", $conf['savedir'])."/locks";
15  $time_older =  time() - $conf['locktime']; // purge only files older than locktime
16  $filedels = 0;
17  $dirdels = 0;
18	removelock($lockdir, $lockdir, $time_older);
19  logMessage(" Lock:: files deleted: ".$filedels.", folders deleted: ".$dirdels);
20
21
22  /**
23	* delete files into cache directory older $time_older
24	*/
25	function rmeverything_cache($fileglob, $basedir, $params, $time_older) {
26    global $filedels, $dirdels;
27
28		$fileglob2 = substr($fileglob, strlen($basedir));
29		if (strpos($fileglob, '*') !== false) {
30			foreach (glob($fileglob) as $filename) {
31				rmeverything_cache($filename, $basedir, $params, $time_older);
32			}
33		} else if (is_file($fileglob)) {
34			if (strcmp($fileglob2, '/_dummy') == 0) return true;
35			$pathinfor = pathinfo($fileglob2);
36			if (strcmp($basedir, dirname($fileglob)) == 0) {
37				if (!($params & 0x02)) return true;
38			} else {
39        if ($pathinfor['mtime'] > $time_older) return true;
40				if (substr_count(strtolower($pathinfor['basename']), '.media.') > 0) {
41					if (!($params & 0x40)) return true;
42				} else if (strcmp(strtolower($pathinfor['extension']), 'i') == 0) {
43					if (!($params & 0x04)) return true;
44				} else if (strcmp(strtolower($pathinfor['extension']), 'xhtml') == 0) {
45					if (!($params & 0x08)) return true;
46				} else if (strcmp(strtolower($pathinfor['extension']), 'js') == 0) {
47					if (!($params & 0x10)) return true;
48				} else if (strcmp(strtolower($pathinfor['extension']), 'css') == 0) {
49					if (!($params & 0x20)) return true;
50				} else {
51					if (!($params & 0x01)) return true;
52				}
53			}
54			if (@unlink($fileglob)) {
55				$filedels++;
56				return true;
57			} else {
58				return false;
59			}
60		} else if (is_dir($fileglob)) {
61			$ok = rmeverything_cache($fileglob.'/*', $basedir, $params, $time_older);
62			if (!$ok) return false;
63			if (strcmp($fileglob, $basedir) == 0) return true;
64			if (@rmdir($fileglob)) {
65				$dirdels++;
66				return true;
67			} else {
68				return true;
69			}
70		} else {
71			return false;
72		}
73		return true;
74	}
75
76
77	/**
78	* remove stale lock files
79	*/
80	function removelock($fileglob, $basedir, $time_older) {
81    global $filedels, $dirdels;
82    global $conf;
83
84		$fileglob2 = substr($fileglob, strlen($basedir));
85		if (strpos($fileglob, '*') !== false) {
86			foreach (glob($fileglob) as $filename) {
87				removelock($filename, $basedir, $time_older);
88			}
89		} else if (is_file($fileglob)) {
90			if (strcmp($fileglob2, '/_dummy') == 0) return true;
91			$pathinfor = pathinfo($fileglob2);
92			if (strcmp(strtolower($pathinfor['extension']), 'lock') != 0) return true;
93      if (@filemtime($fileglob) > $time_older) return true;
94			if (@unlink($fileglob)) {
95				$filedels++;
96				return true;
97			} else {
98				return false;
99			}
100		} else if (is_dir($fileglob)) {
101			$ok = removelock($fileglob.'/*', $basedir, $time_older);
102			if (!$ok) return false;
103			if (strcmp($fileglob, $basedir) == 0) return true;
104			if (@rmdir($fileglob)) {
105				$dirdels++;
106				return true;
107			} else {
108				return true;
109			}
110		} else {
111			return false;
112		}
113		return true;
114	}
115
116?>