Conditions | 34 |
Paths | 241 |
Total Lines | 230 |
Code Lines | 142 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
160 | function smf_main() |
||
161 | { |
||
162 | global $modSettings, $settings, $user_info, $board, $topic; |
||
163 | global $board_info, $maintenance, $sourcedir; |
||
164 | |||
165 | // Special case: session keep-alive, output a transparent pixel. |
||
166 | if (isset($_GET['action']) && $_GET['action'] == 'keepalive') |
||
167 | { |
||
168 | header('Content-Type: image/gif'); |
||
169 | die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B"); |
||
170 | } |
||
171 | |||
172 | // We should set our security headers now. |
||
173 | frameOptionsHeader(); |
||
174 | |||
175 | // Load the user's cookie (or set as guest) and load their settings. |
||
176 | loadUserSettings(); |
||
177 | |||
178 | // Load the current board's information. |
||
179 | loadBoard(); |
||
180 | |||
181 | // Load the current user's permissions. |
||
182 | loadPermissions(); |
||
183 | |||
184 | // Attachments don't require the entire theme to be loaded. |
||
185 | if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'dlattach') |
||
186 | detectBrowser(); |
||
187 | // Load the current theme. (note that ?theme=1 will also work, may be used for guest theming.) |
||
188 | else |
||
189 | loadTheme(); |
||
190 | |||
191 | // Check if the user should be disallowed access. |
||
192 | is_not_banned(); |
||
193 | |||
194 | // If we are in a topic and don't have permission to approve it then duck out now. |
||
195 | if (!empty($topic) && empty($board_info['cur_topic_approved']) && !allowedTo('approve_posts') && ($user_info['id'] != $board_info['cur_topic_starter'] || $user_info['is_guest'])) |
||
196 | fatal_lang_error('not_a_topic', false); |
||
197 | |||
198 | $no_stat_actions = array('clock', 'dlattach', 'findmember', 'jsoption', 'likes', 'loadeditorlocale', 'modifycat', 'requestmembers', 'smstats', 'suggest', 'about:unknown', '.xml', 'xmlhttp', 'verificationcode', 'viewquery', 'viewsmfile'); |
||
199 | call_integration_hook('integrate_pre_log_stats', array(&$no_stat_actions)); |
||
200 | // Do some logging, unless this is an attachment, avatar, toggle of editor buttons, theme option, XML feed etc. |
||
201 | if (empty($_REQUEST['action']) || !in_array($_REQUEST['action'], $no_stat_actions)) |
||
202 | { |
||
203 | // Log this user as online. |
||
204 | writeLog(); |
||
205 | |||
206 | // Track forum statistics and hits...? |
||
207 | if (!empty($modSettings['hitStats'])) |
||
208 | trackStats(array('hits' => '+')); |
||
209 | } |
||
210 | unset($no_stat_actions); |
||
211 | |||
212 | // Is the forum in maintenance mode? (doesn't apply to administrators.) |
||
213 | if (!empty($maintenance) && !allowedTo('admin_forum')) |
||
214 | { |
||
215 | // You can only login.... otherwise, you're getting the "maintenance mode" display. |
||
216 | if (isset($_REQUEST['action']) && (in_array($_REQUEST['action'], array('login2', 'logintfa', 'logout')))) |
||
217 | { |
||
218 | require_once($sourcedir . '/LogInOut.php'); |
||
219 | return ($_REQUEST['action'] == 'login2' ? 'Login2' : ($_REQUEST['action'] == 'logintfa' ? 'LoginTFA' : 'Logout')); |
||
220 | } |
||
221 | // Don't even try it, sonny. |
||
222 | else |
||
223 | return 'InMaintenance'; |
||
224 | } |
||
225 | // If guest access is off, a guest can only do one of the very few following actions. |
||
226 | elseif (empty($modSettings['allow_guestAccess']) && $user_info['is_guest'] && (!isset($_REQUEST['action']) || !in_array($_REQUEST['action'], array('coppa', 'login', 'login2', 'logintfa', 'reminder', 'activate', 'help', 'helpadmin', 'smstats', 'verificationcode', 'signup', 'signup2')))) |
||
227 | return 'KickGuest'; |
||
228 | elseif (empty($_REQUEST['action'])) |
||
229 | { |
||
230 | // Action and board are both empty... BoardIndex! Unless someone else wants to do something different. |
||
231 | if (empty($board) && empty($topic)) |
||
232 | { |
||
233 | if (!empty($modSettings['integrate_default_action'])) |
||
234 | { |
||
235 | $defaultAction = explode(',', $modSettings['integrate_default_action']); |
||
236 | |||
237 | // Sorry, only one default action is needed. |
||
238 | $defaultAction = $defaultAction[0]; |
||
239 | |||
240 | $call = call_helper($defaultAction, true); |
||
241 | |||
242 | if (!empty($call)) |
||
243 | return $call; |
||
244 | } |
||
245 | |||
246 | // No default action huh? then go to our good old BoardIndex. |
||
247 | else |
||
248 | { |
||
249 | require_once($sourcedir . '/BoardIndex.php'); |
||
250 | |||
251 | return 'BoardIndex'; |
||
252 | } |
||
253 | } |
||
254 | |||
255 | // Topic is empty, and action is empty.... MessageIndex! |
||
256 | elseif (empty($topic)) |
||
257 | { |
||
258 | require_once($sourcedir . '/MessageIndex.php'); |
||
259 | return 'MessageIndex'; |
||
260 | } |
||
261 | |||
262 | // Board is not empty... topic is not empty... action is empty.. Display! |
||
263 | else |
||
264 | { |
||
265 | require_once($sourcedir . '/Display.php'); |
||
266 | return 'Display'; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | // Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function). |
||
271 | $actionArray = array( |
||
272 | 'activate' => array('Register.php', 'Activate'), |
||
273 | 'admin' => array('Admin.php', 'AdminMain'), |
||
274 | 'announce' => array('Post.php', 'AnnounceTopic'), |
||
275 | 'attachapprove' => array('ManageAttachments.php', 'ApproveAttach'), |
||
276 | 'buddy' => array('Subs-Members.php', 'BuddyListToggle'), |
||
277 | 'calendar' => array('Calendar.php', 'CalendarMain'), |
||
278 | 'clock' => array('Calendar.php', 'clock'), |
||
279 | 'coppa' => array('Register.php', 'CoppaForm'), |
||
280 | 'credits' => array('Who.php', 'Credits'), |
||
281 | 'deletemsg' => array('RemoveTopic.php', 'DeleteMessage'), |
||
282 | 'dlattach' => array('ShowAttachments.php', 'showAttachment'), |
||
283 | 'editpoll' => array('Poll.php', 'EditPoll'), |
||
284 | 'editpoll2' => array('Poll.php', 'EditPoll2'), |
||
285 | 'findmember' => array('Subs-Auth.php', 'JSMembers'), |
||
286 | 'groups' => array('Groups.php', 'Groups'), |
||
287 | 'help' => array('Help.php', 'ShowHelp'), |
||
288 | 'helpadmin' => array('Help.php', 'ShowAdminHelp'), |
||
289 | 'jsmodify' => array('Post.php', 'JavaScriptModify'), |
||
290 | 'jsoption' => array('Themes.php', 'SetJavaScript'), |
||
291 | 'likes' => array('Likes.php', 'Likes::call#'), |
||
292 | 'loadeditorlocale' => array('Subs-Editor.php', 'loadLocale'), |
||
293 | 'lock' => array('Topic.php', 'LockTopic'), |
||
294 | 'lockvoting' => array('Poll.php', 'LockVoting'), |
||
295 | 'login' => array('LogInOut.php', 'Login'), |
||
296 | 'login2' => array('LogInOut.php', 'Login2'), |
||
297 | 'logintfa' => array('LogInOut.php', 'LoginTFA'), |
||
298 | 'logout' => array('LogInOut.php', 'Logout'), |
||
299 | 'markasread' => array('Subs-Boards.php', 'MarkRead'), |
||
300 | 'mergetopics' => array('SplitTopics.php', 'MergeTopics'), |
||
301 | 'mlist' => array('Memberlist.php', 'Memberlist'), |
||
302 | 'moderate' => array('ModerationCenter.php', 'ModerationMain'), |
||
303 | 'modifycat' => array('ManageBoards.php', 'ModifyCat'), |
||
304 | 'movetopic' => array('MoveTopic.php', 'MoveTopic'), |
||
305 | 'movetopic2' => array('MoveTopic.php', 'MoveTopic2'), |
||
306 | 'notify' => array('Notify.php', 'Notify'), |
||
307 | 'notifyboard' => array('Notify.php', 'BoardNotify'), |
||
308 | 'notifytopic' => array('Notify.php', 'TopicNotify'), |
||
309 | 'pm' => array('PersonalMessage.php', 'MessageMain'), |
||
310 | 'post' => array('Post.php', 'Post'), |
||
311 | 'post2' => array('Post.php', 'Post2'), |
||
312 | 'printpage' => array('Printpage.php', 'PrintTopic'), |
||
313 | 'profile' => array('Profile.php', 'ModifyProfile'), |
||
314 | 'quotefast' => array('Post.php', 'QuoteFast'), |
||
315 | 'quickmod' => array('MessageIndex.php', 'QuickModeration'), |
||
316 | 'quickmod2' => array('Display.php', 'QuickInTopicModeration'), |
||
317 | 'recent' => array('Recent.php', 'RecentPosts'), |
||
318 | 'reminder' => array('Reminder.php', 'RemindMe'), |
||
319 | 'removepoll' => array('Poll.php', 'RemovePoll'), |
||
320 | 'removetopic2' => array('RemoveTopic.php', 'RemoveTopic2'), |
||
321 | 'reporttm' => array('ReportToMod.php', 'ReportToModerator'), |
||
322 | 'requestmembers' => array('Subs-Auth.php', 'RequestMembers'), |
||
323 | 'restoretopic' => array('RemoveTopic.php', 'RestoreTopic'), |
||
324 | 'search' => array('Search.php', 'PlushSearch1'), |
||
325 | 'search2' => array('Search.php', 'PlushSearch2'), |
||
326 | 'sendactivation' => array('Register.php', 'SendActivation'), |
||
327 | 'signup' => array('Register.php', 'Register'), |
||
328 | 'signup2' => array('Register.php', 'Register2'), |
||
329 | 'smstats' => array('Stats.php', 'SMStats'), |
||
330 | 'suggest' => array('Subs-Editor.php', 'AutoSuggestHandler'), |
||
331 | 'spellcheck' => array('Subs-Post.php', 'SpellCheck'), |
||
332 | 'splittopics' => array('SplitTopics.php', 'SplitTopics'), |
||
333 | 'stats' => array('Stats.php', 'DisplayStats'), |
||
334 | 'sticky' => array('Topic.php', 'Sticky'), |
||
335 | 'theme' => array('Themes.php', 'ThemesMain'), |
||
336 | 'trackip' => array('Profile-View.php', 'trackIP'), |
||
337 | 'about:unknown' => array('Likes.php', 'BookOfUnknown'), |
||
338 | 'unread' => array('Recent.php', 'UnreadTopics'), |
||
339 | 'unreadreplies' => array('Recent.php', 'UnreadTopics'), |
||
340 | 'uploadAttach' => array('Attachments.php', 'Attachments::call#'), |
||
341 | 'verificationcode' => array('Register.php', 'VerificationCode'), |
||
342 | 'viewprofile' => array('Profile.php', 'ModifyProfile'), |
||
343 | 'vote' => array('Poll.php', 'Vote'), |
||
344 | 'viewquery' => array('ViewQuery.php', 'ViewQuery'), |
||
345 | 'viewsmfile' => array('Admin.php', 'DisplayAdminFile'), |
||
346 | 'who' => array('Who.php', 'Who'), |
||
347 | '.xml' => array('News.php', 'ShowXmlFeed'), |
||
348 | 'xmlhttp' => array('Xml.php', 'XMLhttpMain'), |
||
349 | ); |
||
350 | |||
351 | // Allow modifying $actionArray easily. |
||
352 | call_integration_hook('integrate_actions', array(&$actionArray)); |
||
353 | |||
354 | // Get the function and file to include - if it's not there, do the board index. |
||
355 | if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']])) |
||
356 | { |
||
357 | // Catch the action with the theme? |
||
358 | if (!empty($settings['catch_action'])) |
||
359 | { |
||
360 | require_once($sourcedir . '/Themes.php'); |
||
361 | return 'WrapAction'; |
||
362 | } |
||
363 | |||
364 | if (!empty($modSettings['integrate_fallback_action'])) |
||
365 | { |
||
366 | $fallbackAction = explode(',', $modSettings['integrate_fallback_action']); |
||
367 | |||
368 | // Sorry, only one fallback action is needed. |
||
369 | $fallbackAction = $fallbackAction[0]; |
||
370 | |||
371 | $call = call_helper($fallbackAction, true); |
||
372 | |||
373 | if (!empty($call)) |
||
374 | return $call; |
||
375 | } |
||
376 | |||
377 | // No fallback action, huh? |
||
378 | else |
||
379 | { |
||
380 | fatal_lang_error('not_found', false, array(), 404); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | // Otherwise, it was set - so let's go to that action. |
||
385 | require_once($sourcedir . '/' . $actionArray[$_REQUEST['action']][0]); |
||
386 | |||
387 | // Do the right thing. |
||
388 | return call_helper($actionArray[$_REQUEST['action']][1], true); |
||
389 | } |
||
390 | |||
391 | ?> |
||
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.