]> git.immae.eu Git - github/wallabag/wallabag.git/blob - install/index.php
Convert the MySQL charset to utf8mb4 to support the full range of unicode characters
[github/wallabag/wallabag.git] / install / index.php
1 <?php
2 /**
3 * wallabag, self hostable application allowing you to not miss any content anymore
4 *
5 * @category wallabag
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
7 * @copyright 2013
8 * @license http://opensource.org/licenses/MIT see COPYING file
9 */
10
11 $errors = array();
12 $successes = array();
13
14 /* Function taken from at http://php.net/manual/en/function.rmdir.php#110489
15 * Idea : nbari at dalmp dot com
16 * Rights unknown
17 * Here in case of .gitignore files
18 */
19 function delTree($dir) {
20 $files = array_diff(scandir($dir), array('.','..'));
21 foreach ($files as $file) {
22 (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
23 }
24 return rmdir($dir);
25 }
26
27 if (isset($_GET['clean'])) {
28 if (is_dir('install')){
29 delTree('install');
30 header('Location: index.php');
31 }
32 }
33
34 if (isset($_POST['download'])) {
35 if (!file_put_contents("cache/vendor.zip", fopen("http://static.wallabag.org/files/vendor.zip", 'r'))) {
36 $errors[] = 'Impossible to download vendor.zip. Please <a href="http://wllbg.org/vendor">download it manually</a> and unzip it in your wallabag folder.';
37 }
38 else {
39 if (extension_loaded('zip')) {
40 $zip = new ZipArchive();
41 if ($zip->open("cache/vendor.zip") !== TRUE){
42 $errors[] = 'Impossible to open cache/vendor.zip. Please unzip it manually in your wallabag folder.';
43 }
44 if ($zip->extractTo(realpath(''))) {
45 @unlink("cache/vendor.zip");
46 $successes[] = 'twig is now installed, you can install wallabag.';
47 }
48 else {
49 $errors[] = 'Impossible to extract cache/vendor.zip. Please unzip it manually in your wallabag folder.';
50 }
51 $zip->close();
52 }
53 else {
54 $errors[] = 'zip extension is not enabled in your PHP configuration. Please unzip cache/vendor.zip in your wallabag folder.';
55 }
56 }
57 }
58 else if (isset($_POST['install'])) {
59 if (!is_dir('vendor')) {
60 $errors[] = 'You must install twig before.';
61 }
62 else {
63 $continue = true;
64 // Create config.inc.php
65 if (!copy('inc/poche/config.inc.default.php', 'inc/poche/config.inc.php')) {
66 $errors[] = 'Installation aborted, impossible to create inc/poche/config.inc.php file. Maybe you don\'t have write access to create it.';
67 $continue = false;
68 }
69 else {
70 function generate_salt() {
71 mt_srand(microtime(true)*100000 + memory_get_usage(true));
72 return md5(uniqid(mt_rand(), true));
73 }
74
75 $content = file_get_contents('inc/poche/config.inc.php');
76 $salt = generate_salt();
77 $content = str_replace("define ('SALT', '');", "define ('SALT', '".$salt."');", $content);
78 file_put_contents('inc/poche/config.inc.php', $content);
79 }
80
81 if ($continue) {
82
83 // User informations
84 $username = trim($_POST['username']);
85 $password = trim($_POST['password']);
86 $salted_password = sha1($password . $username . $salt);
87
88 // Database informations
89 if ($_POST['db_engine'] == 'sqlite') {
90 if (!copy('install/poche.sqlite', 'db/poche.sqlite')) {
91 $errors[] = 'Impossible to create inc/poche/config.inc.php file.';
92 $continue = false;
93 }
94 else {
95 $db_path = 'sqlite:' . realpath('') . '/db/poche.sqlite';
96 $handle = new PDO($db_path);
97 $sql_structure = "";
98 }
99 }
100 else {
101 $content = file_get_contents('inc/poche/config.inc.php');
102
103 if ($_POST['db_engine'] == 'mysql') {
104 $db_path = 'mysql:host=' . $_POST['mysql_server'] . ';dbname=' . $_POST['mysql_database'] . ';charset=utf8mb4';
105 $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['mysql_server']."');", $content);
106 $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['mysql_database']."');", $content);
107 $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['mysql_user']."');", $content);
108 $content = str_replace("define ('STORAGE_PASSWORD', 'poche');", "define ('STORAGE_PASSWORD', '".$_POST['mysql_password']."');", $content);
109 $handle = new PDO($db_path, $_POST['mysql_user'], $_POST['mysql_password'], array(
110 PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
111 ));
112
113 $sql_structure = file_get_contents('install/mysql.sql');
114 }
115 else if ($_POST['db_engine'] == 'postgres') {
116 $db_path = 'pgsql:host=' . $_POST['pg_server'] . ';dbname=' . $_POST['pg_database'];
117 $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['pg_server']."');", $content);
118 $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['pg_database']."');", $content);
119 $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['pg_user']."');", $content);
120 $content = str_replace("define ('STORAGE_PASSWORD', 'poche');", "define ('STORAGE_PASSWORD', '".$_POST['pg_password']."');", $content);
121 $handle = new PDO($db_path, $_POST['pg_user'], $_POST['pg_password']);
122
123 $sql_structure = file_get_contents('install/postgres.sql');
124 }
125
126 $content = str_replace("define ('STORAGE', 'sqlite');", "define ('STORAGE', '".$_POST['db_engine']."');", $content);
127 file_put_contents('inc/poche/config.inc.php', $content);
128 }
129
130 if ($continue) {
131
132 function executeQuery($handle, $sql, $params) {
133 try
134 {
135 $query = $handle->prepare($sql);
136 $query->execute($params);
137 return $query->fetchAll();
138 }
139 catch (Exception $e)
140 {
141 return FALSE;
142 }
143 }
144
145 // create database structure
146 $query = executeQuery($handle, $sql_structure, array());
147
148 // Create user
149 $handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
150
151 $sql = 'INSERT INTO users (username, password, name) VALUES (?, ?, ?)';
152 $params = array($username, $salted_password, $username);
153 $query = executeQuery($handle, $sql, $params);
154
155 $id_user = $handle->lastInsertId();
156
157 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
158 $params = array($id_user, 'pager', '10');
159 $query = executeQuery($handle, $sql, $params);
160
161 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
162 $params = array($id_user, 'language', 'en_EN.UTF8');
163 $query = executeQuery($handle, $sql, $params);
164
165 $successes[] = 'wallabag is now installed. You can now <a href="index.php?clean=0">access it !</a>';
166 }
167 }
168 }
169 }
170 ?>
171 <!DOCTYPE html>
172 <html>
173 <head>
174 <meta name="viewport" content="initial-scale=1.0">
175 <meta charset="utf-8">
176 <!--[if IE]>
177 <meta http-equiv="X-UA-Compatible" content="IE=10">
178 <![endif]-->
179 <title>wallabag - installation</title>
180 <link rel="shortcut icon" type="image/x-icon" href="themes/baggy/img/favicon.ico" />
181 <link rel="apple-touch-icon-precomposed" sizes="144x144" href="themes/baggy/img/apple-touch-icon-144x144-precomposed.png">
182 <link rel="apple-touch-icon-precomposed" sizes="72x72" href="themes/baggy/img/apple-touch-icon-72x72-precomposed.png">
183 <link rel="apple-touch-icon-precomposed" href="themes/baggy/img/apple-touch-icon-precomposed.png">
184 <link href='//fonts.googleapis.com/css?family=PT+Sans:700' rel='stylesheet' type='text/css'>
185 <link rel="stylesheet" href="themes/baggy/css/ratatouille.css" media="all">
186 <link rel="stylesheet" href="themes/baggy/css/font.css" media="all">
187 <link rel="stylesheet" href="themes/baggy/css/main.css" media="all">
188 <link rel="stylesheet" href="themes/baggy/css/messages.css" media="all">
189 <link rel="stylesheet" href="themes/baggy/css/print.css" media="print">
190 <script src="themes/default/js/jquery-2.0.3.min.js"></script>
191 <script src="themes/baggy/js/init.js"></script>
192 </head>
193 <body>
194 <header class="w600p center mbm">
195 <h1 class="logo">
196 <img width="100" height="100" src="themes/baggy/img/logo-w.png" alt="logo poche" />
197 </h1>
198 </header>
199 <div id="main">
200 <button id="menu" class="icon icon-menu desktopHide"><span>Menu</span></button>
201 <ul id="links" class="links">
202 <li><a href="http://www.wallabag.org/frequently-asked-questions/">FAQ</a></li>
203 <li><a href="http://doc.wallabag.org/">doc</a></li>
204 <li><a href="http://www.wallabag.org/help/">help</a></li>
205 <li><a href="http://www.wallabag.org/">wallabag.org</a></li>
206 </ul>
207 <?php if (!empty($errors)) : ?>
208 <div class='messages error install'>
209 <p>Errors during installation:</p>
210 <p>
211 <ul>
212 <?php foreach($errors as $error) :?>
213 <li><?php echo $error; ?></li>
214 <?php endforeach; ?>
215 </ul>
216 </p>
217 <p><a href="index.php">Please reload</a> this page when you think you resolved these problems.</p>
218 </div>
219 <?php endif; ?>
220 <?php if (!empty($successes)) : ?>
221 <div class='messages success install'>
222 <p>
223 <ul>
224 <?php foreach($successes as $success) :?>
225 <li><?php echo $success; ?></li>
226 <?php endforeach; ?>
227 </ul>
228 </p>
229 </div>
230 <?php else : ?>
231 <?php if (file_exists('inc/poche/config.inc.php') && is_dir('vendor')) : ?>
232 <div class='messages success install'>
233 <p>
234 wallabag seems already installed. If you want to update it, you only have to delete install folder, then <a href="index.php">reload this page</a>.
235 </p>
236 </div>
237 <?php endif; ?>
238 <?php endif; ?>
239 <p>To install wallabag, you just have to fill the following fields. That's all.</p>
240 <p>Don't forget to check your server compatibility <a href="wallabag_compatibility_test.php?from=install">here</a>.</p>
241 <form method="post">
242 <fieldset>
243 <legend><strong>Technical settings</strong></legend>
244 <?php if (!is_dir('vendor')) : ?>
245 <div class='messages notice install'>wallabag needs twig, a template engine (<a href="http://twig.sensiolabs.org/">?</a>). Two ways to install it:<br />
246 <ul>
247 <li>automatically download and extract vendor.zip into your wallabag folder.
248 <p><input type="submit" name="download" value="Download vendor.zip" /></p>
249 <?php if (!extension_loaded('zip')) : ?>
250 <b>Be careful, zip extension is not enabled in your PHP configuration. You'll have to unzip vendor.zip manually.</b>
251 <?php endif; ?>
252 <em>This method is mainly recommended if you don't have a dedicated server.</em></li>
253 <li>use <a href="http://getcomposer.org/">Composer</a> :<pre><code>curl -s http://getcomposer.org/installer | php
254 php composer.phar install</code></pre></li>
255 </ul>
256 </div>
257 <?php endif; ?>
258 <p>
259 Database engine:
260 <ul>
261 <li><label for="sqlite">SQLite</label> <input name="db_engine" type="radio" checked="" id="sqlite" value="sqlite" />
262 <div id="pdo_sqlite" class='messages error install'>
263 <p>You have to enable <a href="http://php.net/manual/ref.pdo-sqlite.php">pdo_sqlite extension</a>.</p>
264 </div>
265 </li>
266 <li>
267 <label for="mysql">MySQL</label> <input name="db_engine" type="radio" id="mysql" value="mysql" />
268 <ul id="mysql_infos">
269 <li><label for="mysql_server">Server</label> <input type="text" placeholder="localhost" id="mysql_server" name="mysql_server" /></li>
270 <li><label for="mysql_database">Database</label> <input type="text" placeholder="wallabag" id="mysql_database" name="mysql_database" /></li>
271 <li><label for="mysql_user">User</label> <input type="text" placeholder="user" id="mysql_user" name="mysql_user" /></li>
272 <li><label for="mysql_password">Password</label> <input type="text" placeholder="p4ssw0rd" id="mysql_password" name="mysql_password" /></li>
273 </ul>
274 </li>
275 <li>
276 <label for="postgres">PostgreSQL</label> <input name="db_engine" type="radio" id="postgres" value="postgres" />
277 <ul id="pg_infos">
278 <li><label for="pg_server">Server</label> <input type="text" placeholder="localhost" id="pg_server" name="pg_server" /></li>
279 <li><label for="pg_database">Database</label> <input type="text" placeholder="wallabag" id="pg_database" name="pg_database" /></li>
280 <li><label for="pg_user">User</label> <input type="text" placeholder="user" id="pg_user" name="pg_user" /></li>
281 <li><label for="pg_password">Password</label> <input type="text" placeholder="p4ssw0rd" id="pg_password" name="pg_password" /></li>
282 </ul>
283 </li>
284 </ul>
285 </p>
286 </fieldset>
287
288 <fieldset>
289 <legend><strong>User settings</strong></legend>
290 <p>
291 <label for="username">Username</label>
292 <input type="text" required id="username" name="username" value="wallabag" />
293 </p>
294 <p>
295 <label for="password">Password</label>
296 <input type="password" required id="password" name="password" value="wallabag" />
297 </p>
298 <p>
299 <label for="show">Show password:</label> <input name="show" id="show" type="checkbox" onchange="document.getElementById('password').type = this.checked ? 'text' : 'password'">
300 </p>
301 </fieldset>
302
303 <input type="submit" id="install_button" value="Install wallabag" name="install" />
304 </form>
305 </div>
306 <script>
307 $("#mysql_infos").hide();
308 $("#pg_infos").hide();
309
310 <?php
311 if (!extension_loaded('pdo_sqlite')) : ?>
312 $("#install_button").hide();
313 <?php
314 else :
315 ?>
316 $("#pdo_sqlite").hide();
317 <?php
318 endif;
319 ?>
320
321 $("input[name=db_engine]").click(function()
322 {
323 if ( $("#mysql").prop('checked')) {
324 $("#mysql_infos").show();
325 $("#pg_infos").hide();
326 $("#pdo_sqlite").hide();
327 $("#install_button").show();
328 }
329 else {
330 if ( $("#postgres").prop('checked')) {
331 $("#mysql_infos").hide();
332 $("#pg_infos").show();
333 $("#pdo_sqlite").hide();
334 $("#install_button").show();
335 }
336 else {
337 $("#mysql_infos").hide();
338 $("#pg_infos").hide();
339 <?php
340 if (!extension_loaded('pdo_sqlite')) : ?>
341 $("#pdo_sqlite").show();
342 $("#install_button").hide();
343 <?php
344 endif;
345 ?>
346 }
347 }
348 });
349 </script>
350 </body>
351 </html>