1: <?php
2: /**
3: *
4: * @package XCube
5: * @version $Id: XCube_HttpContext.class.php,v 1.4 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: define("XCUBE_CONTEXT_TYPE_DEFAULT", "web_browser");
12: define("XCUBE_CONTEXT_TYPE_WEB_SERVICE", "web_service");
13:
14: /**
15: * Encapsulates major HTTP specific information about a HTTP request.
16: */
17: class XCube_HttpContext
18: {
19: /**
20: * Hashmap that can be used to organize and share data. Use setAttribute()
21: * and get Attribute() to access this member property. But, direct access
22: * is allowed, because PHP4 is unpossible to handle reference well.
23: *
24: * @var Array
25: * @access protected
26: */
27: var $mAttributes = array();
28:
29: /**
30: * The object which enables to read the request values.
31: *
32: * @access XCube_AbstractRequest
33: */
34: var $mRequest = null;
35:
36: /**
37: * @var XCube_Principal
38: */
39: var $mUser = null;
40:
41: /**
42: * String which expresses the type of the current request.
43: * @var string
44: */
45: var $mType = XCUBE_CONTEXT_TYPE_DEFAULT;
46:
47: /**
48: * The theme is one in one time of request.
49: * A decided theme is registered with this property
50: *
51: * @access private
52: */
53: var $mThemeName = null;
54:
55: function XCube_HttpContext()
56: {
57: }
58:
59: /**
60: * Sets $value with $key to attributes. Use direct access to $mAttributes
61: * if references are must, because PHP4 can't handle reference in the
62: * signature of this member function.
63: *
64: * @param string $key
65: * @param mixed $value
66: */
67: function setAttribute($key, $value)
68: {
69: $this->mAttributes[$key] = $value;
70: }
71:
72: /**
73: * Gets a value indicating whether the value specified by $key exists.
74: *
75: * @param string $key
76: * @return mixed
77: */
78: function hasAttribute($key)
79: {
80: return isset($this->mAttributes[$key]);
81: }
82:
83: /**
84: * Gets a value of attributes with $key. If the value specified by $key
85: * doesn't exist in attributes, gets null.
86: *
87: * @param string $key
88: * @return mixed
89: */
90: function getAttribute($key)
91: {
92: return isset($this->mAttributes[$key]) ? $this->mAttributes[$key] : null;
93: }
94:
95: /**
96: * Sets the object which has a interface of XCube_AbstractRequest.
97: *
98: * @param XCube_AbstractRequest $request
99: */
100: function setRequest(&$request)
101: {
102: $this->mRequest =& $request;
103: }
104:
105: /**
106: * Gets the object which has a interface of XCube_AbstractRequest.
107: *
108: * @return XCube_AbstractRequest
109: */
110: function &getRequest()
111: {
112: return $this->mRequest;
113: }
114:
115: /**
116: * Sets the object which has a interface of XCube_Principal.
117: *
118: * @param XCube_AbstractPrincipal $principal
119: */
120: function setUser(&$principal)
121: {
122: $this->mUser =& $principal;
123: }
124:
125: /**
126: * Gets the object which has a interface of XCube_Principal.
127: *
128: * @return XCube_AbstractPrincipal
129: */
130: function &getUser()
131: {
132: return $this->mUser;
133: }
134:
135: /**
136: * Set the theme name.
137: *
138: * @param $theme string
139: * @deprecated
140: */
141: function setThemeName($theme)
142: {
143: $this->mThemeName = $theme;
144: }
145:
146: /**
147: * Return the theme name.
148: *
149: * @return string
150: * @deprecated
151: */
152: function getThemeName()
153: {
154: return $this->mThemeName;
155: }
156: }
157:
158: /**
159: * This is an interface for request classes.
160: */
161: class XCube_AbstractRequest
162: {
163: /**
164: * Gets a value of the current request.
165: *
166: * @param $key
167: * @return mixed
168: */
169: function getRequest($key)
170: {
171: return null;
172: }
173: }
174:
175: /**
176: * Enables a program to read the HTTP values through XCubeAbstractRequest
177: * interface.
178: */
179: class XCube_HttpRequest extends XCube_AbstractRequest
180: {
181: /**
182: * Gets a value of the current HTTP request. The return value doesn't
183: * include quotes which are appended by magic_quote_gpc, even if it's
184: * active.
185: *
186: * @param string $key
187: * @return mixed
188: */
189: function getRequest($key)
190: {
191: if (!isset($_GET[$key]) && !isset($_POST[$key])) {
192: return null;
193: }
194:
195: $value = isset($_GET[$key]) ? $_GET[$key] : $_POST[$key];
196:
197: if (!get_magic_quotes_gpc()) {
198: return $value;
199: }
200:
201: if (is_array($value)) {
202: return $this->_getArrayRequest($value);
203: }
204:
205: return stripslashes($value);
206: }
207:
208: /**
209: * Supports getRequest().
210: *
211: * @private
212: * @param Array $arr
213: * @return Array
214: */
215: function _getArrayRequest($arr)
216: {
217: foreach (array_keys($arr) as $t_key) {
218: if (is_array($arr[$t_key])) {
219: $arr[$t_key] = $this->_getArrayRequest($arr[$t_key]);
220: }
221: else {
222: $arr[$t_key] = stripslashes($arr[$t_key]);
223: }
224: }
225:
226: return $arr;
227: }
228: }
229:
230: /**
231: * A kind of request objects. This class is free to register values.
232: */
233: class XCube_GenericRequest extends XCube_AbstractRequest
234: {
235: /**
236: * Hash map which stores registered values.
237: * @var Array
238: */
239: var $mAttributes = array();
240:
241: function XCube_GenericRequest($arr = null)
242: {
243: if (is_array($arr)) {
244: $this->mAttributes = $arr;
245: }
246: }
247:
248: function getRequest($key)
249: {
250: if (!isset($this->mAttributes[$key])) {
251: return null;
252: }
253:
254: return $this->mAttributes[$key];
255: }
256: }
257:
258: ?>