summaryrefslogtreecommitdiff
path: root/code/admin/render.php
blob: 9d4175eb2f75f8266ee98fc33a524216065f88f0 (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
<?php
	require_once('utils.php');
	need_auth();

	// 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);

	// URL params clean-up
	$action=sanitize($_GET, 'action', '/[^a-z_]+/', 'preview'); /* Could be : preview, edit, publish */
	$page=sanitize($_GET, 'page', '/[^a-z0-9\/]+/', $site_conf['site_default_page']); // Never put \. in this regex

	// 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));
		}
	}
?>