<?php
require_once('scripts/sb_functions.php');

if (!signed_in()){
  global $logged_in;
  $logged_in = logged_in( true, true );
}
else{
    global $blog_permissions;
    if (! $blog_permissions['setup']==1 ){ 
	redirect_to_url('index.php');
    }
}

//global $logged_in;
//$logged_in = logged_in( true, true );

read_config();
//sb_language( 'index' );
sb_language( 'cache' );

html_header('',false);


/////////////////////////////////////////
//these 4 functions where taken from the commnets at http://php.net/glob

	function globr($sDir, $sPattern, $nFlags = NULL) {
		$aFiles = glob("$sDir/$sPattern", $nFlags);
		$files = getDir($sDir);
		if (is_array($files)) {
			foreach( $files as $file ) {
				$aSubFiles = globr($file, $sPattern, $nFlags);
				$aFiles = array_merge($aFiles,$aSubFiles);
			}
		}
		return $aFiles;
	}

	function getDir($sDir) {
		$i=0;
		if(is_dir($sDir)) {
			if($rContents = opendir($sDir)) {
				while($sNode = readdir($rContents)) {
					if(is_dir($sDir.'/'.$sNode )) {
						if($sNode !="." && $sNode !="..") {
							$aDirs[$i] = $sDir.'/'.$sNode ;
							$i++;
						}
					}
				}
			}
		}
		return $aDirs;
	}

	if(!(function_exists('glob')))
	{
		function glob($pattern)
		{
			#get pathname (everything up until the last / or \)
			$path=$output=null;
			if(PHP_OS=='WIN32')
			{
				$slash='\\';
			}
			else
			{
				$slash='/';
			}
			$lastpos=strrpos($pattern,$slash);
			if(!($lastpos===false))
			{
				$path=substr($pattern,0,-$lastpos-1); #negative length means take from the right
				$pattern=substr($pattern,$lastpos);
			}
			else
			{
				#no dir info, use current dir
				$path=getcwd();
			}
			$handle=@ opendir($path);
			if($handle===false)
			{
				return false;
			}
			while($dir=readdir($handle))
			{
				if(pattern_match($pattern,$dir))
				{
					$output[]=$dir;
				}
			}
			closedir($handle);
			if(is_array($output))
			{
				return $output;
			}
			return false;
		}

		function pattern_match($pattern,$string)
		{
			#basically prepare a regular expression
			$out=null;
			$chunks=explode(';',$pattern);
			foreach($chunks as $pattern)
			{
				$escape=array('$','^','.','{','}',
				'(',')','[',']','|');
				while(strpos($pattern,'**')!==false)
				{
					$pattern=str_replace('**','*',$pattern);
				}
				foreach($escape as $probe)
				{
					$pattern=str_replace($probe,"\\$probe",$pattern);
				}
				$pattern=str_replace('?*','*',str_replace('*?','*',str_replace('*',".*",str_replace('?','.{1,1}',$pattern))));
				$out[]=$pattern;
			}
			if(count($out)==1)
			{
				return(eregi("^$out[0]$",$string));
			}
			else
			{
				foreach($out as $tester)
				{
					if(eregi("^$tester$",$string))
					{
						return true;
					}
					return false;
				}
			}
		}
	}
	///////////////////////////////////////////////////////


	function cached_files()
	{
		clearstatcache();
		$files = globr(".","*.cache");
		return $files;
	}



	function page_content() {
	    global $lang_string;
		$entry_array = array();
		$entry_array[ 'subject' ] = $lang_string['title'];
		$entry_array[ 'entry' ] = $lang_string['instructions'];

		echo( theme_staticentry( $entry_array ) );

		$entry_array = array();
		$entry_array[ 'subject' ] = $lang_string['options'];
		$entry_array[ 'entry' ] = '<form method="post"><input type="submit" name="deleteCache" value="'.$lang_string['delete_cache_btn'].'"></form>';

		echo( theme_staticentry( $entry_array ) );



		$entry_array = array();
		$entry_array[ 'subject' ] = $lang_string['list_of_files'];
		$files = cached_files();
		if(count($files)>0)
		{
			$entry_array[ 'entry' ] = '<table border="0" style="width:100%">';
			$entry_array[ 'entry' ] .= '<tr><td>'.$lang_string['file_path'].'</td><td>'.$lang_string['modification_time'].'</td></tr>';

			foreach ($files AS $file)
			{
				$entry_array[ 'entry' ] .= "<tr>";
				$entry_array[ 'entry' ] .= "<td style=\"border-bottom:1px solid gray;\"> $file</td><td style=\"border-bottom:1px solid gray;\"> ".format_date(filemtime($file))."</td>";
				$entry_array[ 'entry' ] .= "</tr>";
			}
			$entry_array[ 'entry' ] .= '</table>';
		}
		else
		{
			$entry_array[ 'entry' ] = $lang_string['no_cahced_files'];
		}
		echo( theme_staticentry( $entry_array ) );

	}


	if(isset($_POST['deleteCache']))
	{
		foreach (cached_files() AS $file)
		{
			unlink($file);
		}

	}


	theme_pagelayout();

	html_footer();

	  ?>
