1 <?php rcs_id('$Id: Request.php,v 1.7 2002-01-09 16:57:20 carstenklapp 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];
58 function setArg($key, $val) {
59 $this->args[$key] = $val;
62 function getURLtoSelf($args = false) {
63 $get_args = $this->args;
65 $get_args = array_merge($get_args, $args);
67 unset ($get_args['pagename']);
68 if ($get_args['action'] == 'browse')
69 unset($get_args['action']);
71 return WikiURL($this->getArg('pagename'), $get_args);
75 function redirect($url) {
76 header("Location: $url");
77 if (isset($this->_log_entry))
78 $this->_log_entry->setStatus(302);
81 function setStatus($status) {
82 if (preg_match('|^HTTP/.*?\s(\d+)|i', $status, $m)) {
87 $status = (integer) $status;
88 $reasons = array('200' => 'OK',
90 '400' => 'Bad Request',
91 '401' => 'Unauthorized',
93 '404' => 'Not Found');
94 header(sprintf("HTTP/1.0 %d %s", $status, $reason[$status]));
97 if (isset($this->_log_entry))
98 $this->_log_entry->setStatus($status);
101 function compress_output() {
102 if (function_exists('ob_gzhandler')) {
103 ob_start('ob_gzhandler');
104 $this->_is_compressing_output = true;
109 if (!empty($this->_is_compressing_output))
114 function getSessionVar($key) {
115 return $this->session->get($key);
117 function setSessionVar($key, $val) {
118 return $this->session->set($key, $val);
120 function deleteSessionVar($key) {
121 return $this->session->delete($key);
124 function getCookieVar($key) {
125 return $this->cookies->get($key);
127 function setCookieVar($key, $val, $lifetime_in_days = false) {
128 return $this->cookies->set($key, $val, $lifetime_in_days);
130 function deleteCookieVar($key) {
131 return $this->cookies->delete($key);
134 function getUploadedFile($key) {
135 return Request_UploadedFile::getUploadedFile($key);
139 function _fix_magic_quotes_gpc() {
140 $needs_fix = array('HTTP_POST_VARS',
147 if (get_magic_quotes_gpc()) {
148 foreach ($needs_fix as $vars)
149 $this->_stripslashes($GLOBALS[$vars]);
154 function _stripslashes(&$var) {
155 if (is_array($var)) {
156 foreach ($var as $key => $val)
157 $this->_stripslashes($var[$key]);
159 elseif (is_string($var))
160 $var = stripslashes($var);
163 function _fix_multipart_form_data () {
164 if (preg_match('|^multipart/form-data|', $this->get('CONTENT_TYPE')))
165 $this->_strip_leading_nl($GLOBALS['HTTP_POST_VARS']);
168 function _strip_leading_nl(&$var) {
169 if (is_array($var)) {
170 foreach ($var as $key => $val)
171 $this->_strip_leading_nl($var[$key]);
173 elseif (is_string($var))
174 $var = preg_replace('|^\r?\n?|', '', $var);
178 class Request_SessionVars {
179 function Request_SessionVars() {
184 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
185 if (isset($vars[$key]))
190 function set($key, $val) {
191 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
192 if (ini_get('register_globals')) {
193 // This is funky but necessary, at least in some PHP's
194 $GLOBALS[$key] = $val;
197 session_register($key);
200 function delete($key) {
201 $vars = &$GLOBALS['HTTP_SESSION_VARS'];
202 if (ini_get('register_globals'))
203 unset($GLOBALS[$key]);
205 session_unregister($key);
209 class Request_CookieVars {
212 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
213 if (isset($vars[$key])) {
214 @$val = unserialize($vars[$key]);
221 function set($key, $val, $persist_days = false) {
222 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
224 if (is_numeric($persist_days)) {
225 $expires = time() + (24 * 3600) * $persist_days;
231 $packedval = serialize($val);
232 $vars[$key] = $packedval;
233 setcookie($key, $packedval, $expires, '/');
236 function delete($key) {
237 $vars = &$GLOBALS['HTTP_COOKIE_VARS'];
243 class Request_UploadedFile {
244 function getUploadedFile($postname) {
245 global $HTTP_POST_FILES;
247 if (!isset($HTTP_POST_FILES[$postname]))
250 $fileinfo = &$HTTP_POST_FILES[$postname];
251 if (!is_uploaded_file($fileinfo['tmp_name']))
252 return false; // possible malicious attack.
254 return new Request_UploadedFile($fileinfo);
257 function Request_UploadedFile($fileinfo) {
258 $this->_info = $fileinfo;
262 return $this->_info['size'];
266 return $this->_info['name'];
270 return $this->_info['type'];
274 if ( ($fd = fopen($this->_info['tmp_name'], "rb")) ) {
275 if ($this->getSize() < filesize($this->_info['tmp_name'])) {
276 // FIXME: Some PHP's (or is it some browsers?) put
277 // HTTP/MIME headers in the file body, some don't.
279 // At least, I think that's the case. I know I used
280 // to need this code, now I don't.
282 // This code is more-or-less untested currently.
284 // Dump HTTP headers.
285 while ( ($header = fgets($fd, 4096)) ) {
286 if (trim($header) == '') {
289 else if (!preg_match('/^content-(length|type):/i', $header)) {
299 function getContents() {
301 $data = fread($fd, $this->getSize());
308 * Create NCSA "combined" log entry for current request.
310 class Request_AccessLogEntry
315 * The log entry will be automatically appended to the log file
316 * when the current request terminates.
318 * If you want to modify a Request_AccessLogEntry before it gets
319 * written (e.g. via the setStatus and setSize methods) you should
320 * use an '&' on the constructor, so that you're working with the
321 * original (rather than a copy) object.
324 * $log_entry = & new Request_AccessLogEntry($req, "/tmp/wiki_access_log");
325 * $log_entry->setStatus(401);
329 * @param $request object Request object for current request.
330 * @param $logfile string Log file name.
332 function Request_AccessLogEntry ($request, $logfile) {
333 $this->logfile = $logfile;
335 $this->host = $request->get('REMOTE_HOST');
336 $this->ident = $request->get('REMOTE_IDENT');
339 $this->user = '-'; // FIXME: get logged-in user name
340 $this->time = time();
341 $this->request = join(' ', array($request->get('REQUEST_METHOD'),
342 $request->get('REQUEST_URI'),
343 $request->get('SERVER_PROTOCOL')));
346 $this->referer = (string) $request->get('HTTP_REFERER');
347 $this->user_agent = (string) $request->get('HTTP_USER_AGENT');
349 global $Request_AccessLogEntry_entries;
350 if (!isset($Request_AccessLogEntry_entries)) {
351 register_shutdown_function("Request_AccessLogEntry_shutdown_function");
353 $Request_AccessLogEntry_entries[] = &$this;
357 * Set result status code.
359 * @param $status integer HTTP status code.
361 function setStatus ($status) {
362 $this->status = $status;
368 * @param $size integer
370 function setSize ($size) {
375 * Get time zone offset.
377 * This is a static member function.
379 * @param $time integer Unix timestamp (defaults to current time).
380 * @return string Zone offset, e.g. "-0800" for PST.
382 function _zone_offset ($time = false) {
385 $offset = date("Z", $time);
390 $offhours = floor($offset / 3600);
391 $offmins = $offset / 60 - $offhours * 60;
392 return sprintf("%s%02d%02d", $negoffset, $offhours, $offmins);
396 * Format time in NCSA format.
398 * This is a static member function.
400 * @param $time integer Unix timestamp (defaults to current time).
401 * @return string Formatted date & time.
403 function _ncsa_time($time = false) {
407 return date("d/M/Y:H:i:s", $time) .
408 " " . $this->_zone_offset();
412 * Write entry to log file.
415 $entry = sprintf('%s %s %s [%s] "%s" %d %d "%s" "%s"',
416 $this->host, $this->ident, $this->user,
417 $this->_ncsa_time($this->time),
418 $this->request, $this->status, $this->size,
419 $this->referer, $this->user_agent);
421 //Error log doesn't provide locking.
422 //error_log("$entry\n", 3, $this->logfile);
425 if (($fp = fopen($this->logfile, "a"))) {
427 fputs($fp, "$entry\n");
437 * @see Request_AccessLogEntry
439 function Request_AccessLogEntry_shutdown_function ()
441 global $Request_AccessLogEntry_entries;
443 foreach ($Request_AccessLogEntry_entries as $entry) {
446 unset($Request_AccessLogEntry_entries);
453 // c-hanging-comment-ender-p: nil
454 // indent-tabs-mode: nil