<?php
##############################################################
##In-portal													##
##############################################################
##					      In-portal							##
##					Intechnic Corporation					##
##			   All Rights Reserved, 1998-2002				##
##															##
##	No portion of this code may be copied, reproduced or	##
##	   otherwise redistributed without proper written		##
##	  consent of Intechnic Corporation.  Violation will		##
##	   result in revocation of the license and support		##
##	 privileges along maximum prosecution allowed by law.	##
##############################################################
if(!defined('CACHE_PERM_CHUNK_SIZE'))define('CACHE_PERM_CHUNK_SIZE',30);

if(!strlen($pathtoroot))
{
  	$path = dirname(realpath(__FILE__));
  	if( strlen($path) )
  	{
    	// determine the OS type for path parsing
    	$pos = strpos($path, ':');
    	$gOS_TYPE = ($pos === false) ? 'unix' : 'win';
    	$pathchar = ($gOS_TYPE == 'unix') ? '/' : "\\";
    	$p = $path.$pathchar;

    	// Start looking for the root flag file
    	while( !strlen($pathtoroot) && strlen($p) )
    	{
      		$sub = substr($p, strlen($pathchar) * -1);
      		$filename = $p.( ($sub == $pathchar) ? '' : $pathchar).'root.flg';
      		if( !file_exists($filename) )
      		{
        		$parent = realpath($p.$pathchar."..".$pathchar);
				$p = ($parent != $p) ? $parent : '';
      		}
      		else
      			$pathtoroot = $p;
    	}
    	if( !strlen($pathtoroot) ) $pathtoroot = '.'.$pathchar;
  	}
  	else
    	$pathtoroot = '.'.$pathchar;
}

$sub = substr($pathtoroot,strlen($pathchar)*-1);
if( $sub != $pathchar) $pathtoroot = $pathtoroot.$pathchar;

//echo $pathtoroot;
//$FrontEnd=2;

define('REDIRECT_REQUIRED',1); // this script can issue redirect header

require_once($pathtoroot."kernel/startup.php");

//admin only util
$rootURL="http://".ThisDomain().$objConfig->Get("Site_Path");
$admin = $objConfig->Get("AdminDirectory");
if(!strlen($admin))
    $admin = "admin";
$localURL=$rootURL."kernel/";
$adminURL = $rootURL.$admin;
$imagesURL = $adminURL."/images";
//$pathtolocal = $pathtoroot."in-news/";
require_once ($pathtoroot.$admin."/include/elements.php");
require_once ($pathtoroot."kernel/admin/include/navmenu.php");
//require_once ($pathtolocal."admin/include/navmenu.php");
require_once($pathtoroot.$admin."/toolbar.php");
require_once($pathtoroot.$admin."/listview/listview.php");
$section = "in-portal:category_maint";


require_once($pathtoroot.$admin."/category/permcacheupdate.php");
if(!$objSession->GetVariable('PermCache_UpdateRequired'))
{
	die(header('Location: '.$adminURL.'/'.$objSession->GetVariable('ReturnScript').'?env='.BuildEnv()));
}
if(isset($_GET['continue']))
{
	$updater =& new clsCacheUpdater(1);
	if(!intval($_GET['continue']))
	{
		$updater->clearData();
		die(header('Location: '.$adminURL.'/'.$objSession->GetVariable('ReturnScript').'?env='.BuildEnv()));
	}
}
else
{
	$updater =& new clsCacheUpdater();
	$no_url = $_SERVER['PHP_SELF'].'?env='.BuildEnv().'&continue=0';
	$yes_url = $_SERVER['PHP_SELF'].'?env='.BuildEnv().'&continue=1';

	
	if(!isset($_GET['force']))
	if($updater->totalCats > CACHE_PERM_CHUNK_SIZE)
	{
		$updater->setData();
		$title = prompt_language("la_prompt_updating")." ".prompt_language("la_Text_Categories");
		int_header(NULL,NULL,$title);
		flush();
?>
<script language="javascript">
	function goto_url(url)
	{
		document.location = url;
	}
</script>
<table cellspacing="0" cellpadding="2" width="100%" border="0" class="tableborder">
    	<tr>
		<td align="center" colspan="3" bgcolor="#FFFFFF"><?php echo prompt_language('la_confirm_maintenance'); ?></td>
	</tr>
    	<tr>
		<td align="center" colspan="3" bgcolor="#FFFFFF"><?php echo prompt_language('la_prompt_perform_now'); ?></td>
	</tr>
	<tr>
    		<td align="right" width="50%">
    			<input type="button" name="yes_btn" value="<?php echo admin_language("lu_yes"); ?>" onclick="javascript:goto_url('<?php echo $yes_url; ?>');" class="button">
	    	</td>
		<td><img src="<?php echo $imagesURL; ?>/spacer.gif" width="10"></td>
	    	<td align="left" width="50%">
    			<input type="button" name="yes_btn" value="<?php echo admin_language("lu_no"); ?>" onclick="javascript:goto_url('<?php echo $no_url; ?>');" class="button">
	    	</td>
	</tr>
</table>
<?php
		int_footer();
		die();
	}
}
$title = prompt_language("la_prompt_updating")." ".prompt_language("la_Text_Categories");
int_header(NULL,NULL,$title);
flush();
$percent=$updater->getDonePercent();
echo '<TABLE cellspacing="0" cellpadding="2" width="100%" border="0" class="tableborder">';
if ($percent == 0)
	echo '<TR><TD BGCOLOR="#FFFFFF" width="100%" >'.$percent.'%</td></TR>';
else if ($percent < 60)
	echo '<TR><TD BGCOLOR="#4682B2" width="'.$percent.'%"></td><TD BGCOLOR="#FFFFFF" width="'.(100-$percent).'%">'.$percent.'%</td></TR>';
else if ($percent == 100)
	echo '<TR><TD BGCOLOR="#4682B2" align="right" width="100%"><FONT COLOR="#FFFFFF">'.$percent.'%</FONT></td>';
else
	echo '<TR><TD BGCOLOR="#4682B2" align="right" width="'.$percent.'%"><FONT COLOR="#FFFFFF">'.$percent.'%</FONT></td><TD BGCOLOR="#FFFFFF" width="'.(100-$percent).'%"></td></TR>';
echo '</TABLE>';
flush();

$needs_more = TRUE;
while ($needs_more && $updater->iteration < CACHE_PERM_CHUNK_SIZE) {
	$needs_more = $updater->DoTheJob();
}

if ($needs_more)
{
	$updater->setData();
	$url=$adminURL.'/category/category_maint.php?env='.BuildEnv().'&continue=1';
}
else
{
	$updater->clearData();
	$url = $adminURL.'/'.$objSession->GetVariable('ReturnScript').'?env='.BuildEnv();
	$objSession->SetVariable('PermCache_UpdateRequired', 0);
}
print "<script language=\"javascript\">" ;
print "setTimeout(\"document.location='$url';\",400);";
print "</script>";
int_footer();
exit;
?>