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