summaryrefslogtreecommitdiff
path: root/inc
diff options
context:
space:
mode:
authorSavetheinternet <savetheinternet@tinyboard.org>2011-10-05 23:53:43 +1100
committerSavetheinternet <savetheinternet@tinyboard.org>2011-10-05 23:53:43 +1100
commit0629bf68f7eee1fd88d9227124b93a984c1868b9 (patch)
treec00a77d2bd4f9fb8b8efb3e42271d517b33579cd /inc
parent5582b5599ef0aefc7114da7890795baf8e6b683f (diff)
use template files instead of hardcoded HTML for generating psots
Diffstat (limited to 'inc')
-rw-r--r--inc/config.php2
-rw-r--r--inc/display.php290
-rw-r--r--inc/functions.php19
-rw-r--r--inc/template.php60
4 files changed, 84 insertions, 287 deletions
diff --git a/inc/config.php b/inc/config.php
index ba435609..4a99011f 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -467,7 +467,7 @@
// Array('c', 'd', 'e', 'f', 'g'),
// Array('h', 'i', 'j'),
// Array('k', Array('l', 'm')),
- // 'status' => 'http://status.example.org/'
+ // Array('status' => 'http://status.example.org/')
//);
// Categories
diff --git a/inc/display.php b/inc/display.php
index fc15c152..f544b88c 100644
--- a/inc/display.php
+++ b/inc/display.php
@@ -266,144 +266,9 @@
}
public function build($index=false) {
- global $board, $config, $memcached, $debug;
-
- if(!$this->mod && $config['memcached']['enabled']) {
- if($built = $memcached->get($this->memcached_key($index))) {
- if($config['debug']) {
- $debug['memcached'][] = $this->memcached_key($index);
- }
- return $built;
- }
- }
-
- $built = '<div class="post reply" id="reply_' . $this->id . '">' .
- '<p class="intro"' . (!$index?' id="' . $this->id . '"':'') . '>' .
- // Delete
- '<input type="checkbox" class="delete" name="delete_' . $this->id . '" id="delete_' . $this->id . '" /><label for="delete_' . $this->id . '">';
-
- // Subject
- if(!empty($this->subject))
- $built .= '<span class="subject">' . $this->subject . '</span> ';
- // Email
- if(!empty($this->email))
- $built .= '<a class="email" href="mailto:' . $this->email . '">';
- // Name
- $built .= '<span class="name"' .
- (!empty($this->capcode) && isset($config['custom_capcode'][$this->capcode][1]) ?
- ' style="' . $config['custom_capcode'][$this->capcode][1] . '"'
- : '')
- . '>' . $this->name . '</span>'
- // Trip
- . (!empty($this->trip) ? ' <span class="trip"' .
- (!empty($this->capcode) && isset($config['custom_capcode'][$this->capcode][2]) ?
- ' style="' . $config['custom_capcode'][$this->capcode][2] . '"'
- : '')
- . '>'.$this->trip.'</span>':'')
- // End email
- . (!empty($this->email)? '</a>' : '')
- // Capcode
- . (!empty($this->capcode) ? capcode($this->capcode) : '');
-
- // IP Address
- if($this->mod && hasPermission($config['mod']['show_ip'], $board['uri'], $this->mod)) {
- $built .= ' [<a style="margin:0;" href="?/IP/' . $this->ip . '">' . $this->ip . '</a>]';
- }
-
- // Date/time
- $built .= ' ' . date($config['post_date'], $this->time);
-
- // End delete
- $built .= '</label>'
-
- // Poster ID
- . ($config['poster_ids'] ?
- ' ID: ' . poster_id($this->ip, $this->thread)
- : '')
-
- . ' <a class="post_no"' .
- // JavaScript highlight
- ($index?'':' onclick="highlightReply(' . $this->id . ');"') .
- ' href="' . $this->link() . '">No.</a>' .
- // JavaScript cite
- '<a class="post_no"' . ($index?'':' onclick="citeReply(' . $this->id . ');"') . ' href="' . ($index ? $this->link('q') : 'javascript:void(0);') . '">'.$this->id.'</a>' .
- '</p>';
-
- if($this->embed) {
- // Embedded video (or something else; doesn't have to be a video really)
- $built .=
- // Actual embedding
- $this->embed;
- } elseif(!empty($this->file) && $this->file != 'deleted') {
- // File info
- $built .= '<p class="fileinfo">' .
- 'File: <a href="' . $config['uri_img'] . $this->file . '">' . $this->file . '</a> ' .
- '<span class="unimportant">(' .
- ($this->thumb == 'spoiler' ?
- 'Spoiler Image, '
- : '') .
- // Filesize
- format_bytes($this->filesize) .
- // File dimensions
- ($this->filex && $this->filey ?
- ', ' . $this->filex . 'x' . $this->filey
- : '' );
- // Aspect Ratio
- if($config['show_ratio'] && $this->filex && $this->filey) {
- $fraction = fraction($this->filex, $this->filey, ':');
- $built .= ', ' . $fraction;
- }
- if($config['show_filename']) {
- // Filename
- $built .= ', ' .
- (strlen($this->filename) > $config['max_filename_display'] ?
- '<span title="' . $this->filename . '">' .
- substr($this->filename, 0, $config['max_filename_display']) . '&hellip;' .
- '</span>'
- :
- $this->filename
- );
- }
-
- $ext = explode('.', $this->file);
- $ext = $ext[1];
- $built .= ')</span></p>' .
-
- // Thumbnail
- '<a href="' .
- $config['uri_img'] .$this->file .
- '" target="_blank"' .
- ($this->thumb == 'file' ? ' class="file"' : '') .
- '><img src="' .
- ($this->thumb == 'file' ?
- $config['root'] . sprintf($config['file_thumb'], isset($config['file_icons'][$ext]) ? $config['file_icons'][$ext] : $config['file_icons']['default'])
- :
- ($this->thumb == 'spoiler' ?
- $config['root'] . $config['spoiler_image']
- :
- $config['uri_thumb'] . $this->thumb
- )
- ) .
- '" style="width:' . $this->thumbx . 'px;height:' . $this->thumby . 'px;" alt="" /></a>';
- } elseif($this->file == 'deleted') {
- $built .= '<img src="' . $config['image_deleted'] . '" alt="" />';
- }
-
- $built .= $this->postControls();
-
- // Body
- $built .= '<p class="body">' . ($index ? truncate($this->body, $this->link()) : $this->body) . '</p></div><br/>';
-
- if(!$this->mod && $config['memcached']['enabled']) {
- $memcached->set($this->memcached_key($index), $built, time() + $config['memcached']['timeout']);
- }
+ global $board, $config;
- return $built;
- }
-
- function memcached_key($index) {
- global $board;
- return 'post_' . md5($this->body) . '_' . ($this->file ? $this->file : '-') . '_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id;
+ return Element('post_reply.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index));
}
};
@@ -502,147 +367,34 @@
return $built;
}
+ public function ratio() {
+ return fraction($this->filex, $this->filey, ':');
+ }
+
public function build($index=false) {
- global $board, $config;
+ global $board, $config, $memcached, $debug;
- if($this->embed) {
- // Embedded video (or something else; doesn't have to be a video really)
- $built =
- // Actual embedding
- $this->embed;
- } elseif(!empty($this->file) && $this->file != 'deleted') {
- // File info
- $built = '<p class="fileinfo">' .
- 'File: <a href="' . $config['uri_img'] . $this->file . '">' . $this->file . '</a> ' .
- '<span class="unimportant">(' .
- ($this->thumb == 'spoiler' ?
- 'Spoiler Image, '
- : '') .
- // Filesize
- format_bytes($this->filesize) .
- // File dimensions
- ($this->filex && $this->filey ?
- ', ' . $this->filex . 'x' . $this->filey
- : '' );
- // Aspect Ratio
- if($config['show_ratio'] && $this->filex && $this->filey) {
- $fraction = fraction($this->filex, $this->filey, ':');
- $built .= ', ' . $fraction;
- }
- if($config['show_filename']) {
- // Filename
- $built .= ', ' .
- (strlen($this->filename) > $config['max_filename_display'] ?
- '<span title="' . $this->filename . '">' .
- substr($this->filename, 0, $config['max_filename_display']) . '&hellip;' .
- '</span>'
- :
- $this->filename
- );
+ if(!$this->mod && $config['memcached']['enabled']) {
+ if($built = $memcached->get($this->memcached_key($index))) {
+ if($config['debug']) {
+ $debug['memcached'][] = $this->memcached_key($index);
+ }
+ return $built;
}
-
- $ext = explode('.', $this->file);
- $ext = $ext[1];
- $built .= ')</span></p>' .
-
- // Thumbnail
- '<a href="' .
- $config['uri_img'] .$this->file .
- '" target="_blank"' .
- ($this->thumb == 'file' ? ' class="file"' : '') .
- '><img src="' .
- ($this->thumb == 'file' ?
- $config['root'] . sprintf($config['file_thumb'], isset($config['file_icons'][$ext]) ? $config['file_icons'][$ext] : $config['file_icons']['default'])
- :
- ($this->thumb == 'spoiler' ?
- $config['root'] . $config['spoiler_image']
- :
- $config['uri_thumb'] . $this->thumb
- )
- ) .
- '" style="width:' . $this->thumbx . 'px;height:' . $this->thumby . 'px;" alt="" /></a>';
- } elseif($this->file == 'deleted') {
- $built = '<img src="' . $config['image_deleted'] . '" alt="" />';
- }
-
- $built .= '<div class="post op"><p class="intro"' . (!$index?' id="' . $this->id . '"':'') . '>';
-
- // Delete
- $built .= '<input type="checkbox" class="delete" name="delete_' . $this->id . '" id="delete_' . $this->id . '" /><label for="delete_' . $this->id . '">';
-
- // Subject
- if(!empty($this->subject))
- $built .= '<span class="subject">' . $this->subject . '</span> ';
- // Email
- if(!empty($this->email))
- $built .= '<a class="email" href="mailto:' . $this->email . '">';
- // Name
- $built .= '<span class="name">' . $this->name . '</span>'
- // Trip
- . (!empty($this->trip) ? ' <span class="trip">'.$this->trip.'</span>':'')
- // End email
- . (!empty($this->email)? '</a>' : '')
- // Capcode
- . (!empty($this->capcode) ? capcode($this->capcode) : '');
-
- // IP Address
-
- if($this->mod && hasPermission($config['mod']['show_ip'], $board['uri'], $this->mod)) {
- $built .= ' [<a style="margin:0;" href="?/IP/' . $this->ip . '">' . $this->ip . '</a>]';
}
- // Date/time
- $built .= ' ' . date($config['post_date'], $this->time);
-
- // End delete
- $built .= '</label>'
-
- // Poster ID
- . ($config['poster_ids'] ?
- ' ID: ' . poster_id($this->ip, $this->id)
- : '')
-
- . ' <a class="post_no"' .
- // JavaScript highlight
- ($index?'':' onclick="highlightReply(' . $this->id . ');"') .
- ' href="' . $this->link() . '">No.</a>' .
- // JavaScript cite
- '<a class="post_no"' . ($index?'':' onclick="citeReply(' . $this->id . ');"') . ' href="' . ($index ? $this->link('q') : 'javascript:void(0);') . '">'.$this->id.'</a>' .
- // Sticky
- ($this->sticky ? '<img class="icon" title="Sticky" src="' . $config['image_sticky'] . '" alt="Sticky" />' : '') .
- // Locked
- ($this->locked ? '<img class="icon" title="Locked" src="' . $config['image_locked'] . '" alt="Locked" />' : '') .
- // [Reply]
- ($index ? '<a href="' . $this->root . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $this->id) . '">[Reply]</a>' : '') .
-
- // Mod controls
- $this->postControls() .
- '</p>';
-
- // Body
- $built .= '<p class="body">' . ($index ? truncate($this->body, $this->link()) : $this->body) . '</p>' .
+ $built = Element('post_thread.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index));
- // Omitted posts
- ($this->omitted || $this->omitted_images? '<span class="omitted">' .
- ($this->omitted ?
- $this->omitted . ' post' . ($this->omitted==1?'':'s') .
- ($this->omitted_images ? ' and ' : '')
- :'') .
- ($this->omitted_images ?
- $this->omitted_images . ' image repl' . ($this->omitted_images==1?'y':'ies')
- :'') .
- ' omitted. Click reply to view.</span>':'') .
-
- // End
- '</div>';
-
- // Replies
- foreach($this->posts as &$post) {
- $built .= $post->build($index);
+ if(!$this->mod && $config['memcached']['enabled']) {
+ $memcached->set($this->memcached_key($index), $built, time() + $config['memcached']['timeout']);
}
- $built .= '<br class="clear"/>' . ($this->hr ? '<hr/>' : '');
return $built;
}
+ function memcached_key($index) {
+ global $board;
+
+ return 'thread_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id;
+ }
};
?>
diff --git a/inc/functions.php b/inc/functions.php
index 647aee1a..6046b681 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -732,16 +732,6 @@
while($th = $query->fetch()) {
$thread = new Thread($th['id'], $th['subject'], $th['email'], $th['name'], $th['trip'], $th['capcode'], $th['body'], $th['time'], $th['thumb'], $th['thumbwidth'], $th['thumbheight'], $th['file'], $th['filewidth'], $th['fileheight'], $th['filesize'], $th['filename'], $th['ip'], $th['sticky'], $th['locked'], $th['embed'], $mod ? '?/' : $config['root'], $mod);
- if($config['memcached']['enabled'] && !$mod) {
- if($built = $memcached->get("threadindex_{$board['uri']}_{$th['id']}")) {
- $body .= $built;
- if($config['debug']) {
- $debug['memcached'][] = "threadindex_{$board['uri']}_{$th['id']}";
- }
- continue;
- }
- }
-
$posts = prepare(sprintf("SELECT * FROM `posts_%s` WHERE `thread` = :id ORDER BY `id` DESC LIMIT :limit", $board['uri']));
$posts->bindValue(':id', $th['id']);
$posts->bindValue(':limit', ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT);
@@ -771,10 +761,6 @@
$built = '<div id="thread_' . $thread->id . '">' . $thread->build(true) . '</div>';
- if($config['memcached']['enabled'] && !$mod) {
- $memcached->set("threadindex_{$board['uri']}_{$th['id']}", $built, time() + $config['memcached']['timeout']);
- }
-
$body .= $built;
}
@@ -1371,8 +1357,9 @@
$id = round($id);
if($config['memcached']['enabled'] && !$mod) {
- // Clear cache for index pages
- $memcached->delete("threadindex_{$board['uri']}_{$id}");
+ // Clear cache
+ $memcached->delete("thread_index_{$board['uri']}_{$id}");
+ $memcached->delete("thread_{$board['uri']}_{$id}");
}
$query = prepare(sprintf("SELECT * FROM `posts_%s` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id`", $board['uri']));
diff --git a/inc/template.php b/inc/template.php
index ed9e01f7..00cdbd36 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -6,8 +6,65 @@
}
require 'contrib/Twig/Autoloader.php';
+
Twig_Autoloader::register();
+ class Tinyboard_Twig_Extension extends Twig_Extension {
+ public function getFilters() {
+ return Array(
+ 'filesize' => new Twig_Filter_Function('format_bytes', Array('needs_environment' => false)),
+ 'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => false)),
+ 'truncate_body' => new Twig_Filter_Function('truncate', array('needs_environment' => false)),
+ 'extension' => new Twig_Filter_Function('twig_extension_filter', array('needs_environment' => false)),
+ 'sprintf' => new Twig_Filter_Function('sprintf', array('needs_environment' => false)),
+ 'capcode' => new Twig_Filter_Function('capcode', array('needs_environment' => false)),
+ 'hasPermission' => new Twig_Filter_Function('twig_hasPermission_filter', array('needs_environment' => false)),
+ 'date' => new Twig_Filter_Function('twig_date_filter', array('needs_environment' => false)),
+ 'poster_id' => new Twig_Filter_Function('poster_id', array('needs_environment' => false)),
+ 'remove_whitespace' => new Twig_Filter_Function('twig_remove_whitespace_filter', array('needs_environment' => false))
+ );
+ }
+ public function getName() {
+ return 'tinyboard';
+ }
+ }
+
+ function twig_remove_whitespace_filter($data) {
+ return preg_replace('/[\t\r\n]/', '', $data);
+ }
+
+ function twig_date_filter($date, $format) {
+ return date($format, $date);
+ }
+
+ function twig_hasPermission_filter($mod, $permission, $board) {
+ return hasPermission($permission, $board, $mod);
+ }
+
+ function twig_extension_filter($value, $case_insensitive = true) {
+ return 'test';
+ $ext = substr($value, strrpos($value, '.') + 1);
+ if($case_insensitive)
+ $ext = strtolower($ext);
+ return $ext;
+ }
+
+ function twig_sprintf_filter( $value, $var) {
+ return sprintf($value, $var);
+ }
+
+ function twig_truncate_filter($value, $length = 30, $preserve = false, $separator = '&hellip;') {
+ if (strlen($value) > $length) {
+ if ($preserve) {
+ if (false !== ($breakpoint = strpos($value, ' ', $length))) {
+ $length = $breakpoint;
+ }
+ }
+ return substr($value, 0, $length) . $separator;
+ }
+ return $value;
+ }
+
$loader = new Twig_Loader_Filesystem($config['dir']['template']);
function Element($templateFile, array $options) {
@@ -28,9 +85,10 @@
$twig = new Twig_Environment($loader, Array(
'autoescape' => false,
- //'cache' => 'cache',
+ 'cache' => 'templates/cache',
'debug' => ($config['debug'] ? true : false),
));
+ $twig->addExtension(new Tinyboard_Twig_Extension());
// Read the template file
if(@file_get_contents("{$config['dir']['template']}/${templateFile}")) {