# HG changeset patch # User m15o # Date 1684865505 -7200 # Tue May 23 20:11:45 2023 +0200 # Node ID 234b61788adc139a7b6f052ecb60e68583a499ae # Parent b12f3da3f7763f5274ac61608036bed23f08e5bc refactor helpers diff --git a/activate.php b/activate.php --- a/activate.php +++ b/activate.php @@ -1,27 +1,27 @@ getUser()->get($id); + $selected = $GLOBALS['App']->getUser()->get($id); if (!$selected) { $errors[] = "Can't find user"; } else { - if ($GLOBALS['app']->getUser()->setRole($id, 2)) { + if ($GLOBALS['App']->getUser()->setRole($id, 2)) { $subject = "piclog - Welcome!"; $message = "Hello $selected[name]! Your account has been activated. You can now post messages on " . URL . ".\n\nTalk to you soon!"; - $GLOBALS['app']->getEmail()->send($selected['email'], $subject, $message); + $GLOBALS['App']->getEmail()->send($selected['email'], $subject, $message); } } } -$users = $GLOBALS['app']->getUser()->getAllInactive(); +$users = $GLOBALS['App']->getUser()->getAllInactive(); ?> diff --git a/activity.php b/activity.php --- a/activity.php +++ b/activity.php @@ -1,6 +1,6 @@ getPage()->getActivity(); +$pages = $App->getPage()->getActivity(); ?> @@ -11,7 +11,7 @@ diff --git a/admin.php b/admin.php --- a/admin.php +++ b/admin.php @@ -1,7 +1,7 @@ diff --git a/changelog.php b/changelog.php --- a/changelog.php +++ b/changelog.php @@ -1,8 +1,9 @@ getPage()->getChangelog($id); -$site_user = $GLOBALS['app']->getUser()->get($id) or page_not_found(); + +$u = get_param("u"); +$pages = $App->getPage()->getChangelog($u); +$site_user = $App->getUser()->getFromUsername($u) or page_not_found(); ?> @@ -12,7 +13,7 @@ diff --git a/classes/Page.php b/classes/Page.php --- a/classes/Page.php +++ b/classes/Page.php @@ -29,14 +29,15 @@ LIMIT 100"; return $this->db->runSQL($sql)->fetchAll(); } - public function getChangelog($id) + public function getChangelog($name) { $sql = "SELECT - slug, content, updated_at + slug, content, updated_at, name FROM pages - WHERE user_id=? + LEFT JOIN users on pages.user_id = users.id + WHERE name=? ORDER BY updated_at desc"; - return $this->db->runSQL($sql, [$id])->fetchAll(); + return $this->db->runSQL($sql, [$name])->fetchAll(); } public function get($id, $slug) diff --git a/delete.php b/delete.php deleted file mode 100644 --- a/delete.php +++ /dev/null @@ -1,36 +0,0 @@ -getImage()->get(get_id()) or page_not_found(); - -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - if ($form['user_id'] === $user['id'] || is_admin($user)) { - if ($App->getImage()->delete($form['id'], $form['user_id'])) { - $App->getSession()->setFlash("Image deleted"); - redirect("index.php"); - } else { - $errors[]="Can't delete image"; - } - } else { - page_not_found(); - } -} -?> - - - -

Delete image

- - - -

Are you sure you want to delete ""?

- -
- -

-
- - diff --git a/home.php b/home.php deleted file mode 100644 --- a/home.php +++ /dev/null @@ -1,25 +0,0 @@ -getUser()->getFromUsername(get_user()) or page_not_found(); -$content = content_to_html2($site_user['home'], $site_user); -?> - - - -
- - - -

's site

-

Welcome to your site! Use the edit button to edit this page.

- -
- - - - - - \ No newline at end of file diff --git a/home_update.php b/home_update.php --- a/home_update.php +++ b/home_update.php @@ -1,28 +1,28 @@ getPage()->getAll($GLOBALS['user']['id']); +is_member($GLOBALS['User']) or redirect("login.php"); +$site_user = $GLOBALS['User']; +$pages = $GLOBALS['App']->getPage()->getAll($GLOBALS['User']['id']); $errors = []; if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $GLOBALS['user']['home'] = $_POST['home']; + $GLOBALS['User']['home'] = $_POST['home']; // todo validation - $GLOBALS['app']->getUser()->update($GLOBALS['user'], $errors); + $GLOBALS['App']->getUser()->update($GLOBALS['User'], $errors); } ?>
-

