/inc/lib/Twig/Extensions/Extension/Tinyboard.php

http://github.com/savetheinternet/Tinyboard · PHP · 130 lines · 98 code · 17 blank · 15 comment · 5 complexity · 84968f132d97556caebed7f25ba0b329 MD5 · raw file

  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. );
  45. }
  46. /**
  47. * Returns the name of the extension.
  48. *
  49. * @return string The extension name
  50. */
  51. public function getName()
  52. {
  53. return 'tinyboard';
  54. }
  55. }
  56. function twig_timezone_function() {
  57. return 'Z';
  58. }
  59. function twig_push_filter($array, $value) {
  60. array_push($array, $value);
  61. return $array;
  62. }
  63. function twig_remove_whitespace_filter($data) {
  64. return preg_replace('/[\t\r\n]/', '', $data);
  65. }
  66. function twig_date_filter($date, $format) {
  67. return gmstrftime($format, $date);
  68. }
  69. function twig_hasPermission_filter($mod, $permission, $board = null) {
  70. return hasPermission($permission, $board, $mod);
  71. }
  72. function twig_extension_filter($value, $case_insensitive = true) {
  73. $ext = mb_substr($value, mb_strrpos($value, '.') + 1);
  74. if($case_insensitive)
  75. $ext = mb_strtolower($ext);
  76. return $ext;
  77. }
  78. function twig_sprintf_filter( $value, $var) {
  79. return sprintf($value, $var);
  80. }
  81. function twig_truncate_filter($value, $length = 30, $preserve = false, $separator = '…') {
  82. if (mb_strlen($value) > $length) {
  83. if ($preserve) {
  84. if (false !== ($breakpoint = mb_strpos($value, ' ', $length))) {
  85. $length = $breakpoint;
  86. }
  87. }
  88. return mb_substr($value, 0, $length) . $separator;
  89. }
  90. return $value;
  91. }
  92. function twig_filename_truncate_filter($value, $length = 30, $separator = '…') {
  93. if (mb_strlen($value) > $length) {
  94. $value = strrev($value);
  95. $array = array_reverse(explode(".", $value, 2));
  96. $array = array_map("strrev", $array);
  97. $filename = &$array[0];
  98. $extension = isset($array[1]) ? $array[1] : false;
  99. $filename = mb_substr($filename, 0, $length - ($extension ? mb_strlen($extension) + 1 : 0)) . $separator;
  100. return implode(".", $array);
  101. }
  102. return $value;
  103. }
  104. function twig_ratio_function($w, $h) {
  105. return fraction($w, $h, ':');
  106. }
  107. function twig_secure_link_confirm($text, $title, $confirm_message, $href) {
  108. global $config;
  109. 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>';
  110. }
  111. function twig_secure_link($href) {
  112. return $href . '/' . make_secure_link_token($href);
  113. }