Base api.php checked in
[isso.git] / api.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Abstract Datamanger API
24 * api.php
25 *
26 * @package ISSO
27 */
28
29 if (!defined('TYPE_INT'))
30 {
31 /**
32 * Integer type
33 */
34 define('TYPE_INT', 1);
35
36 /**
37 * Unsigned integer
38 */
39 define('TYPE_UINT', 2);
40
41 /**
42 * Float type
43 */
44 define('TYPE_FLOAT', 4);
45
46 /**
47 * Boolean type
48 */
49 define('TYPE_BOOL', 8);
50
51 /**
52 * String - cleaned
53 */
54 define('TYPE_STR', 16);
55
56 /**
57 * String - deliberate unclean
58 */
59 define('TYPE_STRUN', 32);
60
61 /**
62 * No cleaning
63 */
64 define('TYPE_NOCLEAN', 64);
65
66 /**
67 * Yes, required
68 */
69 define('REQ_YES', 1);
70
71 /**
72 * No, not required
73 */
74 define('REQ_NO', 0);
75
76 /**
77 * Auto-increasing value
78 */
79 define('REQ_AUTO', -1);
80
81 /**
82 * Returns an unsigned integer
83 *
84 * @param mixed Original data
85 *
86 * @return integer Unsigned integegral value
87 */
88 function uintval($data)
89 {
90 return abs(intval($data));
91 }
92
93 /**
94 * Returns the same value
95 *
96 * @param mixed Original
97 *
98 * @return mixed Same original value
99 */
100 function sameval($data)
101 {
102 return $data;
103 }
104 }
105
106 /**
107 * Abstract API
108 *
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
111 * insert.
112 *
113 * @author Iris Studios, Inc.
114 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
115 * @version $Revision$
116 * @package ISSO
117 *
118 */
119 class API
120 {
121 /**
122 * Registry object
123 * @var object
124 */
125 var $registry = null;
126
127 /**
128 * Fields: used for verification and sanitization
129 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
130 * @var array
131 */
132 var $fields = array();
133
134 /**
135 * Values array: sanitized and verified field values
136 * @var array
137 */
138 var $values = array();
139
140 /**
141 * Fields that were manually set with set(), not by using set_existing()
142 * @var array
143 */
144 var $setfields = array();
145
146 /**
147 * WHERE condition
148 * @var string
149 */
150 var $condition = '';
151
152 /**
153 * The object table row; a fetched row that represents this instance
154 * @var array
155 */
156 var $objdata = array();
157
158 /**
159 * Constructor: cannot instantiate class directly
160 */
161 function API(&$registry)
162 {
163 if (!is_subclass_of($this, 'API'))
164 {
165 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR);
166 }
167
168 if (!is_object($registry))
169 {
170 trigger_error('The passed registry is not an object', E_USER_ERROR);
171 }
172
173 $this->registry =& $registry;
174 }
175
176 /**
177 * Sets a value, sanitizes it, and verifies it
178 *
179 * @param string Field name
180 * @param mixed Value
181 * @param bool Do clean?
182 * @param bool Do verify?
183 */
184 function set($field, $value, $doclean = true, $doverify = true)
185 {
186 if (!isset($this->fields["$field"]))
187 {
188 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING);
189 return;
190 }
191
192 $this->values["$field"] = ($doclean ? $this->do_clean($value, $this->fields["$field"][0]) : $value);
193
194 $this->setfields[] = $field;
195
196 if (isset($this->fields["$field"][2]) AND $doverify)
197 {
198 if ($this->fields["$field"][2] == ':self')
199 {
200 $verify = $this->{$this->{"verify_$field"}}($field);
201 }
202 else
203 {
204 $verify = $this->{$this->fields["$field"][2]}($field);
205 }
206
207 if (!$verify)
208 {
209 // <ERR:CVT>
210 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR);
211 }
212 }
213 }
214
215 /**
216 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
217 * from the REQ_AUTO field
218 *
219 * @param string WHERE conditional bit
220 */
221 function set_condition($condition = '')
222 {
223 if ($condition != '')
224 {
225 $this->condition = $condition;
226 }
227 else
228 {
229 foreach ($this->fields AS $name => $options)
230 {
231 if ($options[1] == REQ_AUTO)
232 {
233 if (!$this->values["$name"])
234 {
235 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING);
236 continue;
237 }
238
239 $this->condition = "$name = " . (($options[0] == TYPE_NOCLEAN OR $options[0] == TYPE_STR) ? "'" . $this->values["$name"] . "'" : $this->values["$name"]);
240 }
241 }
242
243 if ($this->condition == '')
244 {
245 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING);
246 }
247 }
248 }
249
250 /**
251 * Sets existing data into $values where it's not already present
252 */
253 function set_existing()
254 {
255 static $run;
256 if ($run)
257 {
258 return;
259 }
260
261 $this->fetch();
262
263 foreach ($this->objdata AS $field => $value)
264 {
265 if (!isset($this->values["$field"]))
266 {
267 $this->values["$field"] = $value;
268 }
269 }
270
271 $run = true;
272 }
273
274 /**
275 * Fetches a record based on the condition
276 */
277 function fetch()
278 {
279 if ($this->condition == '')
280 {
281 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
282 }
283
284 $actmethod = (function_exists(array($this, 'pre_fetch')) ? $this->pre_fetch() : '');
285
286 $result = $this->registry->modules['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
287 if (!$result)
288 {
289 // <ERR:CVT>
290 trigger_error('No record was returned', E_USER_ERROR);
291 return;
292 }
293
294 $actmethod = (function_exists(array($this, 'post_fetch')) ? $this->post_fetch() : '');
295
296 $this->objdata = $result;
297 }
298
299 /**
300 * Updates a record in the database using the data in $vaues
301 */
302 function update()
303 {
304 if ($this->condition == '')
305 {
306 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
307 }
308
309 $this->set_existing();
310 $this->verify();
311
312 $actmethod = (function_exists(array($this, 'pre_update')) ? $this->pre_update() : '');
313
314 foreach ($this->setfields AS $field)
315 {
316 $updates[] = "$field = " . (($options[0] == TYPE_NOCLEAN OR $options[0] == TYPE_STR) ? "'" . $this->values["$field"] . "'" : $this->values["$field"]);
317 }
318 $updates = implode(', ', $updates);
319
320 $this->registry->modules['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
321
322 $actmethod = (function_exists(array($this, 'post_update')) ? $this->post_update() : '');
323 }
324
325 /**
326 * Verifies that all required fields are set
327 */
328 function verify()
329 {
330 foreach ($this->fields AS $name => $options)
331 {
332 if ($options[1] == REQ_YES)
333 {
334 if (!in_array($name, $this->setfields))
335 {
336 // <ERR:CVT>
337 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR);
338 }
339 }
340 }
341 }
342
343 /**
344 * Verify field: not a zero value
345 */
346 function verify_nozero($field)
347 {
348 if ($this->values["$field"] == 0)
349 {
350 return false;
351 }
352
353 return true;
354 }
355
356 /**
357 * Cleans a value based on the field option
358 *
359 * @param mixed Uncleaned data
360 * @param integer Cleaning type
361 *
362 * @return mixed Cleaned data
363 */
364 function do_clean($value, $type)
365 {
366 if ($type == TYPE_INT)
367 {
368 $value = intval($value);
369 }
370 else if ($type == TYPE_UINT)
371 {
372 $value = abs(intval($value));
373 }
374 else if ($type == TYPE_FLOAT)
375 {
376 $value = floatval($value);
377 }
378 else if ($type == TYPE_BOOL)
379 {
380 $value = (bool)$value;
381 }
382 else if ($type == TYPE_STR)
383 {
384 if (!$this->registry->escapestrings)
385 {
386 $value = $this->registry->escape($value);
387 }
388 }
389 else if ($type == TYPE_STRUN)
390 {
391 $value = $this->registry->unsanitize($value);
392 }
393 else if ($type == TYPE_NOCLEAN)
394 {
395 }
396 else
397 {
398 trigger_error('Invalid clean type specified', E_USER_ERROR);
399 }
400
401 return $value;
402 }
403 }
404
405 /*=====================================================================*\
406 || ###################################################################
407 || # $HeadURL$
408 || # $Id$
409 || ###################################################################
410 \*=====================================================================*/
411 ?>