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