PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wpforo/wpf-includes/class-cache.php

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