PluginProbe
wpForo Forum / 1.3.0
wpForo Forum v1.3.0
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.3.0, at wpf-includes/class-cache.php

237 lines 7.1 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
7 private $wpforo;
8 public $object;
9 public $dir;
10
11 function __construct( $wpForo ){
12 if(!isset($this->wpforo)) $this->wpforo = $wpForo;
13 $this->init();
14 }
15
16 public function init(){
17 $wp_upload_dir = wp_upload_dir();
18 $uplds_dir = $wp_upload_dir['basedir']."/wpforo";
19 $cache_dir = $uplds_dir . "/cache";
20 if(!is_dir($uplds_dir)) wp_mkdir_p($uplds_dir);
21 if(!is_dir($cache_dir)) $this->dir($cache_dir);
22 $this->dir = $cache_dir;
23 }
24
25 public function get_key( $type = 'html' ){
26 if($type == 'html'){
27 $ug = $this->wpforo->current_user_groupid;
28 return md5( preg_replace('|(.+)\#.+?$|is', '$1', $_SERVER['REQUEST_URI']) . $ug );
29 }
30 }
31
32 private function dir( $cache_dir ){
33
34 $dirs = array( $cache_dir,
35 $cache_dir . '/forum',
36 $cache_dir . '/topic',
37 $cache_dir . '/post',
38 $cache_dir . '/item',
39 $cache_dir . '/item/forum',
40 $cache_dir . '/item/topic',
41 $cache_dir . '/item/post');
42
43 $this->mkdir( $dirs );
44 }
45
46 private function mkdir( $dirs ){
47 foreach( $dirs as $dir ){
48 wp_mkdir_p($dir);
49 wpforo_write_file( $dir . '/index.html' , '' );
50 wpforo_write_file( $dir . '/.htaccess' , 'deny from all' );
51 }
52 }
53
54 public function on( $type = 'object_cashe' ){
55 if( $type == 'html_cashe' ){
56 if( wpforo_feature('output-buffer') && function_exists('ob_start') ){
57 return wpforo_feature('html_cashe', $this->wpforo);
58 }
59 else{
60 return false;
61 }
62 }
63 else{
64 return wpforo_feature($type, $this->wpforo);
65 }
66 }
67
68 public function get( $key, $type = 'loop' ){
69 $template = $this->wpforo->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;
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 = $this->wpforo->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 = $this->wpforo->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 = $this->wpforo->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 = $this->wpforo->forum->get_cache('forums');
123 $this->create_files( $cache, $template );
124 $cache = $this->wpforo->topic->get_cache('topics');
125 $this->create_files( $cache, $template );
126 $cache = $this->wpforo->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( !$this->exists($key, $template) ){
140 $object = serialize($object);
141 wpforo_write_file( $this->dir . '/' . $template . '/' . $type . $key , $object );
142 }
143 }
144 }
145 }
146
147 public function create_html( $content, $template = '', $key = '' ){
148 if( $content ){
149 if( !$this->exists($key, $template) ){
150 wpforo_write_file( $this->dir . '/' . $template . '/' . $key , $content );
151 }
152 }
153 }
154
155 public function filter( $html = '' ){
156 //exit();
157 $html = preg_replace('|<div[\s\t]*id=\"wpf\-msg\-box\"|is', '<div style="display:none;"', $html);
158 return $html;
159 }
160
161 #################################################################################
162 /**
163 * Cleans forum cache
164 *
165 * @since 1.2.1
166 *
167 * @param integer Item ID (e.g.: $topicid or $postid) | (!) ID is 0 on dome actions (e.g.: delete actions)
168 * @param string Item Type (e.g.: 'forum', 'topic', 'post', 'user', 'widget', etc...)
169 *
170 * @return NULL
171 */
172 public function clean( $id, $template ){
173
174 $dirs = array();
175
176 if( $template == 'forum' ){
177 $dirs = array( $this->dir . '/forum', $this->dir . '/item/forum' );
178 }
179 elseif( $template == 'topic' ){
180 $dirs = array( $this->dir . '/forum', $this->dir . '/topic', $this->dir . '/post', $this->dir . '/item', $this->dir . '/item/post', $this->dir . '/item/topic', $this->dir . '/item/forum' );
181 }
182 elseif( $template == 'post' ){
183 $dirs = array( $this->dir . '/forum', $this->dir . '/topic', $this->dir . '/post', $this->dir . '/item', $this->dir . '/item/post', $this->dir . '/item/topic', $this->dir . '/item/forum' );
184 }
185 else{
186 $dirs = array( $this->dir . '/forum', $this->dir . '/topic', $this->dir . '/post', $this->dir . '/item', $this->dir . '/item/post', $this->dir . '/item/topic', $this->dir . '/item/forum' );
187 }
188
189 if(!empty($dirs)){
190 foreach( $dirs as $dir ){
191 $this->clean_files( $dir );
192 }
193 }
194 }
195
196 public function clean_files( $directory ) {
197 $directory_ns = trim( $directory, '/') . '/*';
198 $directory_ws = '/' . trim( $directory, '/') . '/*';
199 $glob = glob( $directory_ns ); if( empty($glob) ) $glob = glob( $directory_ws );
200 foreach( $glob as $item ) {
201 if( strpos($item, 'index.html') !== FALSE || strpos($item, '.htaccess') !== FALSE ) continue;
202 if( !is_dir($item) ) unlink( $item );
203 }
204 }
205
206 public function exists( $key, $template, $type = '' ){
207 $type = ( $type ) ? $type . '/' : '' ;
208 if( file_exists( $this->dir . '/' . $template . '/' . $type . $key ) ){
209 return true;
210 }
211 else{
212 return false;
213 }
214 }
215
216 public function check( $directory ){
217 $filecount = 0;
218 if( class_exists('FilesystemIterator') && is_dir($directory) ){
219 $fi = new FilesystemIterator( $directory, FilesystemIterator::SKIP_DOTS );
220 $filecount = iterator_count($fi);
221 }
222 if( !$filecount ){
223 $directory_ns = trim( $directory, '/') . '/*';
224 $directory_ws = '/' . trim( $directory, '/') . '/*';
225 $files = glob( $directory_ns );
226 if( empty($files) ) $files = glob( $directory_ws );
227 $filecount = count($files);
228 }
229 if( $filecount > 1000 ) {
230 $this->clean_files( $directory );
231 }
232 }
233
234 }
235
236
237 ?>