LimeSurvey API - Master branch

ExpressionManager
in package

Description of ExpressionManager (1) Does safe evaluation of PHP expressions. Only registered Functions, and known Variables are allowed.

(a) Functions include any math, string processing, conditional, formatting, etc. functions (2) This class replaces LimeSurvey's <= 1.91+ process of resolving strings that contain LimeReplacementFields (a) String is split by expressions (by curly braces, but safely supporting strings and escaped curly braces) (b) Expressions (things surrounded by curly braces) are evaluated - thereby doing LimeReplacementField substitution and/or more complex calculations (c) Non-expressions are left intact (d) The array of stringParts are re-joined to create the desired final string. (3) The core of ExpressionScript Engine is a Recursive Descent Parser (RDP), based off of one build via JavaCC by TMSWhite in 1999. (a) Functions that start with RDP_ should not be touched unless you really understand compiler design.

Tags
author

LimeSurvey Team (limesurvey.org)

author

Thomas M. White (TMSWhite)

Table of Contents

Properties

$resetErrorsAndWarningsOnEachPart  : mixed
$allVarsUsed  : mixed
$aRDP_regexpStaticAttribute  : mixed
$aRDP_regexpVariableAttribute  : mixed
$groupSeq  : mixed
$hyperlinkSyntaxHighlighting  : mixed
$jsExpression  : mixed
$prettyPrintSource  : mixed
$questionSeq  : mixed
$RDP_CategorizeTokensRegex  : mixed
$RDP_count  : mixed
$RDP_errs  : array<string|int, array<string|int, mixed>>
$RDP_evalStatus  : mixed
$RDP_expr  : mixed
$RDP_ExpressionRegex  : mixed
$RDP_onlyparse  : mixed
$RDP_pos  : mixed
$RDP_result  : mixed
$RDP_stack  : mixed
$RDP_TokenizerRegex  : mixed
$RDP_tokens  : mixed
$RDP_TokenType  : mixed
$RDP_ValidFunctions  : mixed
$RDP_warnings  : array<string|int, array<string|int, mixed>>
$sgqaNaming  : mixed
$sid  : mixed
$substitutionInfo  : array<string|int, mixed>
$substitutionNum  : mixed
$surveyMode  : mixed
$varsUsed  : mixed

Methods

__construct()  : mixed
addPackageForExpressionManager()  : void
Add a package for expression
addRegexpExtraAttributes()  : void
Add extra attributes for var
asSplitStringOnExpressions()  : array<string|int, mixed>
Split a source string into STRING vs. EXPRESSION, where the latter is surrounded by unescaped curly braces.
ClearSubstitutionInfo()  : mixed
Clear cache of tailoring content.
ExpandThisVar()  : string
If the equation contains reference to this, expand to comma separated list if needed.
ExpressionManagerStartEvent()  : void
Since this class can be get by session, need to add a call the «start» event manually
GetAllJsVarsUsed()  : array<string|int, mixed>
Returns array of all JavaScript-equivalent variable names used when parsing a string via sProcessStringContainingExpressions
GetAllowableFunctions()  : string
Show a table of allowable ExpressionScript Engine functions
GetAllVarsUsed()  : array<string|int, mixed>
Returns array of all variables used when parsing a string via sProcessStringContainingExpressions
GetCurrentSubstitutionInfo()  : array<string|int, mixed>
Get info about all <span> elements needed for dynamic tailoring
GetErrors()  : array<string|int, mixed>
Return an array of errors
GetJavaScriptEquivalentOfExpression()  : string
Converts the most recent expression into a valid JavaScript expression, mapping function and variable names and operators as needed.
GetJavaScriptFunctionForReplacement()  : string
Generate the function needed to dynamically change the value of a <span> section
GetJavascriptTestforExpression()  : string
JavaScript Test function - simply writes the result of the current JavaScriptEquivalentFunction to the output buffer.
GetJsVarFor()  : string
Return the JavaScript variable name for a named variable
GetJsVarsUsed()  : array<string|int, mixed>
Return the list of all of the JavaScript variables used by the most recent expression
GetLastPrettyPrintExpression()  : mixed
Returns the most recent PrettyPrint string generated by sProcessStringContainingExpressions
GetOnPageJsVarsUsed()  : array<string|int, mixed>
Return the list of all of the JavaScript variables used by the most recent expression - only those that are set on the current page This is used to control static vs dynamic substitution. If an expression is entirely made up of off-page changes, it can be statically replaced.
GetPrettyPrintString()  : string
Color-codes Expressions (using HTML <span> tags), showing variable types and values.
getRegexpStaticValidAttributes()  : mixed
getRegexpValidAttributes()  : mixed
GetResult()  : mixed
Return the result of evaluating the equation - NULL if error
GetVarsUsed()  : array<string|int, mixed>
Return array of the list of variables used in the equation
GetWarnings()  : array<string|int, mixed>
Return array of warnings
gT()  : string
Show a translated string for admin user, always in admin language #12208 public for geterrors_exprmgr_regexMatch function only
HasErrors()  : bool
Return true if there were syntax or processing errors
ProcessBooleanExpression()  : bool
Process an expression and return its boolean value
RDP_Evaluate()  : bool
Main entry function
RDP_EvaluateBinary()  : bool
RDP_EvaluateBinary() computes binary expressions, such as (a or b), (c * d), popping the top two entries off the stack and pushing the result back onto the stack.
RDP_GetErrors()  : array<string|int, mixed>
RDP_StackPop()  : token
Pop a value token off of the stack
RDP_StackPush()  : mixed
Stack only holds values (number, string), not operators
RegisterFunctions()  : mixed
Add user functions to array of allowable functions within the equation.
ResetErrors()  : void
Reset current errors
ResetErrorsAndWarnings()  : void
Reset current errors and current warnings
ResetWarnings()  : void
Reset current warnings
SetJsVarsUsed()  : void
SetPrettyPrintSource()  : mixed
This is only used when there are no needed substitutions
SetSurveyMode()  : mixed
Specify the survey mode for this survey. Options are 'survey', 'group', and 'question'
ShowAllowableFunctions()  : string
Show a table of allowable ExpressionScript Engine functions
sProcessStringContainingExpressions()  : string
Process multiple substitution iterations of a full string, containing multiple expressions delimited by {}, return a consolidated string
sProcessStringContainingExpressionsHelper()  : string
Process one substitution iteration of a full string, containing multiple expressions delimited by {}, return a consolidated string
StartProcessingGroup()  : mixed
Start processing a group of substitions - will be incrementally numbered
Tokenize()  : array<string|int, mixed>
Public call of RDP_Tokenize
flatten_array()  : array<string|int, mixed>
Flatten out an array, keeping it in the proper order
getMismatchInformation()  : array<string|int, bool>
Get informatin about type mismatch between arguments.
GetVarAttribute()  : string
Get information about the variable, including JavaScript name, read-write status, and whether set on current page.
HasSyntaxErrors()  : bool
Return true if there are syntax errors
RDP_AddError()  : void
Add an error to the error log
RDP_AddWarning()  : void
Add a warning to the error log
RDP_EvaluateAdditiveExpression()  : bool
Process "a op b" where op in (+,-,concatenate)
RDP_EvaluateConstantVarOrFunction()  : bool|null
Process a Constant (number of string), retrieve the value of a known variable, or process a function, returning result on the stack.
RDP_EvaluateEqualityExpression()  : bool
Process "a == b", "a eq b", "a != b", "a ne b"
RDP_EvaluateExpression()  : bool
Process a single expression (e.g. without commas)
RDP_EvaluateExpressions()  : bool
Process "expression [, expression]*
RDP_EvaluateFunction()  : bool|null
Process a function call
RDP_EvaluateLogicalAndExpression()  : bool
Process "a && b" or "a and b"
RDP_EvaluateLogicalOrExpression()  : bool
Process "a || b" or "a or b"
RDP_EvaluateMultiplicativeExpression()  : bool
Process "a op b" where op in (*,/)
RDP_EvaluatePrimaryExpression()  : bool|null
Process expressions including functions and parenthesized blocks
RDP_EvaluateRelationExpression()  : bool
Process "a op b" where op in (lt, gt, le, ge, <, >, <=, >=)
RDP_EvaluateUnary()  : bool
Processes operations like +a, -b, !c
RDP_EvaluateUnaryExpression()  : bool|null
Process "op a" where op in (+,-,!)
RDP_isValidFunction()  : bool
Return true if the function name is registered
RDP_isValidVariable()  : bool
Return true if the variable name is registered
RDP_isWritableVariable()  : bool
Return true if the variable name is writable
RDP_RunFunction()  : bool|null
Run a registered function Some PHP functions require specific data types - those can be cast here.
RDP_SetVariableValue()  : int
Set the value of a registered variable
RDP_Tokenize()  : array<string|int, mixed>
Split the source string into tokens, removing whitespace, and categorizing them by type.

