PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

https://gitlab.com/nahoor/public
PHP | 337 lines | 296 code | 34 blank | 7 comment | 44 complexity | 66ebbcc97621fef2e78c4a6733788af8 MD5 | raw file
  1. <?php
  2. require 'core.php';
  3. session_start();
  4. \Slim\Slim::registerAutoloader();
  5. $app = new \Slim\Slim();
  6. $checkaddress = function ($app, $need = true) {
  7. return function () use ($app, $need) {
  8. if ($need) {
  9. if (empty($_SESSION['address'])) {
  10. $app->redirect($app->urlFor('root'));
  11. }
  12. } else {
  13. if (!empty($_SESSION['address'])) {
  14. $app->redirect($app->urlFor('faucet'));
  15. }
  16. }
  17. };
  18. };
  19. $checkclaim = function ($app) {
  20. return function () use ($app) {
  21. global $dispenseTime, $recaptchaPub, $recaptchaVersion;
  22. $address = $_SESSION['address'];
  23. $ip = getIP();
  24. $sql = "SELECT dispensed FROM dispenses WHERE email='$address' OR ip='$ip' ";
  25. $sql .= "ORDER BY id DESC LIMIT 1";
  26. $lastclaim_query = sql_query($sql);
  27. $canclaim = true;
  28. if ($lastclaim_query->num_rows) {
  29. $lastclaim = fetch_one($lastclaim_query);
  30. $lastclaim = strtotime($lastclaim);
  31. if ($lastclaim + $dispenseTime > time()) {
  32. $canclaim = false;
  33. $app->view()->setData('nextclaim', relative_time($lastclaim + $dispenseTime));
  34. }
  35. }
  36. $app->view()->setData('canclaim', $canclaim);
  37. if ($canclaim) {
  38. $app->view()->setData('recaptchaKey', $recaptchaPub);
  39. $app->view()->setData('recaptchaVersion', isset($recaptchaVersion) ? $recaptchaVersion : 1);
  40. }
  41. };
  42. };
  43. $app->hook('slim.before.dispatch', function () use ($app) {
  44. global $siteName, $squareAds, $textAds, $bannerAds, $rewards, $links;
  45. global $cashout, $googleAnalyticsId;
  46. $address = null;
  47. if (isset($_SESSION['address'])) {
  48. $address = $_SESSION['address'];
  49. }
  50. $flash = $app->view()->getData('flash');
  51. $error = '';
  52. if (isset($flash['error'])) {
  53. $error = $flash['error'];
  54. }
  55. $success = '';
  56. if (isset($flash['success'])) {
  57. $success = $flash['success'];
  58. }
  59. $app->view()->setData('success', $success);
  60. $app->view()->setData('error', $error);
  61. $app->view()->setData('address', $address);
  62. $app->view()->setData('siteName', $siteName);
  63. $app->view()->setData('squareAds', $squareAds);
  64. $app->view()->setData('textAds', $textAds);
  65. $app->view()->setData('bannerAds', $bannerAds);
  66. $app->view()->setData('rewards', isAssoc($rewards) ? array_keys($rewards) : $rewards);
  67. $app->view()->setData('links', $links);
  68. $app->view()->setData('cashout', $cashout);
  69. $app->view()->setData('isAdmin', false);
  70. $app->view()->setData('googleAnalyticsId', $googleAnalyticsId);
  71. });
  72. $app->get("/", $checkaddress($app, false), function () use ($app) {
  73. global $minReward, $maxReward, $dispenseTimeText, $apiKey, $guid;
  74. global $allowEmail, $allowCoin;
  75. $id = $app->request()->get('id');
  76. if (!is_null($id) && is_numeric($id)) {
  77. $_SESSION['referer'] = $id;
  78. }
  79. if (!empty($apiKey)) {
  80. $app->view()->setData('wallet', "<a href='https://coinbase.com'>Powered by Coinbase</a>");
  81. } elseif (!empty($guid)) {
  82. $app->view()->setData('wallet', "<a href='https://blockchain.info'>Powered by Blockchain.info</a>");
  83. }
  84. $addr = array();
  85. if ($allowCoin) {
  86. $addr[] = COIN_NAME;
  87. }
  88. if ($allowEmail) {
  89. $addr[] = "email";
  90. }
  91. $app->view()->setData('addressType', implode("/", $addr));
  92. $app->view()->setData('minReward', $minReward);
  93. $app->view()->setData('maxReward', $maxReward);
  94. $app->view()->setData('dispenseTimeText', $dispenseTimeText);
  95. $app->render('main.php', array('title' => 'Home'));
  96. })->name('root');
  97. $app->get("/about", function () use ($app) {
  98. $app->render('about.php', array('title' => 'About'));
  99. })->name('about');
  100. $checkadmin = function ($app) {
  101. return function () use ($app) {
  102. $app->view()->setData('isAdmin', isset($_SESSION['isadmin']) ? $_SESSION['isadmin'] : false);
  103. };
  104. };
  105. $app->get("/admin(/:cmd)", $checkadmin($app), function ($cmd = null) use ($app) {
  106. global $recaptchaPub, $recaptchaVersion, $fee;
  107. /*
  108. if (($cmdget = $app->request()->get('cmd')) != null) {
  109. $cmd = $cmdget;
  110. }
  111. */
  112. $flash = $app->view()->getData('flash');
  113. $isadmin = $app->view()->getData('isAdmin');
  114. switch ($cmd) {
  115. default:
  116. defaultlabel:
  117. if (!isset($_SESSION['isadmin'])) {
  118. $app->view()->setData('recaptchaKey', $recaptchaPub);
  119. $app->view()->setData('recaptchaVersion', isset($recaptchaVersion) ? $recaptchaVersion : 1);
  120. }
  121. $sql = "SELECT COUNT(*) AS num_addresses, MAX(balance) AS max_balance, SUM(balance) as sum_balance, ";
  122. $sql .= "MAX(totalbalance) as max_totalbalance, SUM(totalbalance) as sum_totalbalance ";
  123. $sql .= "FROM balances WHERE email <> 'SERVERBALANCE'";
  124. $stat_query = sql_query($sql);
  125. $statBalance = fetch_assoc($stat_query);
  126. $app->view()->setData('statBalance', $statBalance);
  127. $app->view()->setData('serverbalance', number_format(getserverbalance()));
  128. $app->render('admin.php', array('title' => 'Admin'));
  129. }
  130. })->name('admin');
  131. $app->post("/admin", $checkadmin($app), function () use ($app) {
  132. global $adminSeccode;
  133. $isadmin = $app->view()->getData('isAdmin');
  134. $cmd = $app->request()->post('cmd');
  135. switch ($cmd) {
  136. case "updatebalance":
  137. if (!$isadmin) {
  138. goto defaultlabel;
  139. }
  140. $balance = getserverbalance(true);
  141. if ($balance > 0) {
  142. $app->flash('success', "Balance is updated");
  143. } else {
  144. $app->flash('error', "Balance is not updated or balance is empty");
  145. }
  146. break;
  147. case "logout":
  148. unset($_SESSION['isadmin']);
  149. break;
  150. case "login":
  151. $seccode = $app->request()->post('seccode');
  152. if (!empty($adminSeccode) && $seccode === $adminSeccode) {
  153. if (checkRecaptcha($app->request(), getIP())) {
  154. $_SESSION['isadmin'] = true;
  155. } else {
  156. $app->flash('error', "CAPTCHA incorrect. Please try again.");
  157. }
  158. } else {
  159. $app->flash('error', "Invalid security code.");
  160. }
  161. break;
  162. default:
  163. defaultlabel:
  164. break;
  165. }
  166. $app->redirect($app->urlFor('admin'));
  167. })->name('post_admin');
  168. $app->get("/faucet", $checkaddress($app, true), $checkclaim($app), function () use ($app) {
  169. global $referPercent, $forcewait;
  170. $flash = $app->view()->getData('flash');
  171. $address = $app->view()->getData('address');
  172. $amount = null;
  173. if (isset($flash['amount'])) {
  174. $amount = $flash['amount'];
  175. }
  176. $sentamount = null;
  177. if (isset($flash['sentamount'])) {
  178. $sentamount = $flash['sentamount'];
  179. }
  180. $query_balance = sql_query("SELECT * FROM balances WHERE email='$address'");
  181. if ($query_balance->num_rows) {
  182. $balance = $query_balance->fetch_assoc();
  183. } else {
  184. $balance = array('balance' => 0, 'referralbalance' => 0, 'totalbalance' => 0, 'id' => 0);
  185. }
  186. $app->view()->setData('balance_current', $balance["balance"]);
  187. $app->view()->setData('balance_referral', $balance["referralbalance"]);
  188. $app->view()->setData('balance_alltime', $balance["totalbalance"]);
  189. $reflink = "http://" . $_SERVER['SERVER_NAME'] . $app->urlFor('root') . "?id=" . $balance["id"];
  190. $app->view()->setData('reflink', $reflink);
  191. $app->view()->setData('serverbalance', number_format(getserverbalance()));
  192. $app->view()->setData('forcewait', $forcewait);
  193. $app->view()->setData('referPercent', $referPercent);
  194. $app->view()->setData('amount', $amount);
  195. $app->view()->setData('sentamount', $sentamount);
  196. $app->render('faucet.php', array('title' => 'Faucet'));
  197. })->name('faucet');
  198. $app->post("/claim", $checkaddress($app, true), $checkclaim($app), function () use ($app) {
  199. global $mysqli, $rewards, $referPercent;
  200. $address = $app->view()->getData('address');
  201. if (checkRecaptcha($app->request(), getIP())) {
  202. $canclaim = $app->view()->getData('canclaim');
  203. if (!$canclaim) {
  204. $app->redirect($app->urlFor('faucet'));
  205. }
  206. $referral = isset($_SESSION['referer']) ? $_SESSION['referer'] : 0;
  207. if (isAssoc($rewards)) {
  208. $newRewards = array();
  209. foreach ($rewards as $reward => $value)
  210. {
  211. $newRewards = array_merge($newRewards, array_fill(0, $value, $reward));
  212. }
  213. } else {
  214. $newRewards = $rewards;
  215. }
  216. $amount = $newRewards[rand(0, count($newRewards)-1)];
  217. $sql = "INSERT INTO balances(balance, totalbalance, email, referredby) ";
  218. $sql .= "VALUES($amount, $amount, '$address', $referral) ON DUPLICATE KEY ";
  219. $sql .= "UPDATE balance = balance + $amount, totalbalance = totalbalance + $amount;";
  220. sql_query($sql);
  221. if ($mysqli->affected_rows == 2) {
  222. // existing user, check referral
  223. $referral_query = sql_query("SELECT referredby FROM balances WHERE email='$address'");
  224. $referral = fetch_one($referral_query);
  225. }
  226. $ua = $mysqli->real_escape_string($_SERVER['HTTP_USER_AGENT']);
  227. $ip = getIP();
  228. $date = date("Y-m-d H:i:s");
  229. $sql = "INSERT INTO dispenses(amount, dispensed, email, ip, useragent) ";
  230. $sql .= "VALUES('$amount', '$date', '$address', '$ip', '$ua')";
  231. sql_query($sql);
  232. if ($referral != 0) {
  233. $referredamount = $amount * ($referPercent / 100);
  234. $sql = "UPDATE balances SET balance = balance + $referredamount, referralbalance = referralbalance + $referredamount, totalbalance = totalbalance + $referredamount ";
  235. $sql .= "WHERE id='$referral'";
  236. sql_query($sql);
  237. }
  238. $app->view()->setData('canClaim', true);
  239. $app->view()->setData('nextClaim', relative_time(time()+1));
  240. $app->flash('amount', $amount);
  241. } else {
  242. $app->flash('error', "CAPTCHA incorrect. Please try again.");
  243. }
  244. $app->redirect($app->urlFor('faucet'));
  245. })->name('claim');
  246. $app->post("/cashout", $checkaddress($app, true), function () use ($app) {
  247. global $cashout;
  248. $address = $app->view()->getData('address');
  249. $balance_query = sql_query("SELECT balance FROM balances WHERE email='$address'");
  250. if ($balance_query->num_rows) {
  251. $balance = fetch_one($balance_query);
  252. if ($balance >= $cashout) {
  253. sql_query("UPDATE balances SET balance = balance - $balance WHERE email='$address'");
  254. // race attacks check
  255. $balance_query = sql_query("SELECT balance FROM balances WHERE email='$address'");
  256. $balancecheck = fetch_one($balance_query);
  257. if ($balancecheck >= 0) {
  258. try {
  259. sendMoney($address, $balance);
  260. $app->flash('sentamount', true);
  261. } catch (NoCashException $e) {
  262. $app->flash('error', "The site does not have enough coins to pay out! No balance deducted.");
  263. sql_query("UPDATE balances SET balance = balance + $balance WHERE email='$address'");
  264. } catch (Exception $e) {
  265. $response = $e->getMessage();
  266. $app->flash('error', "An error has occured - $response");
  267. sql_query("UPDATE balances SET balance = balance + $balance WHERE email='$address'");
  268. }
  269. }
  270. } else {
  271. $app->flash('error', "Amount is too small");
  272. }
  273. } else {
  274. $app->flash('error', "You don't have enough coins to cash out");
  275. }
  276. $app->redirect($app->urlFor('faucet'));
  277. })->name('cashout');
  278. $app->post("/faucet", function () use ($app) {
  279. global $mysqli, $allowEmail, $allowCoin;
  280. $address = $app->request()->post('address');
  281. if (!checkaddress($address)) {
  282. $err = array();
  283. if ($allowCoin) {
  284. $err[] = COIN_NAME;
  285. }
  286. if ($allowEmail) {
  287. $err[] = "email";
  288. }
  289. $app->flash('error', "Not a valid ".implode("/", $err)." address!");
  290. $app->redirect($app->urlFor('root'));
  291. }
  292. $_SESSION['address'] = $mysqli->real_escape_string($address);
  293. $app->redirect($app->urlFor('faucet'));
  294. })->name("post_faucet");
  295. $app->get('/(:segments+)', function ($segments) use ($app) {
  296. $app->redirect($app->urlFor('root'));
  297. })->name('catchall');
  298. $app->run();