1: <?php
2: /**
3: *
4: * @package XCube
5: * @version $Id: XCube_Property.class.php,v 1.7 2008/10/12 04:30:27 minahito Exp $
6: * @copyright Copyright 2005-2007 XOOPS Cube Project <https://github.com/xoopscube/legacy>
7: * @license https://github.com/xoopscube/legacy/blob/master/docs/bsd_licenses.txt Modified BSD license
8: *
9: */
10:
11: /**
12: * @public
13: * @brief [Abstract] Defines a interface for the property class group.
14: *
15: * XCube_PropertyInterface is designed to work in XCube_ActionForm or XCube_Service (in the near future).
16: * Therefore only sub-classes of them should call constructors of XCube_Property classes.
17: */
18: class XCube_PropertyInterface
19: {
20: /**
21: * @public
22: * @brief Constructor.
23: * @param $name string - A name of this property.
24: */
25: function XCube_PropertyInterface($name)
26: {
27: }
28:
29: /**
30: * @public
31: * @brief [Abstract] Sets $value as raw value to this property. And the value is casted by the property's type'.
32: * @param $value mixed
33: */
34: function set($value)
35: {
36: }
37:
38: /**
39: * @public
40: * @brief [Abstract] Gets the value of this property.
41: * @return mixed
42: */
43: function get()
44: {
45: }
46:
47: /**
48: * @deprecated
49: */
50: function setValue($arg0 = null, $arg1 = null)
51: {
52: $this->set($arg0, $arg1);
53: }
54:
55: /**
56: * @deprecated
57: */
58: function getValue($arg0 = null)
59: {
60: return $this->get($arg0);
61: }
62:
63: /**
64: * @public
65: * @brief [Abstract] Gets a value indicating whether this object expresses Array.
66: * @return bool
67: */
68: function isArray()
69: {
70: }
71:
72: /**
73: * @public
74: * @brief [Abstract] Gets a value indicating whether this object is null.
75: * @return bool
76: */
77: function isNull()
78: {
79: }
80:
81: /**
82: * @public
83: * @brief [Abstract] Gets a value as integer.
84: * @return int
85: */
86: function toNumber()
87: {
88: }
89:
90: /**
91: * @public
92: * @brief [Abstract] Gets a value as string.
93: * @return string
94: */
95: function toString()
96: {
97: }
98:
99: /**
100: * @public
101: * @brief [Abstract] Gets a value as encoded HTML code.
102: * @return string - HTML
103: * @deprecated
104: */
105: function toHTML()
106: {
107: }
108:
109: /**
110: * @public
111: * @brief [Abstract] Gets a value indicating whether this object has a fetch control.
112: * @return bool
113: */
114: function hasFetchControl()
115: {
116: }
117:
118: /**
119: * @public [Abstract] Fetches values.
120: * @param $form XCube_ActionForm
121: * @return void
122: */
123: function fetch(&$form)
124: {
125: }
126: }
127:
128: /**
129: * @public
130: * @brief [Abstract] The base class which implements XCube_PropertyInterface, for all properties.
131: */
132: class XCube_AbstractProperty extends XCube_PropertyInterface
133: {
134: /**
135: * @protected
136: * @brief string
137: */
138: var $mName = null;
139:
140: /**
141: * @protected
142: * @brief string
143: */
144: var $mValue = null;
145:
146: /**
147: * @public
148: * @brief Constructor.
149: * @param $name string - A name of this property.
150: */
151: function XCube_AbstractProperty($name)
152: {
153: parent::XCube_PropertyInterface($name);
154: $this->mName = $name;
155: }
156:
157: /**
158: * @public
159: * @brief Sets $value as raw value to this property. And the value is casted by the property's type'.
160: * @param $value mixed
161: */
162: function set($value)
163: {
164: $this->mValue = $value;
165: }
166:
167: /**
168: * @public
169: * @brief Gets the value of this property.
170: * @return mixed
171: */
172: function get($index = null)
173: {
174: return $this->mValue;
175: }
176:
177: /**
178: * @public
179: * @brief Gets a value indicating whether this object expresses Array.
180: * @return bool
181: *
182: * @remarks
183: * This class is a base class for none-array properties, so a sub-class of this
184: * does not override this method.
185: */
186: function isArray()
187: {
188: return false;
189: }
190:
191: /**
192: * @public
193: * @brief Gets a value indicating whether this object is null.
194: * @return bool
195: */
196: function isNull()
197: {
198: return (strlen(trim($this->mValue)) == 0);
199: }
200:
201: /**
202: * @public
203: * @brief Gets a value as integer.
204: * @return int
205: */
206: function toNumber()
207: {
208: return $this->mValue;
209: }
210:
211: /**
212: * @public
213: * @brief Gets a value as string.
214: * @return string
215: */
216: function toString()
217: {
218: return $this->mValue;
219: }
220:
221: /**
222: * @public
223: * @brief Gets a value as encoded HTML code.
224: * @return string - HTML
225: * @deprecated
226: */
227: function toHTML()
228: {
229: return htmlspecialchars($this->toString(), ENT_QUOTES);
230: }
231:
232: /**
233: * @public
234: * @brief Gets a value indicating whether this object has a fetch control.
235: * @return bool
236: */
237: function hasFetchControl()
238: {
239: return false;
240: }
241: }
242:
243: /**
244: * @public
245: * @brief [Abstract] Defines common array property class which implements XCube_PropertyInterface.
246: *
247: * This class is a kind of template-class --- XCube_GenericArrayProperty<T>.
248: * Developers should know about sub-classes of XCube_AbstractProperty.
249: */
250: class XCube_GenericArrayProperty extends XCube_PropertyInterface
251: {
252: /**
253: * @protected
254: * @brief string
255: */
256: var $mName = null;
257:
258: /**
259: * @protected
260: * @brief XCube_AbstractProperty[] - std::map<mixed_key, mixed_value>
261: */
262: var $mProperties = array();
263:
264: /**
265: * @protected
266: * @brief string - <T>
267: *
268: * If this class is XCube_GenericArrayProperty<T>, mPropertyClassName is <T>.
269: */
270: var $mPropertyClassName = null;
271:
272: /**
273: * @public
274: * @brief Constructor.
275: * @param $classname string - <T>
276: * @param $name string - A name of the property.
277: */
278: function XCube_GenericArrayProperty($classname, $name)
279: {
280: $this->mPropertyClassName = $classname;
281: $this->mName = $name;
282: }
283:
284: /**
285: * @public
286: * @brief Sets a value. And the value is casted by the property's type'.
287: *
288: * This member function has two signatures.
289: *
290: * \par set(something[] values);
291: * Fetches values from the array.
292: *
293: * \par set(mixed key, mixed value);
294: * Set values with index 'key'.
295: */
296: function set($arg1, $arg2 = null)
297: {
298: if (is_array($arg1) && $arg2 == null) {
299: $this->reset();
300: foreach ($arg1 as $t_key => $t_value) {
301: $this->_set($t_key, $t_value);
302: }
303: }
304: elseif($arg1===null && $arg2===null){ //ex) all checkbox options are off
305: $this->reset();
306: }
307: elseif ($arg1 !== null && $arg2 !== null) {
308: $this->_set($arg1, $arg2);
309: }
310: }
311:
312: /**
313: * @internal
314: * @todo Research this method.
315: */
316: function add($arg1, $arg2 = null)
317: {
318: if (is_array($arg1) && $arg2 == null) {
319: foreach ($arg1 as $t_key => $t_value) {
320: $this->_set($t_key, $t_value);
321: }
322: }
323: elseif ($arg1 !== null && $arg2 !== null) {
324: $this->_set($arg1, $arg2);
325: }
326: }
327:
328: /**
329: * @private
330: * @brief This member function helps set().
331: * @param string $index
332: * @param mixed $value
333: * @return void
334: */
335: function _set($index, $value)
336: {
337: if (!isset($this->mProperties[$index])) {
338: $this->mProperties[$index] = new $this->mPropertyClassName($this->mName);
339: }
340: $this->mProperties[$index]->set($value);
341: }
342:
343: /**
344: * @public
345: * @brief Gets values of this property.
346: * @param $index mixed - If $indes is null, gets array (std::map<mixed_key, mixed_value>).
347: * @return mixed
348: */
349: function get($index = null)
350: {
351: if ($index === null) {
352: $ret = array();
353:
354: foreach ($this->mProperties as $t_key => $t_value) {
355: $ret[$t_key] = $t_value->get();
356: }
357:
358: return $ret;
359: }
360:
361: return isset($this->mProperties[$index]) ? $this->mProperties[$index]->get() : null;
362: }
363:
364: /**
365: * @protected
366: * @brief Resets all properties of this.
367: */
368: function reset()
369: {
370: unset($this->mProperties);
371: $this->mProperties = array();
372: }
373:
374: /**
375: * @public
376: * @brief Gets a value indicating whether this object expresses Array.
377: * @return bool
378: *
379: * @remarks
380: * This class is a base class for array properties, so a sub-class of this
381: * does not override this method.
382: */
383: function isArray()
384: {
385: return true;
386: }
387:
388: /**
389: * @public
390: * @brief Gets a value indicating whether this object is null.
391: * @return bool
392: */
393: function isNull()
394: {
395: return (count($this->mProperties) == 0);
396: }
397:
398: /**
399: * @public
400: * @brief Gets a value as integer --- but, gets null always.
401: * @return int
402: */
403: function toNumber()
404: {
405: return null;
406: }
407:
408: /**
409: * @public
410: * @brief Gets a value as string --- but, gets 'Array' always.
411: * @return string
412: */
413: function toString()
414: {
415: return 'Array';
416: }
417:
418: /**
419: * @public
420: * @brief Gets a value as encoded HTML code --- but, gets 'Array' always.
421: * @return string - HTML
422: * @deprecated
423: */
424: function toHTML()
425: {
426: return htmlspecialchars($this->toString(), ENT_QUOTES);
427: }
428:
429: /**
430: * @public
431: * @brief Gets a value indicating whether this object has a fetch control.
432: * @return bool
433: */
434: function hasFetchControl()
435: {
436: return false;
437: }
438: }
439:
440: /**
441: * @internal
442: * @deprecated
443: */
444: class XCube_AbstractArrayProperty extends XCube_GenericArrayProperty
445: {
446: function XCube_AbstractArrayProperty($name)
447: {
448: parent::XCube_GenericArrayProperty($this->mPropertyClassName, $name);
449: }
450: }
451:
452: /**
453: * @public
454: * @brief Represents bool property.
455: */
456: class XCube_BoolProperty extends XCube_AbstractProperty
457: {
458: function set($value)
459: {
460: $this->mValue = (int)$value ? 1 : 0;
461: }
462: }
463:
464: /**
465: * @public
466: * @brief Represents bool[] property. XCube_GenericArrayProperty<XCube_BoolProperty>.
467: * @see XCube_BoolProperty
468: */
469: class XCube_BoolArrayProperty extends XCube_GenericArrayProperty
470: {
471: function XCube_BoolArrayProperty($name)
472: {
473: parent::XCube_GenericArrayProperty("XCube_BoolProperty", $name);
474: }
475: }
476:
477: /**
478: * @public
479: * @brief Represents int property.
480: */
481: class XCube_IntProperty extends XCube_AbstractProperty
482: {
483: function set($value)
484: {
485: $this->mValue = trim($value)!==''?(int)$value:null;
486: }
487: }
488:
489: /**
490: * @public
491: * @brief Represents int[] property. XCube_GenericArrayProperty<XCube_IntProperty>.
492: * @see XCube_IntProperty
493: */
494: class XCube_IntArrayProperty extends XCube_GenericArrayProperty
495: {
496: function XCube_IntArrayProperty($name)
497: {
498: parent::XCube_GenericArrayProperty("XCube_IntProperty", $name);
499: }
500: }
501:
502: /**
503: * @public
504: * @brief Represents float property.
505: */
506: class XCube_FloatProperty extends XCube_AbstractProperty
507: {
508: function set($value)
509: {
510: $this->mValue = trim($value)!== ''?(float)$value:null;
511: }
512: }
513:
514: /**
515: * @public
516: * @brief Represents float[] property. XCube_GenericArrayProperty<XCube_FloatProperty>.
517: * @see XCube_FloatProperty
518: */
519: class XCube_FloatArrayProperty extends XCube_GenericArrayProperty
520: {
521: function XCube_FloatArrayProperty($name)
522: {
523: parent::XCube_GenericArrayProperty("XCube_FloatProperty", $name);
524: }
525: }
526:
527: /**
528: * @public
529: * @brief Represents string property.
530: *
531: * This class shows the property of string. Check whether a request includes control
532: * code. If it does, stop own process.
533: */
534: class XCube_StringProperty extends XCube_AbstractProperty
535: {
536: function set($value)
537: {
538: // if (preg_match_all("/[\\x00-\\x1f]/", $value, $matches, PREG_PATTERN_ORDER)) {
539: // die("Get control code :" . ord($matches[0][0]));
540: // }
541:
542: $this->mValue = preg_replace("/[\\x00-\\x1f]/", '' , $value);
543: }
544:
545: function toNumber()
546: {
547: return (int)$this->mValue;
548: }
549: }
550:
551: /**
552: * @public
553: * @brief Represents string[] property. XCube_GenericArrayProperty<XCube_StringProperty>.
554: * @see XCube_StringProperty
555: */
556: class XCube_StringArrayProperty extends XCube_GenericArrayProperty
557: {
558: function XCube_StringArrayProperty($name)
559: {
560: parent::XCube_GenericArrayProperty("XCube_StringProperty", $name);
561: }
562: }
563:
564: /**
565: * @public
566: * @brief Represents string property which allows CR and LF.
567: *
568: * This class shows the property of text. Check whether a request includes control
569: * code. If it does, stop own process.
570: */
571: class XCube_TextProperty extends XCube_AbstractProperty
572: {
573: function set($value)
574: {
575: $matches = array();
576:
577: // if (preg_match_all("/[\\x00-\\x08]|[\\x0b-\\x0c]|[\\x0e-\\x1f]/", $value, $matches,PREG_PATTERN_ORDER)) {
578: // die("Get control code :" . ord($matches[0][0]));
579: // }
580:
581: $this->mValue = preg_replace("/[\\x00-\\x08]|[\\x0b-\\x0c]|[\\x0e-\\x1f]/", '', $value);
582: }
583:
584: function toNumber()
585: {
586: return (int)$this->mValue;
587: }
588: }
589:
590: /**
591: * @public
592: * @brief Represents string[] property which allows CR and LF. XCube_GenericArrayProperty<XCube_TextProperty>.
593: * @see XCube_TextProperty
594: */
595: class XCube_TextArrayProperty extends XCube_GenericArrayProperty
596: {
597: function XCube_TextArrayProperty($name)
598: {
599: parent::XCube_GenericArrayProperty("XCube_TextProperty", $name);
600: }
601: }
602:
603: /**
604: * @public
605: * @brief Represents the special property which handles uploaded file.
606: * @see XCube_FormFile
607: */
608: class XCube_FileProperty extends XCube_AbstractProperty
609: {
610: /**
611: * @protected
612: * @brief mixed - ID for XCube_FileArrayProperty.
613: *
614: * friend XCube_FileArrayProperty;
615: */
616: var $mIndex = null;
617:
618: function XCube_FileProperty($name)
619: {
620: parent::XCube_AbstractProperty($name);
621: $this->mValue = new XCube_FormFile($name);
622: }
623:
624: function hasFetchControl()
625: {
626: return true;
627: }
628:
629: function fetch(&$form)
630: {
631: if (!is_object($this->mValue)) {
632: return false;
633: }
634:
635: if ($this->mIndex !== null) {
636: $this->mValue->mKey = $this->mIndex;
637: }
638:
639: $this->mValue->fetch();
640:
641: if (!$this->mValue->hasUploadFile()) {
642: $this->mValue = null;
643: }
644: }
645:
646: function isNull()
647: {
648: if (!is_object($this->mValue)) {
649: return true;
650: }
651:
652: return !$this->mValue->hasUploadFile();
653: }
654:
655: function toString()
656: {
657: return null;
658: }
659:
660: function toNumber()
661: {
662: return null;
663: }
664: }
665:
666: /**
667: * @public
668: * @brief Represents the special property[] which handles uploaded file. XCube_GenericArrayProperty<XCube_FileProperty>.
669: * @see XCube_FileProperty
670: */
671: class XCube_FileArrayProperty extends XCube_GenericArrayProperty
672: {
673: function XCube_FileArrayProperty($name)
674: {
675: parent::XCube_GenericArrayProperty("XCube_FileProperty", $name);
676: }
677:
678: function hasFetchControl()
679: {
680: return true;
681: }
682:
683: function fetch(&$form)
684: {
685: unset($this->mProperties);
686: $this->mProperties = array();
687: if (isset($_FILES[$this->mName]) && is_array($_FILES[$this->mName]['name'])) {
688: foreach ($_FILES[$this->mName]['name'] as $_key => $_val) {
689: $this->mProperties[$_key] = new $this->mPropertyClassName($this->mName);
690: $this->mProperties[$_key]->mIndex = $_key;
691: $this->mProperties[$_key]->fetch($form);
692: }
693: }
694: }
695: }
696:
697: /**
698: * @public
699: * @brief This is extended XCube_FileProperty and limits uploaded files by image files.
700: * @see XCube_FormImageFile
701: */
702: class XCube_ImageFileProperty extends XCube_FileProperty
703: {
704: function XCube_ImageFileProperty($name)
705: {
706: parent::XCube_AbstractProperty($name);
707: $this->mValue = new XCube_FormImageFile($name);
708: }
709: }
710:
711: /**
712: * @public
713: * @brief XCube_GenericArrayProperty<XCube_ImageFileProperty>.
714: * @see XCube_ImageFileProperty
715: */
716: class XCube_ImageFileArrayProperty extends XCube_FileArrayProperty
717: {
718: function XCube_ImageFileArrayProperty($name)
719: {
720: parent::XCube_GenericArrayProperty("XCube_ImageFileProperty", $name);
721: }
722: }
723:
724: ?>