PluginProbe
wpForo Forum / 1.4.13
wpForo Forum v1.4.13
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpf-includes / class-cache.php

class-cache.php in wpForo Forum 1.4.13, at wpf-includes/class-cache.php

299 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if( !defined( 'ABSPATH' ) ) exit;
4
5 class wpForoCache{
6 public $object;
7 public $dir;
8 public $lang;
9
10 function __construct(){
11 $this->init();
12 }
13
14 private function init(){
15 $wp_upload_dir = wp_upload_dir();
16 $uplds_dir = $wp_upload_dir['basedir']."/wpforo";
17 $cache_dir = $uplds_dir . "/cache";
18 if(!is_dir($uplds_dir)) wp_mkdir_p($uplds_dir);
19 if(!is_dir($cache_dir)) $this->dir($cache_dir);
20 $this->dir = $cache_dir;
21 $this->lang = get_locale();
22 }
23
24 public function get_key( $type = 'html' ){
25 if($type == 'html'){
26 $ug = WPF()->current_user_groupid;
27 return md5( preg_replace('|(.+)\#.+?$|is', '$1', $_SERVER['REQUEST_URI']) . $ug );
28 }
29 }
30
31 private function dir( $cache_dir ){
32
33 $dirs = array( $cache_dir,
34 $cache_dir . '/forum',
35 $cache_dir . '/topic',
36 $cache_dir . '/post',
37 $cache_dir . '/item',
38 $cache_dir . '/item/forum',
39 $cache_dir . '/item/topic',
40 $cache_dir . '/item/post');
41
42 $this->mkdir( $dirs );
43 }
44
45 private function mkdir( $dirs ){
46 foreach( $dirs as $dir ){
47 wp_mkdir_p($dir);
48 wpforo_write_file( $dir . '/index.html' , '' );
49 wpforo_write_file( $dir . '/.htaccess' , 'deny from all' );
50 }
51 }
52
53 public function on( $type = 'object_cashe' ){
54 if( $type == 'html_cashe' ){
55 if( wpforo_feature('output-buffer') && function_exists('ob_start') ){
56 return wpforo_feature( 'html_cashe');
57 }
58 else{
59 return false;
60 }
61 }
62 else{
63 return wpforo_feature($type);
64 }
65 }
66
67 public function get( $key, $type = 'loop', $template = NULL ){
68
69 $template = ( $template ) ? $template : WPF()->current_object['template'];
70 $loop_templates = array('forum', 'topic', 'post');
71 if( $type == 'loop' && $template ){
72 if( $this->exists($key, $template) ){
73 if( in_array( $template, $loop_templates) ){
74 $cache_file = $this->dir . '/' . $template . '/' . $key;
75 $array = wpforo_get_file_content( $cache_file );
76 return @unserialize( $array );
77 }
78 }
79 }
80 }
81
82 public function get_item( $id, $type = 'post' ){
83 if( $id ){ $key = $id . '_' . $this->lang;
84 if( $this->exists( $key, 'item', $type )){
85 $cache_file = $this->dir . '/item/' . $type . '/' . $key;
86 $array = wpforo_get_file_content( $cache_file );
87 return @unserialize( $array );
88 }
89 }
90 }
91
92 public function get_html(){
93 $template = WPF()->current_object['template'];
94 if( $template == 'forum' ){
95 $key = $this->get_key();
96 if( $this->exists($key, $template) ){
97 $cache_file = $this->dir . '/' . $template . '/' . $key;
98 $html = wpforo_get_file_content( $cache_file );
99 return $this->filter($html);
100 }
101 }
102 return false;
103 }
104
105 public function html( $content ){
106 if(!$this->on('html_cashe')) return false;
107 $template = WPF()->current_object['template'];
108 if( $template == 'forum' ){
109 $key = $this->get_key();
110 $this->create_html( $content, $template, $key );
111 }
112 }
113
114 public function create( $mode = 'loop', $cache = array(), $type = 'post' ){
115
116 if(!$this->on('object_cashe')) return false;
117 $template = WPF()->current_object['template'];
118 if( $template == 'forum' ) { $this->check( $this->dir . '/item/post' ); }
119
120 if( $mode == 'loop' && $template ){
121 if( $template == 'forum' || $template == 'topic' || $template == 'post'){
122 $cache = WPF()->forum->get_cache('forums');
123 $this->create_files( $cache, $template );
124 $cache = WPF()->topic->get_cache('topics');
125 $this->create_files( $cache, $template );
126 $cache = WPF()->post->get_cache('posts');
127 $this->create_files( $cache, $template );
128 }
129 }
130 elseif( $mode == 'item' && !empty($cache) ){
131 $this->create_files( $cache, 'item', $type );
132 }
133 }
134
135 public function create_files( $cache = array(), $template = '', $type = '' ){
136 if( !empty($cache) ){
137 $type = ( $type ) ? $type . '/' : '' ;
138 foreach( $cache as $key => $object ){
139 if($template == 'item') $key = $key . '_' . $this->lang;
140 if( !$this->exists($key, $template) ){
141 $object = serialize($object);
142 wpforo_write_file( $this->dir . '/' . $template . '/' . $type . $key , $object );
143 }
144 }
145 }
146 }
147
148 public function create_html( $content, $template = '', $key = '' ){
149 if( $content ){
150 if( !$this->exists($key, $template) ){
151 wpforo_write_file( $this->dir . '/' . $template . '/' . $key , $content );
152 }
153 }
154 }
155
156 public function create_custom( $args = array(), $items = array(), $template = 'post', $items_count = 0 ){
157 if(empty($args) || !is_array($args)) return;
158 if(empty($items) || !is_array($items)) return;
159 $cache = array(); $hach = serialize($args);
160 $object_key = md5( $hach . WPF()->current_user_groupid );
161 $cache[$object_key]['items'] = $items;
162 $cache[$object_key]['items_count'] = $items_count;
163 $this->create_files( $cache, $template );
164 }
165
166 public function filter( $html = '' ){
167 //exit();
168 $html = preg_replace('|<div[\s\t]*id=\"wpf\-msg\-box\"|is', '<div style="display:none;"', $html);
169 return $html;
170 }
171
172 #################################################################################
173 /**
174 * Cleans forum cache
175 *
176 * @since 1.2.1
177 *
178 * @param integer Item ID (e.g.: $topicid or $postid) | (!) ID is 0 on dome actions (e.g.: delete actions)
179 * @param string Item Type (e.g.: 'forum', 'topic', 'post', 'user', 'widget', etc...)
180 * @param array Item data as array
181 *
182 * @return NULL
183 */
184
185 public function clean( $id, $template, $item = array() ){
186
187 $dirs = array();
188 $userid = (isset($item['userid']) && $item['userid']) ? $item['userid'] : 0;
189 $postid = (isset($item['postid']) && $item['postid']) ? $item['postid'] : 0;
190 $topicid = (isset($item['topicid']) && $item['topicid']) ? $item['topicid'] : 0;
191 $forumid = (isset($item['forumid']) && $item['forumid']) ? $item['forumid'] : 0;
192
193 if( $template == 'forum' || $template == 'forum-soft' ){
194 $id = isset($id) ? $id : $forumid;
195 if( $template == 'forum'){
196 $dirs = array( $this->dir . '/forum', $this->dir . '/item/forum' );
197 }
198 if( $id ){
199 $file = $this->dir . '/item/forum/' . $id . '_' . $this->lang; $this->clean_file( $file );
200 }
201 }
202 elseif( $template == 'topic' || $template == 'topic-soft' ){
203 $id = isset($id) ? $id : $topicid;
204 if( $template == 'topic'){
205 $dirs = array( $this->dir . '/forum', $this->dir . '/item/forum', $this->dir . '/topic', $this->dir . '/post' );
206 }
207 if( $template == 'topic-soft' && $forumid ){
208 $file = $this->dir . '/item/forum/' . $forumid . '_' . $this->lang; $this->clean_file( $file );
209 }
210 if( $id ){
211 $file = $this->dir . '/item/topic/' . $id . '_' . $this->lang; $this->clean_file( $file );
212 $postid = (isset($item['first_postid']) && $item['first_postid']) ? $item['first_postid'] : 0;
213 if( $postid ) $file = $this->dir . '/item/post/' . $postid . '_' . $this->lang; $this->clean_file( $file );
214 }
215 $this->clear_forum_statistic();
216 }
217 elseif( $template == 'post' || $template == 'post-soft' ){
218 $id = isset($id) ? $id : $postid;
219 if( $template == 'post'){
220 $dirs = array( $this->dir . '/forum', $this->dir . '/topic', $this->dir . '/post' );
221 }
222 if( $forumid ){
223 $file = $this->dir . '/item/forum/' . $forumid . '_' . $this->lang; $this->clean_file( $file );
224 }
225 if( $topicid ){
226 $file = $this->dir . '/item/topic/' . $topicid . '_' . $this->lang; $this->clean_file( $file );
227 }
228 if( $id ){
229 $file = $this->dir . '/item/post/' . $id . '_' . $this->lang; $this->clean_file( $file );
230 }
231 $this->clear_forum_statistic();
232 }
233 elseif( $template == 'user' ){
234 //no cache//
235 }
236 elseif( $template == 'loop' ){
237 $dirs = array( $this->dir . '/forum', $this->dir . '/topic', $this->dir . '/post' );
238 }
239 elseif( $template == 'item' ){
240 $dirs = array( $this->dir . '/item/post', $this->dir . '/item/topic', $this->dir . '/item/forum' );
241 }
242 else{
243 $dirs = array( $this->dir . '/forum', $this->dir . '/topic', $this->dir . '/post', $this->dir . '/item/post', $this->dir . '/item/topic', $this->dir . '/item/forum' );
244 }
245
246 if(!empty($dirs)){
247 foreach( $dirs as $dir ){
248 $this->clean_files( $dir );
249 }
250 }
251
252 }
253
254 public function clean_files( $directory ) {
255 $directory_ns = trim( $directory, '/') . '/*';
256 $directory_ws = '/' . trim( $directory, '/') . '/*';
257 $glob = glob( $directory_ns ); if( empty($glob) ) $glob = glob( $directory_ws );
258 foreach( $glob as $item ) {
259 if( strpos($item, 'index.html') !== FALSE || strpos($item, '.htaccess') !== FALSE ) continue;
260 if( !is_dir($item) ) unlink( $item );
261 }
262 }
263
264 public function clean_file( $file ) {
265 if( !is_dir($file) && file_exists($file) ) unlink( $file );
266 }
267
268 public function exists( $key, $template, $type = '' ){
269 $type = ( $type ) ? $type . '/' : '' ;
270 if( file_exists( $this->dir . '/' . $template . '/' . $type . $key ) ){
271 return true;
272 }
273 else{
274 return false;
275 }
276 }
277
278 public function check( $directory ){
279 $filecount = 0;
280 if( class_exists('FilesystemIterator') && is_dir($directory) ){
281 $fi = new FilesystemIterator( $directory, FilesystemIterator::SKIP_DOTS );
282 $filecount = iterator_count($fi);
283 }
284 if( !$filecount ){
285 $directory_ns = trim( $directory, '/') . '/*';
286 $directory_ws = '/' . trim( $directory, '/') . '/*';
287 $files = glob( $directory_ns );
288 if( empty($files) ) $files = glob( $directory_ws );
289 $filecount = count($files);
290 }
291 if( $filecount > 1000 ) {
292 $this->clean_files( $directory );
293 }
294 }
295
296 public function clear_forum_statistic(){
297 WPF()->db->query("DELETE FROM `" . WPF()->db->options."` WHERE `option_name` LIKE 'wpforo_stat%'" );
298 }
299 }