-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinstall.cgi
executable file
·476 lines (418 loc) · 14.4 KB
/
install.cgi
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env lua5.1
require 'includes.bootstrap'
settings.output_buffering = false
ophal.bootstrap(5, function ()
-- Settings
local default_settings = {
site = {
name = 'Ophal',
logo_title = 'The Ophal Project',
logo_path = 'images/ophalproject.png',
},
slash = string.sub(package.config,1,1),
language = 'en',
language_dir = 'ltr',
}
for k, v in pairs(default_settings) do
if settings[k] == nil then
settings[k] = v
end
end
-- Force settings
settings.theme = {name = 'install'}
-- Detect phase
local phase = tonumber(_GET.phase) or 1
-- Load Core API
require 'includes.common'
require 'includes.locale'
require 'includes.route'
require 'includes.theme'
-- Pager
theme.install_pager = function (variables)
if variables == nil then variables = {} end
local options = variables.options
local previous = ''
if phase > 1 then
previous = ('<p><a href="%s%sinstall.cgi?phase=%s"><< Previous</a> '):format(base.system_root , base.route, phase - 1)
end
return table.concat{
('<div %s id="install_pager">'):format(render_attributes(options)),
previous,
('<a href="%s%sinstall.cgi?phase=%s">Next >></a></p>'):format(base.system_root, base.route, phase + 1),
'</div>'
}
end
-- phases
local content = ''
local phases = {
-- Welcome page
function ()
-- Look for settings.lua
if seawolf.fs.is_file [[settings.lua]] then
-- Redirect to next phase
redirect(('%s%sinstall.cgi?phase=3'):format(base.system_root, base.route))
end
page_set_title 'Phase 1: Welcome!'
content = ([[<h3>Welcome to the Ophal installation process.</h3>
<p>Before you proceed, please consider the following</p>
<p><ol>
<li>For enhanced security, do *not* run this installer in production.</li>
<li>Javascript enabled is needed in order to complete the installation.</li>
<li>No dabatase is created by this installer, you need to create one in advance.</li>
</ol>
</p>
]] .. theme{'install_pager'}):format(base.system_root, base.route, 2)
end,
-- Verify pre-requisites
function ()
local libraries, status, err, output, continue
local tinsert, tconcat = table.insert, table.concat
-- Look for settings.lua
if seawolf.fs.is_file 'settings.lua' then
-- Redirect to next phase
redirect(('%s%sinstall.cgi?phase=3'):format(base.system_root, base.route))
end
page_set_title 'Phase 2: Pre-requisites'
-- Library checker
libraries = {
['socket.url'] = {
name = 'LuaSocket',
required = true,
},
lfs = {
name = 'LuaFilesystem',
required = true,
},
uuid = {
name = 'luuid',
required = true,
},
DBI = {
name = 'LuaDBI',
required = true,
},
lpeg = {
name = 'LPEG',
required = true,
},
dkjson = {
name = "David Kolf's JSON",
required = true,
},
['seawolf.variable'] = {
name = 'Seawolf: variable',
required = true,
},
['seawolf.fs'] = {
name = 'Seawolf: filesystem',
required = true,
},
['seawolf.text'] = {
name = 'Seawolf: text',
required = true,
},
['seawolf.behaviour'] = {
name = 'Seawolf: behaviour',
required = true,
},
['seawolf.contrib'] = {
name = 'Seawolf: contrib',
required = true,
},
}
output = {
'<table>',
'<thead><th>Library</th><th>Machine name</th><th>Required?</th><th>Status</th><th>Error</th></thead>'
}
-- Find required libraries, both optional and required
continue = true
for machine_name, library in pairs(libraries) do
tinsert(output, '<tr>')
tinsert(output, ('<td>%s</td>'):format(library.name))
tinsert(output, ('<td>"%s"</td>'):format(machine_name))
tinsert(output, ('<td>%s</td>'):format(library.required and 'Required' or 'Optional'))
-- Status
status, err = pcall(require, machine_name)
if status then
tinsert(output, '<td>Found</td>')
else
continue = false
tinsert(output, ('<td>Missing</td><td><pre>"%s"</pre></td>'):format(err))
end
tinsert(output, '</tr>')
end
tinsert(output, '</table>')
-- Say: All requirements are OK
if continue then
tinsert(output, theme{'install_pager'})
else
tinsert(output, '<p>Please install any missing library. Read the <a href="http://ophal.org/manual/--version--/install#libraries">documentation</a> for details.</p>')
end
content = tconcat(output)
end,
-- Generate configuration file
function ()
local tinsert, tconcat = table.insert, table.concat
-- Look for settings.lua
if seawolf.fs.is_file 'settings.lua' then
-- Redirect to next phase
redirect(('%s%sinstall.cgi?phase=4'):format(base.system_root, base.route))
end
require 'includes.module'
require 'includes.form'
add_js 'libraries/jquery.min.js'
add_js 'libraries/uuid.js'
add_js{[[
$(document).ready(function() {
$('#generate').click(function() {
$('#settings').html($('#settings_template').html()
.replace('!site_name', $('#sitename').val())
.replace('!files_path', $('#files_path').val())
.replace('!lorem_ipsum_module', $('#lorem_ipsum_module').is(':checked'))
.replace('!content_module', $('#content_module').is(':checked'))
.replace('!comment_module', $('#comment_module').is(':checked'))
.replace('!user_module', $('#user_module').is(':checked'))
.replace('!tag_module', $('#tag_module').is(':checked'))
.replace('!menu_module', $('#menu_module').is(':checked'))
.replace('!file_module', $('#file_module').is(':checked'))
.replace('!boost_module', $('#boost_module').is(':checked'))
.replace('!test_module', $('#test_module').is(':checked'))
);
$('#vault').html($('#vault_template').html()
.replace('!site_hash', uuid())
.replace('!db_filepath', $('#db_filepath').val())
);
$('#install_pager').show();
});
});
]], type = 'inline'}
page_set_title 'Phase 3: Configuration file settings.lua'
content = tconcat{
'<h3>Step 1. Configure your site</h3>',
theme{'form', action = 'install.cgi',
elements = {
{'textfield', title = 'Site name', value = 'Ophal', attributes = {id = 'sitename'}, weight = 1},
{'textfield', title = 'Database file path', attributes = {id = 'db_filepath'}, weight = 5},
{'textfield', title = 'File directory', value = 'files', attributes = {id = 'files_path'}, weight = 10},
{'checkbox', title = 'Enable the Lorem Ipsum module', value = '1', attributes = {id = 'lorem_ipsum_module'}, weight = 15},
{'checkbox', title = 'Enable the Content module', value = '0', attributes = {id = 'content_module'}, weight = 20},
{'checkbox', title = 'Enable the Comment module', value = '0', attributes = {id = 'comment_module'}, weight = 25},
{'checkbox', title = 'Enable the User module', value = '0', attributes = {id = 'user_module'}, weight = 30},
{'checkbox', title = 'Enable the Tag module', value = '0', attributes = {id = 'tag_module'}, weight = 35},
{'checkbox', title = 'Enable the Menu module', value = '0', attributes = {id = 'menu_module'}, weight = 40},
{'checkbox', title = 'Enable the File module (experimental)', value = '0', attributes = {id = 'file_module'}, weight = 45},
{'checkbox', title = 'Enable the Boost module (experimental)', value = '0', attributes = {id = 'boost_module'}, weight = 50},
{'checkbox', title = 'Enable the Test module (experimental)', value = '0', attributes = {id = 'test_module'}, weight = 55},
{'button', value = 'Generate', attributes = {id = 'generate'}, weight = 100},
}
},
'<div id="settings"></div>',
[=[<div id="settings_template" style="display:none">
<h3>Step 2. Create file settings.lua</h3>
<p>Copy the following text into the file <i>settings.lua</i> and put it right in the exact same folder of file <i>index.cgi</i>:</p>
<textarea cols="100" rows="15">
return function(settings, vault)
settings.language = 'en'
settings.language_dir = 'ltr'
settings.site = {
-- scheme = 'http',
-- domain_name = 'www.example.com',
frontpage = 'lorem_ipsum',
name = '!site_name',
hash = vault.site.hash,
logo_title = 'The Ophal Project',
logo_path = 'images/ophalproject.png',
files_path = '!files_path',
}
settings.micro_cache = false
settings.debugapi = true
settings.maintenance_mode = false
settings.output_buffering = false
settings.sessionapi = {
enabled = true,
ttl = 86400,
lock_ttl = 120,
}
settings.formapi = false
settings.date_format = '!%Y-%m-%d %H:%M UTC'
settings.route_aliases_storage = false
settings.route_aliases_prepend_language = false
settings.route_redirects_storage = false
settings.route_redirects_prepend_language = false
--[[ Active/Disabled modules
List of Ophal modules to load on bootstrap.
Example:
settings.modules = {
mymodule = true,
othermodule = false, -- disabled module
}
]]
settings.modules = {
lorem_ipsum = !lorem_ipsum_module,
content = !content_module,
user = !user_module,
tag = !tag_module,
menu = !menu_module,
file = !file_module, -- Experimental!
boost = !boost_module, -- Experimental!
test = !test_module, -- Experimental!
}
--[[ Database connection settings
Ophal automatically connects on bootstrap to a database if a the key
'db' is set with connection settings.
Example:
settings.db = {
default = {
driver = 'PostgreSQL',
database = 'database',
username = vault.db.default.username,
password = vault.db.default.password,
host = 'localhost',
port = '5432',
}
}
]]
settings.db = vault.db
--[[ Extend jailed environment
Ophal code is jailed into an environment with few functions. Use the
global variable 'env' to add external functions and lua modules.
Example:
require 'external.library'
env.myfunction = external.library.function
]]
--[[
Theme settings.
]]
settings.theme = {
name = 'basic',
}
--[[ Extend templates environment
Template files (like: *.tpl.*) i.e: page.tpl.html, have a very limited
set of functions available. Use setting 'template_env' to add external
functions and lua modules.
NOTE: Template variables are not overridden by the ones with this setting.
Example:
settings.template_env = {}
require 'external.library'
settings.template_env.myfunction = external.library.function
]]
--[[ Mobile support settings
The mobile_detect library is a helper for mobile web development.
Set settings.mobile to nil to turn off mobile support.
Always make sure to set settings.domain_name if settings.redirect is
set to true.
Example:
settings.mobile = {
theme = 'mobile',
domain_name = 'mobile.mydomain.com',
redirect = true,
}
]]
--[[
Boost provides static cache by saving all the output to files.
Example:
settings.modules.boost = true
settings.boost = {
path = 'files/boost/',
lifetime = 3600, -- seconds
signature = '<!-- Page cached by Boost @ %s, expires @ %s -->',
date_format = '!%Y-%m-%d %T UTC',
}
]]
end
</textarea></div>]=],
'<div id="vault"></div>',
[=[<div id="vault_template" style="display:none">
<h3>Step 3. Create file vault.lua</h3>
<p>Copy the following text into the file <i>vault.lua</i> and put it right in the exact same folder of file <i>index.cgi</i>:</p>
<textarea cols="100" rows="10">
--[[
This file is for storage of sensitive information ONLY.
]]
local m = {
--[[ Site settings ]]
site = {
hash = '!site_hash',
},
--[[ Database connection settings
Ophal automatically connects on bootstrap to a database if a the key
'db' is set with connection settings.
Example:
settings.db = {
default = {
driver = 'PostgreSQL',
database = 'database',
username = 'username',
password = 'password',
host = 'localhost',
port = '5432',
}
}
]]
db = {
default = {
driver = 'SQLite3',
database = '!db_filepath',
}
},
}
return m
</textarea></div>]=],
theme{'install_pager', style = 'display: none;'}
}
end,
-- Do install
function ()
local status, err, file_directory, fh
local tconcat = table.concat
local output = ''
-- Load settings
local status, err = pcall(require, 'settings')
if not status then
err = "Missing file or error when trying to load 'settings.lua'"
else
err = nil
-- Check 'files' directory permissions
if not (settings.site and settings.site.files_path) then
err = 'settings.site.files_path is not set!'
else
files_path = settings.site.files_path
if seawolf.fs.is_file(files_path) then
err = ("Created file directory: '%s' is an actual file, not a directory! Please fix and try again."):format(files_path)
elseif seawolf.fs.is_dir(files_path) then
if not seawolf.fs.is_writable(files_path) then
err = ("File directory: '%s' is not writable!"):format(files_path)
else
fh = io.open(files_path .. '/.htaccess', 'w')
fh:write([[SetHandler Ophal_Security_Do_Not_Remove
Options None
Options +FollowSymLinks
]])
output =
'<p>Installation complete!</p>' ..
('<p>Your new site is available <a href="%s">here</a></p>'):format(base.route)
fh:close()
end
else
err = ("File directory not found! Please create directory '%s'."):format(files_path)
end
end
end
page_set_title 'Installing...'
content = tconcat{
err and ('<strong>Error</strong>: %s'):format(err) or '',
output,
}
end
}
-- Run phase
phases[phase]()
-- Render page
print_t{'html',
header_title = ophal.header_title,
title = ophal.title,
content = content,
javascript = get_js(),
css = get_css(),
}
end)