summaryrefslogtreecommitdiff
path: root/inc
diff options
context:
space:
mode:
authorczaks <marcin@6irc.net>2015-03-10 14:22:47 +0100
committerczaks <marcin@6irc.net>2015-03-10 14:22:47 +0100
commitbf1b6103cf6d3cf7f83e85453aad2d03e03e62da (patch)
tree9460a02b2e7629059349fb88cacb1ad54bab1814 /inc
parent6c4d3941fa729558f4a5313081c85b0a06202e54 (diff)
parent4bf525599e7c596e7aec7f483664a091ef0db57b (diff)
Merge branch 'master' of github.com:vichan-devel/Tinyboard
Diffstat (limited to 'inc')
-rw-r--r--inc/api.php1
-rw-r--r--inc/config.php7
-rw-r--r--inc/display.php4
-rwxr-xr-xinc/functions.php116
-rw-r--r--inc/lib/Twig/Extensions/Extension/Tinyboard.php3
-rw-r--r--inc/mod/pages.php23
6 files changed, 128 insertions, 26 deletions
diff --git a/inc/api.php b/inc/api.php
index 661ee109..ac3b1325 100644
--- a/inc/api.php
+++ b/inc/api.php
@@ -33,6 +33,7 @@ class Api {
'sticky' => 'sticky',
'locked' => 'locked',
'bump' => 'last_modified',
+ 'slug' => 'semantic_url',
);
$this->threadsPageFields = array(
diff --git a/inc/config.php b/inc/config.php
index edbe7b2a..ac4acaa4 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -566,6 +566,9 @@
// Allow dice rolling: an email field of the form "dice XdY+/-Z" will result in X Y-sided dice rolled and summed,
// with the modifier Z added, with the result displayed at the top of the post body.
$config['allow_roll'] = false;
+
+ // Use semantic URLs for threads, like /b/res/12345/daily-programming-thread.html
+ $config['slugify'] = false;
/*
* ====================
@@ -1118,8 +1121,10 @@
// Location of files.
$config['file_index'] = 'index.html';
- $config['file_page'] = '%d.html';
+ $config['file_page'] = '%d.html'; // NB: page is both an index page and a thread
$config['file_page50'] = '%d+50.html';
+ $config['file_page_slug'] = '%d-%s.html';
+ $config['file_page50_slug'] = '%d-%s+50.html';
$config['file_mod'] = 'mod.php';
$config['file_post'] = 'post.php';
$config['file_script'] = 'main.js';
diff --git a/inc/display.php b/inc/display.php
index 1a78e19b..65525ab9 100644
--- a/inc/display.php
+++ b/inc/display.php
@@ -383,7 +383,7 @@ class Post {
public function link($pre = '', $page = false) {
global $config, $board;
- return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->thread) . '#' . $pre . $this->id;
+ return $this->root . $board['dir'] . $config['dir']['res'] . link_for((array)$this, $page == '50') . '#' . $pre . $this->id;
}
public function build($index=false) {
@@ -438,7 +438,7 @@ class Thread {
public function link($pre = '', $page = false) {
global $config, $board;
- return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->id) . '#' . $pre . $this->id;
+ return $this->root . $board['dir'] . $config['dir']['res'] . link_for((array)$this, $page == '50') . '#' . $pre . $this->id;
}
public function add(Post $post) {
$this->posts[] = $post;
diff --git a/inc/functions.php b/inc/functions.php
index 25362a62..704c6600 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'], '/') . '\?\/.+' .
@@ -356,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') {
@@ -377,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']);
}
}
@@ -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,13 @@ function post(array $post) {
$query->bindValue(':filehash', null, PDO::PARAM_NULL);
}
+ if ($post['op']) {
+ $query->bindValue(':slug', slugify($post));
+ }
+ else {
+ $query->bindValue(':slug', NULL);
+ }
+
if (!$query->execute()) {
undoImage($post);
error(db_error($query));
@@ -1076,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));
@@ -1094,8 +1103,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');
@@ -1801,7 +1810,7 @@ function markup(&$body, $track_cites = false) {
if (isset($cited_posts[$cite])) {
$replacement = '<a onclick="highlightReply(\''.$cite.'\');" href="' .
$config['root'] . $board['dir'] . $config['dir']['res'] .
- ($cited_posts[$cite] ? $cited_posts[$cite] : $cite) . '.html#' . $cite . '">' .
+ link_for(array('id' => $cite, 'thread' => $cited_posts[$cite])) . '#' . $cite . '">' .
'&gt;&gt;' . $cite .
'</a>';
@@ -1867,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'];
}
}
@@ -2067,12 +2076,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 +2161,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 +2430,84 @@ function diceRoller($post) {
}
}
}
+
+function slugify($post) {
+ $slug = "";
+
+ 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'])
+ $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 : (isset($post['board']) ? array('uri' => $post['board']) : $board);
+
+ $id = (isset($post['thread']) && $post['thread']) ? $post['thread'] : $post['id'];
+
+ $slug = false;
+
+ if ($config['slugify'] && ( (isset($post['thread']) && $post['thread']) || !isset ($post['slug']) ) ) {
+ $cvar = "slug_".$b['uri']."_".$id;
+ if (!$thread) {
+ $slug = Cache::get($cvar);
+
+ 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);
+
+ $slug = $thread['slug'];
+
+ Cache::set($cvar, $slug);
+ }
+ }
+ else {
+ $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);
+}
diff --git a/inc/lib/Twig/Extensions/Extension/Tinyboard.php b/inc/lib/Twig/Extensions/Extension/Tinyboard.php
index 81276147..028db438 100644
--- a/inc/lib/Twig/Extensions/Extension/Tinyboard.php
+++ b/inc/lib/Twig/Extensions/Extension/Tinyboard.php
@@ -45,7 +45,8 @@ class Twig_Extensions_Extension_Tinyboard extends Twig_Extension
new Twig_SimpleFunction('hiddenInputsHash', 'hiddenInputsHash'),
new Twig_SimpleFunction('ratio', 'twig_ratio_function'),
new Twig_SimpleFunction('secure_link_confirm', 'twig_secure_link_confirm'),
- new Twig_SimpleFunction('secure_link', 'twig_secure_link')
+ new Twig_SimpleFunction('secure_link', 'twig_secure_link'),
+ new Twig_SimpleFunction('link_for', 'link_for')
);
}
diff --git a/inc/mod/pages.php b/inc/mod/pages.php
index c7722763..be713f57 100644
--- a/inc/mod/pages.php
+++ b/inc/mod/pages.php
@@ -1161,7 +1161,7 @@ function mod_move_reply($originBoard, $postID) {
$post = $query->fetch(PDO::FETCH_ASSOC);
// redirect
- header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $newID) . '#' . $newID, true, $config['redirect_http']);
+ header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . link_for($post) . '#' . $newID, true, $config['redirect_http']);
}
else {
@@ -1224,7 +1224,10 @@ function mod_move($originBoard, $postID) {
// create the new thread
$newID = post($post);
-
+
+ $op = $post;
+ $op['id'] = $newID;
+
if ($post['has_file']) {
// copy image
foreach ($post['files'] as $i => &$file) {
@@ -1322,6 +1325,8 @@ function mod_move($originBoard, $postID) {
// trigger themes
rebuildThemes('post', $targetBoard);
+ $newboard = $board;
+
// return to original board
openBoard($originBoard);
@@ -1332,7 +1337,7 @@ function mod_move($originBoard, $postID) {
$query->execute() or error(db_error($query));
// leave a reply, linking to the new thread
- $post = array(
+ $spost = array(
'mod' => true,
'subject' => '',
'email' => '',
@@ -1346,23 +1351,23 @@ function mod_move($originBoard, $postID) {
'op' => false
);
- $post['body'] = $post['body_nomarkup'] = sprintf($config['mod']['shadow_mesage'], '>>>/' . $targetBoard . '/' . $newID);
+ $spost['body'] = $spost['body_nomarkup'] = sprintf($config['mod']['shadow_mesage'], '>>>/' . $targetBoard . '/' . $newID);
- markup($post['body']);
+ markup($spost['body']);
- $botID = post($post);
+ $botID = post($spost);
buildThread($postID);
buildIndex();
- header('Location: ?/' . sprintf($config['board_path'], $originBoard) . $config['dir']['res'] .sprintf($config['file_page'], $postID) .
+ header('Location: ?/' . sprintf($config['board_path'], $newboard['uri']) . $config['dir']['res'] . link_for($op, false, $newboard) .
'#' . $botID, true, $config['redirect_http']);
} else {
deletePost($postID);
buildIndex();
openBoard($targetBoard);
- header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . sprintf($config['file_page'], $newID), true, $config['redirect_http']);
+ header('Location: ?/' . sprintf($config['board_path'], $newboard['uri']) . $config['dir']['res'] . link_for($op, false, $newboard), true, $config['redirect_http']);
}
}
@@ -1494,7 +1499,7 @@ function mod_edit_post($board, $edit_raw_html, $postID) {
rebuildThemes('post', $board);
- header('Location: ?/' . sprintf($config['board_path'], $board) . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $postID) . '#' . $postID, true, $config['redirect_http']);
+ header('Location: ?/' . sprintf($config['board_path'], $board) . $config['dir']['res'] . link_for($post) . '#' . $postID, true, $config['redirect_http']);
} else {
if ($config['minify_html']) {
$post['body_nomarkup'] = str_replace("\n", '&#010;', utf8tohtml($post['body_nomarkup']));