-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathsetup.php
More file actions
277 lines (277 loc) · 12.4 KB
/
setup.php
File metadata and controls
277 lines (277 loc) · 12.4 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
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
<?php
/**
* Setup
*
* @package WikiDocs
* @repository https://github.com/Zavy86/wikidocs
*/
session_start();
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors",true);
if(!isset($_SESSION['wikidocs']['csfr']['token'])){$_SESSION['wikidocs']['csfr']['token']=bin2hex(random_bytes(32));}
function checkCSRF(){return !(!isset($_POST['token']) || $_POST['token'] !== $_SESSION['wikidocs']['csfr']['token']);}
function getSetting($key,$default=''){return (string)($_SESSION['wikidocs']['setup'][$key]??$default);}
function sanitizeInput($input){return htmlspecialchars(trim($input),ENT_COMPAT,'UTF-8');}
function checkWebServer(){
if((isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false)){return "apache";}
elseif(isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false){return "nginx";}
else{return null;}
}
// definitions
$errors=false;
$configured=false;
$checks_array=[];
$g_act=$_GET['act']??'preliminary_check';
// include configuration sample
include("sample.config.inc.php");
// defines constants
define('PATH_URI',explode("setup.php",$_SERVER['REQUEST_URI'])[0]);
// Check if already configured
if(file_exists("datasets/config.inc.php")){header("location:index.php");}
// make root dir from given path
$original_dir=str_replace("\\","/",realpath(dirname(__FILE__))."/");
$root_dir=substr($original_dir,0,strrpos($original_dir,($_POST['path'] ?? ''))).($_POST['path'] ?? '');
// preliminary checks
if($g_act=="preliminary_check"){
$server_modules=[];
if(function_exists('apache_get_modules')){
$available_modules=apache_get_modules();
foreach(['mod_rewrite','mod_mime'] as $module){
$server_modules[$module]=in_array($module,$available_modules);
}
}
$php_modules=[];
foreach(['Core','dom','json','mbstring','session','xml'] as $module) {
$label=($module=='Core'?'php_version':'php_'.$module);
$php_modules[$label]=phpversion($module);
}
}
// check action
if($g_act=="check"){
if(!checkCSRF()){die("CSRF token invalid");}
$required_fields=['path','title','subtitle','owner','notice','editcode'];
$_SESSION['wikidocs']['setup']=[];
foreach($required_fields as $field){
$value=$_POST[$field]??'';
$_SESSION['wikidocs']['setup'][$field]=$value;
$checks_array[$field]=!empty($value);
$errors=$errors||!$checks_array[$field];
}
$checks_array['path']=file_exists($root_dir."setup.php");
$errors=$errors||!$checks_array['path'];
if($_POST['editcode']!==$_POST['editcode_repeat']){
$checks_array['editcode']=false;
$errors=true;
}
}
// conclude action
if($g_act=="conclude"){
if(!checkCSRF()){die("CSRF token invalid");}
$config="<?php\n";
$config_items=[
'DEBUGGABLE'=>'false',
'PATH'=>getSetting('path'),
'TITLE'=>getSetting('title'),
'SUBTITLE'=>getSetting('subtitle'),
'OWNER'=>getSetting('owner'),
'NOTICE'=>getSetting('notice'),
'PRIVACY'=>'null',
'EDITCODE'=>getSetting('editcode')?password_hash(getSetting('editcode'),PASSWORD_DEFAULT):'null',
'VIEWCODE'=>'null',
'COLOR'=>'#4CAF50',
'DARK'=>'false',
'GTAG'=>'null'
];
foreach($config_items as $key=>$value){
if($value===null||$value==='null'){
$config.="define('".$key."', null);\n";
}elseif(is_bool($value)||$value==='true'||$value==='false'){
$config.="define('".$key. "', ".($value==='true'?'true':'false').");\n";
}elseif(is_numeric($value)){
$config.="define('".$key."', ".$value.");\n";
}else{
$config.="define('" .$key."', '".addslashes($value)."');\n";
}
}
// Write configuration file
if(!is_dir($root_dir."datasets/")){mkdir($root_dir."datasets/",0755,true);}
$configPath=$root_dir."datasets/config.inc.php";
file_put_contents($configPath,$config);
// Generate .htaccess
$htaccess="<IfModule mod_rewrite.c>\n";
$htaccess.="\tRewriteEngine On\n";
$htaccess.="\tRewriteBase ".getSetting('path','/')."\n";
$htaccess.="\tRewriteCond %{REQUEST_URI} \.md$ [NC]\n";
$htaccess.="\tRewriteRule ^.*$ ".getSetting('path','/')." [R=301,L]\n";
$htaccess.="\tRewriteCond %{REQUEST_FILENAME} !-f\n";
$htaccess.="\tRewriteRule ^(.*)$ index.php?doc=$1 [NC,L,QSA]\n";
$htaccess.="</IfModule>\n";
// Write .htaccess file
$htaccessPath=$root_dir.".htaccess";
file_put_contents($htaccessPath,$htaccess);
// Check if configuration was successful
$configured=(file_exists($configPath)&&file_exists($htaccessPath));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="helpers/materialize-1.0.0/css/materialize.min.css" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="styles/styles.css" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="styles/styles-light.css" media="screen,projection"/>
<link type="image/ico" rel="icon" href="favicon.ico" sizes="any"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="theme-color" content="#4CAF50">
<style>:root{--theme-color:#4CAF50;}</style>
<title>Setup - Wiki|Docs</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col s12">
<h1>Wiki|Docs</h1>
<p>Just a databaseless markdown flat-file wiki engine..</p>
</div><!-- /col -->
<?php
if($g_act=="preliminary_check"){
// define checks
$check_ok="<span class='secondary-content'><i class='material-icons green-text'>check_circle</i></span>";
$check_ko="<span class='secondary-content'><i class='material-icons red-text'>cancel</i></span>";
$check_nd="<span class='secondary-content'><i class='material-icons grey-text'>cancel</i></span>";
?>
<div class="col s12">
<h2>Checking Web Server and Modules</h2>
Your environment has been verified..
<ul class="collection">
<li class="collection-item">
webserver: <?= checkWebServer() ?? 'apache or nginx is required' ?>
<?= checkWebServer() ? $check_ok : $check_ko ?>
</li>
<?php foreach($server_modules as $key => $module): ?>
<li class="collection-item">
<?= $key ?>: <?= $module ? 'installed' : 'not-installed' ?>
<?= $module ? $check_ok : $check_ko ?>
</li>
<?php endforeach; ?>
<?php foreach($php_modules as $key => $module): ?>
<li class="collection-item">
<span class="secondary-content">
<i class="material-icons <?= $module ? 'green-text' : 'red-text' ?>">
<?= $module ? 'check_circle' : 'cancel' ?>
</i>
</span>
<?=$key?>: <?= $php_modules[$key] ?>
</li>
<?php endforeach; ?>
</ul>
<div class="input-field col s12">
<a href="setup.php?act=setup" class="waves-effect waves-light btn green white-text right">Continue<i class="material-icons right">keyboard_arrow_right</i></a>
</div>
</div><!-- /col -->
<?php
}
?>
<?php if($g_act=="setup"){ ?>
<div class="col s12">
<h2>Configuration</h2>
<p>Setup your wiki engine..</p>
<form action="setup.php?act=check" method="post">
<input type="hidden" name="token" value="<?= $_SESSION['wikidocs']['csfr']['token'] ?>">
<div class="row">
<div class="input-field col s12">
<input type="text" name="path" id="path" class="validate" value="<?= sanitizeInput(PATH_URI) ?>" required>
<label for="path"><span class="green-text">Path</span></label>
</div>
</div>
<div class="row">
<div class="input-field col s12 m5">
<input type="text" name="title" id="title" class="validate" value="<?= sanitizeInput(TITLE) ?>" required>
<label for="title"><span class="green-text">Title</span></label>
</div>
<div class="input-field col s12 m7">
<input type="text" name="subtitle" id="subtitle" class="validate" value="<?= sanitizeInput(SUBTITLE) ?>" required>
<label for="subtitle"><span class="green-text">Subtitle</span></label>
</div>
</div>
<div class="row">
<div class="input-field col s12 m5">
<input type="text" name="owner" id="owner" class="validate" placeholder="Contents owner" required>
<label for="owner"><span class="green-text">Owner</span></label>
</div>
<div class="input-field col s12 m7">
<input type="text" name="notice" id="notice" class="validate" placeholder="Contents copyright notice" required>
<label for="notice"><span class="green-text">Notice</span></label>
</div>
</div>
<div class="row">
<div class="input-field col s12 m5">
<input type="password" name="editcode" id="editcode" class="validate" placeholder="Choose a strong password for editing.." required>
<label for="editcode"><span class="green-text">Edit authentication code</span></label>
</div>
<div class="input-field col s12 m7">
<input type="password" name="editcode_repeat" id="editcode" class="validate" placeholder="Choose a strong password for editing.." required>
<label for="editcode"><span class="green-text">Repeat edit authentication code</span></label>
</div>
</div>
<div class="row">
<div class="input-field col s12 m12">
<button type="submit" class="btn btn-block waves-effect waves-light green right">Continue<i class="material-icons right">keyboard_arrow_right</i></button>
</div>
</div>
</form>
</div><!-- /col -->
<?php
}
if($g_act=="check"){
// define checks
$check_ok="<span class='secondary-content'><i class='material-icons green-text'>check_circle</i></span>";
$check_ko="<span class='secondary-content'><i class='material-icons red-text'>cancel</i></span>";
?>
<div class="col s12">
<h2>Checking configuration</h2>
<p>Your configuration has been verified..</p>
<ul class="collection">
<?php foreach ($checks_array as $key => $value): ?>
<li class="collection-item">
<?= ucfirst($key) ?>: <?= sanitizeInput(getSetting($key)) ?>
<span class="secondary-content">
<i class="material-icons <?= $value ? 'green-text' : 'red-text' ?>">
<?= $value ? 'check_circle' : 'cancel' ?>
</i>
</span>
</li>
<?php endforeach; ?>
</ul>
<div class="input-field col s12">
<?php if($errors){ ?>
<button onClick="window.history.back();" class="btn btn-block waves-effect waves-light green lighten-2">Edit configuration<i class="material-icons left">keyboard_arrow_left</i></button>
<?php }else{ ?>
<form action="setup.php?act=conclude" method="post">
<input type="hidden" name="token" value="<?= $_SESSION['wikidocs']['csfr']['token'] ?>">
<button type="submit" class="waves-effect waves-light btn green white-text right">Continue<i class="material-icons right">keyboard_arrow_right</i></button>
</form>
<?php } ?>
</div>
</div><!-- /col -->
<?php
}
if($g_act=="conclude"){
?>
<div class="col s12">
<h2>Saving configuration</h2>
<?php if($configured){ ?>
<p>Your configuration has been saved..</p>
<p><a href="<?= sanitizeInput(getSetting('path')) ?>">Continue</a> to your wiki!</p>
<i class="material-icons small green-text">check_circle</i>
<?php }else{ ?>
<p class="red-text">An error occurred while saving the configuration!</p>
<i class="material-icons small red-text">cancel</i>
<?php } ?>
<?php } ?>
</div><!-- /row-->
</div><!-- /container-->
<script src="helpers/jquery-3.7.0/js/jquery.min.js"></script>
<script src="helpers/materialize-1.0.0/js/materialize.min.js"></script>
</body>
</html>