xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
Commit 2155bedec6e82343d84c8c504c0beb68fdf01dea

rg_template_eval_cond: now supports more operations
Author: Catalin(ux) M. BOIE
Author date (UTC): 2017-07-10 20:55
Committer name: Catalin(ux) M. BOIE
Committer date (UTC): 2017-07-10 20:55
Parent(s): dc3ce904f00ce5903ac5614ac870a0529d22ee4a
Signing key:
Tree: ae07486a92f1ff9ce876db6660167be8ea0c26cd
File Lines added Lines deleted
inc/util.inc.php 38 25
tests/util.php 29 0
File inc/util.inc.php changed (mode: 100644) (index 8177480..41d439d)
... ... function rg_template_tree_lookup($var, &$data, $xss_protection)
576 576
577 577 /* /*
578 578 * Evaluates a condition * Evaluates a condition
579 * Returns FALSE on error, 0 for TRUE and 1 for FALSE
579 580 */ */
580 581 function rg_template_eval_cond($cond, &$data) function rg_template_eval_cond($cond, &$data)
581 582 { {
582 $t = explode('!=', $cond);
583 if (count($t) == 2) {
584 $negate = TRUE;
585 } else {
586 $t = explode('==', $cond);
587 if (count($t) != 2) {
588 rg_log("invalid condition [$cond]!");
589 return FALSE;
590 }
591 $negate = FALSE;
583 while (strpos($cond, ' '))
584 $cond = str_replace(' ', ' ', $cond);
585
586 $t = explode(' ', $cond);
587 if (count($t) != 3) {
588 rg_util_set_error('invalid condition: ' . $cond
589 . ' (not 3 tokens)');
590 return FALSE;
592 591 } }
593 592
594 $left = trim($t[0]);
595 $left = rg_template_string($left, 0, $data, FALSE);
593 $left = rg_template_string($t[0], 0, $data, FALSE);
594 $op = $t[1];
595 $right = rg_template_string($t[2], 0, $data, FALSE);
596 596
597 $right = trim($t[1]);
598 $right = rg_template_string($right, 0, $data, FALSE);
597 if (strcmp($op, '==') == 0)
598 return strcmp($left, $right) == 0 ? 1 : 0;
599 599
600 $ret = strcmp($left, $right);
601 if ($ret === 0) {
602 if ($negate === TRUE)
603 return FALSE;
604 } else {
605 if (!$negate)
606 return FALSE;
607 }
600 if (strcmp($op, '!=') == 0)
601 return strcmp($left, $right) != 0 ? 1 : 0;
608 602
609 return TRUE;
603 $ileft = intval($left);
604 $iright = intval($right);
605
606 if (strcmp($op, '>=') == 0)
607 return $ileft >= $iright ? 1 : 0;
608
609 if (strcmp($op, '>') == 0)
610 return $ileft > $iright ? 1 : 0;
611
612 if (strcmp($op, '<=') == 0)
613 return $ileft <= $iright ? 1 : 0;
614
615 if (strcmp($op, '<') == 0)
616 return $ileft < $iright ? 1 : 0;
617
618 rg_util_set_error('unknown condition: ' . $cond);
619 return FALSE;
610 620 } }
611 621
612 622 /* /*
 
... ... function rg_template_string_if(&$s, $off, &$data, &$next, $xss_protection)
717 727
718 728 $cond = substr($s, $off, $pos - $off); $off = $pos + 1; $cond = substr($s, $off, $pos - $off); $off = $pos + 1;
719 729 $eval_cond = rg_template_eval_cond($cond, $data); $eval_cond = rg_template_eval_cond($cond, $data);
720 //rg_log("DEBUG: cond=[$cond] eval_cond=" . ($eval_cond ? "true" : "false"));
730 if ($eval_cond === FALSE) {
731 rg_prof_end('template_string_if');
732 return -1;
733 }
721 734
722 735 // TODO: Between ')' and '{{' must be only space, else ignore anything?? // TODO: Between ')' and '{{' must be only space, else ignore anything??
723 736
 
... ... function rg_template_string_if(&$s, $off, &$data, &$next, $xss_protection)
731 744 } }
732 745
733 746 $x = ''; $x = '';
734 if ($eval_cond === TRUE) {
747 if ($eval_cond === 1) {
735 748 $x = substr($s, $true_start, $true_end - $true_start + 1); $x = substr($s, $true_start, $true_end - $true_start + 1);
736 749 } else { } else {
737 750 if ($false_start != -1) if ($false_start != -1)
File tests/util.php changed (mode: 100644) (index 1a22a8c..4a7cdec)
... ... $rg_no_db = TRUE;
14 14 require_once("common.php"); require_once("common.php");
15 15
16 16
17 rg_log('');
18 rg_log_enter('Testing rg_template_eval_cond');
19
20 $a = array('a' => 100, 'b' => 200);
21
22 $cond = '1 >= 100';
23 $r = rg_template_eval_cond($cond, $a);
24 if (($r === FALSE) || ($r === 1)) {
25 rg_log('Error for ' . $cond . '!');
26 exit(1);
27 }
28
29 $cond = '@@a@@ == 100';
30 $r = rg_template_eval_cond($cond, $a);
31 if (($r === FALSE) || ($r === 0)) {
32 rg_log('Error for ' . $cond . '!');
33 exit(1);
34 }
35
36 $cond = '-100 != -100';
37 $r = rg_template_eval_cond($cond, $a);
38 if (($r === FALSE) || ($r === 1)) {
39 rg_log('Error for ' . $cond . '!');
40 exit(1);
41 }
42
43 rg_log_exit();
44
45
17 46 rg_log("Testing template_tree_lookup1"); rg_log("Testing template_tree_lookup1");
18 47 $var = 'a::b'; $var = 'a::b';
19 48 $e = '<'; $e = '<';
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master