]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/main.php
Big refactor of lib/WikiUser.php. The login mechanism no longer uses
[SourceForge/phpwiki.git] / lib / main.php
1 <?php
2 rcs_id('$Id: main.php,v 1.25 2002-01-19 07:21:58 dairiki Exp $');
3
4 include "lib/config.php";
5 include "lib/stdlib.php";
6 require_once('lib/Request.php');
7 require_once("lib/WikiUser.php");
8 require_once('lib/WikiDB.php');
9
10 if (defined('THEME')) {
11     include("themes/" . THEME . "/themeinfo.php");
12 }
13 if (empty($Theme)) {
14     include("themes/default/themeinfo.php");
15 }
16 assert(!empty($Theme));
17
18
19 function deduce_pagename ($request) {
20     if ($request->getArg('pagename'))
21         return $request->getArg('pagename');
22
23     if (USE_PATH_INFO) {
24         $pathinfo = $request->get('PATH_INFO');
25         if (ereg('^' . PATH_INFO_PREFIX . '(..*)$', $pathinfo, $m))
26             return $m[1];
27     }
28
29     $query_string = $request->get('QUERY_STRING');
30     if (preg_match('/^[^&=]+$/', $query_string))
31         return urldecode($query_string);
32     
33     return _("HomePage");
34 }
35
36 function is_safe_action ($action) {
37     if (! ZIPDUMP_AUTH and ($action == 'zip' || $action == 'xmldump'))
38         return true;
39     return in_array ( $action, array('browse', 'info',
40                                      'diff',   'search',
41                                      'edit',   'save',
42                                      'setprefs') );
43 }
44
45 function authlevelForAction ($action) {
46     if (is_safe_action($action))
47         return WIKIAUTH_ANON;
48     else
49         return WIKIAUTH_ADMIN;
50 }
51
52 function main ($request) {
53     
54     
55     if (USE_PATH_INFO && ! $request->get('PATH_INFO')
56         && ! preg_match(',/$,', $request->get('REDIRECT_URL'))) {
57         $request->redirect(SERVER_URL
58                            . preg_replace('/(\?|$)/', '/\1',
59                                           $request->get('REQUEST_URI'),
60                                           1));
61         exit;
62     }
63
64     $request->setArg('pagename', deduce_pagename($request));
65     global $pagename;               // FIXME: can we make this non-global?
66     $pagename = $request->getArg('pagename');
67     
68     $action = $request->getArg('action');
69     if (!$action) {
70         $action = 'browse';
71         $request->setArg('action', $action);
72     }
73     
74     global $user;               // FIXME: can we make this non-global?
75     $user = new WikiUser($request);
76     $user->requireAuth( authlevelForAction($action) );
77     
78
79     //FIXME:
80     //if ($user->is_authenticated())
81     //  $LogEntry->user = $user->getId();
82     
83     // All requests require the database
84     global $dbi;                // FIXME: can we keep this non-global?
85     $dbi = WikiDB::open($GLOBALS['DBParams']);
86
87     // FIXME: need something more robust here...
88     if ( $action == 'browse' && $request->getArg('pagename') == _("HomePage") ) {
89         // if there is no HomePage, create a basic set of Wiki pages
90         if ( ! $dbi->isWikiPage(_("HomePage")) ) {
91             include_once("lib/loadsave.php");
92             SetupWiki($dbi);
93             ExitWiki();
94         }
95     }
96
97     // FIXME: I think this is redundant.
98     //if (!is_safe_action($action))
99     //    $user->must_be_admin($action);
100
101     // FIXME: this should be moved higher in the logic.
102     if (isset($DisabledActions) && in_array($action, $DisabledActions))
103         ExitWiki(sprintf(_("Action %s is disabled in this wiki."), $action));
104    
105     // Enable the output of most of the warning messages.
106     // The warnings will screw up zip files and setpref though.
107     global $ErrorManager;
108     if ($action != 'zip' && $action != 'setprefs') {
109         $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
110     }
111     
112     
113     switch ($action) {
114     case 'edit':
115         $request->compress_output();
116         include "lib/editpage.php";
117         editPage($dbi, $request);
118         break;
119
120     case 'search':
121         // This is obsolete: reformulate URL and redirect.
122         // FIXME: this whole section should probably be deleted.
123         if ($request->getArg('searchtype') == 'full') {
124             $search_page = _("FullTextSearch");
125         }
126         else {
127             $search_page = _("TitleSearch");
128         }
129         $request->redirect(WikiURL($search_page,
130                                    array('s' => $request->getArg('searchterm')),
131                                    'absolute_url'));
132         break;
133         
134     case 'save':
135         $request->compress_output();
136         include "lib/savepage.php";
137         savePage($dbi, $request);
138         break;
139     case 'diff':
140         $request->compress_output();
141         include_once "lib/diff.php";
142         showDiff($dbi, $request);
143         break;
144       
145     case 'zip':
146         include_once("lib/loadsave.php");
147         MakeWikiZip($dbi, $request);
148         // I don't think it hurts to add cruft at the end of the zip file.
149         echo "\n========================================================\n";
150         echo "PhpWiki " . PHPWIKI_VERSION . " source:\n$GLOBALS[RCS_IDS]\n";
151         break;
152
153     /* Not yet implemented:    
154     case 'xmldump':
155         // FIXME:
156         $limit = 1;
157         if ($request->getArg('include') == 'all')
158             $limit = 0;
159         require_once("lib/libxml.php");
160         $xmlwriter = new WikiXmlWriter;
161         $xmlwriter->begin();
162         $xmlwriter->writeComment("PhpWiki " . PHPWIKI_VERSION
163                                  . " source:\n$RCS_IDS\n");
164         $xmlwriter->writeDatabase($dbi, $limit);
165         $xmlwriter->end();
166         break;
167     */
168         
169     case 'upload':
170         include_once("lib/loadsave.php");
171         LoadPostFile($dbi, $request);
172         break;
173    
174     case 'dumpserial':
175         include_once("lib/loadsave.php");
176         DumpToDir($dbi, $request);
177         break;
178
179     case 'loadfile':
180         include_once("lib/loadsave.php");
181         LoadFileOrDir($dbi, $request);
182         break;
183
184     case 'remove':
185         include 'lib/removepage.php';
186         break;
187     
188     case 'lock':
189     case 'unlock':
190         // FIXME: This check is redundant.
191         $user->requireAuth(WIKIAUTH_ADMIN);
192         $page = $dbi->getPage($request->getArg('pagename'));
193         $page->set('locked', $action == 'lock');
194
195         $request->compress_output();
196         include_once("lib/display.php");
197         displayPage($dbi, $request);
198         break;
199
200     case 'setprefs':
201         $prefs = $user->getPreferences();
202         $edit_area_width = $request->getArg('edit_area_width');
203         $edit_area_height = $request->getArg('edit_area_height');
204         if ($edit_area_width)
205             $prefs['edit_area.width'] = $edit_area_width;
206         if ($edit_area_height)
207             $prefs['edit_area.height'] = $edit_area_height;
208         $user->setPreferences($prefs);
209         $ErrorManager->setPostponedErrorMask(E_ALL & ~E_NOTICE);
210
211         $request->compress_output();
212         include_once("lib/display.php");
213         displayPage($dbi, $request);
214         break;
215    
216     case 'browse':
217     case 'login':
218     case 'logout':
219
220         $request->compress_output();
221         include_once("lib/display.php");
222         displayPage($dbi, $request);
223         break;
224         
225     default:
226         echo QElement('p', sprintf(_("Bad action: '%s'"), urlencode($action)));
227         break;
228     }
229     ExitWiki();
230 }
231
232 $request = new Request;
233 main($request);
234
235
236 // Local Variables:
237 // mode: php
238 // tab-width: 8
239 // c-basic-offset: 4
240 // c-hanging-comment-ender-p: nil
241 // indent-tabs-mode: nil
242 // End:   
243 ?>