public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
cmsmadesimple-2-0 / lib / classes / class.cms_response.php
100644 189 lines (174 sloc) 5.369 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
 
/**
* Static methods for handling web responses.
*
* @author Ted Kulp
* @since 2.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license GPL
**/
class CmsResponse extends CmsObject
{
  function __construct()
  {
    parent::__construct();
  }
  
  function get_encoding()
  {
    return 'UTF-8';
  }
  
  /**
   * Redirects a user to given url.
   *
   * @param $to The url to redirect to
   *
   * @return void
   * @author Ted Kulp
   **/
  public static function redirect($to)
  {
    $_SERVER['PHP_SELF'] = null;
 
    $config = array();
    try
    {
      $config = cms_config();
    }
    catch (Exception $e)
    {
    }
 
   $schema = $_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http';
   $host = strlen($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
 
   $components = parse_url($to);
   if(count($components) > 0)
   {
   $to = (isset($components['scheme']) && startswith($components['scheme'], 'http') ? $components['scheme'] : $schema) . '://';
   $to .= isset($components['host']) ? $components['host'] : $host;
   $to .= isset($components['port']) ? ':' . $components['port'] : '';
   if(isset($components['path']))
   {
   if(in_array(substr($components['path'],0,1),array('\\','/')))//Path is absolute, just append.
   {
   $to .= $components['path'];
   }
   //Path is relative, append current directory first.
        else if (isset($_SERVER['PHP_SELF']) && !is_null($_SERVER['PHP_SELF'])) //Apache
   {
   $to .= (strlen(dirname($_SERVER['PHP_SELF'])) > 1 ? dirname($_SERVER['PHP_SELF']).'/' : '/') . $components['path'];
   }
        else if (isset($_SERVER['REQUEST_URI']) && !is_null($_SERVER['REQUEST_URI'])) //Lighttpd
   {
          if (endswith($_SERVER['REQUEST_URI'], '/'))
            $to .= (strlen($_SERVER['REQUEST_URI']) > 1 ? $_SERVER['REQUEST_URI'] : '/') . $components['path'];
          else
            $to .= (strlen(dirname($_SERVER['REQUEST_URI'])) > 1 ? dirname($_SERVER['REQUEST_URI']).'/' : '/') . $components['path'];
   }
   }
   $to .= isset($components['query']) ? '?' . $components['query'] : '';
   $to .= isset($components['fragment']) ? '#' . $components['fragment'] : '';
   }
   else
   {
   $to = $schema."://".$host."/".$to;
   }
 
   if (headers_sent() && !(isset($config) && $config['debug'] == true))
   {
   // use javascript instead
   echo '<script type="text/javascript">
   <!--
   location.replace("'.$to.'");
   // -->
   </script>
   <noscript>
   <meta http-equiv="Refresh" content="0;URL='.$to.'">
   </noscript>';
   exit;
   }
   else
   {
   if (isset($config) && $config['debug'] == true)
   {
   echo "Debug is on. Redirecting disabled... Please click this link to continue.<br />";
   echo "<a href=\"".$to."\">".$to."</a><br />";
 
        echo '<pre>';
        debug_print_backtrace();
        echo '</pre>';
  
   exit();
   }
   else
   {
   header("Location: $to");
   exit();
   }
    }
  }
 
  /**
   * Given a page ID or an alias, redirect to it
   */
  public static function redirect_to_alias($alias)
  {
    $node = CmsPageTree::get_node_by_alias($alias);
    $content = $node->get_content();
    if (isset($content))
      {
        if ($content->get_url() != '')
          {
            redirect($content->get_url());
          }
      }
  }
 
  
  /**
   * Shows a very close approximation of an Apache generated 404 error.
   * It also sends the actual header along as well, so that generic
   * browser error pages (like what IE does) will be displayed.
   *
   * @return void
   * @author Ted Kulp
   **/
  function send_error_404()
  {
    while (@ob_end_clean());
    header("HTTP/1.0 404 Not Found");
    header("Status: 404 Not Found");
    echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>';
    exit();
  }
  
  /**
   * Converts a string into a valid DOM id.
   *
   * @param string The string to be converted
   * @return string The converted string
   * @author Ted Kulp
   **/
  public static function make_dom_id($text)
  {
    return trim(preg_replace("/[^a-z0-9_\-]+/", '_', strtolower($text)), ' _');
  }
 
}
 
# vim:ts=4 sw=4 noet
?>