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