JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
re-licensed under GPLv3
[wfpl.git] / http.php
1 <?php
2
3 #  Copyright (C) 2005 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # return our best guess at the url used to access this page, without the path or query string
20 function this_url_sans_path() {
21         if(strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == 'yes') {
22                 $url = 'https';
23                 $expected_port = 443;
24         } else {
25                 $url = 'http';
26                 $expected_port = 80;
27         }
28
29         $url .= '://';
30         if($_SERVER['HTTP_HOST']) {
31                 $url .= $_SERVER['HTTP_HOST'];
32         } else {
33                 $url .= $_SERVER['SERVER_NAME'];
34                 if($_SERVER['SERVER_PORT'] != $expected_port) {
35                         $url .= ':' . $_SERVER['SERVER_PORT'];
36                 }
37         }
38
39         return $url;
40 }
41
42 # just the hostname, no port number
43 function this_host() {
44         if($_SERVER['HTTP_HOST']) {
45                 $host = $_SERVER['HTTP_HOST'];
46                 $p = strpos($host, ':');
47                 if($p) {
48                         $host = substr($host, 0, $p);
49                 }
50                 return $host;
51         } else {
52                 return $_SERVER['SERVER_NAME'];
53         }
54 }
55
56
57
58 # return our best guess at the url used to access this page
59 function this_url() {
60         $url = this_url_sans_path();
61
62         $url .= $_SERVER['REQUEST_URI'];
63
64         return $url;
65 }
66
67 # sends an HTTP redirect
68 #
69 # $url can be:
70 #   1) a full URL
71 #   2) an absolute path
72 #   3) a filename (you can pass a directory/file.html and such, but "../" prefix is not supported yet)
73 function redirect($url, $status = '302 Moved Temporarily', $message = '') {
74         if(!strpos($url, ':')) {
75                 while(substr($url, 0, 2) == './') {
76                         $url = substr($url, 2);
77                 }
78                 if(substr($url, 0, 1) == '/') {
79                         $url = this_url_sans_path() . $url;
80                 } else {
81                         $url = ereg_replace('/[^/]*$', "/$url", this_url());
82                 }
83         }
84                         
85         if(function_exists('session_save_messages')) {
86                 session_save_messages();
87         }
88
89         header("HTTP/1.0 $status");
90         header("Location: $url");
91         echo($message);
92         exit();
93 }
94
95 ?>