21 defined(
'XOOPS_ROOT_PATH') or die('Restricted access');
74 var $cleanVars = array();
90 var $_isDirty =
false;
98 var $_errors = array();
105 var $_filters = array();
115 function XoopsObject()
127 $this->_isNew =
true;
131 $this->_isNew =
false;
135 return $this->_isNew;
148 $this->_isDirty =
true;
150 function unsetDirty()
152 $this->_isDirty =
false;
156 return $this->_isDirty;
173 function initVar($key, $data_type, $value = null, $required =
false, $maxlength = null,
$options =
'', $enumerations =
'')
175 $this->vars[$key] = array(
'value' => $value ,
'required' => $required ,
'data_type' => $data_type ,
'maxlength' => $maxlength ,
'changed' =>
false ,
'options' =>
$options,
'enumeration' => $enumerations);
185 function assignVar($key, $value)
187 if (isset($key) && isset($this->vars[$key])) {
188 switch ($this->vars[$key][
'data_type']) {
189 case XOBJ_DTYPE_UNICODE_ARRAY:
190 if (is_array($value)) {
191 $this->vars[$key][
'value'] =& array_walk($value,
"xoops_aw_decode");
196 case XOBJ_DTYPE_UNICODE_URL:
197 case XOBJ_DTYPE_UNICODE_EMAIL:
198 case XOBJ_DTYPE_UNICODE_OTHER:
199 case XOBJ_DTYPE_UNICODE_TXTBOX:
200 case XOBJ_DTYPE_UNICODE_TXTAREA:
203 case XOBJ_DTYPE_DATE:
204 if (!is_string($value)&&is_numeric($value)) {
207 $this->vars[$key][
'value'] =& date(
_DBDATESTRING, strtotime($value));
210 case XOBJ_DTYPE_TIME:
211 if (!is_string($value)&&is_numeric($value)) {
214 $this->vars[$key][
'value'] =& date(
_DBTIMESTRING, strtotime($value));
217 case XOBJ_DTYPE_TIMESTAMP:
218 if (!is_string($value)&&is_numeric($value)) {
226 $this->vars[$key][
'value'] =& $value;
237 function assignVars($var_arr)
239 foreach ($var_arr as $key => $value) {
240 $this->assignVar($key, $value);
252 function setVar($key, $value, $not_gpc =
false)
254 if (!empty($key) && isset($value) && isset($this->vars[$key])) {
255 $this->vars[$key][
'value'] =& $value;
256 $this->vars[$key][
'not_gpc'] = $not_gpc;
257 $this->vars[$key][
'changed'] =
true;
269 function setVars($var_arr, $not_gpc =
false)
271 foreach ($var_arr as $key => $value) {
272 $this->setVar($key, $value, $not_gpc);
288 foreach (
$var as $key) {
289 if (!isset($this->vars[$key])) {
292 $this->vars[$key][
'changed'] = null;
308 function setFormVars($var_arr = null, $pref =
'xo_', $not_gpc =
false)
310 $len = strlen($pref);
311 foreach ($var_arr as $key => $value) {
312 if ($pref == substr($key, 0, $len)) {
313 $this->setVar(substr($key, $len), $value, $not_gpc);
337 function getValues($keys = null, $format =
's', $maxDepth = 1)
340 $keys = array_keys($this->vars);
343 foreach ($keys as $key) {
344 if (isset($this->vars[$key])) {
345 if (is_object($this->vars[$key]) && is_a($this->vars[$key],
'XoopsObject')) {
347 $vars[$key] = $this->vars[$key]->getValues(null, $format, $maxDepth - 1);
350 $vars[$key] = $this->getVar($key, $format);
367 function getVar($key, $format =
's')
370 if (!isset($this->vars[$key])) {
373 $ret = $this->vars[$key][
'value'];
375 switch ($this->vars[$key][
'data_type']) {
376 case XOBJ_DTYPE_UNICODE_TXTBOX:
377 case XOBJ_DTYPE_TXTBOX:
378 switch (strtolower($format)) {
383 return $ts->htmlSpecialChars(
$ret);
389 return $ts->htmlSpecialChars($ts->stripSlashesGPC(
$ret));
397 case XOBJ_DTYPE_UNICODE_TXTAREA:
398 case XOBJ_DTYPE_TXTAREA:
399 switch (strtolower($format)) {
402 $html = !empty($this->vars[
'dohtml'][
'value']) ? 1 : 0;
403 $xcode = (!isset($this->vars[
'doxcode'][
'value']) || $this->vars[
'doxcode'][
'value'] == 1) ? 1 : 0;
404 $smiley = (!isset($this->vars[
'dosmiley'][
'value']) || $this->vars[
'dosmiley'][
'value'] == 1) ? 1 : 0;
405 $image = (!isset($this->vars[
'doimage'][
'value']) || $this->vars[
'doimage'][
'value'] == 1) ? 1 : 0;
406 $br = (!isset($this->vars[
'dobr'][
'value']) || $this->vars[
'dobr'][
'value'] == 1) ? 1 : 0;
411 return htmlspecialchars(
$ret, ENT_QUOTES);
415 $html = !empty($this->vars[
'dohtml'][
'value']) ? 1 : 0;
416 $xcode = (!isset($this->vars[
'doxcode'][
'value']) || $this->vars[
'doxcode'][
'value'] == 1) ? 1 : 0;
417 $smiley = (!isset($this->vars[
'dosmiley'][
'value']) || $this->vars[
'dosmiley'][
'value'] == 1) ? 1 : 0;
418 $image = (!isset($this->vars[
'doimage'][
'value']) || $this->vars[
'doimage'][
'value'] == 1) ? 1 : 0;
419 $br = (!isset($this->vars[
'dobr'][
'value']) || $this->vars[
'dobr'][
'value'] == 1) ? 1 : 0;
424 return htmlspecialchars($ts->stripSlashesGPC(
$ret), ENT_QUOTES);
432 case XOBJ_DTYPE_UNICODE_ARRAY:
433 switch (strtolower($format)) {
438 if (!is_array(
$ret)) {
443 if (is_array(
$ret)) {
444 $ret = array_walk(
$ret,
"xoops_aw_decode");
451 case XOBJ_DTYPE_ARRAY:
452 switch (strtolower($format)) {
457 if (!is_array(
$ret)) {
467 case XOBJ_DTYPE_SOURCE:
468 switch (strtolower($format)) {
474 return htmlspecialchars(
$ret, ENT_QUOTES);
478 return $ts->stripSlashesGPC(
$ret);
482 return htmlspecialchars($ts->stripSlashesGPC(
$ret), ENT_QUOTES);
490 case XOBJ_DTYPE_DATE:
491 switch (strtolower($format)) {
494 if (is_string(
$ret)&&!is_numeric(
$ret)) {
502 if (is_string(
$ret)&&!is_numeric(
$ret)) {
510 if (is_string(
$ret)&&!is_numeric(
$ret)) {
518 if (is_string(
$ret)&&!is_numeric(
$ret)) {
519 return htmlspecialchars($ts->stripSlashesGPC(date(
_DBDATESTRING, strtotime(
$ret))), ENT_QUOTES);
521 return htmlspecialchars($ts->stripSlashesGPC(date(
_DBDATESTRING,
$ret)), ENT_QUOTES);
529 case XOBJ_DTYPE_TIME:
530 switch (strtolower($format)) {
533 if (is_string(
$ret)&&!is_numeric(
$ret)) {
541 if (is_string(
$ret)&&!is_numeric(
$ret)) {
549 if (is_string(
$ret)&&!is_numeric(
$ret)) {
557 if (is_string(
$ret)&&!is_numeric(
$ret)) {
558 return htmlspecialchars($ts->stripSlashesGPC(date(
_DBTIMESTRING, strtotime(
$ret))), ENT_QUOTES);
560 return htmlspecialchars($ts->stripSlashesGPC(date(
_DBTIMESTRING,
$ret)), ENT_QUOTES);
568 case XOBJ_DTYPE_TIMESTAMP:
569 switch (strtolower($format)) {
572 if (is_string(
$ret)&&!is_numeric(
$ret)) {
580 if (is_string(
$ret)&&!is_numeric(
$ret)) {
588 if (is_string(
$ret)&&!is_numeric(
$ret)) {
596 if (is_string(
$ret)&&!is_numeric(
$ret)) {
608 if ($this->vars[$key][
'options'] !=
'' &&
$ret !=
'') {
609 switch (strtolower($format)) {
612 $selected = explode(
'|',
$ret);
613 $options = explode(
'|', $this->vars[$key][
'options']);
617 if (in_array(
$i, $selected)) {
622 return implode(
', ',
$ret);
648 $existing_errors = $this->getErrors();
649 $this->_errors = array();
650 foreach ($this->vars as $k => $v) {
651 $cleanv = $v[
'value'];
652 if (!$v[
'changed']) {
654 $cleanv = is_string($cleanv) ? trim($cleanv) : $cleanv;
655 switch ($v[
'data_type']) {
656 case XOBJ_DTYPE_TIMESTAMP:
659 case XOBJ_DTYPE_TIME:
662 case XOBJ_DTYPE_DATE:
665 case XOBJ_DTYPE_TXTBOX:
666 if ($v[
'required'] && $cleanv !=
'0' && $cleanv ==
'') {
670 if (isset($v[
'maxlength']) && strlen($cleanv) > intval($v[
'maxlength'])) {
674 if (!$v[
'not_gpc']) {
675 $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv));
677 $cleanv = $ts->censorString($cleanv);
680 case XOBJ_DTYPE_TXTAREA:
681 if ($v[
'required'] && $cleanv !=
'0' && $cleanv ==
'') {
685 if (!$v[
'not_gpc']) {
686 $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv));
688 $cleanv = $ts->censorString($cleanv);
691 case XOBJ_DTYPE_SOURCE:
692 if (!$v[
'not_gpc']) {
693 $cleanv = $ts->stripSlashesGPC($cleanv);
699 $cleanv = intval($cleanv);
702 case XOBJ_DTYPE_EMAIL:
703 if ($v[
'required'] && $cleanv ==
'') {
707 if ($cleanv !=
'' && !preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i", $cleanv)) {
708 $this->setErrors(
"Invalid Email");
711 if (!$v[
'not_gpc']) {
712 $cleanv = $ts->stripSlashesGPC($cleanv);
716 if ($v[
'required'] && $cleanv ==
'') {
720 if ($cleanv !=
'' && !preg_match(
"/^http[s]*:\/\//i", $cleanv)) {
721 $cleanv =
'http://' . $cleanv;
723 if (!$v[
'not_gpc']) {
724 $cleanv =& $ts->stripSlashesGPC($cleanv);
727 case XOBJ_DTYPE_ARRAY:
728 $cleanv = (array) $cleanv;
729 $cleanv = serialize($cleanv);
731 case XOBJ_DTYPE_STIME:
732 case XOBJ_DTYPE_MTIME:
733 case XOBJ_DTYPE_LTIME:
734 $cleanv = !is_string($cleanv) ? intval($cleanv) : strtotime($cleanv);
736 case XOBJ_DTYPE_FLOAT:
737 $cleanv = floatval($cleanv);
739 case XOBJ_DTYPE_DECIMAL:
740 $cleanv = doubleval($cleanv);
742 case XOBJ_DTYPE_ENUM:
743 if (!in_array($cleanv, $v[
'enumeration'])) {
744 $this->setErrors(
"Invalid Enumeration");
748 case XOBJ_DTYPE_UNICODE_TXTBOX:
749 if ($v[
'required'] && $cleanv !=
'0' && $cleanv ==
'') {
754 if (isset($v[
'maxlength']) && strlen($cleanv) > intval($v[
'maxlength'])) {
758 if (!$v[
'not_gpc']) {
759 $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv));
761 $cleanv = $ts->censorString($cleanv);
764 case XOBJ_DTYPE_UNICODE_TXTAREA:
765 if ($v[
'required'] && $cleanv !=
'0' && $cleanv ==
'') {
770 if (!$v[
'not_gpc']) {
771 $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv));
773 $cleanv = $ts->censorString($cleanv);
776 case XOBJ_DTYPE_UNICODE_EMAIL:
777 if ($v[
'required'] && $cleanv ==
'') {
781 if ($cleanv !=
'' && !preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i", $cleanv)) {
782 $this->setErrors(
"Invalid Email");
786 if (!$v[
'not_gpc']) {
787 $cleanv = $ts->stripSlashesGPC($cleanv);
790 case XOBJ_DTYPE_UNICODE_URL:
791 if ($v[
'required'] && $cleanv ==
'') {
795 if ($cleanv !=
'' && !preg_match(
"/^http[s]*:\/\//i", $cleanv)) {
796 $cleanv =
'http://' . $cleanv;
799 if (!$v[
'not_gpc']) {
800 $cleanv =& $ts->stripSlashesGPC($cleanv);
803 case XOBJ_DTYPE_UNICODE_ARRAY:
804 $cleanv = serialize(array_walk($cleanv,
'xoops_aw_encode'));
811 $this->cleanVars[$k] = str_replace(
'\\"',
'"', $cleanv);
814 if (count($this->_errors) > 0) {
815 $this->_errors = array_merge($existing_errors, $this->_errors);
818 $this->_errors = array_merge($existing_errors, $this->_errors);
829 function registerFilter($filtername)
831 $this->_filters[] = $filtername;
839 function _loadFilters()
842 if (isset($loaded)) {
847 $path = empty($this->plugin_path) ? dirname(__FILE__) .
'/filters' : $this->plugin_path;
848 if (file_exists(
$file =
$path .
'/filter.php')) {
850 foreach ($this->_filters as $f) {
851 if (file_exists($file =
$path .
'/' . strtolower($f) .
'php')) {
872 $this->_loadFilters();
875 $class = get_class($this);
879 $modules_active = array();
880 foreach (array_keys($modules_obj) as $key) {
881 $modules_active[] = $modules_obj[$key]->getVar(
'dirname');
886 foreach ($modules_active as
$dirname) {
887 if (file_exists(
$file = XOOPS_ROOT_PATH .
'/modules/' . $dirname .
'/filter/' . $class .
'.' .
$method .
'.php')) {
889 if (function_exists($class .
'_' . $method)) {
890 call_user_func_array($dirname .
'_' . $class .
'_' . $method, array(&$this));
902 function &xoopsClone()
904 $class = get_class($this);
906 $clone =
new $class();
907 foreach ($this->vars as $k => $v) {
908 $clone->assignVar($k, $v[
'value']);
921 function setErrors($err_str)
923 if (is_array($err_str)) {
924 $this->_errors = array_merge($this->_errors, $err_str);
926 $this->_errors[] = trim($err_str);
938 return $this->_errors;
947 function getHtmlErrors()
949 $ret =
'<h4>Errors</h4>';
950 if (!empty($this->_errors)) {
951 foreach ($this->_errors as
$error) {
952 $ret .= $error .
'<br />';
955 $ret .=
'None<br />';
969 return $this->getValues();
1020 function &
get($int_id)
1040 function delete(&$object)
1081 var
$handlers = array(
'read' => null ,
'stats' => null ,
'joint' => null ,
'write' => null ,
'sync' => null);
1142 $this->handler = null;
1197 if (is_object($this->handler) && is_callable(array($this->handler , $name))) {
1198 return call_user_func_array(array($this->handler , $name), $args);
1200 foreach (array_keys($this->handlers) as $_handler) {
1202 if (is_callable(array(
$handler, $name))) {
1203 return call_user_func_array(array(
$handler , $name), $args);
1223 $obj =
new $this->className();
1224 if ($isNew ===
true) {
1242 $object = $this->
create();
1246 $select = implode(
',',
$fields);
1247 if (!in_array($this->keyName,
$fields)) {
1253 $sql = sprintf(
'SELECT %s FROM %s WHERE %s = %s', $select, $this->table, $this->keyName, $this->db->quote($id));
1258 if (!$this->db->getRowsNum(
$result)) {
1261 $object =& $this->
create(
false);
1262 $object->assignVars($this->db->fetchArray(
$result));
1284 return $handler->insert($object, $force);
1294 function delete(&$object, $force =
false)
1297 return $handler->delete($object, $force);
1537 function cleanOrphan($table_link =
'', $field_link =
'', $field_object =
'')
1540 $ret =
$handler->cleanOrphan($table_link, $field_link, $field_object);
1563 trigger_error(__CLASS__ .
"::" .__FUNCTION__ .
' is deprecated', E_USER_WARNING);