]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/main.php
Restrict the conditions under which we try to load initial page
[SourceForge/phpwiki.git] / lib / main.php
1 <?php
2 rcs_id('$Id: main.php,v 1.13 2001-04-09 19:31:49 dairiki Exp $');
3 include "lib/config.php";
4 include "lib/stdlib.php";
5 include "lib/userauth.php";
6 include "lib/logger.php";
7
8 if (ACCESS_LOG)
9 {
10    $LogEntry = new AccessLogEntry;
11
12    function _write_log () { $GLOBALS['LogEntry']->write(ACCESS_LOG); }
13    register_shutdown_function('_write_log');
14 }
15
16 if (USE_PATH_INFO && !isset($PATH_INFO)
17     && (!isset($REDIRECT_URL) || !preg_match(',/$,', $REDIRECT_URL)))
18 {
19    $LogEntry->status = 302;     // "302 Found"
20    header("Location: " . SERVER_URL . preg_replace('/(\?|$)/', '/\1', $REQUEST_URI, 1));
21    exit;
22 }
23
24 function DeducePagename () 
25 {
26    global $pagename, $PATH_INFO, $QUERY_STRING;
27    
28    if (isset($pagename))
29       return fix_magic_quotes_gpc($pagename);
30
31    if (USE_PATH_INFO && isset($PATH_INFO))
32    {
33       fix_magic_quotes_gpc($PATH_INFO);
34       if (ereg('^' . PATH_INFO_PREFIX . '(..*)$', $PATH_INFO, $m))
35          return $m[1];
36    }
37
38    if (isset($QUERY_STRING) && preg_match('/^[^&=]+$/', $QUERY_STRING))
39       return urldecode(fix_magic_quotes_gpc($QUERY_STRING));
40
41    return gettext("HomePage");
42 }
43
44 $pagename = DeducePagename();
45
46 if (!empty($action))
47 {
48    $action = trim(fix_magic_quotes_gpc($action));
49 }
50 else if (isset($diff))
51 {
52    // Fix for compatibility with very old diff links in RecentChanges.
53    // (The [phpwiki:?diff=PageName] style links are fixed elsewhere.)
54    $action = 'diff';
55    $pagename = fix_magic_quotes_gpc($diff);
56    unset($diff);
57 }
58 else
59 {
60    $action = 'browse';
61 }
62
63 function IsSafeAction ($action)
64 {
65    if (! ZIPDUMP_AUTH and $action == 'zip')
66       return true;
67    return in_array ( $action, array('browse',
68                                     'info', 'diff', 'search',
69                                     'edit', 'save',
70                                     'login', 'logout',
71                                     'setprefs') );
72 }
73
74 function get_auth_mode ($action) 
75 {
76    switch ($action) {
77       case 'logout':
78          return  'LOGOUT';
79       case 'login':
80          return 'LOGIN';
81       default:
82          if (IsSafeAction($action))
83             return 'ANON_OK';
84          else
85             return 'REQUIRE_AUTH';
86    }
87 }
88
89    
90 $user = new WikiUser(get_auth_mode($action));
91 if ($user->is_authenticated())
92    $LogEntry->user = $user->id();
93
94
95
96 // All requests require the database
97 $dbi = OpenDataBase($WikiPageStore);
98
99 if ( $action == 'browse' && $pagename == gettext("HomePage") ) {
100    // if there is no HomePage, create a basic set of Wiki pages
101    if ( ! IsWikiPage($dbi, gettext("HomePage")) ) {
102       include_once("lib/loadsave.php");
103       SetupWiki($dbi);
104       ExitWiki();
105    }
106 }
107
108 // FIXME: I think this is redundant.
109 if (!IsSafeAction($action))
110    $user->must_be_admin($action);
111 if (isset($DisabledActions) && in_array($action, $DisabledActions))
112    ExitWiki(gettext("Action $action is disabled in this wiki."));
113    
114 // Enable the output of most of the warning messages.
115 // The warnings will screw up zip files and setpref though.
116 if ($action != 'zip' && $action != 'setprefs')
117    PostponeErrorMessages(E_NOTICE);
118
119 switch ($action) {
120    case 'edit':
121       include "lib/editpage.php";
122       break;
123
124    case 'search':
125       if (isset($searchtype) && ($searchtype == 'full')) {
126          include "lib/fullsearch.php";
127       }
128       else {
129          include "lib/search.php";
130       }
131       break;
132       
133    case 'save':
134       include "lib/savepage.php";
135       break;
136    case 'info':
137       include "lib/pageinfo.php";
138       break;
139    case 'diff':
140       include "lib/diff.php";
141       break;
142       
143    case 'zip':
144       include_once("lib/loadsave.php");
145       MakeWikiZip($dbi, isset($include) && $include == 'all');
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$RCS_IDS\n";
149       break;
150
151    case 'upload':
152       include_once("lib/loadsave.php");
153       LoadPostFile($dbi, 'file');
154       break;
155    
156    case 'dumpserial':
157       if (empty($directory))
158          ExitWiki(gettext("You must specify a directory to dump to"));
159
160       include_once("lib/loadsave.php");
161       DumpToDir($dbi, fix_magic_quotes_gpc($directory));
162       break;
163
164    case 'loadfile':
165       if (empty($source))
166          ExitWiki(gettext("You must specify a source to read from"));
167
168       include_once("lib/loadsave.php");
169       LoadFileOrDir($dbi, fix_magic_quotes_gpc($source));
170       break;
171
172    case 'remove':
173       include 'admin/removepage.php';
174       break;
175     
176    case 'lock':
177    case 'unlock':
178       include "admin/lockpage.php";
179       include "lib/display.php";
180       break;
181
182    case 'setprefs':
183       $prefs = $user->getPreferences($GLOBALS);
184       if (!empty($edit_area_width))
185          $prefs['edit_area.width'] = $edit_area_width;
186       if (!empty($edit_area_height))
187          $prefs['edit_area.height'] = $edit_area_height;
188       $user->setPreferences($prefs);
189
190       PostponeErrorMessages(E_ALL & ~E_NOTICE);
191
192       include "lib/display.php";
193       break;
194    
195    case 'browse':
196    case 'login':
197    case 'logout':
198       include "lib/display.php";
199       break;
200
201    default:
202       echo QElement('p', sprintf("Bad action: '%s'", urlencode($action)));
203       break;
204 }
205
206 ExitWiki();
207
208 // For emacs users
209 // Local Variables:
210 // mode: php
211 // c-file-style: "ellemtel"
212 // End:   
213 ?>