summaryrefslogtreecommitdiff
path: root/code/admin/render.php
blob: 0492224132f72d3c8ae90cfcd72f5299534bd56a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
	require_once('utils.php');

	// Config loading
	$site_conf = load_ini_site_conf("content/site_conf.ini"); 
	if ( ! is_array($site_conf) ) trigger_error("Error parsing site_conf.ini", E_USER_ERROR);

	if (PHP_SAPI === 'cli') {
		// In cli mode, take page name from the command line (publish only)
		$action='publish';
		$page = sanitize($argv, 1, RE_RELPATH_CLEANER, '');
	} else {
		// In web mode, enforce authentication and take from args from GET request
		need_auth();
		$action=sanitize($_GET, 'action', RE_IDENTIFIER_CLEANER, 'preview'); /* Could be : preview, edit, publish */
		$page = sanitize($_GET, 'page', RE_RELPATH_CLEANER, $site_conf['site_default_page']);
	}

	// Template vars init ($page, $page_path, $page_props, $page_tpl_url)
	$page_path = "content/$page"; 
	if ( ! is_dir($page_path) ) trigger_error("Error : page does not exists ($page)", E_USER_ERROR);
	$page_props = load_ini_page_props($page);
	if ( ! is_array($page_props) ) trigger_error("Error parsing page properties ($page/props.ini)", E_USER_ERROR);
	$page_tpl_url = str_repeat('../', substr_count($page, '/')) . "admin/templates/" . $page_props['page_template'] . '/';

	if ($action === 'publish') ob_start(); /* Buffering for redirecting to file instead of browser */

	// Basic HTML5 skeleton + page props insertion + template call
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?=$page_props['page_title']?></title>
<meta name="description" content="<?=$page_props['page_description']?>">
<meta name="keywords" content="<?=$page_props['page_keywords']?>">
<link rel="stylesheet" href="<?="$page_tpl_url" /*FIXME*/?>screen.css">
</head>
<body>
<?php
	require("templates/" . $page_props['page_template'] . '/layout-' . $page_props['page_layout'] . '.php');
	if ( $action === 'edit') require('editor-bind-code.html');
?>
</body>
</html>
<?php
	// If publishing, write resulting HTML page on a .html file
	if ($action === 'publish') {
		$out=ob_get_contents();
		ob_end_clean();
		$dest="../$page.html";
		$destfold=dirname($dest);
		if ( ! is_dir($destfold) ) {
			if ( ! mkdir($destfold,0777,true) ) {
				trigger_error("Error : Can't create '$destfold'", E_USER_ERROR);
			}
		}
		$size=file_put_contents($dest, $out);
		if ( $size === FALSE ) {
			trigger_error("Error : Can't write " . strlen($out) . " bytes to '$dest'", E_USER_ERROR);
		} else {
			echo json_encode(array('result'=>'ok', 'size' => $size));
		}
	}
?>