r1979 + allow a comment to end RackCode text
authorDenis Ovsienko <infrastation@yandex.ru>
Tue, 17 Jun 2008 11:13:56 +0000 (11:13 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Tue, 17 Jun 2008 11:13:56 +0000 (11:13 +0000)
+ validate RackCode before saving it into DB

inc/code.php
inc/ophandlers.php

index d6e4af523fd5d0ddb44d78462bee98fa8eceb276..4c8dd25de83df0bb58145c59a9a3e4fc21bb156d 100644 (file)
@@ -199,7 +199,7 @@ function getLexemsFromRackCode ($text)
                endswitch;
                $state = $newstate;
        endfor;
-       if ($state != 'ESOTSM')
+       if ($state != 'ESOTSM' and $state != 'skipping comment')
                abortLex3 ($state);
        return $ret;
 }
@@ -564,4 +564,46 @@ function getRackCode ()
        return getSentencesFromLexems (getLexemsFromRackCode (loadScript ('RackCode')));
 }
 
+// Return true, if the given expression can be evaluated against the given
+// predicate list.
+function valid_expression ($plist, $expr)
+{
+       switch ($expr['type'])
+       {
+               case 'BOOLCONST':
+               case 'LEX_TAG':
+                       return TRUE;
+               case 'LEX_PREDICATE':
+                       return in_array ($expr['load'], $plist);
+               case 'SYNT_NOTEXPR':
+                       return valid_expression ($plist, $expr['load']);
+               case 'SYNT_BOOLOP':
+                       return valid_expression ($plist, $expr['left']) and valid_expression ($plist, $expr['right']);
+               default:
+                       showError ("Validation error, cannot process expression type '${expr['type']}'");
+                       break;
+       }
+}
+
+function valid_rackcode ($code)
+{
+       $predicatelist = array();
+       foreach ($code as $sentence)
+               switch ($sentence['type'])
+               {
+                       case 'SYNT_DEFINITION':
+                               if (!valid_expression ($predicatelist, $sentence['definition']))
+                                       return FALSE;
+                               $predicatelist[] = $sentence['term'];
+                               break;
+                       case 'SYNT_GRANT':
+                               if (!valid_expression ($predicatelist, $sentence['condition']))
+                                       return FALSE;
+                               break;
+                       default:
+                               return FALSE;
+               }
+       return TRUE;
+}
+
 ?>
index 6ddbddfd8cd5fe4f730cdfe0989d4a6f2b2c25d7..cdcfdedeefdc60e0037e9f492ad599faa999887e 100644 (file)
@@ -1364,6 +1364,10 @@ function saveRackCode ()
 {
        global $root, $pageno, $tabno;
        assertStringArg ('rackcode');
+       // For the test to succeed, unescape LFs, strip CRs.
+       $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
+       if (!valid_rackcode (getSentencesFromLexems (getLexemsFromRackCode ($newcode))))
+               return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Verification failed.');
        if (saveScript ('RackCode', $_REQUEST['rackcode']))
                return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Saved successfully.');
        else