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_request.php
100644 376 lines (336 sloc) 9.565 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?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 requests.
*
* @author Ted Kulp
* @since 2.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license GPL
**/
class CmsRequest extends CmsObject
{
  function __construct()
  {
    parent::__construct();
  }
  
  /**
   * Sets up various things important for incoming requests
   *
   * @return void
   * @author Ted Kulp
   **/
  public static function setup()
  {
    #magic_quotes_runtime is a nuisance... turn it off before it messes something up
    set_magic_quotes_runtime(false);
    
    # sanitize $_GET
    array_walk_recursive($_GET, array('CmsRequest', 'sanitize_get_var'));
    
    CmsRequest::strip_slashes_from_globals();
    
    #Fix for IIS (and others) to make sure REQUEST_URI is filled in
    if (!isset($_SERVER['REQUEST_URI']))
    {
     $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
     if(isset($_SERVER['QUERY_STRING']))
     {
     $_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
     }
    }
  }
  
  /**
   * Removes possible javascript from a string
   *
   * @return void
   * @author Ted Kulp
   **/
  public static function sanitize_get_var(&$value, $key)
  {
    $value = eregi_replace('\<\/?script[^\>]*\>', '', $value);
  }
  
  /**
   * Get the internal id based on variables sent in from
   * the request.
   *
   * @return void
   * @author Ted Kulp
   **/
  static public function get_id_from_request()
  {
    $id = '';
    
    if (isset($_REQUEST['mact']))
    {
      $ary = explode(',', $_REQUEST['mact'], 4);
      $id = (isset($ary[1])?$ary[1]:'');
    }
    else
    {
      $id = (isset($_REQUEST['id'])?$_REQUEST['id']:'');
    }
    
    return $id;
  }
  
  public static function calculate_page_from_request()
  {
    $config = cms_config();
    $page = '';
    $id = CmsRequest::get_id_from_request();
 
    if (isset($id) && isset($params[$id . 'returnid']))
    {
      $page = $_REQUEST[$id . 'returnid'];
    }
    else if (CmsConfig::exists("query_var") && $config['query_var'] != '' && isset($_GET[$config['query_var']]))
    {
      $page = $_GET[$config["query_var"]];
 
     //trim off the extension, if there is one set
     if ($config['page_extension'] != '' && endswith($page, $config['page_extension']))
     {
     $page = substr($page, 0, strlen($page) - strlen($config['page_extension']));
     }
    }
    else
    {
      $calced = self::cms_calculate_url();
      if ($calced != '')
        $page = $calced;
    }
    
    $page = self::strip_language_from_page($page);
    
    return rtrim($page, '/');
  }
  
  public static function strip_language_from_page($page)
  {
    $enabled_languages = CmsMultiLanguage::get_enabled_languages();
    $exploded = explode('/', $page);
    if (count($exploded) > 1)
    {
      if(in_array($exploded[0], $enabled_languages))
      {
        //TODO: Set $exploded[0] as the current language
        $page = str_replace($exploded[0] . '/', '', $page);
        CmsMultiLanguage::$current_language = $exploded[0];
      }
    }
    else if (isset($_REQUEST['lang']))
    {
      //TODO: Set $_REQUEST['lang'] as the current language
      CmsMultiLanguage::$current_language = $_REQUEST['lang'];
    }
    return $page;
  }
  
  /**
   * Figures out the page name from the uri string. Has to use different logic
   * based on the type of httpd server.
   */
  public static function cms_calculate_url()
  {
    $result = '';
 
   $config = cms_config();
 
    //Apache
    /*
    if (isset($_SERVER["PHP_SELF"]) && !endswith($_SERVER['PHP_SELF'], 'index.php'))
    {
      $matches = array();
 
      //Seems like PHP_SELF has whatever is after index.php in certain situations
      if (strpos($_SERVER['PHP_SELF'], 'index.php') !== FALSE) {
        if (preg_match('/.*index\.php\/(.*?)$/', $_SERVER['PHP_SELF'], $matches))
        {
          $result = $matches[1];
        }
      }
      else
      {
        $result = $_SERVER['PHP_SELF'];
      }
    }
    */
    //lighttpd
    #else if (isset($_SERVER["REQUEST_URI"]) && !endswith($_SERVER['REQUEST_URI'], 'index.php'))
 
    //apache and lighttpd
    if (isset($_SERVER["REQUEST_URI"]) && !endswith($_SERVER['REQUEST_URI'], 'index.php'))
    {
      $matches = array();
      if (preg_match('/.*index\.php\/(.*?)$/', $_SERVER['REQUEST_URI'], $matches))
      {
        $result = $matches[1];
      }
    }
 
    //trim off the extension, if there is one set
    if ($config['page_extension'] != '' && endswith($result, $config['page_extension']))
    {
      $result = substr($result, 0, strlen($result) - strlen($config['page_extension']));
    }
 
    return $result;
 
  }
  
