2 rcs_id('$Id: main.php,v 1.52 2002-02-08 15:37:04 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 class _UserPreference_bool extends _UserPreference
51 function _UserPreference_bool ($default = false) {
52 $this->_UserPreference((bool) $default);
55 function sanify ($value) {
56 if (is_array($value)) {
57 /* This allows for constructs like:
59 * <input type="hidden" name="pref[boolPref][]" value="0" />
60 * <input type="checkbox" name="pref[boolPref][]" value="1" />
62 * (If the checkbox is not checked, only the hidden input gets sent.
63 * If the checkbox is sent, both inputs get sent.)
65 foreach ($value as $val) {
75 $UserPreferences = array('editWidth' => new _UserPreference_int(80, 30, 150),
76 'editHeight' => new _UserPreference_int(22, 5, 80),
77 'timeOffset' => new _UserPreference('+0000'),
78 'relativeDates' => new _UserPreference_bool(),
79 'userid' => new _UserPreference(''));
81 class UserPreferences {
82 function UserPreferences ($saved_prefs = false) {
83 $this->_prefs = array();
85 if (isa($saved_prefs, 'UserPreferences')) {
86 foreach ($saved_prefs->_prefs as $name => $value)
87 $this->set($name, $value);
91 function _getPref ($name) {
92 global $UserPreferences;
93 if (!isset($UserPreferences[$name])) {
94 trigger_error("$name: unknown preference", E_USER_NOTICE);
97 return $UserPreferences[$name];
100 function get ($name) {
101 if (isset($this->_prefs[$name]))
102 return $this->_prefs[$name];
103 if (!($pref = $this->_getPref($name)))
105 return $pref->default_value;
108 function set ($name, $value) {
109 if (!($pref = $this->_getPref($name)))
111 $this->_prefs[$name] = $pref->sanify($value);
116 class WikiRequest extends Request {
118 function WikiRequest () {
122 $this->setArg('pagename', $this->_deducePagename());
123 $this->setArg('action', $this->_deduceAction());
125 // Restore auth state
126 $this->_user = new WikiUser($this->getSessionVar('wiki_user'));
128 // Restore saved preferences
129 if (!($prefs = $this->getCookieVar('WIKI_PREFS2')))
130 $prefs = $this->getSessionVar('wiki_prefs');
131 $this->_prefs = new UserPreferences($prefs);
134 // This really maybe should be part of the constructor, but since it
135 // may involve HTML/template output, the global $request really needs
136 // to be initialized before we do this stuff.
137 function updateAuthAndPrefs () {
138 // Handle preference updates, an authentication requests, if any.
139 if ($new_prefs = $this->getArg('pref')) {
140 $this->setArg('pref', false);
141 foreach ($new_prefs as $key => $val)
142 $this->_prefs->set($key, $val);
145 // Handle authentication request, if any.
146 if ($auth_args = $this->getArg('auth')) {
147 $this->setArg('auth', false);
148 $this->_handleAuthRequest($auth_args); // possible NORETURN
150 elseif ( ! $this->_user->isSignedIn() ) {
151 // If not auth request, try to sign in as saved user.
152 if (($saved_user = $this->getPref('userid')) != false)
153 $this->_signIn($saved_user);
157 $this->setSessionVar('wiki_prefs', $this->_prefs);
158 $this->setCookieVar('WIKI_PREFS2', $this->_prefs, 365);
160 // Ensure user has permissions for action
161 $require_level = $this->requiredAuthority($this->getArg('action'));
162 if (! $this->_user->hasAuthority($require_level))
163 $this->_notAuthorized($require_level); // NORETURN
166 function getUser () {
170 function getPrefs () {
171 return $this->_prefs;
174 // Convenience function:
175 function getPref ($key) {
176 return $this->_prefs->get($key);
180 if (!isset($this->_dbi)) {
181 $this->_dbi = WikiDB::open($GLOBALS['DBParams']);
187 * Get requested page from the page database.
189 * This is a convenience function.
191 function getPage () {
192 if (!isset($this->_dbi))
194 return $this->_dbi->getPage($this->getArg('pagename'));
197 function _handleAuthRequest ($auth_args) {
198 if (!is_array($auth_args))
201 // Ignore password unless POSTed.
202 if (!$this->isPost())
203 unset($auth_args['password']);
205 $user = WikiUser::AuthCheck($auth_args);
207 if (isa($user, 'WikiUser')) {
208 // Successful login (or logout.)
209 $this->_setUser($user);
212 // Login attempt failed.
213 $fail_message = $user;
214 // If no password was submitted, it's not really
215 // a failure --- just need to prompt for password...
216 if (!isset($auth_args['password']))
217 $fail_message = false;
218 WikiUser::PrintLoginForm($this, $auth_args, $fail_message);
219 $this->finish(); //NORETURN
222 // Login request cancelled.
227 * Attempt to sign in (bogo-login).
231 * @param $userid string Userid to attempt to sign in as.
234 function _signIn ($userid) {
235 $user = WikiUser::AuthCheck(array('userid' => $userid));
236 if (isa($user, 'WikiUser'))
237 $this->_setUser($user); // success!
240 function _setUser ($user) {
241 $this->_user = $user;
242 $this->setSessionVar('wiki_user', $user);
244 // Save userid to prefs..
245 $this->_prefs->set('userid',
246 $user->isSignedIn() ? $user->getId() : '');
249 function _notAuthorized ($require_level) {
250 // User does not have required authority. Prompt for login.
251 $what = HTML::em($this->getArg('action'));
253 if ($require_level >= WIKIAUTH_FORBIDDEN) {
254 $this->finish(fmt("Action %s is disallowed on this wiki", $what));
256 elseif ($require_level == WIKIAUTH_BOGO)
257 $msg = fmt("You must sign in to %s this wiki", $what);
258 elseif ($require_level == WIKIAUTH_USER)
259 $msg = fmt("You must log in to %s this wiki", $what);
261 $msg = fmt("You must be an administrator to %s this wiki", $what);
263 WikiUser::PrintLoginForm($this, compact('require_level'), $msg);
264 $this->finish(); // NORETURN
267 function requiredAuthority ($action) {
273 return WIKIAUTH_ANON;
276 if (defined('ZIPDUMP_AUTH') && ZIPDUMP_AUTH)
277 return WIKIAUTH_ADMIN;
278 return WIKIAUTH_ANON;
281 if (defined('REQUIRE_SIGNIN_BEFORE_EDIT') && REQUIRE_SIGNIN_BEFORE_EDIT)
282 return WIKIAUTH_BOGO;
283 return WIKIAUTH_ANON;
284 // return WIKIAUTH_BOGO;
292 return WIKIAUTH_ADMIN;
294 global $WikiNameRegexp;
295 if (preg_match("/$WikiNameRegexp\Z/A", $action))
296 return WIKIAUTH_ANON; // ActionPage.
298 return WIKIAUTH_ADMIN;
302 function possiblyDeflowerVirginWiki () {
303 if ($this->getArg('action') != 'browse')
305 if ($this->getArg('pagename') != HomePage)
308 $page = $this->getPage();
309 $current = $page->getCurrentRevision();
310 if ($current->getVersion() > 0)
311 return; // Homepage exists.
313 include('lib/loadsave.php');
315 $this->finish(); // NORETURN
318 function handleAction () {
319 $action = $this->getArg('action');
320 $method = "action_$action";
321 if (method_exists($this, $method)) {
324 elseif ($this->isActionPage($action)) {
325 $this->actionpage($action);
328 $this->finish(fmt("%s: Bad action", $action));
333 function finish ($errormsg = false) {
337 exit(); // just in case CloseDataBase calls us
340 if (!empty($this->_dbi))
341 $this->_dbi->close();
345 global $ErrorManager;
346 $ErrorManager->flushPostponedErrors();
348 if (!empty($errormsg)) {
351 HTML::h2(_("Fatal PhpWiki Error")),
354 echo "\n</body></html>";
361 function _deducePagename () {
362 if ($this->getArg('pagename'))
363 return $this->getArg('pagename');
366 $pathinfo = $this->get('PATH_INFO');
367 $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
369 if ($tail && $pathinfo == PATH_INFO_PREFIX . $tail) {
374 $query_string = $this->get('QUERY_STRING');
375 if (preg_match('/^[^&=]+$/', $query_string)) {
376 return urldecode($query_string);
382 function _deduceAction () {
383 if (!($action = $this->getArg('action')))
386 if (method_exists($this, "action_$action"))
389 // Allow for, e.g. action=LikePages
390 if ($this->isActionPage($action))
393 trigger_error("$action: Unknown action", E_USER_NOTICE);
397 function isActionPage ($pagename) {
398 // Allow for, e.g. action=LikePages
399 global $WikiNameRegexp;
400 if (!preg_match("/$WikiNameRegexp\\Z/A", $pagename))
402 $dbi = $this->getDbh();
403 $page = $dbi->getPage($pagename);
404 $rev = $page->getCurrentRevision();
405 // FIXME: more restrictive check for sane plugin?
406 if (strstr($rev->getPackedContent(), '<?plugin'))
408 trigger_error("$pagename: Does not appear to be an 'action page'", E_USER_NOTICE);
412 function action_browse () {
413 $this->compress_output();
414 include_once("lib/display.php");
418 function actionpage ($action) {
419 $this->compress_output();
420 include_once("lib/display.php");
421 actionPage($this, $action);
424 function action_diff () {
425 $this->compress_output();
426 include_once "lib/diff.php";
430 function action_search () {
431 // This is obsolete: reformulate URL and redirect.
432 // FIXME: this whole section should probably be deleted.
433 if ($this->getArg('searchtype') == 'full') {
434 $search_page = _("FullTextSearch");
437 $search_page = _("TitleSearch");
439 $this->redirect(WikiURL($search_page,
440 array('s' => $this->getArg('searchterm')),
444 function action_edit () {
445 $this->compress_output();
446 include "lib/editpage.php";
447 $e = new PageEditor ($this);
451 function action_viewsource () {
452 $this->compress_output();
453 include "lib/editpage.php";
454 $e = new PageEditor ($this);
458 function action_lock () {
459 $page = $this->getPage();
460 $page->set('locked', true);
461 $this->action_browse();
464 function action_unlock () {
465 // FIXME: This check is redundant.
466 //$user->requireAuth(WIKIAUTH_ADMIN);
467 $page = $this->getPage();
468 $page->set('locked', false);
469 $this->action_browse();
472 function action_remove () {
473 // FIXME: This check is redundant.
474 //$user->requireAuth(WIKIAUTH_ADMIN);
475 include('lib/removepage.php');
480 function action_upload () {
481 include_once("lib/loadsave.php");
485 function action_zip () {
486 include_once("lib/loadsave.php");
488 // I don't think it hurts to add cruft at the end of the zip file.
489 echo "\n========================================================\n";
490 echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
493 function action_dumpserial () {
494 include_once("lib/loadsave.php");
498 function action_loadfile () {
499 include_once("lib/loadsave.php");
500 LoadFileOrDir($this);
505 function is_safe_action ($action) {
506 return WikiRequest::requiredAuthority($action) < WIKIAUTH_ADMIN;
513 $request = new WikiRequest();
514 $request->updateAuthAndPrefs();
516 /* FIXME: is this needed anymore?
517 if (USE_PATH_INFO && ! $request->get('PATH_INFO')
518 && ! preg_match(',/$,', $request->get('REDIRECT_URL'))) {
519 $request->redirect(SERVER_URL
520 . preg_replace('/(\?|$)/', '/\1',
521 $request->get('REQUEST_URI'),
527 // Enable the output of most of the warning messages.
528 // The warnings will screw up zip files though.
529 global $ErrorManager;
530 if ($request->getArg('action') != 'zip') {
531 $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
532 //$ErrorManager->setPostponedErrorMask(0);
536 //if ($user->is_authenticated())
537 // $LogEntry->user = $user->getId();
539 $request->possiblyDeflowerVirginWiki();
541 $request->handleAction();
545 // Used for debugging purposes
546 function getmicrotime(){
547 list($usec, $sec) = explode(" ", microtime());
548 return ((float)$usec + (float)$sec);
551 if (defined ('DEBUG')) $GLOBALS['debugclock'] = getmicrotime();
560 // c-hanging-comment-ender-p: nil
561 // indent-tabs-mode: nil