Editing home

+

Editing

- +
diff --git a/image.php b/image.php deleted file mode 100644 --- a/image.php +++ /dev/null @@ -1,33 +0,0 @@ -getImage()->get($id); -?> - - -
-

-
- - -
-
- -"/> - -

- - - - - - - diff --git a/includes/app.php b/includes/app.php --- a/includes/app.php +++ b/includes/app.php @@ -6,12 +6,12 @@ require "classes/$class.php"; }); -$GLOBALS['app'] = new App(); -$sess_id = $GLOBALS['app']->getSession()->id; -$GLOBALS['user'] = $sess_id ? $GLOBALS['app']->getUser()->get($sess_id) : null; +$App = new App(); +$sess_id = $App->getSession()->id; +$User = $sess_id ? $App->getUser()->get($sess_id) : null; if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $GLOBALS['app']->getSession()->verifyCSRF($_POST['csrf']) or page_not_found(); + $App->getSession()->verifyCSRF($_POST['csrf']) or page_not_found(); } function is_visitor($user) @@ -31,7 +31,7 @@ function is_site_admin($admin) { - return is_member($GLOBALS['user']) && $admin['id'] === $GLOBALS['user']['id']; + return is_member($GLOBALS['User']) && $admin['id'] === $GLOBALS['User']['id']; } function redirect($page) @@ -76,31 +76,13 @@ return $id; } -function get_slug() +function get_param($param) { - if (!($slug = filter_input(INPUT_GET, 'slug', FILTER_SANITIZE_SPECIAL_CHARS))) { + if (!($rv = filter_input(INPUT_GET, $param, FILTER_SANITIZE_SPECIAL_CHARS))) { page_not_found(); } - 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))) { - return 1; - } - - return $page; + return $rv; } function timeAgo($dateString) @@ -126,42 +108,22 @@ return $output; } -function get_link($user_id, $slug) +function site_link($name, $page = null, $value = null) { - return "$slug"; -} - -function get_link2($username, $slug = null) -{ - if (isset($slug)) { - return "$slug"; - } else { - return "$username"; - } + $qs = "u=$name"; + $page && $qs .= "&p=$page"; + $label = $value ?: ($page ?: $name); + return "$label"; } -function get_url($username, $page = null) +function edit_link($page) { - if (isset($page)) { - return "site.php?u=$username&p=$page"; - } else { - return "site.php?u=$username"; - } + return "Edit"; } -function home_link($user_id, $name) -{ - return "$name"; -} - -function edit_link($slug) +function delete_link($page) { - return "Edit"; -} - -function delete_link($slug) -{ - return "Delete"; + return "Delete"; } function gmi($text) @@ -224,30 +186,11 @@ } define('LINK_REGEXP', '/\[\[([a-z0-9_-]+)\]\]/'); -function content_to_html($content, $user_id) -{ - return preg_replace_callback(LINK_REGEXP, function ($match) use ($user_id) { - $slug = $match[1]; - return get_link($user_id, $slug); - }, gmi($content)); -} -function content_to_html2($content, $user) + +function content_to_html($content, $user) { return preg_replace_callback(LINK_REGEXP, function ($match) use ($user) { $slug = $match[1]; - return get_link2($user['name'], $slug); + return site_link($user['name'], $slug); }, gmi($content)); } - -function nav_admin() -{ - return join(" ", [ - "", - ]); -} \ No newline at end of file diff --git a/includes/csrf.php b/includes/csrf.php --- a/includes/csrf.php +++ b/includes/csrf.php @@ -1,1 +1,1 @@ - + diff --git a/includes/page.php b/includes/page.php --- a/includes/page.php +++ b/includes/page.php @@ -22,7 +22,7 @@ diff --git a/includes/site_header.php b/includes/site_header.php --- a/includes/site_header.php +++ b/includes/site_header.php @@ -14,9 +14,9 @@
diff --git a/index.php b/index.php --- a/index.php +++ b/index.php @@ -1,8 +1,8 @@ @@ -10,7 +10,7 @@

tomoni

- +

You account is pending activation. You will be notified by email when activated.

