Skip to content
Snippets Groups Projects
Evaluate.php 17.3 KiB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/12/16
 * Time: 4:36 PM
 */

Marc Egger's avatar
Marc Egger committed
namespace IMATHUZH\Qfq\Core;
Marc Egger's avatar
Marc Egger committed
use IMATHUZH\Qfq\Core\Database\Database;
use IMATHUZH\Qfq\Core\Helper\EncryptDecrypt;
Marc Egger's avatar
Marc Egger committed
use IMATHUZH\Qfq\Core\Helper\KeyValueStringParser;
use IMATHUZH\Qfq\Core\Helper\OnString;
use IMATHUZH\Qfq\Core\Helper\Path;
use IMATHUZH\Qfq\Core\Helper\Support;
Marc Egger's avatar
Marc Egger committed
use IMATHUZH\Qfq\Core\Report\Link;
use IMATHUZH\Qfq\Core\Report\Report;
use IMATHUZH\Qfq\Core\Report\Tablesorter;
use IMATHUZH\Qfq\Core\Store\Sip;
use IMATHUZH\Qfq\Core\Store\Store;
Carsten  Rose's avatar
Carsten Rose committed
/**
 * Class Evaluate
 * @package qfq
 */
Carsten  Rose's avatar
Carsten Rose committed
class Evaluate {
Carsten  Rose's avatar
Carsten Rose committed
    private $store = null;
     * @var Database[] - Array of Database instantiated class
    /**
     * @var Tablesorter
     */
    private $tablesorter = null;

