Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

script_start.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /*-- Script Start Class --------------------------------*/
  3. /*------------------------------------------------------*/
  4. /* This isnt really a class but a way to tie other */
  5. /* classes and functions used all over the site to the */
  6. /* page currently being displayed. */
  7. /*------------------------------------------------------*/
  8. /* The code that includes the main php files and */
  9. /* generates the page are at the bottom. */
  10. /*------------------------------------------------------*/
  11. /********************************************************/
  12. require 'config.php'; //The config contains all site wide configuration information
  13. //Deal with dumbasses
  14. if (isset($_REQUEST['info_hash']) && isset($_REQUEST['peer_id'])) {
  15. die('d14:failure reason40:Invalid .torrent, try downloading again.e');
  16. }
  17. require(SERVER_ROOT.'/classes/proxies.class.php');
  18. // Get the user's actual IP address if they're proxied.
  19. // Or if cloudflare is used
  20. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
  21. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  22. }
  23. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])
  24. && proxyCheck($_SERVER['REMOTE_ADDR'])
  25. && filter_var($_SERVER['HTTP_X_FORWARDED_FOR'],
  26. FILTER_VALIDATE_IP,
  27. FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  28. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
  29. }
  30. $SSL = true; #(isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443);
  31. if (!isset($argv) && !empty($_SERVER['HTTP_HOST'])) {
  32. //Skip this block if running from cli or if the browser is old and shitty
  33. if (!$SSL && $_SERVER['HTTP_HOST'] == 'www.'.NONSSL_SITE_URL) {
  34. header('Location: http://'.NONSSL_SITE_URL.$_SERVER['REQUEST_URI']); die();
  35. }
  36. if ($SSL && $_SERVER['HTTP_HOST'] == 'www.'.NONSSL_SITE_URL) {
  37. header('Location: https://'.SSL_SITE_URL.$_SERVER['REQUEST_URI']); die();
  38. }
  39. if (SSL_SITE_URL != NONSSL_SITE_URL) {
  40. if (!$SSL && $_SERVER['HTTP_HOST'] == SSL_SITE_URL) {
  41. header('Location: https://'.SSL_SITE_URL.$_SERVER['REQUEST_URI']); die();
  42. }
  43. if ($SSL && $_SERVER['HTTP_HOST'] == NONSSL_SITE_URL) {
  44. header('Location: https://'.SSL_SITE_URL.$_SERVER['REQUEST_URI']); die();
  45. }
  46. }
  47. if ($_SERVER['HTTP_HOST'] == 'www.m.'.NONSSL_SITE_URL) {
  48. header('Location: http://m.'.NONSSL_SITE_URL.$_SERVER['REQUEST_URI']); die();
  49. }
  50. }
  51. $ScriptStartTime = microtime(true); //To track how long a page takes to create
  52. if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
  53. $RUsage = getrusage();
  54. $CPUTimeStart = $RUsage['ru_utime.tv_sec'] * 1000000 + $RUsage['ru_utime.tv_usec'];
  55. }
  56. ob_start(); //Start a buffer, mainly in case there is a mysql error
  57. require(SERVER_ROOT.'/classes/debug.class.php'); //Require the debug class
  58. require(SERVER_ROOT.'/classes/mysql.class.php'); //Require the database wrapper
  59. require(SERVER_ROOT.'/classes/cache.class.php'); //Require the caching class
  60. require(SERVER_ROOT.'/classes/encrypt.class.php'); //Require the encryption class
  61. require(SERVER_ROOT.'/classes/time.class.php'); //Require the time class
  62. require(SERVER_ROOT.'/classes/paranoia.class.php'); //Require the paranoia check_paranoia function
  63. require(SERVER_ROOT.'/classes/regex.php');
  64. require(SERVER_ROOT.'/classes/util.php');
  65. $Debug = new DEBUG;
  66. $Debug->handle_errors();
  67. $Debug->set_flag('Debug constructed');
  68. $DB = new DB_MYSQL;
  69. $Cache = new CACHE(MEMCACHED_SERVERS);
  70. $Enc = new CRYPT;
  71. // Autoload classes.
  72. require(SERVER_ROOT.'/classes/classloader.php');
  73. // Note: G::initialize is called twice.
  74. // This is necessary as the code inbetween (initialization of $LoggedUser) makes use of G::$DB and G::$Cache.
  75. // TODO: remove one of the calls once we're moving everything into that class
  76. G::initialize();
  77. //Begin browser identification
  78. $Browser = UserAgent::browser($_SERVER['HTTP_USER_AGENT']);
  79. $OperatingSystem = UserAgent::operating_system($_SERVER['HTTP_USER_AGENT']);
  80. $Debug->set_flag('start user handling');
  81. // Get classes
  82. // TODO: Remove these globals, replace by calls into Users
  83. list($Classes, $ClassLevels) = Users::get_classes();
  84. //-- Load user information
  85. // User info is broken up into many sections
  86. // Heavy - Things that the site never has to look at if the user isn't logged in (as opposed to things like the class, donor status, etc)
  87. // Light - Things that appear in format_user
  88. // Stats - Uploaded and downloaded - can be updated by a script if you want super speed
  89. // Session data - Information about the specific session
  90. // Enabled - if the user's enabled or not
  91. // Permissions
  92. if (isset($_COOKIE['session'])) {
  93. $LoginCookie = $Enc->decrypt($_COOKIE['session']);
  94. }
  95. if (isset($LoginCookie)) {
  96. list($SessionID, $LoggedUser['ID']) = explode('|~|', $Enc->decrypt($LoginCookie));
  97. $LoggedUser['ID'] = (int)$LoggedUser['ID'];
  98. $UserID = $LoggedUser['ID']; //TODO: UserID should not be LoggedUser
  99. if (!$LoggedUser['ID'] || !$SessionID) {
  100. logout();
  101. }
  102. $UserSessions = $Cache->get_value("users_sessions_$UserID");
  103. if (!is_array($UserSessions)) {
  104. $DB->query(
  105. "SELECT
  106. SessionID,
  107. Browser,
  108. OperatingSystem,
  109. IP,
  110. LastUpdate
  111. FROM users_sessions
  112. WHERE UserID = '$UserID'
  113. AND Active = 1
  114. ORDER BY LastUpdate DESC");
  115. $UserSessions = $DB->to_array('SessionID',MYSQLI_ASSOC);
  116. $Cache->cache_value("users_sessions_$UserID", $UserSessions, 0);
  117. }
  118. if (!array_key_exists($SessionID, $UserSessions)) {
  119. logout();
  120. }
  121. // Check if user is enabled
  122. $Enabled = $Cache->get_value('enabled_'.$LoggedUser['ID']);
  123. if ($Enabled === false) {
  124. $DB->query("
  125. SELECT Enabled
  126. FROM users_main
  127. WHERE ID = '$LoggedUser[ID]'");
  128. list($Enabled) = $DB->next_record();
  129. $Cache->cache_value('enabled_'.$LoggedUser['ID'], $Enabled, 0);
  130. }
  131. if ($Enabled == 2) {
  132. logout();
  133. }
  134. // Up/Down stats
  135. $UserStats = $Cache->get_value('user_stats_'.$LoggedUser['ID']);
  136. if (!is_array($UserStats)) {
  137. $DB->query("
  138. SELECT Uploaded AS BytesUploaded, Downloaded AS BytesDownloaded, RequiredRatio
  139. FROM users_main
  140. WHERE ID = '$LoggedUser[ID]'");
  141. $UserStats = $DB->next_record(MYSQLI_ASSOC);
  142. $Cache->cache_value('user_stats_'.$LoggedUser['ID'], $UserStats, 3600);
  143. }
  144. // Get info such as username
  145. $LightInfo = Users::user_info($LoggedUser['ID']);
  146. $HeavyInfo = Users::user_heavy_info($LoggedUser['ID']);
  147. // Create LoggedUser array
  148. $LoggedUser = array_merge($HeavyInfo, $LightInfo, $UserStats);
  149. $LoggedUser['RSS_Auth'] = md5($LoggedUser['ID'] . RSS_HASH . $LoggedUser['torrent_pass']);
  150. // $LoggedUser['RatioWatch'] as a bool to disable things for users on Ratio Watch
  151. $LoggedUser['RatioWatch'] = (
  152. $LoggedUser['RatioWatchEnds'] != '0000-00-00 00:00:00'
  153. && time() < strtotime($LoggedUser['RatioWatchEnds'])
  154. && ($LoggedUser['BytesDownloaded'] * $LoggedUser['RequiredRatio']) > $LoggedUser['BytesUploaded']
  155. );
  156. // Load in the permissions
  157. $LoggedUser['Permissions'] = Permissions::get_permissions_for_user($LoggedUser['ID'], $LoggedUser['CustomPermissions']);
  158. $LoggedUser['Permissions']['MaxCollages'] += Donations::get_personal_collages($LoggedUser['ID']);
  159. // Change necessary triggers in external components
  160. $Cache->CanClear = check_perms('admin_clear_cache');
  161. // Because we <3 our staff
  162. if (check_perms('site_disable_ip_history')) {
  163. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  164. }
  165. // Update LastUpdate every 10 minutes
  166. if (strtotime($UserSessions[$SessionID]['LastUpdate']) + 600 < time()) {
  167. $DB->query("
  168. UPDATE users_main
  169. SET LastAccess = '".sqltime()."'
  170. WHERE ID = '$LoggedUser[ID]'");
  171. $SessionQuery =
  172. "UPDATE users_sessions
  173. SET ";
  174. // Only update IP if we have an encryption key in memory
  175. if (apc_exists('DBKEY')) {
  176. $SessionQuery .= "IP = '".DBCrypt::encrypt($_SERVER['REMOTE_ADDR'])."', ";
  177. }
  178. $SessionQuery .=
  179. "Browser = '$Browser',
  180. OperatingSystem = '$OperatingSystem',
  181. LastUpdate = '".sqltime()."'
  182. WHERE UserID = '$LoggedUser[ID]'
  183. AND SessionID = '".db_string($SessionID)."'";
  184. $DB->query($SessionQuery);
  185. $Cache->begin_transaction("users_sessions_$UserID");
  186. $Cache->delete_row($SessionID);
  187. $UsersSessionCache = array(
  188. 'SessionID' => $SessionID,
  189. 'Browser' => $Browser,
  190. 'OperatingSystem' => $OperatingSystem,
  191. 'IP' => ((apc_exists('DBKEY')) ? DBCrypt::encrypt($_SERVER['REMOTE_ADDR']) : $UserSessions[$SessionID]['IP']),
  192. 'LastUpdate' => sqltime() );
  193. $Cache->insert_front($SessionID, $UsersSessionCache);
  194. $Cache->commit_transaction(0);
  195. }
  196. // Notifications
  197. if (isset($LoggedUser['Permissions']['site_torrents_notify'])) {
  198. $LoggedUser['Notify'] = $Cache->get_value('notify_filters_'.$LoggedUser['ID']);
  199. if (!is_array($LoggedUser['Notify'])) {
  200. $DB->query("
  201. SELECT ID, Label
  202. FROM users_notify_filters
  203. WHERE UserID = '$LoggedUser[ID]'");
  204. $LoggedUser['Notify'] = $DB->to_array('ID');
  205. $Cache->cache_value('notify_filters_'.$LoggedUser['ID'], $LoggedUser['Notify'], 2592000);
  206. }
  207. }
  208. // We've never had to disable the wiki privs of anyone.
  209. if ($LoggedUser['DisableWiki']) {
  210. unset($LoggedUser['Permissions']['site_edit_wiki']);
  211. }
  212. // IP changed
  213. if (apc_exists('DBKEY') && DBCrypt::decrypt($LoggedUser['IP']) != $_SERVER['REMOTE_ADDR'] && !check_perms('site_disable_ip_history')) {
  214. if (Tools::site_ban_ip($_SERVER['REMOTE_ADDR'])) {
  215. error('Your IP address has been banned.');
  216. }
  217. $CurIP = db_string($LoggedUser['IP']);
  218. $NewIP = db_string($_SERVER['REMOTE_ADDR']);
  219. $DB->query("
  220. SELECT IP
  221. FROM users_history_ips
  222. WHERE EndTime IS NULL
  223. AND UserID = '$LoggedUser[ID]'");
  224. while (list($EncIP) = $DB->next_record()) {
  225. if (DBCrypt::decrypt($EncIP) == $CurIP) {
  226. $CurIP = $EncIP;
  227. // CurIP is now the encrypted IP that was already in the database (for matching)
  228. break;
  229. }
  230. }
  231. $DB->query("
  232. UPDATE users_history_ips
  233. SET EndTime = '".sqltime()."'
  234. WHERE EndTime IS NULL
  235. AND UserID = '$LoggedUser[ID]'
  236. AND IP = '$CurIP'");
  237. $DB->query("
  238. INSERT IGNORE INTO users_history_ips
  239. (UserID, IP, StartTime)
  240. VALUES
  241. ('$LoggedUser[ID]', '".DBCrypt::encrypt($NewIP)."', '".sqltime()."')");
  242. $ipcc = Tools::geoip($NewIP);
  243. $DB->query("
  244. UPDATE users_main
  245. SET IP = '".DBCrypt::encrypt($NewIP)."', ipcc = '$ipcc'
  246. WHERE ID = '$LoggedUser[ID]'");
  247. $Cache->begin_transaction('user_info_heavy_'.$LoggedUser['ID']);
  248. $Cache->update_row(false, array('IP' => DBCrypt::encrypt($_SERVER['REMOTE_ADDR'])));
  249. $Cache->commit_transaction(0);
  250. }
  251. // Get stylesheets
  252. $Stylesheets = $Cache->get_value('stylesheets');
  253. if (!is_array($Stylesheets)) {
  254. $DB->query('
  255. SELECT
  256. ID,
  257. LOWER(REPLACE(Name, " ", "_")) AS Name,
  258. Name AS ProperName
  259. FROM stylesheets');
  260. $Stylesheets = $DB->to_array('ID', MYSQLI_BOTH);
  261. $Cache->cache_value('stylesheets', $Stylesheets, 0);
  262. }
  263. //A9 TODO: Clean up this messy solution
  264. $LoggedUser['StyleName'] = $Stylesheets[$LoggedUser['StyleID']]['Name'];
  265. if (empty($LoggedUser['Username'])) {
  266. logout(); // Ghost
  267. }
  268. }
  269. G::initialize();
  270. $Debug->set_flag('end user handling');
  271. $Debug->set_flag('start function definitions');
  272. /**
  273. * Log out the current session
  274. */
  275. function logout() {
  276. global $SessionID;
  277. setcookie('session', '', time() - 60 * 60 * 24 * 365, '/', '', false);
  278. setcookie('keeplogged', '', time() - 60 * 60 * 24 * 365, '/', '', false);
  279. setcookie('session', '', time() - 60 * 60 * 24 * 365, '/', '', false);
  280. if ($SessionID) {
  281. G::$DB->query("
  282. DELETE FROM users_sessions
  283. WHERE UserID = '" . G::$LoggedUser['ID'] . "'
  284. AND SessionID = '".db_string($SessionID)."'");
  285. G::$Cache->begin_transaction('users_sessions_' . G::$LoggedUser['ID']);
  286. G::$Cache->delete_row($SessionID);
  287. G::$Cache->commit_transaction(0);
  288. }
  289. G::$Cache->delete_value('user_info_' . G::$LoggedUser['ID']);
  290. G::$Cache->delete_value('user_stats_' . G::$LoggedUser['ID']);
  291. G::$Cache->delete_value('user_info_heavy_' . G::$LoggedUser['ID']);
  292. header('Location: login.php');
  293. die();
  294. }
  295. function logout_all_sessions() {
  296. $UserID = G::$LoggedUser['ID'];
  297. G::$DB->query("
  298. DELETE FROM users_sessions
  299. WHERE UserID = '$UserID'");
  300. G::$Cache->delete_value('users_sessions_' . $UserID);
  301. logout();
  302. }
  303. function enforce_login() {
  304. global $SessionID;
  305. if (!$SessionID || !G::$LoggedUser) {
  306. setcookie('redirect', $_SERVER['REQUEST_URI'], time() + 60 * 30, '/', '', false);
  307. logout();
  308. }
  309. }
  310. /**
  311. * Make sure $_GET['auth'] is the same as the user's authorization key
  312. * Should be used for any user action that relies solely on GET.
  313. *
  314. * @param Are we using ajax?
  315. * @return authorisation status. Prints an error message to LAB_CHAN on IRC on failure.
  316. */
  317. function authorize($Ajax = false) {
  318. if (empty($_REQUEST['auth']) || $_REQUEST['auth'] != G::$LoggedUser['AuthKey']) {
  319. send_irc("PRIVMSG ".LAB_CHAN." :".G::$LoggedUser['Username']." just failed authorize on ".$_SERVER['REQUEST_URI'].(!empty($_SERVER['HTTP_REFERER']) ? " coming from ".$_SERVER['HTTP_REFERER'] : ""));
  320. error('Invalid authorization key. Go back, refresh, and try again.', $Ajax);
  321. return false;
  322. }
  323. return true;
  324. }
  325. $Debug->set_flag('ending function definitions');
  326. //Include /sections/*/index.php
  327. $Document = basename(parse_url($_SERVER['SCRIPT_FILENAME'], PHP_URL_PATH), '.php');
  328. if (!preg_match('/^[a-z0-9]+$/i', $Document)) {
  329. error(404);
  330. }
  331. $StripPostKeys = array_fill_keys(array('password', 'cur_pass', 'new_pass_1', 'new_pass_2', 'verifypassword', 'confirm_password', 'ChangePassword', 'Password'), true);
  332. $Cache->cache_value('php_' . getmypid(), array(
  333. 'start' => sqltime(),
  334. 'document' => $Document,
  335. 'query' => $_SERVER['QUERY_STRING'],
  336. 'get' => $_GET,
  337. 'post' => array_diff_key($_POST, $StripPostKeys)), 600);
  338. // Locked account constant
  339. define('STAFF_LOCKED', 1);
  340. $AllowedPages = ['staffpm', 'ajax', 'locked', 'logout', 'login'];
  341. if (isset(G::$LoggedUser['LockedAccount']) && !in_array($Document, $AllowedPages)) {
  342. require(SERVER_ROOT . '/sections/locked/index.php');
  343. } else {
  344. require(SERVER_ROOT . '/sections/' . $Document . '/index.php');
  345. }
  346. $Debug->set_flag('completed module execution');
  347. /* Required in the absence of session_start() for providing that pages will change
  348. upon hit rather than being browser cached for changing content.
  349. Old versions of Internet Explorer choke when downloading binary files over HTTPS with disabled cache.
  350. Define the following constant in files that handle file downloads */
  351. if (!defined('SKIP_NO_CACHE_HEADERS')) {
  352. header('Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0');
  353. header('Pragma: no-cache');
  354. }
  355. //Flush to user
  356. ob_end_flush();
  357. $Debug->set_flag('set headers and send to user');
  358. //Attribute profiling
  359. $Debug->profile();