lainchan/templates/themes/ukko/theme.php

163 lines
5.3 KiB
PHP
Raw Permalink Normal View History

2013-01-02 12:40:30 -05:00
<?php
require 'info.php';
function ukko_build($action, $settings) {
2015-04-02 14:54:28 -04:00
global $config;
2013-01-02 12:40:30 -05:00
$ukko = new ukko();
$ukko->settings = $settings;
if (! ($action == 'all' || $action == 'post' || $action == 'post-thread' || $action == 'post-delete')) {
return;
}
$action = generation_strategy('sb_ukko', array());
if ($action == 'delete') {
2015-04-02 14:54:28 -04:00
file_unlink($settings['uri'] . '/index.html');
2016-12-10 04:15:11 -05:00
if ($config['api']['enabled']) {
$jsonFilename = $settings['uri'] . '/0.json';
file_unlink($jsonFilename);
$jsonFilename = $settings['uri'] . '/catalog.json';
file_unlink($jsonFilename);
$jsonFilename = $settings['uri'] . '/threads.json';
file_unlink($jsonFilename);
2016-12-10 04:15:11 -05:00
}
2015-04-02 14:54:28 -04:00
}
elseif ($action == 'rebuild') {
2015-04-02 14:54:28 -04:00
file_write($settings['uri'] . '/index.html', $ukko->build());
}
2013-01-02 12:40:30 -05:00
}
class ukko {
public $settings;
public function build($mod = false) {
global $config;
$boards = listBoards();
$body = '';
$overflow = array();
$board = array(
'dir' => $this->settings['uri'] . "/",
2013-01-02 12:40:30 -05:00
'url' => $this->settings['uri'],
2015-06-02 09:28:06 -04:00
'uri' => $this->settings['uri'],
2013-01-02 12:40:30 -05:00
'name' => $this->settings['title'],
'title' => sprintf($this->settings['subtitle'], $this->settings['thread_limit'])
);
$boardsforukko = array();
2013-01-02 12:40:30 -05:00
$query = '';
foreach($boards as &$_board) {
if(in_array($_board['uri'], explode(' ', $this->settings['exclude'])))
continue;
$query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` WHERE `thread` IS NULL UNION ALL ", $_board['uri'], $_board['uri']);
array_push($boardsforukko,$_board);
2013-01-02 12:40:30 -05:00
}
$query = preg_replace('/UNION ALL $/', 'ORDER BY `bump` DESC', $query);
$query = query($query) or error(db_error());
$count = 0;
$threads = array();
2016-12-10 04:15:11 -05:00
if ($config['api']['enabled']) {
$apithreads = array();
}
2013-01-02 12:40:30 -05:00
while($post = $query->fetch()) {
if(!isset($threads[$post['board']])) {
$threads[$post['board']] = 1;
} else {
$threads[$post['board']] += 1;
}
if($count < $this->settings['thread_limit']) {
openBoard($post['board']);
$thread = new Thread($post, $mod ? '?/' : $config['root'], $mod);
2013-01-02 12:40:30 -05:00
$posts = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE `thread` = :id ORDER BY `sticky` DESC, `id` DESC LIMIT :limit", $post['board']));
2013-01-02 12:40:30 -05:00
$posts->bindValue(':id', $post['id']);
$posts->bindValue(':limit', ($post['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT);
$posts->execute() or error(db_error($posts));
$num_images = 0;
while ($po = $posts->fetch()) {
2014-04-27 09:48:47 -04:00
if ($po['files'])
2013-01-02 12:40:30 -05:00
$num_images++;
$post2 = new Post($po, $mod ? '?/' : $config['root'], $mod);
$thread->add($post2);
2013-01-02 12:40:30 -05:00
}
if ($posts->rowCount() == ($post['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) {
2014-04-27 09:48:47 -04:00
$ct = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM ``posts_%s`` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM ``posts_%s`` WHERE `files` IS NOT NULL AND `thread` = :thread", $post['board'], $post['board']));
2013-01-02 12:40:30 -05:00
$ct->bindValue(':thread', $post['id'], PDO::PARAM_INT);
$ct->execute() or error(db_error($count));
$c = $ct->fetch();
$thread->omitted = $c['num'] - ($post['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']);
$c = $ct->fetch();
$thread->omitted_images = $c['num'] - $num_images;
}
$thread->posts = array_reverse($thread->posts);
$body .= '<h2><a href="' . $config['root'] . $post['board'] . '">/' . $post['board'] . '/</a></h2>';
$body .= $thread->build(true);
2016-12-10 04:15:11 -05:00
if ($config['api']['enabled']) {
array_push($apithreads,$thread);
}
2013-01-02 12:40:30 -05:00
} else {
$page = 'index';
if(floor($threads[$post['board']] / $config['threads_per_page']) > 0) {
$page = floor($threads[$post['board']] / $config['threads_per_page']) + 1;
}
$overflow[] = array('id' => $post['id'], 'board' => $post['board'], 'page' => $page . '.html');
}
$count += 1;
}
$body .= '<script> var overflow = ' . json_encode($overflow) . '</script>';
2014-04-30 11:24:11 -04:00
$body .= '<script type="text/javascript" src="/'.$this->settings['uri'].'/ukko.js"></script>';
2016-12-10 04:15:11 -05:00
// json api
if ($config['api']['enabled']) {
require_once __DIR__. '/../../../inc/api.php';
$api = new Api();
$jsonFilename = $board['dir'] . '0.json';
$json = json_encode($api->translatePage($apithreads));
file_write($jsonFilename, $json);
$catalog = array();
$catalog[0] = $apithreads;
$json = json_encode($api->translateCatalog($catalog));
$jsonFilename = $board['dir'] . 'catalog.json';
file_write($jsonFilename, $json);
$json = json_encode($api->translateCatalog($catalog, true));
$jsonFilename = $board['dir'] . 'threads.json';
file_write($jsonFilename, $json);
2016-12-10 04:15:11 -05:00
}
$antibot = null;
if (!$antibot) {
$antibot = create_antibot($board['uri']);
}
$antibot->reset();
2014-04-30 11:24:11 -04:00
return Element('index.html', array(
2013-01-02 12:40:30 -05:00
'config' => $config,
'board' => $board,
'no_post_form' => $config['overboard_post_form'] ? false : true,
2013-01-02 12:40:30 -05:00
'body' => $body,
2014-04-30 11:24:11 -04:00
'mod' => $mod,
'boardlist' => createBoardlist($mod),
'boards' => $boardsforukko,
'antibot' => $antibot )
);
2013-01-02 12:40:30 -05:00
}
};
?>