The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.1KB

  1. <?php
  2. class Twig_Extensions_Extension_Tinyboard extends Twig_Extension
  3. {
  4. /**
  5. * Returns a list of filters to add to the existing list.
  6. *
  7. * @return array An array of filters
  8. */
  9. public function getFilters()
  10. {
  11. return array(
  12. new Twig_SimpleFilter('filesize', 'format_bytes'),
  13. new Twig_SimpleFilter('truncate', 'twig_truncate_filter'),
  14. new Twig_SimpleFilter('truncate_body', 'truncate'),
  15. new Twig_SimpleFilter('truncate_filename', 'twig_filename_truncate_filter'),
  16. new Twig_SimpleFilter('extension', 'twig_extension_filter'),
  17. new Twig_SimpleFilter('sprintf', 'sprintf'),
  18. new Twig_SimpleFilter('capcode', 'capcode'),
  19. new Twig_SimpleFilter('hasPermission', 'twig_hasPermission_filter'),
  20. new Twig_SimpleFilter('date', 'twig_date_filter'),
  21. new Twig_SimpleFilter('poster_id', 'poster_id'),
  22. new Twig_SimpleFilter('remove_whitespace', 'twig_remove_whitespace_filter'),
  23. new Twig_SimpleFilter('count', 'count'),
  24. new Twig_SimpleFilter('ago', 'ago'),
  25. new Twig_SimpleFilter('until', 'until'),
  26. new Twig_SimpleFilter('push', 'twig_push_filter'),
  27. new Twig_SimpleFilter('bidi_cleanup', 'bidi_cleanup'),
  28. new Twig_SimpleFilter('addslashes', 'addslashes'),
  29. );
  30. }
  31. /**
  32. * Returns a list of functions to add to the existing list.
  33. *
  34. * @return array An array of filters
  35. */
  36. public function getFunctions()
  37. {
  38. return array(
  39. new Twig_SimpleFunction('time', 'time'),
  40. new Twig_SimpleFunction('floor', 'floor'),
  41. new Twig_SimpleFunction('timezone', 'twig_timezone_function'),
  42. new Twig_SimpleFunction('hiddenInputs', 'hiddenInputs'),
  43. new Twig_SimpleFunction('hiddenInputsHash', 'hiddenInputsHash'),
  44. new Twig_SimpleFunction('ratio', 'twig_ratio_function'),
  45. new Twig_SimpleFunction('secure_link_confirm', 'twig_secure_link_confirm'),
  46. new Twig_SimpleFunction('secure_link', 'twig_secure_link'),
  47. new Twig_SimpleFunction('link_for', 'link_for')
  48. );
  49. }
  50. /**
  51. * Returns the name of the extension.
  52. *
  53. * @return string The extension name
  54. */
  55. public function getName()
  56. {
  57. return 'tinyboard';
  58. }
  59. }
  60. function twig_timezone_function() {
  61. return 'Z';
  62. }
  63. function twig_push_filter($array, $value) {
  64. array_push($array, $value);
  65. return $array;
  66. }
  67. function twig_remove_whitespace_filter($data) {
  68. return preg_replace('/[\t\r\n]/', '', $data);
  69. }
  70. function twig_date_filter($date, $format) {
  71. return gmstrftime($format, $date);
  72. }
  73. function twig_hasPermission_filter($mod, $permission, $board = null) {
  74. return hasPermission($permission, $board, $mod);
  75. }
  76. function twig_extension_filter($value, $case_insensitive = true) {
  77. $ext = mb_substr($value, mb_strrpos($value, '.') + 1);
  78. if($case_insensitive)
  79. $ext = mb_strtolower($ext);
  80. return $ext;
  81. }
  82. function twig_sprintf_filter( $value, $var) {
  83. return sprintf($value, $var);
  84. }
  85. function twig_truncate_filter($value, $length = 30, $preserve = false, $separator = '…') {
  86. if (mb_strlen($value) > $length) {
  87. if ($preserve) {
  88. if (false !== ($breakpoint = mb_strpos($value, ' ', $length))) {
  89. $length = $breakpoint;
  90. }
  91. }
  92. return mb_substr($value, 0, $length) . $separator;
  93. }
  94. return $value;
  95. }
  96. function twig_filename_truncate_filter($value, $length = 30, $separator = '…') {
  97. if (mb_strlen($value) > $length) {
  98. $value = strrev($value);
  99. $array = array_reverse(explode(".", $value, 2));
  100. $array = array_map("strrev", $array);
  101. $filename = &$array[0];
  102. $extension = isset($array[1]) ? $array[1] : false;
  103. $filename = mb_substr($filename, 0, $length - ($extension ? mb_strlen($extension) + 1 : 0)) . $separator;
  104. return implode(".", $array);
  105. }
  106. return $value;
  107. }
  108. function twig_ratio_function($w, $h) {
  109. return fraction($w, $h, ':');
  110. }
  111. function twig_secure_link_confirm($text, $title, $confirm_message, $href) {
  112. global $config;
  113. return '<a onclick="if (event.which==2) return true;if (confirm(\'' . htmlentities(addslashes($confirm_message)) . '\')) document.location=\'?/' . htmlspecialchars(addslashes($href . '/' . make_secure_link_token($href))) . '\';return false;" title="' . htmlentities($title) . '" href="?/' . $href . '">' . $text . '</a>';
  114. }
  115. function twig_secure_link($href) {
  116. return $href . '/' . make_secure_link_token($href);
  117. }