source: temp/test-xoops.ec-cube.net/html/kernel/object.php @ 405

Revision 405, 19.7 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: object.php,v 1.6 2006/07/27 00:17:18 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31
32/**
33 * @package kernel
34 * @copyright copyright &copy; 2000 XOOPS.org
35 */
36
37/**#@+
38 * Xoops object datatype
39 *
40 **/
41define('XOBJ_DTYPE_TXTBOX', 1);
42define('XOBJ_DTYPE_TXTAREA', 2);
43define('XOBJ_DTYPE_INT', 3);
44define('XOBJ_DTYPE_URL', 4);
45define('XOBJ_DTYPE_EMAIL', 5);
46define('XOBJ_DTYPE_ARRAY', 6);
47define('XOBJ_DTYPE_OTHER', 7);
48define('XOBJ_DTYPE_SOURCE', 8);
49define('XOBJ_DTYPE_STIME', 9);
50define('XOBJ_DTYPE_MTIME', 10);
51define('XOBJ_DTYPE_LTIME', 11);
52/**#@-*/
53
54//include_once "xoopspluginloader.php";
55
56/**
57 * Base class for all objects in the Xoops kernel (and beyond)
58 *
59 * @author Kazumi Ono (AKA onokazu)
60 * @copyright copyright &copy; 2000 XOOPS.org
61 * @package kernel
62 **/
63class XoopsObject
64{
65
66    /**
67     * holds all variables(properties) of an object
68     *
69     * @var array
70     * @access protected
71     **/
72    var $vars = array();
73
74    /**
75    * variables cleaned for store in DB
76    *
77    * @var array
78    * @access protected
79    */
80    var $cleanVars = array();
81
82    /**
83    * is it a newly created object?
84    *
85    * @var bool
86    * @access private
87    */
88    var $_isNew = false;
89
90    /**
91    * has any of the values been modified?
92    *
93    * @var bool
94    * @access private
95    */
96    var $_isDirty = false;
97
98    /**
99    * errors
100    *
101    * @var array
102    * @access private
103    */
104    var $_errors = array();
105
106    /**
107    * additional filters registered dynamically by a child class object
108    *
109    * @access private
110    */
111    var $_filters = array();
112
113    /**
114    * constructor
115    *
116    * normally, this is called from child classes only
117    * @access public
118    */
119    function XoopsObject()
120    {
121    }
122
123    /**#@+
124    * used for new/clone objects
125    *
126    * @access public
127    */
128    function setNew()
129    {
130        $this->_isNew = true;
131    }
132    function unsetNew()
133    {
134        $this->_isNew = false;
135    }
136    function isNew()
137    {
138        return $this->_isNew;
139    }
140    /**#@-*/
141
142    /**#@+
143    * mark modified objects as dirty
144    *
145    * used for modified objects only
146    * @access public
147    */
148    function setDirty()
149    {
150        $this->_isDirty = true;
151    }
152    function unsetDirty()
153    {
154        $this->_isDirty = false;
155    }
156    function isDirty()
157    {
158        return $this->_isDirty;
159    }
160    /**#@-*/
161
162    /**
163    * initialize variables for the object
164    *
165    * @access public
166    * @param string $key
167    * @param int $data_type  set to one of XOBJ_DTYPE_XXX constants (set to XOBJ_DTYPE_OTHER if no data type ckecking nor text sanitizing is required)
168    * @param mixed
169    * @param bool $required  require html form input?
170    * @param int $maxlength  for XOBJ_DTYPE_TXTBOX type only
171    * @param string $option  does this data have any select options?
172    */
173    function initVar($key, $data_type, $value = null, $required = false, $maxlength = null, $options = '')
174    {
175        $this->vars[$key] = array('value' => $value, 'required' => $required, 'data_type' => $data_type, 'maxlength' => $maxlength, 'changed' => false, 'options' => $options);
176    }
177
178    /**
179    * assign a value to a variable
180    *
181    * @access public
182    * @param string $key name of the variable to assign
183    * @param mixed $value value to assign
184    */
185    function assignVar($key, $value)
186    {
187        if (isset($value) && isset($this->vars[$key])) {
188            $this->vars[$key]['value'] =& $value;
189        }
190    }
191
192    /**
193    * assign values to multiple variables in a batch
194    *
195    * @access private
196    * @param array $var_array associative array of values to assign
197    */
198    function assignVars($var_arr)
199    {
200        foreach ($var_arr as $key => $value) {
201            $this->assignVar($key, $value);
202        }
203    }
204
205    /**
206    * assign a value to a variable
207    *
208    * @access public
209    * @param string $key name of the variable to assign
210    * @param mixed $value value to assign
211    * @param bool $not_gpc
212    */
213    function setVar($key, $value, $not_gpc = false)
214    {
215        if (!empty($key) && isset($value) && isset($this->vars[$key])) {
216            $this->vars[$key]['value'] =& $value;
217            $this->vars[$key]['not_gpc'] = $not_gpc;
218            $this->vars[$key]['changed'] = true;
219            $this->setDirty();
220        }
221    }
222
223    /**
224    * assign values to multiple variables in a batch
225    *
226    * @access private
227    * @param array $var_arr associative array of values to assign
228    * @param bool $not_gpc
229    */
230    function setVars($var_arr, $not_gpc = false)
231    {
232        foreach ($var_arr as $key => $value) {
233            $this->setVar($key, $value, $not_gpc);
234        }
235    }
236
237    /**
238    * Assign values to multiple variables in a batch
239    *
240    * Meant for a CGI contenxt:
241    * - prefixed CGI args are considered save
242    * - avoids polluting of namespace with CGI args
243    *
244    * @access private
245    * @param array $var_arr associative array of values to assign
246    * @param string $pref prefix (only keys starting with the prefix will be set)
247    */
248    function setFormVars($var_arr=null, $pref='xo_', $not_gpc=false) {
249        $len = strlen($pref);
250        foreach ($var_arr as $key => $value) {
251            if ($pref == substr($key,0,$len)) {
252                $this->setVar(substr($key,$len), $value, $not_gpc);
253            }
254        }
255    }
256
257
258    /**
259    * returns all variables for the object
260    *
261    * @access public
262    * @return array associative array of key->value pairs
263    */
264    function &getVars()
265    {
266        return $this->vars;
267    }
268
269    /**
270    * returns a specific variable for the object in a proper format
271    *
272    * @access public
273    * @param string $key key of the object's variable to be returned
274    * @param string $format format to use for the output
275    * @return mixed formatted value of the variable
276    */
277    function &getVar($key, $format = 's')
278    {
279        $ret = $this->vars[$key]['value'];
280        switch ($this->vars[$key]['data_type']) {
281
282        case XOBJ_DTYPE_TXTBOX:
283            switch (strtolower($format)) {
284            case 's':
285            case 'show':
286            case 'e':
287            case 'edit':
288                $ts =& MyTextSanitizer::getInstance();
289                $ret = $ts->htmlSpecialChars($ret);
290                return $ret;
291                break 1;
292            case 'p':
293            case 'preview':
294            case 'f':
295            case 'formpreview':
296                $ts =& MyTextSanitizer::getInstance();
297                $ret = $ts->htmlSpecialChars($ts->stripSlashesGPC($ret));
298                return $ret;
299                break 1;
300            case 'n':
301            case 'none':
302            default:
303                break 1;
304            }
305            break;
306        case XOBJ_DTYPE_TXTAREA:
307            switch (strtolower($format)) {
308            case 's':
309            case 'show':
310                $ts =& MyTextSanitizer::getInstance();
311                $html = !empty($this->vars['dohtml']['value']) ? 1 : 0;
312                $xcode = (!isset($this->vars['doxcode']['value']) || $this->vars['doxcode']['value'] == 1) ? 1 : 0;
313                $smiley = (!isset($this->vars['dosmiley']['value']) || $this->vars['dosmiley']['value'] == 1) ? 1 : 0;
314                $image = (!isset($this->vars['doimage']['value']) || $this->vars['doimage']['value'] == 1) ? 1 : 0;
315                $br = (!isset($this->vars['dobr']['value']) || $this->vars['dobr']['value'] == 1) ? 1 : 0;
316                $ret = $ts->displayTarea($ret, $html, $smiley, $xcode, $image, $br);
317                return $ret;
318                break 1;
319            case 'e':
320            case 'edit':
321                $ret = htmlspecialchars($ret, ENT_QUOTES);
322                return $ret;
323                break 1;
324            case 'p':
325            case 'preview':
326                $ts =& MyTextSanitizer::getInstance();
327                $html = !empty($this->vars['dohtml']['value']) ? 1 : 0;
328                $xcode = (!isset($this->vars['doxcode']['value']) || $this->vars['doxcode']['value'] == 1) ? 1 : 0;
329                $smiley = (!isset($this->vars['dosmiley']['value']) || $this->vars['dosmiley']['value'] == 1) ? 1 : 0;
330                $image = (!isset($this->vars['doimage']['value']) || $this->vars['doimage']['value'] == 1) ? 1 : 0;
331                $br = (!isset($this->vars['dobr']['value']) || $this->vars['dobr']['value'] == 1) ? 1 : 0;
332                $ret = $ts->previewTarea($ret, $html, $smiley, $xcode, $image, $br);
333                return $ret;
334                break 1;
335            case 'f':
336            case 'formpreview':
337                $ts =& MyTextSanitizer::getInstance();
338                $ret = htmlspecialchars($ts->stripSlashesGPC($ret), ENT_QUOTES);
339                return $ret;
340                break 1;
341            case 'n':
342            case 'none':
343            default:
344                break 1;
345            }
346            break;
347        case XOBJ_DTYPE_ARRAY:
348            $ret = unserialize($ret);
349            break;
350        case XOBJ_DTYPE_SOURCE:
351            switch (strtolower($format)) {
352            case 's':
353            case 'show':
354                break 1;
355            case 'e':
356            case 'edit':
357                $ret = htmlspecialchars($ret, ENT_QUOTES);
358                return $ret;
359                break 1;
360            case 'p':
361            case 'preview':
362                $ts =& MyTextSanitizer::getInstance();
363                $ret = $ts->stripSlashesGPC($ret);
364                return $ret;
365                break 1;
366            case 'f':
367            case 'formpreview':
368                $ts =& MyTextSanitizer::getInstance();
369                $ret = htmlspecialchars($ts->stripSlashesGPC($ret), ENT_QUOTES);
370                return $ret;
371                break 1;
372            case 'n':
373            case 'none':
374            default:
375                break 1;
376            }
377            break;
378        default:
379            if ($this->vars[$key]['options'] != '' && $ret != '') {
380                switch (strtolower($format)) {
381                case 's':
382                case 'show':
383                    $selected = explode('|', $ret);
384                    $options = explode('|', $this->vars[$key]['options']);
385                    $i = 1;
386                    $ret = array();
387                    foreach ($options as $op) {
388                        if (in_array($i, $selected)) {
389                            $ret[] = $op;
390                        }
391                        $i++;
392                    }
393                    $ret = implode(', ', $ret);
394                    return $ret;
395                case 'e':
396                case 'edit':
397                    $ret = explode('|', $ret);
398                    break 1;
399                default:
400                    break 1;
401                }
402
403            }
404            break;
405        }
406        return $ret;
407    }
408
409    /**
410     * clean values of all variables of the object for storage.
411     * also add slashes whereever needed
412     *
413     * @return bool true if successful
414     * @access public
415     */
416    function cleanVars()
417    {
418        $ts =& MyTextSanitizer::getInstance();
419        foreach ($this->vars as $k => $v) {
420            $cleanv = $v['value'];
421            if (!$v['changed']) {
422            } else {
423                $cleanv = is_string($cleanv) ? trim($cleanv) : $cleanv;
424                switch ($v['data_type']) {
425                case XOBJ_DTYPE_TXTBOX:
426                    if ($v['required'] && $cleanv != '0' && $cleanv == '') {
427                        $this->setErrors("$k is required.");
428                        continue;
429                    }
430                    if (isset($v['maxlength']) && strlen($cleanv) > intval($v['maxlength'])) {
431                        $this->setErrors("$k must be shorter than ".intval($v['maxlength'])." characters.");
432                        continue;
433                    }
434                    if (!$v['not_gpc']) {
435                        $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv));
436                    } else {
437                        $cleanv = $ts->censorString($cleanv);
438                    }
439                    break;
440                case XOBJ_DTYPE_TXTAREA:
441                    if ($v['required'] && $cleanv != '0' && $cleanv == '') {
442                        $this->setErrors("$k is required.");
443                        continue;
444                    }
445                    if (!$v['not_gpc']) {
446                        $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv));
447                    } else {
448                        $cleanv = $ts->censorString($cleanv);
449                    }
450                    break;
451                case XOBJ_DTYPE_SOURCE:
452                    if (!$v['not_gpc']) {
453                        $cleanv = $ts->stripSlashesGPC($cleanv);
454                    } else {
455                        $cleanv = $cleanv;
456                    }
457                    break;
458                case XOBJ_DTYPE_INT:
459                    $cleanv = intval($cleanv);
460                    break;
461                case XOBJ_DTYPE_EMAIL:
462                    if ($v['required'] && $cleanv == '') {
463                        $this->setErrors("$k is required.");
464                        continue;
465                    }
466                    if ($cleanv != '' && !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i",$cleanv)) {
467                        $this->setErrors("Invalid Email");
468                        continue;
469                    }
470                    if (!$v['not_gpc']) {
471                        $cleanv = $ts->stripSlashesGPC($cleanv);
472                    }
473                    break;
474                case XOBJ_DTYPE_URL:
475                    if ($v['required'] && $cleanv == '') {
476                        $this->setErrors("$k is required.");
477                        continue;
478                    }
479                    if ($cleanv != '' && !preg_match("/^http[s]*:\/\//i", $cleanv)) {
480                        $cleanv = 'http://' . $cleanv;
481                    }
482                    if (!$v['not_gpc']) {
483                        $cleanv =& $ts->stripSlashesGPC($cleanv);
484                    }
485                    break;
486                case XOBJ_DTYPE_ARRAY:
487                    $cleanv = serialize($cleanv);
488                    break;
489                case XOBJ_DTYPE_STIME:
490                case XOBJ_DTYPE_MTIME:
491                case XOBJ_DTYPE_LTIME:
492                    $cleanv = !is_string($cleanv) ? intval($cleanv) : strtotime($cleanv);
493                    break;
494                default:
495                    break;
496                }
497            }
498            $this->cleanVars[$k] =& $cleanv;
499            unset($cleanv);
500        }
501        if (count($this->_errors) > 0) {
502            return false;
503        }
504        $this->unsetDirty();
505        return true;
506    }
507
508    /**
509     * dynamically register additional filter for the object
510     *
511     * @param string $filtername name of the filter
512     * @access public
513     */
514    function registerFilter($filtername)
515    {
516        $this->_filters[] = $filtername;
517    }
518
519    /**
520     * load all additional filters that have been registered to the object
521     *
522     * @access private
523     */
524    function _loadFilters()
525    {
526        //include_once XOOPS_ROOT_PATH.'/class/filters/filter.php';
527        //foreach ($this->_filters as $f) {
528        //    include_once XOOPS_ROOT_PATH.'/class/filters/'.strtolower($f).'php';
529        //}
530    }
531
532    /**
533     * create a clone(copy) of the current object
534     *
535     * @access public
536     * @return object clone
537     */
538    function &xoopsClone()
539    {
540        $class = get_class($this);
541        $clone =& new $class();
542        foreach ($this->vars as $k => $v) {
543            $clone->assignVar($k, $v['value']);
544        }
545        // need this to notify the handler class that this is a newly created object
546        $clone->setNew();
547        return $clone;
548    }
549
550    /**
551     * add an error
552     *
553     * @param string $value error to add
554     * @access public
555     */
556    function setErrors($err_str)
557    {
558        $this->_errors[] = trim($err_str);
559    }
560
561    /**
562     * return the errors for this object as an array
563     *
564     * @return array an array of errors
565     * @access public
566     */
567    function getErrors()
568    {
569        return $this->_errors;
570    }
571
572    /**
573     * return the errors for this object as html
574     *
575     * @return string html listing the errors
576     * @access public
577     */
578    function getHtmlErrors()
579    {
580        $ret = '<h4>Errors</h4>';
581        if (!empty($this->_errors)) {
582            foreach ($this->_errors as $error) {
583                $ret .= $error.'<br />';
584            }
585        } else {
586            $ret .= 'None<br />';
587        }
588        return $ret;
589    }
590}
591
592/**
593* XOOPS object handler class.
594* This class is an abstract class of handler classes that are responsible for providing
595* data access mechanisms to the data source of its corresponsing data objects
596* @package kernel
597* @abstract
598*
599* @author  Kazumi Ono <[email protected]>
600* @copyright copyright &copy; 2000 The XOOPS Project
601*/
602class XoopsObjectHandler
603{
604
605    /**
606     * holds referenced to {@link XoopsDatabase} class object
607     *
608     * @var object
609     * @see XoopsDatabase
610     * @access protected
611     */
612    var $db;
613
614    //
615    /**
616     * called from child classes only
617     *
618     * @param object $db reference to the {@link XoopsDatabase} object
619     * @access protected
620     */
621    function XoopsObjectHandler(&$db)
622    {
623        $this->db =& $db;
624    }
625
626    /**
627     * creates a new object
628     *
629     * @abstract
630     */
631    function &create()
632    {
633    }
634
635    /**
636     * gets a value object
637     *
638     * @param int $int_id
639     * @abstract
640     */
641    function &get($int_id)
642    {
643    }
644
645    /**
646     * insert/update object
647     *
648     * @param object $object
649     * @abstract
650     */
651    function insert(&$object)
652    {
653    }
654
655    /**
656     * delete obejct from database
657     *
658     * @param object $object
659     * @abstract
660     */
661    function delete(&$object)
662    {
663    }
664
665}
666?>
Note: See TracBrowser for help on using the repository browser.