1 <?php rcs_id('$Id: Request.php,v 1.5 2001-12-11 18:41:03 dairiki Exp $');
3 // FIXME: write log entry.
8 $this->_fix_magic_quotes_gpc();
9 $this->_fix_multipart_form_data();
11 switch($this->get('REQUEST_METHOD')) {
14 $this->args = &$GLOBALS['HTTP_GET_VARS'];
17 $this->args = &$GLOBALS['HTTP_POST_VARS'];
20 $this->args = array();
24 $this->session = new Request_SessionVars;
25 $this->cookies = new Request_CookieVars;
28 $this->_log_entry = & new Request_AccessLogEntry($this, ACCESS_LOG);
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 if (isset($this->_log_entry))
65 $this->_log_entry->setStatus(302);
68 function setStatus($status) {
69 if (preg_match('|^HTTP/.*?\s(\d+)|i', $status, $m)) {
74 $status = (integer) $status;
75 $reasons = array('200' => 'OK',
77 '400' => 'Bad Request',
78 '401' => 'Unauthorized',
80 '404' => 'Not Found');
81 header(sprintf("HTTP/1.0 %d %s", $status, $reason[$status]));
84 if (isset($this->_log_entry))
85 $this->_log_entry->setStatus($status);
88 function compress_output() {
89 if (function_exists('ob_gzhandler')) {
90 ob_start('ob_gzhandler');
91 $this->_is_compressing_output = true;
96 if (!empty($this->_is_compressing_output))
101 function getSessionVar($key) {
102 return $this->session->get($key);
104 function setSessionVar($key, $val) {
105 return $this->session->set($key, $val);
107 function deleteSessionVar($key) {
108 return $this->session->delete($key);
111 function getCookieVar($key) {
112 return $this->cookies->get($key);
114 function setCookieVar($key, $val, $lifetime_in_days = false) {
115 return $this->cookies->set($key, $val, $lifetime_in_days);
117 function deleteCookieVar($key) {
118 return $this->cookies->delete($key);
121 function getUploadedFile($key) {
122 return Request_UploadedFile::getUploadedFile($key);
126 function _fix_magic_quotes_gpc() {
127 $needs_fix = array('HTTP_POST_VARS',
134 if (get_magic_quotes_gpc()) {
135 foreach ($needs_fix as $vars)
136 $this->_stripslashes($GLOBALS[$vars]);
141 function _stripslashes(&$var) {
142 if (is_array($var)) {
143 foreach ($var as $key => $val)
144 $this->_stripslashes($var[$key]);
146 elseif (is_string($var))
147 $var = stripslashes($var);
150 function _fix_multipart_form_data () {
151 if (preg_match('|^multipart/form-data|', $this->get('CONTENT_TYPE')))
152 $this->_strip_leading_nl($GLOBALS['HTTP_POST_VARS']);
155 function _strip_leading_nl(&$var) {
156 if (is_array($var)) {
157 foreach ($var as $key => $val)
158 $this->_strip_leading_nl($var[$key]);
160 elseif (is_string($var))
161 $var = preg_replace('|^\r?\n?|', '', $var);
165 class Request_SessionVars {
166 function Request_SessionVars() {
171 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
172 if (isset($vars[$key]))
177 function set($key, $val) {
178 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
179 if (ini_get('register_globals')) {
180 // This is funky but necessary, at least in some PHP's
181 $GLOBALS[$key] = $val;
184 session_register($key);
187 function delete($key) {
188 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
189 if (ini_get('register_globals'))
190 unset($GLOBALS[$key]);
192 session_unregister($key);
196 class Request_CookieVars {
199 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
200 if (isset($vars[$key])) {
201 @$val = unserialize($vars[$key]);
208 function set($key, $val, $persist_days = false) {
209 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
211 if (is_numeric($persist_days)) {
212 $expires = time() + (24 * 3600) * $persist_days;
218 $packedval = serialize($val);
219 $vars[$key] = $packedval;
220 setcookie($key, $packedval, $expires, '/');
223 function delete($key) {
224 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
230 class Request_UploadedFile {
231 function getUploadedFile($postname) {
232 global $HTTP_POST_FILES;
234 if (!isset($HTTP_POST_FILES[$postname]))
237 $fileinfo = &$HTTP_POST_FILES[$postname];
238 if (!is_uploaded_file($fileinfo['tmp_name']))
239 return false; // possible malicious attack.
241 return new Request_UploadedFile($fileinfo);
244 function Request_UploadedFile($fileinfo) {
245 $this->_info = $fileinfo;
249 return $this->_info['size'];
253 return $this->_info['name'];
257 return $this->_info['type'];
261 if ( ($fd = fopen($this->_info['tmp_name'], "rb")) ) {
262 if ($this->getSize() < filesize($this->_info['tmp_name'])) {
263 // FIXME: Some PHP's (or is it some browsers?) put
264 // HTTP/MIME headers in the file body, some don't.
266 // At least, I think that's the case. I know I used
267 // to need this code, now I don't.
269 // This code is more-or-less untested currently.
271 // Dump HTTP headers.
272 while ( ($header = fgets($fd, 4096)) ) {
273 if (trim($header) == '') {
276 else if (!preg_match('/^content-(length|type):/i', $header)) {
286 function getContents() {
288 $data = fread($fd, $this->getSize());
295 * Create NCSA "combined" log entry for current request.
297 class Request_AccessLogEntry
302 * The log entry will be automatically appended to the log file
303 * when the current request terminates.
305 * If you want to modify a Request_AccessLogEntry before it gets
306 * written (e.g. via the setStatus and setSize methods) you should
307 * use an '&' on the constructor, so that you're working with the
308 * original (rather than a copy) object.
311 * $log_entry = & new Request_AccessLogEntry($req, "/tmp/wiki_access_log");
312 * $log_entry->setStatus(401);
316 * @param $request object Request object for current request.
317 * @param $logfile string Log file name.
319 function Request_AccessLogEntry ($request, $logfile) {
320 $this->logfile = $logfile;
322 $this->host = $request->get('REMOTE_HOST');
323 $this->ident = $request->get('REMOTE_IDENT');
326 $this->user = '-'; // FIXME: get logged-in user name
327 $this->time = time();
328 $this->request = join(' ', array($request->get('REQUEST_METHOD'),
329 $request->get('REQUEST_URI'),
330 $request->get('SERVER_PROTOCOL')));
333 $this->referer = (string) $request->get('HTTP_REFERER');
334 $this->user_agent = (string) $request->get('HTTP_USER_AGENT');
336 global $Request_AccessLogEntry_entries;
337 if (!isset($Request_AccessLogEntry_entries)) {
338 register_shutdown_function("Request_AccessLogEntry_shutdown_function");
340 $Request_AccessLogEntry_entries[] = &$this;
344 * Set result status code.
346 * @param $status integer HTTP status code.
348 function setStatus ($status) {
349 $this->status = $status;
355 * @param $size integer
357 function setSize ($size) {
362 * Get time zone offset.
364 * This is a static member function.
366 * @param $time integer Unix timestamp (defaults to current time).
367 * @return string Zone offset, e.g. "-0800" for PST.
369 function _zone_offset ($time = false) {
372 $offset = date("Z", $time);
377 $offhours = floor($offset / 3600);
378 $offmins = $offset / 60 - $offhours * 60;
379 return sprintf("%s%02d%02d", $negoffset, $offhours, $offmins);
383 * Format time in NCSA format.
385 * This is a static member function.
387 * @param $time integer Unix timestamp (defaults to current time).
388 * @return string Formatted date & time.
390 function _ncsa_time($time = false) {
394 return date("d/M/Y:H:i:s", $time) .
395 " " . $this->_zone_offset();
399 * Write entry to log file.
402 $entry = sprintf('%s %s %s [%s] "%s" %d %d "%s" "%s"',
403 $this->host, $this->ident, $this->user,
404 $this->_ncsa_time($this->time),
405 $this->request, $this->status, $this->size,
406 $this->referer, $this->user_agent);
408 //Error log doesn't provide locking.
409 //error_log("$entry\n", 3, $this->logfile);
412 if (($fp = fopen($this->logfile, "a"))) {
414 fputs($fp, "$entry\n");
424 * @see Request_AccessLogEntry
426 function Request_AccessLogEntry_shutdown_function ()
428 global $Request_AccessLogEntry_entries;
430 foreach ($Request_AccessLogEntry_entries as $entry) {
433 unset($Request_AccessLogEntry_entries);
440 // c-hanging-comment-ender-p: nil
441 // indent-tabs-mode: nil