panel admina
[watermeloncms.git] / wtrmln / libs / cache.php
blob91b20d0909fb6d0ed1db8424b24dd1cfdae554e4
1 <?php
2 /********************************************************************
4 Watermelon CMS
6 Copyright 2009 Radosław Pietruszewski
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ********************************************************************/
24 * Lib Cache
25 * wersja 1.1.0
27 * Cache'owanie
31 class Cache
34 * public static void CacheView(string $name, string $content)
36 * cache'uje widok o nazwie $name z treścią $content
38 * string $name - nazwa widoku, wraz z folderem i końcówką .php
39 * np. 'pw/pwlist.php'
40 * string $content - treść widoku do zacache'owania
43 public static function CacheView($name, $content)
45 if(strpos($name, '/') === false)
47 $fp = fopen(WTRMLN_CACHE . 'views/' . $name, 'w');
48 fwrite($fp, $content);
49 fclose($fp);
51 else
53 $name = explode('/', $name);
54 $dir = $name[0];
55 $name = $name[1];
57 if(!file_exists(WTRMLN_CACHE . 'views/' . $dir))
59 mkdir(WTRMLN_CACHE . 'views/' . $dir);
62 $fp = fopen(WTRMLN_CACHE . 'views/' . $dir . '/' . $name, 'w');
63 fwrite($fp, $content);
64 fclose($fp);
69 * public static string GetView(string $name)
71 * zwraca zacache'owany widok o nazwie $name
72 * zwraca false gdy widok wcale nie został zacache'owany
74 * string $name - nazwa widoku, wraz z folderem i końcówką .php
75 * np. 'pw/pwlist.php'
78 public static function GetView($name)
80 $name = explode('/', $name);
81 $dir = $name[0];
82 $name = $name[1];
84 $path = WTRMLN_CACHE . 'views/' . $dir . '/' . $name;
86 if(!file_exists($path))
88 return false;
91 return file_get_contents($path);
95 * public static void CacheBBCode(string $bbcode, string $parsed)
97 * cache'uje fragment treści bbcode
99 * string $bbcode - fragment bbcode do zacache'owania
100 * string $parsed - ten sam fragment, ale po parsowaniu
103 public static function CacheBBCode($bbcode, $parsed)
105 $bbcodehash = strHash($bbcode);
107 $fp = fopen(WTRMLN_CACHE . 'bbcode/' . $bbcodehash . '.php', 'w');
108 fwrite($fp, '<?php exit; ?>' . $parsed);
109 fclose($fp);
113 * public static string GetBBCode(string $bbcode)
115 * pobiera i zwraca sparsowaną formę BBCode'u
116 * o treści $bbcode
119 public static function GetBBCode($bbcode)
121 $bbcodehash = strHash($bbcode);
123 $path = WTRMLN_CACHE . 'bbcode/' . $bbcodehash . '.php';
125 if(!file_exists($path))
127 return false;
130 return substr(file_get_contents($path), 14);
134 * public static void ClearCache()
136 * czyści cache
139 public static function ClearCache()
141 foreach(new DirectoryIterator(WTRMLN_CACHE . 'views/') as $_res)
143 if($_res->isDot())
145 unset($_res);
146 continue;
149 if($_res->isFile())
151 self::removeResource($_res->getPathName());
153 elseif($_res->isDir())
155 self::removeResource($_res->getRealPath());
158 unset($_res);
161 foreach(new DirectoryIterator(WTRMLN_CACHE . 'bbcode/') as $_res)
163 if($_res->isDot())
165 unset($_res);
166 continue;
169 if($_res->isFile())
171 self::removeResource($_res->getPathName());
173 elseif($_res->isDir())
175 self::removeResource($_res->getRealPath());
178 unset($_res);
183 * private static void removeResource(string $_target)
185 * niszczy zasób (folder lub plik)
187 * based on http://pl.php.net/manual/pl/function.rmdir.php#86112
189 * string $_target - ścieżka zasobu do usunięcia
192 private static function removeResource($_target)
194 if(is_file($_target))
196 if(is_writable($_target))
198 if(@unlink($_target))
200 return true;
204 return false;
207 if(is_dir($_target))
209 if(is_writeable($_target))
211 foreach(new DirectoryIterator($_target) as $_res)
213 if($_res->isDot())
215 unset($_res);
216 continue;
219 if($_res->isFile())
221 self::removeResource($_res->getPathName());
223 elseif($_res->isDir())
225 self::removeResource($_res->getRealPath());
228 unset($_res);
231 if(@rmdir($_target))
233 return true;
237 return false;