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