1 <?php rcs_id('$Id: Request.php,v 1.9 2002-01-23 05:10:22 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,
35 $vars = &$GLOBALS['HTTP_SERVER_VARS'];
37 if (isset($vars[$key]))
42 $addr = $vars['REMOTE_ADDR'];
43 if (defined('ENABLE_REVERSE_DNS') && ENABLE_REVERSE_DNS)
44 return $vars[$key] = gethostbyaddr($addr);
52 function getArg($key) {
53 if (isset($this->args[$key]))
54 return $this->args[$key];
62 function setArg($key, $val) {
64 unset($this->args[$key]);
66 $this->args[$key] = $val;
70 function getURLtoSelf($args = false) {
71 $get_args = $this->args;
73 $get_args = array_merge($get_args, $args);
75 unset ($get_args['pagename']);
76 if ($get_args['action'] == 'browse')
77 unset($get_args['action']);
79 return WikiURL($this->getArg('pagename'), $get_args);
84 return $this->get("REQUEST_METHOD") == "POST";
87 function redirect($url) {
88 header("Location: $url");
89 if (isset($this->_log_entry))
90 $this->_log_entry->setStatus(302);
93 function setStatus($status) {
94 if (preg_match('|^HTTP/.*?\s(\d+)|i', $status, $m)) {
99 $status = (integer) $status;
100 $reasons = array('200' => 'OK',
102 '400' => 'Bad Request',
103 '401' => 'Unauthorized',
104 '403' => 'Forbidden',
105 '404' => 'Not Found');
106 header(sprintf("HTTP/1.0 %d %s", $status, $reason[$status]));
109 if (isset($this->_log_entry))
110 $this->_log_entry->setStatus($status);
113 function compress_output() {
114 if (function_exists('ob_gzhandler')) {
115 ob_start('ob_gzhandler');
116 $this->_is_compressing_output = true;
121 if (!empty($this->_is_compressing_output))
126 function getSessionVar($key) {
127 return $this->session->get($key);
129 function setSessionVar($key, $val) {
130 return $this->session->set($key, $val);
132 function deleteSessionVar($key) {
133 return $this->session->delete($key);
136 function getCookieVar($key) {
137 return $this->cookies->get($key);
139 function setCookieVar($key, $val, $lifetime_in_days = false) {
140 return $this->cookies->set($key, $val, $lifetime_in_days);
142 function deleteCookieVar($key) {
143 return $this->cookies->delete($key);
146 function getUploadedFile($key) {
147 return Request_UploadedFile::getUploadedFile($key);
151 function _fix_magic_quotes_gpc() {
152 $needs_fix = array('HTTP_POST_VARS',
159 if (get_magic_quotes_gpc()) {
160 foreach ($needs_fix as $vars)
161 $this->_stripslashes($GLOBALS[$vars]);
166 function _stripslashes(&$var) {
167 if (is_array($var)) {
168 foreach ($var as $key => $val)
169 $this->_stripslashes($var[$key]);
171 elseif (is_string($var))
172 $var = stripslashes($var);
175 function _fix_multipart_form_data () {
176 if (preg_match('|^multipart/form-data|', $this->get('CONTENT_TYPE')))
177 $this->_strip_leading_nl($GLOBALS['HTTP_POST_VARS']);
180 function _strip_leading_nl(&$var) {
181 if (is_array($var)) {
182 foreach ($var as $key => $val)
183 $this->_strip_leading_nl($var[$key]);
185 elseif (is_string($var))
186 $var = preg_replace('|^\r?\n?|', '', $var);
190 class Request_SessionVars {
191 function Request_SessionVars() {
196 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
197 if (isset($vars[$key]))
202 function set($key, $val) {
203 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
204 if (ini_get('register_globals')) {
205 // This is funky but necessary, at least in some PHP's
206 $GLOBALS[$key] = $val;
209 session_register($key);
212 function delete($key) {
213 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
214 if (ini_get('register_globals'))
215 unset($GLOBALS[$key]);
217 session_unregister($key);
221 class Request_CookieVars {
224 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
225 if (isset($vars[$key])) {
226 @$val = unserialize($vars[$key]);
233 function set($key, $val, $persist_days = false) {
234 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
236 if (is_numeric($persist_days)) {
237 $expires = time() + (24 * 3600) * $persist_days;
243 $packedval = serialize($val);
244 $vars[$key] = $packedval;
245 setcookie($key, $packedval, $expires, '/');
248 function delete($key) {
249 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
255 class Request_UploadedFile {
256 function getUploadedFile($postname) {
257 global $HTTP_POST_FILES;
259 if (!isset($HTTP_POST_FILES[$postname]))
262 $fileinfo = &$HTTP_POST_FILES[$postname];
263 if (!is_uploaded_file($fileinfo['tmp_name']))
264 return false; // possible malicious attack.
266 return new Request_UploadedFile($fileinfo);
269 function Request_UploadedFile($fileinfo) {
270 $this->_info = $fileinfo;
274 return $this->_info['size'];
278 return $this->_info['name'];
282 return $this->_info['type'];
286 if ( ($fd = fopen($this->_info['tmp_name'], "rb")) ) {
287 if ($this->getSize() < filesize($this->_info['tmp_name'])) {
288 // FIXME: Some PHP's (or is it some browsers?) put
289 // HTTP/MIME headers in the file body, some don't.
291 // At least, I think that's the case. I know I used
292 // to need this code, now I don't.
294 // This code is more-or-less untested currently.
296 // Dump HTTP headers.
297 while ( ($header = fgets($fd, 4096)) ) {
298 if (trim($header) == '') {
301 else if (!preg_match('/^content-(length|type):/i', $header)) {
311 function getContents() {
313 $data = fread($fd, $this->getSize());
320 * Create NCSA "combined" log entry for current request.
322 class Request_AccessLogEntry
327 * The log entry will be automatically appended to the log file
328 * when the current request terminates.
330 * If you want to modify a Request_AccessLogEntry before it gets
331 * written (e.g. via the setStatus and setSize methods) you should
332 * use an '&' on the constructor, so that you're working with the
333 * original (rather than a copy) object.
336 * $log_entry = & new Request_AccessLogEntry($req, "/tmp/wiki_access_log");
337 * $log_entry->setStatus(401);
341 * @param $request object Request object for current request.
342 * @param $logfile string Log file name.
344 function Request_AccessLogEntry (&$request, $logfile) {
345 $this->logfile = $logfile;
347 $this->host = $request->get('REMOTE_HOST');
348 $this->ident = $request->get('REMOTE_IDENT');
351 $this->user = '-'; // FIXME: get logged-in user name
352 $this->time = time();
353 $this->request = join(' ', array($request->get('REQUEST_METHOD'),
354 $request->get('REQUEST_URI'),
355 $request->get('SERVER_PROTOCOL')));
358 $this->referer = (string) $request->get('HTTP_REFERER');
359 $this->user_agent = (string) $request->get('HTTP_USER_AGENT');
361 global $Request_AccessLogEntry_entries;
362 if (!isset($Request_AccessLogEntry_entries)) {
363 register_shutdown_function("Request_AccessLogEntry_shutdown_function");
365 $Request_AccessLogEntry_entries[] = &$this;
369 * Set result status code.
371 * @param $status integer HTTP status code.
373 function setStatus ($status) {
374 $this->status = $status;
380 * @param $size integer
382 function setSize ($size) {
387 * Get time zone offset.
389 * This is a static member function.
391 * @param $time integer Unix timestamp (defaults to current time).
392 * @return string Zone offset, e.g. "-0800" for PST.
394 function _zone_offset ($time = false) {
397 $offset = date("Z", $time);
402 $offhours = floor($offset / 3600);
403 $offmins = $offset / 60 - $offhours * 60;
404 return sprintf("%s%02d%02d", $negoffset, $offhours, $offmins);
408 * Format time in NCSA format.
410 * This is a static member function.
412 * @param $time integer Unix timestamp (defaults to current time).
413 * @return string Formatted date & time.
415 function _ncsa_time($time = false) {
419 return date("d/M/Y:H:i:s", $time) .
420 " " . $this->_zone_offset();
424 * Write entry to log file.
427 $entry = sprintf('%s %s %s [%s] "%s" %d %d "%s" "%s"',
428 $this->host, $this->ident, $this->user,
429 $this->_ncsa_time($this->time),
430 $this->request, $this->status, $this->size,
431 $this->referer, $this->user_agent);
433 //Error log doesn't provide locking.
434 //error_log("$entry\n", 3, $this->logfile);
437 if (($fp = fopen($this->logfile, "a"))) {
439 fputs($fp, "$entry\n");
449 * @see Request_AccessLogEntry
451 function Request_AccessLogEntry_shutdown_function ()
453 global $Request_AccessLogEntry_entries;
455 foreach ($Request_AccessLogEntry_entries as $entry) {
458 unset($Request_AccessLogEntry_entries);
465 // c-hanging-comment-ender-p: nil
466 // indent-tabs-mode: nil