1 <?php rcs_id('$Id: Request.php,v 1.10 2002-01-28 20:02:14 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 $pagename = $get_args['pagename'];
76 unset ($get_args['pagename']);
77 if ($get_args['action'] == 'browse')
78 unset($get_args['action']);
80 return WikiURL($pagename, $get_args);
85 return $this->get("REQUEST_METHOD") == "POST";
88 function redirect($url) {
89 header("Location: $url");
90 if (isset($this->_log_entry))
91 $this->_log_entry->setStatus(302);
94 function setStatus($status) {
95 if (preg_match('|^HTTP/.*?\s(\d+)|i', $status, $m)) {
100 $status = (integer) $status;
101 $reasons = array('200' => 'OK',
103 '400' => 'Bad Request',
104 '401' => 'Unauthorized',
105 '403' => 'Forbidden',
106 '404' => 'Not Found');
107 header(sprintf("HTTP/1.0 %d %s", $status, $reason[$status]));
110 if (isset($this->_log_entry))
111 $this->_log_entry->setStatus($status);
114 function compress_output() {
115 if (function_exists('ob_gzhandler')) {
116 ob_start('ob_gzhandler');
117 $this->_is_compressing_output = true;
122 if (!empty($this->_is_compressing_output))
127 function getSessionVar($key) {
128 return $this->session->get($key);
130 function setSessionVar($key, $val) {
131 return $this->session->set($key, $val);
133 function deleteSessionVar($key) {
134 return $this->session->delete($key);
137 function getCookieVar($key) {
138 return $this->cookies->get($key);
140 function setCookieVar($key, $val, $lifetime_in_days = false) {
141 return $this->cookies->set($key, $val, $lifetime_in_days);
143 function deleteCookieVar($key) {
144 return $this->cookies->delete($key);
147 function getUploadedFile($key) {
148 return Request_UploadedFile::getUploadedFile($key);
152 function _fix_magic_quotes_gpc() {
153 $needs_fix = array('HTTP_POST_VARS',
160 if (get_magic_quotes_gpc()) {
161 foreach ($needs_fix as $vars)
162 $this->_stripslashes($GLOBALS[$vars]);
167 function _stripslashes(&$var) {
168 if (is_array($var)) {
169 foreach ($var as $key => $val)
170 $this->_stripslashes($var[$key]);
172 elseif (is_string($var))
173 $var = stripslashes($var);
176 function _fix_multipart_form_data () {
177 if (preg_match('|^multipart/form-data|', $this->get('CONTENT_TYPE')))
178 $this->_strip_leading_nl($GLOBALS['HTTP_POST_VARS']);
181 function _strip_leading_nl(&$var) {
182 if (is_array($var)) {
183 foreach ($var as $key => $val)
184 $this->_strip_leading_nl($var[$key]);
186 elseif (is_string($var))
187 $var = preg_replace('|^\r?\n?|', '', $var);
191 class Request_SessionVars {
192 function Request_SessionVars() {
197 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
198 if (isset($vars[$key]))
203 function set($key, $val) {
204 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
205 if (ini_get('register_globals')) {
206 // This is funky but necessary, at least in some PHP's
207 $GLOBALS[$key] = $val;
210 session_register($key);
213 function delete($key) {
214 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
215 if (ini_get('register_globals'))
216 unset($GLOBALS[$key]);
218 session_unregister($key);
222 class Request_CookieVars {
225 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
226 if (isset($vars[$key])) {
227 @$val = unserialize($vars[$key]);
234 function set($key, $val, $persist_days = false) {
235 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
237 if (is_numeric($persist_days)) {
238 $expires = time() + (24 * 3600) * $persist_days;
244 $packedval = serialize($val);
245 $vars[$key] = $packedval;
246 setcookie($key, $packedval, $expires, '/');
249 function delete($key) {
250 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
256 class Request_UploadedFile {
257 function getUploadedFile($postname) {
258 global $HTTP_POST_FILES;
260 if (!isset($HTTP_POST_FILES[$postname]))
263 $fileinfo = &$HTTP_POST_FILES[$postname];
264 if (!is_uploaded_file($fileinfo['tmp_name']))
265 return false; // possible malicious attack.
267 return new Request_UploadedFile($fileinfo);
270 function Request_UploadedFile($fileinfo) {
271 $this->_info = $fileinfo;
275 return $this->_info['size'];
279 return $this->_info['name'];
283 return $this->_info['type'];
287 if ( ($fd = fopen($this->_info['tmp_name'], "rb")) ) {
288 if ($this->getSize() < filesize($this->_info['tmp_name'])) {
289 // FIXME: Some PHP's (or is it some browsers?) put
290 // HTTP/MIME headers in the file body, some don't.
292 // At least, I think that's the case. I know I used
293 // to need this code, now I don't.
295 // This code is more-or-less untested currently.
297 // Dump HTTP headers.
298 while ( ($header = fgets($fd, 4096)) ) {
299 if (trim($header) == '') {
302 else if (!preg_match('/^content-(length|type):/i', $header)) {
312 function getContents() {
314 $data = fread($fd, $this->getSize());
321 * Create NCSA "combined" log entry for current request.
323 class Request_AccessLogEntry
328 * The log entry will be automatically appended to the log file
329 * when the current request terminates.
331 * If you want to modify a Request_AccessLogEntry before it gets
332 * written (e.g. via the setStatus and setSize methods) you should
333 * use an '&' on the constructor, so that you're working with the
334 * original (rather than a copy) object.
337 * $log_entry = & new Request_AccessLogEntry($req, "/tmp/wiki_access_log");
338 * $log_entry->setStatus(401);
342 * @param $request object Request object for current request.
343 * @param $logfile string Log file name.
345 function Request_AccessLogEntry (&$request, $logfile) {
346 $this->logfile = $logfile;
348 $this->host = $request->get('REMOTE_HOST');
349 $this->ident = $request->get('REMOTE_IDENT');
352 $this->user = '-'; // FIXME: get logged-in user name
353 $this->time = time();
354 $this->request = join(' ', array($request->get('REQUEST_METHOD'),
355 $request->get('REQUEST_URI'),
356 $request->get('SERVER_PROTOCOL')));
359 $this->referer = (string) $request->get('HTTP_REFERER');
360 $this->user_agent = (string) $request->get('HTTP_USER_AGENT');
362 global $Request_AccessLogEntry_entries;
363 if (!isset($Request_AccessLogEntry_entries)) {
364 register_shutdown_function("Request_AccessLogEntry_shutdown_function");
366 $Request_AccessLogEntry_entries[] = &$this;
370 * Set result status code.
372 * @param $status integer HTTP status code.
374 function setStatus ($status) {
375 $this->status = $status;
381 * @param $size integer
383 function setSize ($size) {
388 * Get time zone offset.
390 * This is a static member function.
392 * @param $time integer Unix timestamp (defaults to current time).
393 * @return string Zone offset, e.g. "-0800" for PST.
395 function _zone_offset ($time = false) {
398 $offset = date("Z", $time);
403 $offhours = floor($offset / 3600);
404 $offmins = $offset / 60 - $offhours * 60;
405 return sprintf("%s%02d%02d", $negoffset, $offhours, $offmins);
409 * Format time in NCSA format.
411 * This is a static member function.
413 * @param $time integer Unix timestamp (defaults to current time).
414 * @return string Formatted date & time.
416 function _ncsa_time($time = false) {
420 return date("d/M/Y:H:i:s", $time) .
421 " " . $this->_zone_offset();
425 * Write entry to log file.
428 $entry = sprintf('%s %s %s [%s] "%s" %d %d "%s" "%s"',
429 $this->host, $this->ident, $this->user,
430 $this->_ncsa_time($this->time),
431 $this->request, $this->status, $this->size,
432 $this->referer, $this->user_agent);
434 //Error log doesn't provide locking.
435 //error_log("$entry\n", 3, $this->logfile);
438 if (($fp = fopen($this->logfile, "a"))) {
440 fputs($fp, "$entry\n");
450 * @see Request_AccessLogEntry
452 function Request_AccessLogEntry_shutdown_function ()
454 global $Request_AccessLogEntry_entries;
456 foreach ($Request_AccessLogEntry_entries as $entry) {
459 unset($Request_AccessLogEntry_entries);
466 // c-hanging-comment-ender-p: nil
467 // indent-tabs-mode: nil