update to use username
8 files changed, 41 insertions(+), 16 deletions(-)

M classes/User.php
M home.php
M includes/app.php
M includes/site_header.php
M index.php
M page.php
M page_update.php
M pages_index.php
M classes/User.php +6 -0
@@ 14,6 14,12 @@ class User
         return $this->db->runSQL($sql, [$id])->fetch();
     }
 
+    public function getFromUsername($username)
+    {
+        $sql = "SELECT * FROM users WHERE name=?;";
+        return $this->db->runSQL($sql, [$username])->fetch();
+    }
+
     public function getIdFromEmail($email)
     {
         $sql = "SELECT id FROM users WHERE email=?";

          
M home.php +2 -3
@@ 1,9 1,8 @@ 
 <?php
 require 'includes/app.php';
 
-$id = get_id();
-$site_user = $GLOBALS['app']->getUser()->get($id) or page_not_found();
-$content = content_to_html($site_user['home'], $site_user['id']);
+$site_user = $app->getUser()->getFromUsername(get_user()) or page_not_found();
+$content = content_to_html2($site_user['home'], $site_user);
 ?>
 
 <?php include 'includes/site_header.php'; ?>

          
M includes/app.php +23 -2
@@ 85,6 85,15 @@ function get_slug()
     return $slug;
 }
 
+function get_user()
+{
+    if (!($slug = filter_input(INPUT_GET, 'u', FILTER_SANITIZE_SPECIAL_CHARS))) {
+        page_not_found();
+    }
+
+    return $slug;
+}
+
 function get_page()
 {
     if (!($page = filter_input(INPUT_GET, 'p', FILTER_VALIDATE_INT))) {

          
@@ 122,9 131,14 @@ function get_link($user_id, $slug)
     return "<a href=\"page.php?id=$user_id&slug=$slug\">$slug</a>";
 }
 
+function get_link2($username, $slug)
+{
+    return "<a href=\"page.php?u=$username&slug=$slug\">$slug</a>";
+}
+
 function home_link($user_id, $name)
 {
-    return "<a href=\"home.php?id=$user_id\">$name</a>";
+    return "<a href=\"home.php?u=$name\">$name</a>";
 }
 
 function edit_link($slug)

          
@@ 201,7 215,14 @@ function content_to_html($content, $user
 {
     return preg_replace_callback(LINK_REGEXP, function ($match) use ($user_id) {
         $slug = $match[1];
-        return "<a href=\"page.php?id=$user_id&slug=$slug\">$slug</a>";
+        return get_link($user_id, $slug);
+    }, gmi($content));
+}
+function content_to_html2($content, $user)
+{
+    return preg_replace_callback(LINK_REGEXP, function ($match) use ($user) {
+        $slug = $match[1];
+        return get_link2($user['name'], $slug);
     }, gmi($content));
 }
 

          
M includes/site_header.php +1 -1
@@ 14,7 14,7 @@ 
 
 <header>
     <nav>
-        <a href="home.php?id=<?=$site_user['id']?>"><?=$site_user['name']?></a>
+        <?=home_link(0, $site_user['name'])?>
         <a href="pages_index.php?id=<?=$site_user['id']?>">Pages</a>
         <a href="changelog.php?id=<?=$site_user['id']?>">Changelog</a>
     </nav>

          
M index.php +1 -1
@@ 2,7 2,7 @@ 
 require 'includes/app.php';
 
 if (is_member($user)) {
-    redirect("home.php?id=$user[id]");
+    redirect("home.php?u=$user[name]");
 }
 ?>
 

          
M page.php +5 -6
@@ 1,18 1,17 @@ 
 <?php
 require 'includes/app.php';
 
-$id = get_id();
 $slug = get_slug();
-$site_user = $app->getUser()->get($id) or page_not_found();
-$page = $GLOBALS['app']->getPage()->get($id, $slug);
-$related = $GLOBALS['app']->getPage()->related($id, $slug);
+$site_user = $app->getUser()->getFromUsername(get_user()) or page_not_found();
+$page = $GLOBALS['app']->getPage()->get($site_user['id'], $slug);
+$related = $GLOBALS['app']->getPage()->related($site_user['id'], $slug);
 ?>
 
 <?php include 'includes/site_header.php'; ?>
 
 <main>
     <?php if ($page): ?>
-        <?= content_to_html($page['content'], $page['user_id']) ?>
+        <?= content_to_html2($page['content'], $site_user) ?>
     <?php else: ?>
         <h1>Not found</h1>
         <?php if (is_site_admin($site_user)): ?>

          
@@ 34,7 33,7 @@ require 'includes/app.php';
         <nav>
             <span>Related:</span>
             <?php foreach ($related as $r): ?>
-                <?= get_link($id, $r['slug']) ?>
+                <?= get_link2($site_user['name'], $r['slug']) ?>
             <?php endforeach; ?>
         </nav>
     </section>

          
M page_update.php +2 -2
@@ 5,7 5,7 @@ is_member($GLOBALS['user']) or redirect(
 $slug = get_slug();
 $errors = [];
 $site_user = $GLOBALS['user'];
-$form = $GLOBALS['app']->getPage()->get($GLOBALS['user']['id'], $slug) or page_not_found();
+$form = $app->getPage()->get($GLOBALS['user']['id'], $slug) or page_not_found();
 
 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     $form['content'] = $_POST['content'];

          
@@ 20,7 20,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST
 <?php include 'includes/site_header.php'; ?>
 
 <main>
-    <h1>Editing <?= get_link($form['user_id'], $form['slug']) ?></h1>
+    <h1>Editing <?= get_link2($site_user['name'], $form['slug']) ?></h1>
 
     <?php form_errors($errors) ?>
 

          
M pages_index.php +1 -1
@@ 12,7 12,7 @@ require 'includes/app.php';
 
     <ul>
         <?php foreach ($pages as $page): ?>
-            <li><?= get_link($id, $page['slug']) ?></li>
+            <li><?= get_link2($site_user['name'], $page['slug']) ?></li>
         <?php endforeach; ?>
     </ul>