2 rcs_id('$Id: main.php,v 1.28 2002-01-23 05:10:22 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));
20 class UserPreferences {
21 function UserPreferences ($prefs = false) {
22 if (isa($prefs, 'UserPreferences'))
23 $this->_prefs = $prefs->_prefs;
25 $this->_prefs = array();
31 // FIXME: needs cleanup.
33 $LIMITS = array('edit_area.width' => array(30, 80, 150),
34 'edit_area.height' => array(5, 22, 80));
36 $prefs = $this->_prefs;
38 foreach ($LIMITS as $key => $lims) {
39 list ($min, $default, $max) = $lims;
40 if (isset($prefs[$key]))
41 $new[$key] = min($max, max($min, (int)$prefs[$key]));
43 $new[$key] = $default;
50 return $this->_prefs[$key];
53 function setPrefs($new_prefs) {
54 if (is_array($new_prefs)) {
55 $this->_prefs = array_merge($this->_prefs, $new_prefs);
62 class WikiRequest extends Request {
64 function WikiRequest () {
68 $this->setArg('pagename', $this->_deducePagename());
69 $this->setArg('action', $this->_deduceAction());
71 $this->_user = new WikiUser($this->getSessionVar('auth_state'));
73 if (!($prefs = $this->getSessionVar('user_prefs')))
74 $prefs = $this->getCookieVar('WIKI_PREFS');
75 $this->_prefs = new UserPreferences($prefs);
82 function getPref ($key) {
83 return $this->_prefs->get($key);
87 if (!isset($this->_dbi)) {
88 $this->_dbi = WikiDB::open($GLOBALS['DBParams']);
94 * Get requested page from the page database.
96 * This is a convenience function.
99 if (!isset($this->_dbi))
101 return $this->_dbi->getPage($this->getArg('pagename'));
106 function handleAuthRequest () {
107 $auth_args = $this->getArg('auth');
108 $this->setArg('auth', false);
110 if (!is_array($auth_args) || !$this->isPost())
111 return; // Ignore if not posted.
113 $user = WikiUser::AuthCheck($auth_args);
115 if (isa($user, 'WikiUser')) {
116 // Successful login (or logout.)
117 $this->_user = $user;
118 $this->setSessionVar('auth_state', $user);
121 // Login attempt failed.
122 $fail_message = $user;
123 WikiUser::PrintLoginForm($auth_args, $fail_message);
124 $this->finish(); //NORETURN
127 // Login request cancelled.
131 function checkAuthority () {
132 $action = $this->getArg('action');
133 $require_level = $this->requiredAuthority($action);
135 $user = $this->getUser();
136 if (! $user->hasAuthority($require_level)) {
137 // User does not have required authority. Prompt for login.
138 $what = HTML::em($action);
140 if ($require_level >= WIKIAUTH_FORBIDDEN) {
141 $this->finish(fmt("Action %s is disallowed on this wiki", $what));
143 elseif ($require_level == WIKIAUTH_BOGO)
144 $msg = fmt("You must sign in to %s this wiki", $what);
145 elseif ($require_level == WIKIAUTH_USER)
146 $msg = fmt("You must log in to %s this wiki", $what);
148 $msg = fmt("You must be an administrator to %s this wiki", $what);
150 WikiUser::PrintLoginForm(compact('require_level'), $msg);
151 $this->finish(); // NORETURN
155 function requiredAuthority ($action) {
162 return WIKIAUTH_ANON;
165 return WIKIAUTH_ANON;
168 case 'save': // FIXME delete
169 return WIKIAUTH_ANON;
170 // return WIKIAUTH_BOGO;
179 return WIKIAUTH_ADMIN;
183 function handleSetPrefRequest () {
184 $new_prefs = $this->getArg('pref');
185 $this->setArg('pref', false);
186 if (!is_array($pref_args))
189 // Update and save preferences.
190 $this->_prefs->setPrefs($new_prefs);
191 $this->setSessionVar('user_prefs', $prefs);
192 $this->setCookieVar('WIKI_PREFS', $prefs, 365);
195 function deflowerDatabase () {
196 if ($this->getArg('action') != 'browse')
198 if ($this->getArg('pagename') != _("HomePage"))
201 $page = $this->getPage();
202 $current = $page->getCurrentRevision();
203 if ($current->getVersion() > 0)
204 return; // Homepage exists.
206 include('lib/loadsave.php');
208 $this->finish(); // NORETURN
211 function handleAction () {
212 $action = $this->getArg('action');
213 $method = "action_$action";
214 if (! method_exists($this, $method)) {
215 $this->finish(fmt("%s: Bad action", $action));
221 function finish ($errormsg = false) {
225 exit(); // just in case CloseDataBase calls us
228 if (!empty($this->_dbi))
229 $this->_dbi->close();
233 global $ErrorManager;
234 $ErrorManager->flushPostponedErrors();
236 if (!empty($errormsg)) {
237 PrintXML(array(HTML::br(),
239 HTML::h2(_("Fatal PhpWiki Error")),
242 echo "\n</body></html>";
249 function _deducePagename () {
250 if ($this->getArg('pagename'))
251 return $this->getArg('pagename');
254 $pathinfo = $this->get('PATH_INFO');
255 $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
257 if ($tail && $pathinfo == PATH_INFO_PREFIX . $tail) {
262 $query_string = $this->get('QUERY_STRING');
263 if (preg_match('/^[^&=]+$/', $query_string)) {
264 return urldecode($query_string);
268 return _("HomePage");
272 function _deduceAction () {
273 if (!($action = $this->getArg('action')))
276 if (! method_exists($this, "action_$action")) {
278 trigger_error("$action: Unknown action", E_USER_NOTICE);
284 function action_browse () {
285 $this->compress_output();
286 include_once("lib/display.php");
290 function action_diff () {
291 $this->compress_output();
292 include_once "lib/diff.php";
296 function action_search () {
297 // This is obsolete: reformulate URL and redirect.
298 // FIXME: this whole section should probably be deleted.
299 if ($this->getArg('searchtype') == 'full') {
300 $search_page = _("FullTextSearch");
303 $search_page = _("TitleSearch");
305 $this->redirect(WikiURL($search_page,
306 array('s' => $this->getArg('searchterm')),
310 function action_edit () {
311 $this->compress_output();
312 include "lib/editpage.php";
316 // FIXME: combine this with edit
317 function action_save () {
318 $this->compress_output();
319 include "lib/savepage.php";
323 function action_lock () {
324 $page = $this->getPage();
325 $page->set('locked', true);
326 $this->action_browse();
329 function action_unlock () {
330 // FIXME: This check is redundant.
331 //$user->requireAuth(WIKIAUTH_ADMIN);
332 $page = $this->getPage();
333 $page->set('locked', false);
334 $this->action_browse();
337 function action_remove () {
338 // FIXME: This check is redundant.
339 //$user->requireAuth(WIKIAUTH_ADMIN);
340 include('lib/removepage.php');
344 function action_upload () {
345 include_once("lib/loadsave.php");
349 function action_zip () {
350 include_once("lib/loadsave.php");
352 // I don't think it hurts to add cruft at the end of the zip file.
353 echo "\n========================================================\n";
354 echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
357 function action_dumpserial () {
358 include_once("lib/loadsave.php");
362 function action_loadfile () {
363 include_once("lib/loadsave.php");
364 LoadFileOrDir($this);
369 function is_safe_action ($action) {
370 return WikiRequest::requiredAuthority($action) < WIKIAUTH_ADMIN;
377 $request = new WikiRequest();
380 /* FIXME: is this needed anymore?
381 if (USE_PATH_INFO && ! $request->get('PATH_INFO')
382 && ! preg_match(',/$,', $request->get('REDIRECT_URL'))) {
383 $request->redirect(SERVER_URL
384 . preg_replace('/(\?|$)/', '/\1',
385 $request->get('REQUEST_URI'),
391 // Handle authentication requests
392 if ($request->getArg('auth'))
393 $request->handleAuthRequest();
396 //global $user; // FIXME: can we make this non-global?
397 //$user = $request->getUser();
399 // Handle adjustments to user preferences
400 if ($request->getArg('pref'))
401 $request->handleSetPrefRequest();
403 // Enable the output of most of the warning messages.
404 // The warnings will screw up zip files and setpref though.
405 global $ErrorManager;
406 if ($request->getArg('action') != 'zip') {
407 //$ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
408 $ErrorManager->setPostponedErrorMask(0);
412 // Ensure user has permissions for action
413 $request->checkAuthority();
416 //if ($user->is_authenticated())
417 // $LogEntry->user = $user->getId();
419 $request->deflowerDatabase();
421 $request->handleAction();
432 // c-hanging-comment-ender-p: nil
433 // indent-tabs-mode: nil