home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / kernel / object.php < prev    next >
Encoding:
PHP Script  |  2008-02-03  |  21.2 KB  |  685 lines

  1. <?php
  2. // $Id: object.php 1286 2008-02-03 04:07:19Z phppp $
  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 © 2000 XOOPS.org
  35.  */
  36.  
  37. /**#@+
  38.  * Xoops object datatype
  39.  *
  40.  **/
  41. define('XOBJ_DTYPE_TXTBOX', 1);
  42. define('XOBJ_DTYPE_TXTAREA', 2);
  43. define('XOBJ_DTYPE_INT', 3);
  44. define('XOBJ_DTYPE_URL', 4);
  45. define('XOBJ_DTYPE_EMAIL', 5);
  46. define('XOBJ_DTYPE_ARRAY', 6);
  47. define('XOBJ_DTYPE_OTHER', 7);
  48. define('XOBJ_DTYPE_SOURCE', 8);
  49. define('XOBJ_DTYPE_STIME', 9);
  50. define('XOBJ_DTYPE_MTIME', 10);
  51. define('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 © 2000 XOOPS.org
  61.  * @package kernel
  62.  **/
  63. class 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.     * Returns the values of the specified variables
  270.     *
  271.     * @param mixed $keys An array containing the names of the keys to retrieve, or null to get all of them
  272.     * @param string $format Format to use (see getVar)
  273.     * @param int $maxDepth Maximum level of recursion to use if some vars are objects themselves
  274.     * @return array associative array of key->value pairs
  275.     */
  276.     function getValues( $keys = null, $format = 's', $maxDepth = 1 ) {
  277.         if ( !isset( $keys ) ) {
  278.             $keys = array_keys( $this->vars );
  279.         }
  280.         $vars = array();
  281.         foreach ( $keys as $key ) {
  282.             if ( isset( $this->vars[$key] ) ) {
  283.                 if ( is_object( $this->vars[$key] ) && is_a( $this->vars[$key], 'XoopsObject' ) ) {
  284.                     if ( $maxDepth ) {
  285.                         $vars[$key] = $this->vars[$key]->getValues( null, $format, $maxDepth - 1 );
  286.                     }
  287.                 } else {
  288.                     $vars[$key] = $this->getVar( $key, $format );
  289.                 }
  290.             }
  291.         }
  292.         return $vars;
  293.     }
  294.     /**
  295.     * returns a specific variable for the object in a proper format
  296.     * 
  297.     * @access public
  298.     * @param string $key key of the object's variable to be returned
  299.     * @param string $format format to use for the output
  300.     * @return mixed formatted value of the variable
  301.     */
  302.     function getVar($key, $format = 's')
  303.     {
  304.         $ret = $this->vars[$key]['value'];
  305.         switch ($this->vars[$key]['data_type']) {
  306.  
  307.         case XOBJ_DTYPE_TXTBOX:
  308.             switch (strtolower($format)) {
  309.             case 's':
  310.             case 'show':
  311.             case 'e':
  312.             case 'edit':
  313.                 $ts =& MyTextSanitizer::getInstance();
  314.                 return $ts->htmlSpecialChars($ret);
  315.                 break 1;
  316.             case 'p':
  317.             case 'preview':
  318.             case 'f':
  319.             case 'formpreview':
  320.                 $ts =& MyTextSanitizer::getInstance();
  321.                 return $ts->htmlSpecialChars($ts->stripSlashesGPC($ret));
  322.                 break 1;
  323.             case 'n':
  324.             case 'none':
  325.             default:
  326.                 break 1;
  327.             }
  328.             break;
  329.         case XOBJ_DTYPE_TXTAREA:
  330.             switch (strtolower($format)) {
  331.             case 's':
  332.             case 'show':
  333.                 $ts =& MyTextSanitizer::getInstance();
  334.                 $html = !empty($this->vars['dohtml']['value']) ? 1 : 0;
  335.                 $xcode = (!isset($this->vars['doxcode']['value']) || $this->vars['doxcode']['value'] == 1) ? 1 : 0;
  336.                 $smiley = (!isset($this->vars['dosmiley']['value']) || $this->vars['dosmiley']['value'] == 1) ? 1 : 0;
  337.                 $image = (!isset($this->vars['doimage']['value']) || $this->vars['doimage']['value'] == 1) ? 1 : 0;
  338.                 $br = (!isset($this->vars['dobr']['value']) || $this->vars['dobr']['value'] == 1) ? 1 : 0;
  339.                 return $ts->displayTarea($ret, $html, $smiley, $xcode, $image, $br);
  340.                 break 1;
  341.             case 'e':
  342.             case 'edit':
  343.                 return htmlspecialchars($ret, ENT_QUOTES);
  344.                 break 1;
  345.             case 'p':
  346.             case 'preview':
  347.                 $ts =& MyTextSanitizer::getInstance();
  348.                 $html = !empty($this->vars['dohtml']['value']) ? 1 : 0;
  349.                 $xcode = (!isset($this->vars['doxcode']['value']) || $this->vars['doxcode']['value'] == 1) ? 1 : 0;
  350.                 $smiley = (!isset($this->vars['dosmiley']['value']) || $this->vars['dosmiley']['value'] == 1) ? 1 : 0;
  351.                 $image = (!isset($this->vars['doimage']['value']) || $this->vars['doimage']['value'] == 1) ? 1 : 0;
  352.                 $br = (!isset($this->vars['dobr']['value']) || $this->vars['dobr']['value'] == 1) ? 1 : 0;
  353.                 return $ts->previewTarea($ret, $html, $smiley, $xcode, $image, $br);
  354.                 break 1;
  355.             case 'f':
  356.             case 'formpreview':
  357.                 $ts =& MyTextSanitizer::getInstance();
  358.                 return htmlspecialchars($ts->stripSlashesGPC($ret), ENT_QUOTES);
  359.                 break 1;
  360.             case 'n':
  361.             case 'none':
  362.             default:
  363.                 break 1;
  364.             }
  365.             break;
  366.         case XOBJ_DTYPE_ARRAY:
  367.             $ret = unserialize($ret);
  368.             break;
  369.         case XOBJ_DTYPE_SOURCE:
  370.             switch (strtolower($format)) {
  371.             case 's':
  372.             case 'show':
  373.                 break 1;
  374.             case 'e':
  375.             case 'edit':
  376.                 return htmlspecialchars($ret, ENT_QUOTES);
  377.                 break 1;
  378.             case 'p':
  379.             case 'preview':
  380.                 $ts =& MyTextSanitizer::getInstance();
  381.                 return $ts->stripSlashesGPC($ret);
  382.                 break 1;
  383.             case 'f':
  384.             case 'formpreview':
  385.                 $ts =& MyTextSanitizer::getInstance();
  386.                 return htmlspecialchars($ts->stripSlashesGPC($ret), ENT_QUOTES);
  387.                 break 1;
  388.             case 'n':
  389.             case 'none':
  390.             default:
  391.                 break 1;
  392.             }
  393.             break;
  394.         default:
  395.             if ($this->vars[$key]['options'] != '' && $ret != '') {
  396.                 switch (strtolower($format)) {
  397.                 case 's':
  398.                 case 'show':
  399.                     $selected = explode('|', $ret);
  400.                     $options = explode('|', $this->vars[$key]['options']);
  401.                     $i = 1;
  402.                     $ret = array();
  403.                     foreach ($options as $op) {
  404.                         if (in_array($i, $selected)) {
  405.                             $ret[] = $op;
  406.                         }
  407.                         $i++;
  408.                     }
  409.                     return implode(', ', $ret);
  410.                 case 'e':
  411.                 case 'edit':
  412.                     $ret = explode('|', $ret);
  413.                     break 1;
  414.                 default:
  415.                     break 1;
  416.                 }
  417.  
  418.             }
  419.             break;
  420.         }
  421.         return $ret;
  422.     }
  423.  
  424.     /**
  425.      * clean values of all variables of the object for storage. 
  426.      * also add slashes whereever needed
  427.      * 
  428.      * @return bool true if successful
  429.      * @access public
  430.      */
  431.     function cleanVars()
  432.     {
  433.         $ts =& MyTextSanitizer::getInstance();
  434.         $existing_errors = $this->getErrors();
  435.         $this->_errors = array();
  436.         foreach ($this->vars as $k => $v) {
  437.             $cleanv = $v['value'];
  438.             if (!$v['changed']) {
  439.             } else {
  440.                 $cleanv = is_string($cleanv) ? trim($cleanv) : $cleanv;
  441.                 switch ($v['data_type']) {
  442.                 case XOBJ_DTYPE_TXTBOX:
  443.                     if ($v['required'] && $cleanv != '0' && $cleanv == '') {
  444.                         $this->setErrors( sprintf( _XOBJ_ERR_REQUIRED, $k ) );
  445.                         continue;
  446.                     }
  447.                     if (isset($v['maxlength']) && strlen($cleanv) > intval($v['maxlength'])) {
  448.                         $this->setErrors( sprintf( _XOBJ_ERR_SHORTERTHAN, $k, intval( $v['maxlength'] ) ) );
  449.                         continue;
  450.                     }
  451.                     if (!$v['not_gpc']) {
  452.                         $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv));
  453.                     } else {
  454.                         $cleanv = $ts->censorString($cleanv);
  455.                     }
  456.                     break;
  457.                 case XOBJ_DTYPE_TXTAREA:
  458.                     if ($v['required'] && $cleanv != '0' && $cleanv == '') {
  459.                         $this->setErrors( sprintf( _XOBJ_ERR_REQUIRED, $k ) );
  460.                         continue;
  461.                     }
  462.                     if (!$v['not_gpc']) {
  463.                         $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv));
  464.                     } else {
  465.                         $cleanv = $ts->censorString($cleanv);
  466.                     }
  467.                     break;
  468.                 case XOBJ_DTYPE_SOURCE:
  469.                     if (!$v['not_gpc']) {
  470.                         $cleanv = $ts->stripSlashesGPC($cleanv);
  471.                     } else {
  472.                         $cleanv = $cleanv;
  473.                     }
  474.                     break;
  475.                 case XOBJ_DTYPE_INT:
  476.                     $cleanv = intval($cleanv);
  477.                     break;
  478.                 case XOBJ_DTYPE_EMAIL:
  479.                     if ($v['required'] && $cleanv == '') {
  480.                         $this->setErrors( sprintf( _XOBJ_ERR_REQUIRED, $k ) );
  481.                         continue;
  482.                     }
  483.                     if ($cleanv != '' && !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i",$cleanv)) {
  484.                         $this->setErrors("Invalid Email");
  485.                         continue;
  486.                     }
  487.                     if (!$v['not_gpc']) {
  488.                         $cleanv = $ts->stripSlashesGPC($cleanv);
  489.                     }
  490.                     break;
  491.                 case XOBJ_DTYPE_URL:
  492.                     if ($v['required'] && $cleanv == '') {
  493.                         $this->setErrors( sprintf( _XOBJ_ERR_REQUIRED, $k ) );
  494.                         continue;
  495.                     }
  496.                     if ($cleanv != '' && !preg_match("/^http[s]*:\/\//i", $cleanv)) {
  497.                         $cleanv = 'http://' . $cleanv;
  498.                     }
  499.                     if (!$v['not_gpc']) {
  500.                         $cleanv =& $ts->stripSlashesGPC($cleanv);
  501.                     }
  502.                     break;
  503.                 case XOBJ_DTYPE_ARRAY:
  504.                     $cleanv = serialize($cleanv);
  505.                     break;
  506.                 case XOBJ_DTYPE_STIME:
  507.                 case XOBJ_DTYPE_MTIME:
  508.                 case XOBJ_DTYPE_LTIME:
  509.                     $cleanv = !is_string($cleanv) ? intval($cleanv) : strtotime($cleanv);
  510.                     break;
  511.                 default:
  512.                     break;
  513.                 }
  514.             }
  515.             $this->cleanVars[$k] =& $cleanv;
  516.             unset($cleanv);
  517.         }
  518.         if (count($this->_errors) > 0) {
  519.             $this->_errors = array_merge($existing_errors, $this->_errors);
  520.             return false;
  521.         }
  522.         $this->_errors = array_merge($existing_errors, $this->_errors);
  523.         $this->unsetDirty();
  524.         return true;
  525.     }
  526.  
  527.     /**
  528.      * dynamically register additional filter for the object
  529.      * 
  530.      * @param string $filtername name of the filter
  531.      * @access public
  532.      */
  533.     function registerFilter($filtername)
  534.     {
  535.         $this->_filters[] = $filtername;
  536.     }
  537.  
  538.     /**
  539.      * load all additional filters that have been registered to the object
  540.      * 
  541.      * @access private
  542.      */
  543.     function _loadFilters()
  544.     {
  545.         //include_once XOOPS_ROOT_PATH.'/class/filters/filter.php';
  546.         //foreach ($this->_filters as $f) {
  547.         //    include_once XOOPS_ROOT_PATH.'/class/filters/'.strtolower($f).'php';
  548.         //}
  549.     }
  550.  
  551.     /**
  552.      * create a clone(copy) of the current object
  553.      * 
  554.      * @access public
  555.      * @return object clone
  556.      */
  557.     function &xoopsClone()
  558.     {
  559.         $class = get_class($this);
  560.         $clone =& new $class();
  561.         foreach ($this->vars as $k => $v) {
  562.             $clone->assignVar($k, $v['value']);
  563.         }
  564.         // need this to notify the handler class that this is a newly created object
  565.         $clone->setNew();
  566.         return $clone;
  567.     }
  568.  
  569.     /**
  570.      * add an error 
  571.      * 
  572.      * @param string $value error to add
  573.      * @access public
  574.      */
  575.     function setErrors($err_str)
  576.     {
  577.         $this->_errors[] = trim($err_str);
  578.     }
  579.  
  580.     /**
  581.      * return the errors for this object as an array
  582.      * 
  583.      * @return array an array of errors
  584.      * @access public
  585.      */
  586.     function getErrors()
  587.     {
  588.         return $this->_errors;
  589.     }
  590.  
  591.     /**
  592.      * return the errors for this object as html
  593.      * 
  594.      * @return string html listing the errors
  595.      * @access public
  596.      */
  597.     function getHtmlErrors()
  598.     {
  599.         $ret = '<h4>Errors</h4>';
  600.         if (!empty($this->_errors)) {
  601.             foreach ($this->_errors as $error) {
  602.                 $ret .= $error.'<br />';
  603.             }
  604.         } else {
  605.             $ret .= 'None<br />';
  606.         }
  607.         return $ret;
  608.     }
  609. }
  610.  
  611. /**
  612. * XOOPS object handler class.  
  613. * This class is an abstract class of handler classes that are responsible for providing
  614. * data access mechanisms to the data source of its corresponsing data objects
  615. * @package kernel
  616. * @abstract
  617. *
  618. * @author  Kazumi Ono <onokazu@xoops.org>
  619. * @copyright copyright © 2000 The XOOPS Project
  620. */
  621. class XoopsObjectHandler
  622. {
  623.  
  624.     /**
  625.      * holds referenced to {@link XoopsDatabase} class object
  626.      * 
  627.      * @var object
  628.      * @see XoopsDatabase
  629.      * @access protected
  630.      */
  631.     var $db;
  632.  
  633.     // 
  634.     /**
  635.      * called from child classes only
  636.      * 
  637.      * @param object $db reference to the {@link XoopsDatabase} object
  638.      * @access protected
  639.      */
  640.     function XoopsObjectHandler(&$db)
  641.     {
  642.         $this->db =& $db;
  643.     }
  644.  
  645.     /**
  646.      * creates a new object
  647.      * 
  648.      * @abstract
  649.      */
  650.     function &create()
  651.     {
  652.     }
  653.  
  654.     /**
  655.      * gets a value object
  656.      * 
  657.      * @param int $int_id
  658.      * @abstract
  659.      */
  660.     function &get($int_id)
  661.     {
  662.     }
  663.  
  664.     /**
  665.      * insert/update object
  666.      * 
  667.      * @param object $object
  668.      * @abstract
  669.      */
  670.     function insert(&$object)
  671.     {
  672.     }
  673.  
  674.     /**
  675.      * delete obejct from database
  676.      * 
  677.      * @param object $object
  678.      * @abstract
  679.      */
  680.     function delete(&$object)
  681.     {
  682.     }
  683.  
  684. }
  685. ?>