<?php
/**
 * Class to handle the caching functionality
 *
 * This is having class AgCache to handle the caching functionality
 * Most recent version available from http://acme-web-design.info/php-cache-kit.html
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		AgCache
 * @author		selvaraj_47ag04
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id : $
 * @since 		2008-11-05
 */
class AgCache
	{

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
	 	public function fetch($name, $refreshSeconds = 0)
			{
	  			global $CFG;
				$cacheContent = '';
	  			if(!$CFG['feature']['data_cache']['active_status'])
					return false;

				if(!$refreshSeconds)
					$refreshSeconds = 60;

				$cacheFile = AgCache::cachePath($name);
				if(file_exists($cacheFile) and ((time()-filemtime($cacheFile))< $refreshSeconds))
					{
	   					$cacheContent = file_get_contents($cacheFile);
	   				}
	  			return $cacheContent;
	 		}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public  function save($name, $cacheContent)
			{
	  			global $CFG;
				if(!$CFG['feature']['data_cache']['active_status'])
					return;

				$cacheFile = AgCache::cachePath($name);
				AgCache::savetofile($cacheFile, $cacheContent);
			}

		/**
		 * for internal use
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function cachePath($name)
			{
			  global $CFG;
			  $cacheFolder = $CFG['feature']['data_cache']['folder'];
			  $cacheFolder = (isMember())?$cacheFolder:$cacheFolder; //removed ../
			  if(!$cacheFolder) $cacheFolder = trim($_SERVER['DOCUMENT_ROOT'],'/').'/cache/';
			  return $cacheFolder . md5(strtolower(trim($name))) . '.cache';
		 	}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function savetofile($filename, $data)
			{
				$dir = trim(dirname($filename),'/').'/';
				AgCache::forceDirectory($dir);
				$file = fopen($filename, 'w');
				fwrite($file, $data);
				fclose($file);
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function forceDirectory($dir)
			{
				// force directory structure
	 			return is_dir($dir) or (AgCache::forceDirectory(dirname($dir)) and @mkdir($dir, 0777));
			}
	}
?>