<?php

if(!strlen($pathtoroot))
{
  $path=dirname(realpath($_SERVER['SCRIPT_FILENAME']));
  if(strlen($path))
  {
    /* determine the OS type for path parsing */
    $pos = strpos($path,":");
    if ($pos === false)
    {
      $gOS_TYPE="unix";
      $pathchar = "/";
    }
    else
    {
      $gOS_TYPE="win";
      $pathchar="\\";
    }
    $p = $path.$pathchar;
    /*Start looking for the root flag file */
    while(!strlen($pathtoroot) && strlen($p))
    {
      $sub = substr($p,strlen($pathchar)*-1);
      if($sub==$pathchar)
      {
        $filename = $p."root.flg";
      }
      else
        $filename = $p.$pathchar."root.flg";
      if(file_exists($filename))
      {
        $pathtoroot = $p;
      }
      else
      {
        $parent = realpath($p.$pathchar."..".$pathchar);
	if($parent!=$p)
	{
	  $p = $parent;
	}
	else
	  $p = "";
      }
    }
    if(!strlen($pathtoroot))
      $pathtoroot = ".".$pathchar;
  }
  else
  {
    $pathtoroot = ".".$pathchar;
  }
}

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

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

$rootURL="http://".ThisDomain().$objConfig->Get("Site_Path");

$baseURL = $_SERVER['DOCUMENT_ROOT'].$path_char.$objConfig->Get("Site_Path");

$admin = $objConfig->Get("AdminDirectory");
if(!strlen($admin))
    $admin = "admin";
$localURL=$rootURL."kernel/";
$adminURL = $rootURL.$admin;
$imagesURL = $adminURL."/images";

//admin only util

$pathtolocal = $pathtoroot."kernel/";
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");

$section = $_REQUEST["section"];

$envar = BuildEnv();
int_help_header();

$section = explode(':', $section);
if($section[0] == 'in-portal') $section[0] = 'kernel';

$topic_path = $baseURL.$section[0].'/'.$admin.'/include/help/'.$section[1].'.txt';

// for debugging: save new help content
if( GetVar('action') == 'save_help' )
{
	error_reporting(E_ALL);
	$fp = fopen($topic_path, 'w');	
	fwrite($fp, stripslashes(GetVar('help_content')) );
	fclose($fp);
}

$help_data = file_exists($topic_path) ? file_get_contents($topic_path) : GetVar('help_content');
if($section[0] == 'kernel') $section[0] = 'in-portal';

?>
<table width="100%" border="0" cellspacing="0" cellpadding="4" class="tableborder">
	<tr>
		<td bgcolor="F6F6F6">
		<div class="help_box">
		<?php
			if( file_exists($topic_path) )
				echo $help_data;
			else
				echo defined('DEBUG_HELP') ? 'missing section help file <b>'.$section[1].'.txt</b><br>' : admin_language('la_help_in_progress');
				
			?>
		</div>
		<?php
			if( defined('DEBUG_HELP') )
			{
		?>
				<br><input type="button" onclick="document.save_help.submit();" value="Save Changes" style="font-weight: bolder; background-color: #F6F6F6;"><br>
				<form name="save_help" id="save_help" method="post" action="<?php echo $_SERVER['PHP_SELF'].'?env='.$envar; ?>">
					<textarea id="help_content" name="help_content" rows="15" cols="85"><?php echo $help_data; ?></textarea>
					<input type="hidden" name="section" value="<?php echo implode(':',$section); ?>">
					<input type="hidden" name="action" value="save_help">
					<?php if( GetVar('help_usage') ) { ?>
					<input type="hidden" name="help_usage" value="<?php echo GetVar('help_usage'); ?>">
					<?php } ?>
				</form>	
		<?php 
			}
		?>
		</td>
	</tr>
</table>
<? int_footer(); ?>