Completed
Pull Request — release-2.1 (#5058)
by 01
144:18
created

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This, as you have probably guessed, is the crux on which SMF functions.
5
 * Everything should start here, so all the setup and security is done
6
 * properly.  The most interesting part of this file is the action array in
7
 * the smf_main() function.  It is formatted as so:
8
 * 	'action-in-url' => array('Source-File.php', 'FunctionToCall'),
9
 *
10
 * Then, you can access the FunctionToCall() function from Source-File.php
11
 * with the URL index.php?action=action-in-url.  Relatively simple, no?
12
 *
13
 * Simple Machines Forum (SMF)
14
 *
15
 * @package SMF
16
 * @author Simple Machines http://www.simplemachines.org
17
 * @copyright 2018 Simple Machines and individual contributors
18
 * @license http://www.simplemachines.org/about/smf/license.php BSD
19
 *
20
 * @version 2.1 Beta 4
21
 */
22
23
$software_year = '2018';
24
$forum_version = 'SMF 2.1 Beta 4';
25
26
// Get everything started up...
27
define('SMF', 1);
28
error_reporting(E_ALL);
29
$time_start = microtime(true);
30
31
// This makes it so headers can be sent!
32
ob_start();
33
34
// Do some cleaning, just in case.
35 View Code Duplication
foreach (array('db_character_set', 'cachedir') as $variable)
36
	if (isset($GLOBALS[$variable]))
37
		unset($GLOBALS[$variable], $GLOBALS[$variable]);
38
39
// Load the settings...
40
require_once(dirname(__FILE__) . '/Settings.php');
41
42
// Make absolutely sure the cache directory is defined.
43 View Code Duplication
if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
44
	$cachedir = $boarddir . '/cache';
45
46
// Without those we can't go anywhere
47
require_once($sourcedir . '/QueryString.php');
48
require_once($sourcedir . '/Subs.php');
49
require_once($sourcedir . '/Subs-Auth.php');
50
require_once($sourcedir . '/Errors.php');
51
require_once($sourcedir . '/Load.php');
52
53
// If $maintenance is set specifically to 2, then we're upgrading or something.
54
if (!empty($maintenance) && $maintenance == 2)
55
	display_maintenance_message();
56
57
// Create a variable to store some SMF specific functions in.
58
$smcFunc = array();
59
60
// Initiate the database connection and define some database functions to use.
61
loadDatabase();
62
63
// Load the settings from the settings table, and perform operations like optimizing.
64
$context = array();
65
reloadSettings();
66
// Clean the request variables, add slashes, etc.
67
cleanRequest();
68
69
// Seed the random generator.
70
if (empty($modSettings['rand_seed']) || mt_rand(1, 250) == 69)
71
	smf_seed_generator();
72
73
// Before we get carried away, are we doing a scheduled task? If so save CPU cycles by jumping out!
74
if (isset($_GET['scheduled']))
75
{
76
	require_once($sourcedir . '/ScheduledTasks.php');
77
	AutoTask();
78
}
79
80
// And important includes.
81
require_once($sourcedir . '/Session.php');
82
require_once($sourcedir . '/Errors.php');
83
require_once($sourcedir . '/Logging.php');
84
require_once($sourcedir . '/Security.php');
85
require_once($sourcedir . '/Class-BrowserDetect.php');
86
87
// Check if compressed output is enabled, supported, and not already being done.
88
if (!empty($modSettings['enableCompressedOutput']) && !headers_sent())
89
{
90
	// If zlib is being used, turn off output compression.
91 View Code Duplication
	if (ini_get('zlib.output_compression') >= 1 || ini_get('output_handler') == 'ob_gzhandler')
92
		$modSettings['enableCompressedOutput'] = '0';
93
	else
94
	{
95
		ob_end_clean();
96
		ob_start('ob_gzhandler');
97
	}
98
}
99
100
/**
101
 * An autoloader for certain classes.
102
 *
103
 * @param string $class The fully-qualified class name.
104
 */
105 View Code Duplication
spl_autoload_register(function ($class) use ($sourcedir)
106
{
107
	$classMap = array(
108
		'ReCaptcha\\' => 'ReCaptcha/',
109
		'MatthiasMullie\\Minify\\' => 'minify/src/',
110
		'MatthiasMullie\\PathConverter\\' => 'minify/path-converter/src/',
111
	);
112
113
	// Do any third-party scripts want in on the fun?
114
	call_integration_hook('integrate_autoload', array(&$classMap));
115
116
	foreach ($classMap as $prefix => $dirName)
117
	{
118
		// does the class use the namespace prefix?
119
		$len = strlen($prefix);
120
		if (strncmp($prefix, $class, $len) !== 0)
121
		{
122
			continue;
123
		}
124
125
		// get the relative class name
126
		$relativeClass = substr($class, $len);
127
128
		// replace the namespace prefix with the base directory, replace namespace
129
		// separators with directory separators in the relative class name, append
130
		// with .php
131
		$fileName = $dirName . strtr($relativeClass, '\\', '/') . '.php';
132
133
		// if the file exists, require it
134
		if (file_exists($fileName = $sourcedir . '/' . $fileName))
135
		{
136
			require_once $fileName;
137
138
			return;
139
		}
140
	}
141
});
142
143
// Register an error handler.
144
set_error_handler('smf_error_handler');
145
146
// Start the session. (assuming it hasn't already been.)
147
loadSession();
148
149
// What function shall we execute? (done like this for memory's sake.)
150
call_user_func(smf_main());
151
152
// Call obExit specially; we're coming from the main area ;).
153
obExit(null, null, true);
154
155
/**
156
 * The main dispatcher.
157
 * This delegates to each area.
158
 * @return array|string|void An array containing the file to include and name of function to call, the name of a function to call or dies with a fatal_lang_error if we couldn't find anything to do.
0 ignored issues
show
Should the return type not be null|string|array|boolean? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
159
 */
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
	// Make sure that our scheduled tasks have been running as intended
213
	check_cron();
214
215
	// Is the forum in maintenance mode? (doesn't apply to administrators.)
216
	if (!empty($maintenance) && !allowedTo('admin_forum'))
217
	{
218
		// You can only login.... otherwise, you're getting the "maintenance mode" display.
219
		if (isset($_REQUEST['action']) && (in_array($_REQUEST['action'], array('login2', 'logintfa', 'logout'))))
220
		{
221
			require_once($sourcedir . '/LogInOut.php');
222
			return ($_REQUEST['action'] == 'login2' ? 'Login2' : ($_REQUEST['action'] == 'logintfa' ? 'LoginTFA' : 'Logout'));
223
		}
224
		// Don't even try it, sonny.
225
		else
226
			return 'InMaintenance';
227
	}
228
	// If guest access is off, a guest can only do one of the very few following actions.
229
	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'))))
230
		return 'KickGuest';
231
	elseif (empty($_REQUEST['action']))
232
	{
233
		// Action and board are both empty... BoardIndex! Unless someone else wants to do something different.
234
		if (empty($board) && empty($topic))
235
		{
236
			if (!empty($modSettings['integrate_default_action']))
237
			{
238
				$defaultAction = explode(',', $modSettings['integrate_default_action']);
239
240
				// Sorry, only one default action is needed.
241
				$defaultAction = $defaultAction[0];
242
243
				$call = call_helper($defaultAction, true);
244
245
				if (!empty($call))
246
					return $call;
247
			}
248
249
			// No default action huh? then go to our good old BoardIndex.
250
			else
251
			{
252
				require_once($sourcedir . '/BoardIndex.php');
253
254
				return 'BoardIndex';
255
			}
256
		}
257
258
		// Topic is empty, and action is empty.... MessageIndex!
259
		elseif (empty($topic))
260
		{
261
			require_once($sourcedir . '/MessageIndex.php');
262
			return 'MessageIndex';
263
		}
264
265
		// Board is not empty... topic is not empty... action is empty.. Display!
266
		else
267
		{
268
			require_once($sourcedir . '/Display.php');
269
			return 'Display';
270
		}
271
	}
272
273
	// Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function).
274
	$actionArray = array(
275
		'activate' => array('Register.php', 'Activate'),
276
		'admin' => array('Admin.php', 'AdminMain'),
277
		'announce' => array('Post.php', 'AnnounceTopic'),
278
		'attachapprove' => array('ManageAttachments.php', 'ApproveAttach'),
279
		'buddy' => array('Subs-Members.php', 'BuddyListToggle'),
280
		'calendar' => array('Calendar.php', 'CalendarMain'),
281
		'clock' => array('Calendar.php', 'clock'),
282
		'coppa' => array('Register.php', 'CoppaForm'),
283
		'credits' => array('Who.php', 'Credits'),
284
		'deletemsg' => array('RemoveTopic.php', 'DeleteMessage'),
285
		'dlattach' => array('ShowAttachments.php', 'showAttachment'),
286
		'editpoll' => array('Poll.php', 'EditPoll'),
287
		'editpoll2' => array('Poll.php', 'EditPoll2'),
288
		'findmember' => array('Subs-Auth.php', 'JSMembers'),
289
		'groups' => array('Groups.php', 'Groups'),
290
		'help' => array('Help.php', 'ShowHelp'),
291
		'helpadmin' => array('Help.php', 'ShowAdminHelp'),
292
		'jsmodify' => array('Post.php', 'JavaScriptModify'),
293
		'jsoption' => array('Themes.php', 'SetJavaScript'),
294
		'likes' => array('Likes.php', 'Likes::call#'),
295
		'loadeditorlocale' => array('Subs-Editor.php', 'loadLocale'),
296
		'lock' => array('Topic.php', 'LockTopic'),
297
		'lockvoting' => array('Poll.php', 'LockVoting'),
298
		'login' => array('LogInOut.php', 'Login'),
299
		'login2' => array('LogInOut.php', 'Login2'),
300
		'logintfa' => array('LogInOut.php', 'LoginTFA'),
301
		'logout' => array('LogInOut.php', 'Logout'),
302
		'markasread' => array('Subs-Boards.php', 'MarkRead'),
303
		'mergetopics' => array('SplitTopics.php', 'MergeTopics'),
304
		'mlist' => array('Memberlist.php', 'Memberlist'),
305
		'moderate' => array('ModerationCenter.php', 'ModerationMain'),
306
		'modifycat' => array('ManageBoards.php', 'ModifyCat'),
307
		'movetopic' => array('MoveTopic.php', 'MoveTopic'),
308
		'movetopic2' => array('MoveTopic.php', 'MoveTopic2'),
309
		'notify' => array('Notify.php', 'Notify'),
310
		'notifyboard' => array('Notify.php', 'BoardNotify'),
311
		'notifytopic' => array('Notify.php', 'TopicNotify'),
312
		'pm' => array('PersonalMessage.php', 'MessageMain'),
313
		'post' => array('Post.php', 'Post'),
314
		'post2' => array('Post.php', 'Post2'),
315
		'printpage' => array('Printpage.php', 'PrintTopic'),
316
		'profile' => array('Profile.php', 'ModifyProfile'),
317
		'quotefast' => array('Post.php', 'QuoteFast'),
318
		'quickmod' => array('MessageIndex.php', 'QuickModeration'),
319
		'quickmod2' => array('Display.php', 'QuickInTopicModeration'),
320
		'recent' => array('Recent.php', 'RecentPosts'),
321
		'reminder' => array('Reminder.php', 'RemindMe'),
322
		'removepoll' => array('Poll.php', 'RemovePoll'),
323
		'removetopic2' => array('RemoveTopic.php', 'RemoveTopic2'),
324
		'reporttm' => array('ReportToMod.php', 'ReportToModerator'),
325
		'requestmembers' => array('Subs-Auth.php', 'RequestMembers'),
326
		'restoretopic' => array('RemoveTopic.php', 'RestoreTopic'),
327
		'search' => array('Search.php', 'PlushSearch1'),
328
		'search2' => array('Search.php', 'PlushSearch2'),
329
		'sendactivation' => array('Register.php', 'SendActivation'),
330
		'signup' => array('Register.php', 'Register'),
331
		'signup2' => array('Register.php', 'Register2'),
332
		'smstats' => array('Stats.php', 'SMStats'),
333
		'suggest' => array('Subs-Editor.php', 'AutoSuggestHandler'),
334
		'spellcheck' => array('Subs-Post.php', 'SpellCheck'),
335
		'splittopics' => array('SplitTopics.php', 'SplitTopics'),
336
		'stats' => array('Stats.php', 'DisplayStats'),
337
		'sticky' => array('Topic.php', 'Sticky'),
338
		'theme' => array('Themes.php', 'ThemesMain'),
339
		'trackip' => array('Profile-View.php', 'trackIP'),
340
		'about:unknown' => array('Likes.php', 'BookOfUnknown'),
341
		'unread' => array('Recent.php', 'UnreadTopics'),
342
		'unreadreplies' => array('Recent.php', 'UnreadTopics'),
343
		'uploadAttach' => array('Attachments.php', 'Attachments::call#'),
344
		'verificationcode' => array('Register.php', 'VerificationCode'),
345
		'viewprofile' => array('Profile.php', 'ModifyProfile'),
346
		'vote' => array('Poll.php', 'Vote'),
347
		'viewquery' => array('ViewQuery.php', 'ViewQuery'),
348
		'viewsmfile' => array('Admin.php', 'DisplayAdminFile'),
349
		'who' => array('Who.php', 'Who'),
350
		'.xml' => array('News.php', 'ShowXmlFeed'),
351
		'xmlhttp' => array('Xml.php', 'XMLhttpMain'),
352
	);
353
354
	// Allow modifying $actionArray easily.
355
	call_integration_hook('integrate_actions', array(&$actionArray));
356
357
	// Get the function and file to include - if it's not there, do the board index.
358
	if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']]))
359
	{
360
		// Catch the action with the theme?
361
		if (!empty($settings['catch_action']))
362
		{
363
			require_once($sourcedir . '/Themes.php');
364
			return 'WrapAction';
365
		}
366
367
		if (!empty($modSettings['integrate_fallback_action']))
368
		{
369
			$fallbackAction = explode(',', $modSettings['integrate_fallback_action']);
370
371
			// Sorry, only one fallback action is needed.
372
			$fallbackAction = $fallbackAction[0];
373
374
			$call = call_helper($fallbackAction, true);
375
376
			if (!empty($call))
377
				return $call;
378
		}
379
380
		// No fallback action, huh?
381
		else
382
		{
383
			fatal_lang_error('not_found', false, array(), 404);
384
		}
385
	}
386
387
	// Otherwise, it was set - so let's go to that action.
388
	if (!empty($actionArray[$_REQUEST['action']][0]))
389
		require_once($sourcedir . '/' . $actionArray[$_REQUEST['action']][0]);
390
391
	// Do the right thing.
392
	return call_helper($actionArray[$_REQUEST['action']][1], true);
393
}
394
395
?>