Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. <?php
  2.  
  3. function getToken($length) {
  4. $token = "";
  5. $symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  6. $max = strlen($symbols);
  7.  
  8. for ($i = 0; $i < $length; $i++) {
  9. $token .= $symbols[random_int(0, $max - 1)];
  10. }
  11.  
  12. return $token;
  13. }
  14.  
  15. session_start();
  16. include __DIR__.'/pdo-wrapper.php';
  17. include __DIR__.'/../vendor/autoload.php';
  18. use Mailgun\Mailgun;
  19.  
  20. $db = new Database();
  21.  
  22. $error = array();
  23.  
  24. if (empty($_POST['username'])) {
  25. $error[] = "Your Username field is empty.";
  26. } else {
  27. $count = $db->getCount('todo_users', 'username = :username', array(':username' => $_POST['username']));
  28.  
  29. if ($count[0][0] >= 1) {
  30. $error[] = "This user already exists.";
  31. } else {
  32. $username = strip_tags($_POST['username']);
  33. }
  34. }
  35.  
  36. if (empty($_POST['email'])) {
  37. $error[] = "Your E-Mail field is empty.";
  38. } else {
  39. if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  40. $error[] = "Your E-Mail is not valid.";
  41. } else {
  42. $count = $db->getCount('todo_users', 'email = :email', array(':email' => $_POST['email']));
  43.  
  44. if ($count[0][0] >= 1) {
  45. $error[] = "This E-Mail is already linked with another account.";
  46. } else {
  47. $email = strip_tags($_POST['email']);
  48. }
  49. }
  50. }
  51.  
  52. if (empty($_POST['password']) && empty($_POST['repassword'])) {
  53. $error[] = "Your Password fields is empty.";
  54. } else {
  55. if ($_POST['password'] != $_POST['repassword']) {
  56. $error[] = "Your Password and Repeat Password field does not match.";
  57. } else {
  58. if ($_POST['password'] == $_POST['username']) {
  59. $error[] = "You can't use Username as your Password.";
  60. } else {
  61. $password = hash('sha256', strip_tags($_POST['password']));
  62. }
  63. }
  64. }
  65.  
  66. if (empty($_POST['securityq'])) {
  67. $error[] = "Your Security Question field is empty.";
  68. } else {
  69. $question = hash('sha256', strip_tags($_POST['securityq']));
  70. }
  71.  
  72. if (empty($_POST['token'])) {
  73. $error[] = "Your Invitation Key field is empty.";
  74. } else {
  75. $key = strip_tags($_POST['token']);
  76. }
  77.  
  78. if (!empty($error)) {
  79. $_SESSION['errors'] = $error;
  80. header("Location: ../auth/register");
  81. } else {
  82. date_default_timezone_set("Europe/Helsinki");
  83. $joined = time();
  84.  
  85. $activateToken = getToken(25);
  86.  
  87. $stmt = $db->db->prepare("INSERT INTO todo_users (email, username, password, question, active, role, joined, sortby) VALUES (:email, :username, :password, :question, :token, 'Regular', :joined, '0')");
  88. $stmt->bindValue(':email', $email);
  89. $stmt->bindValue(':username', $username);
  90. $stmt->bindValue(':password', $password);
  91. $stmt->bindValue(':question', $question);
  92. $stmt->bindValue(':token', $activateToken);
  93. $stmt->bindValue(':joined', $joined);
  94. $stmt->execute();
  95.  
  96.  
  97. var_dump($email);
  98. $apiKey = 'api-key';
  99. $domain = 'mg.my-domain.com';
  100. $mg = Mailgun::create($apiKey);
  101. $res = $mg->messages()->send($domain, array(
  102. 'from' => 'todo@my-domain.com',
  103. 'to' => ''.$email,
  104. 'subject' => 'subject',
  105. 'html' => 'Click <a href="https://my-domain.com?token='. $activateToken .'">here</a> to activate your account.'
  106. ));
  107.  
  108. $_SESSION['success'] = "You have been successfully registered. Now you have to go to your E-Mail and activate account.";
  109. header("Location: ../auth/register");
  110. }
  111.  
  112. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement