2 rcs_id('$Id: main.php,v 1.53 2002-02-08 22:03:00 dairiki 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_numeric extends _UserPreference
33 function _UserPreference_numeric ($default, $minval = false, $maxval = false) {
34 $this->_UserPreference((double) $default);
35 $this->_minval = (double) $minval;
36 $this->_maxval = (double) $maxval;
39 function sanify ($value) {
40 $value = (double) $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_int extends _UserPreference_numeric
51 function _UserPreference_int ($default, $minval = false, $maxval = false) {
52 $this->_UserPreference_numeric((int) $default, (int)$minval, (int)$maxval);
55 function sanify ($value) {
56 return (int) parent::sanify((int)$value);
60 class _UserPreference_bool extends _UserPreference
62 function _UserPreference_bool ($default = false) {
63 $this->_UserPreference((bool) $default);
66 function sanify ($value) {
67 if (is_array($value)) {
68 /* This allows for constructs like:
70 * <input type="hidden" name="pref[boolPref][]" value="0" />
71 * <input type="checkbox" name="pref[boolPref][]" value="1" />
73 * (If the checkbox is not checked, only the hidden input gets sent.
74 * If the checkbox is sent, both inputs get sent.)
76 foreach ($value as $val) {
86 $UserPreferences = array('editWidth' => new _UserPreference_int(80, 30, 150),
87 'editHeight' => new _UserPreference_int(22, 5, 80),
88 'timeOffset' => new _UserPreference_numeric(0, -26, 26),
89 'relativeDates' => new _UserPreference_bool(),
90 'userid' => new _UserPreference(''));
92 class UserPreferences {
93 function UserPreferences ($saved_prefs = false) {
94 $this->_prefs = array();
96 if (isa($saved_prefs, 'UserPreferences')) {
97 foreach ($saved_prefs->_prefs as $name => $value)
98 $this->set($name, $value);
102 function _getPref ($name) {
103 global $UserPreferences;
104 if (!isset($UserPreferences[$name])) {
105 trigger_error("$name: unknown preference", E_USER_NOTICE);
108 return $UserPreferences[$name];
111 function get ($name) {
112 if (isset($this->_prefs[$name]))
113 return $this->_prefs[$name];
114 if (!($pref = $this->_getPref($name)))
116 return $pref->default_value;
119 function set ($name, $value) {
120 if (!($pref = $this->_getPref($name)))
122 $this->_prefs[$name] = $pref->sanify($value);
127 class WikiRequest extends Request {
129 function WikiRequest () {
133 $this->setArg('pagename', $this->_deducePagename());
134 $this->setArg('action', $this->_deduceAction());
136 // Restore auth state
137 $this->_user = new WikiUser($this->getSessionVar('wiki_user'));
139 // Restore saved preferences
140 if (!($prefs = $this->getCookieVar('WIKI_PREFS2')))
141 $prefs = $this->getSessionVar('wiki_prefs');
142 $this->_prefs = new UserPreferences($prefs);
145 // This really maybe should be part of the constructor, but since it
146 // may involve HTML/template output, the global $request really needs
147 // to be initialized before we do this stuff.
148 function updateAuthAndPrefs () {
149 // Handle preference updates, an authentication requests, if any.
150 if ($new_prefs = $this->getArg('pref')) {
151 $this->setArg('pref', false);
152 foreach ($new_prefs as $key => $val)
153 $this->_prefs->set($key, $val);
156 // Handle authentication request, if any.
157 if ($auth_args = $this->getArg('auth')) {
158 $this->setArg('auth', false);
159 $this->_handleAuthRequest($auth_args); // possible NORETURN
161 elseif ( ! $this->_user->isSignedIn() ) {
162 // If not auth request, try to sign in as saved user.
163 if (($saved_user = $this->getPref('userid')) != false)
164 $this->_signIn($saved_user);
168 $this->setSessionVar('wiki_prefs', $this->_prefs);
169 $this->setCookieVar('WIKI_PREFS2', $this->_prefs, 365);
171 // Ensure user has permissions for action
172 $require_level = $this->requiredAuthority($this->getArg('action'));
173 if (! $this->_user->hasAuthority($require_level))
174 $this->_notAuthorized($require_level); // NORETURN
177 function getUser () {
181 function getPrefs () {
182 return $this->_prefs;
185 // Convenience function:
186 function getPref ($key) {
187 return $this->_prefs->get($key);
191 if (!isset($this->_dbi)) {
192 $this->_dbi = WikiDB::open($GLOBALS['DBParams']);
198 * Get requested page from the page database.
200 * This is a convenience function.
202 function getPage () {
203 if (!isset($this->_dbi))
205 return $this->_dbi->getPage($this->getArg('pagename'));
208 function _handleAuthRequest ($auth_args) {
209 if (!is_array($auth_args))
212 // Ignore password unless POSTed.
213 if (!$this->isPost())
214 unset($auth_args['password']);
216 $user = WikiUser::AuthCheck($auth_args);
218 if (isa($user, 'WikiUser')) {
219 // Successful login (or logout.)
220 $this->_setUser($user);
223 // Login attempt failed.
224 $fail_message = $user;
225 // If no password was submitted, it's not really
226 // a failure --- just need to prompt for password...
227 if (!isset($auth_args['password']))
228 $fail_message = false;
229 WikiUser::PrintLoginForm($this, $auth_args, $fail_message);
230 $this->finish(); //NORETURN
233 // Login request cancelled.
238 * Attempt to sign in (bogo-login).
242 * @param $userid string Userid to attempt to sign in as.
245 function _signIn ($userid) {
246 $user = WikiUser::AuthCheck(array('userid' => $userid));
247 if (isa($user, 'WikiUser'))
248 $this->_setUser($user); // success!
251 function _setUser ($user) {
252 $this->_user = $user;
253 $this->setSessionVar('wiki_user', $user);
255 // Save userid to prefs..
256 $this->_prefs->set('userid',
257 $user->isSignedIn() ? $user->getId() : '');
260 function _notAuthorized ($require_level) {
261 // User does not have required authority. Prompt for login.
262 $what = HTML::em($this->getArg('action'));
264 if ($require_level >= WIKIAUTH_FORBIDDEN) {
265 $this->finish(fmt("Action %s is disallowed on this wiki", $what));
267 elseif ($require_level == WIKIAUTH_BOGO)
268 $msg = fmt("You must sign in to %s this wiki", $what);
269 elseif ($require_level == WIKIAUTH_USER)
270 $msg = fmt("You must log in to %s this wiki", $what);
272 $msg = fmt("You must be an administrator to %s this wiki", $what);
274 WikiUser::PrintLoginForm($this, compact('require_level'), $msg);
275 $this->finish(); // NORETURN
278 function requiredAuthority ($action) {
284 return WIKIAUTH_ANON;
287 if (defined('ZIPDUMP_AUTH') && ZIPDUMP_AUTH)
288 return WIKIAUTH_ADMIN;
289 return WIKIAUTH_ANON;
292 if (defined('REQUIRE_SIGNIN_BEFORE_EDIT') && REQUIRE_SIGNIN_BEFORE_EDIT)
293 return WIKIAUTH_BOGO;
294 return WIKIAUTH_ANON;
295 // return WIKIAUTH_BOGO;
303 return WIKIAUTH_ADMIN;
305 global $WikiNameRegexp;
306 if (preg_match("/$WikiNameRegexp\Z/A", $action))
307 return WIKIAUTH_ANON; // ActionPage.
309 return WIKIAUTH_ADMIN;
313 function possiblyDeflowerVirginWiki () {
314 if ($this->getArg('action') != 'browse')
316 if ($this->getArg('pagename') != HomePage)
319 $page = $this->getPage();
320 $current = $page->getCurrentRevision();
321 if ($current->getVersion() > 0)
322 return; // Homepage exists.
324 include('lib/loadsave.php');
326 $this->finish(); // NORETURN
329 function handleAction () {
330 $action = $this->getArg('action');
331 $method = "action_$action";
332 if (method_exists($this, $method)) {
335 elseif ($this->isActionPage($action)) {
336 $this->actionpage($action);
339 $this->finish(fmt("%s: Bad action", $action));
344 function finish ($errormsg = false) {
348 exit(); // just in case CloseDataBase calls us
351 if (!empty($this->_dbi))
352 $this->_dbi->close();
356 global $ErrorManager;
357 $ErrorManager->flushPostponedErrors();
359 if (!empty($errormsg)) {
362 HTML::h2(_("Fatal PhpWiki Error")),
365 echo "\n</body></html>";
372 function _deducePagename () {
373 if ($this->getArg('pagename'))
374 return $this->getArg('pagename');
377 $pathinfo = $this->get('PATH_INFO');
378 $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
380 if ($tail && $pathinfo == PATH_INFO_PREFIX . $tail) {
385 $query_string = $this->get('QUERY_STRING');
386 if (preg_match('/^[^&=]+$/', $query_string)) {
387 return urldecode($query_string);
393 function _deduceAction () {
394 if (!($action = $this->getArg('action')))
397 if (method_exists($this, "action_$action"))
400 // Allow for, e.g. action=LikePages
401 if ($this->isActionPage($action))
404 trigger_error("$action: Unknown action", E_USER_NOTICE);
408 function isActionPage ($pagename) {
409 // Allow for, e.g. action=LikePages
410 global $WikiNameRegexp;
411 if (!preg_match("/$WikiNameRegexp\\Z/A", $pagename))
413 $dbi = $this->getDbh();
414 $page = $dbi->getPage($pagename);
415 $rev = $page->getCurrentRevision();
416 // FIXME: more restrictive check for sane plugin?
417 if (strstr($rev->getPackedContent(), '<?plugin'))
419 trigger_error("$pagename: Does not appear to be an 'action page'", E_USER_NOTICE);
423 function action_browse () {
424 $this->compress_output();
425 include_once("lib/display.php");
429 function actionpage ($action) {
430 $this->compress_output();
431 include_once("lib/display.php");
432 actionPage($this, $action);
435 function action_diff () {
436 $this->compress_output();
437 include_once "lib/diff.php";
441 function action_search () {
442 // This is obsolete: reformulate URL and redirect.
443 // FIXME: this whole section should probably be deleted.
444 if ($this->getArg('searchtype') == 'full') {
445 $search_page = _("FullTextSearch");
448 $search_page = _("TitleSearch");
450 $this->redirect(WikiURL($search_page,
451 array('s' => $this->getArg('searchterm')),
455 function action_edit () {
456 $this->compress_output();
457 include "lib/editpage.php";
458 $e = new PageEditor ($this);
462 function action_viewsource () {
463 $this->compress_output();
464 include "lib/editpage.php";
465 $e = new PageEditor ($this);
469 function action_lock () {
470 $page = $this->getPage();
471 $page->set('locked', true);
472 $this->action_browse();
475 function action_unlock () {
476 // FIXME: This check is redundant.
477 //$user->requireAuth(WIKIAUTH_ADMIN);
478 $page = $this->getPage();
479 $page->set('locked', false);
480 $this->action_browse();
483 function action_remove () {
484 // FIXME: This check is redundant.
485 //$user->requireAuth(WIKIAUTH_ADMIN);
486 include('lib/removepage.php');
491 function action_upload () {
492 include_once("lib/loadsave.php");
496 function action_zip () {
497 include_once("lib/loadsave.php");
499 // I don't think it hurts to add cruft at the end of the zip file.
500 echo "\n========================================================\n";
501 echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
504 function action_dumpserial () {
505 include_once("lib/loadsave.php");
509 function action_loadfile () {
510 include_once("lib/loadsave.php");
511 LoadFileOrDir($this);
516 function is_safe_action ($action) {
517 return WikiRequest::requiredAuthority($action) < WIKIAUTH_ADMIN;
524 $request = new WikiRequest();
525 $request->updateAuthAndPrefs();
527 /* FIXME: is this needed anymore?
528 if (USE_PATH_INFO && ! $request->get('PATH_INFO')
529 && ! preg_match(',/$,', $request->get('REDIRECT_URL'))) {
530 $request->redirect(SERVER_URL
531 . preg_replace('/(\?|$)/', '/\1',
532 $request->get('REQUEST_URI'),
538 // Enable the output of most of the warning messages.
539 // The warnings will screw up zip files though.
540 global $ErrorManager;
541 if ($request->getArg('action') != 'zip') {
542 $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
543 //$ErrorManager->setPostponedErrorMask(0);
547 //if ($user->is_authenticated())
548 // $LogEntry->user = $user->getId();
550 $request->possiblyDeflowerVirginWiki();
552 $request->handleAction();
556 // Used for debugging purposes
557 function getmicrotime(){
558 list($usec, $sec) = explode(" ", microtime());
559 return ((float)$usec + (float)$sec);
562 if (defined ('DEBUG')) $GLOBALS['debugclock'] = getmicrotime();
571 // c-hanging-comment-ender-p: nil
572 // indent-tabs-mode: nil