<?php
/**
 * ZenTaoPHP的验证和过滤类。
 * The validater and fixer class file of ZenTaoPHP framework.
 *
 * The author disclaims copyright to this source code.  In place of
 * a legal notice, here is a blessing:
 * 
 *  May you do good and not evil.
 *  May you find forgiveness for yourself and forgive others.
 *  May you share freely, never taking more than you give.
 */

helper::import(dirname(dirname(__FILE__)) . '/base/filter/filter.class.php');
/**
 * validater类，检查数据是否符合规则。
 * The validater class, checking data by rules.
 * 
 * @package framework
 */
class validater extends baseValidater
{
}

/**
 * fixer类，处理数据。
 * fixer class, to fix data types.
 * 
 * @package framework
 */
class fixer extends baseFixer
{
    /**
     * 忽略该标签。
     * Strip tags 
     * 
     * @param  string $fieldName 
     * @param  string $allowableTags 
     * @param  array  $attributes
     * @access public
     * @return object fixer object
     */
    public function stripTags($fieldName, $allowedTags = '', $attributes = array())
    {
        $fields = $this->processFields($fieldName);
        foreach($fields as $fieldName)
        {
            /* Filter out mark for tiptap editor. */
            $this->data->$fieldName = str_replace('<mark>', "<span class='tiptap-mark'>", $this->data->$fieldName);
            $this->data->$fieldName = str_replace('</mark>', "</span>", $this->data->$fieldName);

            if(version_compare(phpversion(), '5.4', '<') && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) $this->data->$fieldName = stripslashes($this->data->$fieldName);

            /* Filter out emoji. */
            $this->data->$fieldName = preg_replace_callback('/./u', function(array $match){return strlen($match[0]) >= 4 ? '' : $match[0];}, $this->data->$fieldName);

            if(!isset($this->stripedFields[$fieldName]) and (!defined('RUN_MODE') or RUN_MODE != 'admin'))
            {
                $this->data->$fieldName = self::stripDataTags($this->data->$fieldName, $allowedTags, $attributes);

                /* Code for bug #2721. */
                $this->data->$fieldName = baseValidater::replaceSpace2Tag($this->data->$fieldName);
            }
            $this->stripedFields[$fieldName] = $fieldName;
        }
        return $this;
    }

    /**
     * 如果提交的数据中包含工作流中扩展的字段，且字段是以数组的形式提交，则把提交的值拼接成字符串，防止出错。
     * If the submitted data contains extended fields in the workflow and the fields are submitted as arrays,
     * the submitted values are concatenated into strings to prevent errors.
     * 
     * @param  string $fields
     * @access public
     * @return object
     */
    public function get($fields = '')
    {
        /* Get extend field by flow. */
        global $app;
        if($app->isBiz)
        {
            global $dbh;
            $flowFields = array();
            $moduleName = $app->rawModule;
            $stmt       = $dbh->query("SELECT * FROM " . TABLE_WORKFLOWFIELD . " WHERE `module` = '{$moduleName}' and `buildin` = '0'");
            while($flowField = $stmt->fetch()) $flowFields[$flowField->field] = $flowField;

            foreach($flowFields as $field => $fieldObject)
            {
                if(!isset($this->data->$field)) continue;

                $value = $this->data->$field;
                if(is_array($value))
                {
                    $canImplode = true;
                    foreach($value as $k => $v)
                    {
                        if(is_object($v) or is_array($v))
                        {
                            $canImplode = false;
                            break;
                        }
                    }
                    if($canImplode) $this->data->$field = implode(',', $value);
                }
                if($fieldObject->control == 'textarea') $this->skipSpecial($field);
            }
        }

        return parent::get($fields);
    }
}
