<?php

/**
 *
 * INDEXU
 * Copyright(C), Nicecoder, 2000-2006, All Rights Reserved.
 *
 * INDEXU(tm) is protected by Indonesia and International copyright laws.
 * Unauthorized use or distribution of INDEXU(tm) is strictly prohibited,
 * violators will be prosecuted. To obtain a license for using INDEXU(tm),
 * please register at Nicecoder home page at http://www.nicecoder.com
 *
 * Author:
 *    Dody Rachmat Wicaksono (dody@nicecoder.com)
 *    M. Zuber (zubby@nicecoder.com)
 *
 */

function filter_page_cache($type = "pre", $file = "") {
  global $base_path;
  
  $cache_dir = 'cache/';
  $cache_param = $_SERVER["REQUEST_URI"];
  $cache_time_arr = explode(';', implode(file($base_path . 'page_caching.txt'), ""));
  $cache_use_subdir = 0;

  if (strpos($file, 'index.php') !== FALSE) {
    $cache_file = 'index_';
    $cache_time = $cache_time_arr[0];
  }
  elseif (strpos($file, 'browse.php') !== FALSE) {
    $cache_param .= $_COOKIE['COOKIE_SORT_BY'] . $_COOKIE['COOKIE_SORT_BY'];
    $cache_file = 'browse_';
    $cache_time = $cache_time_arr[1];
    $cache_use_subdir = 1;
  }
  elseif (strpos($file, 'browsepr.php') !== FALSE) {
    $cache_param .= $_COOKIE['COOKIE_SORT_BY'] . $_COOKIE['COOKIE_SORT_BY'];
    $cache_file = 'browsepr_';
    $cache_time = $cache_time_arr[1];
    $cache_use_subdir = 1;
  }
  elseif (strpos($file, 'detail.php') !== FALSE) {
    $cache_file = 'index_';
    $cache_time = $cache_time_arr[2];
    $cache_use_subdir = 1;
  }
  elseif (strpos($file, 'new.php') !== FALSE) {
    $cache_file = 'new_';
    $cache_time = $cache_time_arr[3];
  }
  elseif (strpos($file, 'hot.php') !== FALSE) {
    $cache_file = 'hot_';
    $cache_time = $cache_time_arr[4];
  }
  elseif (strpos($file, 'top_rated.php') !== FALSE) {
    $cache_file = 'top_rated_';
    $cache_time = $cache_time_arr[5];
  }
  elseif (strpos($file, 'pick.php') !== FALSE) {
    $cache_file = 'pick_';
    $cache_time = $cache_time_arr[5];
  }


  if ($cache_use_subdir) {
    if (ini_get('safe_mode')) {
      $cache_subdir = '';
    }
    else {
      $cache_subdir = md5($cache_file . $cache_param . ".html");
      $cache_subdir = "$cache_subdir[0]/$cache_subdir[1]/";
    }
    $cache_file = $cache_dir . $cache_subdir . $cache_file . md5($cache_param) . ".html";
  }
  else  {
    $cache_file = $cache_dir . $cache_file . md5($cache_param) . ".html";
  }
  
  $cache_time = $cache_time * 60;
  if (file_exists($cache_file) && time() - $cache_time < filemtime($cache_file)) {
    define('CACHED_PAGE', 1);
  }


  if ($type == 'pre') {
    if (defined('CACHED_PAGE')) {
      include $cache_file;
      echo "\n<!-- cached page -->";
    }
  }

  if ($type == 'post') {
    if (!defined('CACHED_PAGE')) {
      if ($cache_time) {
        if ($cache_use_subdir) {
          CreateDir($cache_dir . $cache_subdir, 0777);
        }
        $fp = fopen($cache_file, 'w+');
        fwrite($fp, ob_get_contents());
        fclose($fp);
      }
    }
  }
}

?>
