changes for 1026 for PDO exceptions

This commit is contained in:
Thomas Citharel 2015-01-27 00:41:25 +01:00
parent 369e00e60b
commit ca056e7fd1
2 changed files with 130 additions and 140 deletions

View file

@ -13,19 +13,6 @@ $successes = array();
require_once('wallabag_compatibility_test.php'); require_once('wallabag_compatibility_test.php');
/* Function taken from at http://php.net/manual/en/function.rmdir.php#110489
* Idea : nbari at dalmp dot com
* Rights unknown
* Here in case of .gitignore files
*/
function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
if (isset($_GET['clean'])) { if (isset($_GET['clean'])) {
if (is_dir('install')){ if (is_dir('install')){
delTree('install'); delTree('install');
@ -60,27 +47,10 @@ if (isset($_POST['download'])) {
else if (isset($_POST['install'])) { else if (isset($_POST['install'])) {
if (!is_dir('vendor')) { if (!is_dir('vendor')) {
$errors[] = 'You must install twig before.'; $errors[] = 'You must install twig before.';
} } else {
else {
$continue = true; $continue = true;
// Create config.inc.php
if (!copy('inc/poche/config.inc.default.php', 'inc/poche/config.inc.php')) {
$errors[] = 'Installation aborted, impossible to create inc/poche/config.inc.php file. Maybe you don\'t have write access to create it.';
$continue = false;
}
else {
function generate_salt() {
mt_srand(microtime(true)*100000 + memory_get_usage(true));
return md5(uniqid(mt_rand(), true));
}
$content = file_get_contents('inc/poche/config.inc.php');
$salt = generate_salt(); $salt = generate_salt();
$content = str_replace("define ('SALT', '');", "define ('SALT', '".$salt."');", $content); $content = file_get_contents('inc/poche/config.inc.default.php');
file_put_contents('inc/poche/config.inc.php', $content);
}
if ($continue) {
// User informations // User informations
$username = trim($_POST['username']); $username = trim($_POST['username']);
@ -89,19 +59,20 @@ else if (isset($_POST['install'])) {
// Database informations // Database informations
$moreQueries = array(); $moreQueries = array();
if ($_POST['db_engine'] == 'sqlite') { if ($_POST['db_engine'] == 'sqlite') {
if (!copy('install/poche.sqlite', 'db/poche.sqlite')) { if (!copy('install/poche.sqlite', 'db/poche.sqlite')) {
$errors[] = 'Impossible to create inc/poche/config.inc.php file.'; $errors[] = 'Impossible to create the SQLite database file.';
$continue = false;
} }
else { else {
$db_path = 'sqlite:' . realpath('') . '/db/poche.sqlite'; $db_path = 'sqlite:' . realpath('') . '/db/poche.sqlite';
$handle = new PDO($db_path); $handle = new PDO($db_path);
$handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql_structure = ""; $sql_structure = "";
} }
} } else {
else { // MySQL and Postgre
$content = file_get_contents('inc/poche/config.inc.php'); try {
if ($_POST['db_engine'] == 'mysql') { if ($_POST['db_engine'] == 'mysql') {
$db_path = 'mysql:host=' . $_POST['mysql_server'] . ';dbname=' . $_POST['mysql_database'] . ';charset=utf8mb4'; $db_path = 'mysql:host=' . $_POST['mysql_server'] . ';dbname=' . $_POST['mysql_database'] . ';charset=utf8mb4';
@ -139,34 +110,15 @@ else if (isset($_POST['install'])) {
$sql_structure = file_get_contents('install/postgres.sql'); $sql_structure = file_get_contents('install/postgres.sql');
} }
$content = str_replace("define ('STORAGE', 'sqlite');", "define ('STORAGE', '".$_POST['db_engine']."');", $content);
file_put_contents('inc/poche/config.inc.php', $content);
}
if ($continue) {
function executeQuery($handle, $sql, $params) {
try
{
$query = $handle->prepare($sql);
$query->execute($params);
return $query->fetchAll();
}
catch (Exception $e)
{
return FALSE;
}
}
if ($_POST['db_engine'] != "sqlite") {
// create database structure // create database structure
$query = $handle->exec($sql_structure); $query = $handle->exec($sql_structure);
} catch (PDOException $e) {
$errors[] = $e->getMessage();
$continue = false;
} }
}
// Create user }
$handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if ($continue) {
$sql = "INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, '')"; $sql = "INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, '')";
$params = array($username, $salted_password, $username); $params = array($username, $salted_password, $username);
$query = executeQuery($handle, $sql, $params); $query = executeQuery($handle, $sql, $params);
@ -184,9 +136,17 @@ else if (isset($_POST['install'])) {
foreach ($moreQueries as $query) { foreach ($moreQueries as $query) {
executeQuery($handle, $query, array()); executeQuery($handle, $query, array());
} }
$successes[] = 'wallabag is now installed. You can now <a href="index.php?clean=0">access it !</a>'; $successes[] = 'wallabag is now installed. You can now <a href="index.php?clean=0">access it !</a>';
if (!copy('inc/poche/config.inc.default.php', 'inc/poche/config.inc.php')) {
$errors[] = 'Installation aborted, impossible to create inc/poche/config.inc.php file. Maybe you don\'t have write access to create it.';
} else {
if ($_POST['db_engine'] != 'sqlite') {
$content = str_replace("define ('STORAGE', 'sqlite');", "define ('STORAGE', '".$_POST['db_engine']."');", $content);
file_put_contents('inc/poche/config.inc.php', $content);
} }
$content = str_replace("define ('SALT', '');", "define ('SALT', '".$salt."');", $content);
file_put_contents('inc/poche/config.inc.php', $content);
} }
} }
} }
@ -368,21 +328,6 @@ cursor: pointer;
<p>To install wallabag, you just have to fill the following fields. That's all.</p> <p>To install wallabag, you just have to fill the following fields. That's all.</p>
<p>If you need help, you can read the doc: <a href="docs/" target="_blank">offline documentation</a> and <a href="http://doc.wallabag.org" target="_blank">online one</a> (already up-to-date).</p> <p>If you need help, you can read the doc: <a href="docs/" target="_blank">offline documentation</a> and <a href="http://doc.wallabag.org" target="_blank">online one</a> (already up-to-date).</p>
<?php if (!is_dir('vendor')) : ?>
<div class='messages notice install'>wallabag needs twig, a template engine (<a href="http://twig.sensiolabs.org/">?</a>). Two ways to install it:<br />
<ul>
<li>automatically download and extract vendor.zip into your wallabag folder.
<p><input type="submit" name="download" value="Download vendor.zip" /></p>
<?php if (!extension_loaded('zip')) : ?>
<b>Be careful, zip extension is not enabled in your PHP configuration. You'll have to unzip vendor.zip manually.</b>
<?php endif; ?>
<em>This method is mainly recommended if you don't have a dedicated server.</em></li>
<li>use <a href="http://getcomposer.org/">Composer</a> :<pre><code>curl -s http://getcomposer.org/installer | php
php composer.phar install</code></pre></li>
</ul>
</div>
<?php endif; ?>
<p class="detail">Server compatibility test (click to view details) : <?php if (isOkay()) { ?> <p class="detail">Server compatibility test (click to view details) : <?php if (isOkay()) { ?>
<span class="good">All good</span> <span class="good">All good</span>
<?php } elseif (isPassing()) { ?> <?php } elseif (isPassing()) { ?>
@ -532,9 +477,22 @@ php composer.phar install</code></pre></li>
</div> </div>
</div> </div>
<form method="post" class="technical">
<hr> <hr>
<form method="post" class="technical">
<?php if (!is_dir('vendor')) : ?>
<div class='messages notice install'>wallabag needs twig, a template engine (<a href="http://twig.sensiolabs.org/">?</a>). Two ways to install it:<br />
<ul>
<li>automatically download and extract vendor.zip into your wallabag folder.
<p><input type="submit" name="download" value="Download vendor.zip" /></p>
<?php if (!extension_loaded('zip')) : ?>
<b>Be careful, zip extension is not enabled in your PHP configuration. You'll have to unzip vendor.zip manually.</b>
<?php endif; ?>
<em>This method is mainly recommended if you don't have a dedicated server.</em></li>
<li>use <a href="http://getcomposer.org/">Composer</a> :<pre><code>curl -s http://getcomposer.org/installer | php
php composer.phar install</code></pre></li>
</ul>
</div>
<?php endif; ?>
<fieldset> <fieldset>
<legend><strong>Technical settings</strong></legend> <legend><strong>Technical settings</strong></legend>
<p> <p>

View file

@ -52,4 +52,36 @@ function isPassing() {
return !in_array(false, $status); return !in_array(false, $status);
} }
/* Function taken from at http://php.net/manual/en/function.rmdir.php#110489
* Idea : nbari at dalmp dot com
* Rights unknown
* Here in case of .gitignore files
*/
function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
function generate_salt() {
mt_srand(microtime(true)*100000 + memory_get_usage(true));
return md5(uniqid(mt_rand(), true));
}
function executeQuery($handle, $sql, $params) {
try
{
$query = $handle->prepare($sql);
$query->execute($params);
return $query->fetchAll();
}
catch (Exception $e)
{
return FALSE;
}
}
?> ?>