Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require "public/assets/include/conf.inc.php";
- // Login to database
- function connectDb()
- {
- try
- {
- $db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8", DB_USER, DB_PWD);
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e)
- {
- die("Erreur de connection: " . $e->getMessage() );
- }
- return $db;
- }
- // Register a new user on the website
- function registerUser()
- {
- $db = connectDb();
- $error = false;
- if ($_SERVER["REQUEST_METHOD"] == "POST")
- {
- // Verify first name input
- if (empty($_POST["first-name"]))
- {
- $_SESSION["errors"]["first_name"] = "A first name is required";
- $error = true;
- }
- else
- {
- $first_name = verifyInput($_POST["first-name"]);
- if (strlen($_POST["first-name"]) < 1 or strlen($_POST["first-name"]) > 100)
- {
- $_SESSION["errors"]["first_name"] = "Your first name is too long";
- $error = true;
- }
- }
- // Verify last name input
- if (empty($_POST["last-name"]))
- {
- $_SESSION["errors"]["last_name"] = "A last name is required";
- $error = true;
- }
- else
- {
- $last_name = verifyInput($_POST["last-name"]);
- if (strlen($_POST["last-name"]) < 1 or strlen($_POST["last-name"]) > 100)
- {
- $_SESSION["errors"]["last_name"] = "Your last name is too long";
- $error = true;
- }
- }
- // Verify email input
- if (empty($_POST["email"]))
- {
- $_SESSION["errors"]["email"] = "An email is required";
- $error = true;
- }
- else
- {
- $email = verifyInput($_POST["email"]);
- if (!filter_var($email, FILTER_VALIDATE_EMAIL))
- {
- $_SESSION["errors"]["email"] = "Invalid email format";
- $error = true;
- }
- }
- // Verify confirm email input
- if (empty($_POST["confirm-email"]))
- {
- $_SESSION["errors"]["confirm_email"] = "You must confirm your email";
- $error = true;
- }
- else
- {
- $confirm_email = verifyInput($_POST["confirm-email"]);
- if($_POST["confirm-email"] != $_POST["email"])
- {
- $_SESSION["errors"]["confirm_email"] = "Emails do not match";
- $error = true;
- }
- }
- // Verify password input
- if (empty($_POST["password"]))
- {
- $_SESSION["errors"]["password"] = "A password is required";
- $error = true;
- }
- else
- {
- $password = verifyInput($_POST["password"]);
- if(!ctype_alnum($password))
- {
- $_SESSION["errors"]["password"] = "Only letters and numbers are allowed";
- $error = true;
- }
- if (strlen($_POST["password"]) < 8 or strlen($_POST["password"]) > 20)
- {
- $_SESSION["errors"]["password"] = "Your password must be between 8 and 20 characters long";
- $error = true;
- }
- }
- // Verify confirm password input
- if (empty($_POST["confirm-password"]))
- {
- $_SESSION["errors"]["confirm_password"] = "You must confirm your password";
- $error = true;
- }
- else
- {
- $confirm_password = verifyInput($_POST["confirm-password"]);
- if($_POST["confirm-password"] != $_POST["password"])
- {
- $_SESSION["errors"]["confirm_password"] = "Passwords do not match";
- $error = true;
- }
- }
- // Verify username input
- if (empty($_POST["username"]))
- {
- $_SESSION["errors"]["username"] = "An username is required";
- $error = true;
- }
- else
- {
- $username = verifyInput($_POST["username"]);
- if(!ctype_alnum($username))
- {
- $_SESSION["errors"]["username"] = "Only letters and numbers are allowed";
- $error = true;
- }
- if (strlen($_POST["username"]) < 1 or strlen($_POST["username"]) > 50)
- {
- $_SESSION["errors"]["username"] = "Your username must be between 1 and 50 characters long";
- $error = true;
- }
- }
- // Verify prefered communication language select
- if (empty($_POST["prefered-communication-language"]))
- {
- $_SESSION["errors"]["prefered_communication_language"] = "A prefered communication language is required";
- $error = true;
- }
- elseif (languageArray($_POST["prefered-communication-language"]) == false)
- {
- $_SESSION["errors"]["prefered_communication_language"] = "This prefered communication language is invalid";
- $error = true;
- }
- // Verify country select
- if (empty($_POST["country"]))
- {
- $_SESSION["errors"]["country"] = "A country is required";
- $error = true;
- }
- elseif (countryArray($_POST["country"]) == false)
- {
- $_SESSION["errors"]["country"] = "This country is invalid";
- $error = true;
- }
- // Verify birth date select
- if (empty($_POST["birth-date"]))
- {
- $_SESSION["errors"]["birth_date"] = "A birth date is required";
- $error = true;
- }
- // Verify gender select
- if (empty($_POST["gender"]))
- {
- $_SESSION["errors"]["gender"] = "A gender is required";
- $error = true;
- }
- elseif (genderArray($_POST["gender"]) == false)
- {
- $_SESSION["errors"]["gender"] = "This gender is invalid";
- $error = true;
- }
- // Verify ToS check
- if (!isset($_POST["tos"]))
- {
- $_SESSION["errors"]["tos"] = "You must validate the conditions";
- $error = true;
- }
- }
- // Verify that the email isn't already used
- $query = $db->prepare("SELECT COUNT(email) FROM user WHERE email=:email");
- $query->bindParam(':email', $email);
- $query->execute();
- $count = $query->fetch();
- if ($count[0] > 0)
- {
- $_SESSION["errors"]["email"] = "Email is already in use";
- $error = true;
- }
- // Register the new user account in the database
- if(!$error)
- {
- $query = $db->prepare("INSERT INTO user (email, password, username, prefered_communication_language, first_name, last_name, country, birth_date, gender) VALUES (:email, :password, :username, :prefered_communication_language, :first_name, :last_name, :country, :birth_date, :gender)");
- $query->bindParam(':email', $email);
- $query->bindParam(':password', $password);
- $query->bindParam(':username', $username);
- $query->bindParam(':prefered_communication_language', $_POST["prefered-communication-language"]);
- $query->bindParam(':first_name', $first_name);
- $query->bindParam(':last_name', $last_name);
- $query->bindParam(':country', $_POST["country"]);
- $query->bindParam(':birth_date', $_POST["birth-date"]);
- $query->bindParam(':gender', $_POST["gender"]);
- $password = password_hash($_POST["password"], PASSWORD_DEFAULT);
- $query->execute();
- $_SESSION["accountCreated"] = 1;
- header("Location: /login.php");
- exit;
- }
- }
- // Verification of user input: removing leading and trailing spaces, removing backslashes, converting special characters to HTML entities
- function verifyInput($data)
- {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- // Check if prefered communication language value is correct
- function languageArray($language)
- {
- $language_array = ["Afrikanns", "Albanian", "Arabic", "Armenian", "Basque", "Bengali", "Bulgarian", "Catalan", "Cambodian", "Chinese (Mandarin)", "Croation", "Czech", "Danish", "Dutch", "English", "Estonian", "Fiji", "Finnish",
- "French", "Georgian", "German", "Greek", "Gujarati", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Irish", "Italian", "Japanese", "Javanese", "Korean", "Latin", "Latvian", "Lithuanian", "Macedonian",
- "Malay", "Malayalam", "Maltese", "Maori", "Marathi", "Mongolian", "Nepali", "Norwegian", "Persian", "Polish", "Portuguese", "Punjabi", "Quechua", "Romanian", "Russian", "Samoan", "Serbian", "Slovak", "Slovenian", "Spanish",
- "Swahili", "Swedish", "Tamil", "Tatar", "Telugu", "Thai", "Tibetan", "Tonga", "Turkish", "Ukranian", "Urdu", "Uzbek", "Vietnamese", "Welsh", "Xhosa"];
- if (in_array($language, $language_array))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- // Check if country value is correct
- function countryArray($country)
- {
- $country_array = ["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh",
- "Barbados", "Barbuda", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Trty.", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Caicos Islands", "Cambodia",
- "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus",
- "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "French Southern Territories",
- "Futuna Islands", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard", "Herzegovina",
- "Holy See", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Jan Mayen Islands", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya",
- "Kiribati", "Korea", "Korea (Democratic)", "Kuwait", "Kyrgyzstan", "Lao", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives",
- "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "McDonald Islands", "Mexico", "Micronesia", "Miquelon", "Moldova", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
- "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "Nevis", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Palestinian Territory, Occupied", "Panama", "Papua New Guinea",
- "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Principe", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Barthelemy", "Saint Helena", "Saint Kitts", "Saint Lucia", "Saint Martin (French part)", "Saint Pierre", "Saint Vincent",
- "Samoa", "San Marino", "Sao Tome", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia", "South Sandwich Islands", "Spain", "Sri Lanka", "Sudan", "Suriname",
- "Svalbard", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Grenadines", "Timor-Leste", "Tobago", "Togo", "Tokelau", "Tonga", "Trinidad", "Tunisia", "Turkey", "Turkmenistan", "Turks Islands",
- "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "US Minor Outlying Islands", "Uzbekistan", "Vanuatu", "Vatican City State", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (US)", "Wallis", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"];
- if (in_array($country, $country_array))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- // Check if gender value is correct
- function genderArray($gender)
- {
- $gender_array = ["Male", "Female", "Not specified"];
- if (in_array($gender, $gender_array))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment