PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

https://gitlab.com/ankitpati/coderush
PHP | 322 lines | 303 code | 19 blank | 0 comment | 56 complexity | 6d0319a0ebc6694622d7495ee230ce48 MD5 | raw file
  1. <?php
  2. header("Expires: Wed, 13 Dec 1995 05:43:00 GMT");
  3. header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
  4. header("Cache-Control: private, no-store, max-age=0, no-cache, must-revalidate, post-check=0, pre-check=0");
  5. ?>
  6. <!DOCTYPE html>
  7. <!-- index.php -->
  8. <!-- Date : 13 November 2016
  9. - Author: Ankit Pati
  10. -->
  11. <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  12. <head>
  13. <meta charset="UTF-8" />
  14. <meta name="theme-color" content="#300a24" />
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  16. <title>CodeRush</title>
  17. <link rel="stylesheet" type="text/css" href="css/style.css" />
  18. </head>
  19. <body spellcheck="false">
  20. <h1>CodeRush</h1>
  21. <?php
  22. function login($reason)
  23. {
  24. ?>
  25. <div id="superset">
  26. <?php
  27. if(isset($reason)) {
  28. ?>
  29. <div id="note"><?= htmlspecialchars($reason) ?></div>
  30. <?php
  31. }
  32. ?>
  33. <h2>Login</h2>
  34. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  35. <input placeholder="Username" name="username" required="required" />
  36. <br />
  37. <br />
  38. <input type="password" placeholder="Password" name="password" required="required" />
  39. <br />
  40. <br />
  41. <div class="bottom">
  42. <input class="button bottom" type="submit" value="Done" />
  43. </div>
  44. </form>
  45. </div>
  46. <?php
  47. }
  48. ?>
  49. <?php
  50. function console($welcome)
  51. {
  52. ?>
  53. <div id="superset">
  54. <h2><div class="truncate" title="<?= htmlspecialchars($welcome) ?>"><?= htmlspecialchars($welcome) ?></div>
  55. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  56. <input class="button" id="logout" type="submit" name="logout" value="Logout" />
  57. </form>
  58. </h2>
  59. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  60. <input class="button" id="usermanage" type="submit" name="usermanage" value="Create/Update User" />
  61. </form>
  62. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  63. <input class="button" id="list" type="submit" name="list" value="List Submissions" />
  64. </form>
  65. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  66. <input class="button" id="dlsubmit" type="submit" name="dlsubmit" value="Download Submissions" />
  67. </form>
  68. </div>
  69. <?php
  70. if($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["list"])) {
  71. $ext_permit=array("c", "h", "cc", "cpp", "cxx", "c++", "hh", "hpp", "hxx", "h++", "java");
  72. ?>
  73. <table>
  74. <tr>
  75. <th>Username</th>
  76. <th>Level</th>
  77. <th>Question</th>
  78. <th>Language</th>
  79. <th>Time</th>
  80. </tr>
  81. <?php
  82. if($handle = opendir("./uploads/")) {
  83. while(($entry = readdir($handle)) !== false) {
  84. ?>
  85. <tr>
  86. <?php
  87. $mtime = filemtime("./uploads/".$entry);
  88. $mtime = date("H:i:s", $mtime)." IST";
  89. $ext = pathinfo($entry, PATHINFO_EXTENSION);
  90. if($entry != "." && $entry != ".." && in_array($ext, $ext_permit)) {
  91. $cells = explode("_", $entry, 3);
  92. $lang = explode(".", $cells[2], 2);
  93. ?>
  94. <td title="<?= htmlspecialchars($cells[0]) ?>"><a href="./uploads/<?= $entry ?>" target="_blank"><?= htmlspecialchars($cells[0]) ?></a></td>
  95. <td><a href="./uploads/<?= $entry ?>" target="_blank"><?= htmlspecialchars($cells[1]) ?></a></td>
  96. <td><a href="./uploads/<?= $entry ?>" target="_blank"><?= htmlspecialchars($lang [0]) ?></a></td>
  97. <td><a href="./uploads/<?= $entry ?>" target="_blank"><?= htmlspecialchars($lang [1]) ?></a></td>
  98. <td title="<?= htmlspecialchars($mtime) ?>"><a href="./uploads/<?= $entry ?>" target="_blank"><?= htmlspecialchars($mtime) ?></a></td>
  99. <?php
  100. }
  101. ?>
  102. </tr>
  103. <?php
  104. }
  105. closedir($handle);
  106. }
  107. ?>
  108. </table>
  109. <?php
  110. }else if($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["newusername"]) && isset($_POST["newpassword"])) {
  111. $con = mysqli_connect("localhost", "coderush", "coderush", "coderush");
  112. $username = preg_replace("/[^A-Za-z0-9_.@\-]/", "_", $_POST["newusername"]);
  113. $username = substr($username, 0, 255);
  114. $password = substr($_POST["newpassword"], 0, 100);
  115. $admin = 0;
  116. if(isset($_POST["newadmin"])) {
  117. $admin = 1;
  118. }
  119. mysqli_query($con, "insert into users (username, password, admin) values('".mysqli_real_escape_string($con, $username)."', '".mysqli_real_escape_string($con, $password)."', '".mysqli_real_escape_string($con, $admin)."') on duplicate key update password='".mysqli_real_escape_string($con, $password)."', admin='".mysqli_real_escape_string($con, $admin)."';");
  120. mysqli_close($con);
  121. ?>
  122. <div id="superset">
  123. <div id="note">
  124. Changes Applied
  125. </div>
  126. </div>
  127. <?php
  128. }
  129. else if($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["usermanage"])) {
  130. ?>
  131. <div id="superset">
  132. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  133. <p>Create/Update User</p>
  134. <input placeholder="New Username" name="newusername" required="required" />
  135. <br />
  136. <br />
  137. <input type="password" placeholder="New Password" name="newpassword" required="required" />
  138. <br />
  139. <br />
  140. <input type="checkbox" id="newadmin" name="newadmin" value="1" />
  141. <label for="newadmin"><span><span></span></span>Administrator</label>
  142. <br />
  143. <br />
  144. <div class="bottom">
  145. <input class="button bottom" type="submit" value="Done" />
  146. </div>
  147. </form>
  148. </div>
  149. <?php
  150. }
  151. else if($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["dlsubmit"])) {
  152. $ext_permit = array("c", "h", "cc", "cpp", "cxx", "c++", "hh", "hpp", "hxx", "h++", "java");
  153. $zip = new ZipArchive();
  154. $zip->open("uploads/code.zip", ZipArchive::CREATE);
  155. if($handle = opendir("uploads/")) {
  156. while(($entry = readdir($handle)) !== false) {
  157. $ext = pathinfo($entry, PATHINFO_EXTENSION);
  158. if($entry != "." && $entry != ".." && in_array($ext, $ext_permit)) {
  159. $zip->addFile("uploads/".$entry);
  160. }
  161. }
  162. closedir($handle);
  163. }
  164. $zip->close();
  165. header("Content-Type: application/zip");
  166. header("Content-Disposition: attachment; filename='uploads/code.zip'");
  167. header("Content-Length: ".filesize("uploads/code.zip"));
  168. header("Location: uploads/code.zip");
  169. }
  170. }
  171. ?>
  172. <?php
  173. function workspace($welcome)
  174. {
  175. ?>
  176. <div id="superset">
  177. <h2><div class="truncate" title="<?= htmlspecialchars($welcome) ?>"><?= htmlspecialchars($welcome) ?></div>
  178. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  179. <input class="button" id="logout" type="submit" name="logout" value="Logout" />
  180. </form>
  181. </h2>
  182. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  183. <input class="button" id="debug" type="submit" name="debug" value="Debug Files" />
  184. <br />
  185. <br />
  186. </form>
  187. <?php
  188. if($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["level"]) && isset($_POST["ques"]) && isset($_FILES["ans"])) {
  189. $ext_permit = array("c", "h", "cc", "cpp", "cxx", "c++", "hh", "hpp", "hxx", "h++", "java");
  190. $ext = pathinfo($_FILES["ans"]["name"], PATHINFO_EXTENSION);
  191. $ext = strtolower($ext);
  192. if(in_array($ext, $ext_permit)) {
  193. $con = mysqli_connect("localhost", "coderush", "coderush", "coderush");
  194. $level = preg_replace("/[^0-9]/", "_", $_POST["level"]);
  195. $ques = preg_replace("/[^0-9]/", "_", $_POST["ques"]);
  196. $ans = preg_replace("/[^A-Za-z0-9_.@\-]/", "_", $welcome."_".$level."_".$ques);
  197. $ans = substr($ans, 0, 250);
  198. $ext = substr($ext, 0, 4);
  199. $ans = $ans.".".$ext;
  200. $fin = fopen($_FILES["ans"]["tmp_name"], "rb");
  201. $fout = fopen("./uploads/".$ans, "wb");
  202. $data = fread($fin, $_FILES["ans"]["size"]);
  203. fwrite($fout, $data);
  204. fclose($fout);
  205. mysqli_query($con, "insert into submit (username, level, ques, ans, curtime) values('".mysqli_real_escape_string($con, $welcome)."', '".mysqli_real_escape_string($con, $_POST["level"])."', '".mysqli_real_escape_string($con, $_POST["ques"])."', '".mysqli_real_escape_string($con, $ans)."', now()) on duplicate key update ans='".mysqli_real_escape_string($con, $ans)."';");
  206. mysqli_close($con);
  207. ?>
  208. <div id="note">
  209. Code Uploaded
  210. </div>
  211. <?php
  212. }
  213. else {
  214. ?>
  215. <div id="note">
  216. Unsupported Language
  217. </div>
  218. <?php
  219. }
  220. }
  221. else if($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["debug"])) {
  222. $ext_permit = array("c", "h", "cc", "cpp", "cxx", "c++", "hh", "hpp", "hxx", "h++", "java");
  223. $zip = new ZipArchive();
  224. $zip->open("uploads/debug.zip", ZipArchive::CREATE);
  225. if($handle = opendir("debug/")) {
  226. while(($entry = readdir($handle)) !== false) {
  227. $ext = pathinfo($entry, PATHINFO_EXTENSION);
  228. if($entry != "." && $entry != ".." && in_array($ext, $ext_permit)) {
  229. $zip->addFile("debug/".$entry);
  230. }
  231. }
  232. closedir($handle);
  233. }
  234. $zip->close();
  235. header("Content-Type: application/zip");
  236. header("Content-Disposition: attachment; filename='uploads/debug.zip'");
  237. header("Content-Length: ".filesize("uploads/debug.zip"));
  238. header("Location: uploads/debug.zip");
  239. }
  240. ?>
  241. <form method="post" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" enctype="multipart/form-data" autocomplete="on">
  242. <input placeholder="Level" name="level" maxlength="1" pattern="^[1-3]{1}$" title="1, 2, or 3" required="required" />
  243. <br />
  244. <br />
  245. <input placeholder="Question" name="ques" maxlength="2" pattern="^[0-9]{1,2}$" title="Maximum of 2 digits" required="required" />
  246. <br />
  247. <br />
  248. <input type="file" name="ans" required="required" />
  249. <div class="bottom">
  250. <input class="button bottom" type="submit" value="Done" />
  251. </div>
  252. </form>
  253. </div>
  254. <?php
  255. }
  256. ?>
  257. <?php
  258. if(isset($_POST["logout"])) {
  259. setcookie("username", "", 1);
  260. setcookie("password", "", 1);
  261. login("Logged Out");
  262. }
  263. else if(isset($_POST["username"]) && isset($_POST["password"])) {
  264. $con = mysqli_connect("localhost", "coderush", "coderush", "coderush");
  265. $result = mysqli_query($con, "select username, password from users where username='".mysqli_real_escape_string($con, $_POST["username"])."';");
  266. $tuples = mysqli_fetch_array($result);
  267. mysqli_free_result($result);
  268. mysqli_close($con);
  269. if($_POST["username"] === $tuples[0] && $_POST["password"] === $tuples[1]) {
  270. setcookie("username", $_POST["username"]);
  271. setcookie("password", $_POST["password"]);
  272. header("Location: index.php");
  273. die();
  274. }
  275. else {
  276. login("Incorrect Credentials");
  277. }
  278. }
  279. else if(isset($_COOKIE["username"]) && isset($_COOKIE["password"])) {
  280. $con = mysqli_connect("localhost", "coderush", "coderush", "coderush");
  281. $result = mysqli_query($con, "select username, password, admin from users where username='".mysqli_real_escape_string($con, $_COOKIE["username"])."';");
  282. $tuples = mysqli_fetch_array($result);
  283. mysqli_free_result($result);
  284. mysqli_close($con);
  285. if($_COOKIE["username"] === $tuples[0] && $_COOKIE["password"] === $tuples[1]) {
  286. if($tuples[2] === "1") {
  287. console($_COOKIE["username"]);
  288. }
  289. else {
  290. workspace($_COOKIE["username"]);
  291. }
  292. }
  293. else {
  294. login();
  295. }
  296. }
  297. else {
  298. login();
  299. }
  300. ?>
  301. </body>
  302. </html>
  303. <!-- end of index.php -->