index.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * This, as you have probably guessed, is the crux on which SMF functions.
  4. * Everything should start here, so all the setup and security is done
  5. * properly. The most interesting part of this file is the action array in
  6. * the smf_main() function. It is formatted as so:
  7. * 'action-in-url' => array('Source-File.php', 'FunctionToCall'),
  8. *
  9. * Then, you can access the FunctionToCall() function from Source-File.php
  10. * with the URL index.php?action=action-in-url. Relatively simple, no?
  11. *
  12. * Simple Machines Forum (SMF)
  13. *
  14. * @package SMF
  15. * @author Simple Machines http://www.simplemachines.org
  16. * @copyright 2014 Simple Machines and individual contributors
  17. * @license http://www.simplemachines.org/about/smf/license.php BSD
  18. *
  19. * @version 2.1 Alpha 1
  20. */
  21. $forum_version = 'SMF 2.1 Alpha 1';
  22. $software_year = '2014';
  23. // Get everything started up...
  24. define('SMF', 1);
  25. if (function_exists('set_magic_quotes_runtime'))
  26. @set_magic_quotes_runtime(0);
  27. error_reporting(defined('E_STRICT') ? E_ALL | E_STRICT : E_ALL);
  28. $time_start = microtime();
  29. // This makes it so headers can be sent!
  30. ob_start();
  31. // Do some cleaning, just in case.
  32. foreach (array('db_character_set', 'cachedir') as $variable)
  33. if (isset($GLOBALS[$variable]))
  34. unset($GLOBALS[$variable], $GLOBALS[$variable]);
  35. // Load the settings...
  36. require_once(dirname(__FILE__) . '/Settings.php');
  37. // Make absolutely sure the cache directory is defined.
  38. if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
  39. $cachedir = $boarddir . '/cache';
  40. // Without those we can't go anywhere
  41. require_once($sourcedir . '/QueryString.php');
  42. require_once($sourcedir . '/Subs.php');
  43. require_once($sourcedir . '/Errors.php');
  44. require_once($sourcedir . '/Load.php');
  45. // If $maintenance is set specifically to 2, then we're upgrading or something.
  46. if (!empty($maintenance) && $maintenance == 2)
  47. display_maintenance_message();
  48. // Create a variable to store some SMF specific functions in.
  49. $smcFunc = array();
  50. // Initiate the database connection and define some database functions to use.
  51. loadDatabase();
  52. // Load the settings from the settings table, and perform operations like optimizing.
  53. $context = array();
  54. reloadSettings();
  55. // Clean the request variables, add slashes, etc.
  56. cleanRequest();
  57. // Seed the random generator.
  58. if (empty($modSettings['rand_seed']) || mt_rand(1, 250) == 69)
  59. smf_seed_generator();
  60. // Before we get carried away, are we doing a scheduled task? If so save CPU cycles by jumping out!
  61. if (isset($_GET['scheduled']))
  62. {
  63. require_once($sourcedir . '/ScheduledTasks.php');
  64. AutoTask();
  65. }
  66. // Displaying attached avatars, legacy.
  67. elseif (isset($_GET['action']) && $_GET['action'] == 'dlattach' && isset($_GET['type']) && $_GET['type'] == 'avatar')
  68. {
  69. require_once($sourcedir. '/Avatar.php');
  70. showAvatar();
  71. }
  72. // And important includes.
  73. require_once($sourcedir . '/Session.php');
  74. require_once($sourcedir . '/Errors.php');
  75. require_once($sourcedir . '/Logging.php');
  76. require_once($sourcedir . '/Security.php');
  77. require_once($sourcedir . '/Class-BrowserDetect.php');
  78. // Using an pre-PHP 5.1 version?
  79. if (version_compare(PHP_VERSION, '5.1', '<'))
  80. require_once($sourcedir . '/Subs-Compat.php');
  81. // Check if compressed output is enabled, supported, and not already being done.
  82. if (!empty($modSettings['enableCompressedOutput']) && !headers_sent())
  83. {
  84. // If zlib is being used, turn off output compression.
  85. if (ini_get('zlib.output_compression') >= 1 || ini_get('output_handler') == 'ob_gzhandler')
  86. $modSettings['enableCompressedOutput'] = '0';
  87. else
  88. {
  89. ob_end_clean();
  90. ob_start('ob_gzhandler');
  91. }
  92. }
  93. // Register an error handler.
  94. set_error_handler('error_handler');
  95. // Start the session. (assuming it hasn't already been.)
  96. loadSession();
  97. // Determine if this is using WAP, WAP2, or imode. Technically, we should check that wap comes before application/xhtml or text/html, but this doesn't work in practice as much as it should.
  98. if (isset($_REQUEST['wap']) || isset($_REQUEST['wap2']) || isset($_REQUEST['imode']))
  99. unset($_SESSION['nowap']);
  100. elseif (isset($_REQUEST['nowap']))
  101. $_SESSION['nowap'] = true;
  102. elseif (!isset($_SESSION['nowap']))
  103. {
  104. if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/vnd.wap.xhtml+xml') !== false)
  105. $_REQUEST['wap2'] = 1;
  106. elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'text/vnd.wap.wml') !== false)
  107. {
  108. if (strpos($_SERVER['HTTP_USER_AGENT'], 'DoCoMo/') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'portalmmm/') !== false)
  109. $_REQUEST['imode'] = 1;
  110. else
  111. $_REQUEST['wap'] = 1;
  112. }
  113. }
  114. if (!defined('WIRELESS'))
  115. define('WIRELESS', isset($_REQUEST['wap']) || isset($_REQUEST['wap2']) || isset($_REQUEST['imode']));
  116. // Some settings and headers are different for wireless protocols.
  117. if (WIRELESS)
  118. {
  119. define('WIRELESS_PROTOCOL', isset($_REQUEST['wap']) ? 'wap' : (isset($_REQUEST['wap2']) ? 'wap2' : (isset($_REQUEST['imode']) ? 'imode' : '')));
  120. // Some cellphones can't handle output compression...
  121. // @todo shouldn't the phone handle that?
  122. $modSettings['enableCompressedOutput'] = '0';
  123. // @todo Do we want these hard coded?
  124. $modSettings['defaultMaxMessages'] = 5;
  125. $modSettings['defaultMaxTopics'] = 9;
  126. // Wireless protocol header.
  127. if (WIRELESS_PROTOCOL == 'wap')
  128. header('Content-Type: text/vnd.wap.wml');
  129. }
  130. // Restore post data if we are revalidating OpenID.
  131. if (isset($_GET['openid_restore_post']) && !empty($_SESSION['openid']['saved_data'][$_GET['openid_restore_post']]['post']) && empty($_POST))
  132. {
  133. $_POST = $_SESSION['openid']['saved_data'][$_GET['openid_restore_post']]['post'];
  134. unset($_SESSION['openid']['saved_data'][$_GET['openid_restore_post']]);
  135. }
  136. // What function shall we execute? (done like this for memory's sake.)
  137. call_user_func(smf_main());
  138. // Call obExit specially; we're coming from the main area ;).
  139. obExit(null, null, true);
  140. /**
  141. * The main dispatcher.
  142. * This delegates to each area.
  143. */
  144. function smf_main()
  145. {
  146. global $modSettings, $settings, $user_info, $board, $topic;
  147. global $board_info, $maintenance, $sourcedir, $db_show_debug, $context;
  148. // Special case: session keep-alive, output a transparent pixel.
  149. if (isset($_GET['action']) && $_GET['action'] == 'keepalive')
  150. {
  151. header('Content-Type: image/gif');
  152. 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");
  153. }
  154. // We should set our security headers now.
  155. frameOptionsHeader();
  156. // Load the user's cookie (or set as guest) and load their settings.
  157. loadUserSettings();
  158. // Load the current board's information.
  159. loadBoard();
  160. // Load the current user's permissions.
  161. loadPermissions();
  162. // Attachments don't require the entire theme to be loaded.
  163. if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'dlattach' && (!empty($modSettings['allow_guestAccess']) && $user_info['is_guest']))
  164. detectBrowser();
  165. // Load the current theme. (note that ?theme=1 will also work, may be used for guest theming.)
  166. else
  167. loadTheme();
  168. // Check if the user should be disallowed access.
  169. is_not_banned();
  170. // If we are in a topic and don't have permission to approve it then duck out now.
  171. if (!empty($topic) && empty($board_info['cur_topic_approved']) && !allowedTo('approve_posts') && ($user_info['id'] != $board_info['cur_topic_starter'] || $user_info['is_guest']))
  172. fatal_lang_error('not_a_topic', false);
  173. $no_stat_actions = array('clock', 'dlattach', 'findmember', 'jsoption', 'likes', 'loadeditorlocale', 'modifycat', 'requestmembers', 'smstats', 'suggest', 'about:unknown', '.xml', 'xmlhttp', 'verificationcode', 'viewquery', 'viewsmfile');
  174. call_integration_hook('integrate_pre_log_stats', array(&$no_stat_actions));
  175. // Do some logging, unless this is an attachment, avatar, toggle of editor buttons, theme option, XML feed etc.
  176. if (empty($_REQUEST['action']) || !in_array($_REQUEST['action'], $no_stat_actions))
  177. {
  178. // Log this user as online.
  179. writeLog();
  180. // Track forum statistics and hits...?
  181. if (!empty($modSettings['hitStats']))
  182. trackStats(array('hits' => '+'));
  183. }
  184. unset($no_stat_actions);
  185. // Is the forum in maintenance mode? (doesn't apply to administrators.)
  186. if (!empty($maintenance) && !allowedTo('admin_forum'))
  187. {
  188. // You can only login.... otherwise, you're getting the "maintenance mode" display.
  189. if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'login2' || $_REQUEST['action'] == 'logout'))
  190. {
  191. require_once($sourcedir . '/LogInOut.php');
  192. return $_REQUEST['action'] == 'login2' ? 'Login2' : 'Logout';
  193. }
  194. // Don't even try it, sonny.
  195. else
  196. {
  197. require_once($sourcedir . '/Subs-Auth.php');
  198. return 'InMaintenance';
  199. }
  200. }
  201. // If guest access is off, a guest can only do one of the very few following actions.
  202. elseif (empty($modSettings['allow_guestAccess']) && $user_info['is_guest'] && (!isset($_REQUEST['action']) || !in_array($_REQUEST['action'], array('coppa', 'login', 'login2', 'register', 'register2', 'reminder', 'activate', 'help', 'helpadmin', 'smstats', 'mailq', 'verificationcode', 'openidreturn'))))
  203. {
  204. require_once($sourcedir . '/Subs-Auth.php');
  205. return 'KickGuest';
  206. }
  207. elseif (empty($_REQUEST['action']))
  208. {
  209. // Action and board are both empty... BoardIndex! Unless someone else wants to do something different.
  210. if (empty($board) && empty($topic))
  211. {
  212. $defaultActions = call_integration_hook('integrate_default_action');
  213. foreach ($defaultActions as $defaultAction)
  214. {
  215. $call = strpos($defaultAction, '::') !== false ? explode('::', $defaultAction) : $defaultAction;
  216. if (!empty($call) && is_callable($call))
  217. return $call;
  218. }
  219. require_once($sourcedir . '/BoardIndex.php');
  220. return 'BoardIndex';
  221. }
  222. // Topic is empty, and action is empty.... MessageIndex!
  223. elseif (empty($topic))
  224. {
  225. require_once($sourcedir . '/MessageIndex.php');
  226. return 'MessageIndex';
  227. }
  228. // Board is not empty... topic is not empty... action is empty.. Display!
  229. else
  230. {
  231. require_once($sourcedir . '/Display.php');
  232. return 'Display';
  233. }
  234. }
  235. // Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function).
  236. $actionArray = array(
  237. 'activate' => array('Register.php', 'Activate'),
  238. 'admin' => array('Admin.php', 'AdminMain'),
  239. 'announce' => array('Post.php', 'AnnounceTopic'),
  240. 'attachapprove' => array('ManageAttachments.php', 'ApproveAttach'),
  241. 'buddy' => array('Subs-Members.php', 'BuddyListToggle'),
  242. 'calendar' => array('Calendar.php', 'CalendarMain'),
  243. 'clock' => array('Calendar.php', 'clock'),
  244. 'collapse' => array('BoardIndex.php', 'CollapseCategory'),
  245. 'coppa' => array('Register.php', 'CoppaForm'),
  246. 'credits' => array('Who.php', 'Credits'),
  247. 'deletemsg' => array('RemoveTopic.php', 'DeleteMessage'),
  248. 'dlattach' => array('Display.php', 'Download'),
  249. 'editpoll' => array('Poll.php', 'EditPoll'),
  250. 'editpoll2' => array('Poll.php', 'EditPoll2'),
  251. 'findmember' => array('Subs-Auth.php', 'JSMembers'),
  252. 'groups' => array('Groups.php', 'Groups'),
  253. 'help' => array('Help.php', 'ShowHelp'),
  254. 'helpadmin' => array('Help.php', 'ShowAdminHelp'),
  255. 'jsmodify' => array('Post.php', 'JavaScriptModify'),
  256. 'jsoption' => array('Themes.php', 'SetJavaScript'),
  257. 'likes' => array('Likes.php', 'Likes'),
  258. 'loadeditorlocale' => array('Subs-Editor.php', 'loadLocale'),
  259. 'lock' => array('Topic.php', 'LockTopic'),
  260. 'lockvoting' => array('Poll.php', 'LockVoting'),
  261. 'login' => array('LogInOut.php', 'Login'),
  262. 'login2' => array('LogInOut.php', 'Login2'),
  263. 'logout' => array('LogInOut.php', 'Logout'),
  264. 'markasread' => array('Subs-Boards.php', 'MarkRead'),
  265. 'mergetopics' => array('SplitTopics.php', 'MergeTopics'),
  266. 'mlist' => array('Memberlist.php', 'Memberlist'),
  267. 'moderate' => array('ModerationCenter.php', 'ModerationMain'),
  268. 'modifycat' => array('ManageBoards.php', 'ModifyCat'),
  269. 'modifykarma' => array('Karma.php', 'ModifyKarma'),
  270. 'movetopic' => array('MoveTopic.php', 'MoveTopic'),
  271. 'movetopic2' => array('MoveTopic.php', 'MoveTopic2'),
  272. 'notify' => array('Notify.php', 'Notify'),
  273. 'notifyboard' => array('Notify.php', 'BoardNotify'),
  274. 'openidreturn' => array('Subs-OpenID.php', 'smf_openID_return'),
  275. 'pm' => array('PersonalMessage.php', 'MessageMain'),
  276. 'post' => array('Post.php', 'Post'),
  277. 'post2' => array('Post.php', 'Post2'),
  278. 'printpage' => array('Printpage.php', 'PrintTopic'),
  279. 'profile' => array('Profile.php', 'ModifyProfile'),
  280. 'quotefast' => array('Post.php', 'QuoteFast'),
  281. 'quickmod' => array('MessageIndex.php', 'QuickModeration'),
  282. 'quickmod2' => array('Display.php', 'QuickInTopicModeration'),
  283. 'recent' => array('Recent.php', 'RecentPosts'),
  284. 'register' => array('Register.php', 'Register'),
  285. 'register2' => array('Register.php', 'Register2'),
  286. 'reminder' => array('Reminder.php', 'RemindMe'),
  287. 'removepoll' => array('Poll.php', 'RemovePoll'),
  288. 'removetopic2' => array('RemoveTopic.php', 'RemoveTopic2'),
  289. 'reporttm' => array('ReportToMod.php', 'ReportToModerator'),
  290. 'requestmembers' => array('Subs-Auth.php', 'RequestMembers'),
  291. 'restoretopic' => array('RemoveTopic.php', 'RestoreTopic'),
  292. 'search' => array('Search.php', 'PlushSearch1'),
  293. 'search2' => array('Search.php', 'PlushSearch2'),
  294. 'sendactivation' => array('Register.php', 'SendActivation'),
  295. 'smstats' => array('Stats.php', 'SMStats'),
  296. 'suggest' => array('Subs-Editor.php', 'AutoSuggestHandler'),
  297. 'spellcheck' => array('Subs-Post.php', 'SpellCheck'),
  298. 'splittopics' => array('SplitTopics.php', 'SplitTopics'),
  299. 'stats' => array('Stats.php', 'DisplayStats'),
  300. 'sticky' => array('Topic.php', 'Sticky'),
  301. 'theme' => array('Themes.php', 'ThemesMain'),
  302. 'trackip' => array('Profile-View.php', 'trackIP'),
  303. 'about:unknown' => array('Likes.php', 'BookOfUnknown'),
  304. 'unread' => array('Recent.php', 'UnreadTopics'),
  305. 'unreadreplies' => array('Recent.php', 'UnreadTopics'),
  306. 'unwatchtopic' => array('Notify.php', 'TopicUnwatch'),
  307. 'verificationcode' => array('Register.php', 'VerificationCode'),
  308. 'viewprofile' => array('Profile.php', 'ModifyProfile'),
  309. 'vote' => array('Poll.php', 'Vote'),
  310. 'viewquery' => array('ViewQuery.php', 'ViewQuery'),
  311. 'viewsmfile' => array('Admin.php', 'DisplayAdminFile'),
  312. 'who' => array('Who.php', 'Who'),
  313. '.xml' => array('News.php', 'ShowXmlFeed'),
  314. 'xmlhttp' => array('Xml.php', 'XMLhttpMain'),
  315. );
  316. // Allow modifying $actionArray easily.
  317. call_integration_hook('integrate_actions', array(&$actionArray));
  318. // Get the function and file to include - if it's not there, do the board index.
  319. if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']]))
  320. {
  321. // Catch the action with the theme?
  322. if (!empty($settings['catch_action']))
  323. {
  324. require_once($sourcedir . '/Themes.php');
  325. return 'WrapAction';
  326. }
  327. $fallbackActions = call_integration_hook('integrate_fallback_action');
  328. foreach ($fallbackActions as $fallbackAction)
  329. {
  330. $call = strpos($defaultAction, '::') !== false ? explode('::', $fallbackAction) : $fallbackAction;
  331. if (!empty($call) && is_callable($call))
  332. return $call;
  333. }
  334. // Fall through to the board index then...
  335. require_once($sourcedir . '/BoardIndex.php');
  336. return 'BoardIndex';
  337. }
  338. // Otherwise, it was set - so let's go to that action.
  339. require_once($sourcedir . '/' . $actionArray[$_REQUEST['action']][0]);
  340. // Found a method?
  341. if (strpos($actionArray[$_REQUEST['action']][1], '::') !== false)
  342. {
  343. // Handle it better.
  344. $string = $actionArray[$_REQUEST['action']][1];
  345. list($class, $method) = explode('::', $string);
  346. // Check if a new object will be created.
  347. if (strpos($method, '#') !== false)
  348. {
  349. // Need to remove the # thing.
  350. $method = str_replace('#', '', $method);
  351. // Don't need to create a new instance for every method.
  352. if (empty($context['instances'][$class]) || !($context['instances'][$class] instanceof $class))
  353. {
  354. $context['instances'][$class] = new $class;
  355. // Add another one to the list.
  356. if ($db_show_debug === true)
  357. {
  358. if (!isset($context['debug']['instances']))
  359. $context['debug']['instances'] = array();
  360. $context['debug']['instances'][$class] = $class;
  361. }
  362. }
  363. return array($context['instances'][$class], $method);
  364. }
  365. // Right then. This is a call to a static method.
  366. else
  367. return array($class, $method);
  368. }
  369. else
  370. return $actionArray[$_REQUEST['action']][1];
  371. }
  372. ?>