1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Cache;
13
14 use ICanBoogie\I18n;
15
16 class Module extends \Icybee\Module
17 {
18 static public function get_files_stat($path, $pattern=null)
19 {
20 $root = \ICanBoogie\DOCUMENT_ROOT;
21
22 if (!file_exists($path))
23 {
24 $path = $root . $path;
25 }
26
27 if (!file_exists($path))
28 {
29 mkdir($path, 0705, true);
30
31 if (!file_exists($path))
32 {
33 return array
34 (
35 0, '<span class="warning">Impossible de créer le dossier : <em>' . \ICanBoogie\strip_root($path) . '</em></span>'
36 );
37 }
38 }
39
40 if (!is_writable($path))
41 {
42 return array
43 (
44 0, '<span class="warning">Dossier vérouillé en écriture : <em>' . \ICanBoogie\strip_root($path) . '</em></span>'
45 );
46 }
47
48 $n = 0;
49 $size = 0;
50 $iterator = new \DirectoryIterator($path);
51
52 if ($pattern)
53 {
54 $iterator = new \RegexIterator($iterator, $pattern);
55 }
56
57 foreach ($iterator as $file)
58 {
59 $filename = $file->getFilename();
60
61 if ($filename{0} == '.')
62 {
63 continue;
64 }
65
66 ++$n;
67 $size += $file->getSize();
68 }
69
70 return array
71 (
72 $n, I18n\t(':count files<br /><span class="small">:size</span>', array(':count' => $n, 'size' => \ICanBoogie\I18n\format_size($size)))
73 );
74 }
75
76 static public function get_vars_stat($regex)
77 {
78 global $core;
79
80 $n = 0;
81 $size = 0;
82
83 foreach ($core->vars->matching($regex) as $pathname => $fileinfo)
84 {
85 ++$n;
86 $size += $fileinfo->getSize();
87 }
88
89 return array
90 (
91 $n, I18n\t(':count files<br /><span class="small">:size</span>', array(':count' => $n, 'size' => \ICanBoogie\I18n\format_size($size)))
92 );
93 }
94
95 96 97 98 99 100 101
102 static public function clear_files($path, $pattern=null)
103 {
104 $root = \ICanBoogie\DOCUMENT_ROOT;
105
106 if (strpos($path, $root) !== 0)
107 {
108 $path = $root . $path;
109 }
110
111 if (!is_dir($path))
112 {
113 return false;
114 }
115
116 $n = 0;
117 $dh = opendir($path);
118
119 while (($file = readdir($dh)) !== false)
120 {
121 if ($file{0} == '.' || ($pattern && !preg_match($pattern, $file)))
122 {
123 continue;
124 }
125
126 $n++;
127 unlink($path . '/' . $file);
128 }
129
130 return $n;
131 }
132 }