diff --git a/latest.php b/latest.php deleted file mode 100644 --- a/latest.php +++ /dev/null @@ -1,17 +0,0 @@ -getImage()->getFromUser($id, 1); -if (!count($images)) { - http_response_code(404); - exit; -} -$filename = file_path($id, $images['rows'][0]['filename']); - -header("Content-Type: image/jpeg"); -header("Content-Length: " . filesize($filename)); - -$fp = fopen($filename, 'rb'); - -fpassthru($fp); diff --git a/login.php b/login.php --- a/login.php +++ b/login.php @@ -12,8 +12,8 @@ Validate::isPassword($password) or $errors[] = "Wrong password"; if (!count($errors)) { - if ($member = $GLOBALS['app']->getUser()->login($email, $password, $errors)) { - $GLOBALS['app']->getSession()->login($member['id']); + if ($member = $GLOBALS['App']->getUser()->login($email, $password, $errors)) { + $GLOBALS['App']->getSession()->login($member['id']); redirect('index.php'); } } diff --git a/logout.php b/logout.php --- a/logout.php +++ b/logout.php @@ -2,5 +2,5 @@ require 'includes/app.php'; -$GLOBALS['app']->getSession()->logout(); +$GLOBALS['App']->getSession()->logout(); header('Location: .'); diff --git a/page.php b/page.php deleted file mode 100644 --- a/page.php +++ /dev/null @@ -1,49 +0,0 @@ -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); -?> - - - -
- - - -

Not found

- -

Create page for ?

-
- - - - - -
- - -
- - - - - - - - - - diff --git a/page_create.php b/page_create.php --- a/page_create.php +++ b/page_create.php @@ -1,8 +1,8 @@ '', @@ -18,8 +18,8 @@ Validate::isPage($name) or $errors[] = "Slug can only contain a-z0-9_-"; if (!count($errors)) { - $id = $GLOBALS['app']->getPage()->create($GLOBALS['user']['id'], $name, ''); - redirect("page_update.php?slug=$name"); + $id = $App->getPage()->create($User['id'], $name, ''); + redirect("page_update.php?p=$name"); } } ?> diff --git a/page_delete.php b/page_delete.php --- a/page_delete.php +++ b/page_delete.php @@ -1,15 +1,15 @@ getPage()->get($GLOBALS['user']['id'], $slug) or page_not_found(); +$form = $App->getPage()->get($User['id'], $p) or page_not_found(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $GLOBALS['app']->getPage()->delete($GLOBALS['user']['id'], $slug); - $GLOBALS['app']->getSession()->setFlash("Page deleted"); + $App->getPage()->delete($User['id'], $p); + $App->getSession()->setFlash("Page deleted"); redirect("index.php"); } ?> diff --git a/page_update.php b/page_update.php --- a/page_update.php +++ b/page_update.php @@ -1,11 +1,11 @@ getPage()->get($GLOBALS['user']['id'], $slug) or page_not_found(); +$site_user = $User; +$form = $App->getPage()->get($User['id'], $p) or page_not_found(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $form['content'] = $_POST['content']; @@ -13,14 +13,15 @@ // todo validation if (!count($errors)) { - $GLOBALS['app']->getPage()->update($GLOBALS['user']['id'], $slug, $form['content']); + $App->getPage()->update($User['id'], $p, $form['content']); } } ?> +
-

Editing

+

Editing

@@ -34,7 +35,7 @@
diff --git a/pages.php b/pages.php deleted file mode 100644 --- a/pages.php +++ /dev/null @@ -1,26 +0,0 @@ -getPage()->getAll($user['id']); -?> - - - -

Pages

-

create page

- - - - - - - - - - - - - -
homeedit
edit" ?>delete" ?>
- diff --git a/pages_index.php b/pages_index.php --- a/pages_index.php +++ b/pages_index.php @@ -1,8 +1,9 @@ getPage()->getAll($id); -$site_user = $GLOBALS['app']->getUser()->get($id) or page_not_found(); +$pages = $App->getPage()->getAll($id); +$site_user = $App->getUser()->get($id) or page_not_found(); ?> @@ -12,7 +13,7 @@ diff --git a/password-update.php b/password-update.php --- a/password-update.php +++ b/password-update.php @@ -1,7 +1,7 @@ getUser()->updatePassword($user['id'], $password); + $App->getUser()->updatePassword($User['id'], $password); $App->getSession()->setFlash("Password updated"); } } diff --git a/profile.php b/profile.php deleted file mode 100644 --- a/profile.php +++ /dev/null @@ -1,43 +0,0 @@ -getUser()->get($id) or page_not_found(); -$res = $App->getImage()->getFromUser($id, $page); -$images = $res['rows']; -$next_page = $res['next_page']; -?> - - -

