]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/config.php
Jeff's hacks II.
[SourceForge/phpwiki.git] / lib / config.php
1 <?php
2 rcs_id('$Id: config.php,v 1.41 2001-09-18 19:16:23 dairiki Exp $');
3 /*
4  * NOTE: the settings here should probably not need to be changed.
5  *
6  *
7  * (The user-configurable settings have been moved to index.php.)
8  */
9
10 if (!defined("LC_ALL")) {
11    // Backward compatibility (for PHP < 4.0.5)
12    define("LC_ALL", "LC_ALL");
13    define("LC_CTYPE", "LC_CTYPE");
14 }
15
16 // essential internal stuff
17 set_magic_quotes_runtime(0);
18
19 // Some constants.
20
21 // "\x80"-"\x9f" (and "\x00" - "\x1f") are non-printing control
22 // chars in iso-8859-*
23 // $FieldSeparator = "\263"; //this is a superscript 3 in ISO-8859-1.
24 $FieldSeparator = "\x81";
25
26 // Search PHP's include_path to find file or directory.
27 function FindFile ($file, $missing_okay = false)
28 {
29    // FIXME: This wont work for DOS filenames.
30    if (ereg('^/', $file))
31    {
32       // absolute path.
33       if (file_exists($file))
34          return $file;
35    }
36    else
37    {
38       $include_path = ini_get('include_path');
39       if (empty($include_path))
40          $include_path = '.';
41       // FIXME: This wont work for DOS filenames.
42       $path = explode(':', $include_path);
43       while (list($i, $dir) = each ($path))
44          if (file_exists("$dir/$file"))
45             return "$dir/$file";
46    }
47    
48    if (!$missing_okay)
49       ExitWiki("$file: file not found");
50    return false;
51 }
52
53 // Search PHP's include_path to find file or directory.
54 // Searches for "locale/$LANG/$file", then for "$file".
55 function FindLocalizedFile ($file, $missing_okay = false)
56 {
57    $language = $GLOBALS['LANG'];
58    
59    if (empty($language))
60       $language = getenv("LC_ALL");
61    if (empty($language))
62       $language = getenv("LC_MESSAGES");
63    if (empty($language))
64       $language = getenv("LC_RESPONSES"); // deprecated
65    if (empty($language))
66       $language = getenv("LANG");
67    if (empty($language))
68       $language = "C";
69    
70    // FIXME: This wont work for DOS filenames.
71    if (!ereg('^/', $file))
72    {
73       if ( ($path = FindFile("locale/$language/$file", 'missing_is_okay')) )
74          return $path;
75       // A locale can be, e.g. de_DE.iso8859-1@euro.
76       // Try less specific versions of the locale: 
77       $seps = array('@', '.', '_');
78       for ($i = 0; $i < count($seps); $i++)
79          if ( ($tail = strchr($language, $seps[$i])) ) {
80             $head = substr($language, 0, -strlen($tail));
81             if ( ($path = FindFile("locale/$head/$file", 'missing_is_okay')) )
82                return $path;
83          }
84    }
85    return FindFile($file, $missing_okay);
86 }
87
88 // Setup localisation
89 setlocale(LC_ALL, "$LANG");
90 putenv("LC_ALL=$LANG");
91
92 if (!function_exists ('gettext'))
93 {
94    $locale = array();
95
96    function gettext ($text) { 
97       global $locale;
98       if (!empty ($locale[$text]))
99          return $locale[$text];
100       return $text;
101    }
102
103    if ( ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok')) )
104    {
105       include($lcfile);
106    }
107 }
108 else
109 {
110    // Setup localisation
111    bindtextdomain ("phpwiki", FindFile("locale"));
112    textdomain ("phpwiki");
113 }
114
115
116
117 // To get the POSIX character classes in the PCRE's (e.g.
118 // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
119 // to set the locale, using setlocale().
120 //
121 // The problem is which locale to set?  We would like to recognize all
122 // upper-case characters in the iso-8859-1 character set as upper-case
123 // characters --- not just the ones which are in the current $LANG.
124 //
125 // As it turns out, at least on my system (Linux/glibc-2.2) as long as
126 // you setlocale() to anything but "C" it works fine.  (I'm not sure
127 // whether this is how it's supposed to be, or whether this is a bug
128 // in the libc...)
129 //
130 // We don't currently use the locale setting for anything else, so for
131 // now, just set the locale to US English.
132 //
133 // FIXME: Not all environments may support en_US?  We should probably
134 // have a list of locales to try.
135 if (setlocale(LC_CTYPE, 0) == 'C')
136    setlocale(LC_CTYPE, 'en_US.iso-8859-1');
137
138 /** string pcre_fix_posix_classes (string $regexp)
139  *
140  * Older version (pre 3.x?) of the PCRE library do not support
141  * POSIX named character classes (e.g. [[:alnum:]]).
142  *
143  * This is a helper function which can be used to convert a regexp
144  * which contains POSIX named character classes to one that doesn't.
145  *
146  * All instances of strings like '[:<class>:]' are replaced by the equivalent
147  * enumerated character class.
148  *
149  * Implementation Notes:
150  *
151  * Currently we use hard-coded values which are valid only for
152  * ISO-8859-1.  Also, currently on the classes [:alpha:], [:alnum:],
153  * [:upper:] and [:lower:] are implemented.  (The missing classes:
154  * [:blank:], [:cntrl:], [:digit:], [:graph:], [:print:], [:punct:],
155  * [:space:], and [:xdigit:] could easily be added if needed.)
156  *
157  * This is a hack.  I tried to generate these classes automatically
158  * using ereg(), but discovered that in my PHP, at least, ereg() is
159  * slightly broken w.r.t. POSIX character classes.  (It includes
160  * "\xaa" and "\xba" in [:alpha:].)
161  *
162  * So for now, this will do.  --Jeff <dairiki@dairiki.org> 14 Mar, 2001
163  */
164 function pcre_fix_posix_classes ($regexp) {
165    // First check to see if our PCRE lib supports POSIX character
166    // classes.  If it does, there's nothing to do.
167    if (preg_match('/[[:upper:]]/', 'A'))
168       return $regexp;
169
170    static $classes = array(
171       'alnum' => "0-9A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
172       'alpha' => "A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
173       'upper' => "A-Z\xc0-\xd6\xd8-\xde",
174       'lower' => "a-z\xdf-\xf6\xf8-\xff"
175       );
176
177    $keys = join('|', array_keys($classes));
178
179    return preg_replace("/\[:($keys):]/e", '$classes["\1"]', $regexp);
180 }
181          
182 $WikiNameRegexp = pcre_fix_posix_classes($WikiNameRegexp);
183
184 //////////////////////////////////////////////////////////////////
185 // Autodetect URL settings:
186 //
187 if (!defined('SERVER_NAME')) define('SERVER_NAME', $HTTP_SERVER_VARS['SERVER_NAME']);
188 if (!defined('SERVER_PORT')) define('SERVER_PORT', $HTTP_SERVER_VARS['SERVER_PORT']);
189 if (!defined('SCRIPT_NAME')) define('SCRIPT_NAME', $HTTP_SERVER_VARS['SCRIPT_NAME']);
190 if (!defined('DATA_PATH'))
191    define('DATA_PATH', dirname(SCRIPT_NAME));
192 if (!defined('USE_PATH_INFO'))
193 {
194    /*
195     * If SCRIPT_NAME does not look like php source file,
196     * or user cgi we assume that php is getting run by an
197     * action handler in /cgi-bin.  In this case,
198     * I think there is no way to get Apache to pass
199     * useful PATH_INFO to the php script (PATH_INFO
200     * is used to the the php interpreter where the
201     * php script is...)
202     */
203    if (php_sapi_name() == 'apache')
204       define('USE_PATH_INFO', true);
205    else
206       define('USE_PATH_INFO', ereg('\.(php3?|cgi)$', $SCRIPT_NAME));
207 }
208
209
210 function IsProbablyRedirectToIndex () 
211 {
212    // This might be a redirect to the DirectoryIndex,
213    // e.g. REQUEST_URI = /dir/  got redirected
214    // to SCRIPT_NAME = /dir/index.php
215    
216    // In this case, the proper virtual path is still
217    // $SCRIPT_NAME, since pages appear at
218    // e.g. /dir/index.php/HomePage.
219    
220    //global $REQUEST_URI, $SCRIPT_NAME;
221    extract($GLOBALS['HTTP_SERVER_VARS']);
222    
223    $requri = preg_quote($REQUEST_URI, '%');
224    return preg_match("%^${requri}[^/]*$%", $SCRIPT_NAME);
225 }
226
227    
228 if (!defined('VIRTUAL_PATH'))
229 {
230    // We'd like to auto-detect when the cases where apaches
231    // 'Action' directive (or similar means) is used to
232    // redirect page requests to a cgi-handler.
233    //
234    // In cases like this, requests for e.g. /wiki/HomePage
235    // get redirected to a cgi-script called, say,
236    // /path/to/wiki/index.php.  The script gets all
237    // of /wiki/HomePage as it's PATH_INFO.
238    //
239    // The problem is:
240    //   How to detect when this has happened reliably?
241    //   How to pick out the "virtual path" (in this case '/wiki')?
242    //
243    // (Another time an redirect might occur is to a DirectoryIndex
244    // -- the requested URI is '/wikidir/', the request gets
245    // passed to '/wikidir/index.php'.  In this case, the
246    // proper VIRTUAL_PATH is '/wikidir/index.php', since the
247    // pages will appear at e.g. '/wikidir/index.php/HomePage'.
248    //
249
250    $REDIRECT_URL = &$HTTP_SERVER_VARS['REDIRECT_URL'];
251    if (USE_PATH_INFO and isset($REDIRECT_URL)
252        and ! IsProbablyRedirectToIndex())
253    {
254       // FIXME: This is a hack, and won't work if the requested
255       // pagename has a slash in it.
256       define('VIRTUAL_PATH', dirname($REDIRECT_URL . 'x'));
257    }
258    else
259       define('VIRTUAL_PATH', SCRIPT_NAME);
260 }
261
262 if (SERVER_PORT && SERVER_PORT != 80)
263    define('SERVER_URL',
264           "http://" . SERVER_NAME . ':' . SERVER_PORT);
265 else
266    define('SERVER_URL',
267           "http://" . SERVER_NAME);
268
269 if (VIRTUAL_PATH != SCRIPT_NAME)
270 {
271    // Apache action handlers are used.
272    define('PATH_INFO_PREFIX', VIRTUAL_PATH . "/");
273 }
274 else
275    define("PATH_INFO_PREFIX", '/');
276
277
278 //////////////////////////////////////////////////////////////////
279 // Select database
280 //
281 if (empty($DBParams['dbtype']))
282 {
283    $DBParams['dbtype'] = 'dba';
284 }
285
286 // InterWiki linking -- wiki-style links to other wikis on the web
287 //
288 if (defined('INTERWIKI_MAP_FILE'))
289 {
290    include ('lib/interwiki.php');
291 }
292
293 // FIXME: delete
294 // Access log
295 if (!defined('ACCESS_LOG'))
296    define('ACCESS_LOG', '');
297
298 // FIXME: delete
299 // Get remote host name, if apache hasn't done it for us
300 if (empty($HTTP_SERVER_VARS['REMOTE_HOST']) && ENABLE_REVERSE_DNS)
301    $HTTP_SERVER_VARS['REMOTE_HOST'] = gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR']);
302
303    
304 // For emacs users
305 // Local Variables:
306 // mode: php
307 // c-file-style: "ellemtel"
308 // End:   
309 ?>