Properties

$resetErrorsAndWarningsOnEachPart

public mixed $resetErrorsAndWarningsOnEachPart = \true

$aRDP_regexpStaticAttribute

private mixed $aRDP_regexpStaticAttribute = array('qid', 'gid', 'question', 'sgqa', 'type', 'relevance', 'grelevance', 'qseq', 'gseq', 'jsName', 'jsName_on', 'mandatory', 'rowdivid')

$aRDP_regexpVariableAttribute

private mixed $aRDP_regexpVariableAttribute = array('code', 'NAOK', 'relevanceStatus', 'shown', 'valueNAOK', 'value')

$hyperlinkSyntaxHighlighting

private mixed $hyperlinkSyntaxHighlighting = \true

$RDP_CategorizeTokensRegex

private mixed $RDP_CategorizeTokensRegex

$RDP_errs

private array<string|int, array<string|int, mixed>> $RDP_errs

information about current errors : array with string, $token (EM internal array). Reset in RDP_Evaluate (and only in RDP_Evaluate)

$RDP_warnings

private array<string|int, array<string|int, mixed>> $RDP_warnings = array()

information about current warnings : array with string, $token (EM internal array) and optional link Reset in RDP_Evaluate or manually

$substitutionInfo

private array<string|int, mixed> $substitutionInfo

Methods

addPackageForExpressionManager()

Add a package for expression

public addPackageForExpressionManager(string $name, array<string|int, mixed> $definition) : void
Parameters
$name : string

of package

$definition : array<string|int, mixed>

@see https://www.yiiframework.com/doc/api/1.1/CClientScript#packages-detail

addRegexpExtraAttributes()

Add extra attributes for var

public addRegexpExtraAttributes(array<string|int, string> $extraAttributes[, bool $static = true ]) : void
Parameters
$extraAttributes : array<string|int, string>
$static : bool = true

is a static attribute , unused currently since there are no way to create the EM js system

asSplitStringOnExpressions()

Split a source string into STRING vs. EXPRESSION, where the latter is surrounded by unescaped curly braces.

public asSplitStringOnExpressions(string $src) : array<string|int, mixed>

This version properly handles nested curly braces and curly braces within strings within curly braces - both of which are needed to better support JavaScript Users still need to add a space or carriage return after opening braces (and ideally before closing braces too) to avoid having them treated as expressions.

Parameters
$src : string
Return values
array<string|int, mixed>

ClearSubstitutionInfo()

Clear cache of tailoring content.

public ClearSubstitutionInfo() : mixed

When re-displaying same page, need to avoid generating double the amount of tailoring content.

ExpandThisVar()

If the equation contains reference to this, expand to comma separated list if needed.

public ExpandThisVar(string $src) : string
Parameters
$src : string
Return values
string

ExpressionManagerStartEvent()

Since this class can be get by session, need to add a call the «start» event manually

public ExpressionManagerStartEvent() : void

GetAllJsVarsUsed()

Returns array of all JavaScript-equivalent variable names used when parsing a string via sProcessStringContainingExpressions

public GetAllJsVarsUsed() : array<string|int, mixed>
Return values
array<string|int, mixed>

GetAllowableFunctions()

Show a table of allowable ExpressionScript Engine functions

public static GetAllowableFunctions() : string
Return values
string

GetAllVarsUsed()

Returns array of all variables used when parsing a string via sProcessStringContainingExpressions

