2 rcs_id('$Id: main.php,v 1.30 2002-01-23 20:18:21 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_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 'userid' => new _UserPreference(''));
53 class UserPreferences {
54 function UserPreferences ($saved_prefs = false) {
55 $this->_prefs = array();
57 if (isa($saved_prefs, 'UserPreferences')) {
58 foreach ($saved_prefs->_prefs as $name => $value)
59 $this->set($name, $value);
63 function _getPref ($name) {
64 global $UserPreferences;
65 if (!isset($UserPreferences[$name])) {
66 trigger_error("$key: unknown preference", E_USER_NOTICE);
69 return $UserPreferences[$name];
72 function get ($name) {
73 if (isset($this->_prefs[$name]))
74 return $this->_prefs[$name];
75 if (!($pref = $this->_getPref($name)))
77 return $pref->default_value;
80 function set ($name, $value) {
81 if (!($pref = $this->_getPref($name)))
83 $this->_prefs[$name] = $pref->sanify($value);
88 class WikiRequest extends Request {
90 function WikiRequest () {
94 // FIXME: make $request an argument of WikiTemplate,
95 // then subsume _init into WikiRequest().
98 $this->setArg('pagename', $this->_deducePagename());
99 $this->setArg('action', $this->_deduceAction());
101 // Restore auth state
102 $this->_user = new WikiUser($this->getSessionVar('auth_state'));
104 // Restore saved preferences
105 if (!($prefs = $this->getCookieVar('WIKI_PREFS')))
106 $prefs = $this->getSessionVar('user_prefs');
107 $this->_prefs = new UserPreferences($prefs);
109 // Handle preference updates, an authentication requests, if any.
110 if ($new_prefs = $this->getArg('pref')) {
111 $this->setArg('pref', false);
112 foreach ($new_prefs as $key => $val)
113 $this->_prefs->set($key, $val);
116 // Handle authentication request, if any.
117 if ($auth_args = $this->getArg('auth')) {
118 $this->setArg('auth', false);
119 $this->_handleAuthRequest($auth_args); // possible NORETURN
121 elseif ( ! $this->_user->isSignedIn() ) {
122 // If not auth request, try to sign in as saved user.
123 if (($saved_user = $this->getPref('userid')) != false)
124 $this->_signIn($saved_user);
128 $this->setSessionVar('user_prefs', $this->_prefs);
129 $this->setCookieVar('WIKI_PREFS', $this->_prefs, 365);
131 // Ensure user has permissions for action
132 $require_level = $this->requiredAuthority($this->getArg('action'));
133 if (! $this->_user->hasAuthority($require_level))
134 $this->_notAuthorized($require_level); // NORETURN
137 function getUser () {
141 function getPrefs () {
142 return $this->_prefs;
145 // Convenience function:
146 function getPref ($key) {
147 return $this->_prefs->get($key);
151 if (!isset($this->_dbi)) {
152 $this->_dbi = WikiDB::open($GLOBALS['DBParams']);
158 * Get requested page from the page database.
160 * This is a convenience function.
162 function getPage () {
163 if (!isset($this->_dbi))
165 return $this->_dbi->getPage($this->getArg('pagename'));
168 function _handleAuthRequest ($auth_args) {
169 if (!is_array($auth_args))
172 // Ignore password unless POSTed.
173 if (!$this->isPost())
174 unset($auth_args['password']);
176 $user = WikiUser::AuthCheck($auth_args);
178 if (isa($user, 'WikiUser')) {
179 // Successful login (or logout.)
180 $this->_setUser($user);
183 // Login attempt failed.
184 $fail_message = $user;
185 WikiUser::PrintLoginForm($auth_args, $fail_message);
186 $this->finish(); //NORETURN
189 // Login request cancelled.
194 * Attempt to sign in (bogo-login).
198 * @param $userid string Userid to attempt to sign in as.
201 function _signIn ($userid) {
202 $user = WikiUser::AuthCheck(array('userid' => $userid));
203 if (isa($user, 'WikiUser'))
204 $this->_setUser($user); // success!
207 function _setUser ($user) {
208 $this->_user = $user;
209 $this->setSessionVar('auth_state', $user);
211 // Save userid to prefs..
212 $this->_prefs->set('userid',
213 $user->isSignedIn() ? $user->getId() : '');
216 function _notAuthorized ($require_level) {
217 // User does not have required authority. Prompt for login.
218 $what = HTML::em($this->getArg('action'));
220 if ($require_level >= WIKIAUTH_FORBIDDEN) {
221 $this->finish(fmt("Action %s is disallowed on this wiki", $what));
223 elseif ($require_level == WIKIAUTH_BOGO)
224 $msg = fmt("You must sign in to %s this wiki", $what);
225 elseif ($require_level == WIKIAUTH_USER)
226 $msg = fmt("You must log in to %s this wiki", $what);
228 $msg = fmt("You must be an administrator to %s this wiki", $what);
230 WikiUser::PrintLoginForm(compact('require_level'), $msg);
231 $this->finish(); // NORETURN
234 function requiredAuthority ($action) {
241 return WIKIAUTH_ANON;
244 if (defined('ZIPDUMP_AUTH') && ZIPDUMP_AUTH)
245 return WIKIAUTH_ADMIN;
246 return WIKIAUTH_ANON;
249 case 'save': // FIXME delete
250 if (defined('REQUIRE_SIGNIN_BEFORE_EDIT') && REQUIRE_SIGNIN_BEFORE_EDIT)
251 return WIKIAUTH_BOGO;
252 return WIKIAUTH_ANON;
253 // return WIKIAUTH_BOGO;
262 return WIKIAUTH_ADMIN;
266 function deflowerDatabase () {
267 if ($this->getArg('action') != 'browse')
269 if ($this->getArg('pagename') != _("HomePage"))
272 $page = $this->getPage();
273 $current = $page->getCurrentRevision();
274 if ($current->getVersion() > 0)
275 return; // Homepage exists.
277 include('lib/loadsave.php');
279 $this->finish(); // NORETURN
282 function handleAction () {
283 $action = $this->getArg('action');
284 $method = "action_$action";
285 if (! method_exists($this, $method)) {
286 $this->finish(fmt("%s: Bad action", $action));
292 function finish ($errormsg = false) {
296 exit(); // just in case CloseDataBase calls us
299 if (!empty($this->_dbi))
300 $this->_dbi->close();
304 global $ErrorManager;
305 $ErrorManager->flushPostponedErrors();
307 if (!empty($errormsg)) {
308 PrintXML(array(HTML::br(),
310 HTML::h2(_("Fatal PhpWiki Error")),
313 echo "\n</body></html>";
320 function _deducePagename () {
321 if ($this->getArg('pagename'))
322 return $this->getArg('pagename');
325 $pathinfo = $this->get('PATH_INFO');
326 $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
328 if ($tail && $pathinfo == PATH_INFO_PREFIX . $tail) {
333 $query_string = $this->get('QUERY_STRING');
334 if (preg_match('/^[^&=]+$/', $query_string)) {
335 return urldecode($query_string);
339 return _("HomePage");
343 function _deduceAction () {
344 if (!($action = $this->getArg('action')))
347 if (! method_exists($this, "action_$action")) {
349 trigger_error("$action: Unknown action", E_USER_NOTICE);
355 function action_browse () {
356 $this->compress_output();
357 include_once("lib/display.php");
361 function action_diff () {
362 $this->compress_output();
363 include_once "lib/diff.php";
367 function action_search () {
368 // This is obsolete: reformulate URL and redirect.
369 // FIXME: this whole section should probably be deleted.
370 if ($this->getArg('searchtype') == 'full') {
371 $search_page = _("FullTextSearch");
374 $search_page = _("TitleSearch");
376 $this->redirect(WikiURL($search_page,
377 array('s' => $this->getArg('searchterm')),
381 function action_edit () {
382 $this->compress_output();
383 include "lib/editpage.php";
387 // FIXME: combine this with edit
388 function action_save () {
389 $this->compress_output();
390 include "lib/savepage.php";
394 function action_lock () {
395 $page = $this->getPage();
396 $page->set('locked', true);
397 $this->action_browse();
400 function action_unlock () {
401 // FIXME: This check is redundant.
402 //$user->requireAuth(WIKIAUTH_ADMIN);
403 $page = $this->getPage();
404 $page->set('locked', false);
405 $this->action_browse();
408 function action_remove () {
409 // FIXME: This check is redundant.
410 //$user->requireAuth(WIKIAUTH_ADMIN);
411 include('lib/removepage.php');
415 function action_upload () {
416 include_once("lib/loadsave.php");
420 function action_zip () {
421 include_once("lib/loadsave.php");
423 // I don't think it hurts to add cruft at the end of the zip file.
424 echo "\n========================================================\n";
425 echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
428 function action_dumpserial () {
429 include_once("lib/loadsave.php");
433 function action_loadfile () {
434 include_once("lib/loadsave.php");
435 LoadFileOrDir($this);
440 function is_safe_action ($action) {
441 return WikiRequest::requiredAuthority($action) < WIKIAUTH_ADMIN;
448 $request = new WikiRequest();
451 /* FIXME: is this needed anymore?
452 if (USE_PATH_INFO && ! $request->get('PATH_INFO')
453 && ! preg_match(',/$,', $request->get('REDIRECT_URL'))) {
454 $request->redirect(SERVER_URL
455 . preg_replace('/(\?|$)/', '/\1',
456 $request->get('REQUEST_URI'),
462 // Enable the output of most of the warning messages.
463 // The warnings will screw up zip files and setpref though.
464 global $ErrorManager;
465 if ($request->getArg('action') != 'zip') {
466 //$ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
467 $ErrorManager->setPostponedErrorMask(0);
472 //if ($user->is_authenticated())
473 // $LogEntry->user = $user->getId();
475 $request->deflowerDatabase();
477 $request->handleAction();
488 // c-hanging-comment-ender-p: nil
489 // indent-tabs-mode: nil