This commit was manufactured by cvs2svn to create tag
[Packages/TYPO3.CMS.git] / misc / phpcheck / incfile.php
1 <?php
2 SetCookie("test_script_cookie", "Cookie Value!", 0, "/");
3
4
5 class t3lib_div {
6 function trimExplode($delim, $string, $onlyNonEmptyValues=0) {
7 // This explodes a comma-list into an array where the values are parsed through trim();
8 $temp = explode($delim,$string);
9 $newtemp=array();
10 while(list($key,$val)=each($temp)) {
11 if (!$onlyNonEmptyValues || strcmp("",trim($val))) {
12 $newtemp[]=trim($val);
13 }
14 }
15 reset($newtemp);
16 return $newtemp;
17 }
18 function dirname($path) {
19 $p=t3lib_div::revExplode("/",$path,2);
20 return count($p)==2?$p[0]:"";
21 }
22 function revExplode($delim, $string, $count=0) {
23 $temp = explode($delim,strrev($string),$count);
24 while(list($key,$val)=each($temp)) {
25 $temp[$key]=strrev($val);
26 }
27 $temp=array_reverse($temp);
28 reset($temp);
29 return $temp;
30 }
31
32 /**
33 * Abstraction method which returns System Environment Variables regardless of server OS, CGI/MODULE version etc. Basically this is SERVER variables for most of them.
34 * This should be used instead of getEnv() and HTTP_SERVER_VARS/ENV_VARS to get reliable values for all situations.
35 *
36 * Usage: 226
37 *
38 * @param string Name of the "environment variable"/"server variable" you wish to use. Valid values are SCRIPT_NAME, SCRIPT_FILENAME, REQUEST_URI, PATH_INFO, REMOTE_ADDR, REMOTE_HOST, HTTP_REFERER, HTTP_HOST, HTTP_USER_AGENT, HTTP_ACCEPT_LANGUAGE, QUERY_STRING, TYPO3_DOCUMENT_ROOT, TYPO3_HOST_ONLY, TYPO3_HOST_ONLY, TYPO3_REQUEST_HOST, TYPO3_REQUEST_URL, TYPO3_REQUEST_SCRIPT, TYPO3_REQUEST_DIR, TYPO3_SITE_URL, _ARRAY
39 * @return string Value based on the input key, independent of server/os environment.
40 */
41 function getIndpEnv($getEnvName) {
42 global $HTTP_SERVER_VARS;
43 /*
44 Conventions:
45 output from parse_url():
46 URL: http://username:password@192.168.1.4:8080/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value#link1
47 [scheme] => 'http'
48 [user] => 'username'
49 [pass] => 'password'
50 [host] => '192.168.1.4'
51 [port] => '8080'
52 [path] => '/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/'
53 [query] => 'arg1,arg2,arg3&p1=parameter1&p2[key]=value'
54 [fragment] => 'link1'
55
56 Further definition: [path_script] = '/typo3/32/temp/phpcheck/index.php'
57 [path_dir] = '/typo3/32/temp/phpcheck/'
58 [path_info] = '/arg1/arg2/arg3/'
59 [path] = [path_script/path_dir][path_info]
60
61
62 Keys supported:
63
64 URI______:
65 REQUEST_URI = [path]?[query] = /typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value
66 HTTP_HOST = [host][:[port]] = 192.168.1.4:8080
67 SCRIPT_NAME = [path_script]++ = /typo3/32/temp/phpcheck/index.php // NOTICE THAT SCRIPT_NAME will return the php-script name ALSO. [path_script] may not do that (eg. '/somedir/' may result in SCRIPT_NAME '/somedir/index.php')!
68 PATH_INFO = [path_info] = /arg1/arg2/arg3/
69 QUERY_STRING = [query] = arg1,arg2,arg3&p1=parameter1&p2[key]=value
70 HTTP_REFERER = [scheme]://[host][:[port]][path] = http://192.168.1.4:8080/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value
71 (Notice: NO username/password + NO fragment)
72
73 CLIENT____:
74 REMOTE_ADDR = (client IP)
75 REMOTE_HOST = (client host)
76 HTTP_USER_AGENT = (client user agent)
77 HTTP_ACCEPT_LANGUAGE = (client accept language)
78
79 SERVER____:
80 SCRIPT_FILENAME = Absolute filename of script (Differs between windows/unix). On windows 'C:\\blabla\\blabl\\' will be converted to 'C:/blabla/blabl/'
81
82 Special extras:
83 TYPO3_HOST_ONLY = [host] = 192.168.1.4
84 TYPO3_PORT = [port] = 8080 (blank if 80, taken from host value)
85 TYPO3_REQUEST_HOST = [scheme]://[host][:[port]]
86 TYPO3_REQUEST_URL = [scheme]://[host][:[port]][path]?[query] (sheme will by default be 'http' until we can detect if it's https -
87 TYPO3_REQUEST_SCRIPT = [scheme]://[host][:[port]][path_script]
88 TYPO3_REQUEST_DIR = [scheme]://[host][:[port]][path_dir]
89 TYPO3_SITE_URL = [scheme]://[host][:[port]][path_dir] of the TYPO3 website
90 TYPO3_DOCUMENT_ROOT = Absolute path of root of documents: TYPO3_DOCUMENT_ROOT.SCRIPT_NAME = SCRIPT_FILENAME (typically)
91
92 Notice: [fragment] is apparently NEVER available to the script!
93
94
95 Testing suggestions:
96 - Output all the values.
97 - In the script, make a link to the script it self, maybe add some parameters and click the link a few times so HTTP_REFERER is seen
98 - ALSO TRY the script from the ROOT of a site (like 'http://www.mytest.com/' and not 'http://www.mytest.com/test/' !!)
99
100 */
101
102 # if ($getEnvName=='HTTP_REFERER') return '';
103 switch((string)$getEnvName) {
104 case 'SCRIPT_NAME':
105 return php_sapi_name()=='cgi' ? $HTTP_SERVER_VARS['PATH_INFO'] : $HTTP_SERVER_VARS['SCRIPT_NAME'];
106 break;
107 case 'SCRIPT_FILENAME':
108 return str_replace('//','/', str_replace('\\','/', php_sapi_name()=='cgi'||php_sapi_name()=='isapi' ? $HTTP_SERVER_VARS['PATH_TRANSLATED']:$HTTP_SERVER_VARS['SCRIPT_FILENAME']));
109 break;
110 case 'REQUEST_URI':
111 // Typical application of REQUEST_URI is return urls, forms submitting to itselt etc. Eg: returnUrl='.rawurlencode(t3lib_div::getIndpEnv('REQUEST_URI'))
112 if (!$HTTP_SERVER_VARS['REQUEST_URI']) { // This is for ISS/CGI which does not have the REQUEST_URI available.
113 return '/'.ereg_replace('^/','',t3lib_div::getIndpEnv('SCRIPT_NAME')).
114 ($HTTP_SERVER_VARS['QUERY_STRING']?'?'.$HTTP_SERVER_VARS['QUERY_STRING']:'');
115 } else return $HTTP_SERVER_VARS['REQUEST_URI'];
116 break;
117 case 'PATH_INFO':
118 // $HTTP_SERVER_VARS['PATH_INFO']!=$HTTP_SERVER_VARS['SCRIPT_NAME'] is necessary because some servers (Windows/CGI) are seen to set PATH_INFO equal to script_name
119 // Further, there must be at least one '/' in the path - else the PATH_INFO value does not make sense.
120 // IF 'PATH_INFO' never works for our purpose in TYPO3 with CGI-servers, then 'php_sapi_name()=='cgi'' might be a better check. Right now strcmp($HTTP_SERVER_VARS['PATH_INFO'],t3lib_div::getIndpEnv('SCRIPT_NAME')) will always return false for CGI-versions, but that is only as long as SCRIPT_NAME is set equal to PATH_INFO because of php_sapi_name()=='cgi' (see above)
121 // if (strcmp($HTTP_SERVER_VARS['PATH_INFO'],t3lib_div::getIndpEnv('SCRIPT_NAME')) && count(explode('/',$HTTP_SERVER_VARS['PATH_INFO']))>1) {
122 if (php_sapi_name()!='cgi') {
123 return $HTTP_SERVER_VARS['PATH_INFO'];
124 } else return '';
125 break;
126 // These are let through without modification
127 case 'REMOTE_ADDR':
128 case 'REMOTE_HOST':
129 case 'HTTP_REFERER':
130 case 'HTTP_HOST':
131 case 'HTTP_USER_AGENT':
132 case 'HTTP_ACCEPT_LANGUAGE':
133 case 'QUERY_STRING':
134 return $HTTP_SERVER_VARS[$getEnvName];
135 break;
136 case 'TYPO3_DOCUMENT_ROOT':
137 // Some CGI-versions (LA13CGI) and mod-rewrite rules on MODULE versions will deliver a 'wrong' DOCUMENT_ROOT (according to our description). Further various aliases/mod_rewrite rules can disturb this as well.
138 // Therefore the DOCUMENT_ROOT is now always calculated as the SCRIPT_FILENAME minus the end part shared with SCRIPT_NAME.
139 $SFN = t3lib_div::getIndpEnv('SCRIPT_FILENAME');
140 $SN_A = explode('/',strrev(t3lib_div::getIndpEnv('SCRIPT_NAME')));
141 $SFN_A = explode('/',strrev($SFN));
142 $acc=array();
143 while(list($kk,$vv)=each($SN_A)) {
144 if (!strcmp($SFN_A[$kk],$vv)) {
145 $acc[]=$vv;
146 } else break;
147 }
148 $commonEnd=strrev(implode('/',$acc));
149 if (strcmp($commonEnd,'')) $DR = substr($SFN,0,-(strlen($commonEnd)+1));
150 return $DR;
151 break;
152 case 'TYPO3_HOST_ONLY':
153 $p=explode(':',$HTTP_SERVER_VARS['HTTP_HOST']);
154 return $p[0];
155 break;
156 case 'TYPO3_PORT':
157 $p=explode(':',$HTTP_SERVER_VARS['HTTP_HOST']);
158 return $p[1];
159 break;
160 case 'TYPO3_REQUEST_HOST':
161 return 'http'.($HTTP_SERVER_VARS['SSL_SESSION_ID']?'s':'').'://'. // I hope this: ($HTTP_SERVER_VARS['SSL_SESSION_ID']?'s':'') - is sufficient to detect https...
162 $HTTP_SERVER_VARS['HTTP_HOST'];
163 break;
164 case 'TYPO3_REQUEST_URL':
165 return t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::getIndpEnv('REQUEST_URI');
166 break;
167 case 'TYPO3_REQUEST_SCRIPT':
168 return t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::getIndpEnv('SCRIPT_NAME');
169 break;
170 case 'TYPO3_REQUEST_DIR':
171 return t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::dirname(t3lib_div::getIndpEnv('SCRIPT_NAME')).'/';
172 break;
173 case 'TYPO3_SITE_URL':
174 if (defined('PATH_thisScript') && defined('PATH_site')) {
175 $lPath = substr(dirname(PATH_thisScript),strlen(PATH_site)).'/';
176 $url = t3lib_div::getIndpEnv('TYPO3_REQUEST_DIR');
177 $siteUrl = substr($url,0,-strlen($lPath));
178 if (substr($siteUrl,-1)!='/') $siteUrl.='/';
179 return $siteUrl;
180 } else return '';
181
182 break;
183 case '_ARRAY':
184 $out=array();
185 // Here, list ALL possible keys to this function for debug display.
186 $envTestVars = t3lib_div::trimExplode(',','
187 HTTP_HOST,
188 TYPO3_HOST_ONLY,
189 TYPO3_PORT,
190 PATH_INFO,
191 QUERY_STRING,
192 REQUEST_URI,
193 HTTP_REFERER,
194 TYPO3_REQUEST_HOST,
195 TYPO3_REQUEST_URL,
196 TYPO3_REQUEST_SCRIPT,
197 TYPO3_REQUEST_DIR,
198 TYPO3_SITE_URL,
199 SCRIPT_NAME,
200 TYPO3_DOCUMENT_ROOT,
201 SCRIPT_FILENAME,
202 REMOTE_ADDR,
203 REMOTE_HOST,
204 HTTP_USER_AGENT,
205 HTTP_ACCEPT_LANGUAGE',1);
206 reset($envTestVars);
207 while(list(,$v)=each($envTestVars)) {
208 $out[$v]=t3lib_div::getIndpEnv($v);
209 }
210 reset($out);
211 return $out;
212 break;
213 }
214 }
215
216 }
217
218 function view_array($array_in) {
219 // Returns HTML-code, which is a visual representation of a multidimensional array
220 // use t3lib_div::print_array() in order to print an array
221 // Returns false if $array_in is not an array
222 if (is_array($array_in)) {
223 $result='<table border=1 cellpadding=1 cellspacing=0 bgcolor=white>';
224 if (!count($array_in)) {$result.= '<tr><td><font face="Verdana,Arial" size="1"><b>'.HTMLSpecialChars("EMPTY!").'</b></font></td></tr>';}
225 while (list($key,$val)=each($array_in)) {
226 $result.= '<tr><td><font face="Verdana,Arial" size="1">'.HTMLSpecialChars($key).'</font></td><td>';
227 if (is_array($array_in[$key])) {
228 $result.=t3lib_div::view_array($array_in[$key]);
229 } else
230 $result.= '<font face="Verdana,Arial" size="1" color=red>'.nl2br(HTMLSpecialChars($val)).'<BR></font>';
231 $result.= '</td></tr>';
232 }
233 $result.= '</table>';
234 } else {
235 $result = false;
236 }
237 return $result;
238 }
239 function debug($array_in) {
240 // Prints an array
241 echo view_array($array_in);
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 error_reporting (E_ALL ^ E_NOTICE);
260
261 define("TYPO3_OS", stristr(PHP_OS,"win")&&!stristr(PHP_OS,"darwin")?"WIN":"");
262 /*
263 define("PATH_thisScript",
264 TYPO3_OS=="WIN" ?
265 str_replace('//','/',str_replace('\\','/', $HTTP_SERVER_VARS["PATH_TRANSLATED"]?$HTTP_SERVER_VARS["PATH_TRANSLATED"]:getenv("PATH_TRANSLATED"))) :
266 (php_sapi_name()=="cgi"?(getenv("PATH_TRANSLATED")?getenv("PATH_TRANSLATED"):getenv("SCRIPT_FILENAME")):$HTTP_SERVER_VARS["PATH_TRANSLATED"])
267 );
268 */
269
270 define("PATH_thisScript",str_replace('//','/', str_replace('\\','/', php_sapi_name()=="cgi"||php_sapi_name()=="isapi" ? $HTTP_SERVER_VARS["PATH_TRANSLATED"]:$HTTP_SERVER_VARS["SCRIPT_FILENAME"])));
271 define('PATH_site', dirname(PATH_thisScript).'/');
272
273
274 if (count($HTTP_GET_VARS) || $HTTP_SERVER_VARS["HTTP_REFERER"]) {
275 # KOMPENSATED:
276 echo "<H3>t3lib_div::getIndpEnv()</H3><p>These are 'system variables' returned from t3lib_div::getIndpEnv() and should be universal for any server configuration:</p>";
277 debug(t3lib_div::getIndpEnv("_ARRAY"));
278
279 debug(array(
280 "PHP_OS"=>PHP_OS,
281 "TYPO3_OS"=>TYPO3_OS,
282 "PATH_thisScript"=>PATH_thisScript,
283 "php_sapi_name()" => php_sapi_name()
284 ));
285
286
287
288
289 ##debug(parse_url("http://admin:palindrom@192.168.1.4:8080/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/index.php?arg1,arg2,arg3&p1=parameter1&p2[key]=value#link1"));
290
291
292 echo "<H3>Raw values</H3><p>These are the raw 'system variables' returned from getenv(), HTTP_SERVER_VARS, HTTP_ENV_VARS etc. These are displayed here so we can find the right values via this testscript to map to with t3lib_div::getIndpEnv()</p>";
293 $envTestVars = explode(",","REQUEST_URI,REMOTE_ADDR,REMOTE_HOST,PATH_INFO,SCRIPT_NAME,SCRIPT_FILENAME,HTTP_HOST,HTTP_USER_AGENT,HTTP_ACCEPT_ENCODING,HTTP_REFERER,QUERY_STRING");
294 $lines=array();
295 $lines[] = '<tr bgcolor="#eeeeee">
296 <td>Key</td>
297 <td nowrap>getenv()</td>
298 <td nowrap>HTTP_SERVER_VARS</td>
299 <td nowrap>_SERVER</td>
300 <td nowrap>HTTP_ENV_VARS</td>
301 <td nowrap>_ENV</td>
302 </tr>';
303 while(list(,$v)=each($envTestVars)) {
304 $lines[] = '<tr>
305 <td bgcolor="#eeeeee">'.htmlspecialchars($v).'</td>
306 <td nowrap>'.htmlspecialchars(getenv($v)).'&nbsp;</td>
307 <td nowrap>'.htmlspecialchars($GLOBALS["HTTP_SERVER_VARS"][$v]).'&nbsp;</td>
308 <td nowrap>'.htmlspecialchars($GLOBALS["_SERVER"][$v]).'&nbsp;</td>
309 <td nowrap>'.htmlspecialchars($GLOBALS["HTTP_ENV_VARS"][$v]).'&nbsp;</td>
310 <td nowrap>'.htmlspecialchars($GLOBALS["_ENV"][$v]).'&nbsp;</td>
311 </tr>';
312 }
313 echo '<table border=1 style="font-family:verdana; font-size:10px;">'.implode("",$lines).'</table>';
314
315 echo '<table border=1 style="font-family:verdana; font-size:10px;">
316 <tr><td>'.htmlspecialchars('$GLOBALS["HTTP_SERVER_VARS"]["DOCUMENT_ROOT"]').'</td><td>'.htmlspecialchars($GLOBALS["HTTP_SERVER_VARS"]["DOCUMENT_ROOT"]).'</td></tr>
317 <tr><td>'.htmlspecialchars('$HTTP_SERVER_VARS["PATH_TRANSLATED"]').'</td><td>'.htmlspecialchars($HTTP_SERVER_VARS["PATH_TRANSLATED"]).'</td></tr>
318 <tr><td>'.htmlspecialchars('$GLOBALS["HTTP_SERVER_VARS"]["REDIRECT_URL"]').'</td><td>'.htmlspecialchars($GLOBALS["HTTP_SERVER_VARS"]["REDIRECT_URL"]).'</td></tr>
319 <tr><td>'.htmlspecialchars('$GLOBALS["HTTP_SERVER_VARS"]["REQUEST_URI"]').'</td><td>'.htmlspecialchars($GLOBALS["HTTP_SERVER_VARS"]["REQUEST_URI"]).'</td></tr>
320 </table>';
321
322
323
324 echo "Cookie 'test_script_cookie': '<strong>".$HTTP_COOKIE_VARS["test_script_cookie"]."</strong>'<BR>";
325
326
327 echo '<HR><a name="link1"></a>';
328 echo '<div style="border: 1px solid black; padding: 10px 10px 10px 10px;"><h3>What to do now?</h3>
329 <p>1) Click this link above once more: <a href="index.php?arg1,arg2,arg3&p1=parameter1&p2[key]='.substr(md5(time()),0,4).'#link1">Go to this page again.</a><BR>
330 2) Then save this HTML-page and send it to kasper@typo3.com with information about 1) which webserver (Apache/ISS), 2) Unix/Windows, 3) CGI or module (ISAPI)<br>
331 2a) You might help us find any differences in your values to this <a href="reference.html" target="_blank">reference example</a> by comparing the values before you send the result (thanks).
332 <br>
333 3) If you are really advanced you try and click the link below here. With CGI-versions of servers it will most likely give an error page. If it does not, please send the output to me as well (save HTML-page and send to kasper@typo3.com). If you do this PATH_INFO test, please let me know.<br><br>
334
335 4) For the really, really advanced folks, it might be interesting to see the output if you could place this link in the root of a domain. That means the index.php script will be executed from eg. "http://www.blablabla.com/" and not "http://www.blablabla.com/kaspers_test/" - it can make a difference.<br>
336 <br>
337 <br>
338 I am operating with these categories of servers. <strong>Please identify your configuration and label your email with that "type":</strong><br><br>
339
340 <table border=1>
341 <tr bgcolor="#eeeeee">
342 <td><em>TYPE:</em></td>
343 <td><em>Description:</em></td>
344 </tr>
345 <tr>
346 <td>WA13CGI</td>
347 <td>Windows / Apache 1.3.x / CGI</td>
348 </tr>
349 <tr>
350 <td>WA2CGI</td>
351 <td>Windows / Apache 2.x / CGI</td>
352 </tr>
353 <tr>
354 <td>WA13ISAPI</td>
355 <td>Windows / Apache 1.3.x / ISAPI-module</td>
356 </tr>
357 <tr>
358 <td>WA2ISAPI</td>
359 <td>Windows / Apache 2.x / ISAPI-module</td>
360 </tr>
361 <tr>
362 <td>WISS_CGI</td>
363 <td>Windows / ISS / CGI</td>
364 </tr>
365 <tr>
366 <td>WISS_ISAPI</td>
367 <td>Windows / ISS / ISAPI-module</td>
368 </tr>
369 <tr>
370 <td>MA13MOD</td>
371 <td>Mac (Darwin) / Apache 1.3.x / Module</td>
372 </tr>
373 <tr>
374 <td>LA13CGI</td>
375 <td>Linux / Apache 1.3.x / CGI</td>
376 </tr>
377 <tr>
378 <td>LA2CGI</td>
379 <td>Linux / Apache 2.x / CGI</td>
380 </tr>
381 <tr>
382 <td>LA13MOD</td>
383 <td>Linux / Apache 1.3.x / Module</td>
384 </tr>
385 <tr>
386 <td>LA2MOD</td>
387 <td>Linux / Apache 2.x / Module</td>
388 </tr>
389 </table>
390
391
392 </p></div>';
393 echo '<a href="index.php/arg1/arg2/arg3/#link2" name="link2">Go to this page again (PATH_INFO).</a><BR>';
394
395 phpinfo();
396 } else {
397 echo '<a href="index.php?arg1,arg2,arg3&p1=parameter1&p2[key]=value#link1" name="link1">Click this link to start the test.</a><BR>';
398 }
399 ?>