public GetAllVarsUsed() : array<string|int, mixed>
Return values
array<string|int, mixed>

GetCurrentSubstitutionInfo()

Get info about all <span> elements needed for dynamic tailoring

public GetCurrentSubstitutionInfo() : array<string|int, mixed>
Return values
array<string|int, mixed>

GetErrors()

Return an array of errors

public GetErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>

GetJavaScriptEquivalentOfExpression()

Converts the most recent expression into a valid JavaScript expression, mapping function and variable names and operators as needed.

public GetJavaScriptEquivalentOfExpression() : string
Return values
string

the JavaScript expresssion

GetJavaScriptFunctionForReplacement()

Generate the function needed to dynamically change the value of a <span> section

public GetJavaScriptFunctionForReplacement(int $questionNum, string $elementId, string $eqn) : string
Parameters
$questionNum : int

No longer used

$elementId : string
  • the ID name for the function
$eqn : string

No longer used

Return values
string

: javascript part

GetJavascriptTestforExpression()

JavaScript Test function - simply writes the result of the current JavaScriptEquivalentFunction to the output buffer.

public GetJavascriptTestforExpression(string $expected, int $num) : string
Parameters
$expected : string
$num : int
Return values
string

GetJsVarFor()

Return the JavaScript variable name for a named variable

public GetJsVarFor(string $name) : string
Parameters
$name : string
Return values
string

GetJsVarsUsed()

Return the list of all of the JavaScript variables used by the most recent expression

public GetJsVarsUsed() : array<string|int, mixed>
Return values
array<string|int, mixed>

GetLastPrettyPrintExpression()

Returns the most recent PrettyPrint string generated by sProcessStringContainingExpressions

public GetLastPrettyPrintExpression() : mixed

GetOnPageJsVarsUsed()

Return the list of all of the JavaScript variables used by the most recent expression - only those that are set on the current page This is used to control static vs dynamic substitution. If an expression is entirely made up of off-page changes, it can be statically replaced.

public GetOnPageJsVarsUsed() : array<string|int, mixed>
Return values
array<string|int, mixed>

GetPrettyPrintString()

Color-codes Expressions (using HTML <span> tags), showing variable types and values.

public GetPrettyPrintString() : string
Return values
string

HTML

getRegexpStaticValidAttributes()

public getRegexpStaticValidAttributes() : mixed

getRegexpValidAttributes()

public getRegexpValidAttributes() : mixed

GetResult()

Return the result of evaluating the equation - NULL if error

public GetResult() : mixed

GetVarsUsed()

Return array of the list of variables used in the equation

public GetVarsUsed() : array<string|int, mixed>
Return values
array<string|int, mixed>

GetWarnings()

Return array of warnings

public GetWarnings() : array<string|int, mixed>
Return values
array<string|int, mixed>

gT()

Show a translated string for admin user, always in admin language #12208 public for geterrors_exprmgr_regexMatch function only

public static gT(string $string[, string $sEscapeMode = 'html' ]) : string
Parameters
$string : string

to translate

$sEscapeMode : string = 'html'

Valid values are html (this is the default, js and unescaped)

Return values
string

: translated string

HasErrors()

Return true if there were syntax or processing errors

public HasErrors() : bool
Return values
bool

ProcessBooleanExpression()

Process an expression and return its boolean value

public ProcessBooleanExpression(string $expr[, int $groupSeq = -1 ][, int $questionSeq = -1 ]) : bool
Parameters
$expr : string
$groupSeq : int = -1
  • needed to determine whether using variables before they are declared
$questionSeq : int = -1
  • needed to determine whether using variables before they are declared
Return values
bool

RDP_Evaluate()

Main entry function

public RDP_Evaluate(string $expr[, bool $onlyparse = false ][, bool $resetErrorsAndWarnings = true ]) : bool
Parameters
$expr : string
$onlyparse : bool = false
  • if true, then validate the syntax without computing an answer
$resetErrorsAndWarnings : bool = true
  • if true (default), EM errors and warnings will be cleared before evaluation
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateBinary()

RDP_EvaluateBinary() computes binary expressions, such as (a or b), (c * d), popping the top two entries off the stack and pushing the result back onto the stack.

public RDP_EvaluateBinary(array<string|int, mixed> $token) : bool
Parameters
$token : array<string|int, mixed>
Return values
bool
  • false if there is any error, else true

RDP_GetErrors()

public RDP_GetErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>

RDP_StackPop()

Pop a value token off of the stack

public RDP_StackPop() : token
Return values
token

RDP_StackPush()

Stack only holds values (number, string), not operators

public RDP_StackPush(array<string|int, mixed> $token) : mixed
Parameters
$token : array<string|int, mixed>

RegisterFunctions()

Add user functions to array of allowable functions within the equation.

public RegisterFunctions(array<string|int, mixed> $functions) : mixed

$functions is an array of key to value mappings like this: See $this->RDP_ValidFunctions for examples of the syntax

Parameters
$functions : array<string|int, mixed>

ResetErrorsAndWarnings()

Reset current errors and current warnings

public ResetErrorsAndWarnings() : void

SetJsVarsUsed()

public SetJsVarsUsed(mixed $vars) : void
Parameters
$vars : mixed

SetPrettyPrintSource()

This is only used when there are no needed substitutions

public SetPrettyPrintSource(string $expr) : mixed
Parameters
$expr : string

SetSurveyMode()

Specify the survey mode for this survey. Options are 'survey', 'group', and 'question'

public SetSurveyMode(string $mode) : mixed
Parameters
$mode : string

ShowAllowableFunctions()

Show a table of allowable ExpressionScript Engine functions

public static ShowAllowableFunctions() : string
Return values
string

sProcessStringContainingExpressions()

Process multiple substitution iterations of a full string, containing multiple expressions delimited by {}, return a consolidated string

public sProcessStringContainingExpressions(string $src[, int $questionNum = 0 ][, int $numRecursionLevels = 1 ][, int $whichPrettyPrintIteration = 1 ][, int $groupSeq = -1 ][, int $questionSeq = -1 ][, bool $staticReplacement = false ]) : string
Parameters
$src : string
$questionNum : int = 0
$numRecursionLevels : int = 1
  • number of levels of recursive substitution to perform
$whichPrettyPrintIteration : int = 1
  • if recursing, specify which pretty-print iteration is desired
$groupSeq : int = -1
  • needed to determine whether using variables before they are declared
$questionSeq : int = -1
  • needed to determine whether using variables before they are declared
$staticReplacement : bool = false
Return values
string

sProcessStringContainingExpressionsHelper()

Process one substitution iteration of a full string, containing multiple expressions delimited by {}, return a consolidated string

public sProcessStringContainingExpressionsHelper(string $src, int $questionNum[, bool $staticReplacement = false ]) : string
Parameters
$src : string
$questionNum : int
  • used to generate substitution s that indicate to which question they belong
$staticReplacement : bool = false
Return values
string

StartProcessingGroup()

Start processing a group of substitions - will be incrementally numbered

public StartProcessingGroup([mixed $sid = null ][, mixed $rooturl = '' ][, mixed $hyperlinkSyntaxHighlighting = true ]) : mixed
Parameters
$sid : mixed = null
$rooturl : mixed = ''
$hyperlinkSyntaxHighlighting : mixed = true

Tokenize()

Public call of RDP_Tokenize

public Tokenize(string $sSource, bool $bOnEdit) : array<string|int, mixed>
Parameters
$sSource : string

: the string to tokenize

$bOnEdit : bool

: on edition, actually don't remove space

Return values
array<string|int, mixed>

flatten_array()

Flatten out an array, keeping it in the proper order

private flatten_array(array<string|int, mixed> $a) : array<string|int, mixed>
Parameters
$a : array<string|int, mixed>
Return values
array<string|int, mixed>

getMismatchInformation()

Get informatin about type mismatch between arguments.

private getMismatchInformation(Token $arg1, Token $arg2) : array<string|int, bool>
Parameters
$arg1 : Token
$arg2 : Token
Return values
array<string|int, bool>

Like (boolean $bMismatchType, boolean $bBothNumeric, boolean $bBothString)

GetVarAttribute()

Get information about the variable, including JavaScript name, read-write status, and whether set on current page.

private GetVarAttribute(string $name, string|null $attr, string $default) : string
Parameters
$name : string
$attr : string|null
$default : string
Return values
string

HasSyntaxErrors()

Return true if there are syntax errors

private HasSyntaxErrors() : bool
Return values
bool

RDP_AddError()

Add an error to the error log

private RDP_AddError(string $errMsg, array<string|int, mixed>|null $token) : void
Parameters
$errMsg : string
$token : array<string|int, mixed>|null

RDP_EvaluateAdditiveExpression()

Process "a op b" where op in (+,-,concatenate)

private RDP_EvaluateAdditiveExpression() : bool
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateConstantVarOrFunction()

Process a Constant (number of string), retrieve the value of a known variable, or process a function, returning result on the stack.

private RDP_EvaluateConstantVarOrFunction() : bool|null
Return values
bool|null
  • true if success, false if any error occurred

RDP_EvaluateEqualityExpression()

Process "a == b", "a eq b", "a != b", "a ne b"

private RDP_EvaluateEqualityExpression() : bool
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateExpression()

Process a single expression (e.g. without commas)

private RDP_EvaluateExpression() : bool
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateExpressions()

Process "expression [, expression]*

private RDP_EvaluateExpressions() : bool
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateFunction()

Process a function call

private RDP_EvaluateFunction() : bool|null
Return values
bool|null
  • true if success, false if any error occurred

RDP_EvaluateLogicalAndExpression()

Process "a && b" or "a and b"

private RDP_EvaluateLogicalAndExpression() : bool
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateLogicalOrExpression()

Process "a || b" or "a or b"

private RDP_EvaluateLogicalOrExpression() : bool
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateMultiplicativeExpression()

Process "a op b" where op in (*,/)

private RDP_EvaluateMultiplicativeExpression() : bool
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluatePrimaryExpression()

Process expressions including functions and parenthesized blocks

private RDP_EvaluatePrimaryExpression() : bool|null
Return values
bool|null
  • true if success, false if any error occurred

RDP_EvaluateRelationExpression()

Process "a op b" where op in (lt, gt, le, ge, <, >, <=, >=)

private RDP_EvaluateRelationExpression() : bool
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateUnary()

Processes operations like +a, -b, !c

private RDP_EvaluateUnary(array<string|int, mixed> $token) : bool
Parameters
$token : array<string|int, mixed>
Return values
bool
  • true if success, false if any error occurred

RDP_EvaluateUnaryExpression()

Process "op a" where op in (+,-,!)

private RDP_EvaluateUnaryExpression() : bool|null
Return values
bool|null
  • true if success, false if any error occurred

RDP_isValidFunction()

Return true if the function name is registered

private RDP_isValidFunction(string $name) : bool
Parameters
$name : string
Return values
bool

RDP_isValidVariable()

Return true if the variable name is registered

private RDP_isValidVariable(string $name) : bool
Parameters
$name : string
Return values
bool

RDP_isWritableVariable()

Return true if the variable name is writable

private RDP_isWritableVariable(string $name) : bool
Parameters
$name : string
Return values
bool

RDP_RunFunction()

Run a registered function Some PHP functions require specific data types - those can be cast here.

private RDP_RunFunction(array<string|int, mixed> $funcNameToken, array<string|int, mixed> $params) : bool|null
Parameters
$funcNameToken : array<string|int, mixed>
$params : array<string|int, mixed>
Return values
bool|null

RDP_SetVariableValue()

Set the value of a registered variable

private RDP_SetVariableValue(string $op, string $name, string $value) : int
Parameters
$op : string
  • the operator (=,*=,/=,+=,-=)
$name : string
$value : string
Return values
int

RDP_Tokenize()

Split the source string into tokens, removing whitespace, and categorizing them by type.

private RDP_Tokenize(string $sSource[, bool $bOnEdit = false ]) : array<string|int, mixed>
Parameters
$sSource : string

: the string to tokenize

$bOnEdit : bool = false

: on edition, actually don't remove space

Return values
array<string|int, mixed>

        
On this page

Search results