-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
219 lines (184 loc) · 8.03 KB
/
install.php
File metadata and controls
219 lines (184 loc) · 8.03 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
<?php
/**
* DDb - Copyright 2013 Yosko (www.yosko.net)
*
* This file is part of DDb.
*
* DDb is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DDb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with DDb. If not, see <http://www.gnu.org/licenses/>.
*
*/
include_once "inc/functions.php";
$tpl_cache = 'cache/tpl/';
if ( !is_writable(dirname(__FILE__)) ) {
$tpl_cache = sys_get_temp_dir().'/DDb/';
}
$tpl = setRainTpl('tpl/', $tpl_cache);
$serverConfig['phpVersion'] = PHP_VERSION;
$serverConfig['phpMinVersion'] = '5.3.0';
$serverConfig['phpIsVersionValid'] = (version_compare(PHP_VERSION, $serverConfig['phpMinVersion']) >= 0);
$serverConfig['pdo'] = extension_loaded('pdo');
$serverConfig['pdoSqlite'] = extension_loaded('pdo_sqlite');
$serverConfig['rootDirectory'] = dirname($_SERVER['SCRIPT_FILENAME']);
$serverConfig['rootPermissions'] = is_writable($serverConfig['rootDirectory']);
$serverConfig['cacheTplDirectory'] = $serverConfig['rootDirectory'].'/cache/tpl/';
$serverConfig['cacheTplPermissions'] = is_writable($serverConfig['cacheTplDirectory']);
$serverConfig['cacheSessionDirectory'] = $serverConfig['rootDirectory'].'/cache/session/';
$serverConfig['cacheSessionPermissions'] = is_writable($serverConfig['cacheSessionDirectory']);
$serverOk = $serverConfig['phpIsVersionValid'] && $serverConfig['pdo']
&& $serverConfig['pdoSqlite'] && $serverConfig['rootPermissions']
&& $serverConfig['cacheTplPermissions'] && $serverConfig['cacheSessionPermissions'];
//STEP 3: install done
if(file_exists("database.sqlite")) {
$step = 3;
//STEP 2.5: do install (uses same view as step 2)
} elseif($serverOk && isset($_POST['submitInstall'])) {
$values = array();
$errors = array();
$values['login'] = htmlspecialchars(trim($_POST['login']));
$values['password'] = htmlspecialchars(trim($_POST['password']));
$values['firstDreamer'] = htmlspecialchars(trim($_POST['firstDreamer']));
$values['hash'] = YosLoginTools::hashPassword($values['password']);
$errors['login'] = (!isset($_POST['login']) || trim($_POST['login']) == "");
$errors['password'] = (!isset($_POST['password']) || trim($_POST['password']) == "");
$errors['firstDreamer'] = (!isset($_POST['firstDreamer']) || trim($_POST['firstDreamer']) == "");
$errors['hash'] = (strlen($values["hash"]) < 60);
if(!$errors["login"] && !$errors["password"] && !$errors["firstDreamer"] && !$errors["hash"]) {
//Create and open database
try {
$db = new PDO('sqlite:database.sqlite');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
}
//Create tables
$sql = <<<QUERY
DROP TABLE IF EXISTS ddb_dreamer;
DROP TABLE IF EXISTS ddb_tag;
DROP TABLE IF EXISTS ddb_dream;
DROP TABLE IF EXISTS ddb_dream_tag;
DROP TABLE IF EXISTS ddb_user;
DROP TABLE IF EXISTS ddb_user_dreamer;
DROP TABLE IF EXISTS ddb_settings;
CREATE TABLE IF NOT EXISTS ddb_dreamer (
'dreamerId' INTEGER NULL PRIMARY KEY AUTOINCREMENT,
'dreamerName' VARCHAR(256) NOT NULL
);
CREATE TABLE IF NOT EXISTS ddb_tag (
'tagId' INTEGER PRIMARY KEY AUTOINCREMENT,
'tagName' VARCHAR(256) NOT NULL,
'tagIcon' VARCHAR(256)
);
CREATE TABLE IF NOT EXISTS ddb_dream (
'dreamId' INTEGER NULL PRIMARY KEY AUTOINCREMENT,
'dreamerId_FK' INT NOT NULL,
'dreamDate' DATETIME,
'dreamTitle' VARCHAR(256),
'dreamCharacters' TEXT,
'dreamPlace' TEXT,
'dreamText' TEXT,
'dreamPointOfVue' TEXT,
'dreamFunFacts' TEXT,
'dreamFeelings' TEXT,
'userId_FK' INT NOT NULL,
'dreamCreation' DATETIME NOT NULL DEFAULT current_timestamp,
'dreamPublication' DATETIME NOT NULL DEFAULT current_timestamp,
'dreamStatus' INTEGER NOT NULL DEFAULT 1
);
CREATE TABLE IF NOT EXISTS ddb_dream_tag (
'dreamId_FK' INT NOT NULL,
'tagId_FK' INT NOT NULL
);
CREATE TABLE IF NOT EXISTS ddb_comment (
'commentId' INTEGER NULL PRIMARY KEY AUTOINCREMENT,
'dreamId_FK' INT NOT NULL,
'userId_FK' INT NOT NULL,
'commentText' TEXT,
'commentCreation' DATETIME NOT NULL DEFAULT current_timestamp,
'commentLastEdit' DATETIME NOT NULL DEFAULT current_timestamp
);
CREATE TABLE IF NOT EXISTS ddb_user (
'userId' INTEGER NULL PRIMARY KEY AUTOINCREMENT,
'userLogin' VARCHAR(256) NOT NULL,
'userPassword' VARCHAR(256) NOT NULL,
'userRole' VARCHAR(50) NOT NULL DEFAULT 'user'
);
CREATE TABLE IF NOT EXISTS ddb_user_dreamer (
'userId_FK' INT NOT NULL,
'dreamerId_FK' INT NOT NULL
);
CREATE TABLE IF NOT EXISTS ddb_settings (
'LTDir' TEXT NOT NULL DEFAULT 'cache/session/',
'nbLTSession' INT NOT NULL DEFAULT 200,
'LTDuration' INT NOT NULL DEFAULT 2592000,
'tplDir' TEXT NOT NULL DEFAULT 'tpl/',
'tplCache' TEXT NOT NULL DEFAULT 'cache/tpl/',
'timezone' VARCHAR(256) NOT NULL,
'dusk' INT NOT NULL DEFAULT 20,
'dawn' INT NOT NULL DEFAULT 7,
'useNightSkin' INT NOT NULL DEFAULT 0,
'useTagIcon' INT NOT NULL DEFAULT 1,
'appKey' VARCHAR(42) NOT NULL
);
INSERT INTO ddb_tag (tagName, tagIcon) VALUES ('adulte', 'notification-counter-18.png');
INSERT INTO ddb_tag (tagName, tagIcon) VALUES ('cauchemar', 'skull.png');
INSERT INTO ddb_tag (tagName, tagIcon) VALUES ('lucide', 'brain.png');
INSERT INTO ddb_tag (tagName, tagIcon) VALUES ('récurrent', 'arrow-circle-225.png');
QUERY;
try {
//install everything for version 1.4
$db->exec($sql);
//update for v1.5
createTableVersion(DDB_VERSION);
} catch(PDOException $e) {
echo $e->getMessage();
}
//insert first dreamer
$qry = $db->prepare(
'INSERT INTO ddb_dreamer (dreamerName) VALUES (:name)');
$qry->bindParam(':name', $values["firstDreamer"], PDO::PARAM_STR);
$qry->execute();
//insert user
$qry = $db->prepare(
'INSERT INTO ddb_user (userLogin, userPassword, userRole) VALUES (:login, :password, "admin")');
$qry->bindParam(':login', $values["login"], PDO::PARAM_STR);
$qry->bindParam(':password', $values["hash"], PDO::PARAM_STR);
$qry->execute();
//insert settings
$values['timezone'] = date_default_timezone_get();
$values['appKey'] = YosLoginTools::generateRandomString(42);
$qry = $db->prepare(
'INSERT INTO ddb_settings (timezone, appKey) VALUES (:timezone, :appKey)');
$qry->bindParam(':timezone', $values['timezone'], PDO::PARAM_STR);
$qry->bindParam(':appKey', $values['appKey'], PDO::PARAM_STR);
$qry->execute();
//install done: redirect to avoid second execution
header("Location: $_SERVER[PHP_SELF]");
} else {
//go back to step 2 with entered values
$tpl->assign( "errors", $errors );
$tpl->assign( "values", $values );
}
}
//STEP 2: ask settings
if(!file_exists("database.sqlite") && isset($_GET['step']) && intval($_GET['step']) == 2) {
$step = 2;
//STEP 1: check server configuration
} elseif(!file_exists("database.sqlite") && ((!isset($_GET['step']) || intval($_GET['step']) == 1))) {
$step = 1;
$tpl->assign( "serverConfig", $serverConfig );
}
$tpl->assign( "noLogout", true ); //no DDb command button
$tpl->assign( "step", $step );
$tpl->draw( "install" );
?>