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