<?php
/**
* @file $id: index.php (17-12-2009)
* @pack Integral-Education
* @copyright Copyright (C) 2009 Integral-Group. All rights reserved.
* This package can be modified by the licencee, but the license is non-transferrable.
*/
error_reporting(1);
require_once("Basic.php");
if($_REQUEST['servicefunction']=='print')
{
	$_REQUEST = $_SESSION['_REQUEST_vars'];
	$_REQUEST['CREATEPDF'] = true;
	if(strpos($_REQUEST['servicemod'],'?')!==false)
		$servicemod = substr($_REQUEST['servicemod'],0,strpos($_REQUEST['servicemod'],'?'));
	else
		$servicemod = $_REQUEST['servicemod'];

	if($_REQUEST['bottom_print_servicemod'])
	{
		// bottom_print_servicemod is set by StudentGBGrades.php
		$_REQUEST['servicemod'] = $_REQUEST['bottom_print_servicemod'];
		if(strpos($_REQUEST['servicemod'],'?')!==false)
			$servicemod = substr($_REQUEST['servicemod'],0,strpos($_REQUEST['servicemod'],'?'));
		else
			$servicemod = $_REQUEST['servicemod'];
		if($_REQUEST['bottom_print_cpid'])
			$_REQUEST['course_section_id'] = $_REQUEST['bottom_print_cpid'];
		if($_REQUEST['bottom_print_resource_id'])
			$_REQUEST['resource_id'] = $_REQUEST['bottom_print_resource_id'];
	}
	ob_start();
	define('LOADED_BY_MODULES',true);
	include('modules/'.$servicemod);

	if($_REQUEST['_template_id'])
		DrawTemplateHeaders('','footer');

	if($_SERVER['HTTPS']=='on')
		$html = ob_get_contents();
	else
		$html = ob_get_contents();


	ob_end_clean();
	if(!$htmldocPath)
		ErrorMessage(array(_("PDF file can not be created, because HTMLDOC is not installed or not working. Ensure that HTMLDOC is installed and path is configured in config.php")), 'fatal');

	// get a temp filename, and then change its extension from .tmp to .html to make htmldoc happy.
	if(function_exists('sys_get_temp_dir'))
		$tmp_dir = sys_get_temp_dir();
		
	$temphtml=tempnam($tmp_dir,'html');
	$fp=@fopen($temphtml,"w+");
	if (!$fp) {
		die("Can't open $temphtml");
	}
	fputs($fp,'<HTML><BODY>'.$html.'</BODY></HTML>');
	@fclose($fp);

	header("Cache-Control: public");
	header("Pragma: ");
	header("Content-Type: application/pdf");
	header("Content-Disposition: inline; filename=\"".ProgramTitle().".pdf\"\n");

	$orientation = 'portrait';

	if(($_REQUEST['customised_view'] && $_REQUEST['customised_view']!='false') || $_SESSION['orientation'] == 'landscape')
	{
		$orientation = 'landscape';
		unset($_SESSION['orientation']);
	}

	if($language!='en_US')
		$charset = '--charset utf-8';
	putenv("HTMLDOC_NOCGI=1");	
	passthru("$htmldocPath --webpage --embedfonts --quiet -t pdf12 --jpeg --no-links --$orientation $charset --footer t --header . --left 0.5in --top 0.5in \"$temphtml\"");
//	exec("$htmldocPath --webpage --embedfonts --quiet -t pdf12 --jpeg --no-links --$orientation $charset --footer t --header . --left 0.5in --top 0.5in \"$temphtml\"");
//	@unlink($temphtml);
}
else
{
	if($language=='en_US')
		echo "<HTML>";
	else
		echo '<HTML lang="'.substr($language,0,2).'" xml:lang="'.substr($language,0,2).'">';
	echo "
		<HEAD>
		<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
		<TITLE>AIS-Integral Education</TITLE>";
		if($language!='en_US')
			echo '<meta http-equiv="Content-Type" content="text/html; charset="UTF-8" /><meta http-equiv="Content-Language" content="'.strtolower(str_replace('_','-',$language)).'" />';

		if($_SYSTEM['direction']=='RTL')
			echo '<link rel="stylesheet" type="text/css" media="all" href="js/rtl.css" />';

			echo "<link rel=stylesheet type=text/css href='style.css'>";
			echo	 "<style>";
	/*		echo	 	".SelectedTopTab {";
			echo	 		"background-image:url(images/themes/bg_top.jpg);";
			echo	 	"}"; */
			echo	 	".StandartBGStyle {";
			echo	 		"background-image:url(images/themes/bg_dark.png);";
			echo	 	"}";
			echo	 	".Gridtable_header {";
			echo	 		"background-image:url(images/themes/bg_dark.png);";
			echo 			"background-repeat:repeat-x";
			echo	 	"}";
			echo	 	".select-free";
			echo	 	"{";
			echo	 		"position:absolute";
			echo	 		"z-index:10";
			echo	 		"overflow:hidden;/*must have*/";
				 		if(get_user('PROFILE')=='parent' || get_user('PROFILE')=='student')
							echo "/*must have for any value*/;";
						  else
							echo "width:209px;/*must have for any value*/;";
					echo "
				}
				</style>
		<link rel=stylesheet type=text/css href=images/themes/stylesheet.css>
		</HEAD>

		<BODY style='background-image:url(themes/".Preferences('HIGHLIGHT_MENU')."/bg_light.png);background-position:bottom;background-repeat:repeat-x;background-attachment:fixed;' bgcolor='#".Preferences('HIGHLIGHT_BACKGROUND')."' leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>";
	// LEFT
	echo '<TABLE width=100% cellpadding=0 cellspacing=0 border=0><TR>';

	echo '<TD align=left width=10%>';
	echo '</TD>';

	echo '<TD align=center width=80%>';
	echo '<TABLE><TR>';

	echo '</TR></TABLE>';
	echo '</TD>';

	echo '<TD align=right width=10%>';
	echo '<TABLE><TR>';

	echo '</TR></TABLE>';
	echo '</TD></TR></TABLE>';

	if($_REQUEST['servicefunction']=='templates')
	{
		$templates_array = db_request(db_execute("SELECT ID,TITLE FROM LH_TEMPLATES ORDER BY TITLE"));
		DisplayHeader(_('Letterhead Templates'));
		DisplayHeader(_('Choose a template to add to the above report.'));

		$content = '<TABLE><TR>';
		foreach($templates_array as $template)
		{
			$i++;
			if($i%5===0)
				$content .= '</TR><TR>';
			$content .= '<TD>'.DrawTab($template['TITLE'],'modloader.php?_template_id='.$template['ID'].' target=body','CCBBCC','000000','_circle',array('tabcolor'=>Preferences('HIGHLIGHT'),'textcolor'=>'FFFFFF'),'width=100%').'</TD>';
		}
		$content .= '</TR></TABLE><BR><BR><BR>';
		echo DisplayHeader($content);
	}
	elseif($_REQUEST['external_help'])
	{
		echo '<IFRAME src='.$_REQUEST['external_help'].' height=200 width=100% border=0 />';
	}
	else
	{
//		include 'Help.php';
		include 'Menu.php';

		$profile = get_user('PROFILE');
		if($_REQUEST['help_expanded']=='true')
		{
			echo '<DIV style="width:1070px;position:relative;clear:both;">'; // wrapper
			echo '<DIV style="position:relative;float:left;background-color:white;width:800;min-height:168px;scroll:auto;border:1px solid #'.Preferences('HIGHLIGHT').';margin-left:20px;padding-left:15px;padding-right:15px;">';
			echo '<DIV class=BottomButton>';
			if($_REQUEST['servicemod'])
			{
				$_REQUEST['servicemod'] = $_SESSION['_REQUEST_vars']['servicemod'];
			}
			else
				echo '<b>'._('Welcome to AIS-Integral Help').'</b>';

			if(strpos($_SESSION['modcat'],'../')!==false || strpos($_SESSION['modcat'],'://')!==false || strpos($_REQUEST['servicemod'],'../')!==false || strpos($_REQUEST['servicemod'],'://'))
				die('go away');

			if(file_exists($staticpath.'modules/'.str_replace('.php','.html',_str_replace_once('/','/Help/',$_REQUEST['servicemod']))) || file_exists($staticpath.'modules/'._str_replace_once('.php',($profile_extra = '.'.get_user('PROFILE')).'.html',_str_replace_once('/','/Help/',$_REQUEST['servicemod']))))
			{
				include($staticpath.'modules/'.str_replace('.php',$profile_extra.'.html',_str_replace_once('/','/Help/',$_REQUEST['servicemod'])));
			}
			elseif($_REQUEST['servicemod'])
				echo '<P>No help is available for this program.</P>';
			else
				echo $help['default'];
			echo '</DIV>';
			echo '</DIV>';
			echo '</DIV>'; // wrapper

		}
	}
	echo '</BODY>';
	echo '</HTML>';
}

function _str_replace_once($needle , $replace , $haystack)
{
	$pos = strpos($haystack, $needle);
	if ($pos === false)
		return $haystack;

	return substr_replace($haystack, $replace, $pos, strlen($needle));
}
?>
