2 rcs_id('$Id: main.php,v 1.50 2002-02-07 04:11:44 carstenklapp Exp $');
5 include "lib/config.php";
6 include "lib/stdlib.php";
7 require_once('lib/Request.php');
8 require_once("lib/WikiUser.php");
9 require_once('lib/WikiDB.php');
11 // FIXME: move to config?
12 if (defined('THEME')) {
13 include("themes/" . THEME . "/themeinfo.php");
16 include("themes/default/themeinfo.php");
18 assert(!empty($Theme));
22 function _UserPreference ($default_value) {
23 $this->default_value = $default_value;
26 function sanify ($value) {
27 return (string) $value;
31 class _UserPreference_int extends _UserPreference
33 function _UserPreference_int ($default, $minval = false, $maxval = false) {
34 $this->_UserPreference((int) $default);
35 $this->_minval = (int) $minval;
36 $this->_maxval = (int) $maxval;
39 function sanify ($value) {
40 $value = (int) $value;
41 if ($this->_minval !== false && $value < $this->_minval)
42 $value = $this->_minval;
43 if ($this->_maxval !== false && $value > $this->_maxval)
44 $value = $this->_maxval;
49 $UserPreferences = array('editWidth' => new _UserPreference_int(80, 30, 150),
50 'editHeight' => new _UserPreference_int(22, 5, 80),
51 'timeOffset' => new _UserPreference(TimezoneOffset(false, true)),
52 'relativeDates' => new _UserPreference(false),
53 'userid' => new _UserPreference(''));
55 class UserPreferences {
56 function UserPreferences ($saved_prefs = false) {
57 $this->_prefs = array();
59 if (isa($saved_prefs, 'UserPreferences')) {
60 foreach ($saved_prefs->_prefs as $name => $value)
61 $this->set($name, $value);
65 function _getPref ($name) {
66 global $UserPreferences;
67 if (!isset($UserPreferences[$name])) {
68 trigger_error("$name: unknown preference", E_USER_NOTICE);
71 return $UserPreferences[$name];
74 function get ($name) {
75 if (isset($this->_prefs[$name]))
76 return $this->_prefs[$name];
77 if (!($pref = $this->_getPref($name)))
79 return $pref->default_value;
82 function set ($name, $value) {
83 if (!($pref = $this->_getPref($name)))
85 $this->_prefs[$name] = $pref->sanify($value);
90 class WikiRequest extends Request {
92 function WikiRequest () {
96 $this->setArg('pagename', $this->_deducePagename());
97 $this->setArg('action', $this->_deduceAction());
100 $this->_user = new WikiUser($this->getSessionVar('wiki_user'));
102 // Restore saved preferences
103 if (!($prefs = $this->getCookieVar('WIKI_PREFS2')))
104 $prefs = $this->getSessionVar('wiki_prefs');
105 $this->_prefs = new UserPreferences($prefs);
108 // This really maybe should be part of the constructor, but since it
109 // may involve HTML/template output, the global $request really needs
110 // to be initialized before we do this stuff.
111 function updateAuthAndPrefs () {
112 // Handle preference updates, an authentication requests, if any.
113 if ($new_prefs = $this->getArg('pref')) {
114 $this->setArg('pref', false);
115 foreach ($new_prefs as $key => $val)
116 $this->_prefs->set($key, $val);
119 // Handle authentication request, if any.
120 if ($auth_args = $this->getArg('auth')) {
121 $this->setArg('auth', false);
122 $this->_handleAuthRequest($auth_args); // possible NORETURN
124 elseif ( ! $this->_user->isSignedIn() ) {
125 // If not auth request, try to sign in as saved user.
126 if (($saved_user = $this->getPref('userid')) != false)
127 $this->_signIn($saved_user);
131 $this->setSessionVar('wiki_prefs', $this->_prefs);
132 $this->setCookieVar('WIKI_PREFS2', $this->_prefs, 365);
134 // Ensure user has permissions for action
135 $require_level = $this->requiredAuthority($this->getArg('action'));
136 if (! $this->_user->hasAuthority($require_level))
137 $this->_notAuthorized($require_level); // NORETURN
140 function getUser () {
144 function getPrefs () {
145 return $this->_prefs;
148 // Convenience function:
149 function getPref ($key) {
150 return $this->_prefs->get($key);
154 if (!isset($this->_dbi)) {
155 $this->_dbi = WikiDB::open($GLOBALS['DBParams']);
161 * Get requested page from the page database.
163 * This is a convenience function.
165 function getPage () {
166 if (!isset($this->_dbi))
168 return $this->_dbi->getPage($this->getArg('pagename'));
171 function _handleAuthRequest ($auth_args) {
172 if (!is_array($auth_args))
175 // Ignore password unless POSTed.
176 if (!$this->isPost())
177 unset($auth_args['password']);
179 $user = WikiUser::AuthCheck($auth_args);
181 if (isa($user, 'WikiUser')) {
182 // Successful login (or logout.)
183 $this->_setUser($user);
186 // Login attempt failed.
187 $fail_message = $user;
188 // If no password was submitted, it's not really
189 // a failure --- just need to prompt for password...
190 if (!isset($auth_args['password']))
191 $fail_message = false;
192 WikiUser::PrintLoginForm($this, $auth_args, $fail_message);
193 $this->finish(); //NORETURN
196 // Login request cancelled.
201 * Attempt to sign in (bogo-login).
205 * @param $userid string Userid to attempt to sign in as.
208 function _signIn ($userid) {
209 $user = WikiUser::AuthCheck(array('userid' => $userid));
210 if (isa($user, 'WikiUser'))
211 $this->_setUser($user); // success!
214 function _setUser ($user) {
215 $this->_user = $user;
216 $this->setSessionVar('wiki_user', $user);
218 // Save userid to prefs..
219 $this->_prefs->set('userid',
220 $user->isSignedIn() ? $user->getId() : '');
223 function _notAuthorized ($require_level) {
224 // User does not have required authority. Prompt for login.
225 $what = HTML::em($this->getArg('action'));
227 if ($require_level >= WIKIAUTH_FORBIDDEN) {
228 $this->finish(fmt("Action %s is disallowed on this wiki", $what));
230 elseif ($require_level == WIKIAUTH_BOGO)
231 $msg = fmt("You must sign in to %s this wiki", $what);
232 elseif ($require_level == WIKIAUTH_USER)
233 $msg = fmt("You must log in to %s this wiki", $what);
235 $msg = fmt("You must be an administrator to %s this wiki", $what);
237 WikiUser::PrintLoginForm($this, compact('require_level'), $msg);
238 $this->finish(); // NORETURN
241 function requiredAuthority ($action) {
247 return WIKIAUTH_ANON;
250 if (defined('ZIPDUMP_AUTH') && ZIPDUMP_AUTH)
251 return WIKIAUTH_ADMIN;
252 return WIKIAUTH_ANON;
255 if (defined('REQUIRE_SIGNIN_BEFORE_EDIT') && REQUIRE_SIGNIN_BEFORE_EDIT)
256 return WIKIAUTH_BOGO;
257 return WIKIAUTH_ANON;
258 // return WIKIAUTH_BOGO;
266 return WIKIAUTH_ADMIN;
268 global $WikiNameRegexp;
269 if (preg_match("/$WikiNameRegexp\Z/A", $action))
270 return WIKIAUTH_ANON; // ActionPage.
272 return WIKIAUTH_ADMIN;
276 function possiblyDeflowerVirginWiki () {
277 if ($this->getArg('action') != 'browse')
279 if ($this->getArg('pagename') != HomePage)
282 $page = $this->getPage();
283 $current = $page->getCurrentRevision();
284 if ($current->getVersion() > 0)
285 return; // Homepage exists.
287 include('lib/loadsave.php');
289 $this->finish(); // NORETURN
292 function handleAction () {
293 $action = $this->getArg('action');
294 $method = "action_$action";
295 if (method_exists($this, $method)) {
298 elseif ($this->isActionPage($action)) {
299 $this->actionpage($action);
302 $this->finish(fmt("%s: Bad action", $action));
307 function finish ($errormsg = false) {
311 exit(); // just in case CloseDataBase calls us
314 if (!empty($this->_dbi))
315 $this->_dbi->close();
319 global $ErrorManager;
320 $ErrorManager->flushPostponedErrors();
322 if (!empty($errormsg)) {
325 HTML::h2(_("Fatal PhpWiki Error")),
328 echo "\n</body></html>";
335 function _deducePagename () {
336 if ($this->getArg('pagename'))
337 return $this->getArg('pagename');
340 $pathinfo = $this->get('PATH_INFO');
341 $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
343 if ($tail && $pathinfo == PATH_INFO_PREFIX . $tail) {
348 $query_string = $this->get('QUERY_STRING');
349 if (preg_match('/^[^&=]+$/', $query_string)) {
350 return urldecode($query_string);
356 function _deduceAction () {
357 if (!($action = $this->getArg('action')))
360 if (method_exists($this, "action_$action"))
363 // Allow for, e.g. action=LikePages
364 if ($this->isActionPage($action))
367 trigger_error("$action: Unknown action", E_USER_NOTICE);
371 function isActionPage ($pagename) {
372 // Allow for, e.g. action=LikePages
373 global $WikiNameRegexp;
374 if (!preg_match("/$WikiNameRegexp\\Z/A", $pagename))
376 $dbi = $this->getDbh();
377 $page = $dbi->getPage($pagename);
378 $rev = $page->getCurrentRevision();
379 // FIXME: more restrictive check for sane plugin?
380 if (strstr($rev->getPackedContent(), '<?plugin'))
382 trigger_error("$pagename: Does not appear to be an 'action page'", E_USER_NOTICE);
386 function action_browse () {
387 $this->compress_output();
388 include_once("lib/display.php");
392 function actionpage ($action) {
393 $this->compress_output();
394 include_once("lib/display.php");
395 actionPage($this, $action);
398 function action_diff () {
399 $this->compress_output();
400 include_once "lib/diff.php";
404 function action_search () {
405 // This is obsolete: reformulate URL and redirect.
406 // FIXME: this whole section should probably be deleted.
407 if ($this->getArg('searchtype') == 'full') {
408 $search_page = _("FullTextSearch");
411 $search_page = _("TitleSearch");
413 $this->redirect(WikiURL($search_page,
414 array('s' => $this->getArg('searchterm')),
418 function action_edit () {
419 $this->compress_output();
420 include "lib/editpage.php";
421 $e = new PageEditor ($this);
425 function action_viewsource () {
426 $this->compress_output();
427 include "lib/editpage.php";
428 $e = new PageEditor ($this);
432 function action_lock () {
433 $page = $this->getPage();
434 $page->set('locked', true);
435 $this->action_browse();
438 function action_unlock () {
439 // FIXME: This check is redundant.
440 //$user->requireAuth(WIKIAUTH_ADMIN);
441 $page = $this->getPage();
442 $page->set('locked', false);
443 $this->action_browse();
446 function action_remove () {
447 // FIXME: This check is redundant.
448 //$user->requireAuth(WIKIAUTH_ADMIN);
449 include('lib/removepage.php');
454 function action_upload () {
455 include_once("lib/loadsave.php");
459 function action_zip () {
460 include_once("lib/loadsave.php");
462 // I don't think it hurts to add cruft at the end of the zip file.
463 echo "\n========================================================\n";
464 echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
467 function action_dumpserial () {
468 include_once("lib/loadsave.php");
472 function action_loadfile () {
473 include_once("lib/loadsave.php");
474 LoadFileOrDir($this);
479 function is_safe_action ($action) {
480 return WikiRequest::requiredAuthority($action) < WIKIAUTH_ADMIN;
487 $request = new WikiRequest();
488 $request->updateAuthAndPrefs();
490 /* FIXME: is this needed anymore?
491 if (USE_PATH_INFO && ! $request->get('PATH_INFO')
492 && ! preg_match(',/$,', $request->get('REDIRECT_URL'))) {
493 $request->redirect(SERVER_URL
494 . preg_replace('/(\?|$)/', '/\1',
495 $request->get('REQUEST_URI'),
501 // Enable the output of most of the warning messages.
502 // The warnings will screw up zip files though.
503 global $ErrorManager;
504 if ($request->getArg('action') != 'zip') {
505 $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
506 //$ErrorManager->setPostponedErrorMask(0);
510 //if ($user->is_authenticated())
511 // $LogEntry->user = $user->getId();
513 $request->possiblyDeflowerVirginWiki();
515 $request->handleAction();
519 // Used for debugging purposes
520 function getmicrotime(){
521 list($usec, $sec) = explode(" ", microtime());
522 return ((float)$usec + (float)$sec);
525 if (defined ('DEBUG')) $GLOBALS['debugclock'] = getmicrotime();
534 // c-hanging-comment-ender-p: nil
535 // indent-tabs-mode: nil