diff --git a/model/PFAHandler.php b/model/PFAHandler.php index ff094631..789dfb29 100644 --- a/model/PFAHandler.php +++ b/model/PFAHandler.php @@ -1,22 +1,70 @@ new) { foreach($this->struct as $key=>$row) { if ($row['editable'] && !isset($values[$key]) ) { - $func="_missing_".$key; # call $this->_missing_$fieldname() + # if a field is editable and not set, call $this->_missing_$fieldname() + # (if the method exists - otherwise the field won't be set, resulting in an error later) + $func="_missing_".$key; if (method_exists($this, $func) ) { $this->{$func}($key); # function can set $this->RAWvalues[$key] (or do nothing if it can't set a useful value) } @@ -117,14 +167,15 @@ class PFAHandler { foreach($this->struct as $key=>$row) { if ($row['editable'] == 0) { # not editable if ($this->new == 1) { + # on $new, always set non-editable field to default value on $new (even if input data contains another value) $this->values[$key] = $row['default']; } - } else { + } else { # field is editable if (isset($values[$key])) { if ($row['type'] != "pass" || strlen($values[$key]) > 0 || $this->new == 1) { # skip on empty (aka unchanged) password on edit $valid = true; # trust input unless validator objects - # validate based on field type (_inp_$type) + # validate based on field type ($this->_inp_$type) $func="_inp_".$row['type']; if (method_exists($this, $func) ) { if (!$this->{$func}($key, $values[$key])) $valid = false; @@ -225,7 +276,9 @@ class PFAHandler { /** * read_from_db * @param array or string - condition (an array will be AND'ed using db_where_clause, a string will be directly used) - * @return array - rows + * @param integer limit - maximum number of rows to return + * @param integer offset - number of first row to return + * @return array - rows (as associative array, with the ID as key) */ protected function read_from_db($condition, $limit=-1, $offset=-1) { $select_cols = array(); @@ -281,6 +334,8 @@ class PFAHandler { $query = "SELECT $cols FROM $table $extrafrom $where ORDER BY " . $this->id_field; if ($limit > -1 && $offset > -1) { + # TODO: make sure $limit and $offset are really integers - cast via (int) ? + # TODO: make sure $limit is > 0 (0 doesn't break anything, but guarantees an empty resultset, so it's pointless) $query .= " LIMIT $limit OFFSET $offset "; } @@ -324,6 +379,8 @@ class PFAHandler { /** * get a list of one or more items with all values * @param array or string $condition + * @param integer limit - maximum number of rows to return + * @param integer offset - number of first row to return * @return bool - true if at least one item was found * The data is stored in $this->return (as array of rows, each row is an associative array of column => value) */ @@ -388,31 +445,47 @@ class PFAHandler { /************************************************************************** + * _inp_*() * functions for basic input validation + * @return boolean - true if the value is valid, otherwise false + * also set $this->errormsg[$field] if a value is invalid */ + + /** + * check if value is numeric and >= -1 (= minimum value for quota) + */ function _inp_num($field, $val) { $valid = is_numeric($val); if ($val < -1) $valid = false; - if (!$valid) $this->errormsg[$field] = "$field must be numeric"; + if (!$valid) $this->errormsg[$field] = "$field must be numeric"; # TODO: make translateable return $valid; # return (int)($val); } + /** + * check if value is (numeric) boolean - in other words: 0 or 1 + */ function _inp_bool($field, $val) { if ($val == "0" || $val == "1") return true; - $this->errormsg[$field] = "$field must be boolean"; + $this->errormsg[$field] = "$field must be boolean"; # TODO: make translateable return false; # return $val ? db_get_boolean(true): db_get_boolean(false); } + /** + * check if value of an enum field is in the list of allowed values + */ function _inp_enum($field, $val) { if(in_array($val, $this->struct[$field]['options'])) return true; - $this->errormsg[$field] = "Invalid parameter given for $field"; + $this->errormsg[$field] = "Invalid parameter given for $field"; # TODO: make translateable return false; } + /** + * check if a password is secure enough + */ function _inp_pass($field, $val){ - $validpass = validate_password($val); + $validpass = validate_password($val); # returns array of error messages, or empty array on success if(count($validpass) == 0) return true;