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