index.php 14 KB

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