2 rcs_id('$Id: main.php,v 1.60 2002-02-21 06:08:25 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');
13 // FIXME: move to config?
14 if (defined('THEME')) {
15 include("themes/" . THEME . "/themeinfo.php");
18 include("themes/default/themeinfo.php");
20 assert(!empty($Theme));
24 function _UserPreference ($default_value) {
25 $this->default_value = $default_value;
28 function sanify ($value) {
29 return (string) $value;
33 class _UserPreference_numeric extends _UserPreference
35 function _UserPreference_numeric ($default, $minval = false, $maxval = false) {
36 $this->_UserPreference((double) $default);
37 $this->_minval = (double) $minval;
38 $this->_maxval = (double) $maxval;
41 function sanify ($value) {
42 $value = (double) $value;
43 if ($this->_minval !== false && $value < $this->_minval)
44 $value = $this->_minval;
45 if ($this->_maxval !== false && $value > $this->_maxval)
46 $value = $this->_maxval;
51 class _UserPreference_int extends _UserPreference_numeric
53 function _UserPreference_int ($default, $minval = false, $maxval = false) {
54 $this->_UserPreference_numeric((int) $default, (int)$minval, (int)$maxval);
57 function sanify ($value) {
58 return (int) parent::sanify((int)$value);
62 class _UserPreference_bool extends _UserPreference
64 function _UserPreference_bool ($default = false) {
65 $this->_UserPreference((bool) $default);
68 function sanify ($value) {
69 if (is_array($value)) {
70 /* This allows for constructs like:
72 * <input type="hidden" name="pref[boolPref][]" value="0" />
73 * <input type="checkbox" name="pref[boolPref][]" value="1" />
75 * (If the checkbox is not checked, only the hidden input gets sent.
76 * If the checkbox is sent, both inputs get sent.)
78 foreach ($value as $val) {
88 $UserPreferences = array('editWidth' => new _UserPreference_int(80, 30, 150),
89 'editHeight' => new _UserPreference_int(22, 5, 80),
90 'timeOffset' => new _UserPreference_numeric(0, -26, 26),
91 'relativeDates' => new _UserPreference_bool(),
92 'userid' => new _UserPreference(''));
94 class UserPreferences {
95 function UserPreferences ($saved_prefs = false) {
96 $this->_prefs = array();
98 if (isa($saved_prefs, 'UserPreferences')) {
99 foreach ($saved_prefs->_prefs as $name => $value)
100 $this->set($name, $value);
104 function _getPref ($name) {
105 global $UserPreferences;
106 if (!isset($UserPreferences[$name])) {
107 trigger_error("$name: unknown preference", E_USER_NOTICE);
110 return $UserPreferences[$name];
113 function get ($name) {
114 if (isset($this->_prefs[$name]))
115 return $this->_prefs[$name];
116 if (!($pref = $this->_getPref($name)))
118 return $pref->default_value;
121 function set ($name, $value) {
122 if (!($pref = $this->_getPref($name)))
124 $this->_prefs[$name] = $pref->sanify($value);
129 class WikiRequest extends Request {
131 function WikiRequest () {
135 $this->setArg('pagename', $this->_deducePagename());
136 $this->setArg('action', $this->_deduceAction());
138 // Restore auth state
139 $this->_user = new WikiUser($this->getSessionVar('wiki_user'));
141 // Restore saved preferences
142 if (!($prefs = $this->getCookieVar('WIKI_PREFS2')))
143 $prefs = $this->getSessionVar('wiki_prefs');
144 $this->_prefs = new UserPreferences($prefs);
147 // This really maybe should be part of the constructor, but since it
148 // may involve HTML/template output, the global $request really needs
149 // to be initialized before we do this stuff.
150 function updateAuthAndPrefs () {
151 // Handle preference updates, an authentication requests, if any.
152 if ($new_prefs = $this->getArg('pref')) {
153 $this->setArg('pref', false);
154 foreach ($new_prefs as $key => $val)
155 $this->_prefs->set($key, $val);
158 // Handle authentication request, if any.
159 if ($auth_args = $this->getArg('auth')) {
160 $this->setArg('auth', false);
161 $this->_handleAuthRequest($auth_args); // possible NORETURN
163 elseif ( ! $this->_user->isSignedIn() ) {
164 // If not auth request, try to sign in as saved user.
165 if (($saved_user = $this->getPref('userid')) != false)
166 $this->_signIn($saved_user);
170 $this->setSessionVar('wiki_prefs', $this->_prefs);
171 $this->setCookieVar('WIKI_PREFS2', $this->_prefs, 365);
173 // Ensure user has permissions for action
174 $require_level = $this->requiredAuthority($this->getArg('action'));
175 if (! $this->_user->hasAuthority($require_level))
176 $this->_notAuthorized($require_level); // NORETURN
179 function getUser () {
183 function getPrefs () {
184 return $this->_prefs;
187 // Convenience function:
188 function getPref ($key) {
189 return $this->_prefs->get($key);
193 if (!isset($this->_dbi)) {
194 $this->_dbi = WikiDB::open($GLOBALS['DBParams']);
200 * Get requested page from the page database.
202 * This is a convenience function.
204 function getPage () {
205 if (!isset($this->_dbi))
207 return $this->_dbi->getPage($this->getArg('pagename'));
210 function _handleAuthRequest ($auth_args) {
211 if (!is_array($auth_args))
214 // Ignore password unless POSTed.
215 if (!$this->isPost())
216 unset($auth_args['password']);
218 $user = WikiUser::AuthCheck($auth_args);
220 if (isa($user, 'WikiUser')) {
221 // Successful login (or logout.)
222 $this->_setUser($user);
225 // Login attempt failed.
226 $fail_message = $user;
227 // If no password was submitted, it's not really
228 // a failure --- just need to prompt for password...
229 if (!isset($auth_args['password']))
230 $fail_message = false;
231 WikiUser::PrintLoginForm($this, $auth_args, $fail_message);
232 $this->finish(); //NORETURN
235 // Login request cancelled.
240 * Attempt to sign in (bogo-login).
244 * @param $userid string Userid to attempt to sign in as.
247 function _signIn ($userid) {
248 $user = WikiUser::AuthCheck(array('userid' => $userid));
249 if (isa($user, 'WikiUser'))
250 $this->_setUser($user); // success!
253 function _setUser ($user) {
254 $this->_user = $user;
255 $this->setSessionVar('wiki_user', $user);
257 // Save userid to prefs..
258 $this->_prefs->set('userid',
259 $user->isSignedIn() ? $user->getId() : '');
262 function _notAuthorized ($require_level) {
263 // User does not have required authority. Prompt for login.
264 $what = HTML::em($this->getArg('action'));
266 if ($require_level >= WIKIAUTH_FORBIDDEN) {
267 $this->finish(fmt("Action %s is disallowed on this wiki", $what));
269 elseif ($require_level == WIKIAUTH_BOGO)
270 $msg = fmt("You must sign in to %s this wiki", $what);
271 elseif ($require_level == WIKIAUTH_USER)
272 $msg = fmt("You must log in to %s this wiki", $what);
274 $msg = fmt("You must be an administrator to %s this wiki", $what);
276 WikiUser::PrintLoginForm($this, compact('require_level'), $msg);
277 $this->finish(); // NORETURN
280 function requiredAuthority ($action) {
286 return WIKIAUTH_ANON;
289 if (defined('ZIPDUMP_AUTH') && ZIPDUMP_AUTH)
290 return WIKIAUTH_ADMIN;
291 return WIKIAUTH_ANON;
294 if (defined('REQUIRE_SIGNIN_BEFORE_EDIT') && REQUIRE_SIGNIN_BEFORE_EDIT)
295 return WIKIAUTH_BOGO;
296 return WIKIAUTH_ANON;
297 // return WIKIAUTH_BOGO;
306 return WIKIAUTH_ADMIN;
308 // Temp workaround for french single-word action page 'Historique'
309 if (_("PageHistory") == $action)
310 return WIKIAUTH_ANON; // ActionPage.
311 global $WikiNameRegexp;
312 if (preg_match("/$WikiNameRegexp\Z/A", $action))
313 return WIKIAUTH_ANON; // ActionPage.
315 return WIKIAUTH_ADMIN;
319 function possiblyDeflowerVirginWiki () {
320 if ($this->getArg('action') != 'browse')
322 if ($this->getArg('pagename') != HomePage)
325 $page = $this->getPage();
326 $current = $page->getCurrentRevision();
327 if ($current->getVersion() > 0)
328 return; // Homepage exists.
330 include('lib/loadsave.php');
332 $this->finish(); // NORETURN
335 function handleAction () {
336 $action = $this->getArg('action');
337 $method = "action_$action";
338 if (method_exists($this, $method)) {
341 elseif ($this->isActionPage($action)) {
342 $this->actionpage($action);
345 $this->finish(fmt("%s: Bad action", $action));
350 function finish ($errormsg = false) {
354 exit(); // just in case CloseDataBase calls us
357 if (!empty($this->_dbi))
358 $this->_dbi->close();
362 global $ErrorManager;
363 $ErrorManager->flushPostponedErrors();
365 if (!empty($errormsg)) {
368 HTML::h2(_("Fatal PhpWiki Error")),
371 echo "\n</body></html>";
378 function _deducePagename () {
379 if ($this->getArg('pagename'))
380 return $this->getArg('pagename');
383 $pathinfo = $this->get('PATH_INFO');
384 $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
386 if ($tail && $pathinfo == PATH_INFO_PREFIX . $tail) {
391 $query_string = $this->get('QUERY_STRING');
392 if (preg_match('/^[^&=]+$/', $query_string)) {
393 return urldecode($query_string);
399 function _deduceAction () {
400 if (!($action = $this->getArg('action')))
403 if (method_exists($this, "action_$action"))
406 // Allow for, e.g. action=LikePages
407 if ($this->isActionPage($action))
410 trigger_error("$action: Unknown action", E_USER_NOTICE);
414 function isActionPage ($pagename) {
415 // Temp workaround for french single-word action page 'Historique'
416 $singleWordActionPages = array("Historique", "Info");
417 if (! in_array($pagename, $singleWordActionPages)) {
418 // Allow for, e.g. action=LikePages
419 global $WikiNameRegexp;
420 if (!preg_match("/$WikiNameRegexp\\Z/A", $pagename))
423 $dbi = $this->getDbh();
424 $page = $dbi->getPage($pagename);
425 $rev = $page->getCurrentRevision();
426 // FIXME: more restrictive check for sane plugin?
427 if (strstr($rev->getPackedContent(), '<?plugin'))
429 trigger_error("$pagename: Does not appear to be an 'action page'", E_USER_NOTICE);
433 function action_browse () {
434 $this->compress_output();
435 include_once("lib/display.php");
439 function actionpage ($action) {
440 $this->compress_output();
441 include_once("lib/display.php");
442 actionPage($this, $action);
445 function action_diff () {
446 $this->compress_output();
447 include_once "lib/diff.php";
451 function action_search () {
452 // This is obsolete: reformulate URL and redirect.
453 // FIXME: this whole section should probably be deleted.
454 if ($this->getArg('searchtype') == 'full') {
455 $search_page = _("FullTextSearch");
458 $search_page = _("TitleSearch");
460 $this->redirect(WikiURL($search_page,
461 array('s' => $this->getArg('searchterm')),
465 function action_edit () {
466 $this->compress_output();
467 include "lib/editpage.php";
468 $e = new PageEditor ($this);
472 function action_viewsource () {
473 $this->compress_output();
474 include "lib/editpage.php";
475 $e = new PageEditor ($this);
479 function action_lock () {
480 $page = $this->getPage();
481 $page->set('locked', true);
482 $this->action_browse();
485 function action_unlock () {
486 // FIXME: This check is redundant.
487 //$user->requireAuth(WIKIAUTH_ADMIN);
488 $page = $this->getPage();
489 $page->set('locked', false);
490 $this->action_browse();
493 function action_remove () {
494 // FIXME: This check is redundant.
495 //$user->requireAuth(WIKIAUTH_ADMIN);
496 include('lib/removepage.php');
501 function action_upload () {
502 include_once("lib/loadsave.php");
506 function action_zip () {
507 include_once("lib/loadsave.php");
509 // I don't think it hurts to add cruft at the end of the zip file.
510 echo "\n========================================================\n";
511 echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
514 function action_dumpserial () {
515 include_once("lib/loadsave.php");
519 function action_dumphtml () {
520 include_once("lib/loadsave.php");
521 DumpHtmlToDir($this);
524 function action_loadfile () {
525 include_once("lib/loadsave.php");
526 LoadFileOrDir($this);
531 function is_safe_action ($action) {
532 return WikiRequest::requiredAuthority($action) < WIKIAUTH_ADMIN;
539 $request = new WikiRequest();
540 $request->updateAuthAndPrefs();
542 /* FIXME: is this needed anymore?
543 if (USE_PATH_INFO && ! $request->get('PATH_INFO')
544 && ! preg_match(',/$,', $request->get('REDIRECT_URL'))) {
545 $request->redirect(SERVER_URL
546 . preg_replace('/(\?|$)/', '/\1',
547 $request->get('REQUEST_URI'),
553 // Enable the output of most of the warning messages.
554 // The warnings will screw up zip files though.
555 global $ErrorManager;
556 if ($request->getArg('action') != 'zip') {
557 $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
558 //$ErrorManager->setPostponedErrorMask(0);
562 //if ($user->is_authenticated())
563 // $LogEntry->user = $user->getId();
565 $request->possiblyDeflowerVirginWiki();
567 $request->handleAction();
571 // Used for debugging purposes
572 function getmicrotime(){
573 list($usec, $sec) = explode(" ", microtime());
574 return ((float)$usec + (float)$sec);
576 if (defined ('DEBUG')) $GLOBALS['debugclock'] = getmicrotime();
585 // c-hanging-comment-ender-p: nil
586 // indent-tabs-mode: nil