2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Abstract Datamanger API
29 if (!defined('TYPE_INT'))
34 define('TYPE_INT', 1);
39 define('TYPE_UINT', 2);
44 define('TYPE_FLOAT', 4);
49 define('TYPE_BOOL', 8);
54 define('TYPE_STR', 16);
57 * String - deliberate unclean
59 define('TYPE_STRUN', 32);
64 define('TYPE_NOCLEAN', 64);
77 * Auto-increasing value
79 define('REQ_AUTO', -1);
82 * Returns an unsigned integer
84 * @param mixed Original data
86 * @return integer Unsigned integegral value
88 function uintval($data)
90 return abs(intval($data));
94 * Returns the same value
96 * @param mixed Original
98 * @return mixed Same original value
100 function sameval($data)
109 * Abstract class that is used as an API base for most common database interaction
110 * schemes. Creates a simple structure that holds data and can update, delete, and
113 * @author Iris Studios, Inc.
114 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
115 * @version $Revision$
125 var $registry = null
;
128 * Fields: used for verification and sanitization
129 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
132 var $fields = array();
135 * Values array: sanitized and verified field values
138 var $values = array();
141 * Fields that were manually set with set(), not by using set_existing()
144 var $setfields = array();
153 * The object table row; a fetched row that represents this instance
156 var $objdata = array();
159 * Constructor: cannot instantiate class directly
161 function API(&$registry)
163 if (!is_subclass_of($this, 'API'))
165 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR
);
168 if (!is_object($registry))
170 trigger_error('The passed registry is not an object', E_USER_ERROR
);
173 $this->registry
=& $registry;
177 * Sets a value, sanitizes it, and verifies it
179 * @param string Field name
181 * @param bool Do clean?
182 * @param bool Do verify?
184 function set($field, $value, $doclean = true
, $doverify = true
)
186 if (!isset($this->fields
["$field"]))
188 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING
);
192 $this->values
["$field"] = ($doclean ?
$this->do_clean($value, $this->fields
["$field"][0]) : $value);
194 $this->setfields
[] = $field;
196 if (isset($this->fields
["$field"][2]) AND $doverify)
198 if ($this->fields
["$field"][2] == ':self')
200 $verify = $this->{$this->{"verify_$field"}}($field);
204 $verify = $this->{$this->fields
["$field"][2]}($field);
210 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR
);
216 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
217 * from the REQ_AUTO field
219 * @param string WHERE conditional bit
221 function set_condition($condition = '')
223 if ($condition != '')
225 $this->condition
= $condition;
229 foreach ($this->fields
AS $name => $options)
231 if ($options[1] == REQ_AUTO
)
233 if (!$this->values
["$name"])
235 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING
);
239 $this->condition
= "$name = " . (($options[0] == TYPE_NOCLEAN
OR $options[0] == TYPE_STR
) ?
"'" . $this->values
["$name"] . "'" : $this->values
["$name"]);
243 if ($this->condition
== '')
245 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING
);
251 * Sets existing data into $values where it's not already present
253 function set_existing()
263 foreach ($this->objdata
AS $field => $value)
265 if (!isset($this->values
["$field"]))
267 $this->values
["$field"] = $value;
275 * Fetches a record based on the condition
279 if ($this->condition
== '')
281 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
284 $actmethod = (function_exists(array($this, 'pre_fetch')) ?
$this->pre_fetch() : '');
286 $result = $this->registry
->modules
['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
290 trigger_error('No record was returned', E_USER_ERROR
);
294 $actmethod = (function_exists(array($this, 'post_fetch')) ?
$this->post_fetch() : '');
296 $this->objdata
= $result;
300 * Updates a record in the database using the data in $vaues
304 if ($this->condition
== '')
306 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
309 $this->set_existing();
312 $actmethod = (function_exists(array($this, 'pre_update')) ?
$this->pre_update() : '');
314 foreach ($this->setfields
AS $field)
316 $updates[] = "$field = " . (($options[0] == TYPE_NOCLEAN
OR $options[0] == TYPE_STR
) ?
"'" . $this->values
["$field"] . "'" : $this->values
["$field"]);
318 $updates = implode(', ', $updates);
320 $this->registry
->modules
['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
322 $actmethod = (function_exists(array($this, 'post_update')) ?
$this->post_update() : '');
326 * Verifies that all required fields are set
330 foreach ($this->fields
AS $name => $options)
332 if ($options[1] == REQ_YES
)
334 if (!in_array($name, $this->setfields
))
337 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR
);
344 * Verify field: not a zero value
346 function verify_nozero($field)
348 if ($this->values
["$field"] == 0)
357 * Cleans a value based on the field option
359 * @param mixed Uncleaned data
360 * @param integer Cleaning type
362 * @return mixed Cleaned data
364 function do_clean($value, $type)
366 if ($type == TYPE_INT
)
368 $value = intval($value);
370 else if ($type == TYPE_UINT
)
372 $value = abs(intval($value));
374 else if ($type == TYPE_FLOAT
)
376 $value = floatval($value);
378 else if ($type == TYPE_BOOL
)
380 $value = (bool
)$value;
382 else if ($type == TYPE_STR
)
384 if (!$this->registry
->escapestrings
)
386 $value = $this->registry
->escape($value);
389 else if ($type == TYPE_STRUN
)
391 $value = $this->registry
->unsanitize($value);
393 else if ($type == TYPE_NOCLEAN
)
398 trigger_error('Invalid clean type specified', E_USER_ERROR
);
405 /*=====================================================================*\
406 || ###################################################################
409 || ###################################################################
410 \*=====================================================================*/