From bdb6001f3f7e93e0b9655c0491bbef6f1df8a5fa Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 12:48:59 +0100 Subject: support for slugified links; may introduce a few bugs --- inc/functions.php | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 7 deletions(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index 25362a62..79d06f5c 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -150,7 +150,9 @@ function loadConfig() { preg_quote($config['dir']['res'], '/') . '(' . str_replace('%d', '\d+', preg_quote($config['file_page'], '/')) . '|' . - str_replace('%d', '\d+', preg_quote($config['file_page50'], '/')) . + str_replace('%d', '\d+', preg_quote($config['file_page50'], '/')) . '|' . + str_replace(array('%d', '%s'), array('\d+', '[a-z0-9-]+'), preg_quote($config['file_page_slug'], '/')) . '|' . + str_replace(array('%d', '%s'), array('\d+', '[a-z0-9-]+'), preg_quote($config['file_page50_slug'], '/')) . ')' . '|' . preg_quote($config['file_mod'], '/') . '\?\/.+' . @@ -912,7 +914,7 @@ function insertFloodPost(array $post) { function post(array $post) { global $pdo, $board; - $query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed)", $board['uri'])); + $query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed, :slug)", $board['uri'])); // Basic stuff if (!empty($post['subject'])) { @@ -981,6 +983,10 @@ function post(array $post) { $query->bindValue(':filehash', null, PDO::PARAM_NULL); } + if ($post['op']) { + $query->bindValue(':slug', slugify($post)); + } + if (!$query->execute()) { undoImage($post); error(db_error($query)); @@ -1094,8 +1100,8 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { if (!$post['thread']) { // Delete thread HTML page - file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id'])); - file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id'])); + file_unlink($board['dir'] . $config['dir']['res'] . link_for($post) ); + file_unlink($board['dir'] . $config['dir']['res'] . link_for($post, true) ); // noko50 file_unlink($board['dir'] . $config['dir']['res'] . sprintf('%d.json', $post['id'])); $antispam_query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board AND `thread` = :thread'); @@ -2067,12 +2073,12 @@ function buildThread($id, $return = false, $mod = false) { if ($return) { return $body; } else { - $noko50fn = $board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id); + $noko50fn = $board['dir'] . $config['dir']['res'] . link_for($thread, true); if ($hasnoko50 || file_exists($noko50fn)) { buildThread50($id, $return, $mod, $thread, $antibot); } - file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body); + file_write($board['dir'] . $config['dir']['res'] . link_for($thread), $body); } } @@ -2152,7 +2158,7 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti if ($return) { return $body; } else { - file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id), $body); + file_write($board['dir'] . $config['dir']['res'] . link_for($thread, true), $body); } } @@ -2421,3 +2427,75 @@ function diceRoller($post) { } } } + +function slugify($post) { + $slug = ""; + + if (isset($post['thread']) && $post['thread']) + $slug = $post['thread']; + elseif (isset ($post['body_nomarkup']) && $post['body_nomarkup']) + $slug = $post['body_nomarkup']; + elseif (isset ($post['body']) && $post['body']) + $slug = strip_html($post['body']); + + // Fix UTF-8 first + $slug = mb_convert_encoding($slug, "UTF-8", "UTF-8"); + + // Transliterate local characters like ΓΌ, I wonder how would it work for weird alphabets :^) + $slug = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $slug); + + // Downcase everything + $slug = strtolower($slug); + + // Strip bad characters, alphanumerics should suffice + $slug = preg_replace('/[^a-zA-Z0-9]/', '-', $slug); + + // Replace multiple dashes with single ones + $slug = preg_replace('/-+/', '-', $slug); + + // Strip dashes at the beginning and at the end + $slug = preg_replace('/^-|-$/', '', $slug); + + // Slug should be 200 characters long, at max + $slug = substr($slug, 0, 200); + + // Slug is now ready + return $slug; +} + +function link_for($post, $page50 = false, $foreignlink = false, $thread = false) { + global $config, $board; + + $post = (array)$post; + + // Where do we need to look for OP? + $b = $foreignlink ? $foreignlink : $board; + + $id = (isset($post['thread']) && $post['thread']) ? $post['thread'] : $post['id']; + + $slug = false; + + if ($config['slugify'] && isset($post['thread']) && $post['thread']) { + if (!$thread) { + // Oh fuck, we'd better optimize it ASAP + + $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); + $query->bindValue(':id', $id, PDO::PARAM_INT); + $query->execute() or error(db_error($query)); + + $thread = $query->fetch(PDO::FETCH_ASSOC); + } + $slug = $thread['slug']; + } + elseif ($config['slugify']) { + $slug = $post['slug']; + } + + + if ( $page50 && $slug) $tpl = $config['file_page50_slug']; + else if (!$page50 && $slug) $tpl = $config['file_page_slug']; + else if ( $page50 && !$slug) $tpl = $config['file_page50']; + else if (!$page50 && !$slug) $tpl = $config['file_page']; + + return sprintf($tpl, $id, $slug); +} -- cgit v1.3 From f4bba2e9ed7cbce52c1f33c3f6b5bfac6e8261c3 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 12:57:06 +0100 Subject: ... --- inc/functions.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index 79d06f5c..b318ff35 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -986,6 +986,9 @@ function post(array $post) { if ($post['op']) { $query->bindValue(':slug', slugify($post)); } + else { + $query->bindValue(':slug', NULL); + } if (!$query->execute()) { undoImage($post); -- cgit v1.3 From 429c9f890f67246a38e1de29436fd37f525393d8 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:02:38 +0100 Subject: ... --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index b318ff35..ef7d3b7d 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2472,7 +2472,7 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false) $post = (array)$post; // Where do we need to look for OP? - $b = $foreignlink ? $foreignlink : $board; + $b = $foreignlink ? $foreignlink : (isset($post['board']) ? array('uri' => $post['board']) : $board); $id = (isset($post['thread']) && $post['thread']) ? $post['thread'] : $post['id']; -- cgit v1.3 From fe7e9c5103d1dc2d0b7c1aad2cdd58a552b218fe Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:03:47 +0100 Subject: ... --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index ef7d3b7d..df5bc426 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2482,7 +2482,7 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false) if (!$thread) { // Oh fuck, we'd better optimize it ASAP - $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); + $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $b['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); -- cgit v1.3 From 0062125f5c724183915ec48a9448684cc1b943db Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:09:53 +0100 Subject: ... --- inc/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index df5bc426..2a398ae7 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2434,8 +2434,8 @@ function diceRoller($post) { function slugify($post) { $slug = ""; - if (isset($post['thread']) && $post['thread']) - $slug = $post['thread']; + if (isset($post['subject']) && $post['subject']) + $slug = $post['subject']; elseif (isset ($post['body_nomarkup']) && $post['body_nomarkup']) $slug = $post['body_nomarkup']; elseif (isset ($post['body']) && $post['body']) -- cgit v1.3 From d690567b44464962b0f99c9c222ce4530b9b716e Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:13:18 +0100 Subject: ... (minor fix for locales) --- inc/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index 2a398ae7..846dbdaa 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -358,7 +358,7 @@ function rebuildThemes($action, $boardname = false) { // Reload the locale if ($config['locale'] != $current_locale) { $current_locale = $config['locale']; - init_locale($config['locale'], $error); + init_locale($config['locale']); } if (PHP_SAPI === 'cli') { @@ -379,7 +379,7 @@ function rebuildThemes($action, $boardname = false) { // Reload the locale if ($config['locale'] != $current_locale) { $current_locale = $config['locale']; - init_locale($config['locale'], $error); + init_locale($config['locale']); } } -- cgit v1.3 From 7623de9e2fe8424f35be70cddb9cf68254e49e3e Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:42:10 +0100 Subject: ... (cache) --- inc/functions.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index 846dbdaa..6880ad44 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2479,16 +2479,25 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false) $slug = false; if ($config['slugify'] && isset($post['thread']) && $post['thread']) { + $cvar = "slug_".$b['uri']."_".$id; if (!$thread) { - // Oh fuck, we'd better optimize it ASAP + $slug = Cache::get($cvar); - $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $b['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); - $query->execute() or error(db_error($query)); + if ($slug === false) { + $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $b['uri'])); + $query->bindValue(':id', $id, PDO::PARAM_INT); + $query->execute() or error(db_error($query)); - $thread = $query->fetch(PDO::FETCH_ASSOC); + $thread = $query->fetch(PDO::FETCH_ASSOC); + + $slug = $thread['slug']; + + Cache::set($cvar, $slug); + } + } + else { + $slug = $thread['slug']; } - $slug = $thread['slug']; } elseif ($config['slugify']) { $slug = $post['slug']; -- cgit v1.3 From 9f34d334d3d45471ab88fc90a702999cd2cf6bf5 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:52:31 +0100 Subject: .. --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index 6880ad44..1beb7f6e 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1085,7 +1085,7 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { global $board, $config; // Select post and replies (if thread) in one query - $query = prepare(sprintf("SELECT `id`,`thread`,`files` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri'])); + $query = prepare(sprintf("SELECT `id`,`thread`,`files`,`slug` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); -- cgit v1.3 From a2544bc596465f9915589a47d112fa1475f54720 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 14:16:27 +0100 Subject: ... (cites) --- inc/functions.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index 1beb7f6e..4dc516fc 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1810,7 +1810,7 @@ function markup(&$body, $track_cites = false) { if (isset($cited_posts[$cite])) { $replacement = '' . + link_for(array('id' => $cite)) . '#' . $cite . '">' . '>>' . $cite . ''; @@ -1876,12 +1876,12 @@ function markup(&$body, $track_cites = false) { if (!empty($clauses)) { $cited_posts[$_board] = array(); - $query = query(sprintf('SELECT `thread`, `id` FROM ``posts_%s`` WHERE ' . + $query = query(sprintf('SELECT `thread`, `id`, `slug` FROM ``posts_%s`` WHERE ' . implode(' OR ', $clauses), $board['uri'])) or error(db_error()); while ($cite = $query->fetch(PDO::FETCH_ASSOC)) { $cited_posts[$_board][$cite['id']] = $config['root'] . $board['dir'] . $config['dir']['res'] . - ($cite['thread'] ? $cite['thread'] : $cite['id']) . '.html#' . $cite['id']; + link_for($cite) . '#' . $cite['id']; } } @@ -2478,7 +2478,7 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false) $slug = false; - if ($config['slugify'] && isset($post['thread']) && $post['thread']) { + if ($config['slugify'] && ( (isset($post['thread']) && $post['thread']) || !isset ($post['slug']) ) ) { $cvar = "slug_".$b['uri']."_".$id; if (!$thread) { $slug = Cache::get($cvar); -- cgit v1.3 From 4bf525599e7c596e7aec7f483664a091ef0db57b Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 14:19:36 +0100 Subject: ... --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index 4dc516fc..704c6600 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1810,7 +1810,7 @@ function markup(&$body, $track_cites = false) { if (isset($cited_posts[$cite])) { $replacement = '' . + link_for(array('id' => $cite, 'thread' => $cited_posts[$cite])) . '#' . $cite . '">' . '>>' . $cite . ''; -- cgit v1.3