1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
<?php
/* OpenDb - Open Media Lending Database
Copyright (C) 2001,2002 by Jason Pell
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
include_once("./functions/database.php");
include_once("./functions/logging.php");
include_once("./functions/utils.php");
function file_cache_check_is_installed()
{
// Only get one row, to save processing them.
$query = "SELECT 'x' FROM file_cache LIMIT 0,1";
// In this case the run_opendb_query() would return FALSE, if
// table does not exist.
$result = run_opendb_query($query);
if($result)
{
mysql_free_result($result);
// The table exists (Does not have to have any records!)
return TRUE;
}
//else
return FALSE;
}
/*
*/
function file_cache_is_exists_url($url, $cache_type='HTTP')
{
$query = "SELECT sequence_number FROM file_cache WHERE cache_type = '$cache_type' AND url = '".trim($url)."' AND expire_date > NOW()";
$result = run_opendb_query($query);
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
if ($found!== FALSE)
return $found['sequence_number'];
}
//else
return FALSE;
}
/**
* Will include expired files.
*/
function fetch_file_cache_rs($cache_type='HTTP')
{
$query = "SELECT sequence_number, url, location, content_type, content_length, UNIX_TIMESTAMP(cache_date) as cache_date, UNIX_TIMESTAMP(expire_date) as expire_date, IF(expire_date<=NOW(),'Y','N') as expired_ind ".
"FROM file_cache ".
"WHERE cache_type = '$cache_type' ".
"ORDER BY expire_date ASC ";
$result = run_opendb_query($query);
if($result && mysql_num_rows($result)>0)
return $result;
else
return FALSE;
}
/**
* Assumed used file_cache_is_exists_url(...) or checked expired_ind if
* using fetch_file_cache_rs so as not to return expired file.
*
* @$include_content if TRUE, will request content as well.
*/
function fetch_file_cache_r($sequence_number)
{
$query = "SELECT sequence_number, url, location, content_type, content_length, UNIX_TIMESTAMP(cache_date) as cache_date, UNIX_TIMESTAMP(expire_date) as expire_date ".
"FROM file_cache ".
"WHERE sequence_number = '$sequence_number'";
$result = run_opendb_query($query);
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
return $found;
}
//else
return FALSE;
}
/**
* Assumed used file_cache_is_exists_url(...) or checked expired_ind if
* using fetch_file_cache_rs so as not to return expired file.
*/
function file_cache_fetch_content($sequence_number)
{
$query = "SELECT content, gzcompress_level FROM file_cache WHERE sequence_number = '$sequence_number'";
$result = run_opendb_query($query);
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
if ($found!== FALSE)
{
if(is_numeric($found['gzcompress_level']) && $found['gzcompress_level']>0)
return gzuncompress($found['content']);
else
return $found['content'];
}
}
//else
return FALSE;
}
/*
* File Cache - create cache file
*
* This function will automatically insert the create_date and expiry_date fields
* based on the include/config.php variable $CONFIG_VARS['http_cache.lifetime']
*
* This function will also use the include/config.php variable $CONFIG_VARS['http_cache.max_cache_size']
* to determine whether existing records need to be purged to make room for this one.
*
* It will lock the table at the beginning of the operation to ensure everything
* is done as a single atomic operation.
*/
function file_cache_insert_file($url, $location, $content_type, $content, $cache_type='HTTP')
{
global $CONFIG_VARS;
global $HTTP_SESSION_VARS;
$url = trim($url);
$location = trim($location);
$gzcompress_level = 0;
if(is_numeric($CONFIG_VARS['http_cache.gzcompress.level']) && $CONFIG_VARS['http_cache.gzcompress.level']>0)
{
$gzcompress_level = $CONFIG_VARS['http_cache.gzcompress.level'];
$content = gzcompress($content, $gzcompress_level);
}
$content_length = strlen($content);
if(run_opendb_query("LOCK TABLES file_cache WRITE"))
{
if(file_cache_is_exists_url($url, $cache_type)===FALSE)
{
if(is_numeric($CONFIG_VARS['http_cache.max_cache_size']))
{
// if current size of cache is too big, we need to reduce it.
$total_cache_size = file_cache_fetch_cache_size($cache_type);
// if total cache size + the content_length do not exceed max
// cache size, we can just go ahead and insert the file.
if(($total_cache_size + $content_length) >= $CONFIG_VARS['http_cache.max_cache_size'])
{
// if we did delete some files, we want to check if we have room or
// not, before we go trying to make room.
$delete_expired_files_count = file_cache_delete_expired_files($cache_type);
// reset the total cache size.
if($delete_expired_files_count>0)
{
$total_cache_size = file_cache_fetch_cache_size($cache_type);
}
if(($total_cache_size + $content_length) >= $CONFIG_VARS['http_cache.max_cache_size'])
{
// if failure to make room, we have to abort.
if(file_cache_delete_files_to_fit($content_length, $cache_type)===FALSE)
{
run_opendb_query("UNLOCK TABLES");
return FALSE;
}
}
}//else we have enough room.
}
$content = addslashes($content);
//else insert new cache record here.
$query = "INSERT INTO file_cache (cache_type, url, location, content_type, content_length, cache_date, expire_date, gzcompress_level, content)".
" VALUES ('$cache_type','$url','$location','$content_type','$content_length',NOW(),".(is_numeric($CONFIG_VARS['http_cache.lifetime'])?"NOW()+ INTERVAL ".$CONFIG_VARS['http_cache.lifetime']." SECOND":"NULL").", $gzcompress_level, '$content')";
$insert = run_opendb_query($query);
if ($insert && mysql_affected_rows() > 0)
{
$new_sequence_number = mysql_insert_id();
run_opendb_query("UNLOCK TABLES");
opendb_log("Inserted file_cache record (sequence_number=$new_sequence_number, cache_type=$cache_type, url=$url, content_type=$content_type, content_length=$content_length, update_who=".$HTTP_SESSION_VARS['user_id'].")");
return $new_sequence_number;
}
else
{
run_opendb_query("UNLOCK TABLES");
opendb_log("Failed to insert file_cache record (cache_type=$cache_type, url=$url, content_type=$content_type, content_length=$content_length, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
return FALSE;
}
}//if(file_cache_is_exists_url($url, $cache_type)!==FALSE)
else
{
run_opendb_query("UNLOCK TABLES");
opendb_log("Failed to insert file_cache record (cache_type=$cache_type, url=$url, content_type=$content_type, content_length=$content_length, update_who=".$HTTP_SESSION_VARS['user_id'].") [URL already exists]");
// file already exists, and has not expired.
return FALSE;
}
}
else
{
opendb_log("Could not lock file_cache for write (update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
// could not lock table.
return FALSE;
}
}
/*
* NOTE: Assumes file_cache lock
*/
function file_cache_fetch_cache_size($cache_type='HTTP')
{
$query = "SELECT SUM(content_length) AS total_size FROM file_cache WHERE cache_type = '$cache_type'";
$result = run_opendb_query($query);
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
if ($found!== FALSE)
{
if(is_numeric($found['total_size']))
return $found['total_size'];
else
return 0;
}
}
//else
return FALSE;
}
/**
* NOTE: Assumes file_cache lock
*
* Return number of files deleted or FALSE. Be sure to use ===FALSE, so you do not get
* confused with successful completion, but no files deleted.
*/
function file_cache_delete_expired_files($cache_type='HTTP')
{
global $HTTP_SESSION_VARS;
$query ="DELETE FROM file_cache WHERE cache_type = '$cache_type' AND expire_date <= NOW()";
$delete = run_opendb_query($query);
// still successful even if no expired records to delete.
if( $delete )
{
opendb_log("Deleted expired file_cache records (cache_type=$cache_type, rows_deleted=".mysql_affected_rows().", update_who=".$HTTP_SESSION_VARS['user_id'].")");
return mysql_affected_rows();
}
else
{
opendb_log("Failed to delete expired file_cache records (cache_type=$cache_type, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
return FALSE;
}
}
function file_cache_delete_files($cache_type='HTTP')
{
global $HTTP_SESSION_VARS;
$query ="DELETE FROM file_cache WHERE cache_type = '$cache_type'";
$delete = run_opendb_query($query);
// still successful even if no expired records to delete.
if( $delete )
{
opendb_log("Deleted all file_cache records (cache_type=$cache_type, rows_deleted=".mysql_affected_rows().", update_who=".$HTTP_SESSION_VARS['user_id'].")");
return mysql_affected_rows();
}
else
{
opendb_log("Failed to delete all file_cache records (cache_type=$cache_type, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
return FALSE;
}
}
/*
* This function assumes you have checked the file cache total size, and need
* to delete a number of files to make room. It will start with the oldest
* files (create_date desc) and delete files with the sume of content_lengths
* equal to the present file content_length.
*
* Note: Assumes the cache is too big to fit current file, so we have to
* reduce it by the number of files.
*
* NOTE: Assumes file_cache lock
*/
function file_cache_delete_files_to_fit($content_length, $cache_type='HTTP')
{
global $HTTP_SESSION_VARS;
// this logic is not very efficient, because we are selecting the complete
// table here, need to think of more efficient method of working out how
// to clean up the table.
// One method would be to select a row at a time, and delete that row until
// the sum of the content lengths of the deleted rows equals the content
// length we need for the new record, but for now will leave as is.
$query = "SELECT sequence_number, content_length FROM file_cache ORDER BY expire_date ASC";
$results = run_opendb_query($query);
if($results)
{
$sequence_numbers = NULL;
$cache_content_length = 0;
while($file_cache_r = mysql_fetch_array($results, MYSQL_ASSOC))
{
if(is_numeric($file_cache_r['content_length']))
{
// save sequence number, cos we are going to delete it soon.
$sequence_numbers[] = $file_cache_r['sequence_number'];
$cache_content_length += $file_cache_r['content_length'];
// do we have enough space yet?
if($cache_content_length >= $content_length)
{
break;
}
}
}
mysql_free_result($results);
// could not delete enough cache files to make room.
if($cache_content_length >= $content_length)
{
if(is_not_empty_array($sequence_numbers))
{
// now lets delete those sequence numbers.
$query ="DELETE FROM file_cache WHERE cache_type = '$cache_type' AND sequence_number IN(".format_sql_in_clause($sequence_numbers).")";
$delete = run_opendb_query($query);
// still successful even if no expired records to delete.
if( $delete )
{
opendb_log("Deleted old file_cache records (cache_type=$cache_type, new_content_length=$content_length, rows_deleted=".mysql_affected_rows().", update_who=".$HTTP_SESSION_VARS['user_id'].")");
return mysql_affected_rows();
}
else
{
opendb_log("Failed to old file_cache records (cache_type=$cache_type, new_content_length=$content_length, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
return FALSE;
}
}
}
else
{
// file is too big for cache.
return FALSE;
}
}//if($results)
//else - no records to delete
return 0;
}
?>
|