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 * Insert ID from the insert() command
165 * Constructor: cannot instantiate class directly
167 function API(&$registry)
169 if (!is_subclass_of($this, 'API'))
171 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR
);
174 if (!is_object($registry))
176 trigger_error('The passed registry is not an object', E_USER_ERROR
);
179 $this->registry
=& $registry;
183 * Sets a value, sanitizes it, and verifies it
185 * @param string Field name
187 * @param bool Do clean?
188 * @param bool Do verify?
190 function set($field, $value, $doclean = true
, $doverify = true
)
192 if (!isset($this->fields
["$field"]))
194 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING
);
198 $this->values
["$field"] = ($doclean ?
$this->do_clean($value, $this->fields
["$field"][0]) : $value);
200 $this->setfields
[] = $field;
202 if (isset($this->fields
["$field"][2]) AND $doverify)
204 if ($this->fields
["$field"][2] == ':self')
206 $verify = $this->{$this->{"verify_$field"}}($field);
210 $verify = $this->{$this->fields
["$field"][2]}($field);
216 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR
);
222 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
223 * from the REQ_AUTO field
225 * @param string WHERE conditional bit
227 function set_condition($condition = '')
229 if ($condition != '')
231 $this->condition
= $condition;
235 foreach ($this->fields
AS $name => $options)
237 if ($options[1] == REQ_AUTO
)
239 if (!$this->values
["$name"])
241 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING
);
245 $this->condition
= "$name = " . (($options[0] == TYPE_NOCLEAN
OR $options[0] == TYPE_STR
) ?
"'" . $this->values
["$name"] . "'" : $this->values
["$name"]);
249 if ($this->condition
== '')
251 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING
);
257 * Sets existing data into $values where it's not already present
259 function set_existing()
269 foreach ($this->objdata
AS $field => $value)
271 if (!isset($this->values
["$field"]))
273 $this->values
["$field"] = $value;
281 * Fetches a record based on the condition
285 if ($this->condition
== '')
287 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
290 $actmethod = (method_exists($this, 'pre_fetch') ?
$this->pre_fetch() : '');
292 $result = $this->registry
->modules
['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
296 trigger_error('No record was returned', E_USER_ERROR
);
300 $actmethod = (method_exists($this, 'post_fetch') ?
$this->post_fetch() : '');
302 $this->objdata
= $result;
306 * Inserts a record in the database
312 $actmethod = (method_exists($this, 'pre_insert') ?
$this->pre_insert() : '');
314 foreach ($this->setfields
AS $field)
317 $values[] (($this->fields
["$field"][0] == TYPE_NOCLEAN
OR $this->fields
["$field"][0] == TYPE_STR
) ?
"'" . $this->values
["$field"] . "'" : $this->values
["$field"]);
320 $this->registry
->modules
['db_mysql']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
321 $this->insertid
= $this->registry
->modules
['db_mysql']->insert_id();
323 $actmethod = (method_exists($this, 'post_insert') ?
$this->post_insert() : '');
327 * Updates a record in the database using the data in $vaues
331 if ($this->condition
== '')
333 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
336 $this->set_existing();
339 $actmethod = (method_exists($this, 'pre_update') ?
$this->pre_update() : '');
341 foreach ($this->setfields
AS $field)
343 $updates[] = "$field = " . (($this->fields
["$field"][0] == TYPE_NOCLEAN
OR $this->fields
["$field"][0] == TYPE_STR
) ?
"'" . $this->values
["$field"] . "'" : $this->values
["$field"]);
345 $updates = implode(', ', $updates);
347 $this->registry
->modules
['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
349 $actmethod = (method_exists($this, 'post_update') ?
$this->post_update() : '');
357 if ($this->condition
== '')
359 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
362 $this->set_existing();
364 $actmethod = (method_exists($this, 'pre_delete') ?
$this->pre_delete() : '');
366 $this->registry
->modules
['db_mysql']->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
368 $actmethod = (method_exists($this, 'post_delete') ?
$this->post_delete() : '');
372 * Verifies that all required fields are set
376 foreach ($this->fields
AS $name => $options)
378 if ($options[1] == REQ_YES
)
380 if (!isset($this->values
["$name"]))
383 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR
);
390 * Verify field: not a zero value
392 function verify_nozero($field)
394 if ($this->values
["$field"] == 0)
403 * Cleans a value based on the field option
405 * @param mixed Uncleaned data
406 * @param integer Cleaning type
408 * @return mixed Cleaned data
410 function do_clean($value, $type)
412 if ($type == TYPE_INT
)
414 $value = intval($value);
416 else if ($type == TYPE_UINT
)
418 $value = abs(intval($value));
420 else if ($type == TYPE_FLOAT
)
422 $value = floatval($value);
424 else if ($type == TYPE_BOOL
)
426 $value = (bool
)$value;
428 else if ($type == TYPE_STR
)
430 if (!$this->registry
->escapestrings
)
432 $value = $this->registry
->escape($value);
435 else if ($type == TYPE_STRUN
)
437 $value = $this->registry
->unsanitize($value);
439 else if ($type == TYPE_NOCLEAN
)
444 trigger_error('Invalid clean type specified', E_USER_ERROR
);
451 /*=====================================================================*\
452 || ###################################################################
455 || ###################################################################
456 \*=====================================================================*/