summaryrefslogtreecommitdiff
path: root/code/admin/render.php
blob: 2d49f01596d00280c22aee07d5157f44c4f70e72 (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
66
67
68
<?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 args from the command line
		$params=array(
			'action' => 'publish',
			'page' => $argv[1]
		);
	} else {
		// In web mode, take from args from GET request
		$params=&$_GET;
	}
	// params clean-up
	$action=sanitize($params, 'action', '/[^a-z_]+/', 'preview'); /* Could be : preview, edit, publish */
	$page=sanitize($params, '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));
		}
	}
?>