2011-10-11 06:49:14 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Twig_Extensions_Extension_Tinyboard extends Twig_Extension
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns a list of filters to add to the existing list.
|
|
|
|
*
|
|
|
|
* @return array An array of filters
|
|
|
|
*/
|
|
|
|
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)),
|
2011-11-16 23:24:39 -05:00
|
|
|
'remove_whitespace' => new Twig_Filter_Function('twig_remove_whitespace_filter', array('needs_environment' => false)),
|
2011-11-23 00:12:19 -05:00
|
|
|
'count' => new Twig_Filter_Function('count', array('needs_environment' => false)),
|
|
|
|
'until' => new Twig_Filter_Function('until', array('needs_environment' => false))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of functions to add to the existing list.
|
|
|
|
*
|
|
|
|
* @return array An array of filters
|
|
|
|
*/
|
|
|
|
public function getFunctions()
|
|
|
|
{
|
|
|
|
return Array(
|
2012-01-19 10:37:53 -05:00
|
|
|
'time' => new Twig_Filter_Function('time', array('needs_environment' => false)),
|
2012-02-19 07:38:55 -05:00
|
|
|
'timezone' => new Twig_Filter_Function('twig_timezone_function', array('needs_environment' => false)),
|
2012-04-12 03:20:49 -04:00
|
|
|
'hiddenInputs' => new Twig_Filter_Function('hiddenInputs', array('needs_environment' => false)),
|
|
|
|
'hiddenInputsHash' => new Twig_Filter_Function('hiddenInputsHash', array('needs_environment' => false))
|
2011-10-11 06:49:14 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the extension.
|
|
|
|
*
|
|
|
|
* @return string The extension name
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'tinyboard';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-19 07:38:55 -05:00
|
|
|
function twig_timezone_function() {
|
|
|
|
// there's probably a much easier way of doing this
|
|
|
|
return sprintf("%s%02d", ($hr = (int)floor(($tz = date('Z')) / 3600)) > 0 ? '+' : '-', abs($hr)) . ':' . sprintf("%02d", (($tz / 3600) - $hr) * 60);
|
|
|
|
}
|
|
|
|
|
2011-10-11 06:49:14 -04:00
|
|
|
function twig_remove_whitespace_filter($data) {
|
|
|
|
return preg_replace('/[\t\r\n]/', '', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function twig_date_filter($date, $format) {
|
2011-11-19 12:07:17 -05:00
|
|
|
return strftime($format, $date);
|
2011-10-11 06:49:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function twig_hasPermission_filter($mod, $permission, $board) {
|
|
|
|
return hasPermission($permission, $board, $mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
function twig_extension_filter($value, $case_insensitive = true) {
|
|
|
|
$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 = '…') {
|
2011-12-07 01:47:36 -05:00
|
|
|
if (mb_strlen($value) > $length) {
|
2011-10-11 06:49:14 -04:00
|
|
|
if ($preserve) {
|
|
|
|
if (false !== ($breakpoint = strpos($value, ' ', $length))) {
|
|
|
|
$length = $breakpoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return substr($value, 0, $length) . $separator;
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|