1: <?php
2: /*
3: * Simpletools Framework.
4: * Copyright (c) 2009, Marcin Rosinski. (http://www.getsimpletools.com)
5: * All rights reserved.
6: *
7: * LICENCE
8: *
9: * Redistribution and use in source and binary forms, with or without modification,
10: * are permitted provided that the following conditions are met:
11: *
12: * - Redistributions of source code must retain the above copyright notice,
13: * this list of conditions and the following disclaimer.
14: *
15: * - Redistributions in binary form must reproduce the above copyright notice,
16: * this list of conditions and the following disclaimer in the documentation and/or other
17: * materials provided with the distribution.
18: *
19: * - Neither the name of the Simpletools nor the names of its contributors may be used to
20: * endorse or promote products derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29: * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: *
31: * @framework Simpletools
32: * @description Secure connections handler
33: * @copyright Copyright (c) 2009 Marcin Rosinski. (http://www.getsimpletools.com)
34: * @license http://www.opensource.org/licenses/bsd-license.php - BSD
35: *
36: */
37: namespace Simpletools\Http;
38:
39: use Simpletools\Http\Ssl;
40:
41: class Ssl
42: {
43: private $_loop = false;
44: protected static $_instance = null;
45:
46: public static function &getInstance()
47: {
48: if (empty(self::$_instance))
49: {
50: self::$_instance = new Ssl();
51: }
52:
53: return self::$_instance;
54: }
55:
56: public function https($flag=true,$redirectionMethod=301)
57: {
58: if(strtolower($flag) !== 'allowed')
59: {
60: $url = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
61:
62: if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']))
63: {
64: $https = ($_SERVER['HTTP_X_FORWARDED_PROTO']=='https') ? true : false;
65: }
66: else
67: {
68: $https = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']!='off') ? true : false;
69: }
70:
71: if(!$this->_loop && $flag)
72: {
73: $this->_loop = 'https';
74: }
75: else if(!$this->_loop)
76: {
77: $this->_loop = 'http';
78: }
79:
80: if($flag && (!$https))
81: {
82: if($this->_loop != 'http')
83: {
84: $url = 'https://'.$url;
85: header('Location: '.$url,true,$redirectionMethod);
86: exit;
87: }
88: }
89: else if(!$flag && $https)
90: {
91: if($this->_loop != 'https')
92: {
93: $url = 'http://'.$url;
94: header('Location: '.$url,true,$redirectionMethod);
95: exit;
96: }
97: }
98: }
99: }
100: }
101:
102: ?>