app.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. session_name('default');
  3. session_set_cookie_params(0, '/', '.tankernn.eu');
  4. session_start();
  5. require_once('config.php');
  6. class App {
  7. function __construct($silent = false) {
  8. global $conn;
  9. //Get username
  10. $this->user = 'guest';
  11. if(isset($_SESSION['user']))
  12. $this->user = htmlspecialchars($_SESSION['user']);
  13. //Get page name
  14. $this->pagename = "Start";
  15. if(isset($_GET['p']))
  16. $this->pagename = htmlspecialchars($conn->escapeString($_GET['p']));
  17. //Get page
  18. $query=$conn->query("SELECT * FROM Page WHERE name='$this->pagename'");
  19. $this->pageRow=$query->fetchArray() or print($silent ? "" : "<i>Couldn't find page: ".$this->pagename."</i>"); /*header("Location: /?p=error")*/
  20. //Import scripts
  21. $this->scripts = "";
  22. foreach (glob("scripts/*.js") as $filename)
  23. $this->scripts .= "<script src='$filename'></script>";
  24. //Import stylesheets
  25. $this->styles = "";
  26. foreach (glob("stylesheets/*.css") as $filename)
  27. $this->styles .= "<link rel='Stylesheet' type='text/css' href='$filename'/>";
  28. //Setup variables
  29. $this_year = getdate()['year'];
  30. $loginbuttons = <<<EOT
  31. <a class="btn btn-primary" href="/login">Log in</a>
  32. or
  33. <a class="btn btn-default" href="/login?signup">Sign up</a>
  34. EOT;
  35. $this->vars = array(
  36. '{$year}' => $this_year,
  37. '{$user}' => $this->user,
  38. '{$sitename}' => Config::$sitename,
  39. '{$menuitems}' => $this->getMenuItems(),
  40. '{$login}' => isset($_SESSION['user']) ? "<a href='/admin' class='btn btn-primary' target='_blank'>Control panel <i class='fa fa-external-link'></i></a>" : $loginbuttons
  41. );
  42. //Get components
  43. $this->components = array();
  44. $query=$conn->query("SELECT * FROM Component");
  45. while ($compRow = $query->fetchArray()) {
  46. $this->components[$compRow['name']] = strtr($compRow['content'], $this->vars);
  47. }
  48. }
  49. function addSection($sectionName) {
  50. global $conn;
  51. $query=$conn->query("SELECT * FROM Section WHERE name='$sectionName' OR UID='$sectionName'");
  52. $sectionrow=$query->fetchArray();
  53. echo "<section class=".strtolower($sectionrow["name"])." id=". $sectionrow["UID"] ."><div class='container'>";
  54. echo strtr($sectionrow['content'], $this->vars);
  55. echo '</div></section>';
  56. }
  57. function printPage() {
  58. $sections = json_decode($this->pageRow['sections']);
  59. if ($sections === NULL)
  60. $sections = explode(',', $this->pageRow['sections']);
  61. foreach ($sections as $section) {
  62. $this->addSection($section);
  63. }
  64. }
  65. function getMenuItems() {
  66. global $conn;
  67. $menuquery = $conn->query("SELECT * FROM Menu ORDER BY listId");
  68. $menuString = "";
  69. while($menurow = $menuquery->fetchArray()) {
  70. $selected = "";
  71. if ($this->pagename == $menurow['value'])
  72. $selected = "active";
  73. if ($menurow['valuetype'] == "page") {
  74. $link = "?p=" . $menurow['value'];
  75. } else {
  76. $link = $menurow['value'];
  77. }
  78. $menuString .= "<li class='$selected'><a href='$link'>".$menurow['name']."</a></li>";
  79. }
  80. return $menuString;
  81. }
  82. }
  83. class Message {
  84. public $text;
  85. public $type;
  86. function __construct($message, $messtype) {
  87. $this->text = $message;
  88. $this->type = $messtype;
  89. }
  90. }
  91. function display_message($message) {
  92. ?>
  93. <div class="alert alert-<?php echo $message->type ?> alert-dismissable fade in">
  94. <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
  95. <?php echo $message->text ?>
  96. </div>
  97. <?php
  98. }
  99. function list_messages() {
  100. if (isset($_SESSION['message'])) {
  101. $messages = json_decode($_SESSION['message']);
  102. foreach ($messages as $message)
  103. display_message($message);
  104. unset($_SESSION['message']);
  105. }
  106. }
  107. function queue_message($message) {
  108. $messages = array();
  109. if (isset($_SESSION['message'])) {
  110. $messages = json_decode($_SESSION['message']);
  111. }
  112. array_push($messages, $message);
  113. $_SESSION['message'] = json_encode($messages);
  114. }
  115. function generateKey($length) {
  116. return bin2hex(openssl_random_pseudo_bytes($length));
  117. }
  118. function new_activation($user, $type, $value) {
  119. global $conn;
  120. if ($type === "Email") {
  121. $mail = $value;
  122. } else {
  123. $mail = $conn->query("SELECT Email FROM Users WHERE User='$user'")->fetchArray()['Email'];
  124. }
  125. $key = generateKey(16);
  126. if ($conn->query("INSERT INTO Activations (User,Type,Val,Activation_key) VALUES ('$user','$type','$value','$key')")) {
  127. mail($mail, Config::$sitename . " $type confirmation", "To activate your new $type, navigate to this address: http://tankernn.eu/admin/actions/activate.php?key=" . $key);
  128. return new Message("Activation E-mail sent to " . $mail, "success");
  129. } else {
  130. return new Message("Failed to update database. Error: " . $conn->lastErrorMsg(), "danger");
  131. }
  132. }
  133. function changePassword($userid, $newPass, $newPassRepeat, $oldPass = false) {
  134. global $conn;
  135. if ($newPass === $newPassRepeat) {
  136. if (strlen($newPass) < 8) {
  137. queue_message(new Message("Your password must be at least 8 characters long.", "danger"));
  138. return false;
  139. }
  140. $query = $conn->query("SELECT * FROM Users WHERE UID='$userid'");
  141. $row = $query->fetchArray();
  142. if ($oldPass === false || password_verify($oldPass, $row['Password'])) {
  143. $pass_hashed = password_hash($newPass, PASSWORD_DEFAULT);
  144. if($conn->query("UPDATE Users SET Password='$pass_hashed' WHERE UID='$userid'")) {
  145. queue_message(new Message("Successfully updated password.", "success"));
  146. return true;
  147. } else {
  148. queue_message(new Message("SQL error: " . $conn->lastErrorMsg(), "danger"));
  149. return false;
  150. }
  151. } else {
  152. queue_message(new Message("Password incorrect.", "danger"));
  153. return false;
  154. }
  155. } else {
  156. queue_message(new Message("Passwords do not match.", "danger"));
  157. return false;
  158. }
  159. }
  160. function hasPermission($permission = 2) {
  161. if (!isset($_SESSION['user']) or !isset($_SESSION['permissions']))
  162. return false;
  163. $myPermissions = json_decode($_SESSION['permissions']);
  164. if ($myPermissions->permission_level >= 2)
  165. return true;
  166. switch (gettype($permission)) {
  167. case "string":
  168. return in_array($permission, $myPermissions->custom_permissions);
  169. break;
  170. case "integer":
  171. return $myPermissions->permission_level >= $permission;
  172. break;
  173. default:
  174. throw new InvalidArgumentException("Permission type must be integer or string.", 1);
  175. }
  176. }
  177. function admin_check() {
  178. if (!hasPermission(2)) {
  179. die("Not enough permissions.");
  180. }
  181. }
  182. ?>