Esempio n. 1
0
/**
 * Saves updated values of config list in DB
 *
 * @param string $name Extension or Section name config belongs to
 * @param array $optionslist Option list as return by cot_config_list()
 * @param mixed $is_module Flag indicating if it is module or plugin config
 * @param string $update_new_only Update changes values only
 * @param string $source Source of imported data
 * @return boolean|number Number of updated values
 */
function cot_config_update_options($name, &$optionslist, $is_module = false, $update_new_only = true, $source = 'POST')
{
    global $cot_import_filters;
    if (!is_array($optionslist)) {
        return false;
    }
    $new_options = array();
    //$cfg_var = $val;
    foreach ($optionslist as $cfg_name => $cfg_var) {
        // Visual separator/fieldset have no value
        if ($cfg_var['config_type'] == COT_CONFIG_TYPE_SEPARATOR) {
            continue;
        }
        $filtered = FALSE;
        $builtin_filter = FALSE;
        $data = $raw_input = cot_import($cfg_name, $source, 'NOC');
        $custom_type = $cfg_var['config_type'] == COT_CONFIG_TYPE_CUSTOM && $cfg_var['config_variants'] && preg_match('#^(\\w+)\\((.*?)\\)$#', $cfg_var['config_variants'], $mt);
        if ($custom_type) {
            $custom_func = $mt[1];
            $custom_filter_func = $custom_func . '_filter';
            // use addition custom function for filtration if exists
            if (function_exists($custom_filter_func)) {
                $callback_params = preg_split('#\\s*,\\s*#', $mt[2]);
                if (count($callback_params) > 0 && !empty($callback_params[0])) {
                    for ($i = 0; $i < count($callback_params); $i++) {
                        $callback_params[$i] = str_replace(array("'", '"'), array('', ''), $callback_params[$i]);
                    }
                }
                /**
                 * Filters Value with custom function
                 * @param string $data User input value
                 * @param array $cfg_var Config Variable data
                 *  ...   other callback params defined for function
                 * @return NULL|mixed Filtered Value or NULL in case Value can not be filtered.
                 * @see cot_config_type_int_filter() as example
                 */
                $filtered = call_user_func_array($custom_filter_func, array_merge(array(&$raw_input, $cfg_var), $callback_params));
            } else {
                // last part of custom function name may treats as built-in filter type
                list($base_filter) = array_reverse(explode('_', strtoupper($custom_func)));
                if (in_array(strtoupper($base_filter), array('INT', 'BOL', 'PSW', 'ALP', 'TXT', 'NUM')) || sizeof($cot_import_filters[$base_filter])) {
                    $filtered = cot_config_import($cfg_name, $source, $base_filter);
                    $builtin_filter = true;
                }
            }
        }
        if (is_null($filtered)) {
            $optionslist[$cfg_name]['config_value'] = $builtin_filter ? '' : $raw_input;
        } else {
            if (false !== $filtered) {
                $data = $filtered;
            }
            if (is_array($data)) {
                $data = serialize($data);
            }
            if ($data != $cfg_var['config_value'] || !$update_new_only) {
                $new_options[$cfg_name] = $data;
            }
            $optionslist[$cfg_name]['config_value'] = $data;
        }
    }
    return sizeof($new_options) ? cot_config_set($name, $new_options, $is_module) : 0;
}
Esempio n. 2
0
/**
 * Saves updated values of config list in DB
 *
 * @param string $name Extension or Section name config belongs to
 * @param array $optionslist Option list as return by cot_config_list()
 * @param mixed $is_module Flag indicating if it is module or plugin config
 * @param string $changed_only Update changes values only
 * @param string $source Source of imported data
 * @return boolean|number Number of updated values
 */
function cot_config_update_options($name, &$optionslist, $is_module = false, $changed_only = true, $source = 'POST')
{
    global $cot_import_filters;
    if (!is_array($optionslist)) {
        return false;
    }
    $new_options = array();
    foreach ($optionslist as $key => $val) {
        $filter = 'NOC';
        // Visual separator/fieldset have no value
        if ($val['config_type'] == COT_CONFIG_TYPE_SEPARATOR) {
            continue;
        }
        if ($val['config_type'] == COT_CONFIG_TYPE_CUSTOM && $val['config_variants']) {
            if (preg_match('#^(\\w+)\\((.*?)\\)$#', $val['config_variants'], $mt)) {
                $custom_func = $mt[1];
                $custom_filter_func = $custom_func . '_filter';
                // use custom filter function or built-in
                if (function_exists($custom_filter_func)) {
                    $callback_params = preg_split('#\\s*,\\s*#', $mt[2]);
                    if (count($callback_params) > 0 && !empty($callback_params[0])) {
                        for ($i = 0; $i < count($callback_params); $i++) {
                            $callback_params[$i] = str_replace(array("'", '"'), array('', ''), $callback_params[$i]);
                        }
                    }
                    $data = call_user_func_array($custom_filter_func, array_merge(array($key, $val['config_value']), $callback_params));
                } else {
                    // last part of custom function name treats as built-in filter name
                    $last_func_name = array_reverse(explode('_', $custom_func));
                    $custom_filter = strtoupper($last_func_name[0]);
                    if (in_array(strtoupper($custom_filter), array('INT', 'BOL', 'PSW', 'ALP', 'TXT', 'NUM')) || sizeof($cot_import_filters[$custom_filter])) {
                        $filter = $custom_filter;
                    }
                    $data = cot_config_import($key, $source, $filter, $val['config_default']);
                }
            }
        } else {
            $data = cot_config_import($key, $source, $filter, $val['config_default']);
        }
        if ($val['config_value'] != $data || !$changed_only) {
            $new_options[$key] = $data;
            $optionslist[$key]['config_value'] = $data;
        }
    }
    return sizeof($new_options) ? cot_config_set($name, $new_options, $is_module) : 0;
}