    private $dbIndex = EVALUATE_DB_INDEX_DEFAULT;
Carsten  Rose's avatar
Carsten Rose committed
    private $startDelimiter = '';
    private $startDelimiterLength = 0;
    private $endDelimiter = '';
    private $endDelimiterLength = 0;
    private $sqlKeywords = array('SELECT ', 'INSERT ', 'DELETE ', 'UPDATE ', 'SHOW ', 'REPLACE ', 'TRUNCATE ', 'DESCRIBE ', 'EXPLAIN ', 'SET ', 'WITH ');
    private $escapeTypeDefault = '';
//    private $debugStack = array();
Marc Egger's avatar
Marc Egger committed
     * @param Store $store
Carsten  Rose's avatar
Carsten Rose committed
     * @param Database $db
     * @param string $startDelimiter
     * @param string $endDelimiter
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \UserFormException
Carsten  Rose's avatar
Carsten Rose committed
    public function __construct(Store $store, Database $db, $startDelimiter = '{{', $endDelimiter = '}}') {
        $this->store = $store;

        $this->dbArray[EVALUATE_DB_INDEX_DEFAULT] = $db;
Carsten  Rose's avatar
Carsten Rose committed
        $this->startDelimiter = $startDelimiter;
        $this->startDelimiterLength = strlen($startDelimiter);
        $this->endDelimiter = $endDelimiter;
        $this->endDelimiterLength = strlen($endDelimiter);
        $this->escapeTypeDefault = $this->store::getVar(F_ESCAPE_TYPE_DEFAULT, STORE_SYSTEM);
        if (empty($this->escapeTypeDefault) || $this->escapeTypeDefault == TOKEN_ESCAPE_CONFIG) {
            $this->escapeTypeDefault = $this->store::getVar(SYSTEM_ESCAPE_TYPE_DEFAULT, STORE_SYSTEM);
     * Evaluate a whole array or an array of arrays.
Carsten  Rose's avatar
Carsten Rose committed
     * @param       $tokenArray
     * @param array $skip Optional Array with keynames, which will not be evaluated.
     * @param array $debugStack
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
    public function parseArray($tokenArray, array $skip = array(), &$debugStack = array()) {
        // In case there is an Element 'fillStoreVar', process that first (if not defined to skip).
        $flagSkipFillStoreVar = (array_search(FE_FILL_STORE_VAR, $skip) !== false);
        if (!$flagSkipFillStoreVar && !empty($tokenArray[FE_FILL_STORE_VAR]) && is_string($tokenArray[FE_FILL_STORE_VAR])) {
            $arr = $this->parse($tokenArray[FE_FILL_STORE_VAR], ROW_REGULAR, 0, $debugStack);
            if (!empty($arr)) {
                $this->store::appendToStore($arr[0], STORE_VAR);
        foreach ($tokenArray as $key => $value) {
                $arr[$key] = $value;
                $value = Support::handleEscapeSpaceComment($value);
                $arr[$key] = $this->parse($value, ROW_IMPLODE_ALL, 0, $debugStack);
Carsten  Rose's avatar
Carsten Rose committed
     * Recursive evaluation of 'line'. Constant string, Variables or SQL Query or all of them. All queries will be
     * fired. In case of an 'INSERT' statement, return the last_insert_id().
     *
     * Token to replace have to be enclosed by '{{' and '}}'
     * @param string $sqlMode ROW_IMPLODE | ROW_REGULAR | ... - might be overwritten in $line by '{{!...'
     * @param array $debugStack
     * @param string $foundInStore
     *
     * @return array|mixed|null|string - in case of INSERT: last_insert_id()
     *
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
     */
    public function parse($line, $sqlMode = ROW_IMPLODE_ALL, $recursion = 0, &$debugStack = array(), &$foundInStore = '', $frCmd = '') {
        if ($line === '') {
            return '';
        }

        $flagTokenReplaced = false;
Carsten  Rose's avatar
Carsten Rose committed

        if ($recursion > 4) {
Marc Egger's avatar
Marc Egger committed
            throw new \UserFormException(
Marc Egger's avatar
Marc Egger committed
                json_encode([ERROR_MESSAGE_TO_USER => 'Recursion too deep', ERROR_MESSAGE_TO_DEVELOPER => "Level: $recursion, Line: $line"]),
        $result = $line;
        $recursion = ($recursion === null) ? 0 : $recursion;
        $debugIndent = str_repeat(' ', $recursion);
        $debugLocal[] = $debugIndent . "Parse: $result";
        $posFirstClose = strpos($result, $this->endDelimiter);
        $posLastClose = strrpos($result, $this->endDelimiter);
        // Variables like 'fillStoreVar' might contain SQL statements. Put them in store in case a DB exception is thrown.
        $this->store::setVar(SYSTEM_SQL_RAW, $line, STORE_SYSTEM);
        while ($posFirstClose !== false) {

            $posMatchOpen = strrpos(substr($result, 0, $posFirstClose), $this->startDelimiter);
Carsten  Rose's avatar
Carsten Rose committed
            if ($posMatchOpen === false) {
Marc Egger's avatar
Marc Egger committed
                throw new \UserFormException(
Marc Egger's avatar
Marc Egger committed
                    json_encode([ERROR_MESSAGE_TO_USER => 'Missing open delimiter', ERROR_MESSAGE_TO_DEVELOPER => "Text: $result"]),
            $pre = substr($result, 0, $posMatchOpen);
            $post = substr($result, $posFirstClose + $this->endDelimiterLength);
            $match = substr($result, $posMatchOpen + $this->startDelimiterLength, $posFirstClose - $posMatchOpen - $this->startDelimiterLength);
            $tmpSqlMode = ($posFirstClose == $posLastClose) ? $sqlMode : ROW_IMPLODE_ALL;
            $evaluated = $this->substitute($match, $foundInStore, $tmpSqlMode, $frCmd);
            $debugLocal[] = $debugIndent . "Replace: $match";
                // Encode the non replaceable part as preparation not to process again. Recode them at the end.
                $evaluated = Support::encryptDoubleCurlyBraces($this->startDelimiter . $match . $this->endDelimiter);
                $debugLocal[] = $debugIndent . "BY: <nothing found - not replaced>";
                // If an array is returned, break everything and return this assoc array.
                if (is_array($evaluated)) {
                    $result = $evaluated;
                    $debugLocal[] = $debugIndent . "BY: array(" . count($result) . ")";
                $debugLocal[] = $debugIndent . "BY: $evaluated";
                // More to substitute in the new evaluated result? Start recursion just with the new result..
                if ($foundInStore === TOKEN_FOUND_STOP_REPLACE) {
                    $evaluated = Support::encryptDoubleCurlyBraces($evaluated);
                } else {
                    if (strpos($evaluated, $this->endDelimiter) !== false) {
                        $evaluated = $this->parse($evaluated, ROW_IMPLODE_ALL, $recursion + 1, $debugLocal, $foundInStore, $frCmd);
            $result = $pre . $evaluated . $post;
            $posFirstClose = strpos($result, $this->endDelimiter);
        }
        $result = Support::decryptDoubleCurlyBraces($result);

        if ($flagTokenReplaced === true) {
            if (is_array($result)) {
                $str = "array(" . count($result) . ")";
            } else {
            }
            $debugLocal[] = $debugIndent . "FINAL: " . $str;
            $debugStack = $debugLocal;
    /**
     * @param $arrToken
     * @param $dbIndex
     * @param $foundInStore
     * @return string
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
    private function inlineLink($arrToken, $dbIndex, &$foundInStore): string {

        $token = OnString::trimQuote(trim(implode(' ', $arrToken)));

        if ($this->link === null) {
            $this->link = new Link($this->store::getSipInstance(), $dbIndex);
        }

        $foundInStore = TOKEN_FOUND_AS_COLUMN;

        return $this->link->renderLink($token);
    }

    /**
     * @param $arrToken
     * @param $dbIndex
     * @param $foundInStore
     * @return string
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
    private function inlineDataDndApi($arrToken, $dbIndex, &$foundInStore): string {

        $token = OnString::trimQuote(trim(implode(' ', $arrToken)));

        # Include current SIP store, to fetch SIP parameter later.
        $token .= '&' . DND_FORM_SIP_VALUES . '=' . $this->store::getVar(SIP_SIP, STORE_SIP);
Marc Egger's avatar
Marc Egger committed
            throw new \UserReportException('Missing form name for "data-dnd-api"', ERROR_MISSING_FORM);
        }

        if ($this->link === null) {
            $this->link = new Link($this->store::getSipInstance(), $dbIndex);
        }

        $foundInStore = TOKEN_FOUND_AS_COLUMN;

        $s = $this->link->renderLink('U:' . $token . '|s|r:8');

        // Flag to add DND JS code later on.
        $this->store::setVar(SYSTEM_DRAG_AND_DROP_JS, 'true', STORE_SYSTEM);
Marc Egger's avatar
Marc Egger committed
        // data-dnd-api="typo3conf/ext/qfq/qfq/Api/dragAndDrop.php?s={{'U:form=<form name>[&paramX=<any value>]|s|r:8' AS _link}}"
        return DND_DATA_DND_API . '="' . Path::urlApi(API_DRAG_AND_DROP_PHP) . '?s=' . $s . '"';
    /** Execute qfqFunction and output value. Content is accessible in record store
     *
     * @param $arrToken
     * @param $foundInStore
     * @return string
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
     * @throws \DbException
     */
    private function inlineFunction($arrToken, &$foundInStore): string {
        $output = '';
        $token = OnString::trimQuote(trim(implode(' ', $arrToken)));
        if ($this->report === null) {
            $this->report = new Report(array(), $this);
        }

        $this->report->doQfqFunction($token);
        $foundInStore = TOKEN_FOUND_AS_COLUMN;

        // Check for => in qfqFunction. If not given then output content.
        if (!strpos($token, '=>')) {
            $output = $this->store::getVar(COLUMN_FUNCTION_OUTPUT, STORE_RECORD);
        }

        return $output;
    }

Carsten  Rose's avatar
Carsten Rose committed
    /**
     * Tries to substitute $token.
     *   a) a SQL statement to fire
     *   b) fetch from a store. Syntax: '\[db index\]form:[store]:[sanitize]:[escape]:[default]:[type violate message]', ''
     * The token have to be *without* Delimiter '{{' , '}}'
     * If neither a) or b) match, return the token itself.
Carsten  Rose's avatar
Carsten Rose committed
     *
     * @param string $foundInStore Returns the name of the store where $key has been found. If $key is not found, return ''.
     * @param string $sqlMode - ROW_IMPLODE | ROW_REGULAR | ... - might be overwritten in $line by '{{!...'
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
     */
    public function substitute($token, &$foundInStore = '', $sqlMode = ROW_IMPLODE_ALL, $frCmd = '') {
Carsten  Rose's avatar
Carsten Rose committed

        $token = trim($token);

        // Check if the $token starts with '[<int>]...' - yes: open the necessary database.
        if (strlen($token) > 2 && $token[0] === '[') {
Marc Egger's avatar
Marc Egger committed
                throw new \UserFormException(json_encode(
                    [ERROR_MESSAGE_TO_USER => "Missing token ']' on position 3",
Marc Egger's avatar
Marc Egger committed
                        ERROR_MESSAGE_TO_DEVELOPER => "In string '$token'"]), ERROR_TOKEN_MISSING);
            }
            $dbIndex = $token[1];
            $token = trim(substr($token, 3));

            if (empty($this->dbArray[$dbIndex])) {
                $this->dbArray[$dbIndex] = new Database($dbIndex);
            }
        }
        // Get SQL column / row separated
Carsten  Rose's avatar
Carsten Rose committed
        if ($token[0] === '!') {
Carsten  Rose's avatar
Carsten Rose committed
            $sqlMode = ROW_REGULAR;
        }

        // In case there is a statement starting with '(' (multiple): remove them just to detect if the following is a SQL statement.
        // E.g. original:  "{{[1] ! ( SELECT name FROM person ORDER BY name UNION SELECT 'new'}}"
        $tokenClean = OnString::removeLeadingBrace($token);

        // Extract token: check if this is a 'variable', 'SQL Statement', 'link', 'data-dnd-api'
        $arrToken = explode(' ', $tokenClean);
        if (in_array(strtoupper($arrToken[VAR_INDEX_VALUE] . ' '), $this->sqlKeywords)) {
            return $this->dbArray[$dbIndex]->sql($token, $sqlMode);
        // Variable Type '... AS _link', '... as data-dnd-api', '... AS _tablesorter-view-saver'
        if ($countToken > 2 && strtolower($arrToken[$countToken - 2]) == 'as') {

            $type = OnString::stripFirstCharIf('_', $arrToken[$countToken - 1]);

            array_pop($arrToken); // remove 'link' | 'data-dnd-api' | 'tablesorter-view-saver'
            array_pop($arrToken); // remove 'as'
Carsten  Rose's avatar
Carsten Rose committed

            switch (strtolower($type)) {
                case COLUMN_LINK:
                    return ($this->inlineLink($arrToken, $dbIndex, $foundInStore));
                    break;

                case DND_DATA_DND_API:
                    return ($this->inlineDataDndApi($arrToken, $dbIndex, $foundInStore));
                    break;
                case TABLESORTER_VIEW_SAVER:
                    if ($this->tablesorter === null) {
Carsten  Rose's avatar
Carsten Rose committed

                        // Read settings always from dbIndexQfq
                        $dbIndexQfq = $this->store::getVar(SYSTEM_DB_INDEX_QFQ, STORE_SYSTEM);
                        if (empty($this->dbArray[$dbIndexQfq])) {
                            $this->dbArray[$dbIndexQfq] = new Database($dbIndexQfq);
                        }

                        $this->tablesorter = new Tablesorter($dbIndexQfq);
                    // create baseUrl attribute for tablesorter api
                    $baseUrlAttribute = DATA_TABLESORTER_BASE_URL . "='" . $this->store->getVar(SYSTEM_BASE_URL, STORE_SYSTEM) . "'";
                    return ($this->tablesorter->inlineTablesorterView($arrToken[VAR_INDEX_VALUE], $foundInStore, $frCmd)) . $baseUrlAttribute;

                case COLUMN_FUNCTION:
                    return ($this->inlineFunction($arrToken, $foundInStore));
                    break;
                default:
                    break;
        // explode for: <key>:<store priority>:<sanitize class>:<escape>:<default>:<type violate message>
        $arrToken = array_merge(KeyValueStringParser::explodeEscape(':', $token, 6), [null, null, null, null, null, null]);
        $escapeTypes = (empty($arrToken[VAR_INDEX_ESCAPE])) ? $this->escapeTypeDefault : $arrToken[VAR_INDEX_ESCAPE];
        $typeMessageViolate = ($arrToken[VAR_INDEX_MESSAGE] === null || $arrToken[VAR_INDEX_MESSAGE] === '') ? SANITIZE_TYPE_MESSAGE_VIOLATE_CLASS : $arrToken[VAR_INDEX_MESSAGE];
Carsten  Rose's avatar
Carsten Rose committed

        // search for value in stores
Carsten  Rose's avatar
Carsten Rose committed
        $value = $this->store::getVar($arrToken[VAR_INDEX_VALUE], $arrToken[VAR_INDEX_STORE], $arrToken[VAR_INDEX_SANITIZE],
            $foundInStore, $typeMessageViolate, $arrToken[VAR_INDEX_DEFAULT]);
        $value = OnString::escape($escapeTypes, $value, $rcFlagWipe);
        if (($foundInStore == '' || $value === '') && $arrToken[VAR_INDEX_DEFAULT] != '') {
            $foundInStore = TOKEN_FOUND_AS_DEFAULT;
            $value = str_replace('\\:', ':', $arrToken[VAR_INDEX_DEFAULT]);
                    $this->store::unsetVar($arrToken[VAR_INDEX_VALUE], STORE_SIP);
                    $sip->removeKeyFromSip($this->store::getVar(SIP_SIP, STORE_SIP), $arrToken[VAR_INDEX_VALUE]);
Carsten  Rose's avatar
Carsten Rose committed
                    throw new \UserReportException("Wipe not implemented for store $foundInStore", ERROR_WIPE_NOT_IMPLEMENTED_FOR_STORE);
Carsten  Rose's avatar
Carsten Rose committed
    }
    /**
     * @return string
     */
//    public function getDebug() {
//        return '<pre>' . implode("\n", $this->debugStack) . '</pre>';
//    }