- -

-

Subscribe via RSS

- - -
-
-

">

-
- -
-
- - "/> -

-
- - - - - - diff --git a/register.php b/register.php --- a/register.php +++ b/register.php @@ -25,15 +25,15 @@ } if (!count($errors)) { - $id = $GLOBALS['app']->getUser()->create([ + $id = $GLOBALS['App']->getUser()->create([ "name" => $form['name'], "email" => $form['email'], "cover" => $form['cover'], "password" => $password, ], $errors); if ($id !== false) { - $GLOBALS['app']->getSession()->login($id); - $GLOBALS['app']->getSession()->setFlash("Thanks for registering! You will be able to start posting as soon as your account gets activated!"); + $GLOBALS['App']->getSession()->login($id); + $GLOBALS['App']->getSession()->setFlash("Thanks for registering! You will be able to start posting as soon as your account gets activated!"); redirect("index.php"); } } diff --git a/role.php b/role.php --- a/role.php +++ b/role.php @@ -1,7 +1,7 @@ getUser()->get($id); + $selected = $GLOBALS['App']->getUser()->get($id); if (!$selected) { $errors[] = "Can't find user"; @@ -18,7 +18,7 @@ } } -$users = $GLOBALS['app']->getUser()->getAll(); +$users = $GLOBALS['App']->getUser()->getAll(); ?> diff --git a/settings.php b/settings.php --- a/settings.php +++ b/settings.php @@ -1,10 +1,10 @@ getUser()->update($form, $errors)) { - $app->getSession()->setFlash("Settings updated"); + if ($App->getUser()->update($form, $errors)) { + $App->getSession()->setFlash("Settings updated"); $site_user['style'] = $form['style']; } } diff --git a/site.php b/site.php --- a/site.php +++ b/site.php @@ -1,19 +1,19 @@ getUser()->getFromUsername($u) or page_not_found(); +$site_user = $App->getUser()->getFromUsername($u) or page_not_found(); $is_admin = is_site_admin($site_user); if ($p) { - $page = $app->getPage()->get($site_user['id'], $p); - $related = $app->getPage()->related($site_user['id'], $p); + $page = $App->getPage()->get($site_user['id'], $p); + $related = $App->getPage()->related($site_user['id'], $p); if ($page) { - $content = content_to_html2($page['content'], $site_user); + $content = content_to_html($page['content'], $site_user); } include 'includes/page.php'; } else { - $content = content_to_html2($site_user['home'], $site_user); + $content = content_to_html($site_user['home'], $site_user); include 'includes/home.php'; } \ No newline at end of file diff --git a/update.php b/update.php deleted file mode 100644 --- a/update.php +++ /dev/null @@ -1,44 +0,0 @@ -getImage()->get(get_id()) or page_not_found(); - -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - if ($form['user_id'] === $user['id'] || is_admin($user)) { - $form['description'] = $_POST['description']; - Validate::isFilename($_POST['filename']) or $errors [] = 'Wrong file name'; - if ($form['filename'] !== $_POST['filename']) { - Validate::isAvailableFilename($user['id'], $_POST['filename']) or $errors [] = 'File already exists'; - } - Validate::isAcceptableHTML($form['description']) or $errors [] = "Description has a forbidden HTML tag"; - - if (!count($errors)) { - $App->getImage()->update($form['id'], $form['user_id'], $form['filename'], $_POST['filename'], $form['description']); - $App->getSession()->setFlash("Image updated"); - redirect("index.php"); - } - } -} -?> - - - -

Update image

- -
- - - - - - - - -
- - \ No newline at end of file diff --git a/upload.php b/upload.php --- a/upload.php +++ b/upload.php @@ -1,7 +1,7 @@ getImage()->create([ "filename" => $_FILES['image']['name'], "description" => $_POST['description'], - "user_id" => $user['id'] + "user_id" => $User['id'] ]); redirect('image.php?id=' . $id); } diff --git a/user-feed.php b/user-feed.php deleted file mode 100644 --- a/user-feed.php +++ /dev/null @@ -1,24 +0,0 @@ -getUser()->get($id) or page_not_found(); -$images = $App->getImage()->getFromUser($id, 1); -?> - - - <?= $profile['name'] ?> - piclog feed - - - - <?= $image['filename'] ?> - - - - - - - -Widget

Add the following snippet to your site to show your latest picture. Feel free to customize how it looks like!

-

+ style="width: 100%;"> +