Core.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. <?php
  2. if (!defined('ENT_SUBSTITUTE')) {
  3. // use 0 as hhvm does not support several flags yet
  4. define('ENT_SUBSTITUTE', 0);
  5. }
  6. /*
  7. * This file is part of Twig.
  8. *
  9. * (c) 2009 Fabien Potencier
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. class Twig_Extension_Core extends Twig_Extension
  15. {
  16. protected $dateFormats = array('F j, Y H:i', '%d days');
  17. protected $numberFormat = array(0, '.', ',');
  18. protected $timezone = null;
  19. protected $escapers = array();
  20. /**
  21. * Defines a new escaper to be used via the escape filter.
  22. *
  23. * @param string $strategy The strategy name that should be used as a strategy in the escape call
  24. * @param callable $callable A valid PHP callable
  25. */
  26. public function setEscaper($strategy, $callable)
  27. {
  28. $this->escapers[$strategy] = $callable;
  29. }
  30. /**
  31. * Gets all defined escapers.
  32. *
  33. * @return array An array of escapers
  34. */
  35. public function getEscapers()
  36. {
  37. return $this->escapers;
  38. }
  39. /**
  40. * Sets the default format to be used by the date filter.
  41. *
  42. * @param string $format The default date format string
  43. * @param string $dateIntervalFormat The default date interval format string
  44. */
  45. public function setDateFormat($format = null, $dateIntervalFormat = null)
  46. {
  47. if (null !== $format) {
  48. $this->dateFormats[0] = $format;
  49. }
  50. if (null !== $dateIntervalFormat) {
  51. $this->dateFormats[1] = $dateIntervalFormat;
  52. }
  53. }
  54. /**
  55. * Gets the default format to be used by the date filter.
  56. *
  57. * @return array The default date format string and the default date interval format string
  58. */
  59. public function getDateFormat()
  60. {
  61. return $this->dateFormats;
  62. }
  63. /**
  64. * Sets the default timezone to be used by the date filter.
  65. *
  66. * @param DateTimeZone|string $timezone The default timezone string or a DateTimeZone object
  67. */
  68. public function setTimezone($timezone)
  69. {
  70. $this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
  71. }
  72. /**
  73. * Gets the default timezone to be used by the date filter.
  74. *
  75. * @return DateTimeZone The default timezone currently in use
  76. */
  77. public function getTimezone()
  78. {
  79. if (null === $this->timezone) {
  80. $this->timezone = new DateTimeZone(date_default_timezone_get());
  81. }
  82. return $this->timezone;
  83. }
  84. /**
  85. * Sets the default format to be used by the number_format filter.
  86. *
  87. * @param int $decimal The number of decimal places to use.
  88. * @param string $decimalPoint The character(s) to use for the decimal point.
  89. * @param string $thousandSep The character(s) to use for the thousands separator.
  90. */
  91. public function setNumberFormat($decimal, $decimalPoint, $thousandSep)
  92. {
  93. $this->numberFormat = array($decimal, $decimalPoint, $thousandSep);
  94. }
  95. /**
  96. * Get the default format used by the number_format filter.
  97. *
  98. * @return array The arguments for number_format()
  99. */
  100. public function getNumberFormat()
  101. {
  102. return $this->numberFormat;
  103. }
  104. public function getTokenParsers()
  105. {
  106. return array(
  107. new Twig_TokenParser_For(),
  108. new Twig_TokenParser_If(),
  109. new Twig_TokenParser_Extends(),
  110. new Twig_TokenParser_Include(),
  111. new Twig_TokenParser_Block(),
  112. new Twig_TokenParser_Use(),
  113. new Twig_TokenParser_Filter(),
  114. new Twig_TokenParser_Macro(),
  115. new Twig_TokenParser_Import(),
  116. new Twig_TokenParser_From(),
  117. new Twig_TokenParser_Set(),
  118. new Twig_TokenParser_Spaceless(),
  119. new Twig_TokenParser_Flush(),
  120. new Twig_TokenParser_Do(),
  121. new Twig_TokenParser_Embed(),
  122. );
  123. }
  124. public function getFilters()
  125. {
  126. $filters = array(
  127. // formatting filters
  128. new Twig_SimpleFilter('date', 'twig_date_format_filter', array('needs_environment' => true)),
  129. new Twig_SimpleFilter('date_modify', 'twig_date_modify_filter', array('needs_environment' => true)),
  130. new Twig_SimpleFilter('format', 'sprintf'),
  131. new Twig_SimpleFilter('replace', 'twig_replace_filter'),
  132. new Twig_SimpleFilter('number_format', 'twig_number_format_filter', array('needs_environment' => true)),
  133. new Twig_SimpleFilter('abs', 'abs'),
  134. new Twig_SimpleFilter('round', 'twig_round'),
  135. // encoding
  136. new Twig_SimpleFilter('url_encode', 'twig_urlencode_filter'),
  137. new Twig_SimpleFilter('json_encode', 'twig_jsonencode_filter'),
  138. new Twig_SimpleFilter('convert_encoding', 'twig_convert_encoding'),
  139. // string filters
  140. new Twig_SimpleFilter('title', 'twig_title_string_filter', array('needs_environment' => true)),
  141. new Twig_SimpleFilter('capitalize', 'twig_capitalize_string_filter', array('needs_environment' => true)),
  142. new Twig_SimpleFilter('upper', 'strtoupper'),
  143. new Twig_SimpleFilter('lower', 'strtolower'),
  144. new Twig_SimpleFilter('striptags', 'strip_tags'),
  145. new Twig_SimpleFilter('trim', 'trim'),
  146. new Twig_SimpleFilter('nl2br', 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))),
  147. // array helpers
  148. new Twig_SimpleFilter('join', 'twig_join_filter'),
  149. new Twig_SimpleFilter('split', 'twig_split_filter', array('needs_environment' => true)),
  150. new Twig_SimpleFilter('sort', 'twig_sort_filter'),
  151. new Twig_SimpleFilter('merge', 'twig_array_merge'),
  152. new Twig_SimpleFilter('batch', 'twig_array_batch'),
  153. // string/array filters
  154. new Twig_SimpleFilter('reverse', 'twig_reverse_filter', array('needs_environment' => true)),
  155. new Twig_SimpleFilter('length', 'twig_length_filter', array('needs_environment' => true)),
  156. new Twig_SimpleFilter('slice', 'twig_slice', array('needs_environment' => true)),
  157. new Twig_SimpleFilter('first', 'twig_first', array('needs_environment' => true)),
  158. new Twig_SimpleFilter('last', 'twig_last', array('needs_environment' => true)),
  159. // iteration and runtime
  160. new Twig_SimpleFilter('default', '_twig_default_filter', array('node_class' => 'Twig_Node_Expression_Filter_Default')),
  161. new Twig_SimpleFilter('keys', 'twig_get_array_keys_filter'),
  162. // escaping
  163. new Twig_SimpleFilter('escape', 'twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')),
  164. new Twig_SimpleFilter('e', 'twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')),
  165. );
  166. if (function_exists('mb_get_info')) {
  167. $filters[] = new Twig_SimpleFilter('upper', 'twig_upper_filter', array('needs_environment' => true));
  168. $filters[] = new Twig_SimpleFilter('lower', 'twig_lower_filter', array('needs_environment' => true));
  169. }
  170. return $filters;
  171. }
  172. public function getFunctions()
  173. {
  174. return array(
  175. new Twig_SimpleFunction('max', 'max'),
  176. new Twig_SimpleFunction('min', 'min'),
  177. new Twig_SimpleFunction('range', 'range'),
  178. new Twig_SimpleFunction('constant', 'twig_constant'),
  179. new Twig_SimpleFunction('cycle', 'twig_cycle'),
  180. new Twig_SimpleFunction('random', 'twig_random', array('needs_environment' => true)),
  181. new Twig_SimpleFunction('date', 'twig_date_converter', array('needs_environment' => true)),
  182. new Twig_SimpleFunction('include', 'twig_include', array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))),
  183. new Twig_SimpleFunction('source', 'twig_source', array('needs_environment' => true, 'is_safe' => array('all'))),
  184. );
  185. }
  186. public function getTests()
  187. {
  188. return array(
  189. new Twig_SimpleTest('even', null, array('node_class' => 'Twig_Node_Expression_Test_Even')),
  190. new Twig_SimpleTest('odd', null, array('node_class' => 'Twig_Node_Expression_Test_Odd')),
  191. new Twig_SimpleTest('defined', null, array('node_class' => 'Twig_Node_Expression_Test_Defined')),
  192. new Twig_SimpleTest('sameas', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas', 'deprecated' => '1.21', 'alternative' => 'same as')),
  193. new Twig_SimpleTest('same as', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')),
  194. new Twig_SimpleTest('none', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
  195. new Twig_SimpleTest('null', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
  196. new Twig_SimpleTest('divisibleby', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby', 'deprecated' => '1.21', 'alternative' => 'divisible by')),
  197. new Twig_SimpleTest('divisible by', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')),
  198. new Twig_SimpleTest('constant', null, array('node_class' => 'Twig_Node_Expression_Test_Constant')),
  199. new Twig_SimpleTest('empty', 'twig_test_empty'),
  200. new Twig_SimpleTest('iterable', 'twig_test_iterable'),
  201. );
  202. }
  203. public function getOperators()
  204. {
  205. return array(
  206. array(
  207. 'not' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'),
  208. '-' => array('precedence' => 500, 'class' => 'Twig_Node_Expression_Unary_Neg'),
  209. '+' => array('precedence' => 500, 'class' => 'Twig_Node_Expression_Unary_Pos'),
  210. ),
  211. array(
  212. 'or' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  213. 'and' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  214. 'b-or' => array('precedence' => 16, 'class' => 'Twig_Node_Expression_Binary_BitwiseOr', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  215. 'b-xor' => array('precedence' => 17, 'class' => 'Twig_Node_Expression_Binary_BitwiseXor', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  216. 'b-and' => array('precedence' => 18, 'class' => 'Twig_Node_Expression_Binary_BitwiseAnd', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  217. '==' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Equal', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  218. '!=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  219. '<' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Less', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  220. '>' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Greater', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  221. '>=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  222. '<=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  223. 'not in' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotIn', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  224. 'in' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_In', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  225. 'matches' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Matches', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  226. 'starts with' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_StartsWith', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  227. 'ends with' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_EndsWith', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  228. '..' => array('precedence' => 25, 'class' => 'Twig_Node_Expression_Binary_Range', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  229. '+' => array('precedence' => 30, 'class' => 'Twig_Node_Expression_Binary_Add', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  230. '-' => array('precedence' => 30, 'class' => 'Twig_Node_Expression_Binary_Sub', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  231. '~' => array('precedence' => 40, 'class' => 'Twig_Node_Expression_Binary_Concat', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  232. '*' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mul', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  233. '/' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Div', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  234. '//' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_FloorDiv', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  235. '%' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mod', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  236. 'is' => array('precedence' => 100, 'callable' => array($this, 'parseTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  237. 'is not' => array('precedence' => 100, 'callable' => array($this, 'parseNotTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
  238. '**' => array('precedence' => 200, 'class' => 'Twig_Node_Expression_Binary_Power', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
  239. '??' => array('precedence' => 300, 'class' => 'Twig_Node_Expression_NullCoalesce', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
  240. ),
  241. );
  242. }
  243. public function parseNotTestExpression(Twig_Parser $parser, Twig_NodeInterface $node)
  244. {
  245. return new Twig_Node_Expression_Unary_Not($this->parseTestExpression($parser, $node), $parser->getCurrentToken()->getLine());
  246. }
  247. public function parseTestExpression(Twig_Parser $parser, Twig_NodeInterface $node)
  248. {
  249. $stream = $parser->getStream();
  250. list($name, $test) = $this->getTest($parser, $node->getLine());
  251. if ($test instanceof Twig_SimpleTest && $test->isDeprecated()) {
  252. $message = sprintf('Twig Test "%s" is deprecated', $name);
  253. if (!is_bool($test->getDeprecatedVersion())) {
  254. $message .= sprintf(' since version %s', $test->getDeprecatedVersion());
  255. }
  256. if ($test->getAlternative()) {
  257. $message .= sprintf('. Use "%s" instead', $test->getAlternative());
  258. }
  259. $message .= sprintf(' in %s at line %d.', $stream->getFilename(), $stream->getCurrent()->getLine());
  260. @trigger_error($message, E_USER_DEPRECATED);
  261. }
  262. $class = $this->getTestNodeClass($parser, $test);
  263. $arguments = null;
  264. if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
  265. $arguments = $parser->getExpressionParser()->parseArguments(true);
  266. }
  267. return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
  268. }
  269. protected function getTest(Twig_Parser $parser, $line)
  270. {
  271. $stream = $parser->getStream();
  272. $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
  273. $env = $parser->getEnvironment();
  274. if ($test = $env->getTest($name)) {
  275. return array($name, $test);
  276. }
  277. if ($stream->test(Twig_Token::NAME_TYPE)) {
  278. // try 2-words tests
  279. $name = $name.' '.$parser->getCurrentToken()->getValue();
  280. if ($test = $env->getTest($name)) {
  281. $parser->getStream()->next();
  282. return array($name, $test);
  283. }
  284. }
  285. $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $parser->getFilename());
  286. $e->addSuggestions($name, array_keys($env->getTests()));
  287. throw $e;
  288. }
  289. protected function getTestNodeClass(Twig_Parser $parser, $test)
  290. {
  291. if ($test instanceof Twig_SimpleTest) {
  292. return $test->getNodeClass();
  293. }
  294. return $test instanceof Twig_Test_Node ? $test->getClass() : 'Twig_Node_Expression_Test';
  295. }
  296. public function getName()
  297. {
  298. return 'core';
  299. }
  300. }
  301. /**
  302. * Cycles over a value.
  303. *
  304. * @param ArrayAccess|array $values An array or an ArrayAccess instance
  305. * @param int $position The cycle position
  306. *
  307. * @return string The next value in the cycle
  308. */
  309. function twig_cycle($values, $position)
  310. {
  311. if (!is_array($values) && !$values instanceof ArrayAccess) {
  312. return $values;
  313. }
  314. return $values[$position % count($values)];
  315. }
  316. /**
  317. * Returns a random value depending on the supplied parameter type:
  318. * - a random item from a Traversable or array
  319. * - a random character from a string
  320. * - a random integer between 0 and the integer parameter.
  321. *
  322. * @param Twig_Environment $env A Twig_Environment instance
  323. * @param Traversable|array|int|string $values The values to pick a random item from
  324. *
  325. * @throws Twig_Error_Runtime When $values is an empty array (does not apply to an empty string which is returned as is).
  326. *
  327. * @return mixed A random value from the given sequence
  328. */
  329. function twig_random(Twig_Environment $env, $values = null)
  330. {
  331. if (null === $values) {
  332. return mt_rand();
  333. }
  334. if (is_int($values) || is_float($values)) {
  335. return $values < 0 ? mt_rand($values, 0) : mt_rand(0, $values);
  336. }
  337. if ($values instanceof Traversable) {
  338. $values = iterator_to_array($values);
  339. } elseif (is_string($values)) {
  340. if ('' === $values) {
  341. return '';
  342. }
  343. if (null !== $charset = $env->getCharset()) {
  344. if ('UTF-8' !== $charset) {
  345. $values = twig_convert_encoding($values, 'UTF-8', $charset);
  346. }
  347. // unicode version of str_split()
  348. // split at all positions, but not after the start and not before the end
  349. $values = preg_split('/(?<!^)(?!$)/u', $values);
  350. if ('UTF-8' !== $charset) {
  351. foreach ($values as $i => $value) {
  352. $values[$i] = twig_convert_encoding($value, $charset, 'UTF-8');
  353. }
  354. }
  355. } else {
  356. return $values[mt_rand(0, strlen($values) - 1)];
  357. }
  358. }
  359. if (!is_array($values)) {
  360. return $values;
  361. }
  362. if (0 === count($values)) {
  363. throw new Twig_Error_Runtime('The random function cannot pick from an empty array.');
  364. }
  365. return $values[array_rand($values, 1)];
  366. }
  367. /**
  368. * Converts a date to the given format.
  369. *
  370. * <pre>
  371. * {{ post.published_at|date("m/d/Y") }}
  372. * </pre>
  373. *
  374. * @param Twig_Environment $env A Twig_Environment instance
  375. * @param DateTime|DateTimeInterface|DateInterval|string $date A date
  376. * @param string|null $format The target format, null to use the default
  377. * @param DateTimeZone|string|null|false $timezone The target timezone, null to use the default, false to leave unchanged
  378. *
  379. * @return string The formatted date
  380. */
  381. function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $timezone = null)
  382. {
  383. if (null === $format) {
  384. $formats = $env->getExtension('core')->getDateFormat();
  385. $format = $date instanceof DateInterval ? $formats[1] : $formats[0];
  386. }
  387. if ($date instanceof DateInterval) {
  388. return $date->format($format);
  389. }
  390. return twig_date_converter($env, $date, $timezone)->format($format);
  391. }
  392. /**
  393. * Returns a new date object modified.
  394. *
  395. * <pre>
  396. * {{ post.published_at|date_modify("-1day")|date("m/d/Y") }}
  397. * </pre>
  398. *
  399. * @param Twig_Environment $env A Twig_Environment instance
  400. * @param DateTime|string $date A date
  401. * @param string $modifier A modifier string
  402. *
  403. * @return DateTime A new date object
  404. */
  405. function twig_date_modify_filter(Twig_Environment $env, $date, $modifier)
  406. {
  407. $date = twig_date_converter($env, $date, false);
  408. $resultDate = $date->modify($modifier);
  409. // This is a hack to ensure PHP 5.2 support and support for DateTimeImmutable
  410. // DateTime::modify does not return the modified DateTime object < 5.3.0
  411. // and DateTimeImmutable does not modify $date.
  412. return null === $resultDate ? $date : $resultDate;
  413. }
  414. /**
  415. * Converts an input to a DateTime instance.
  416. *
  417. * <pre>
  418. * {% if date(user.created_at) < date('+2days') %}
  419. * {# do something #}
  420. * {% endif %}
  421. * </pre>
  422. *
  423. * @param Twig_Environment $env A Twig_Environment instance
  424. * @param DateTime|DateTimeInterface|string|null $date A date
  425. * @param DateTimeZone|string|null|false $timezone The target timezone, null to use the default, false to leave unchanged
  426. *
  427. * @return DateTime A DateTime instance
  428. */
  429. function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null)
  430. {
  431. // determine the timezone
  432. if (false !== $timezone) {
  433. if (null === $timezone) {
  434. $timezone = $env->getExtension('core')->getTimezone();
  435. } elseif (!$timezone instanceof DateTimeZone) {
  436. $timezone = new DateTimeZone($timezone);
  437. }
  438. }
  439. // immutable dates
  440. if ($date instanceof DateTimeImmutable) {
  441. return false !== $timezone ? $date->setTimezone($timezone) : $date;
  442. }
  443. if ($date instanceof DateTime || $date instanceof DateTimeInterface) {
  444. $date = clone $date;
  445. if (false !== $timezone) {
  446. $date->setTimezone($timezone);
  447. }
  448. return $date;
  449. }
  450. if (null === $date || 'now' === $date) {
  451. return new DateTime($date, false !== $timezone ? $timezone : $env->getExtension('core')->getTimezone());
  452. }
  453. $asString = (string) $date;
  454. if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) {
  455. $date = new DateTime('@'.$date);
  456. } else {
  457. $date = new DateTime($date, $env->getExtension('core')->getTimezone());
  458. }
  459. if (false !== $timezone) {
  460. $date->setTimezone($timezone);
  461. }
  462. return $date;
  463. }
  464. /**
  465. * Replaces strings within a string.
  466. *
  467. * @param string $str String to replace in
  468. * @param array|Traversable $from Replace values
  469. * @param string|null $to Replace to, deprecated (@see http://php.net/manual/en/function.strtr.php)
  470. *
  471. * @return string
  472. */
  473. function twig_replace_filter($str, $from, $to = null)
  474. {
  475. if ($from instanceof Traversable) {
  476. $from = iterator_to_array($from);
  477. } elseif (is_string($from) && is_string($to)) {
  478. @trigger_error('Using "replace" with character by character replacement is deprecated since version 1.22 and will be removed in Twig 2.0', E_USER_DEPRECATED);
  479. return strtr($str, $from, $to);
  480. } elseif (!is_array($from)) {
  481. throw new Twig_Error_Runtime(sprintf('The "replace" filter expects an array or "Traversable" as replace values, got "%s".',is_object($from) ? get_class($from) : gettype($from)));
  482. }
  483. return strtr($str, $from);
  484. }
  485. /**
  486. * Rounds a number.
  487. *
  488. * @param int|float $value The value to round
  489. * @param int|float $precision The rounding precision
  490. * @param string $method The method to use for rounding
  491. *
  492. * @return int|float The rounded number
  493. */
  494. function twig_round($value, $precision = 0, $method = 'common')
  495. {
  496. if ('common' == $method) {
  497. return round($value, $precision);
  498. }
  499. if ('ceil' != $method && 'floor' != $method) {
  500. throw new Twig_Error_Runtime('The round filter only supports the "common", "ceil", and "floor" methods.');
  501. }
  502. return $method($value * pow(10, $precision)) / pow(10, $precision);
  503. }
  504. /**
  505. * Number format filter.
  506. *
  507. * All of the formatting options can be left null, in that case the defaults will
  508. * be used. Supplying any of the parameters will override the defaults set in the
  509. * environment object.
  510. *
  511. * @param Twig_Environment $env A Twig_Environment instance
  512. * @param mixed $number A float/int/string of the number to format
  513. * @param int $decimal The number of decimal points to display.
  514. * @param string $decimalPoint The character(s) to use for the decimal point.
  515. * @param string $thousandSep The character(s) to use for the thousands separator.
  516. *
  517. * @return string The formatted number
  518. */
  519. function twig_number_format_filter(Twig_Environment $env, $number, $decimal = null, $decimalPoint = null, $thousandSep = null)
  520. {
  521. $defaults = $env->getExtension('core')->getNumberFormat();
  522. if (null === $decimal) {
  523. $decimal = $defaults[0];
  524. }
  525. if (null === $decimalPoint) {
  526. $decimalPoint = $defaults[1];
  527. }
  528. if (null === $thousandSep) {
  529. $thousandSep = $defaults[2];
  530. }
  531. return number_format((float) $number, $decimal, $decimalPoint, $thousandSep);
  532. }
  533. /**
  534. * URL encodes (RFC 3986) a string as a path segment or an array as a query string.
  535. *
  536. * @param string|array $url A URL or an array of query parameters
  537. *
  538. * @return string The URL encoded value
  539. */
  540. function twig_urlencode_filter($url)
  541. {
  542. if (is_array($url)) {
  543. if (defined('PHP_QUERY_RFC3986')) {
  544. return http_build_query($url, '', '&', PHP_QUERY_RFC3986);
  545. }
  546. return http_build_query($url, '', '&');
  547. }
  548. return rawurlencode($url);
  549. }
  550. if (PHP_VERSION_ID < 50300) {
  551. /**
  552. * JSON encodes a variable.
  553. *
  554. * @param mixed $value The value to encode.
  555. * @param int $options Not used on PHP 5.2.x
  556. *
  557. * @return mixed The JSON encoded value
  558. */
  559. function twig_jsonencode_filter($value, $options = 0)
  560. {
  561. if ($value instanceof Twig_Markup) {
  562. $value = (string) $value;
  563. } elseif (is_array($value)) {
  564. array_walk_recursive($value, '_twig_markup2string');
  565. }
  566. return json_encode($value);
  567. }
  568. } else {
  569. /**
  570. * JSON encodes a variable.
  571. *
  572. * @param mixed $value The value to encode.
  573. * @param int $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
  574. *
  575. * @return mixed The JSON encoded value
  576. */
  577. function twig_jsonencode_filter($value, $options = 0)
  578. {
  579. if ($value instanceof Twig_Markup) {
  580. $value = (string) $value;
  581. } elseif (is_array($value)) {
  582. array_walk_recursive($value, '_twig_markup2string');
  583. }
  584. return json_encode($value, $options);
  585. }
  586. }
  587. function _twig_markup2string(&$value)
  588. {
  589. if ($value instanceof Twig_Markup) {
  590. $value = (string) $value;
  591. }
  592. }
  593. /**
  594. * Merges an array with another one.
  595. *
  596. * <pre>
  597. * {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
  598. *
  599. * {% set items = items|merge({ 'peugeot': 'car' }) %}
  600. *
  601. * {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
  602. * </pre>
  603. *
  604. * @param array|Traversable $arr1 An array
  605. * @param array|Traversable $arr2 An array
  606. *
  607. * @return array The merged array
  608. */
  609. function twig_array_merge($arr1, $arr2)
  610. {
  611. if ($arr1 instanceof Traversable) {
  612. $arr1 = iterator_to_array($arr1);
  613. } elseif (!is_array($arr1)) {
  614. throw new Twig_Error_Runtime(sprintf('The merge filter only works with arrays or "Traversable", got "%s" as first argument.', gettype($arr1)));
  615. }
  616. if ($arr2 instanceof Traversable) {
  617. $arr2 = iterator_to_array($arr2);
  618. } elseif (!is_array($arr2)) {
  619. throw new Twig_Error_Runtime(sprintf('The merge filter only works with arrays or "Traversable", got "%s" as second argument.', gettype($arr2)));
  620. }
  621. return array_merge($arr1, $arr2);
  622. }
  623. /**
  624. * Slices a variable.
  625. *
  626. * @param Twig_Environment $env A Twig_Environment instance
  627. * @param mixed $item A variable
  628. * @param int $start Start of the slice
  629. * @param int $length Size of the slice
  630. * @param bool $preserveKeys Whether to preserve key or not (when the input is an array)
  631. *
  632. * @return mixed The sliced variable
  633. */
  634. function twig_slice(Twig_Environment $env, $item, $start, $length = null, $preserveKeys = false)
  635. {
  636. if ($item instanceof Traversable) {
  637. if ($item instanceof IteratorAggregate) {
  638. $item = $item->getIterator();
  639. }
  640. if ($start >= 0 && $length >= 0 && $item instanceof Iterator) {
  641. try {
  642. return iterator_to_array(new LimitIterator($item, $start, $length === null ? -1 : $length), $preserveKeys);
  643. } catch (OutOfBoundsException $exception) {
  644. return array();
  645. }
  646. }
  647. $item = iterator_to_array($item, $preserveKeys);
  648. }
  649. if (is_array($item)) {
  650. return array_slice($item, $start, $length, $preserveKeys);
  651. }
  652. $item = (string) $item;
  653. if (function_exists('mb_get_info') && null !== $charset = $env->getCharset()) {
  654. return (string) mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset);
  655. }
  656. return (string) (null === $length ? substr($item, $start) : substr($item, $start, $length));
  657. }
  658. /**
  659. * Returns the first element of the item.
  660. *
  661. * @param Twig_Environment $env A Twig_Environment instance
  662. * @param mixed $item A variable
  663. *
  664. * @return mixed The first element of the item
  665. */
  666. function twig_first(Twig_Environment $env, $item)
  667. {
  668. $elements = twig_slice($env, $item, 0, 1, false);
  669. return is_string($elements) ? $elements : current($elements);
  670. }
  671. /**
  672. * Returns the last element of the item.
  673. *
  674. * @param Twig_Environment $env A Twig_Environment instance
  675. * @param mixed $item A variable
  676. *
  677. * @return mixed The last element of the item
  678. */
  679. function twig_last(Twig_Environment $env, $item)
  680. {
  681. $elements = twig_slice($env, $item, -1, 1, false);
  682. return is_string($elements) ? $elements : current($elements);
  683. }
  684. /**
  685. * Joins the values to a string.
  686. *
  687. * The separator between elements is an empty string per default, you can define it with the optional parameter.
  688. *
  689. * <pre>
  690. * {{ [1, 2, 3]|join('|') }}
  691. * {# returns 1|2|3 #}
  692. *
  693. * {{ [1, 2, 3]|join }}
  694. * {# returns 123 #}
  695. * </pre>
  696. *
  697. * @param array $value An array
  698. * @param string $glue The separator
  699. *
  700. * @return string The concatenated string
  701. */
  702. function twig_join_filter($value, $glue = '')
  703. {
  704. if ($value instanceof Traversable) {
  705. $value = iterator_to_array($value, false);
  706. }
  707. return implode($glue, (array) $value);
  708. }
  709. /**
  710. * Splits the string into an array.
  711. *
  712. * <pre>
  713. * {{ "one,two,three"|split(',') }}
  714. * {# returns [one, two, three] #}
  715. *
  716. * {{ "one,two,three,four,five"|split(',', 3) }}
  717. * {# returns [one, two, "three,four,five"] #}
  718. *
  719. * {{ "123"|split('') }}
  720. * {# returns [1, 2, 3] #}
  721. *
  722. * {{ "aabbcc"|split('', 2) }}
  723. * {# returns [aa, bb, cc] #}
  724. * </pre>
  725. *
  726. * @param Twig_Environment $env A Twig_Environment instance
  727. * @param string $value A string
  728. * @param string $delimiter The delimiter
  729. * @param int $limit The limit
  730. *
  731. * @return array The split string as an array
  732. */
  733. function twig_split_filter(Twig_Environment $env, $value, $delimiter, $limit = null)
  734. {
  735. if (!empty($delimiter)) {
  736. return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit);
  737. }
  738. if (!function_exists('mb_get_info') || null === $charset = $env->getCharset()) {
  739. return str_split($value, null === $limit ? 1 : $limit);
  740. }
  741. if ($limit <= 1) {
  742. return preg_split('/(?<!^)(?!$)/u', $value);
  743. }
  744. $length = mb_strlen($value, $charset);
  745. if ($length < $limit) {
  746. return array($value);
  747. }
  748. $r = array();
  749. for ($i = 0; $i < $length; $i += $limit) {
  750. $r[] = mb_substr($value, $i, $limit, $charset);
  751. }
  752. return $r;
  753. }
  754. // The '_default' filter is used internally to avoid using the ternary operator
  755. // which costs a lot for big contexts (before PHP 5.4). So, on average,
  756. // a function call is cheaper.
  757. /**
  758. * @internal
  759. */
  760. function _twig_default_filter($value, $default = '')
  761. {
  762. if (twig_test_empty($value)) {
  763. return $default;
  764. }
  765. return $value;
  766. }
  767. /**
  768. * Returns the keys for the given array.
  769. *
  770. * It is useful when you want to iterate over the keys of an array:
  771. *
  772. * <pre>
  773. * {% for key in array|keys %}
  774. * {# ... #}
  775. * {% endfor %}
  776. * </pre>
  777. *
  778. * @param array $array An array
  779. *
  780. * @return array The keys
  781. */
  782. function twig_get_array_keys_filter($array)
  783. {
  784. if ($array instanceof Traversable) {
  785. return array_keys(iterator_to_array($array));
  786. }
  787. if (!is_array($array)) {
  788. return array();
  789. }
  790. return array_keys($array);
  791. }
  792. /**
  793. * Reverses a variable.
  794. *
  795. * @param Twig_Environment $env A Twig_Environment instance
  796. * @param array|Traversable|string $item An array, a Traversable instance, or a string
  797. * @param bool $preserveKeys Whether to preserve key or not
  798. *
  799. * @return mixed The reversed input
  800. */
  801. function twig_reverse_filter(Twig_Environment $env, $item, $preserveKeys = false)
  802. {
  803. if ($item instanceof Traversable) {
  804. return array_reverse(iterator_to_array($item), $preserveKeys);
  805. }
  806. if (is_array($item)) {
  807. return array_reverse($item, $preserveKeys);
  808. }
  809. if (null !== $charset = $env->getCharset()) {
  810. $string = (string) $item;
  811. if ('UTF-8' !== $charset) {
  812. $item = twig_convert_encoding($string, 'UTF-8', $charset);
  813. }
  814. preg_match_all('/./us', $item, $matches);
  815. $string = implode('', array_reverse($matches[0]));
  816. if ('UTF-8' !== $charset) {
  817. $string = twig_convert_encoding($string, $charset, 'UTF-8');
  818. }
  819. return $string;
  820. }
  821. return strrev((string) $item);
  822. }
  823. /**
  824. * Sorts an array.
  825. *
  826. * @param array|Traversable $array
  827. *
  828. * @return array
  829. */
  830. function twig_sort_filter($array)
  831. {
  832. if ($array instanceof Traversable) {
  833. $array = iterator_to_array($array);
  834. } elseif (!is_array($array)) {
  835. throw new Twig_Error_Runtime(sprintf('The sort filter only works with arrays or "Traversable", got "%s".', gettype($array)));
  836. }
  837. asort($array);
  838. return $array;
  839. }
  840. /**
  841. * @internal
  842. */
  843. function twig_in_filter($value, $compare)
  844. {
  845. if (is_array($compare)) {
  846. return in_array($value, $compare, is_object($value) || is_resource($value));
  847. } elseif (is_string($compare) && (is_string($value) || is_int($value) || is_float($value))) {
  848. return '' === $value || false !== strpos($compare, (string) $value);
  849. } elseif ($compare instanceof Traversable) {
  850. return in_array($value, iterator_to_array($compare, false), is_object($value) || is_resource($value));
  851. }
  852. return false;
  853. }
  854. /**
  855. * Escapes a string.
  856. *
  857. * @param Twig_Environment $env A Twig_Environment instance
  858. * @param string $string The value to be escaped
  859. * @param string $strategy The escaping strategy
  860. * @param string $charset The charset
  861. * @param bool $autoescape Whether the function is called by the auto-escaping feature (true) or by the developer (false)
  862. *
  863. * @return string
  864. */
  865. function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html', $charset = null, $autoescape = false)
  866. {
  867. if ($autoescape && $string instanceof Twig_Markup) {
  868. return $string;
  869. }
  870. if (!is_string($string)) {
  871. if (is_object($string) && method_exists($string, '__toString')) {
  872. $string = (string) $string;
  873. } elseif (in_array($strategy, array('html', 'js', 'css', 'html_attr', 'url'))) {
  874. return $string;
  875. }
  876. }
  877. if (null === $charset) {
  878. $charset = $env->getCharset();
  879. }
  880. switch ($strategy) {
  881. case 'html':
  882. // see http://php.net/htmlspecialchars
  883. // Using a static variable to avoid initializing the array
  884. // each time the function is called. Moving the declaration on the
  885. // top of the function slow downs other escaping strategies.
  886. static $htmlspecialcharsCharsets;
  887. if (null === $htmlspecialcharsCharsets) {
  888. if (defined('HHVM_VERSION')) {
  889. $htmlspecialcharsCharsets = array('utf-8' => true, 'UTF-8' => true);
  890. } else {
  891. $htmlspecialcharsCharsets = array(
  892. 'ISO-8859-1' => true, 'ISO8859-1' => true,
  893. 'ISO-8859-15' => true, 'ISO8859-15' => true,
  894. 'utf-8' => true, 'UTF-8' => true,
  895. 'CP866' => true, 'IBM866' => true, '866' => true,
  896. 'CP1251' => true, 'WINDOWS-1251' => true, 'WIN-1251' => true,
  897. '1251' => true,
  898. 'CP1252' => true, 'WINDOWS-1252' => true, '1252' => true,
  899. 'KOI8-R' => true, 'KOI8-RU' => true, 'KOI8R' => true,
  900. 'BIG5' => true, '950' => true,
  901. 'GB2312' => true, '936' => true,
  902. 'BIG5-HKSCS' => true,
  903. 'SHIFT_JIS' => true, 'SJIS' => true, '932' => true,
  904. 'EUC-JP' => true, 'EUCJP' => true,
  905. 'ISO8859-5' => true, 'ISO-8859-5' => true, 'MACROMAN' => true,
  906. );
  907. }
  908. }
  909. if (isset($htmlspecialcharsCharsets[$charset])) {
  910. return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
  911. }
  912. if (isset($htmlspecialcharsCharsets[strtoupper($charset)])) {
  913. // cache the lowercase variant for future iterations
  914. $htmlspecialcharsCharsets[$charset] = true;
  915. return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
  916. }
  917. $string = twig_convert_encoding($string, 'UTF-8', $charset);
  918. $string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  919. return twig_convert_encoding($string, $charset, 'UTF-8');
  920. case 'js':
  921. // escape all non-alphanumeric characters
  922. // into their \xHH or \uHHHH representations
  923. if ('UTF-8' !== $charset) {
  924. $string = twig_convert_encoding($string, 'UTF-8', $charset);
  925. }
  926. if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
  927. throw new Twig_Error_Runtime('The string to escape is not a valid UTF-8 string.');
  928. }
  929. $string = preg_replace_callback('#[^a-zA-Z0-9,\._]#Su', '_twig_escape_js_callback', $string);
  930. if ('UTF-8' !== $charset) {
  931. $string = twig_convert_encoding($string, $charset, 'UTF-8');
  932. }
  933. return $string;
  934. case 'css':
  935. if ('UTF-8' !== $charset) {
  936. $string = twig_convert_encoding($string, 'UTF-8', $charset);
  937. }
  938. if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
  939. throw new Twig_Error_Runtime('The string to escape is not a valid UTF-8 string.');
  940. }
  941. $string = preg_replace_callback('#[^a-zA-Z0-9]#Su', '_twig_escape_css_callback', $string);
  942. if ('UTF-8' !== $charset) {
  943. $string = twig_convert_encoding($string, $charset, 'UTF-8');
  944. }
  945. return $string;
  946. case 'html_attr':
  947. if ('UTF-8' !== $charset) {
  948. $string = twig_convert_encoding($string, 'UTF-8', $charset);
  949. }
  950. if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
  951. throw new Twig_Error_Runtime('The string to escape is not a valid UTF-8 string.');
  952. }
  953. $string = preg_replace_callback('#[^a-zA-Z0-9,\.\-_]#Su', '_twig_escape_html_attr_callback', $string);
  954. if ('UTF-8' !== $charset) {
  955. $string = twig_convert_encoding($string, $charset, 'UTF-8');
  956. }
  957. return $string;
  958. case 'url':
  959. if (PHP_VERSION_ID < 50300) {
  960. return str_replace('%7E', '~', rawurlencode($string));
  961. }
  962. return rawurlencode($string);
  963. default:
  964. static $escapers;
  965. if (null === $escapers) {
  966. $escapers = $env->getExtension('core')->getEscapers();
  967. }
  968. if (isset($escapers[$strategy])) {
  969. return call_user_func($escapers[$strategy], $env, $string, $charset);
  970. }
  971. $validStrategies = implode(', ', array_merge(array('html', 'js', 'url', 'css', 'html_attr'), array_keys($escapers)));
  972. throw new Twig_Error_Runtime(sprintf('Invalid escaping strategy "%s" (valid ones: %s).', $strategy, $validStrategies));
  973. }
  974. }
  975. /**
  976. * @internal
  977. */
  978. function twig_escape_filter_is_safe(Twig_Node $filterArgs)
  979. {
  980. foreach ($filterArgs as $arg) {
  981. if ($arg instanceof Twig_Node_Expression_Constant) {
  982. return array($arg->getAttribute('value'));
  983. }
  984. return array();
  985. }
  986. return array('html');
  987. }
  988. if (function_exists('mb_convert_encoding')) {
  989. function twig_convert_encoding($string, $to, $from)
  990. {
  991. return mb_convert_encoding($string, $to, $from);
  992. }
  993. } elseif (function_exists('iconv')) {
  994. function twig_convert_encoding($string, $to, $from)
  995. {
  996. return iconv($from, $to, $string);
  997. }
  998. } else {
  999. function twig_convert_encoding($string, $to, $from)
  1000. {
  1001. throw new Twig_Error_Runtime('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
  1002. }
  1003. }
  1004. function _twig_escape_js_callback($matches)
  1005. {
  1006. $char = $matches[0];
  1007. // \xHH
  1008. if (!isset($char[1])) {
  1009. return '\\x'.strtoupper(substr('00'.bin2hex($char), -2));
  1010. }
  1011. // \uHHHH
  1012. $char = twig_convert_encoding($char, 'UTF-16BE', 'UTF-8');
  1013. return '\\u'.strtoupper(substr('0000'.bin2hex($char), -4));
  1014. }
  1015. function _twig_escape_css_callback($matches)
  1016. {
  1017. $char = $matches[0];
  1018. // \xHH
  1019. if (!isset($char[1])) {
  1020. $hex = ltrim(strtoupper(bin2hex($char)), '0');
  1021. if (0 === strlen($hex)) {
  1022. $hex = '0';
  1023. }
  1024. return '\\'.$hex.' ';
  1025. }
  1026. // \uHHHH
  1027. $char = twig_convert_encoding($char, 'UTF-16BE', 'UTF-8');
  1028. return '\\'.ltrim(strtoupper(bin2hex($char)), '0').' ';
  1029. }
  1030. /**
  1031. * This function is adapted from code coming from Zend Framework.
  1032. *
  1033. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  1034. * @license http://framework.zend.com/license/new-bsd New BSD License
  1035. */
  1036. function _twig_escape_html_attr_callback($matches)
  1037. {
  1038. /*
  1039. * While HTML supports far more named entities, the lowest common denominator
  1040. * has become HTML5's XML Serialisation which is restricted to the those named
  1041. * entities that XML supports. Using HTML entities would result in this error:
  1042. * XML Parsing Error: undefined entity
  1043. */
  1044. static $entityMap = array(
  1045. 34 => 'quot', /* quotation mark */
  1046. 38 => 'amp', /* ampersand */
  1047. 60 => 'lt', /* less-than sign */
  1048. 62 => 'gt', /* greater-than sign */
  1049. );
  1050. $chr = $matches[0];
  1051. $ord = ord($chr);
  1052. /*
  1053. * The following replaces characters undefined in HTML with the
  1054. * hex entity for the Unicode replacement character.
  1055. */
  1056. if (($ord <= 0x1f && $chr != "\t" && $chr != "\n" && $chr != "\r") || ($ord >= 0x7f && $ord <= 0x9f)) {
  1057. return '&#xFFFD;';
  1058. }
  1059. /*
  1060. * Check if the current character to escape has a name entity we should
  1061. * replace it with while grabbing the hex value of the character.
  1062. */
  1063. if (strlen($chr) == 1) {
  1064. $hex = strtoupper(substr('00'.bin2hex($chr), -2));
  1065. } else {
  1066. $chr = twig_convert_encoding($chr, 'UTF-16BE', 'UTF-8');
  1067. $hex = strtoupper(substr('0000'.bin2hex($chr), -4));
  1068. }
  1069. $int = hexdec($hex);
  1070. if (array_key_exists($int, $entityMap)) {
  1071. return sprintf('&%s;', $entityMap[$int]);
  1072. }
  1073. /*
  1074. * Per OWASP recommendations, we'll use hex entities for any other
  1075. * characters where a named entity does not exist.
  1076. */
  1077. return sprintf('&#x%s;', $hex);
  1078. }
  1079. // add multibyte extensions if possible
  1080. if (function_exists('mb_get_info')) {
  1081. /**
  1082. * Returns the length of a variable.
  1083. *
  1084. * @param Twig_Environment $env A Twig_Environment instance
  1085. * @param mixed $thing A variable
  1086. *
  1087. * @return int The length of the value
  1088. */
  1089. function twig_length_filter(Twig_Environment $env, $thing)
  1090. {
  1091. return is_scalar($thing) ? mb_strlen($thing, $env->getCharset()) : count($thing);
  1092. }
  1093. /**
  1094. * Converts a string to uppercase.
  1095. *
  1096. * @param Twig_Environment $env A Twig_Environment instance
  1097. * @param string $string A string
  1098. *
  1099. * @return string The uppercased string
  1100. */
  1101. function twig_upper_filter(Twig_Environment $env, $string)
  1102. {
  1103. if (null !== $charset = $env->getCharset()) {
  1104. return mb_strtoupper($string, $charset);
  1105. }
  1106. return strtoupper($string);
  1107. }
  1108. /**
  1109. * Converts a string to lowercase.
  1110. *
  1111. * @param Twig_Environment $env A Twig_Environment instance
  1112. * @param string $string A string
  1113. *
  1114. * @return string The lowercased string
  1115. */
  1116. function twig_lower_filter(Twig_Environment $env, $string)
  1117. {
  1118. if (null !== $charset = $env->getCharset()) {
  1119. return mb_strtolower($string, $charset);
  1120. }
  1121. return strtolower($string);
  1122. }
  1123. /**
  1124. * Returns a titlecased string.
  1125. *
  1126. * @param Twig_Environment $env A Twig_Environment instance
  1127. * @param string $string A string
  1128. *
  1129. * @return string The titlecased string
  1130. */
  1131. function twig_title_string_filter(Twig_Environment $env, $string)
  1132. {
  1133. if (null !== $charset = $env->getCharset()) {
  1134. return mb_convert_case($string, MB_CASE_TITLE, $charset);
  1135. }
  1136. return ucwords(strtolower($string));
  1137. }
  1138. /**
  1139. * Returns a capitalized string.
  1140. *
  1141. * @param Twig_Environment $env A Twig_Environment instance
  1142. * @param string $string A string
  1143. *
  1144. * @return string The capitalized string
  1145. */
  1146. function twig_capitalize_string_filter(Twig_Environment $env, $string)
  1147. {
  1148. if (null !== $charset = $env->getCharset()) {
  1149. return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset).mb_strtolower(mb_substr($string, 1, mb_strlen($string, $charset), $charset), $charset);
  1150. }
  1151. return ucfirst(strtolower($string));
  1152. }
  1153. }
  1154. // and byte fallback
  1155. else {
  1156. /**
  1157. * Returns the length of a variable.
  1158. *
  1159. * @param Twig_Environment $env A Twig_Environment instance
  1160. * @param mixed $thing A variable
  1161. *
  1162. * @return int The length of the value
  1163. */
  1164. function twig_length_filter(Twig_Environment $env, $thing)
  1165. {
  1166. return is_scalar($thing) ? strlen($thing) : count($thing);
  1167. }
  1168. /**
  1169. * Returns a titlecased string.
  1170. *
  1171. * @param Twig_Environment $env A Twig_Environment instance
  1172. * @param string $string A string
  1173. *
  1174. * @return string The titlecased string
  1175. */
  1176. function twig_title_string_filter(Twig_Environment $env, $string)
  1177. {
  1178. return ucwords(strtolower($string));
  1179. }
  1180. /**
  1181. * Returns a capitalized string.
  1182. *
  1183. * @param Twig_Environment $env A Twig_Environment instance
  1184. * @param string $string A string
  1185. *
  1186. * @return string The capitalized string
  1187. */
  1188. function twig_capitalize_string_filter(Twig_Environment $env, $string)
  1189. {
  1190. return ucfirst(strtolower($string));
  1191. }
  1192. }
  1193. /**
  1194. * @internal
  1195. */
  1196. function twig_ensure_traversable($seq)
  1197. {
  1198. if ($seq instanceof Traversable || is_array($seq)) {
  1199. return $seq;
  1200. }
  1201. return array();
  1202. }
  1203. /**
  1204. * Checks if a variable is empty.
  1205. *
  1206. * <pre>
  1207. * {# evaluates to true if the foo variable is null, false, or the empty string #}
  1208. * {% if foo is empty %}
  1209. * {# ... #}
  1210. * {% endif %}
  1211. * </pre>
  1212. *
  1213. * @param mixed $value A variable
  1214. *
  1215. * @return bool true if the value is empty, false otherwise
  1216. */
  1217. function twig_test_empty($value)
  1218. {
  1219. if ($value instanceof Countable) {
  1220. return 0 == count($value);
  1221. }
  1222. return '' === $value || false === $value || null === $value || array() === $value;
  1223. }
  1224. /**
  1225. * Checks if a variable is traversable.
  1226. *
  1227. * <pre>
  1228. * {# evaluates to true if the foo variable is an array or a traversable object #}
  1229. * {% if foo is traversable %}
  1230. * {# ... #}
  1231. * {% endif %}
  1232. * </pre>
  1233. *
  1234. * @param mixed $value A variable
  1235. *
  1236. * @return bool true if the value is traversable
  1237. */
  1238. function twig_test_iterable($value)
  1239. {
  1240. return $value instanceof Traversable || is_array($value);
  1241. }
  1242. /**
  1243. * Renders a template.
  1244. *
  1245. * @param Twig_Environment $env
  1246. * @param array $context
  1247. * @param string|array $template The template to render or an array of templates to try consecutively
  1248. * @param array $variables The variables to pass to the template
  1249. * @param bool $withContext
  1250. * @param bool $ignoreMissing Whether to ignore missing templates or not
  1251. * @param bool $sandboxed Whether to sandbox the template or not
  1252. *
  1253. * @return string The rendered template
  1254. */
  1255. function twig_include(Twig_Environment $env, $context, $template, $variables = array(), $withContext = true, $ignoreMissing = false, $sandboxed = false)
  1256. {
  1257. $alreadySandboxed = false;
  1258. $sandbox = null;
  1259. if ($withContext) {
  1260. $variables = array_merge($context, $variables);
  1261. }
  1262. if ($isSandboxed = $sandboxed && $env->hasExtension('sandbox')) {
  1263. $sandbox = $env->getExtension('sandbox');
  1264. if (!$alreadySandboxed = $sandbox->isSandboxed()) {
  1265. $sandbox->enableSandbox();
  1266. }
  1267. }
  1268. $result = null;
  1269. try {
  1270. $result = $env->resolveTemplate($template)->render($variables);
  1271. } catch (Twig_Error_Loader $e) {
  1272. if (!$ignoreMissing) {
  1273. if ($isSandboxed && !$alreadySandboxed) {
  1274. $sandbox->disableSandbox();
  1275. }
  1276. throw $e;
  1277. }
  1278. }
  1279. if ($isSandboxed && !$alreadySandboxed) {
  1280. $sandbox->disableSandbox();
  1281. }
  1282. return $result;
  1283. }
  1284. /**
  1285. * Returns a template content without rendering it.
  1286. *
  1287. * @param Twig_Environment $env
  1288. * @param string $name The template name
  1289. * @param bool $ignoreMissing Whether to ignore missing templates or not
  1290. *
  1291. * @return string The template source
  1292. */
  1293. function twig_source(Twig_Environment $env, $name, $ignoreMissing = false)
  1294. {
  1295. try {
  1296. return $env->getLoader()->getSource($name);
  1297. } catch (Twig_Error_Loader $e) {
  1298. if (!$ignoreMissing) {
  1299. throw $e;
  1300. }
  1301. }
  1302. }
  1303. /**
  1304. * Provides the ability to get constants from instances as well as class/global constants.
  1305. *
  1306. * @param string $constant The name of the constant
  1307. * @param null|object $object The object to get the constant from
  1308. *
  1309. * @return string
  1310. */
  1311. function twig_constant($constant, $object = null)
  1312. {
  1313. if (null !== $object) {
  1314. $constant = get_class($object).'::'.$constant;
  1315. }
  1316. return constant($constant);
  1317. }
  1318. /**
  1319. * Batches item.
  1320. *
  1321. * @param array $items An array of items
  1322. * @param int $size The size of the batch
  1323. * @param mixed $fill A value used to fill missing items
  1324. *
  1325. * @return array
  1326. */
  1327. function twig_array_batch($items, $size, $fill = null)
  1328. {
  1329. if ($items instanceof Traversable) {
  1330. $items = iterator_to_array($items, false);
  1331. }
  1332. $size = ceil($size);
  1333. $result = array_chunk($items, $size, true);
  1334. if (null !== $fill && !empty($result)) {
  1335. $last = count($result) - 1;
  1336. if ($fillCount = $size - count($result[$last])) {
  1337. $result[$last] = array_merge(
  1338. $result[$last],
  1339. array_fill(0, $fillCount, $fill)
  1340. );
  1341. }
  1342. }
  1343. return $result;
  1344. }