Conditions | 34 |
Paths | 241 |
Total Lines | 232 |
Code Lines | 143 |
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 |
||
119 | function smf_main() |
||
120 | { |
||
121 | global $modSettings, $settings, $user_info, $board, $topic; |
||
122 | global $board_info, $maintenance, $sourcedir; |
||
123 | |||
124 | // Special case: session keep-alive, output a transparent pixel. |
||
125 | if (isset($_GET['action']) && $_GET['action'] == 'keepalive') |
||
126 | { |
||
127 | header('Content-Type: image/gif'); |
||
128 | 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"); |
||
129 | } |
||
130 | |||
131 | // We should set our security headers now. |
||
132 | frameOptionsHeader(); |
||
133 | |||
134 | // Load the user's cookie (or set as guest) and load their settings. |
||
135 | loadUserSettings(); |
||
136 | |||
137 | // Load the current board's information. |
||
138 | loadBoard(); |
||
139 | |||
140 | // Load the current user's permissions. |
||
141 | loadPermissions(); |
||
142 | |||
143 | // Attachments don't require the entire theme to be loaded. |
||
144 | if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'dlattach') |
||
145 | detectBrowser(); |
||
146 | // Load the current theme. (note that ?theme=1 will also work, may be used for guest theming.) |
||
147 | else |
||
148 | loadTheme(); |
||
149 | |||
150 | // Check if the user should be disallowed access. |
||
151 | is_not_banned(); |
||
152 | |||
153 | // If we are in a topic and don't have permission to approve it then duck out now. |
||
154 | if (!empty($topic) && empty($board_info['cur_topic_approved']) && !allowedTo('approve_posts') && ($user_info['id'] != $board_info['cur_topic_starter'] || $user_info['is_guest'])) |
||
155 | fatal_lang_error('not_a_topic', false); |
||
156 | |||
157 | $no_stat_actions = array('clock', 'dlattach', 'findmember', 'jsoption', 'likes', 'loadeditorlocale', 'modifycat', 'requestmembers', 'smstats', 'suggest', 'about:unknown', '.xml', 'xmlhttp', 'verificationcode', 'viewquery', 'viewsmfile'); |
||
158 | call_integration_hook('integrate_pre_log_stats', array(&$no_stat_actions)); |
||
159 | // Do some logging, unless this is an attachment, avatar, toggle of editor buttons, theme option, XML feed etc. |
||
160 | if (empty($_REQUEST['action']) || !in_array($_REQUEST['action'], $no_stat_actions)) |
||
161 | { |
||
162 | // Log this user as online. |
||
163 | writeLog(); |
||
164 | |||
165 | // Track forum statistics and hits...? |
||
166 | if (!empty($modSettings['hitStats'])) |
||
167 | trackStats(array('hits' => '+')); |
||
168 | } |
||
169 | unset($no_stat_actions); |
||
170 | |||
171 | // Is the forum in maintenance mode? (doesn't apply to administrators.) |
||
172 | if (!empty($maintenance) && !allowedTo('admin_forum')) |
||
173 | { |
||
174 | // You can only login.... otherwise, you're getting the "maintenance mode" display. |
||
175 | if (isset($_REQUEST['action']) && (in_array($_REQUEST['action'], array('login2', 'logintfa', 'logout')))) |
||
176 | { |
||
177 | require_once($sourcedir . '/LogInOut.php'); |
||
178 | return ($_REQUEST['action'] == 'login2' ? 'Login2' : ($_REQUEST['action'] == 'logintfa' ? 'LoginTFA' : 'Logout')); |
||
179 | } |
||
180 | // Don't even try it, sonny. |
||
181 | else |
||
182 | return 'InMaintenance'; |
||
183 | } |
||
184 | // If guest access is off, a guest can only do one of the very few following actions. |
||
185 | 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')))) |
||
186 | return 'KickGuest'; |
||
187 | elseif (empty($_REQUEST['action'])) |
||
188 | { |
||
189 | // Action and board are both empty... BoardIndex! Unless someone else wants to do something different. |
||
190 | if (empty($board) && empty($topic)) |
||
191 | { |
||
192 | $defaultAction = false; |
||
193 | |||
194 | if (!empty($modSettings['integrate_default_action'])) |
||
195 | { |
||
196 | $defaultAction = explode(',', $modSettings['integrate_default_action']); |
||
197 | |||
198 | // Sorry, only one default action is needed. |
||
199 | $defaultAction = $defaultAction[0]; |
||
200 | |||
201 | $call = call_helper($defaultAction, true); |
||
202 | |||
203 | if (!empty($call)) |
||
204 | return $call; |
||
205 | } |
||
206 | |||
207 | // No default action huh? then go to our good old BoardIndex. |
||
208 | else |
||
209 | { |
||
210 | require_once($sourcedir . '/BoardIndex.php'); |
||
211 | |||
212 | return 'BoardIndex'; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | // Topic is empty, and action is empty.... MessageIndex! |
||
217 | elseif (empty($topic)) |
||
218 | { |
||
219 | require_once($sourcedir . '/MessageIndex.php'); |
||
220 | return 'MessageIndex'; |
||
221 | } |
||
222 | |||
223 | // Board is not empty... topic is not empty... action is empty.. Display! |
||
224 | else |
||
225 | { |
||
226 | require_once($sourcedir . '/Display.php'); |
||
227 | return 'Display'; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | // Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function). |
||
232 | $actionArray = array( |
||
233 | 'activate' => array('Register.php', 'Activate'), |
||
234 | 'admin' => array('Admin.php', 'AdminMain'), |
||
235 | 'announce' => array('Post.php', 'AnnounceTopic'), |
||
236 | 'attachapprove' => array('ManageAttachments.php', 'ApproveAttach'), |
||
237 | 'buddy' => array('Subs-Members.php', 'BuddyListToggle'), |
||
238 | 'calendar' => array('Calendar.php', 'CalendarMain'), |
||
239 | 'clock' => array('Calendar.php', 'clock'), |
||
240 | 'coppa' => array('Register.php', 'CoppaForm'), |
||
241 | 'credits' => array('Who.php', 'Credits'), |
||
242 | 'deletemsg' => array('RemoveTopic.php', 'DeleteMessage'), |
||
243 | 'dlattach' => array('ShowAttachments.php', 'showAttachment'), |
||
244 | 'editpoll' => array('Poll.php', 'EditPoll'), |
||
245 | 'editpoll2' => array('Poll.php', 'EditPoll2'), |
||
246 | 'findmember' => array('Subs-Auth.php', 'JSMembers'), |
||
247 | 'groups' => array('Groups.php', 'Groups'), |
||
248 | 'help' => array('Help.php', 'ShowHelp'), |
||
249 | 'helpadmin' => array('Help.php', 'ShowAdminHelp'), |
||
250 | 'jsmodify' => array('Post.php', 'JavaScriptModify'), |
||
251 | 'jsoption' => array('Themes.php', 'SetJavaScript'), |
||
252 | 'likes' => array('Likes.php', 'Likes::call#'), |
||
253 | 'loadeditorlocale' => array('Subs-Editor.php', 'loadLocale'), |
||
254 | 'lock' => array('Topic.php', 'LockTopic'), |
||
255 | 'lockvoting' => array('Poll.php', 'LockVoting'), |
||
256 | 'login' => array('LogInOut.php', 'Login'), |
||
257 | 'login2' => array('LogInOut.php', 'Login2'), |
||
258 | 'logintfa' => array('LogInOut.php', 'LoginTFA'), |
||
259 | 'logout' => array('LogInOut.php', 'Logout'), |
||
260 | 'markasread' => array('Subs-Boards.php', 'MarkRead'), |
||
261 | 'mergetopics' => array('SplitTopics.php', 'MergeTopics'), |
||
262 | 'mlist' => array('Memberlist.php', 'Memberlist'), |
||
263 | 'moderate' => array('ModerationCenter.php', 'ModerationMain'), |
||
264 | 'modifycat' => array('ManageBoards.php', 'ModifyCat'), |
||
265 | 'movetopic' => array('MoveTopic.php', 'MoveTopic'), |
||
266 | 'movetopic2' => array('MoveTopic.php', 'MoveTopic2'), |
||
267 | 'notify' => array('Notify.php', 'Notify'), |
||
268 | 'notifyboard' => array('Notify.php', 'BoardNotify'), |
||
269 | 'notifytopic' => array('Notify.php', 'TopicNotify'), |
||
270 | 'pm' => array('PersonalMessage.php', 'MessageMain'), |
||
271 | 'post' => array('Post.php', 'Post'), |
||
272 | 'post2' => array('Post.php', 'Post2'), |
||
273 | 'printpage' => array('Printpage.php', 'PrintTopic'), |
||
274 | 'profile' => array('Profile.php', 'ModifyProfile'), |
||
275 | 'quotefast' => array('Post.php', 'QuoteFast'), |
||
276 | 'quickmod' => array('MessageIndex.php', 'QuickModeration'), |
||
277 | 'quickmod2' => array('Display.php', 'QuickInTopicModeration'), |
||
278 | 'recent' => array('Recent.php', 'RecentPosts'), |
||
279 | 'reminder' => array('Reminder.php', 'RemindMe'), |
||
280 | 'removepoll' => array('Poll.php', 'RemovePoll'), |
||
281 | 'removetopic2' => array('RemoveTopic.php', 'RemoveTopic2'), |
||
282 | 'reporttm' => array('ReportToMod.php', 'ReportToModerator'), |
||
283 | 'requestmembers' => array('Subs-Auth.php', 'RequestMembers'), |
||
284 | 'restoretopic' => array('RemoveTopic.php', 'RestoreTopic'), |
||
285 | 'search' => array('Search.php', 'PlushSearch1'), |
||
286 | 'search2' => array('Search.php', 'PlushSearch2'), |
||
287 | 'sendactivation' => array('Register.php', 'SendActivation'), |
||
288 | 'signup' => array('Register.php', 'Register'), |
||
289 | 'signup2' => array('Register.php', 'Register2'), |
||
290 | 'smstats' => array('Stats.php', 'SMStats'), |
||
291 | 'suggest' => array('Subs-Editor.php', 'AutoSuggestHandler'), |
||
292 | 'spellcheck' => array('Subs-Post.php', 'SpellCheck'), |
||
293 | 'splittopics' => array('SplitTopics.php', 'SplitTopics'), |
||
294 | 'stats' => array('Stats.php', 'DisplayStats'), |
||
295 | 'sticky' => array('Topic.php', 'Sticky'), |
||
296 | 'theme' => array('Themes.php', 'ThemesMain'), |
||
297 | 'trackip' => array('Profile-View.php', 'trackIP'), |
||
298 | 'about:unknown' => array('Likes.php', 'BookOfUnknown'), |
||
299 | 'unread' => array('Recent.php', 'UnreadTopics'), |
||
300 | 'unreadreplies' => array('Recent.php', 'UnreadTopics'), |
||
301 | 'uploadAttach' => array('Attachments.php', 'Attachments::call#'), |
||
302 | 'verificationcode' => array('Register.php', 'VerificationCode'), |
||
303 | 'viewprofile' => array('Profile.php', 'ModifyProfile'), |
||
304 | 'vote' => array('Poll.php', 'Vote'), |
||
305 | 'viewquery' => array('ViewQuery.php', 'ViewQuery'), |
||
306 | 'viewsmfile' => array('Admin.php', 'DisplayAdminFile'), |
||
307 | 'who' => array('Who.php', 'Who'), |
||
308 | '.xml' => array('News.php', 'ShowXmlFeed'), |
||
309 | 'xmlhttp' => array('Xml.php', 'XMLhttpMain'), |
||
310 | ); |
||
311 | |||
312 | // Allow modifying $actionArray easily. |
||
313 | call_integration_hook('integrate_actions', array(&$actionArray)); |
||
314 | |||
315 | // Get the function and file to include - if it's not there, do the board index. |
||
316 | if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']])) |
||
317 | { |
||
318 | // Catch the action with the theme? |
||
319 | if (!empty($settings['catch_action'])) |
||
320 | { |
||
321 | require_once($sourcedir . '/Themes.php'); |
||
322 | return 'WrapAction'; |
||
323 | } |
||
324 | |||
325 | if (!empty($modSettings['integrate_fallback_action'])) |
||
326 | { |
||
327 | $fallbackAction = explode(',', $modSettings['integrate_fallback_action']); |
||
328 | |||
329 | // Sorry, only one fallback action is needed. |
||
330 | $fallbackAction = $fallbackAction[0]; |
||
331 | |||
332 | $call = call_helper($fallbackAction, true); |
||
333 | |||
334 | if (!empty($call)) |
||
335 | return $call; |
||
336 | } |
||
337 | |||
338 | // No fallback action, huh? |
||
339 | else |
||
340 | { |
||
341 | fatal_lang_error('not_found', false, array(), 404); |
||
342 | } |
||
343 | } |
||
344 | |||
345 | // Otherwise, it was set - so let's go to that action. |
||
346 | require_once($sourcedir . '/' . $actionArray[$_REQUEST['action']][0]); |
||
347 | |||
348 | // Do the right thing. |
||
349 | return call_helper($actionArray[$_REQUEST['action']][1], true); |
||
350 | } |
||
351 | |||
353 |
If you suppress an error, we recommend checking for the error condition explicitly: