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