@@ -79,46 +79,33 @@ public function test_base_url()
79
79
$ old_script_name = isset ($ _SERVER ['SCRIPT_NAME ' ]) ? $ _SERVER ['SCRIPT_NAME ' ] : NULL ;
80
80
$ old_script_filename = $ _SERVER ['SCRIPT_FILENAME ' ];
81
81
$ old_https = isset ($ _SERVER ['HTTPS ' ]) ? $ _SERVER ['HTTPS ' ] : NULL ;
82
+ $ old_server_addr = isset ($ _SERVER ['SERVER_ADDR ' ]) ? $ _SERVER ['SERVER_ADDR ' ] : NULL ;
82
83
83
- // Setup server vars for detection
84
- $ host = 'test.com ' ;
85
- $ path = '/ ' ;
86
- $ script = 'base_test.php ' ;
87
- $ _SERVER ['HTTP_HOST ' ] = $ host ;
88
- $ _SERVER ['SCRIPT_NAME ' ] = $ path .$ script ;
89
- $ _SERVER ['SCRIPT_FILENAME ' ] = '/foo/bar/ ' .$ script ;
90
-
91
- // Rerun constructor
84
+ // The 'Host' header is user input and must not be trusted
85
+ $ _SERVER ['HTTP_HOST ' ] = 'test.com ' ;
92
86
$ this ->config = new $ cls ;
87
+ $ this ->assertEquals ('http://localhost/ ' , $ this ->config ->base_url ());
93
88
94
- // Test plain detected (root)
95
- $ this ->assertEquals ('http:// ' .$ host .$ path , $ this ->config ->base_url ());
96
-
97
- // Rerun constructor
98
- $ path = '/path/ ' ;
99
- $ _SERVER ['SCRIPT_NAME ' ] = $ path .$ script ;
100
- $ _SERVER ['SCRIPT_FILENAME ' ] = '/foo/bar/ ' .$ path .$ script ;
89
+ // However, we may fallback to the server's IP address
90
+ $ _SERVER ['SERVER_ADDR ' ] = '127.0.0.1 ' ;
91
+ $ _SERVER ['SCRIPT_NAME ' ] = '/base_test.php ' ;
92
+ $ _SERVER ['SCRIPT_FILENAME ' ] = '/foo/bar/base_test.php ' ;
101
93
$ this ->config = new $ cls ;
94
+ $ this ->assertEquals ('http://127.0.0.1/ ' , $ this ->config ->base_url ());
102
95
103
- // Test plain detected (subfolder)
104
- $ this ->assertEquals ('http:// ' .$ host .$ path , $ this ->config ->base_url ());
105
-
106
- // Rerun constructor
96
+ // Making sure that HTTPS and URI path are also detected
107
97
$ _SERVER ['HTTPS ' ] = 'on ' ;
98
+ $ _SERVER ['SCRIPT_NAME ' ] = '/path/base_test.php ' ;
99
+ $ _SERVER ['SCRIPT_FILENAME ' ] = '/foo/bar/path/base_test.php ' ;
108
100
$ this ->config = new $ cls ;
109
-
110
- // Test secure detected
111
- $ this ->assertEquals ('https:// ' .$ host .$ path , $ this ->config ->base_url ());
101
+ $ this ->assertEquals ('https://127.0.0.1/path/ ' , $ this ->config ->base_url ());
112
102
113
103
// Restore server vars
114
- if ($ old_host === NULL ) unset($ _SERVER ['HTTP_HOST ' ]);
115
- else $ _SERVER ['HTTP_HOST ' ] = $ old_host ;
116
- if ($ old_script_name === NULL ) unset($ _SERVER ['SCRIPT_NAME ' ]);
117
- else $ _SERVER ['SCRIPT_NAME ' ] = $ old_script_name ;
118
- if ($ old_https === NULL ) unset($ _SERVER ['HTTPS ' ]);
119
- else $ _SERVER ['HTTPS ' ] = $ old_https ;
120
-
104
+ $ _SERVER ['HTTP_HOST ' ] = $ old_host ;
105
+ $ _SERVER ['SCRIPT_NAME ' ] = $ old_script_name ;
121
106
$ _SERVER ['SCRIPT_FILENAME ' ] = $ old_script_filename ;
107
+ $ _SERVER ['HTTPS ' ] = $ old_https ;
108
+ $ _SERVER ['SERVER_ADDR ' ] = $ old_server_addr ;
122
109
}
123
110
124
111
// --------------------------------------------------------------------
0 commit comments