1: <?php
2: /**
3: * XOOPS Kernel Class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: */
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: /**#@+
21: * Config type
22: */
23: define('XOOPS_CONF', 1);
24: define('XOOPS_CONF_USER', 2);
25: define('XOOPS_CONF_METAFOOTER', 3);
26: define('XOOPS_CONF_CENSOR', 4);
27: define('XOOPS_CONF_SEARCH', 5);
28: define('XOOPS_CONF_MAILER', 6);
29: define('XOOPS_CONF_AUTH', 7);
30: /**#@-*/
31:
32: /**
33: *
34: *
35: * @author Kazumi Ono <onokazu@xoops.org>
36: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
37: */
38: class XoopsConfigItem extends XoopsObject
39: {
40: /**
41: * Config options
42: *
43: * @var array
44: * @access private
45: */
46: public $_confOptions = array();
47: //PHP 8.2 Dynamic properties deprecated
48: public $conf_id;
49: public $conf_modid;
50: public $conf_catid;
51: public $conf_name;
52: public $conf_title;
53: public $conf_value;
54: public $conf_desc;
55: public $conf_formtype;
56: public $conf_valuetype;
57: public $conf_order;
58:
59: /**
60: * Constructor
61: */
62: public function __construct()
63: {
64: $this->initVar('conf_id', XOBJ_DTYPE_INT, null, false);
65: $this->initVar('conf_modid', XOBJ_DTYPE_INT, null, false);
66: $this->initVar('conf_catid', XOBJ_DTYPE_INT, null, false);
67: $this->initVar('conf_name', XOBJ_DTYPE_OTHER);
68: $this->initVar('conf_title', XOBJ_DTYPE_TXTBOX);
69: $this->initVar('conf_value', XOBJ_DTYPE_TXTAREA);
70: $this->initVar('conf_desc', XOBJ_DTYPE_OTHER);
71: $this->initVar('conf_formtype', XOBJ_DTYPE_OTHER);
72: $this->initVar('conf_valuetype', XOBJ_DTYPE_OTHER);
73: $this->initVar('conf_order', XOBJ_DTYPE_INT);
74: }
75:
76: /**
77: * Returns Class Base Variable conf_id
78: * @param string $format
79: * @return mixed
80: */
81: public function id($format = 'N')
82: {
83: return $this->getVar('conf_id', $format);
84: }
85:
86: /**
87: * Returns Class Base Variable conf_id
88: * @param string $format
89: * @return mixed
90: */
91: public function conf_id($format = '')
92: {
93: return $this->getVar('conf_id', $format);
94: }
95:
96: /**
97: * Returns Class Base Variable conf_modid
98: * @param string $format
99: * @return mixed
100: */
101: public function conf_modid($format = '')
102: {
103: return $this->getVar('conf_modid', $format);
104: }
105:
106: /**
107: * Returns Class Base Variable conf_catid
108: * @param string $format
109: * @return mixed
110: */
111: public function conf_catid($format = '')
112: {
113: return $this->getVar('conf_catid', $format);
114: }
115:
116: /**
117: * Returns Class Base Variable conf_name
118: * @param string $format
119: * @return mixed
120: */
121: public function conf_name($format = '')
122: {
123: return $this->getVar('conf_name', $format);
124: }
125:
126: /**
127: * Returns Class Base Variable conf_title
128: * @param string $format
129: * @return mixed
130: */
131: public function conf_title($format = '')
132: {
133: return $this->getVar('conf_title', $format);
134: }
135:
136: /**
137: * Returns Class Base Variable conf_value
138: * @param string $format
139: * @return mixed
140: */
141: public function conf_value($format = '')
142: {
143: return $this->getVar('conf_value', $format);
144: }
145:
146: /**
147: * Returns Class Base Variable conf_desc
148: * @param string $format
149: * @return mixed
150: */
151: public function conf_desc($format = '')
152: {
153: return $this->getVar('conf_desc', $format);
154: }
155:
156: /**
157: * Returns Class Base Variable conf_formtype
158: * @param string $format
159: * @return mixed
160: */
161: public function conf_formtype($format = '')
162: {
163: return $this->getVar('conf_formtype', $format);
164: }
165:
166: /**
167: * Returns Class Base Variable conf_valuetype
168: * @param string $format
169: * @return mixed
170: */
171: public function conf_valuetype($format = '')
172: {
173: return $this->getVar('conf_valuetype', $format);
174: }
175:
176: /**
177: * Returns Class Base Variable conf_order
178: * @param string $format
179: * @return mixed
180: */
181: public function conf_order($format = '')
182: {
183: return $this->getVar('conf_order', $format);
184: }
185:
186: /**
187: * Get a config value in a format ready for output
188: *
189: * @return string
190: */
191: public function getConfValueForOutput()
192: {
193: switch ($this->getVar('conf_valuetype')) {
194: case 'int':
195: return (int)$this->getVar('conf_value', 'N');
196: break;
197: case 'array':
198: $value = @unserialize($this->getVar('conf_value', 'N'));
199:
200: return $value ?: array();
201: case 'float':
202: $value = $this->getVar('conf_value', 'N');
203:
204: return (float)$value;
205: break;
206: case 'textarea':
207: return $this->getVar('conf_value');
208: default:
209: return $this->getVar('conf_value', 'N');
210: break;
211: }
212: }
213:
214: /**
215: * Set a config value
216: *
217: * @param mixed &$value Value
218: * @param bool $force_slash
219: */
220: public function setConfValueForInput(&$value, $force_slash = false)
221: {
222: switch ($this->getVar('conf_valuetype')) {
223: case 'array':
224: if (!is_array($value)) {
225: $value = explode('|', trim((string)$value));
226: }
227: $this->setVar('conf_value', serialize($value), $force_slash);
228: break;
229: case 'text':
230: $this->setVar('conf_value', trim((string)$value), $force_slash);
231: break;
232: default:
233: $this->setVar('conf_value', $value, $force_slash);
234: break;
235: }
236: }
237:
238: /**
239: * Assign one or more {@link XoopsConfigItemOption}s
240: *
241: * @param mixed $option either a {@link XoopsConfigItemOption} object or an array of them
242: */
243: public function setConfOptions($option)
244: {
245: if (is_array($option)) {
246: $count = count($option);
247: for ($i = 0; $i < $count; ++$i) {
248: $this->setConfOptions($option[$i]);
249: }
250: } else {
251: if (is_object($option)) {
252: $this->_confOptions[] =& $option;
253: }
254: }
255: }
256:
257: /**
258: * Get the {@link XoopsConfigItemOption}s of this Config
259: *
260: * @return array array of {@link XoopsConfigItemOption}
261: */
262: public function &getConfOptions()
263: {
264: return $this->_confOptions;
265: }
266:
267: /**
268: * Clear options from this item
269: *
270: * @return void
271: **/
272: public function clearConfOptions()
273: {
274: $this->_confOptions = array();
275: }
276: }
277:
278: /**
279: * XOOPS configuration handler class.
280: *
281: * This class is responsible for providing data access mechanisms to the data source
282: * of XOOPS configuration class objects.
283: *
284: * @author Kazumi Ono <onokazu@xoops.org>
285: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
286: */
287: class XoopsConfigItemHandler extends XoopsObjectHandler
288: {
289: /**
290: * Create a new {@link XoopsConfigItem}
291: *
292: * @see XoopsConfigItem
293: * @param bool $isNew Flag the config as "new"?
294: * @return XoopsConfigItem reference to the new config
295: */
296: public function create($isNew = true)
297: {
298: $config = new XoopsConfigItem();
299: if ($isNew) {
300: $config->setNew();
301: }
302:
303: return $config;
304: }
305:
306: /**
307: * Load a config from the database
308: *
309: * @param int $id ID of the config
310: * @return XoopsConfigItem|false reference to the config, false on fail
311: */
312: public function get($id)
313: {
314: $config = false;
315: $id = (int)$id;
316: if ($id > 0) {
317: $sql = 'SELECT * FROM ' . $this->db->prefix('config') . ' WHERE conf_id=' . $id;
318: $result = $this->db->query($sql);
319: if (!$this->db->isResultSet($result)) {
320: return $config;
321: }
322: $numrows = $this->db->getRowsNum($result);
323: if ($numrows == 1) {
324: $myrow = $this->db->fetchArray($result);
325: $config = new XoopsConfigItem();
326: $config->assignVars($myrow);
327: }
328: }
329:
330: return $config;
331: }
332:
333: /**
334: * Write a config to the database
335: *
336: * @param XoopsObject|XoopsConfigItem $config a XoopsConfigCategory object
337: *
338: * @return bool true on success, otherwise false
339: */
340: public function insert(XoopsObject $config)
341: {
342: $className = 'XoopsConfigItem';
343: if (!($config instanceof $className)) {
344: return false;
345: }
346: if (!$config->isDirty()) {
347: return true;
348: }
349: if (!$config->cleanVars()) {
350: return false;
351: }
352:
353: $conf_id = $config->getVar('conf_id', 'n');
354: $conf_modid = $config->getVar('conf_modid', 'n');
355: $conf_catid = $config->getVar('conf_catid', 'n');
356: $conf_name = $config->getVar('conf_name', 'n');
357: $conf_title = $config->getVar('conf_title', 'n');
358: $conf_value = $config->getVar('conf_value', 'n');
359: $conf_desc = $config->getVar('conf_desc', 'n');
360: $conf_formtype = $config->getVar('conf_formtype', 'n');
361: $conf_valuetype = $config->getVar('conf_valuetype', 'n');
362: $conf_order = $config->getVar('conf_order', 'n');
363:
364: if ($config->isNew()) {
365: $conf_id = $this->db->genId('config_conf_id_seq');
366: $sql = sprintf('INSERT INTO %s (conf_id, conf_modid, conf_catid, conf_name, conf_title, conf_value, conf_desc, conf_formtype, conf_valuetype, conf_order) VALUES (%u, %u, %u, %s, %s, %s, %s, %s, %s, %u)', $this->db->prefix('config'), $conf_id, $conf_modid, $conf_catid, $this->db->quoteString($conf_name), $this->db->quoteString($conf_title), $this->db->quoteString($conf_value), $this->db->quoteString($conf_desc), $this->db->quoteString($conf_formtype), $this->db->quoteString($conf_valuetype), $conf_order);
367: } else {
368: $sql = sprintf('UPDATE %s SET conf_modid = %u, conf_catid = %u, conf_name = %s, conf_title = %s, conf_value = %s, conf_desc = %s, conf_formtype = %s, conf_valuetype = %s, conf_order = %u WHERE conf_id = %u', $this->db->prefix('config'), $conf_modid, $conf_catid, $this->db->quoteString($conf_name), $this->db->quoteString($conf_title), $this->db->quoteString($conf_value), $this->db->quoteString($conf_desc), $this->db->quoteString($conf_formtype), $this->db->quoteString($conf_valuetype), $conf_order, $conf_id);
369: }
370: if (!$result = $this->db->query($sql)) {
371: return false;
372: }
373: if (empty($conf_id)) {
374: $conf_id = $this->db->getInsertId();
375: }
376: $config->assignVar('conf_id', $conf_id);
377:
378: return true;
379: }
380:
381: /**
382: * Delete a config from the database
383: *
384: * @param XoopsObject|XoopsConfigItem $config a XoopsConfigCategory object
385: *
386: * @return bool true on success, otherwise false
387: */
388: public function delete(XoopsObject $config)
389: {
390: $className = 'XoopsConfigItem';
391: if (!($config instanceof $className)) {
392: return false;
393: }
394: $sql = sprintf('DELETE FROM %s WHERE conf_id = %u', $this->db->prefix('config'), $config->getVar('conf_id'));
395: if (!$result = $this->db->query($sql)) {
396: return false;
397: }
398:
399: return true;
400: }
401:
402: /**
403: * Get configs from the database
404: *
405: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
406: * @param bool $id_as_key return the config's id as key?
407: * @return array Array of {@link XoopsConfigItem} objects
408: */
409: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
410: {
411: $ret = array();
412: $limit = $start = 0;
413: $sql = 'SELECT * FROM ' . $this->db->prefix('config');
414: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
415: $sql .= ' ' . $criteria->renderWhere();
416: $sql .= ' ORDER BY conf_order ASC';
417: $limit = $criteria->getLimit();
418: $start = $criteria->getStart();
419: }
420: $result = $this->db->query($sql, $limit, $start);
421: if (!$this->db->isResultSet($result)) {
422: return $ret;
423: }
424: /** @var array $myrow */
425: while (false !== ($myrow = $this->db->fetchArray($result))) {
426: $config = new XoopsConfigItem();
427: $config->assignVars($myrow);
428: if (!$id_as_key) {
429: $ret[] =& $config;
430: } else {
431: $ret[$myrow['conf_id']] = &$config;
432: }
433: unset($config);
434: }
435:
436: return $ret;
437: }
438:
439: /**
440: * Count configs
441: *
442: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
443: * @return int Count of configs matching $criteria
444: */
445: public function getCount(CriteriaElement $criteria = null)
446: {
447: $sql = 'SELECT * FROM ' . $this->db->prefix('config');
448: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
449: $sql .= ' ' . $criteria->renderWhere();
450: }
451: $result = $this->db->query($sql);
452: if (!$this->db->isResultSet($result)) {
453: return 0;
454: }
455: list($count) = $this->db->fetchRow($result);
456:
457: return (int)$count;
458: }
459: }
460: