plugins/admin/CacheManager.php
changeset 1227 bdac73ed481e
parent 1081 745200a9cc2a
equal deleted inserted replaced
1226:de56132c008d 1227:bdac73ed481e
    13 
    13 
    14 // Cache manager - regenerate and clear various cached values
    14 // Cache manager - regenerate and clear various cached values
    15 
    15 
    16 function page_Admin_CacheManager()
    16 function page_Admin_CacheManager()
    17 {
    17 {
    18   global $db, $session, $paths, $template, $plugins; // Common objects
    18 	global $db, $session, $paths, $template, $plugins; // Common objects
    19   global $lang;
    19 	global $lang;
    20   global $cache;
    20 	global $cache;
    21   if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN )
    21 	if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN )
    22   {
    22 	{
    23     $login_link = makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true);
    23 		$login_link = makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true);
    24     echo '<h3>' . $lang->get('adm_err_not_auth_title') . '</h3>';
    24 		echo '<h3>' . $lang->get('adm_err_not_auth_title') . '</h3>';
    25     echo '<p>' . $lang->get('adm_err_not_auth_body', array( 'login_link' => $login_link )) . '</p>';
    25 		echo '<p>' . $lang->get('adm_err_not_auth_body', array( 'login_link' => $login_link )) . '</p>';
    26     return;
    26 		return;
    27   }
    27 	}
    28   
    28 	
    29   // validation/actions
    29 	// validation/actions
    30   if ( isset($_POST['refresh']) || isset($_POST['clear']) )
    30 	if ( isset($_POST['refresh']) || isset($_POST['clear']) )
    31   {
    31 	{
    32     $success = false;
    32 		$success = false;
    33     
    33 		
    34     $target = ( isset($_POST['refresh']) ) ? $_POST['refresh'] : $_POST['clear'];
    34 		$target = ( isset($_POST['refresh']) ) ? $_POST['refresh'] : $_POST['clear'];
    35     $do_refresh = isset($_POST['refresh']);
    35 		$do_refresh = isset($_POST['refresh']);
    36     switch ( $target )
    36 		switch ( $target )
    37     {
    37 		{
    38       case 'page':
    38 			case 'page':
    39         $success = $cache->purge('page_meta');
    39 				$success = $cache->purge('page_meta');
    40         if ( $do_refresh && $success )
    40 				if ( $do_refresh && $success )
    41           $success = $paths->update_metadata_cache();
    41 					$success = $paths->update_metadata_cache();
    42         break;
    42 				break;
    43       case 'ranks':
    43 			case 'ranks':
    44         $success = $cache->purge('ranks');
    44 				$success = $cache->purge('ranks');
    45         if ( $do_refresh && $success )
    45 				if ( $do_refresh && $success )
    46           $success = generate_cache_userranks();
    46 					$success = generate_cache_userranks();
    47         break;
    47 				break;
    48       case 'sidebar':
    48 			case 'sidebar':
    49         $success = $cache->purge('anon_sidebar');
    49 				$success = $cache->purge('anon_sidebar');
    50         break;
    50 				break;
    51       case 'plugins':
    51 			case 'plugins':
    52         $success = $cache->purge('plugins');
    52 				$success = $cache->purge('plugins');
    53         if ( $do_refresh && $success )
    53 				if ( $do_refresh && $success )
    54           $success = $plugins->generate_plugins_cache();
    54 					$success = $plugins->generate_plugins_cache();
    55         break;
    55 				break;
    56       case 'template':
    56 			case 'template':
    57         if ( $dh = opendir(ENANO_ROOT . '/cache') )
    57 				if ( $dh = opendir(ENANO_ROOT . '/cache') )
    58         {
    58 				{
    59           while ( $file = @readdir($dh) )
    59 					while ( $file = @readdir($dh) )
    60           {
    60 					{
    61             $fullpath = ENANO_ROOT . "/cache/$file";
    61 						$fullpath = ENANO_ROOT . "/cache/$file";
    62             // we don't want to mess with directories
    62 						// we don't want to mess with directories
    63             if ( !is_file($fullpath) )
    63 						if ( !is_file($fullpath) )
    64               continue;
    64 							continue;
    65             
    65 						
    66             if ( preg_match('/\.(?:tpl|css)\.php$/', $file) )
    66 						if ( preg_match('/\.(?:tpl|css)\.php$/', $file) )
    67             {
    67 						{
    68               unlink($fullpath);
    68 							unlink($fullpath);
    69             }
    69 						}
    70           }
    70 					}
    71           $success = true;
    71 					$success = true;
    72         }
    72 				}
    73         break;
    73 				break;
    74       case 'aes':
    74 			case 'aes':
    75         $success = @unlink(ENANO_ROOT . '/cache/aes_decrypt.php');
    75 				$success = @unlink(ENANO_ROOT . '/cache/aes_decrypt.php');
    76         break;
    76 				break;
    77       case 'lang':
    77 			case 'lang':
    78         if ( $dh = opendir(ENANO_ROOT . '/cache') )
    78 				if ( $dh = opendir(ENANO_ROOT . '/cache') )
    79         {
    79 				{
    80           while ( $file = @readdir($dh) )
    80 					while ( $file = @readdir($dh) )
    81           {
    81 					{
    82             $fullpath = ENANO_ROOT . "/cache/$file";
    82 						$fullpath = ENANO_ROOT . "/cache/$file";
    83             // we don't want to mess with directories
    83 						// we don't want to mess with directories
    84             if ( !is_file($fullpath) )
    84 						if ( !is_file($fullpath) )
    85               continue;
    85 							continue;
    86             
    86 						
    87             if ( preg_match('/^lang_json_(?:[a-f0-9]+?)\.php$/', $file) || preg_match('/^(?:cache_)?lang_(?:[0-9]+?)\.php$/', $file) )
    87 						if ( preg_match('/^lang_json_(?:[a-f0-9]+?)\.php$/', $file) || preg_match('/^(?:cache_)?lang_(?:[0-9]+?)\.php$/', $file) )
    88               unlink($fullpath);
    88 							unlink($fullpath);
    89           }
    89 					}
    90           $success = true;
    90 					$success = true;
    91         }
    91 				}
    92         if ( $do_refresh && $success )
    92 				if ( $do_refresh && $success )
    93         {
    93 				{
    94           // for each language in the database, call regen_caches()
    94 					// for each language in the database, call regen_caches()
    95           $q = $db->sql_query('SELECT lang_id FROM ' . table_prefix . 'language;');
    95 					$q = $db->sql_query('SELECT lang_id FROM ' . table_prefix . 'language;');
    96           if ( !$q )
    96 					if ( !$q )
    97             $db->_die();
    97 						$db->_die();
    98           while ( $row = $db->fetchrow($q) )
    98 					while ( $row = $db->fetchrow($q) )
    99           {
    99 					{
   100             $lang_local = ( $row['lang_id'] == $lang->lang_id ) ? $lang : new Language($row['lang_id']);
   100 						$lang_local = ( $row['lang_id'] == $lang->lang_id ) ? $lang : new Language($row['lang_id']);
   101             $success = $lang_local->regen_caches();
   101 						$success = $lang_local->regen_caches();
   102             if ( !$success )
   102 						if ( !$success )
   103               break 2;
   103 							break 2;
   104           }
   104 					}
   105         }
   105 				}
   106         break;
   106 				break;
   107       case 'js':
   107 			case 'js':
   108         if ( $dh = opendir(ENANO_ROOT . '/cache') )
   108 				if ( $dh = opendir(ENANO_ROOT . '/cache') )
   109         {
   109 				{
   110           while ( $file = @readdir($dh) )
   110 					while ( $file = @readdir($dh) )
   111           {
   111 					{
   112             $fullpath = ENANO_ROOT . "/cache/$file";
   112 						$fullpath = ENANO_ROOT . "/cache/$file";
   113             // we don't want to mess with directories
   113 						// we don't want to mess with directories
   114             if ( !is_file($fullpath) )
   114 						if ( !is_file($fullpath) )
   115               continue;
   115 							continue;
   116             
   116 						
   117             // compressed javascript
   117 						// compressed javascript
   118             if ( preg_match('/^jsres_(?:[A-z0-9_-]+)\.js\.json$/', $file) )
   118 						if ( preg_match('/^jsres_(?:[A-z0-9_-]+)\.js\.json$/', $file) )
   119               unlink($fullpath);
   119 							unlink($fullpath);
   120             // tinymce stuff
   120 						// tinymce stuff
   121             else if ( preg_match('/^tiny_mce_(?:[a-f0-9]+)\.gz$/', $file) )
   121 						else if ( preg_match('/^tiny_mce_(?:[a-f0-9]+)\.gz$/', $file) )
   122               unlink($fullpath);
   122 							unlink($fullpath);
   123           }
   123 					}
   124           $success = true;
   124 					$success = true;
   125         }
   125 				}
   126         break;
   126 				break;
   127       case 'thumbs':
   127 			case 'thumbs':
   128         if ( $dh = opendir(ENANO_ROOT . '/cache') )
   128 				if ( $dh = opendir(ENANO_ROOT . '/cache') )
   129         {
   129 				{
   130           while ( $file = @readdir($dh) )
   130 					while ( $file = @readdir($dh) )
   131           {
   131 					{
   132             $fullpath = ENANO_ROOT . "/cache/$file";
   132 						$fullpath = ENANO_ROOT . "/cache/$file";
   133             // we don't want to mess with directories
   133 						// we don't want to mess with directories
   134             if ( !is_file($fullpath) )
   134 						if ( !is_file($fullpath) )
   135               continue;
   135 							continue;
   136             
   136 						
   137             if ( preg_match('/^(?:[a-z0-9\._,-]+)-(?:[0-9]{10})-[0-9]+x[0-9]+\.([a-z0-9_-]+)$/i', $file) )
   137 						if ( preg_match('/^(?:[a-z0-9\._,-]+)-(?:[0-9]{10})-[0-9]+x[0-9]+\.([a-z0-9_-]+)$/i', $file) )
   138               unlink($fullpath);
   138 							unlink($fullpath);
   139           }
   139 					}
   140           $success = true;
   140 					$success = true;
   141         }
   141 				}
   142         break;
   142 				break;
   143       case 'wikieditnotice':
   143 			case 'wikieditnotice':
   144         $cache->purge('wiki_edit_notice');
   144 				$cache->purge('wiki_edit_notice');
   145         if ( $do_refresh )
   145 				if ( $do_refresh )
   146           $template->get_wiki_edit_notice();
   146 					$template->get_wiki_edit_notice();
   147         
   147 				
   148         $success = true;
   148 				$success = true;
   149         break;
   149 				break;
   150       case 'all':
   150 			case 'all':
   151         $success = purge_all_caches();
   151 				$success = purge_all_caches();
   152         if ( $do_refresh )
   152 				if ( $do_refresh )
   153         {
   153 				{
   154           //
   154 					//
   155           // refresh all static (non-incremental) caches
   155 					// refresh all static (non-incremental) caches
   156           //
   156 					//
   157           
   157 					
   158           // pages
   158 					// pages
   159           $success = $paths->update_metadata_cache();
   159 					$success = $paths->update_metadata_cache();
   160           if ( !$success )
   160 					if ( !$success )
   161             break;
   161 						break;
   162           
   162 					
   163           // user ranks
   163 					// user ranks
   164           $success = generate_cache_userranks();
   164 					$success = generate_cache_userranks();
   165           if ( !$success )
   165 					if ( !$success )
   166             break;
   166 						break;
   167           
   167 					
   168           // plugins
   168 					// plugins
   169           $success = $plugins->generate_plugins_cache();
   169 					$success = $plugins->generate_plugins_cache();
   170           if ( !$success )
   170 					if ( !$success )
   171             break;
   171 						break;
   172           
   172 					
   173           // wiki edit notice
   173 					// wiki edit notice
   174           $template->get_wiki_edit_notice();
   174 					$template->get_wiki_edit_notice();
   175           
   175 					
   176           // languages
   176 					// languages
   177           $q = $db->sql_query('SELECT lang_id FROM ' . table_prefix . 'language;');
   177 					$q = $db->sql_query('SELECT lang_id FROM ' . table_prefix . 'language;');
   178           if ( !$q )
   178 					if ( !$q )
   179             $db->_die();
   179 						$db->_die();
   180           while ( $row = $db->fetchrow($q) )
   180 					while ( $row = $db->fetchrow($q) )
   181           {
   181 					{
   182             $lang_local = ( $row['lang_id'] == $lang->lang_id ) ? $lang : new Language($row['lang_id']);
   182 						$lang_local = ( $row['lang_id'] == $lang->lang_id ) ? $lang : new Language($row['lang_id']);
   183             $success = $lang_local->regen_caches();
   183 						$success = $lang_local->regen_caches();
   184             if ( !$success )
   184 						if ( !$success )
   185               break 2;
   185 							break 2;
   186           }
   186 					}
   187         }
   187 				}
   188         break;
   188 				break;
   189       default:
   189 			default:
   190         $code = $plugins->setHook('acp_cache_manager_action');
   190 				$code = $plugins->setHook('acp_cache_manager_action');
   191         foreach ( $code as $cmd )
   191 				foreach ( $code as $cmd )
   192         {
   192 				{
   193           eval($cmd);
   193 					eval($cmd);
   194         }
   194 				}
   195         break;
   195 				break;
   196     }
   196 		}
   197     if ( $success )
   197 		if ( $success )
   198     {
   198 		{
   199       echo '<div class="info-box">' . $lang->get('acpcm_msg_action_success') . '</div>';
   199 			echo '<div class="info-box">' . $lang->get('acpcm_msg_action_success') . '</div>';
   200     }
   200 		}
   201     else
   201 		else
   202     {
   202 		{
   203       echo '<div class="error-box">' . $lang->get('acpcm_err_action_failed') . '</div>';
   203 			echo '<div class="error-box">' . $lang->get('acpcm_err_action_failed') . '</div>';
   204     }
   204 		}
   205   }
   205 	}
   206   else if ( isset($_POST['save']) )
   206 	else if ( isset($_POST['save']) )
   207   {
   207 	{
   208     $config_value = ( isset($_POST['cache_thumbs']) ) ? '1' : '0';
   208 		$config_value = ( isset($_POST['cache_thumbs']) ) ? '1' : '0';
   209     setConfig('cache_thumbs', $config_value);
   209 		setConfig('cache_thumbs', $config_value);
   210     echo '<div class="info-box">' . $lang->get('acpcm_msg_action_success') . '</div>';
   210 		echo '<div class="info-box">' . $lang->get('acpcm_msg_action_success') . '</div>';
   211   }
   211 	}
   212   
   212 	
   213   echo '<h3><img alt=" " src="' . scriptPath . '/images/icons/applets/cachemanager.png" />&nbsp;&nbsp;&nbsp;' . $lang->get('acpcm_heading_main') . '</h3>';
   213 	echo '<h3><img alt=" " src="' . scriptPath . '/images/icons/applets/cachemanager.png" />&nbsp;&nbsp;&nbsp;' . $lang->get('acpcm_heading_main') . '</h3>';
   214   echo '<p>' . $lang->get('acpcm_intro') . '</p>';
   214 	echo '<p>' . $lang->get('acpcm_intro') . '</p>';
   215   
   215 	
   216   echo '<div class="warning-box">' . $lang->get('acpcm_msg_refresh_warning') . '</div>';
   216 	echo '<div class="warning-box">' . $lang->get('acpcm_msg_refresh_warning') . '</div>';
   217   
   217 	
   218   acp_start_form();
   218 	acp_start_form();
   219   ?>
   219 	?>
   220   <div class="tblholder">
   220 	<div class="tblholder">
   221     <table border="0" cellspacing="1" cellpadding="4">
   221 		<table border="0" cellspacing="1" cellpadding="4">
   222       <!-- HEADER -->
   222 			<!-- HEADER -->
   223       <tr>
   223 			<tr>
   224         <th colspan="2">
   224 				<th colspan="2">
   225           <?php echo $lang->get('acpcm_table_header'); ?>
   225 					<?php echo $lang->get('acpcm_table_header'); ?>
   226         </th>
   226 				</th>
   227       </tr>
   227 			</tr>
   228       
   228 			
   229       <!-- ENABLE CACHE -->
   229 			<!-- ENABLE CACHE -->
   230       <tr>
   230 			<tr>
   231         <td class="row1" colspan="2">
   231 				<td class="row1" colspan="2">
   232           <label>
   232 					<label>
   233             <input type="checkbox" name="cache_thumbs"<?php if ( getConfig('cache_thumbs') == '1' ) echo ' checked="checked"'; ?> />
   233 						<input type="checkbox" name="cache_thumbs"<?php if ( getConfig('cache_thumbs') == '1' ) echo ' checked="checked"'; ?> />
   234             <?php echo $lang->get('acpcm_lbl_enable_cache'); ?>
   234 						<?php echo $lang->get('acpcm_lbl_enable_cache'); ?>
   235           </label>
   235 					</label>
   236           <br />
   236 					<br />
   237           <small>
   237 					<small>
   238             <?php echo $lang->get('acpcm_hint_enable_cache'); ?>
   238 						<?php echo $lang->get('acpcm_hint_enable_cache'); ?>
   239           </small>
   239 					</small>
   240         </td>
   240 				</td>
   241       </tr>
   241 			</tr>
   242       
   242 			
   243       <!-- CLEAR ALL -->
   243 			<!-- CLEAR ALL -->
   244       <tr>
   244 			<tr>
   245       <td class="row2" style="width: 120px; text-align: center;">
   245 			<td class="row2" style="width: 120px; text-align: center;">
   246           <button name="clear" value="all"><?php echo $lang->get('acpcm_btn_clear_all'); ?></button>
   246 					<button name="clear" value="all"><?php echo $lang->get('acpcm_btn_clear_all'); ?></button>
   247         </td>
   247 				</td>
   248         <td class="row2">
   248 				<td class="row2">
   249           <?php echo $lang->get('acpcm_hint_clear_all'); ?>
   249 					<?php echo $lang->get('acpcm_hint_clear_all'); ?>
   250         </td>
   250 				</td>
   251       </tr>
   251 			</tr>
   252       
   252 			
   253       <?php
   253 			<?php
   254       // if caching is disabled, might as well break off here
   254 			// if caching is disabled, might as well break off here
   255       if ( getConfig('cache_thumbs') == '1' ):
   255 			if ( getConfig('cache_thumbs') == '1' ):
   256       ?>
   256 			?>
   257       
   257 			
   258       <!-- REFRESH ALL -->
   258 			<!-- REFRESH ALL -->
   259       <tr>
   259 			<tr>
   260         <td class="row1" style="text-align: center;">
   260 				<td class="row1" style="text-align: center;">
   261           <button name="refresh" value="all"><?php echo $lang->get('acpcm_btn_refresh_all'); ?></button>
   261 					<button name="refresh" value="all"><?php echo $lang->get('acpcm_btn_refresh_all'); ?></button>
   262         </td>
   262 				</td>
   263         <td class="row1">
   263 				<td class="row1">
   264           <?php echo $lang->get('acpcm_hint_refresh_all'); ?>
   264 					<?php echo $lang->get('acpcm_hint_refresh_all'); ?>
   265         </td>
   265 				</td>
   266       </tr>
   266 			</tr>
   267       
   267 			
   268       <!-- INDIVIDUAL CACHES -->
   268 			<!-- INDIVIDUAL CACHES -->
   269       <tr>
   269 			<tr>
   270         <th class="subhead" colspan="2">
   270 				<th class="subhead" colspan="2">
   271           <?php echo $lang->get('acpcm_th_individual_caches'); ?>
   271 					<?php echo $lang->get('acpcm_th_individual_caches'); ?>
   272         </th>
   272 				</th>
   273       </tr>
   273 			</tr>
   274       
   274 			
   275       <?php
   275 			<?php
   276       $class = 'row2';
   276 			$class = 'row2';
   277       $cache_list = array('page', 'ranks', 'sidebar', 'plugins', 'template', 'aes', 'lang', 'js', 'thumbs', 'wikieditnotice');
   277 			$cache_list = array('page', 'ranks', 'sidebar', 'plugins', 'template', 'aes', 'lang', 'js', 'thumbs', 'wikieditnotice');
   278       $code = $plugins->setHook('acp_cache_manager_list_caches');
   278 			$code = $plugins->setHook('acp_cache_manager_list_caches');
   279       foreach ( $code as $cmd )
   279 			foreach ( $code as $cmd )
   280       {
   280 			{
   281         eval($cmd);
   281 				eval($cmd);
   282       }
   282 			}
   283       foreach ( $cache_list as $target )
   283 			foreach ( $cache_list as $target )
   284       {
   284 			{
   285         $class = ( $class == 'row1' ) ? 'row2' : 'row1';
   285 				$class = ( $class == 'row1' ) ? 'row2' : 'row1';
   286         ?><tr>
   286 				?><tr>
   287         <td class="<?php echo $class; ?>" style="text-align: center;">
   287 				<td class="<?php echo $class; ?>" style="text-align: center;">
   288           <button name="refresh" value="<?php echo $target; ?>"<?php if ( in_array($target, array('template', 'sidebar', 'aes', 'js', 'thumbs')) ) echo ' disabled="disabled"'; ?>>
   288 					<button name="refresh" value="<?php echo $target; ?>"<?php if ( in_array($target, array('template', 'sidebar', 'aes', 'js', 'thumbs')) ) echo ' disabled="disabled"'; ?>>
   289             <?php echo $lang->get('acpcm_btn_refresh'); ?>
   289 						<?php echo $lang->get('acpcm_btn_refresh'); ?>
   290           </button>
   290 					</button>
   291           <button name="clear" value="<?php echo $target; ?>">
   291 					<button name="clear" value="<?php echo $target; ?>">
   292             <?php echo $lang->get('acpcm_btn_clear'); ?>
   292 						<?php echo $lang->get('acpcm_btn_clear'); ?>
   293           </button>
   293 					</button>
   294         </td>
   294 				</td>
   295         <td class="<?php echo $class; ?>">
   295 				<td class="<?php echo $class; ?>">
   296           <b><?php echo $lang->get("acpcm_cache_{$target}_desc_title"); ?></b> &ndash;
   296 					<b><?php echo $lang->get("acpcm_cache_{$target}_desc_title"); ?></b> &ndash;
   297           <?php echo $lang->get("acpcm_cache_{$target}_desc_body"); ?>
   297 					<?php echo $lang->get("acpcm_cache_{$target}_desc_body"); ?>
   298         </td>
   298 				</td>
   299         </tr>
   299 				</tr>
   300       <?php
   300 			<?php
   301       }
   301 			}
   302       
   302 			
   303       // getConfig('cache_thumbs') == '1'
   303 			// getConfig('cache_thumbs') == '1'
   304       endif;
   304 			endif;
   305       ?>
   305 			?>
   306       
   306 			
   307       <!-- SAVE CHANGES -->
   307 			<!-- SAVE CHANGES -->
   308       <tr>
   308 			<tr>
   309         <th colspan="2" class="subhead">
   309 				<th colspan="2" class="subhead">
   310           <input type="submit" name="save" value="<?php echo $lang->get('etc_save_changes'); ?>" style="font-weight: bold;" />
   310 					<input type="submit" name="save" value="<?php echo $lang->get('etc_save_changes'); ?>" style="font-weight: bold;" />
   311           <input type="submit" name="cancel" value="<?php echo $lang->get('etc_cancel'); ?>" />
   311 					<input type="submit" name="cancel" value="<?php echo $lang->get('etc_cancel'); ?>" />
   312         </th>
   312 				</th>
   313       </tr>
   313 			</tr>
   314     </table>
   314 		</table>
   315   </div>
   315 	</div>
   316   <?php
   316 	<?php
   317   echo '</form>';
   317 	echo '</form>';
   318 }
   318 }
   319 
   319 
   320 ?>
   320 ?>