2 rcs_id('$Id: main.php,v 1.78 2002-09-12 17:35:39 rurban Exp $');
4 define ('USE_PREFS_IN_PAGE', true);
6 include "lib/config.php";
7 include "lib/stdlib.php";
8 require_once('lib/Request.php');
9 require_once("lib/WikiUser.php");
10 require_once('lib/WikiDB.php');
13 require_once('lib/DB_Session.php');
16 class WikiRequest extends Request {
19 function WikiRequest () {
22 $this->_dbi = $this->getDbh();
23 new DB_Session($this->_dbi->_backend->_dbh, $GLOBALS['DBParams']['db_session_table']);
26 $this->session = new Request_SessionVars;
29 $this->setArg('pagename', $this->_deducePagename());
30 $this->setArg('action', $this->_deduceAction());
33 $this->_user = new WikiUser($this->_deduceUsername());
34 $this->_deduceUsername();
36 // $this->_user = new WikiDB_User($this->getSessionVar('wiki_user'), $this->getAuthDbh());
37 $this->_prefs = $this->_user->getPreferences();
40 // This really maybe should be part of the constructor, but since it
41 // may involve HTML/template output, the global $request really needs
42 // to be initialized before we do this stuff.
43 function updateAuthAndPrefs () {
45 // Handle preference updates, an authentication requests, if any.
46 if ($new_prefs = $this->getArg('pref')) {
47 $this->setArg('pref', false);
48 if ($this->isPost() and !empty($new_prefs['passwd']) and
49 ($new_prefs['passwd2'] != $new_prefs['passwd'])) {
50 $this->_prefs->set('passwd','');
51 // $this->_prefs->set('passwd2',''); // This is not stored anyway
52 include_once("themes/" . THEME . "/themeinfo.php");
55 foreach ($new_prefs as $key => $val) {
56 if ($key == 'passwd') {
57 $val = crypt('passwd');
59 $this->_prefs->set($key, $val);
63 // Handle authentication request, if any.
64 if ($auth_args = $this->getArg('auth')) {
65 $this->setArg('auth', false);
66 include_once("themes/" . THEME . "/themeinfo.php");
67 $this->_handleAuthRequest($auth_args); // possible NORETURN
69 elseif ( ! $this->_user->isSignedIn() ) {
70 // If not auth request, try to sign in as saved user.
71 if (($saved_user = $this->getPref('userid')) != false) {
72 include_once("themes/" . THEME . "/themeinfo.php");
73 $this->_signIn($saved_user);
77 // Save preferences in session and cookie
79 $this->_user->setPreferences($this->_prefs, $id_only);
81 if ($theme = $this->getPref('theme') ) {
82 // Load user-defined theme
83 include_once("themes/$theme/themeinfo.php");
86 include_once("themes/" . THEME . "/themeinfo.php");
90 include_once("themes/" . THEME . "/themeinfo.php");
93 include_once("themes/default/themeinfo.php");
95 assert(!empty($Theme));
97 // Ensure user has permissions for action
98 $require_level = $this->requiredAuthority($this->getArg('action'));
99 if (! $this->_user->hasAuthority($require_level))
100 $this->_notAuthorized($require_level); // NORETURN
103 function getUser () {
107 function getPrefs () {
108 return $this->_prefs;
111 // Convenience function:
112 function getPref ($key) {
113 return $this->_prefs->get($key);
117 if (!isset($this->_dbi)) {
118 // needs PHP 4.1. better use $this->_user->...
119 $this->_dbi = WikiDB::open($GLOBALS['DBParams']);
124 function getAuthDbh () {
125 global $DBParams, $DBAuthParams;
126 if (!isset($this->_auth_dbi)) {
127 if ($DBParams['dbtype'] == 'dba' or empty($DBAuthParams['auth_dsn']))
128 $this->_auth_dbi = $this->getDbh(); // use phpwiki database
129 elseif ($DBAuthParams['auth_dsn'] == $DBParams['dsn'])
130 $this->_auth_dbi = $this->getDbh(); // same phpwiki database
131 else // use external database
132 // needs PHP 4.1. better use $this->_user->...
133 $this->_auth_dbi = WikiDB_User::open($DBAuthParams);
135 return $this->_auth_dbi;
139 * Get requested page from the page database.
141 * This is a convenience function.
143 function getPage () {
144 if (!isset($this->_dbi))
146 return $this->_dbi->getPage($this->getArg('pagename'));
149 function _handleAuthRequest ($auth_args) {
150 if (!is_array($auth_args))
153 // Ignore password unless POST'ed.
154 if (!$this->isPost())
155 unset($auth_args['passwd']);
157 $user = $this->_user->AuthCheck($auth_args);
159 if (isa($user, 'WikiUser')) {
160 // Successful login (or logout.)
161 $this->_setUser($user);
164 // Login attempt failed.
165 $fail_message = $user;
166 $auth_args['pass_required'] = true;
167 // If no password was submitted, it's not really
168 // a failure --- just need to prompt for password...
169 if (!isset($auth_args['passwd'])) {
170 //$auth_args['pass_required'] = false;
171 $fail_message = false;
173 $this->_user->PrintLoginForm($this, $auth_args, $fail_message);
174 $this->finish(); //NORETURN
177 // Login request cancelled.
182 * Attempt to sign in (bogo-login).
186 * @param $userid string Userid to attempt to sign in as.
189 function _signIn ($userid) {
190 $user = $this->_user->AuthCheck(array('userid' => $userid));
191 if (isa($user, 'WikiUser')) {
192 $this->_setUser($user); // success!
196 function _setUser ($user) {
197 $this->_user = $user;
198 $this->setCookieVar('WIKI_ID', $user->_userid, 365);
199 $this->setSessionVar('wiki_user', $user);
200 if ($user->isSignedIn())
201 $user->_authhow = 'signin';
203 // Save userid to prefs..
204 $this->_prefs->set('userid',
205 $user->isSignedIn() ? $user->getId() : '');
208 function _notAuthorized ($require_level) {
209 // User does not have required authority. Prompt for login.
210 $what = $this->getActionDescription($this->getArg('action'));
212 if ($require_level >= WIKIAUTH_FORBIDDEN) {
213 $this->finish(fmt("%s is disallowed on this wiki.",
214 $this->getDisallowedActionDescription($this->getArg('action'))));
216 elseif ($require_level == WIKIAUTH_BOGO)
217 $msg = fmt("You must sign in to %s.", $what);
218 elseif ($require_level == WIKIAUTH_USER)
219 $msg = fmt("You must log in to %s.", $what);
221 $msg = fmt("You must be an administrator to %s.", $what);
222 $pass_required = ($require_level >= WIKIAUTH_USER);
224 $this->_user->PrintLoginForm($this, compact('require_level','pass_required'), $msg);
225 $this->finish(); // NORETURN
228 function getActionDescription($action) {
229 static $actionDescriptions;
230 if (! $actionDescriptions) {
232 = array('browse' => _("browse pages in this wiki"),
233 'diff' => _("diff pages in this wiki"),
234 'dumphtml' => _("dump html pages from this wiki"),
235 'dumpserial' => _("dump serial pages from this wiki"),
236 'edit' => _("edit pages in this wiki"),
237 'loadfile' => _("load files into this wiki"),
238 'lock' => _("lock pages in this wiki"),
239 'remove' => _("remove pages from this wiki"),
240 'unlock' => _("unlock pages in this wiki"),
241 'upload' => _("upload a zip dump to this wiki"),
242 'verify' => _("verify the current action"),
243 'viewsource' => _("view the source of pages in this wiki"),
244 'zip' => _("download a zip dump from this wiki"),
245 'ziphtml' => _("download an html zip dump from this wiki")
248 if (in_array($action, array_keys($actionDescriptions)))
249 return $actionDescriptions[$action];
253 function getDisallowedActionDescription($action) {
254 static $disallowedActionDescriptions;
255 if (! $disallowedActionDescriptions) {
256 $disallowedActionDescriptions
257 = array('browse' => _("Browsing pages"),
258 'diff' => _("Diffing pages"),
259 'dumphtml' => _("Dumping html pages"),
260 'dumpserial' => _("Dumping serial pages"),
261 'edit' => _("Editing pages"),
262 'loadfile' => _("Loading files"),
263 'lock' => _("Locking pages"),
264 'remove' => _("Removing pages"),
265 'unlock' => _("Unlocking pages"),
266 'upload' => _("Uploading zip dumps"),
267 'verify' => _("Verify the current action"),
268 'viewsource' => _("Viewing the source of pages"),
269 'zip' => _("Downloading zip dumps"),
270 'ziphtml' => _("Downloading html zip dumps")
273 if (in_array($action, array_keys($disallowedActionDescriptions)))
274 return $disallowedActionDescriptions[$action];
279 function requiredAuthority ($action) {
281 // Todo: Check individual page permissions instead.
287 return WIKIAUTH_ANON;
290 if (defined('ZIPDUMP_AUTH') && ZIPDUMP_AUTH)
291 return WIKIAUTH_ADMIN;
292 return WIKIAUTH_ANON;
295 if (defined('ZIPDUMP_AUTH') && ZIPDUMP_AUTH)
296 return WIKIAUTH_ADMIN;
297 return WIKIAUTH_ANON;
300 if (defined('REQUIRE_SIGNIN_BEFORE_EDIT') && REQUIRE_SIGNIN_BEFORE_EDIT)
301 return WIKIAUTH_BOGO;
302 return WIKIAUTH_ANON;
303 // return WIKIAUTH_BOGO;
312 return WIKIAUTH_ADMIN;
314 // Temp workaround for french single-word action pages 'Historique'
315 // Some of this make sense as SubPage actions or buttons.
316 $singleWordActionPages =
317 array("Historique", "Info",
318 _("Preferences"), _("Administration"),
319 _("Today"), _("Help"));
320 if (in_array($action, $singleWordActionPages))
321 return WIKIAUTH_ANON; // ActionPage.
322 global $WikiNameRegexp;
323 if (preg_match("/$WikiNameRegexp\Z/A", $action))
324 return WIKIAUTH_ANON; // ActionPage.
326 return WIKIAUTH_ADMIN;
330 function possiblyDeflowerVirginWiki () {
331 if ($this->getArg('action') != 'browse')
333 if ($this->getArg('pagename') != HOME_PAGE)
336 $page = $this->getPage();
337 $current = $page->getCurrentRevision();
338 if ($current->getVersion() > 0)
339 return; // Homepage exists.
341 include('lib/loadsave.php');
343 $this->finish(); // NORETURN
346 function handleAction () {
347 $action = $this->getArg('action');
348 $method = "action_$action";
349 if (method_exists($this, $method)) {
352 elseif ($this->isActionPage($action)) {
353 $this->actionpage($action);
356 $this->finish(fmt("%s: Bad action", $action));
361 function finish ($errormsg = false) {
365 exit(); // just in case CloseDataBase calls us
368 if (!empty($this->_dbi))
369 $this->_dbi->close();
373 global $ErrorManager;
374 $ErrorManager->flushPostponedErrors();
376 if (!empty($errormsg)) {
379 HTML::h2(_("Fatal PhpWiki Error")),
382 echo "\n</body></html>";
389 function _deducePagename () {
390 if ($this->getArg('pagename'))
391 return $this->getArg('pagename');
394 $pathinfo = $this->get('PATH_INFO');
395 $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
397 if ($tail && $pathinfo == PATH_INFO_PREFIX . $tail) {
402 $query_string = $this->get('QUERY_STRING');
403 if (preg_match('/^[^&=]+$/', $query_string)) {
404 return urldecode($query_string);
410 function _deduceAction () {
411 if (!($action = $this->getArg('action')))
414 if (method_exists($this, "action_$action"))
417 // Allow for, e.g. action=LikePages
418 if ($this->isActionPage($action))
421 // Check for _('PhpWikiAdministration').'/'._('Remove') actions
422 $pagename = $this->getArg('pagename');
423 if (strstr($pagename,_('PhpWikiAdministration')))
426 trigger_error("$action: Unknown action", E_USER_NOTICE);
430 function _deduceUsername () {
431 if ($userid = $this->getSessionVar('wiki_user')) {
432 if (!empty($this->_user))
433 $this->_user->_authhow = 'session';
436 if ($userid = $this->getCookieVar('WIKI_ID')) {
437 if (!empty($this->_user))
438 $this->_user->authhow = 'cookie';
444 function isActionPage ($pagename) {
445 if (isSubPage($pagename))
446 $subpagename = subPageSlice($pagename,-1); // last element
448 $subpagename = $pagename;
449 // Temp workaround for french single-word action page 'Historique'
450 $singleWordActionPages = array("Historique", "Info", _('Preferences'));
451 if (! in_array($subpagename, $singleWordActionPages)) {
452 // Allow for, e.g. action=LikePages
453 global $WikiNameRegexp;
454 if (!preg_match("/$WikiNameRegexp\\Z/A", $subpagename))
457 $dbi = $this->getDbh();
458 $page = $dbi->getPage($pagename);
459 $rev = $page->getCurrentRevision();
460 // FIXME: more restrictive check for sane plugin?
461 if (strstr($rev->getPackedContent(), '<?plugin'))
463 trigger_error("$pagename: Does not appear to be an 'action page'", E_USER_NOTICE);
467 function action_browse () {
468 $this->compress_output();
469 include_once("lib/display.php");
473 function action_verify () {
474 $this->action_browse();
477 function actionpage ($action) {
478 $this->compress_output();
479 include_once("lib/display.php");
480 actionPage($this, $action);
483 function action_diff () {
484 $this->compress_output();
485 include_once "lib/diff.php";
489 function action_search () {
490 // This is obsolete: reformulate URL and redirect.
491 // FIXME: this whole section should probably be deleted.
492 if ($this->getArg('searchtype') == 'full') {
493 $search_page = _("FullTextSearch");
496 $search_page = _("TitleSearch");
498 $this->redirect(WikiURL($search_page,
499 array('s' => $this->getArg('searchterm')),
503 function action_edit () {
504 $this->compress_output();
505 include "lib/editpage.php";
506 $e = new PageEditor ($this);
510 function action_viewsource () {
511 $this->compress_output();
512 include "lib/editpage.php";
513 $e = new PageEditor ($this);
517 function action_lock () {
518 $page = $this->getPage();
519 $page->set('locked', true);
520 $this->action_browse();
523 function action_unlock () {
524 // FIXME: This check is redundant.
525 //$user->requireAuth(WIKIAUTH_ADMIN);
526 $page = $this->getPage();
527 $page->set('locked', false);
528 $this->action_browse();
531 function action_remove () {
532 // FIXME: This check is redundant.
533 //$user->requireAuth(WIKIAUTH_ADMIN);
534 $pagename = $this->getArg('pagename');
535 if (strstr($pagename,_('PhpWikiAdministration'))) {
536 $this->action_browse();
538 include('lib/removepage.php');
544 function action_upload () {
545 include_once("lib/loadsave.php");
549 function action_zip () {
550 include_once("lib/loadsave.php");
552 // I don't think it hurts to add cruft at the end of the zip file.
553 echo "\n========================================================\n";
554 echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
557 function action_ziphtml () {
558 include_once("lib/loadsave.php");
559 MakeWikiZipHtml($this);
560 // I don't think it hurts to add cruft at the end of the zip file.
561 echo "\n========================================================\n";
562 echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
565 function action_dumpserial () {
566 include_once("lib/loadsave.php");
570 function action_dumphtml () {
571 include_once("lib/loadsave.php");
572 DumpHtmlToDir($this);
575 function action_loadfile () {
576 include_once("lib/loadsave.php");
577 LoadFileOrDir($this);
582 function is_safe_action ($action) {
583 return WikiRequest::requiredAuthority($action) < WIKIAUTH_ADMIN;
590 $request = new WikiRequest();
591 $request->updateAuthAndPrefs();
593 /* FIXME: is this needed anymore?
594 if (USE_PATH_INFO && ! $request->get('PATH_INFO')
595 && ! preg_match(',/$,', $request->get('REDIRECT_URL'))) {
596 $request->redirect(SERVER_URL
597 . preg_replace('/(\?|$)/', '/\1',
598 $request->get('REQUEST_URI'),
604 // Enable the output of most of the warning messages.
605 // The warnings will screw up zip files though.
606 global $ErrorManager;
607 if (substr($request->getArg('action'), 0, 3) != 'zip') {
608 $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
609 //$ErrorManager->setPostponedErrorMask(0);
613 //if ($user->is_authenticated())
614 // $LogEntry->user = $user->getId();
616 $request->possiblyDeflowerVirginWiki();
617 if(defined('WIKI_XMLRPC')) return;
618 $request->handleAction();
619 if (defined('DEBUG') and DEBUG>1) phpinfo(INFO_VARIABLES);
623 // Used for debugging purposes
624 function getmicrotime(){
625 list($usec, $sec) = explode(" ", microtime());
626 return ((float)$usec + (float)$sec);
628 if (defined('DEBUG')) $GLOBALS['debugclock'] = getmicrotime();
637 // c-hanging-comment-ender-p: nil
638 // indent-tabs-mode: nil