<?php
	require(dirname(__FILE__).'/../init.php');

	define('AKB_WL_LANG_FILE',			dirname(__FILE__).'/../includes/language/'.$GLOBALS['currentLanguage'].'/whitelabel.ini');
	define('AKB_LANG_FILE',				dirname(__FILE__).'/../includes/language/'.$GLOBALS['currentLanguage'].'/front_language.ini');
	define('AKB_TPL_LOAD_PATH',			dirname(__FILE__).'/templates/');
	define('AKB_TPL_PHP_PATH',			dirname(__FILE__).'/includes/display/');
	define('AKB_TPL_HTTP_PATH',			$GLOBALS['pathToKB'].'/admin/templates/');
	define('AKB_TPL_IMAGE_PATH',		AKB_TPL_HTTP_PATH.'Images');
	define('AKB_PANEL_LOAD_PATH',		AKB_TPL_LOAD_PATH.'Panels/');
	define('AKB_SNIPPET_LOAD_PATH',		AKB_TPL_LOAD_PATH.'Snippets/');
	define('CLASS_DIR',					dirname(__FILE__).'/includes/classes');

	require(dirname(__FILE__).'/../lib/email.php');
	require(dirname(__FILE__).'/includes/helpers/helper.template.php');

	$GLOBALS['SNIPPETS'] = '';
	$GLOBALS['Message'] = '';
	$GLOBALS['HidePanels'] = array();
	
	require(CLASS_DIR.'/class.helper.php');
	require(CLASS_DIR.'/class.category.php');
	require(CLASS_DIR.'/../../../includes/classes/class.categories.php');
	require(CLASS_DIR.'/class.news.php');
	require(CLASS_DIR.'/class.template.php');
	require(CLASS_DIR.'/class.admintable.php');
	require(CLASS_DIR.'/class.question.php');
	require(CLASS_DIR.'/class.fileindex.php');
	require(CLASS_DIR.'/../../../includes/classes/class.questions.php');
	require(CLASS_DIR.'/class.auth.php');
	require(CLASS_DIR.'/class.install.php');
	require(CLASS_DIR.'/class.engine.php');
	
	$GLOBALS['AKB_CLASS_HELPER']	= new AKB_HELPER();
	$GLOBALS['AKB_CLASS_CATEGORY']	= new AKB_CATEGORY();
	$GLOBALS['AKB_CLASS_NEWS']	= new AKB_NEWS();
	$GLOBALS['AKB_CLASS_TEMPLATE']	= new AKB_TEMPLATE();
	$GLOBALS['AKB_CLASS_QUESTIONS']	= new AKB_QUESTION();
	$GLOBALS['AKB_CLASS_QUESTIONS_FE']	= new AKB_QUESTIONS();
	$GLOBALS['AKB_CLASS_AUTH']		= new AKB_AUTH();
	$GLOBALS['AKB_CLASS_INSTALL']	= new AKB_INSTALL();
	$GLOBALS['AKB_CLASS_FILEINDEX']		= new AKB_FILEINDEX();
	
	$GLOBALS['SNIPPETS'] = '';
	if (!pageLoad()) {
		AKB_AUTH::ShowLoginForm(GetLang('SessionLoggedOutMessage'));
		die();
	}
	// Is the site down for maintenance?
	if ($GLOBALS['downForMaintenance'] == 1) {
		// Show the down for maintenance panel
		$GLOBALS['AKB_CLASS_TEMPLATE']->SetPageTitle(GetLang('hpDownForMaintenance'));
		$GLOBALS['AKB_CLASS_TEMPLATE']->SetTemplate('Maintenance');
		$GLOBALS['AKB_CLASS_TEMPLATE']->ParseTemplate();
		die();
	}
	
	$success = false;
	
	if (FrontendAccess()) {
		
		$type = $_GET['todo'];
		$applies = array();
		
		if ($GLOBALS['AKB_CLASS_AUTH']->IsLoggedIn() && $GLOBALS['AKB_CLASS_AUTH']->HasAdminAccess()) {
			switch ($type) {
				case '__showeditor':
					echo "<html><head></head><body style=\"margin: 0 0 0 0;\"><form action=\"#\">";
					if ($GLOBALS['useWYSIWYGEditor']) {
						echo $GLOBALS['AKB_CLASS_QUESTIONS']->GetWYSIWYGEditor('',"100%","400"); 
					} else {
						echo "<textarea id=\"contentPlainText\" style=\"width: 100%; height: 400px;\"></textarea>";
					}
					echo "</form></body></html>";
					break;
				case '__showadd':
					$categoryId = (isset($_GET['cid'])) ? (int)$_GET['cid'] : 0;
					$answer = '';
					$GLOBALS['CategoryId'] = $categoryId;
					$GLOBALS['FormAction'] = '__processadd';
					$GLOBALS['Message'] = GetLang('AddQuestionIntro');
					$GLOBALS['CancelMessage'] = GetLang('CancelMessage');
					if (!$GLOBALS['AKB_CLASS_AUTH']->HasApprovalAccess(array($categoryId),$editMode = false, $checkCat = true) && validUrl()) {
						$GLOBALS['Message'] = GetLang('NoAppPermAddMessage');
					}
					$GLOBALS['WYSIWYG'] = '<iframe id="editorContainer" src="'.$GLOBALS['pathToKB'].'/admin/incadd_get.php?todo=__showeditor" scrolling="no" style="padding: 0px; border: 0px solid black; width: 100%; height: 430px;" frameborder="0"></iframe>';
					$GLOBALS['CustomFields'] = $GLOBALS['AKB_CLASS_QUESTIONS']->processCustomFields();
					$GLOBALS['HasGroups'] = 0;
					$GLOBALS['SortOrder'] = 0;
					$GLOBALS['lblRestrictPublic'] = GetLang("RestrictPublicQuest");
					$GLOBALS['lblRestrictPrivate'] = GetLang("RestrictPrivateQuest");
					$GLOBALS['HidePanels'][] = 'QuestionStatus';
					$cat = array($categoryId);
					$GLOBALS['CategoryOptions'] = $GLOBALS['AKB_CLASS_CATEGORY']->GetCategoryOptions($cat, '<option %s value="%d">%s</option>', 'selected', '- ', false, $applies);
					$GLOBALS['RelatedCategoryOptions'] = $GLOBALS['AKB_CLASS_CATEGORY']->GetCategoryOptions(array(0), '<option %s value="%d">%s</option>', 'selected', '- ', false, $applies);
					//Setup groups
					$GLOBALS['groups'] = $GLOBALS['AKB_CLASS_QUESTIONS']->getGroupsForSelect();
					$GLOBALS['groupListSize'] = 5;
					
					$GLOBALS['AKB_CLASS_HELPER']->populateArticleDates();
					
					$GLOBALS['AKB_CLASS_TEMPLATE']->SetTemplate('question.quickadd.form');
					$GLOBALS['AKB_CLASS_TEMPLATE']->ParseTemplate();
				break;
				case '__processadd' :
					$questionid = $GLOBALS['AKB_CLASS_QUESTIONS']->SaveNewQuestion();
					$categoryid = $_POST['originCid'];
					?>
					<script>
						window.location='<?php echo GetUrl('category', $categoryid, 'todo=' . $GLOBALS['SuccessMesg'].'_'.$questionid);?>';
					</script>
					<?php
				break;
			}
		}
	} else {
		if (!isset($_POST['frontendPass'])) {
			$GLOBALS['HidePanels'] = array (
				'FrontendLoginError',
			);
		}
	}

	// {{{ If debug is on, print the debug footer
	if (isset($GLOBALS['debug']) && $GLOBALS['debug'] == true) {
		$end_time = microtime_float();
		if (function_exists('memory_get_usage')) {
			$mem_used = memory_get_usage()/1024/1024;
		} else {
			$mem_used = 0;
		}
		printf('<div id="debug">Time taken to generate the page %.4f seconds and performed %d sql queries and used %.4f MB of memory</div>', ($end_time - $start_time), $GLOBALS['AKB_DB']->NumQueries, $mem_used);
	}
	// }}}
?>