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);
85 * Abstract class that is used as an API base for most common database interaction
86 * schemes. Creates a simple structure that holds data and can update, delete, and
89 * @author Iris Studios, Inc.
90 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
101 var $registry = null
;
104 * Fields: used for verification and sanitization
105 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
108 var $fields = array();
111 * Values array: sanitized and verified field values
114 var $values = array();
117 * Fields that were manually set with set(), not by using set_existing()
120 var $setfields = array();
129 * The object table row; a fetched row that represents this instance
132 var $objdata = array();
135 * Insert ID from the insert() command
141 * Constructor: cannot instantiate class directly
143 function API(&$registry)
145 if (!is_subclass_of($this, 'API'))
147 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR
);
150 if (!is_object($registry))
152 trigger_error('The passed registry is not an object', E_USER_ERROR
);
155 $this->registry
=& $registry;
159 * Sets a value, sanitizes it, and verifies it
161 * @param string Field name
163 * @param bool Do clean?
164 * @param bool Do verify?
166 function set($field, $value, $doclean = true
, $doverify = true
)
168 if (!isset($this->fields
["$field"]))
170 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING
);
174 $this->values
["$field"] = ($doclean ?
$this->do_clean($value, $this->fields
["$field"][0]) : $value);
176 $this->setfields
[] = $field;
178 if (isset($this->fields
["$field"][2]) AND $doverify)
180 if ($this->fields
["$field"][2] == ':self')
182 $verify = $this->{"verify_$field"}($field);
186 $verify = $this->{$this->fields
["$field"][2]}($field);
192 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR
);
198 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
199 * from the REQ_AUTO field
201 * @param string WHERE conditional bit
203 function set_condition($condition = '')
205 if ($condition != '')
207 $this->condition
= $condition;
211 foreach ($this->fields
AS $name => $options)
213 if ($options[1] == REQ_AUTO
)
215 if (!$this->values
["$name"])
217 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING
);
221 $this->condition
= "$name = " . (($options[0] == TYPE_NOCLEAN
OR $options[0] == TYPE_STR
) ?
"'" . $this->values
["$name"] . "'" : $this->values
["$name"]);
225 if ($this->condition
== '')
227 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING
);
233 * Sets existing data into $values where it's not already present
235 function set_existing()
245 foreach ($this->objdata
AS $field => $value)
247 if (!isset($this->values
["$field"]))
249 $this->values
["$field"] = $value;
257 * Fetches a record based on the condition
261 if ($this->condition
== '')
263 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
266 $actmethod = (method_exists($this, 'pre_fetch') ?
$this->pre_fetch() : '');
268 $result = $this->registry
->modules
['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
272 trigger_error('No record was returned', E_USER_ERROR
);
276 $actmethod = (method_exists($this, 'post_fetch') ?
$this->post_fetch() : '');
278 $this->objdata
= $result;
282 * Inserts a record in the database
288 $actmethod = (method_exists($this, 'pre_insert') ?
$this->pre_insert() : '');
290 foreach ($this->setfields
AS $field)
293 $values[] = (($this->fields
["$field"][0] == TYPE_NOCLEAN
OR $this->fields
["$field"][0] == TYPE_STR
) ?
"'" . $this->values
["$field"] . "'" : $this->values
["$field"]);
296 $this->registry
->modules
['db_mysql']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
297 $this->insertid
= $this->registry
->modules
['db_mysql']->insert_id();
299 $actmethod = (method_exists($this, 'post_insert') ?
$this->post_insert() : '');
303 * Updates a record in the database using the data in $vaues
307 if ($this->condition
== '')
309 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
312 $this->set_existing();
315 $actmethod = (method_exists($this, 'pre_update') ?
$this->pre_update() : '');
317 foreach ($this->setfields
AS $field)
319 $updates[] = "$field = " . (($this->fields
["$field"][0] == TYPE_NOCLEAN
OR $this->fields
["$field"][0] == TYPE_STR
) ?
"'" . $this->values
["$field"] . "'" : $this->values
["$field"]);
321 $updates = implode(', ', $updates);
323 $this->registry
->modules
['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
325 $actmethod = (method_exists($this, 'post_update') ?
$this->post_update() : '');
333 if ($this->condition
== '')
335 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
338 $this->set_existing();
340 $actmethod = (method_exists($this, 'pre_delete') ?
$this->pre_delete() : '');
342 $this->registry
->modules
['db_mysql']->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
344 $actmethod = (method_exists($this, 'post_delete') ?
$this->post_delete() : '');
348 * Verifies that all required fields are set
352 foreach ($this->fields
AS $name => $options)
354 if ($options[1] == REQ_YES
)
356 if (!isset($this->values
["$name"]))
359 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR
);
366 * Verify field: not a zero value
368 function verify_nozero($field)
370 if ($this->values
["$field"] == 0)
379 * Verify field: not empty
381 function verify_noempty($field)
383 if (empty($this->values
["$field"]))
392 * Cleans a value based on the field option
394 * @param mixed Uncleaned data
395 * @param integer Cleaning type
397 * @return mixed Cleaned data
399 function do_clean($value, $type)
401 if ($type == TYPE_INT
)
403 $value = intval($value);
405 else if ($type == TYPE_UINT
)
407 $value = abs(intval($value));
409 else if ($type == TYPE_FLOAT
)
411 $value = floatval($value);
413 else if ($type == TYPE_BOOL
)
415 $value = (bool
)$value;
417 else if ($type == TYPE_STR
)
419 if (!$this->registry
->escapestrings
)
421 $value = $this->registry
->escape($value);
424 else if ($type == TYPE_STRUN
)
426 $value = $this->registry
->unsanitize($value);
428 else if ($type == TYPE_NOCLEAN
)
433 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR
);
440 /*=====================================================================*\
441 || ###################################################################
444 || ###################################################################
445 \*=====================================================================*/