示例#1
0
function process_widget($name, $params)
{
    $value = null;
    switch ($params['type']) {
        case 'phone':
            if (array_get($params, 'allow_empty', TRUE) && empty($_REQUEST[$name])) {
                $value = '';
            } else {
                if (!is_valid_phone_number($_REQUEST[$name], $params['formats'])) {
                    trigger_error('The phone number "' . $_REQUEST[$name] . '" is not valid and has not been set', E_USER_NOTICE);
                    $value = NULL;
                } else {
                    $value = clean_phone_number($_REQUEST[$name]);
                }
            }
            break;
        case 'date':
            if (isset($_REQUEST[$name])) {
                // might have an ISO8601 date
                if (preg_match('/^(\\d\\d\\d\\d-\\d\\d-\\d\\d)$/', $_REQUEST[$name])) {
                    return $_REQUEST[$name];
                }
            }
            if (FALSE === strpos($name, '[')) {
                $subindex = NULL;
            } else {
                $subindex = substr($name, strpos($name, '[') + 1, strpos($name, ']') - strpos($name, '[') - 1);
                $name = substr($name, 0, strpos($name, '['));
            }
            if (!isset($_REQUEST[$name . '_d'])) {
                return NULL;
            }
            if (!is_null($subindex) && !isset($_REQUEST[$name . '_d'][$subindex])) {
                return NULL;
            }
            foreach (array('y', 'm', 'd') as $comp) {
                $comp_vals[$comp] = array_get($_REQUEST, $name . '_' . $comp, 0);
                if (!is_null($subindex)) {
                    $comp_vals[$comp] = $comp_vals[$comp][$subindex];
                }
            }
            $value = sprintf('%04d-%02d-%02d', $comp_vals['y'], $comp_vals['m'], $comp_vals['d']);
            if ($value == '0000-00-00') {
                return NULL;
            }
            if ($value == '0000-01-00') {
                return NULL;
            }
            if (array_get($params, 'allow_blank_year') && !(int) $comp_vals['y']) {
                $value = substr($value, 4);
                if (date('-m-d', strtotime('2000' . $value)) != $value) {
                    trigger_error('The date "' . $value . '" is not valid and has not been set', E_USER_NOTICE);
                    $value = NULL;
                }
            } else {
                if (date('Y-m-d', strtotime($value)) != $value) {
                    trigger_error('The date "' . $value . '" is not valid and has not been set', E_USER_NOTICE);
                    $value = NULL;
                }
            }
            break;
        case 'bibleref':
            if (!empty($_REQUEST[$name])) {
                require_once 'bible_ref.class.php';
                $br = new bible_ref($_REQUEST[$name]);
                if ($br->book) {
                    $value = $br->toCode();
                }
            }
            break;
        case 'bitmask':
            // value is the bitwise-or of all submitted values
            $value = 0;
            if (isset($_REQUEST[$name])) {
                if (isset($_REQUEST[$name])) {
                    foreach ($_REQUEST[$name] as $i) {
                        $value = $value | (int) $i;
                    }
                }
            }
            break;
        case 'html':
            if (isset($_REQUEST[$name])) {
                require_once 'htmLawed.php';
                $value = htmLawed($_REQUEST[$name], array('deny_attribute' => '* -href', 'safe' => 1));
            }
            break;
        default:
            $value = array_get($_REQUEST, $name);
            if (!empty($params['regex']) && !empty($value) && !preg_match($params['regex'] . 'i', $value)) {
                trigger_error($value . ' is not a valid value for ' . array_get($params, 'label', ucfirst($name)));
                $value = NULL;
            }
            break;
    }
    return $value;
}
示例#2
0
 public function setValue($name, $value)
 {
     if (!isset($this->fields[$name])) {
         trigger_error('Cannot set value for field ' . ents($name) . ' - field does not exist', E_USER_WARNING);
         return FALSE;
     }
     if (array_get($this->fields[$name], 'readonly')) {
         trigger_error('Cannot set value for readonly field "' . $name . '"', E_USER_WARNING);
         return;
     }
     if (array_get($this->fields[$name], 'initial_cap')) {
         $value = ucfirst($value);
     }
     if (array_get($this->fields[$name], 'trim')) {
         $value = trim($value, ",;. \t\n\r\v");
     }
     if ($this->fields[$name]['type'] == 'select') {
         if (!isset($this->fields[$name]['options'][$value]) && !(array_get($this->fields[$name], 'allow_empty', 1) && empty($value))) {
             trigger_error(ents($value) . ' is not a valid value for field "' . $name . '", and has not been set', E_USER_NOTICE);
             return;
         }
     }
     if ($this->fields[$name]['type'] == 'phone' && $value != '') {
         if (!is_valid_phone_number($value, $this->fields[$name]['formats'])) {
             trigger_error(ents($value) . ' is not a valid phone number for field "' . $name . '", and has not been set', E_USER_NOTICE);
             return;
         }
         $value = clean_phone_number($value);
     }
     if (!empty($this->fields[$name]['maxlength']) && strlen($value) > $this->fields[$name]['maxlength']) {
         $value = substr($value, 0, $this->fields[$name]['maxlength']);
     }
     if ($this->fields[$name]['type'] == 'int') {
         if (!array_get($this->fields[$name], 'allow_empty', true) || $value !== '') {
             $strval = (string) $value;
             for ($i = 0; $i < strlen($strval); $i++) {
                 $char = $strval[$i];
                 if ((int) $char != $char) {
                     trigger_error(ents($value) . ' is not a valid value for integer field "' . $name . '" and has not been set', E_USER_NOTICE);
                     return;
                 }
             }
         }
     }
     if (array_key_exists($name, $this->values) && $this->values[$name] != $value && !isset($this->_old_values[$name])) {
         $this->_old_values[$name] = $this->values[$name];
     }
     $this->values[$name] = $value;
 }