1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
<?php
/**
* PHPTAL templating engine
*
* PHP Version 5
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Moritz Bechler <mbechler@eenterphace.org>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id$
* @link http://phptal.org/
*/
/**
* TALES Specification 1.3
*
* Expression ::= [type_prefix ':'] String
* type_prefix ::= Name
*
* Examples:
*
* a/b/c
* path:a/b/c
* nothing
* path:nothing
* python: 1 + 2
* string:Hello, ${username}
*
*
* Builtin Names in Page Templates (for PHPTAL)
*
* * nothing - special singleton value used by TAL to represent a
* non-value (e.g. void, None, Nil, NULL).
*
* * default - special singleton value used by TAL to specify that
* existing text should not be replaced.
*
* * repeat - the repeat variables (see RepeatVariable).
*
*
*/
/**
* @package PHPTAL
* @subpackage Php
*/
class PHPTAL_Php_TalesInternal implements PHPTAL_Tales
{
const DEFAULT_KEYWORD = 'new PHPTAL_DefaultKeyword';
const NOTHING_KEYWORD = 'new PHPTAL_NothingKeyword';
static public function true($src, $nothrow)
{
return 'phptal_true(' . self::compileToPHPExpression($src, true) . ')';
}
/**
* not:
*
* not: Expression
*
* evaluate the expression string (recursively) as a full expression,
* and returns the boolean negation of its value
*
* return boolean based on the following rules:
*
* 1. integer 0 is false
* 2. integer > 0 is true
* 3. an empty string or other sequence is false
* 4. a non-empty string or other sequence is true
* 5. a non-value (e.g. void, None, Nil, NULL, etc) is false
* 6. all other values are implementation-dependent.
*
* Examples:
*
* not: exists: foo/bar/baz
* not: php: object.hasChildren()
* not: string:${foo}
* not: foo/bar/booleancomparable
*/
static public function not($expression, $nothrow)
{
return '!phptal_true(' . self::compileToPHPExpression($expression, $nothrow) . ')';
}
/**
* path:
*
* PathExpr ::= Path [ '|' Path ]*
* Path ::= variable [ '/' URL_Segment ]*
* variable ::= Name
*
* Examples:
*
* path: username
* path: user/name
* path: object/method/10/method/member
* path: object/${dynamicmembername}/method
* path: maybethis | path: maybethat | path: default
*
* PHPTAL:
*
* 'default' may lead to some 'difficult' attributes implementation
*
* For example, the tal:content will have to insert php code like:
*
* if (isset($ctx->maybethis)) {
* echo $ctx->maybethis;
* }
* elseif (isset($ctx->maybethat) {
* echo $ctx->maybethat;
* }
* else {
* // process default tag content
* }
*
* @returns string or array
*/
static public function path($expression, $nothrow=false)
{
$expression = trim($expression);
if ($expression == 'default') return self::DEFAULT_KEYWORD;
if ($expression == 'nothing') return self::NOTHING_KEYWORD;
if ($expression == '') return self::NOTHING_KEYWORD;
// split OR expressions terminated by a string
if (preg_match('/^(.*?)\s*\|\s*?(string:.*)$/sm', $expression, $m)) {
list(, $expression, $string) = $m;
}
// split OR expressions terminated by a 'fast' string
elseif (preg_match('/^(.*?)\s*\|\s*\'((?:[^\'\\\\]|\\\\.)*)\'\s*$/sm', $expression, $m)) {
list(, $expression, $string) = $m;
$string = 'string:'.stripslashes($string);
}
// split OR expressions
$exps = preg_split('/\s*\|\s*/sm', $expression);
// if (many expressions) or (expressions or terminating string) found then
// generate the array of sub expressions and return it.
if (count($exps) > 1 || isset($string)) {
$result = array();
foreach ($exps as $i=>$exp) {
if(isset($string) || $i < count($exps) - 1) {
$result[] = self::compileToPHPExpressions(trim($exp), true);
}
else {
// the last expression can thorw exception.
$result[] = self::compileToPHPExpressions(trim($exp), false);
}
}
if (isset($string)) {
$result[] = self::compileToPHPExpressions($string, true);
}
return $result;
}
// see if there are subexpressions, but skip interpolated parts, i.e. ${a/b}/c is 2 parts
if (preg_match('/^((?:[^$\/]+|\$\$|\${[^}]+}|\$))\/(.+)$/s', $expression, $m))
{
if (!self::checkExpressionPart($m[1])) {
throw new PHPTAL_ParserException("Invalid TALES path: '$expression', expected '{$m[1]}' to be variable name");
}
$next = self::string($m[1]);
$expression = self::string($m[2]);
} else {
if (!self::checkExpressionPart($expression)) {
throw new PHPTAL_ParserException("Invalid TALES path: '$expression', expected variable name. Complex expressions need php: modifier.");
}
$next = self::string($expression);
$expression = null;
}
if ($nothrow) {
return '$ctx->path($ctx, ' . $next . ($expression === null ? '' : '."/".'.$expression) . ', true)';
}
if (preg_match('/^\'[a-z][a-z0-9_]*\'$/i', $next)) $next = substr($next, 1, -1); else $next = '{'.$next.'}';
// if no sub part for this expression, just optimize the generated code
// and access the $ctx->var
if ($expression === null) {
return '$ctx->'.$next;
}
// otherwise we have to call PHPTAL_Context::path() to resolve the path at runtime
// extract the first part of the expression (it will be the PHPTAL_Context::path()
// $base and pass the remaining of the path to PHPTAL_Context::path()
return '$ctx->path($ctx->'.$next.', '.$expression.')';
}
/**
* check if part of exprssion (/foo/ or /foo${bar}/) is alphanumeric
*/
private static function checkExpressionPart($expression)
{
$expression = preg_replace('/\${[^}]+}/', 'a', $expression); // pretend interpolation is done
return preg_match('/^[a-z_][a-z0-9_]*$/i', $expression);
}
/**
* string:
*
* string_expression ::= ( plain_string | [ varsub ] )*
* varsub ::= ( '$' Path ) | ( '${' Path '}' )
* plain_string ::= ( '$$' | non_dollar )*
* non_dollar ::= any character except '$'
*
* Examples:
*
* string:my string
* string:hello, $username how are you
* string:hello, ${user/name}
* string:you have $$130 in your bank account
*/
static public function string($expression, $nothrow=false)
{
return self::parseString($expression, $nothrow, '');
}
/**
* @param string $tales_prefix prefix added to all TALES in the string
*/
static public function parseString($expression, $nothrow, $tales_prefix)
{
// This is a simple parser which evaluates ${foo} inside
// 'string:foo ${foo} bar' expressions, it returns the php code which will
// print the string with correct interpollations.
// Nothing special there :)
$inPath = false;
$inAccoladePath = false;
$lastWasDollar = false;
$result = '';
$len = strlen($expression);
for ($i=0; $i<$len; $i++) {
$c = $expression[$i];
switch ($c) {
case '$':
if ($lastWasDollar) {
$lastWasDollar = false;
} elseif ($inAccoladePath) {
$subPath .= $c;
$c = '';
} else {
$lastWasDollar = true;
$c = '';
}
break;
case '\\':
if ($inAccoladePath) {
$subPath .= $c;
$c = '';
}
else {
$c = '\\\\';
}
break;
case '\'':
if ($inAccoladePath) {
$subPath .= $c;
$c = '';
}
else {
$c = '\\\'';
}
break;
case '{':
if ($inAccoladePath) {
$subPath .= $c;
$c = '';
} elseif ($lastWasDollar) {
$lastWasDollar = false;
$inAccoladePath = true;
$subPath = '';
$c = '';
}
break;
case '}':
if ($inAccoladePath) {
$inAccoladePath = false;
$subEval = self::compileToPHPExpression($tales_prefix.$subPath,false);
$result .= "'.(" . $subEval . ").'";
$subPath = '';
$lastWasDollar = false;
$c = '';
}
break;
default:
if ($lastWasDollar) {
$lastWasDollar = false;
$inPath = true;
$subPath = $c;
$c = '';
} elseif ($inAccoladePath) {
$subPath .= $c;
$c = '';
} elseif ($inPath) {
$t = strtolower($c);
if (($t >= 'a' && $t <= 'z') || ($t >= '0' && $t <= '9') || ($t == '_')) {
$subPath .= $c;
$c = '';
} else {
$inPath = false;
$subEval = self::compileToPHPExpression($tales_prefix.$subPath,false);
$result .= "'.(" . $subEval . ").'";
}
}
break;
}
$result .= $c;
}
if ($inPath) {
$subEval = self::compileToPHPExpression($tales_prefix.$subPath, false);
$result .= "'.(" . $subEval . ").'";
}
// optimize ''.foo.'' to foo
$result = preg_replace("/^(?:''\.)?(.*?)(?:\.'')?$/", '\1', '\''.$result.'\'');
/*
The following expression (with + in first alternative):
"/^\(((?:[^\(\)]+|\([^\(\)]*\))*)\)$/"
did work properly for (aaaaaaa)aa, but not for (aaaaaaaaaaaaaaaaaaaaa)aa
WTF!?
*/
// optimize (foo()) to foo()
$result = preg_replace("/^\(((?:[^\(\)]|\([^\(\)]*\))*)\)$/", '\1', $result);
return $result;
}
/**
* php: modifier.
*
* Transform the expression into a regular PHP expression.
*/
static public function php($src)
{
return PHPTAL_Php_Transformer::transform($src, '$ctx->');
}
/**
* phptal-internal-php-block: modifier for emulation of <?php ?> in attributes.
*
* Please don't use it in the templates!
*/
static public function phptal_internal_php_block($src)
{
$src = rawurldecode($src);
// Simple echo can be supported via regular method
if (preg_match('/^\s*echo\s+((?:[^;]+|"[^"\\\\]*"|\'[^\'\\\\]*\'|\/\*.*?\*\/)+);*\s*$/s',$src,$m))
{
return $m[1];
}
// <?php block expects statements, but modifiers must return expressions.
// unfortunately this ugliness is the only way to support it currently.
// ? > keeps semicolon optional
return "eval(".self::string($src.'?>').")";
}
/**
* exists: modifier.
*
* Returns the code required to invoke Context::exists() on specified path.
*/
static public function exists($src, $nothrow)
{
$src = trim($src);
if (ctype_alnum($src)) return 'isset($ctx->'.$src.')';
return '(null !== ' . self::compileToPHPExpression($src, true) . ')';
}
/**
* number: modifier.
*
* Returns the number as is.
*/
static public function number($src, $nothrow)
{
if (!is_numeric(trim($src))) throw new PHPTAL_ParserException("'$src' is not a number");
return trim($src);
}
/**
* json: modifier. Serializes anything as JSON.
*/
static public function json($src, $nothrow)
{
return 'json_encode('.phptal_tale($src,$nothrow).')';
}
/**
* urlencode: modifier. Escapes a string.
*/
static public function urlencode($src, $nothrow)
{
return 'rawurlencode('.phptal_tale($src,$nothrow).')';
}
/**
* translates TALES expression with alternatives into single PHP expression.
* Identical to compileToPHPExpressions() for singular expressions.
*
* @see PHPTAL_Php_TalesInternal::compileToPHPExpressions()
* @return string
*/
public static function compileToPHPExpression($expression, $nothrow=false)
{
$r = self::compileToPHPExpressions($expression, $nothrow);
if (!is_array($r)) return $r;
// this weird ternary operator construct is to execute noThrow inside the expression
return '($ctx->noThrow(true)||1?'.self::convertExpressionsToExpression($r, $nothrow).':"")';
}
/*
* helper function for compileToPHPExpression
* @access private
*/
private static function convertExpressionsToExpression(array $array, $nothrow)
{
if (count($array)==1) return '($ctx->noThrow('.($nothrow?'true':'false').')||1?('.
($array[0]==self::NOTHING_KEYWORD?'null':$array[0]).
'):"")';
$expr = array_shift($array);
return "(!phptal_isempty(\$_tmp5=$expr) && (\$ctx->noThrow(false)||1)?\$_tmp5:".self::convertExpressionsToExpression($array, $nothrow).')';
}
/**
* returns PHP code that will evaluate given TALES expression.
* e.g. "string:foo${bar}" may be transformed to "'foo'.phptal_escape($ctx->bar)"
*
* Expressions with alternatives ("foo | bar") will cause it to return array
* Use PHPTAL_Php_TalesInternal::compileToPHPExpression() if you always want string.
*
* @param bool $nothrow if true, invalid expression will return NULL (at run time) rather than throwing exception
*
* @return string or array
*/
public static function compileToPHPExpressions($expression, $nothrow=false)
{
$expression = trim($expression);
// Look for tales modifier (string:, exists:, Namespaced\Tale:, etc...)
if (preg_match('/^([a-z](?:[a-z0-9._\\\\-]*[a-z0-9])?):(.*)$/si', $expression, $m)) {
list(, $typePrefix, $expression) = $m;
}
// may be a 'string'
elseif (preg_match('/^\'((?:[^\']|\\\\.)*)\'$/s', $expression, $m)) {
$expression = stripslashes($m[1]);
$typePrefix = 'string';
}
// failback to path:
else {
$typePrefix = 'path';
}
// is a registered TALES expression modifier
$callback = PHPTAL_TalesRegistry::getInstance()->getCallback($typePrefix);
if ($callback !== NULL)
{
$result = call_user_func($callback, $expression, $nothrow);
self::verifyPHPExpressions($typePrefix, $result);
return $result;
}
$func = 'phptal_tales_'.str_replace('-', '_', $typePrefix);
throw new PHPTAL_UnknownModifierException("Unknown phptal modifier '$typePrefix'. Function '$func' does not exist", $typePrefix);
}
private static function verifyPHPExpressions($typePrefix,$expressions)
{
if (!is_array($expressions)) {
$expressions = array($expressions);
}
foreach($expressions as $expr) {
if (preg_match('/;\s*$/', $expr)) {
throw new PHPTAL_ParserException("Modifier $typePrefix generated PHP statement rather than expression (don't add semicolons)");
}
}
}
}
|