index.php 14 KB

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