summaryrefslogtreecommitdiff
path: root/inc/controller.php
blob: ceb4fe415ea07dabe3c163aa493c670943341e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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 = [$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', (string) $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(["id" => $thread], $slugcheck === 50, ["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", "", (string) $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 = [-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;
}