forked from k0a1a/hotglue2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_build.inc.php
More file actions
174 lines (144 loc) · 3.87 KB
/
module_build.inc.php
File metadata and controls
174 lines (144 loc) · 3.87 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/*
* module_build.inc.php
* Module for building a page as static HTML
*
* Copyright Gottfried Haider, Danja Vasiliev 2010.
* This source code is licensed under the GNU General Public License.
* See the file COPYING for more details.
*/
@require_once('config.inc.php');
require_once('common.inc.php');
require_once('controller.inc.php');
require_once('html.inc.php');
require_once('modules.inc.php');
function build_render_page_early($args) {
if ($args['edit']) {
if (USE_MIN_FILES) {
html_add_js(base_url() . 'modules/build/build-edit.min.js');
} else {
html_add_js(base_url() . 'modules/build/build-edit.js');
}
//html_add_css(base_url().'modules/build/build-edit.css');
}
//html_add_css(base_url().'modules/build/build.css');
}
function get_single_page_html($page_name) {
global $base_url_cached;
$base_url_cached = '/';
$page = $page_name;
page_canonical($page);
$args[0] = array($page);
load_modules('glue');
html_flush();
default_html(false);
render_page(array('page' => $page, 'edit' => false, 'build' => true));
$html = html_finalize();
return $html;
}
function write_single_page($page_name, $html) {
// save the HTML to a file
if ($page_name == 'start') {
$filename = STATIC_DIR . '/index.html';
} else {
$filename = STATIC_DIR . '/' . $page_name . '.html';
}
file_put_contents($filename, $html);
}
function copy_folders($dirs) {
foreach ($dirs as $in => $out) {
// Create directory if it doesn't exist
if (!is_dir($out)) {
mkdir($out, 0777, true);
}
// Clear directory
$files = glob($out . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
// Copy files from $in to $out
if (is_dir($in)) {
$files = glob($in . '/*');
foreach ($files as $file) {
if (is_file($file)) {
$filename = basename($file);
copy($file, $out . '/' . $filename);
}
}
}
}
}
function copy_global_assets() {
$dirs = [
'css' => STATIC_DIR . '/css',
'img' => STATIC_DIR . '/img',
'js' => STATIC_DIR . '/js'
];
copy_folders($dirs);
}
function copy_page_assets($page_name) {
// Copy uploads to static directory
$uploads_in = CONTENT_DIR . '/' . $page_name . '/shared';
$uploads_out = STATIC_DIR . STATIC_UPLOAD_DIR . '/' . $page_name;
// Make sure key directories exist
$dirs = [
$uploads_in => $uploads_out
];
copy_folders($dirs);
}
function is_draft($page) {
$pagefile = $page . '.head.page';
if (page_exists($pagefile)) {
$obj = @load_object(["name" => $pagefile]);
if (!isset($obj)) {
return true;
}
if (@isset($obj['#data']) && @isset($obj['#data']['page-status'])) {
if ($obj['#data']['page-status'] == 'live') {
return false;
}
}
}
return true;
}
function controller_builder($args) {
if ($args[0][1] == 'build_all') {
load_modules('glue');
$pns = pagenames(array());
$pns = $pns['#data'];
foreach ($pns as $pn) {
// If page is a draft, continue
if (is_draft($pn)) {
continue;
}
$page_html = get_single_page_html($pn);
write_single_page($pn, $page_html);
copy_page_assets($pn);
}
copy_global_assets();
} else {
$page_name = $args[0][0];
// If page is a draft, throw 404
if (is_draft($page_name)) {
return response('Page not found', 404);
}
$page_html = get_single_page_html($page_name);
write_single_page($page_name, $page_html);
copy_page_assets($page_name);
copy_global_assets();
}
// If script is being run via CLI, exit
if (PHP_SAPI === 'cli') {
return;
}
// If referrer exists, redirect back to it, otherwise redirect to $page
if (isset($_SERVER['HTTP_REFERER'])) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
header('Location: ' . str_replace('/build', '/edit', $_SERVER['REQUEST_URI']));
}
}
register_controller('*', 'build', 'controller_builder', array('auth' => PAGES_NEED_AUTH));
register_controller('*', 'build_all', 'controller_builder', array('auth' => PAGES_NEED_AUTH));