The version of vichan running on lainchan.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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