1 <?php rcs_id('$Id: Request.php,v 1.2 2001-09-19 02:58:00 dairiki Exp $');
3 // FIXME: write log entry.
9 $this->_fix_magic_quotes_gpc();
10 $this->_fix_multipart_form_data();
12 switch($this->get('REQUEST_METHOD')) {
15 $this->args = &$GLOBALS['HTTP_GET_VARS'];
18 $this->args = &$GLOBALS['HTTP_POST_VARS'];
21 $this->args = array();
25 $this->session = new Request_SessionVars;
26 $this->cookies = new Request_CookieVars;
28 $this->_log_entry = new Request_AccessLogEntry($this);
34 $vars = &$GLOBALS['HTTP_SERVER_VARS'];
36 if (isset($vars[$key]))
41 $addr = $vars['REMOTE_ADDR'];
42 if (defined('ENABLE_REVERSE_DNS') && ENABLE_REVERSE_DNS)
43 return $vars[$key] = gethostbyaddr($addr);
51 function getArg($key) {
52 if (isset($this->args[$key]))
53 return $this->args[$key];
57 function setArg($key, $val) {
58 $this->args[$key] = $val;
62 function redirect($url) {
63 header("Location: $url");
64 $this->_log_entry->setStatus(302);
67 function compress_output() {
68 if (function_exists('ob_gzhandler')) {
69 ob_start('ob_gzhandler');
70 $this->_is_compressing_output = true;
75 if (!empty($this->_is_compressing_output))
80 function getSessionVar($key) {
81 return $this->session->get($key);
83 function setSessionVar($key, $val) {
84 return $this->session->set($key, $val);
86 function deleteSessionVar($key) {
87 return $this->session->delete($key);
90 function getCookieVar($key) {
91 return $this->cookies->get($key);
93 function setCookieVar($key, $val, $lifetime_in_days = false) {
94 return $this->cookies->set($key, $val, $lifetime_in_days);
96 function deleteCookieVar($key) {
97 return $this->cookies->delete($key);
100 function getUploadedFile($key) {
101 return Request_UploadedFile::getUploadedFile($key);
105 function _fix_magic_quotes_gpc() {
106 $needs_fix = array('HTTP_POST_VARS',
113 if (get_magic_quotes_gpc()) {
114 foreach ($needs_fix as $vars)
115 $this->_stripslashes($GLOBALS[$vars]);
120 function _stripslashes(&$var) {
121 if (is_array($var)) {
122 foreach ($var as $key => $val)
123 $this->_stripslashes($var[$key]);
125 elseif (is_string($var))
126 $var = stripslashes($var);
129 function _fix_multipart_form_data () {
130 if (preg_match('|^multipart/form-data|', $this->get('CONTENT_TYPE')))
131 $this->_strip_leading_nl($GLOBALS['HTTP_POST_VARS']);
134 function _strip_leading_nl(&$var) {
135 if (is_array($var)) {
136 foreach ($var as $key => $val)
137 $this->_strip_leading_nl($var[$key]);
139 elseif (is_string($var))
140 $var = preg_replace('|^\r?\n?|', '', $var);
144 class Request_SessionVars {
145 function Request_SessionVars() {
150 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
151 if (isset($vars[$key]))
156 function set($key, $val) {
157 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
158 if (ini_get('register_globals')) {
159 // This is funky but necessary, at least in some PHP's
160 $GLOBALS[$key] = $val;
163 session_register($key);
166 function delete($key) {
167 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
168 if (ini_get('register_globals'))
169 unset($GLOBALS[$key]);
171 session_unregister($key);
175 class Request_CookieVars {
178 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
179 if (isset($vars[$key])) {
180 @$val = unserialize($vars[$key]);
187 function set($key, $val, $persist_days = false) {
188 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
190 if (is_numeric($persist_days)) {
191 $expires = time() + (24 * 3600) * $persist_days;
197 $packedval = serialize($val);
198 $vars[$key] = $packedval;
199 setcookie($key, $packedval, $expires, '/');
202 function delete($key) {
203 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
209 class Request_UploadedFile {
210 function getUploadedFile($postname) {
211 global $HTTP_POST_FILES;
213 if (!isset($HTTP_POST_FILES[$postname]))
216 $fileinfo = &$HTTP_POST_FILES[$postname];
217 if (!is_uploaded_file($fileinfo['tmp_name']))
218 return false; // possible malicious attack.
220 return new Request_UploadedFile($fileinfo);
223 function Request_UploadedFile($fileinfo) {
224 $this->_info = $fileinfo;
228 return $this->_info['size'];
232 return $this->_info['name'];
236 return $this->_info['type'];
240 if ( ($fd = fopen($this->_info['tmp_name'], "rb")) ) {
241 // Dump http headers.
242 while ( ($header = fgets($fd, 4096)) )
243 if (trim($header) == '')
249 function getContents() {
251 $data = fread($fd, $this->getSize());
257 class Request_AccessLogEntry
259 function AccessLogEntry ($request) {
260 $this->host = $req->get('REMOTE_HOST');
261 $this->ident = $req->get('REMOTE_IDENT');
265 $this->time = time();
266 $this->request = join(' ', array($req->get('REQUEST_METHOD'),
267 $req->get('REQUEST_URI'),
268 $req->get('SERVER_PROTOCOL')));
271 $this->referer = (string) $req->get('HTTP_REFERER');
272 $this->user_agent = (string) $req->get('HTTP_USER_AGENT');
276 // Returns zone offset, like "-0800" for PST.
278 function _zone_offset () {
279 $offset = date("Z", $this->time);
285 $offhours = floor($offset / 3600);
286 $offmins = $offset / 60 - $offhours * 60;
287 return sprintf("%s%02d%02d", $negoffset, $offhours, $offmins);
290 // Format time into NCSA format.
291 function _ncsa_time($time = false) {
295 return date("d/M/Y:H:i:s", $time) .
296 " " . $this->_zone_offset();
299 function write($logfile) {
300 $entry = sprintf('%s %s %s [%s] "%s" %d %d "%s" "%s"',
301 $this->host, $this->ident, $this->user,
302 $this->_ncsa_time($this->time),
303 $this->request, $this->status, $this->size,
304 $this->referer, $this->user_agent);
306 //Error log doesn't provide locking.
307 //error_log("$entry\n", 3, $logfile);
310 if (($fp = fopen($logfile, "a")))
313 fputs($fp, "$entry\n");
323 // c-hanging-comment-ender-p: nil
324 // indent-tabs-mode: nil