  /**
   * Determines the uri that was requested
   */
  public static function get_requested_uri()
  {
    $result = '';
    if (isset($_SERVER['HTTP_HOST']))
    {
      $result .= 'http://' . $_SERVER['HTTP_HOST'];
    }
    
    if (isset($_SERVER['REQUEST_URI']))
    {
      $result .= $_SERVER['REQUEST_URI'];
    }
    else if (isset($_SERVER['SCRIPT_NAME']))
    {
      $result .= $_SERVER['SCRIPT_NAME'];
    }
    
    return $result;
  }
  
  public static function get_request_filename()
  {
    if (isset($_SERVER['PATH_TRANSLATED']))
    {
     return str_replace('\\\\', '\\', $_SERVER('PATH_TRANSLATED'));
    }
    else if (isset($_ENV['PATH_TRANSLATED']))
    {
     return str_replace('\\\\', '\\', $_ENV('PATH_TRANSLATED'));
    }
    else
    {
      return $_SERVER['SCRIPT_FILENAME'];
    }
  }
  
  public static function get_calculated_url_base()
  {
    $cur_url_dir = isset($_SERVER['SCRIPT_NAME']) ? dirname($_SERVER['SCRIPT_NAME']) : dirname($_SERVER['SCRIPT_NAME']);
    $cur_file_dir = dirname(self::get_request_filename());
    $root_file_dir = dirname(dirname(dirname(__FILE__)));
 
    //Get the difference in number of characters between the root
    //and the requested file
    $len = strlen($cur_file_dir) - strlen($root_file_dir);
    
    //Now substract that # from the currently requested uri
    $result = substr($cur_url_dir, 0, strlen($cur_url_dir) - $len);
 
    return $result;
  }
 
  /**
   * Strips the slashes from all incoming superglobals,
   * if necessary.
   *
   * @return void
   * @author Ted Kulp
   **/
  public static function strip_slashes_from_globals()
  {
    if (get_magic_quotes_gpc())
    {
     $_GET = CmsRequest::stripslashes_deep($_GET);
     $_POST = CmsRequest::stripslashes_deep($_POST);
     $_REQUEST = CmsRequest::stripslashes_deep($_REQUEST);
     $_COOKIE = CmsRequest::stripslashes_deep($_COOKIE);
     $_SESSION = CmsRequest::stripslashes_deep($_SESSION);
    }
  }
  
  function stripslashes_deep($value)
  {
    if (is_array($value))
    {
      $value = array_map(array('CmsRequest', 'stripslashes_deep'), $value);
    }
    elseif (!empty($value) && is_string($value))
    {
      $value = stripslashes($value);
    }
    return $value;
  }
  
  /**
   * Sanitize input to prevent against XSS and other nasty stuff.
   * Taken from cakephp (http://cakephp.org)
   * Licensed under the MIT License
   *
   * @return string The cleansed string
   **/
  public static function clean_value($val)
  {
    if ($val == "")
    {
      return $val;
    }
    //Replace odd spaces with safe ones
    $val = str_replace(" ", " ", $val);
    $val = str_replace(chr(0xCA), "", $val);
    //Encode any HTML to entities (including \n --> <br />)
    $val = CmsRequest::clean_html($val);
    //Double-check special chars and remove carriage returns
    //For increased SQL security
    $val = preg_replace("/\\\$/", "$", $val);
    $val = preg_replace("/\r/", "", $val);
    $val = str_replace("!", "!", $val);
    $val = str_replace("'", "'", $val);
    //Allow unicode (?)
    $val = preg_replace("/&amp;#([0-9]+);/s", "&#\\1;", $val);
    //Add slashes for SQL
    //$val = $this->sql($val);
    //Swap user-inputted backslashes (?)
    $val = preg_replace("/\\\(?!&amp;#|\?#)/", "\\", $val);
    return $val;
  }
  
  /**
   * Method to sanitize incoming html.
   * Take from cakephp (http://cakephp.org)
   * Licensed under the MIT License
   *
   * @return string The cleansed string
   **/
  public static function clean_html($string, $remove = false)
  {
    if ($remove)
    {
      $string = strip_tags($string);
    }
    else
    {
      $patterns = array("/\&/", "/%/", "/</", "/>/", '/"/', "/'/", "/\(/", "/\)/", "/\+/", "/-/");
      $replacements = array("&amp;", "&#37;", "&lt;", "&gt;", "&quot;", "&#39;", "&#40;", "&#41;", "&#43;", "&#45;");
      $string = preg_replace($patterns, $replacements, $string);
    }
    return $string;
  }
  
  public static function has($name)
  {
    return array_key_exists($name, $_REQUEST);
  }
  
  public static function get($name, $clean = true)
  {
    $value = '';
    if (array_key_exists($name, $_REQUEST))
      $value = $_REQUEST[$name];
    if ($clean)
      $value = self::clean_value($value);
    return $value;
  }
  
  public static function get_cookie($name)
  {
    if (array_key_exists($name, $_COOKIE))
      return self::clean_value($_COOKIE[$name]);
    return '';
  }
  
  public static function set_cookie($name, $value, $expire = null)
  {
    setcookie($name, $value, $expire);
  }
}
 
# vim:ts=4 sw=4 noet
?>