<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="csrf-token" content="<?php echo pjAppController::getCsrfToken(); ?>" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title><?php __('script_name') ?> by PHPJabbers.com</title>
        <?php
        $cnt = count($controller->getCss());
        foreach ($controller->getCss() as $i => $css)
        {
            $isRemote = isset($css['remote']) && $css['remote'];
            $href = ($isRemote ? NULL : PJ_INSTALL_URL) . $css['path'] . $css['file'];
            if (!$isRemote) {
                $file = PJ_INSTALL_PATH . $css['path'] . $css['file'];
                if (is_file($file)) {
                    $href .= '?v=' . filemtime($file);
                }
            }
        	echo '<link rel="stylesheet" href="' . htmlspecialchars($href, ENT_QUOTES, 'UTF-8') . '">';
        	echo "\n";
        	if ($i < $cnt - 1)
        	{
        		echo "\t";
        	}
        }
        ?>
        <?php if (isset($tpl['adminThemeColors']) && is_array($tpl['adminThemeColors'])): ?>
        <style>:root{<?php foreach ($tpl['adminThemeColors'] as $var => $val) { echo $var . ':' . $val . ';'; } ?>}</style>
        <?php endif; ?>
	</head>
	<body class="fixed-sidebar" data-table-density="<?php echo isset($tpl['option_arr']['o_admin_table_density']) ? htmlspecialchars($tpl['option_arr']['o_admin_table_density']) : 'default'; ?>">
		<div id="wrapper">
			<?php require dirname(__FILE__) . '/elements/menu-left.php'; ?>
			<div id="page-wrapper" class="gray-bg dashbard-1">
    			<?php
    			require dirname(__FILE__) . '/elements/menu-top.php';
    			
    			require $content_tpl;
    			
    			include dirname(__FILE__) . '/elements/footer-default.php'; 
    			?>
    		</div>
		</div><!-- #wrapper -->
		<?php 
    	$cnt = count($controller->getJs());
    	foreach ($controller->getJs() as $i => $js)
    	{
            $isRemote = isset($js['remote']) && $js['remote'];
            $src = ($isRemote ? NULL : PJ_INSTALL_URL) . $js['path'] . $js['file'];
            if (!$isRemote) {
                $file = PJ_INSTALL_PATH . $js['path'] . $js['file'];
                if (is_file($file)) {
                    $src .= '?v=' . filemtime($file);
                }
            }
    					$_src_file = ltrim(str_replace(PJ_INSTALL_URL, '', $src), '/');
			$_src_ver = file_exists(rtrim($_SERVER['DOCUMENT_ROOT'],'/') . '/' . $_src_file) ? filemtime(rtrim($_SERVER['DOCUMENT_ROOT'],'/') . '/' . $_src_file) : '';
			echo '<script src="' . htmlspecialchars($src . ($_src_ver ? '?v=' . $_src_ver : ''), ENT_QUOTES, 'UTF-8') . '"></script>';
    		echo "\n";
    		if ($i < $cnt - 1)
    		{
    			echo "\t";
    		}
    	}
    	?>
	<script>
	/* CSRF: hook jQuery, fetch, and normal forms to send token on every internal POST */
	function pjGetCsrfToken() {
		var m = document.querySelector('meta[name="csrf-token"]');
		return m ? m.getAttribute("content") : "";
	}
	function pjIsInternalAction(action) {
		if (!action) {
			return true;
		}
		var a = document.createElement("a");
		a.href = action;
		return !a.hostname || a.hostname === window.location.hostname;
	}
	if (typeof jQuery !== "undefined") {
		(function(jq) {
			var _ajax = jq.ajax;
			jq.ajax = function(url, options) {
				var s = (typeof url === "string") ? (options || {}) : (url || {});
				if (typeof url === "string") s.url = url;
				if (s.type && s.type.toUpperCase() === "POST") {
					var token = pjGetCsrfToken();
					if (token) { s.headers = s.headers || {}; s.headers["X-CSRF-Token"] = token; }
				}
				return _ajax.call(this, s);
			};

			/* Session expiry: if any AJAX call returns 401 (unauthenticated),
			   send the user to the login page instead of failing silently. */
			jq(document).ajaxError(function (event, jqxhr) {
				if (jqxhr && jqxhr.status === 401) {
					window.location.href = "index.php?controller=pjBase&action=pjActionLogin";
				}
			});
		})(jQuery);
	}
	if (window.fetch) {
		(function(nativeFetch) {
			window.fetch = function(input, init) {
				init = init || {};
				var method = (init.method || (input && input.method) || "GET").toUpperCase();
				var url = typeof input === "string" ? input : (input && input.url ? input.url : "");
				if (method === "POST" && pjIsInternalAction(url)) {
					var token = pjGetCsrfToken();
					if (token) {
						init.headers = new Headers(init.headers || (input && input.headers) || {});
						init.headers.set("X-CSRF-Token", token);
					}
				}
				return nativeFetch.call(this, input, init);
			};
		})(window.fetch);
	}
	document.addEventListener("submit", function(e) {
		var form = e.target;
		if (!form || !form.tagName || form.tagName.toLowerCase() !== "form") {
			return;
		}
		if ((form.getAttribute("method") || "get").toLowerCase() !== "post" || !pjIsInternalAction(form.getAttribute("action"))) {
			return;
		}
		if (!form.querySelector('input[name="csrf_token"]')) {
			var token = pjGetCsrfToken();
			if (token) {
				var input = document.createElement("input");
				input.type = "hidden";
				input.name = "csrf_token";
				input.value = token;
				form.appendChild(input);
			}
		}
	}, true);
	</script>
	</body>
</html>
