summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/controller.php108
-rw-r--r--inc/route.php62
-rw-r--r--smart_build.php153
3 files changed, 179 insertions, 144 deletions
diff --git a/inc/controller.php b/inc/controller.php
new file mode 100644
index 00000000..02e33443
--- /dev/null
+++ b/inc/controller.php
@@ -0,0 +1,108 @@
+<?php
+
+// This file contains the controller part of vichan
+
+// don't bother with that unless you use smart build or advanced build
+// you can use those parts for your own implementations though :^)
+
+defined('TINYBOARD') or exit;
+
+function sb_board($b, $page = 1) { global $config, $build_pages; $page = (int)$page;
+ if ($page < 1) return false;
+ if (!openBoard($b)) return false;
+ if ($page > $config['max_pages']) return false;
+ $config['try_smarter'] = true;
+ $build_pages = array($page);
+ buildIndex("skip");
+ return true;
+}
+
+function sb_api_board($b, $page = 0) { $page = (int)$page;
+ return sb_board($b, $page + 1);
+}
+
+function sb_thread($b, $thread, $slugcheck = false) { global $config; $thread = (int)$thread;
+ if ($thread < 1) return false;
+
+ if (!preg_match('/^'.$config['board_regex'].'$/u', $b)) return false;
+
+ if (Cache::get("thread_exists_".$b."_".$thread) == "no") return false;
+
+ $query = prepare(sprintf("SELECT MAX(`id`) AS `max` FROM ``posts_%s``", $b));
+ if (!$query->execute()) return false;
+
+ $s = $query->fetch(PDO::FETCH_ASSOC);
+ $max = $s['max'];
+
+ if ($thread > $max) return false;
+
+ $query = prepare(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL", $b));
+ $query->bindValue(':id', $thread);
+
+ if (!$query->execute() || !$query->fetch(PDO::FETCH_ASSOC) ) {
+ Cache::set("thread_exists_".$b."_".$thread, "no", 3600);
+ return false;
+ }
+
+ if ($slugcheck && $config['slugify']) {
+ global $request;
+
+ $link = link_for(array("id" => $thread), $slugcheck === 50, array("uri" => $b));
+ $link = "/".$b."/".$config['dir']['res'].$link;
+
+ if ($link != $request) {
+ header("Location: $link", true, 301);
+ die();
+ }
+ }
+
+ if ($slugcheck == 50) { // Should we really generate +50 page? Maybe there are not enough posts anyway
+ global $request;
+ $r = str_replace("+50", "", $request);
+ $r = substr($r, 1); // Cut the slash
+
+ if (file_exists($r)) return false;
+ }
+
+ if (!openBoard($b)) return false;
+ buildThread($thread);
+ return true;
+}
+
+function sb_thread_slugcheck($b, $thread) {
+ return sb_thread($b, $thread, true);
+}
+function sb_thread_slugcheck50($b, $thread) {
+ return sb_thread($b, $thread, 50);
+}
+
+function sb_api($b) { global $config, $build_pages;
+ if (!openBoard($b)) return false;
+ $config['try_smarter'] = true;
+ $build_pages = array(-1);
+ buildIndex();
+ return true;
+}
+
+function sb_ukko() {
+ rebuildTheme("ukko", "post-thread");
+ return true;
+}
+
+function sb_catalog($b) {
+ if (!openBoard($b)) return false;
+
+ rebuildTheme("catalog", "post-thread", $b);
+ return true;
+}
+
+function sb_recent() {
+ rebuildTheme("recent", "post-thread");
+ return true;
+}
+
+function sb_sitemap() {
+ rebuildTheme("sitemap", "all");
+ return true;
+}
+
diff --git a/inc/route.php b/inc/route.php
new file mode 100644
index 00000000..66602d77
--- /dev/null
+++ b/inc/route.php
@@ -0,0 +1,62 @@
+<?php
+
+// vichan's routing mechanism
+
+// don't bother with that unless you use smart build or advanced build
+// you can use those parts for your own implementations though :^)
+
+defined('TINYBOARD') or exit;
+
+function route($path) {
+ $entrypoints = array();
+
+ $entrypoints['/%b/'] = 'sb_board';
+ $entrypoints['/%b/'.$config['file_index']] = 'sb_board';
+ $entrypoints['/%b/'.$config['file_page']] = 'sb_board';
+ $entrypoints['/%b/%d.json'] = 'sb_api_board';
+ if ($config['api']['enabled']) {
+ $entrypoints['/%b/threads.json'] = 'sb_api';
+ $entrypoints['/%b/catalog.json'] = 'sb_api';
+ }
+
+ $entrypoints['/%b/'.$config['dir']['res'].$config['file_page']] = 'sb_thread_slugcheck';
+ $entrypoints['/%b/'.$config['dir']['res'].$config['file_page50']] = 'sb_thread_slugcheck50';
+ if ($config['slugify']) {
+ $entrypoints['/%b/'.$config['dir']['res'].$config['file_page_slug']] = 'sb_thread_slugcheck';
+ $entrypoints['/%b/'.$config['dir']['res'].$config['file_page50_slug']] = 'sb_thread_slugcheck50';
+ }
+ if ($config['api']['enabled']) {
+ $entrypoints['/%b/'.$config['dir']['res'].'%d.json'] = 'sb_thread';
+ }
+
+ $entrypoints['/*/'] = 'sb_ukko';
+ $entrypoints['/*/index.html'] = 'sb_ukko';
+ $entrypoints['/recent.html'] = 'sb_recent';
+ $entrypoints['/%b/catalog.html'] = 'sb_catalog';
+ $entrypoints['/sitemap.xml'] = 'sb_sitemap';
+
+ $reached = false;
+
+ list($request) = explode('?', $path);
+
+ foreach ($entrypoints as $id => $fun) {
+ $id = '@^' . preg_quote($id, '@') . '$@u';
+
+ $id = str_replace('%b', '('.$config['board_regex'].')', $id);
+ $id = str_replace('%d', '([0-9]+)', $id);
+ $id = str_replace('%s', '[a-zA-Z0-9-]+', $id);
+
+ $matches = null;
+
+ if (preg_match ($id, $request, $matches)) {
+ array_shift($matches);
+
+ $reached = array($fun, $matches);
+
+ break;
+ }
+ }
+
+ return $reached;
+}
+
diff --git a/smart_build.php b/smart_build.php
index e287a31a..58596055 100644
--- a/smart_build.php
+++ b/smart_build.php
@@ -1,5 +1,7 @@
<?php
require_once("inc/functions.php");
+require_once("inc/route.php");
+require_once("inc/controller.php");
if (!$config['smart_build'] && !$config["smart_build_helper"]) {
die('You need to enable $config["smart_build"] or $config["smart_build_helper"]');
@@ -11,153 +13,16 @@ function after_open_board() { global $config;
$config['smart_build'] = false;
};
-function sb_board($b, $page = 1) { global $config, $build_pages; $page = (int)$page;
- if ($page < 1) return false;
- if (!openBoard($b)) return false;
- if ($page > $config['max_pages']) return false;
- $config['try_smarter'] = true;
- $build_pages = array($page);
- buildIndex("skip");
- return true;
-}
-
-function sb_api_board($b, $page = 0) { $page = (int)$page;
- return sb_board($b, $page + 1);
-}
-
-function sb_thread($b, $thread, $slugcheck = false) { global $config; $thread = (int)$thread;
- if ($thread < 1) return false;
-
- if (!preg_match('/^'.$config['board_regex'].'$/u', $b)) return false;
-
- if (Cache::get("thread_exists_".$b."_".$thread) == "no") return false;
-
- $query = prepare(sprintf("SELECT MAX(`id`) AS `max` FROM ``posts_%s``", $b));
- if (!$query->execute()) return false;
-
- $s = $query->fetch(PDO::FETCH_ASSOC);
- $max = $s['max'];
-
- if ($thread > $max) return false;
-
- $query = prepare(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL", $b));
- $query->bindValue(':id', $thread);
-
- if (!$query->execute() || !$query->fetch(PDO::FETCH_ASSOC) ) {
- Cache::set("thread_exists_".$b."_".$thread, "no", 3600);
- return false;
- }
-
- if ($slugcheck && $config['slugify']) {
- global $request;
-
- $link = link_for(array("id" => $thread), $slugcheck === 50, array("uri" => $b));
- $link = "/".$b."/".$config['dir']['res'].$link;
-
- if ($link != $request) {
- header("Location: $link", true, 301);
- die();
- }
- }
-
- if ($slugcheck == 50) { // Should we really generate +50 page? Maybe there are not enough posts anyway
- global $request;
- $r = str_replace("+50", "", $request);
- $r = substr($r, 1); // Cut the slash
-
- if (file_exists($r)) return false;
- }
-
- if (!openBoard($b)) return false;
- buildThread($thread);
- return true;
-}
-
-function sb_thread_slugcheck($b, $thread) {
- return sb_thread($b, $thread, true);
-}
-function sb_thread_slugcheck50($b, $thread) {
- return sb_thread($b, $thread, 50);
-}
-
-function sb_api($b) { global $config, $build_pages;
- if (!openBoard($b)) return false;
- $config['try_smarter'] = true;
- $build_pages = array(-1);
- buildIndex();
- return true;
-}
-
-function sb_ukko() {
- rebuildTheme("ukko", "post-thread");
- return true;
-}
-
-function sb_catalog($b) {
- if (!openBoard($b)) return false;
-
- rebuildTheme("catalog", "post-thread", $b);
- return true;
-}
-
-function sb_recent() {
- rebuildTheme("recent", "post-thread");
- return true;
-}
-
-function sb_sitemap() {
- rebuildTheme("sitemap", "all");
- return true;
-}
-
-$entrypoints = array();
-
-$entrypoints['/%b/'] = 'sb_board';
-$entrypoints['/%b/'.$config['file_index']] = 'sb_board';
-$entrypoints['/%b/'.$config['file_page']] = 'sb_board';
-$entrypoints['/%b/%d.json'] = 'sb_api_board';
-if ($config['api']['enabled']) {
- $entrypoints['/%b/threads.json'] = 'sb_api';
- $entrypoints['/%b/catalog.json'] = 'sb_api';
-}
-
-$entrypoints['/%b/'.$config['dir']['res'].$config['file_page']] = 'sb_thread_slugcheck';
-$entrypoints['/%b/'.$config['dir']['res'].$config['file_page50']] = 'sb_thread_slugcheck50';
-if ($config['slugify']) {
- $entrypoints['/%b/'.$config['dir']['res'].$config['file_page_slug']] = 'sb_thread_slugcheck';
- $entrypoints['/%b/'.$config['dir']['res'].$config['file_page50_slug']] = 'sb_thread_slugcheck50';
-}
-if ($config['api']['enabled']) {
- $entrypoints['/%b/'.$config['dir']['res'].'%d.json'] = 'sb_thread';
-}
-
-$entrypoints['/*/'] = 'sb_ukko';
-$entrypoints['/*/index.html'] = 'sb_ukko';
-$entrypoints['/recent.html'] = 'sb_recent';
-$entrypoints['/%b/catalog.html'] = 'sb_catalog';
-$entrypoints['/sitemap.xml'] = 'sb_sitemap';
-
-$reached = false;
-
$request = $_SERVER['REQUEST_URI'];
-list($request) = explode('?', $request);
-
-foreach ($entrypoints as $id => $fun) {
- $id = '@^' . preg_quote($id, '@') . '$@u';
- $id = str_replace('%b', '('.$config['board_regex'].')', $id);
- $id = str_replace('%d', '([0-9]+)', $id);
- $id = str_replace('%s', '[a-zA-Z0-9-]+', $id);
+$route = route($request);
- $matches = null;
-
- if (preg_match ($id, $request, $matches)) {
- array_shift($matches);
-
- $reached = call_user_func_array($fun, $matches);
-
- break;
- }
+if (!$route) {
+ $reached = false;
+}
+else {
+ list ($fun, $args) = $route;
+ $reached = call_user_func_array($route);
}
function die_404() { global $config;