From 4cfbec05042c556f06a42f1f3d647a4a4adb353c Mon Sep 17 00:00:00 2001 From: towards-a-new-leftypol Date: Wed, 2 Aug 2023 20:04:13 -0400 Subject: [PATCH] Fix Tinyboard Twig extension date filter --- composer.json | 3 +- composer.lock | 46 ++++++++- inc/lib/twig/Extensions/Tinyboard.php | 142 ++++++++++++++++++++++++++ inc/template.php | 3 +- templates/post_thread.html | 6 +- 5 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 inc/lib/twig/Extensions/Tinyboard.php diff --git a/composer.json b/composer.json index 7c4478df..bcf75a06 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,7 @@ "guzzlehttp/guzzle": "^7.0", "lifo/ip": "^1.1", "twig/twig": "^3.0", - "phpmyadmin/twig-i18n-extension": "^4.0" + "phpmyadmin/twig-i18n-extension": "^4.0", + "php81_bc/strftime": "^0.5.0" } } diff --git a/composer.lock b/composer.lock index 899eabf5..4d7ea502 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3c18ea3d0277087541385610b2db992b", + "content-hash": "fd9d40f38b577e52a0475b62da8bd994", "packages": [ { "name": "guzzlehttp/guzzle", @@ -604,6 +604,50 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "php81_bc/strftime", + "version": "0.5.0", + "source": { + "type": "git", + "url": "https://github.com/alphp/strftime.git", + "reference": "4c1b56eaae4bb3f02f994ba47c2e5a225378e62f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/alphp/strftime/zipball/4c1b56eaae4bb3f02f994ba47c2e5a225378e62f", + "reference": "4c1b56eaae4bb3f02f994ba47c2e5a225378e62f", + "shasum": "" + }, + "require": { + "ext-intl": "*", + "php": ">=7.1.0" + }, + "require-dev": { + "phpunit/phpunit": "@stable" + }, + "type": "library", + "autoload": { + "files": [ + "src/php-8.1-strftime.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fernando Herrero", + "homepage": "https://github.com/alphp/strftime/graphs/contributors" + } + ], + "description": "Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible)", + "support": { + "issues": "https://github.com/alphp/strftime/issues", + "source": "https://github.com/alphp/strftime" + }, + "time": "2022-04-10T22:31:16+00:00" + }, { "name": "phpmyadmin/twig-i18n-extension", "version": "v4.0.1", diff --git a/inc/lib/twig/Extensions/Tinyboard.php b/inc/lib/twig/Extensions/Tinyboard.php new file mode 100644 index 00000000..27015c8e --- /dev/null +++ b/inc/lib/twig/Extensions/Tinyboard.php @@ -0,0 +1,142 @@ + $length) { + if ($preserve) { + if (false !== ($breakpoint = mb_strpos($value, ' ', $length))) { + $length = $breakpoint; + } + } + return mb_substr($value, 0, $length) . $separator; + } + return $value; +} + +function twig_filename_truncate_filter($value, $length = 30, $separator = '…') { + if (mb_strlen($value) > $length) { + $value = strrev($value); + $array = array_reverse(explode(".", $value, 2)); + $array = array_map("strrev", $array); + + $filename = &$array[0]; + $extension = isset($array[1]) ? $array[1] : false; + + $filename = mb_substr($filename, 0, $length - ($extension ? mb_strlen($extension) + 1 : 0)) . $separator; + + return implode(".", $array); + } + return $value; +} + +function twig_ratio_function($w, $h) { + return fraction($w, $h, ':'); +} +function twig_secure_link_confirm($text, $title, $confirm_message, $href) { + global $config; + + return '' . $text . ''; +} +function twig_secure_link($href) { + return $href . '/' . make_secure_link_token($href); +} diff --git a/inc/template.php b/inc/template.php index 6689d83e..45596d18 100644 --- a/inc/template.php +++ b/inc/template.php @@ -7,6 +7,7 @@ defined('TINYBOARD') or exit; require_once 'vendor/autoload.php'; +require_once 'inc/lib/twig/Extensions/Tinyboard.php'; use PhpMyAdmin\Twig\Extensions\I18nExtension; @@ -15,7 +16,6 @@ $twig = false; function load_twig() { global $twig, $config; - $loader = new Twig\Loader\FilesystemLoader($config['dir']['template']); $twig = new Twig\Environment($loader, array( 'autoescape' => false, @@ -25,6 +25,7 @@ function load_twig() { )); $twig->addExtension(new I18nExtension()); + $twig->addExtension(new TinyboardExtension()); } function Element($templateFile, array $options) { diff --git a/templates/post_thread.html b/templates/post_thread.html index 448bbd72..3914675d 100644 --- a/templates/post_thread.html +++ b/templates/post_thread.html @@ -1,4 +1,4 @@ -{% filter remove_whitespace %} +{% apply remove_whitespace %} {# tabs and new lines will be ignored #}
@@ -58,7 +58,7 @@ {% include 'post/post_controls.html' %}

- {% endfilter %}{% if index %}{{ post.body|truncate_body(post.link) }}{% else %}{{ post.body }}{% endif %}{% filter remove_whitespace %} + {% endapply %}{% if index %}{{ post.body|truncate_body(post.link) }}{% else %}{{ post.body }}{% endif %}{% apply remove_whitespace %} {% if post.modifiers['ban message'] %} {{ config.mod.ban_message|sprintf(post.modifiers['ban message']) }} {% endif %} @@ -89,7 +89,7 @@ {% endif %} {% if not index %} {% endif %} -
{% endfilter %} +
{% endapply %} {% set iparray = [post.ip] %} {% set hr = post.hr %} {% for post in post.posts %}