Commit 190b605a authored by Qwerty's avatar Qwerty 🦄
Browse files

url rewrite

parent 7916d1cb
RewriteEngine On
RewriteRule ^\~([^/]*)$ ?do=$1 [L]
RewriteRule ^\@([^/]*)$ ?user=$1 [L]
......@@ -24,8 +24,8 @@ section, header {
margin-bottom:2em;
}
.avatar {
height:100px;
width:100px;
height:200px;
width:200px;
border-radius:100%;
margin-left: auto;
margin-right: auto;
......@@ -54,13 +54,18 @@ section, header {
include 'vendor/qwertygc/unicornsystem/sql.functions.php';
include 'vendor/qwertygc/unicornsystem/form.class.php';
include 'vendor/qwertygc/unicornsystem/misc.functions.php';
if(!file_exists('data.db')) {
$db = sqliteconnect('database.db');
if(!file_exists('data/database.db')) {
mkdir('data/');
mkdir('data/uploads/');
file_put_contents('data/index.html', '');
file_put_contents('data/uploads/index.html', '');
$db = sqliteconnect('data/database.db');
sql("CREATE TABLE IF NOT EXISTS `users` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`email` TEXT,
`password` TEXT,
`slug` TEXT,
`avatar` TEXT,
`bgcolor` TEXT,
`maincolor` TEXT,
`secondcolor` TEXT);");
......@@ -71,12 +76,12 @@ if(!file_exists('data.db')) {
`label`TEXT);");
}
else {
$db = sqliteconnect('database.db');
$db = sqliteconnect('data/database.db');
}
if(isset($_GET['do'])) {
switch ($_GET['do']) {
case 'subscribe':
$form = New Unicorn\form(array('method'=>'post', 'action'=>'?do=subscribe'));
$form = New Unicorn\form(array('method'=>'post', 'action'=>'~subscribe'));
echo '<div class="form-group">';
$form->label('email', 'Courriel');
$form->input(array('type'=>'email', 'id'=>'email', 'name'=>'email', 'class'=>'form-control'));
......@@ -95,7 +100,7 @@ if(isset($_GET['do'])) {
print_r($data);
if((int)$data['countslug'] == 0 AND (int)$data['countemail'] == 0) {
sql('INSERT INTO users(email, password, slug) VALUES (?,?,?)', array($_POST['email'], password_hash($_POST['password'], PASSWORD_DEFAULT), $_POST['slug']));
redirect('?do=login');
redirect('~login');
}
else {
echo '<p class="alert alert-warning" role="alert">Courriel ou identifiant déjà existant dans la base de données</p>';
......@@ -104,8 +109,8 @@ if(isset($_GET['do'])) {
break;
case 'login':
if(isset($_SESSION['login'])) {redirect('?do=edit');}
$form = New Unicorn\form(array('method'=>'post', 'action'=>'?do=login'));
if(isset($_SESSION['login'])) {redirect('~edit');}
$form = New Unicorn\form(array('method'=>'post', 'action'=>'~login'));
echo '<div class="form-group">';
$form->label('email', 'Courriel');
$form->input(array('type'=>'email', 'id'=>'email', 'name'=>'email', 'class'=>'form-control'));
......@@ -123,7 +128,7 @@ if(isset($_GET['do'])) {
echo 'Yolo';
$_SESSION['login'] = true;
$_SESSION['user_id'] = $data['id'];
redirect('?do=edit');
redirect('~edit');
}
else {
echo '<p class="alert alert-warning" role="alert">Mauvais mot de passe</p>';
......@@ -142,15 +147,15 @@ if(isset($_GET['do'])) {
exit();
break;
case 'edit':
if(!isset($_SESSION['login'])) {redirect('?do=login'); exit();}
if(!isset($_SESSION['login'])) {redirect('~login'); exit();}
$query = sql('SELECT * FROM users WHERE id=:id', array('id'=>$_SESSION['user_id']));
$data = $query->fetch();
echo '<h1>Bienvenue <a href="@'.$data['slug'].'">@'.$data['slug'].'</a></h1>';
echo '<a href="?do=logout">Déconnexion</a>';
echo '<a href="~logout">Déconnexion</a>';
echo '<h1>Gérer ses liens</h1>';
$form = New Unicorn\form(array('method'=>'post', 'action'=>'?do=edit'));
$form = New Unicorn\form(array('method'=>'post', 'action'=>'~edit'));
echo '<div class="form-group">';
$form->label('url', 'Lien');
$form->input(array('type'=>'url', 'id'=>'url', 'name'=>'url', 'class'=>'form-control'));
......@@ -162,22 +167,22 @@ if(isset($_GET['do'])) {
$form->endform();
if(isset($_POST['url'])) {
sql('INSERT INTO links(url, label, id_user) VALUES (?,?,?)', array($_POST['url'], $_POST['label'], $_SESSION['user_id']));
redirect('?do=edit');
redirect('~edit');
}
$query = sql('SELECT url, label, id FROM links WHERE id_user=?', array($_SESSION['user_id']));
echo '<ul>';
while($links = $query->fetch()) {
echo '<li><b>'.$links['label'].'</b> : <a href="'.$links['url'].'">'.$links['url'].'</a> - <a href="?do=edit&delink='.$links['id'].'" class="text-danger">Supprimer le lien</a></li>';
echo '<li><b>'.$links['label'].'</b> : <a href="'.$links['url'].'">'.$links['url'].'</a> - <a href="~edit&delink='.$links['id'].'" class="text-danger">Supprimer le lien</a></li>';
}
echo '</ul>';
if(isset($_GET['delink'])) {
sql('DELETE FROM links WHERE id=?', array($_GET['delink']));
redirect('?do=edit');
redirect('~edit');
}
echo '<h1>Mettre à jour ses informations</h1>';
$form = New Unicorn\form(array('method'=>'post', 'action'=>'?do=edit'));
$form = New Unicorn\form(array('method'=>'post', 'action'=>'~edit', 'enctype'=>'multipart/form-data'));
echo '<div class="form-group">';
$form->label('email', 'Courriel');
$form->input(array('type'=>'email', 'id'=>'email', 'name'=>'email', 'class'=>'form-control', 'value'=>$data['email']));
......@@ -185,6 +190,9 @@ if(isset($_GET['do'])) {
$form->label('password', 'Mot de passe (laisser vide pour garder le même)');
$form->input(array('type'=>'password', 'id'=>'password', 'name'=>'password', 'class'=>'form-control'));
echo '</div><div class="form-group">';
$form->label('avatar', 'Photo de profil');
$form->input(array('type'=>'file', 'id'=>'avatar', 'name'=>'avatar', 'class'=>'form-control-file'));
echo '</div><div class="form-group">';
$form->label('slug', 'Identifiant');
$form->input(array('type'=>'text', 'id'=>'slug', 'name'=>'slug', 'class'=>'form-control', 'disabled'=>'disabled', 'value'=>$data['slug']));
echo '</div>';
......@@ -192,12 +200,22 @@ if(isset($_GET['do'])) {
$form->endform();
if(isset($_POST['email'])) {
sql('UPDATE users SET email=? WHERE id=?', array($_POST['email'], $_SESSION['user_id']));
if(isset($_POST['password'])) {sql('UPDATE users SET password=? WHERE id=?', array(password_hash($_POST['password'], PASSWORD_DEFAULT), $_SESSION['user_id']));}
redirect('?do=edit');
if(isset($_FILES['avatar'])) {
if (in_array($file['type'], array ('image/gif', 'image/jpeg','image/png'))) {
$filename = 'data/uploads/'.md5(microtime()).'_'.basename($_FILES['avatar']['name']);
move_uploaded_file($_FILES['avatar']['tmp_name'], $filename);
sql('UPDATE users SET avatar=? WHERE id=?', array($filename, $_SESSION['user_id']));
}
else {
echo '<p class="alert alert-warning" role="alert">Une image (gif, jpg, png), pas autre chose !</p>';
}
}
//if(isset($_POST['password'])) {sql('UPDATE users SET password=? WHERE id=?', array(password_hash($_POST['password'], PASSWORD_DEFAULT), $_SESSION['user_id']));}
//redirect('~edit');
}
echo '<h1>Personnalisation</h1>';
$form = New Unicorn\form(array('method'=>'post', 'action'=>'?do=edit'));
$form = New Unicorn\form(array('method'=>'post', 'action'=>'~edit'));
echo '<div class="form-group">';
$form->label('bgcolor', 'Couleur de fond');
$form->input(array('type'=>'color', 'id'=>'bgcolor', 'name'=>'bgcolor', 'class'=>'form-control', 'value'=>$data['bgcolor']));
......@@ -213,16 +231,16 @@ if(isset($_GET['do'])) {
$form->endform();
if(isset($_POST['bgcolor'])) {
sql('UPDATE users SET bgcolor=?, maincolor=?, secondcolor=? WHERE id=?', array($_POST['bgcolor'], $_POST['maincolor'], $_POST['secondcolor'], $_SESSION['user_id']));
redirect('?do=edit');
redirect('~edit');
}
echo '<hr>';
echo '<a href="?do=edit&deleteaccount=true" class="text-danger">Supprimer mon compte</a>';
echo '<a href="~edit&deleteaccount=true" class="text-danger">Supprimer mon compte</a>';
if(isset($_GET['deleteaccount'])) {
sql('DELETE FROM links WHERE id_user=?', array($_SESSION['user_id']));
sql('DELETE FROM users WHERE id=?', array($_SESSION['user_id']));
redirect('?do=logout');
redirect('~logout');
}
break;
}
......@@ -232,7 +250,7 @@ elseif(isset($_GET['user'])) {
$query = sql('SELECT count(slug) as nb FROM users WHERE slug=:slug', array('slug'=>$_GET['user']));
$nb = $query->fetch();
if($nb['nb'] == 1) {
$query = sql('SELECT id, slug, email, bgcolor, maincolor, secondcolor FROM users WHERE slug=:slug', array('slug'=>$_GET['user']));
$query = sql('SELECT id, slug, email, avatar, bgcolor, maincolor, secondcolor FROM users WHERE slug=:slug', array('slug'=>$_GET['user']));
$data = $query->fetch();
echo '<style>
:root {
......@@ -241,7 +259,7 @@ elseif(isset($_GET['user'])) {
--secondcolor: '.$data['secondcolor'].';
}</style>';
echo '<header class="col text-center">
<img src="'.$data['avatar'].'" alt="" class="avatar"/>
<h1>@'.$data['slug'].'</h1>
</header>';
echo '<section>';
......@@ -269,7 +287,7 @@ else {
echo '<footer>';
if(!isset($_GET['user'])) {
echo '<p>';
if(!isset($_SESSION['login'])) {echo '<a href="?do=subscribe">Pas encore de compte ?<a> <a href="?do=login">Se connecter</a>'; } else {echo '<a href="?do=edit">Administration</a>';}
if(!isset($_SESSION['login'])) {echo '<a href="~subscribe">Pas encore de compte ?<a> <a href="~login">Se connecter</a>'; } else {echo '<a href="~edit">Administration</a>';}
echo '</p>';
}
echo '<p><a href="index.php">Accueil</a> <a href="https://framagit.org/qwertygc/bricolages/-/tree/master/Bildumak">Bildumak</a></p>';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment