Files
luos/workflow/engine/classes/class.pmScript.php

727 lines
32 KiB
PHP
Raw Normal View History

2010-12-02 23:34:41 +00:00
<?php
2017-08-01 12:16:06 -04:00
use ProcessMaker\Plugins\PluginRegistry;
2012-10-09 12:59:01 -04:00
2018-06-04 12:33:56 -04:00
function __autoload($sClassName)
2010-12-02 23:34:41 +00:00
{
2017-10-10 12:33:25 -04:00
if (!empty(config("system.workspace"))) {
$sPath = PATH_DB . config("system.workspace") . PATH_SEP . 'classes' . PATH_SEP;
2018-06-04 12:33:56 -04:00
if (file_exists($sPath . $sClassName . '.php')) {
require_once $sPath . $sClassName . '.php';
}
2010-12-02 23:34:41 +00:00
}
}
2017-10-10 12:33:25 -04:00
if (!empty(config("system.workspace")) && (!defined('PATH_DATA_SITE') || !defined('PATH_WORKSPACE'))) {
Bootstrap::setConstantsRelatedWs(config("system.workspace"));
2016-10-19 16:31:32 -04:00
}
2010-12-02 23:34:41 +00:00
//Add External Triggers
2018-06-04 12:33:56 -04:00
$dir = G::ExpandPath("classes") . 'triggers';
$filesArray = [];
if (file_exists($dir)) {
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (($file != ".") && ($file != "..")) {
2018-06-04 12:33:56 -04:00
$extFile = explode(".", $file);
if ($extFile[sizeof($extFile) - 1] == 'php') {
2012-10-09 12:59:01 -04:00
include_once ($dir . PATH_SEP . $file);
}
}
}
2018-06-04 12:33:56 -04:00
closedir($handle);
}
2010-12-02 23:34:41 +00:00
}
/**
* PMScript - PMScript class
2012-10-09 12:59:01 -04:00
*
2011-02-01 12:49:40 +00:00
* @package workflow.engine.ProcessMaker
2010-12-02 23:34:41 +00:00
*/
class PMScript
{
2018-10-30 13:41:00 -04:00
/**
* Constants to identify the execution origin
*/
const UNDEFINED_ORIGIN = 'executed.undefined.origin';
const BEFORE_DYNAFORM = 'executed.before.dynaform';
const AFTER_DYNAFORM = 'executed.after.dynaform';
const BEFORE_INPUT_DOCUMENT = 'executed.before.input';
const AFTER_INPUT_DOCUMENT = 'executed.after.input';
const BEFORE_OUTPUT_DOCUMENT = 'executed.before.output';
const AFTER_OUTPUT_DOCUMENT = 'executed.after.output';
const BEFORE_EXTERNAL_STEP = 'executed.before.external';
const AFTER_EXTERNAL_STEP = 'executed.after.external';
const BEFORE_ASSIGNMENT = 'executed.before.assignment';
const BEFORE_ROUTING = 'executed.before.routing';
const AFTER_ROUTING = 'executed.after.routing';
const CONDITION = 'executed.condition';
const SCRIPT_TASK = 'executed.script.task';
const CLASSIC_PROCESS_EVENTS = 'executed.classic.process.events';
const SELF_SERVICE_TIMEOUT = 'executed.selfservice.timeout';
const ISOLATED_TRIGGER = 'executed.isolated.trigger';
const PROCESS_ACTION = 'executed.process.action';
const EVALUATE_FUNCTION = 'executed.evaluate.function';
/**
* @var array $dataTrigger
*/
2018-06-04 12:33:56 -04:00
public $dataTrigger = [];
/**
* Original fields
*/
2018-06-04 12:33:56 -04:00
public $aOriginalFields = [];
/**
* Fields to use
*/
2018-06-04 12:33:56 -04:00
public $aFields = [];
/**
* Script
*/
public $sScript = '';
/**
* Error has happened?
*/
public $bError = false;
/**
* Affected fields
*/
public $affected_fields = [];
public $scriptExecutionTime = 0;
2016-11-17 06:54:34 -05:00
public $sRegexp = '/\@(?:([\@\%\#\?\$\=\&])([a-zA-Z\_]\w*)|([a-zA-Z\_][\w\-\>\:]*)\(((?:[^\\\\\)]*(?:[\\\\][\w\W])?)*)\))((?:\s*\[[\'"]?\w+[\'"]?\])+|\-\>([a-zA-Z\_]\w*))?/';
2018-10-30 13:41:00 -04:00
/**
* Execution origin, by default is undefined
*/
protected $executedOn = self::UNDEFINED_ORIGIN;
2018-11-21 16:32:48 -04:00
/**
* Variables changed in the trigger execution
*/
private $varsChanged = [];
/**
* Constructor of the class PMScript
2012-10-09 12:59:01 -04:00
*
* @return void
*/
2018-10-30 13:41:00 -04:00
public function __construct()
{
$this->aFields['__ERROR__'] = 'none';
2010-12-02 23:34:41 +00:00
}
2018-11-21 16:32:48 -04:00
/**
* Set the fields changed in the trigger execution
*
* @param array $v
*
* @return void
*/
public function setVarsChanged(array $v)
{
$this->varsChanged = $v;
}
/**
* Get the fields changed in the trigger execution
*
* @return array
*/
public function getVarsChanged()
{
return $this->varsChanged;
}
/**
* Set the fields to use
2012-10-09 12:59:01 -04:00
*
* @param array $aFields
* @return void
*/
2018-06-04 12:33:56 -04:00
public function setFields($aFields = [])
{
2018-06-04 12:33:56 -04:00
if (!is_array($aFields)) {
$aFields = [];
2010-12-02 23:34:41 +00:00
}
$this->aOriginalFields = $this->aFields = $aFields;
2010-12-02 23:34:41 +00:00
}
/**
* Set the current script
2012-10-09 12:59:01 -04:00
*
* @param string $sScript
* @return void
*/
2018-06-04 12:33:56 -04:00
public function setScript($sScript = '')
{
if (!defined("T_ML_COMMENT")) {
define("T_ML_COMMENT", T_COMMENT);
} else {
2014-04-10 12:52:13 -04:00
if (!defined("T_DOC_COMMENT")) {
define("T_DOC_COMMENT", T_ML_COMMENT);
}
}
$script = "<?php " . $sScript;
$tokens = token_get_all($script);
$result = "";
foreach ($tokens as $token) {
if (is_string($token)) {
2014-04-10 12:52:13 -04:00
$result .= $token;
} else {
list($id, $text) = $token;
switch ($id) {
case T_OPEN_TAG:
case T_CLOSE_TAG:
case T_COMMENT:
case T_ML_COMMENT: //we've defined this
case T_DOC_COMMENT: //and this
2014-04-10 12:52:13 -04:00
if ($text != '<?php ' && $text != '<?php' && $text != '<? ' && $text != '<?' && $text != '<% ' && $text != '<%') {
$result .= $text;
}
break;
default:
2014-04-10 12:52:13 -04:00
$result .= $text;
break;
}
}
}
2014-04-10 12:52:13 -04:00
$this->sScript = trim($result);
2010-12-02 23:34:41 +00:00
}
/**
* Verify the syntax
2012-10-09 12:59:01 -04:00
*
* @param string $sScript
* @return boolean
*/
2018-06-04 12:33:56 -04:00
public function validSyntax($sScript)
{
return true;
}
/**
* @param $dataTrigger
*/
public function setDataTrigger($dataTrigger)
{
2018-06-04 12:33:56 -04:00
$this->dataTrigger = is_array($dataTrigger) ? $dataTrigger : [];
}
2018-10-30 13:41:00 -04:00
/**
* Set the execution origin
*
* @param string $executedOn
*/
public function setExecutedOn($executedOn)
{
$this->executedOn = $executedOn;
}
/**
* Get the execution origin
*
* @return string
*/
public function executedOn() {
return $this->executedOn;
}
/**
* Helper to get the execution origin from an step
*
* @param string $stepType
* @param mixed $stepUidObj
* @param string $triggerType
*
* @return string
*/
public function getExecutionOriginForAStep($stepType, $stepUidObj, $triggerType)
{
switch ($stepType) {
case 'DYNAFORM':
$executedOn = $triggerType === 'BEFORE' ? self::BEFORE_DYNAFORM : $triggerType === 'AFTER' ?
self::AFTER_DYNAFORM : self::UNDEFINED_ORIGIN;
break;
case 'INPUT_DOCUMENT':
$executedOn = $triggerType === 'BEFORE' ? self::BEFORE_INPUT_DOCUMENT : $triggerType === 'AFTER' ?
self::AFTER_INPUT_DOCUMENT : self::UNDEFINED_ORIGIN;
break;
case 'OUTPUT_DOCUMENT':
$executedOn = $triggerType === 'BEFORE' ? self::BEFORE_OUTPUT_DOCUMENT : $triggerType === 'AFTER' ?
self::AFTER_OUTPUT_DOCUMENT : self::UNDEFINED_ORIGIN;
break;
case 'EXTERNAL':
$executedOn = $triggerType === 'BEFORE' ? self::BEFORE_EXTERNAL_STEP : $triggerType === 'AFTER' ?
self::AFTER_EXTERNAL_STEP : self::UNDEFINED_ORIGIN;
break;
case 'ASSIGN_TASK':
2019-02-04 11:44:48 -04:00
$stepUidObj = (int)$stepUidObj;
2018-10-30 13:41:00 -04:00
if ($stepUidObj === -1) {
$executedOn = $triggerType === 'BEFORE' ? self::BEFORE_ASSIGNMENT : self::UNDEFINED_ORIGIN;
} elseif ($stepUidObj === -2) {
2019-02-04 11:44:48 -04:00
$executedOn = $triggerType === 'BEFORE' ? self::BEFORE_ROUTING : ($triggerType === 'AFTER' ?
self::AFTER_ROUTING : self::UNDEFINED_ORIGIN);
2018-10-30 13:41:00 -04:00
} else {
$executedOn = self::UNDEFINED_ORIGIN;
}
break;
2019-01-30 09:11:57 -04:00
case 'PROCESS_ACTION':
$executedOn = self::PROCESS_ACTION;
break;
case 'SCRIPT_TASK':
$executedOn = self::SCRIPT_TASK;
break;
2018-10-30 13:41:00 -04:00
default:
$executedOn = self::UNDEFINED_ORIGIN;
break;
}
return $executedOn;
}
/**
* @param $sScript
* @param $sCode
*/
public function executeAndCatchErrors($sScript, $sCode)
{
ob_start('handleFatalErrors');
2019-01-10 11:03:07 -04:00
set_error_handler('handleErrors', ini_get('error_reporting'));
$_SESSION['_CODE_'] = $sCode;
$_SESSION['_DATA_TRIGGER_'] = $this->dataTrigger;
$_SESSION['_DATA_TRIGGER_']['_EXECUTION_TIME_'] = microtime(true);
eval($sScript);
$this->scriptExecutionTime = round(microtime(true) -
2018-06-04 12:33:56 -04:00
$_SESSION['_DATA_TRIGGER_']['_EXECUTION_TIME_'], 5);
$this->evaluateVariable();
ob_end_flush();
//log trigger execution in processmaker.log
G::logTriggerExecution($_SESSION, '', '', $this->scriptExecutionTime);
unset($_SESSION['_CODE_']);
unset($_SESSION['_DATA_TRIGGER_']);
}
/**
* Execute the current script
2012-10-09 12:59:01 -04:00
*
* @return void
*/
2018-06-04 12:33:56 -04:00
public function execute()
{
$sScript = "";
$iAux = 0;
2018-06-04 12:33:56 -04:00
$iOcurrences = preg_match_all($this->sRegexp, $this->sScript, $aMatch, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE);
if ($iOcurrences) {
2012-10-09 12:59:01 -04:00
for ($i = 0; $i < $iOcurrences; $i ++) {
$bEqual = false;
2018-06-04 12:33:56 -04:00
$sAux = substr($this->sScript, $iAux, $aMatch[0][$i][1] - $iAux);
if (!$bEqual) {
if (strpos($sAux, "==") !== false || strpos($sAux, "!=") !== false || strpos($sAux, ">") !== false || strpos($sAux, "<") !== false || strpos($sAux, ">=") !== false || strpos($sAux, "<=") !== false || strpos($sAux, "<>") !== false || strpos($sAux, "===") !== false || strpos($sAux, "!==") !== false) {
$bEqual = false;
} else {
if (strpos($sAux, "=") !== false || strpos($sAux, "+=") !== false || strpos($sAux, "-=") !== false || strpos($sAux, "*=") !== false || strpos($sAux, "/=") !== false || strpos($sAux, "%=") !== false || strpos($sAux, ".=") !== false) {
$bEqual = true;
}
}
}
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (strpos($sAux, ';') !== false) {
$bEqual = false;
}
}
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
eval("if (!isset(\$this->aFields['" . $aMatch[2][$i][0] . "'])) { \$this->aFields['" . $aMatch[2][$i][0] . "'] = " . ($aMatch[1][$i][0] == "&" ? "new stdclass()" : "null") . "; }");
} else {
2016-11-17 06:54:34 -05:00
if ($aMatch[1][$i][0] == "&") {
2018-06-04 12:33:56 -04:00
eval("if (!isset(\$this->aFields['" . $aMatch[2][$i][0] . "'])) { \$this->aFields['" . $aMatch[2][$i][0] . "'] = new stdclass(); }");
2016-11-17 06:54:34 -05:00
}
2018-06-04 12:33:56 -04:00
eval("if (!isset(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")) { \$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . " = null; }");
}
2016-11-23 10:21:43 -05:00
} else {
if ($aMatch[1][$i][0] == "&") {
2018-06-04 12:33:56 -04:00
eval("if (!isset(\$this->aFields['" . $aMatch[2][$i][0] . "'])) { \$this->aFields['" . $aMatch[2][$i][0] . "'] = new stdclass(); }");
2016-11-23 10:21:43 -05:00
}
}
$sScript .= $sAux;
2018-06-04 12:33:56 -04:00
$iAux = $aMatch[0][$i][1] + strlen($aMatch[0][$i][0]);
switch ($aMatch[1][$i][0]) {
case '@':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmToString(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmToString(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '%':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmToInteger(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmToInteger(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '#':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmToFloat(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmToFloat(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '?':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmToUrl(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmToUrl(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '$':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmSqlEscape(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmSqlEscape(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '=':
2016-11-17 06:54:34 -05:00
case '&':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
}
$this->affected_fields[] = $aMatch[2][$i][0];
}
2010-12-02 23:34:41 +00:00
}
2018-06-04 12:33:56 -04:00
$sScript .= substr($this->sScript, $iAux);
2012-10-09 12:59:01 -04:00
$sScript = "try {\n" . $sScript . "\n} catch (Exception \$oException) {\n " . " \$this->aFields['__ERROR__'] = utf8_encode(\$oException->getMessage());\n}";
$this->executeAndCatchErrors($sScript, $this->sScript);
2019-01-16 12:10:25 -04:00
//We get the affected_fields only if has the prefix
//@see https://wiki.processmaker.com/3.2/Triggers#Typing_rules_for_Case_Variables
2018-11-21 16:32:48 -04:00
$this->setVarsChanged($this->affected_fields);
$this->aFields["__VAR_CHANGED__"] = implode(",", $this->affected_fields);
2018-06-04 12:33:56 -04:00
for ($i = 0; $i < count($this->affected_fields); $i ++) {
$_SESSION['TRIGGER_DEBUG']['DATA'][] = Array('key' => $this->affected_fields[$i], 'value' => isset($this->aFields[$this->affected_fields[$i]]) ? $this->aFields[$this->affected_fields[$i]] : ''
);
2010-12-02 23:34:41 +00:00
}
}
/**
* Evaluate the current script
2012-10-09 12:59:01 -04:00
*
2016-08-02 11:22:27 -04:00
* @return boolean
*/
2018-06-04 12:33:56 -04:00
public function evaluate()
{
$bResult = null;
$sScript = '';
$iAux = 0;
$bEqual = false;
$variableIsDefined = true;
2018-06-04 12:33:56 -04:00
$iOcurrences = preg_match_all($this->sRegexp, $this->sScript, $aMatch, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE);
if ($iOcurrences) {
2012-10-09 12:59:01 -04:00
for ($i = 0; $i < $iOcurrences; $i ++) {
// if the variables for that condition has not been previously defined then $variableIsDefined
// is set to false
if (!isset($this->aFields[$aMatch[2][$i][0]]) && !isset($aMatch[5][$i][0])) {
2018-06-04 12:33:56 -04:00
eval("if (!isset(\$this->aFields['" . $aMatch[2][$i][0] . "'])) { \$this->aFields['" . $aMatch[2][$i][0] . "'] = " . ($aMatch[1][$i][0] == "&" ? "new stdclass()" : "null") . "; }");
} else {
2016-11-17 06:54:34 -05:00
if ($aMatch[1][$i][0] == "&") {
2018-06-04 12:33:56 -04:00
eval("if (!isset(\$this->aFields['" . $aMatch[2][$i][0] . "'])) { \$this->aFields['" . $aMatch[2][$i][0] . "'] = new stdclass(); }");
2016-11-17 06:54:34 -05:00
}
if (!isset($this->aFields[$aMatch[2][$i][0]])) {
eval("\$this->aFields['" . $aMatch[2][$i][0] . "']" . $aMatch[5][$i][0] . " = '';");
} else {
if (isset($aMatch[5][$i][0])) {
eval("if (!isset(\$this->aFields['" . $aMatch[2][$i][0] . "']" . $aMatch[5][$i][0] . ")) {\$this->aFields['" . $aMatch[2][$i][0] . "']" . $aMatch[5][$i][0] . " = '';}");
} else {
2016-11-17 06:54:34 -05:00
eval("if (!isset(\$this->aFields['" . $aMatch[2][$i][0] . "'])) {\$this->aFields['" . $aMatch[2][$i][0] . "'] = " . ($aMatch[1][$i][0] == "&" ? "new stdclass()" : "''") . ";}");
}
}
}
2018-06-04 12:33:56 -04:00
$sAux = substr($this->sScript, $iAux, $aMatch[0][$i][1] - $iAux);
if (!$bEqual) {
if (strpos($sAux, '=') !== false) {
$bEqual = true;
}
}
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (strpos($sAux, ';') !== false) {
$bEqual = false;
}
}
$sScript .= $sAux;
2018-06-04 12:33:56 -04:00
$iAux = $aMatch[0][$i][1] + strlen($aMatch[0][$i][0]);
switch ($aMatch[1][$i][0]) {
case '@':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmToString(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmToString(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '%':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmToInteger(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmToInteger(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '#':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmToFloat(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmToFloat(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '?':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmToUrl(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmToUrl(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '$':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "pmSqlEscape(\$this->aFields['" . $aMatch[2][$i][0] . "'])";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "pmSqlEscape(\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0] . ")";
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
case '=':
2016-11-17 06:54:34 -05:00
case '&':
if ($bEqual) {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
} else {
2018-06-04 12:33:56 -04:00
if (!isset($aMatch[5][$i][0])) {
$sScript .= "\$this->aFields['" . $aMatch[2][$i][0] . "']";
} else {
2018-06-04 12:33:56 -04:00
$sScript .= "\$this->aFields" . (isset($aMatch[2][$i][0]) ? "['" . $aMatch[2][$i][0] . "']" : '') . $aMatch[5][$i][0];
}
}
break;
}
2010-12-02 23:34:41 +00:00
}
}
2018-06-04 12:33:56 -04:00
$sScript .= substr($this->sScript, $iAux);
if (preg_match('/\b(or|and|xor)\b/i', $sScript)) {
$sScript = "( " . $sScript . " )";
}
$sScript = '$bResult = ' . $sScript . ';';
// checks if the syntax is valid or if the variables in that condition has been previously defined
2018-06-04 12:33:56 -04:00
if ($this->validSyntax($sScript) && $variableIsDefined) {
$this->bError = false;
2018-06-04 12:33:56 -04:00
eval($sScript);
} else {
2018-06-04 12:33:56 -04:00
G::SendTemporalMessage('MSG_CONDITION_NOT_DEFINED', 'error', 'labels');
$this->bError = true;
}
return $bResult;
2010-12-02 23:34:41 +00:00
}
2018-06-04 12:33:56 -04:00
Public function evaluateVariable()
{
$process = new Process();
2018-06-04 12:33:56 -04:00
if (!$process->isBpmnProcess($_SESSION['PROCESS'])) {
return;
}
2018-06-04 12:33:56 -04:00
require_once PATH_CORE . 'controllers/pmTablesProxy.php';
$pmTablesProxy = new pmTablesProxy();
$variableModule = new ProcessMaker\BusinessModel\Variable();
2018-06-04 12:33:56 -04:00
$searchTypes = array('checkgroup', 'dropdown', 'suggest');
$processVariables = $pmTablesProxy->getDynaformVariables($_SESSION['PROCESS'], $searchTypes, false);
$variables = $this->affected_fields;
2018-06-04 12:33:56 -04:00
$variables = (is_array($variables)) ? array_unique($variables) : $variables;
$newFields = [];
$arrayValues = [];
$arrayLabels = [];
if (is_array($variables) && is_array($processVariables)) {
foreach ($variables as $var) {
if (strpos($var, '_label') === false) {
if (in_array($var, $processVariables)) {
if (isset($this->aFields[$var]) && is_array($this->aFields[$var][1])) {
$varLabel = $var . '_label';
$arrayValue = $this->aFields[$var];
2018-06-04 12:33:56 -04:00
if (is_array($arrayValue) && sizeof($arrayValue)) {
foreach ($arrayValue as $val) {
if (is_array($val)) {
$val = array_values($val);
$arrayValues[] = $val[0];
$arrayLabels[] = $val[1];
}
}
2018-06-04 12:33:56 -04:00
if (sizeof($arrayLabels)) {
$varInfo = $variableModule->getVariableTypeByName($_SESSION['PROCESS'], $var);
if (is_array($varInfo) && sizeof($varInfo)) {
$varType = $varInfo['VAR_FIELD_TYPE'];
2018-06-04 12:33:56 -04:00
switch ($varType) {
case 'array':
2018-06-04 12:33:56 -04:00
$arrayLabels = '["' . implode('","', $arrayLabels) . '"]';
$newFields[$var] = $arrayValues;
$newFields[$varLabel] = $arrayLabels;
2018-06-04 12:33:56 -04:00
break;
case 'string':
$newFields[$var] = $arrayValues[0];
$newFields[$varLabel] = $arrayLabels[0];
2018-06-04 12:33:56 -04:00
break;
}
$this->affected_fields[] = $varLabel;
2018-06-04 12:33:56 -04:00
$this->aFields = array_merge($this->aFields, $newFields);
unset($newFields);
unset($arrayValues);
unset($arrayLabels);
}
}
}
}
2015-12-07 17:53:33 -04:00
if (isset($this->aFields[$var]) && is_string($this->aFields[$var])) {
$varInfo = $variableModule->getVariableTypeByName($_SESSION['PROCESS'], $var);
$options = G::json_decode($varInfo["VAR_ACCEPTED_VALUES"]);
$no = count($options);
for ($io = 0; $io < $no; $io++) {
if ($options[$io]->value === $this->aFields[$var]) {
$this->aFields[$var . "_label"] = $options[$io]->label;
}
}
if ($varInfo["VAR_DBCONNECTION"] !== "" && $varInfo["VAR_DBCONNECTION"] !== "none" && $varInfo["VAR_SQL"] !== "") {
try {
$cnn = Propel::getConnection($varInfo["VAR_DBCONNECTION"]);
$stmt = $cnn->createStatement();
2019-07-01 11:49:26 -04:00
$sql = G::replaceDataField($varInfo["VAR_SQL"], $this->aFields, 'mysql', false);
2015-12-07 17:53:33 -04:00
$rs = $stmt->executeQuery($sql, \ResultSet::FETCHMODE_NUM);
while ($rs->next()) {
$row = $rs->getRow();
if ($row[0] === $this->aFields[$var]) {
$this->aFields[$var . "_label"] = isset($row[1]) ? $row[1] : $row[0];
}
}
} catch (Exception $e) {
}
}
}
}
}
}
}
}
2018-06-04 12:33:56 -04:00
}