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