From 69c0dd651b176ff74ee45610b2a731b2449d4203 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 19 Apr 2015 13:00:32 +0000 Subject: [PATCH] Registration and activation works --- _developer_data/structure.sql | 61 +++++-- _sakura/components/Main.php | 61 ++++++- _sakura/components/Users.php | 106 ++++++++++- _sakura/templates/yuuno/main/authenticate.tpl | 164 +++++++++++------- content/data/yuuno/css/yuuno.css | 10 +- main/.htaccess | 2 +- main/authenticate.php | 118 ++++++++++--- main/index.php | 2 +- 8 files changed, 405 insertions(+), 119 deletions(-) diff --git a/_developer_data/structure.sql b/_developer_data/structure.sql index 5991643..e960d29 100644 --- a/_developer_data/structure.sql +++ b/_developer_data/structure.sql @@ -5,9 +5,20 @@ SET time_zone = '+00:00'; SET foreign_key_checks = 0; SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; -DROP DATABASE IF EXISTS `flashiidev`; -CREATE DATABASE `flashiidev` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */; -USE `flashiidev`; +DROP DATABASE IF EXISTS `sakura`; +CREATE DATABASE `sakura` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */; +USE `sakura`; + +DROP TABLE IF EXISTS `fii_actioncodes`; +CREATE TABLE `fii_actioncodes` ( + `id` bigint(255) NOT NULL AUTO_INCREMENT COMMENT 'Automatically generated ID by MySQL for management.', + `action` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Action identifier so the backend knows what to do.', + `userid` bigint(255) NOT NULL COMMENT 'ID of the user that would be affected by this action', + `actkey` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'The URL key for using this code.', + `instruction` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Things the backend should do upon using this code', + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; + DROP TABLE IF EXISTS `fii_apikeys`; CREATE TABLE `fii_apikeys` ( @@ -39,6 +50,34 @@ CREATE TABLE `fii_config` ( `config_value` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'The value, obviously.' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +TRUNCATE `fii_config`; +INSERT INTO `fii_config` (`config_name`, `config_value`) VALUES +('recaptcha_public', ''), +('recaptcha_private', ''), +('charset', 'utf-8'), +('cookie_prefix', ''), +('cookie_domain', ''), +('cookie_path', '/'), +('site_style', 'yuuno'), +('manage_style', 'Manage'), +('allow_registration', '0'), +('smtp_server', ''), +('smtp_auth', ''), +('smtp_secure', ''), +('smtp_port', ''), +('smtp_username', ''), +('smtp_password', ''), +('smtp_replyto_mail', ''), +('smtp_replyto_name', ''), +('smtp_from_email', ''), +('smtp_from_name', 'Sakura Noreply'), +('sitename', 'Sakura'), +('recaptcha', '1'), +('require_activation', '1'), +('require_registration_code', '0'), +('disable_registration', '1'), +('max_reg_keys', '5'), +('mail_signature', 'Team Flashii'); DROP TABLE IF EXISTS `fii_infopages`; CREATE TABLE `fii_infopages` ( @@ -83,6 +122,7 @@ CREATE TABLE `fii_profilefields` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +TRUNCATE `fii_profilefields`; INSERT INTO `fii_profilefields` (`id`, `name`, `formtype`, `description`, `additional`) VALUES (1, 'Website', 'url', 'URL to your website', ''), (2, 'Twitter', 'text', 'Your @twitter Username', ''), @@ -94,8 +134,7 @@ INSERT INTO `fii_profilefields` (`id`, `name`, `formtype`, `description`, `addit (8, 'osu!', 'text', 'Your osu! Username', ''), (9, 'Origin', 'text', 'Your Origin User ID', ''), (10, 'Xbox Live', 'text', 'Your Xbox User ID', ''), -(11, 'PSN', 'text', 'Your PSN User ID', '') -ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `name` = VALUES(`name`), `formtype` = VALUES(`formtype`), `description` = VALUES(`description`), `additional` = VALUES(`additional`); +(11, 'PSN', 'text', 'Your PSN User ID', ''); DROP TABLE IF EXISTS `fii_ranks`; CREATE TABLE `fii_ranks` ( @@ -108,6 +147,7 @@ CREATE TABLE `fii_ranks` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +TRUNCATE `fii_ranks`; INSERT INTO `fii_ranks` (`id`, `name`, `multi`, `colour`, `description`, `title`) VALUES (1, 'Deactivated', 0, '#555', 'Users that are yet to be activated or that deactivated their own account.', 'Deactivated'), (2, 'Regular user', 1, 'inherit', 'Regular users with regular permissions.', 'Regular user'), @@ -117,8 +157,7 @@ INSERT INTO `fii_ranks` (`id`, `name`, `multi`, `colour`, `description`, `title` (6, 'Bot', 1, '#9E8DA7', 'Reserved user accounts for services.', 'Bot'), (7, 'Chat moderator', 1, '#09F', 'Moderators of the chat room.', 'Staff'), (8, 'Tenshi', 0, '#EE9400', 'Users that donated $5.00 or more in order to keep the site and it\'s services alive!', 'Tenshi'), -(9, 'Alumnii', 0, '#FF69B4', 'People who have contributed to the community but have moved on or resigned.', 'Alumnii') -ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `name` = VALUES(`name`), `multi` = VALUES(`multi`), `colour` = VALUES(`colour`), `description` = VALUES(`description`), `title` = VALUES(`title`); +(9, 'Alumnii', 0, '#FF69B4', 'People who have contributed to the community but have moved on or resigned.', 'Alumnii'); DROP TABLE IF EXISTS `fii_regcodes`; CREATE TABLE `fii_regcodes` ( @@ -128,7 +167,7 @@ CREATE TABLE `fii_regcodes` ( `used_by` bigint(128) unsigned NOT NULL COMMENT 'ID of user who used this code.', `key_used` tinyint(1) unsigned NOT NULL COMMENT 'Boolean for setting this key as used.', PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `fii_sessions`; @@ -142,7 +181,7 @@ CREATE TABLE `fii_sessions` ( `expire` int(64) unsigned NOT NULL COMMENT 'The timestamp for when this session should end, -1 for permanent. ', `remember` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT 'If set to 1 session will be extended each time a page is loaded.', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `fii_tenshi`; @@ -184,7 +223,7 @@ CREATE TABLE `fii_users` ( `profile_data` text COLLATE utf8_bin NOT NULL COMMENT 'Modular array containing profile data.', PRIMARY KEY (`id`), UNIQUE KEY `username_clean` (`username_clean`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `fii_warnings`; @@ -199,4 +238,4 @@ CREATE TABLE `fii_warnings` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; --- 2015-04-13 10:06:13 +-- 2015-04-19 12:58:09 diff --git a/_sakura/components/Main.php b/_sakura/components/Main.php index c6e43e7..8390a41 100644 --- a/_sakura/components/Main.php +++ b/_sakura/components/Main.php @@ -243,8 +243,8 @@ class Main { // Validate MX records public static function checkMXRecord($email) { - // Split up the address in two parts (user and domain) - list($user, $domain) = split('@', $email); + // Get the domain from the e-mail address + $domain = substr(strstr($email, '@'), 1); // Check the MX record $record = checkdnsrr($domain, 'MX'); @@ -407,4 +407,61 @@ class Main { } + // Create a new action code + public static function newActionCode($action, $userid, $instruct) { + + // Make sure the user we're working with exists + if(Users::getUser($userid)['id'] == 0) + return false; + + // Convert the instruction array to a JSON + $instruct = json_encode($instruct); + + // Generate a key + $key = sha1(date("r") . time() . $userid . $action . rand(0, 9999)); + + // Insert the key into the database + Database::insert('actioncodes', [ + 'action' => $action, + 'userid' => $userid, + 'actkey' => $key, + 'instruction' => $instruct + ]); + + // Return the key + return $key; + + } + + // Use an action code + public static function useActionCode($action, $key, $uid = 0) { + + // Retrieve the row from the database + $keyRow = Database::fetch('actioncodes', false, [ + 'actkey' => [$key, '='], + 'action' => [$action, '='] + ]); + + // Check if the code exists + if(count($keyRow) <= 1) + return [0, 'INVALID_CODE']; + + // Check if the code was intended for the user that's using this code + if($keyRow['userid'] != 0) { + + if($keyRow['userid'] != $uid) + return [0, 'INVALID_USER']; + + } + + // Remove the key from the database + Database::delete('actioncodes', [ + 'id' => [$keyRow['id'], '='] + ]); + + // Return success + return [1, 'SUCCESS', $keyRow['instruction']]; + + } + } diff --git a/_sakura/components/Users.php b/_sakura/components/Users.php index c9b1a66..e32e095 100644 --- a/_sakura/components/Users.php +++ b/_sakura/components/Users.php @@ -191,7 +191,7 @@ class Users { return [0, 'PASS_TOO_LONG']; // Passwords do not match - if($password != $confirmpassword) + if($password != $confirmpass) return [0, 'PASS_NOT_MATCH']; // Check if the given email address is formatted properly @@ -254,7 +254,7 @@ class Users { } // Send the activation e-mail and do other required stuff - public static function sendActivationMail($uid) { + public static function sendActivationMail($uid, $customKey = null) { // Get the user data $user = Database::fetch('users', false, ['id' => [$uid, '=']]); @@ -264,14 +264,18 @@ class Users { return false; // Generate activation key - // $activate = ; - $activate = 'null'; + $activate = ($customKey ? $customKey : Main::newActionCode('ACTIVATE', $uid, [ + 'user' => [ + 'rank_main' => 1, + 'ranks' => json_encode([1]) + ] + ])); // Build the e-mail $message = "Welcome to ". Configuration::getConfig('sitename') ."!\r\n\r\n"; $message .= "Please keep this e-mail for your records. Your account intormation is as follows:\r\n\r\n"; $message .= "----------------------------\r\n\r\n"; - $message .= "Username: ". $user['username'] ."\r\n"; + $message .= "Username: ". $user['username'] ."\r\n\r\n"; $message .= "Your profile: http://". Configuration::getLocalConfig('urls', 'main') ."/u/". $user['id'] ."\r\n\r\n"; $message .= "----------------------------\r\n\r\n"; $message .= "Please visit the following link in order to activate your account:\r\n\r\n"; @@ -289,6 +293,87 @@ class Users { } + // Activating a user + public static function activateUser($uid, $requireKey = false, $key = null) { + + // Get the user data + $user = Database::fetch('users', false, ['id' => [$uid, '=']]); + + // Check if user exists + if(!count($user) > 1) + return [0, 'USER_NOT_EXIST']; + + // Check if user is already activated + if($user['rank_main']) + return [0, 'USER_ALREADY_ACTIVE']; + + // Set default values for activation + $rank = 1; + $ranks = json_encode([1]); + + // Check if a key is set (there's an option to not set one for user management reasons but you can't really get around this anyway) + if($requireKey) { + + // Check the action code + $action = Main::useActionCode('ACTIVATE', $key, $uid); + + // Check if we got a negative return + if(!$action[0]) + return [0, $action[1]]; + + // Assign the special values + $instructionData = json_decode($action[2], true); + $rank = $instructionData['user']['rank_main']; + $ranks = $instructionData['user']['ranks']; + + } + + // Activate the account + Database::update('users', [ + [ + 'rank_main' => $rank, + 'ranks' => $ranks + ], + [ + 'id' => [$uid, '='] + ] + ]); + + // Return success + return [1, 'SUCCESS']; + + } + + // Deactivating a user + public static function deactivateUser($uid) { + + // Get the user data + $user = Database::fetch('users', false, ['id' => [$uid, '=']]); + + // Check if user exists + if(!count($user) > 1) + return [0, 'USER_NOT_EXIST']; + + // Check if user is already deactivated + if(!$user['rank_main']) + return [0, 'USER_ALREADY_DEACTIVE']; + + // Deactivate the account + Database::update('users', [ + [ + 'rank_main' => 0, + 'ranks' => json_encode([0]) + ], + [ + 'id' => [$uid, '='] + ] + ]); + + // Return success + return [1, 'SUCCESS']; + + } + // Check if registration code is valid public static function checkRegistrationCode($code) { @@ -412,15 +497,22 @@ class Users { } // Get all users - public static function getAllUsers() { + public static function getAllUsers($includeInactive = true) { // Execute query $getUsers = Database::fetch('users', true); // Reorder shit - foreach($getUsers as $user) + foreach($getUsers as $user) { + + // Skip if inactive and not include deactivated users + if(!$includeInactive && $user['rank_main'] == 0) + continue; + $users[$user['id']] = $user; + } + // and return an array with the users return $users; diff --git a/_sakura/templates/yuuno/main/authenticate.tpl b/_sakura/templates/yuuno/main/authenticate.tpl index 57b7b8a..adbf2b6 100644 --- a/_sakura/templates/yuuno/main/authenticate.tpl +++ b/_sakura/templates/yuuno/main/authenticate.tpl @@ -60,78 +60,108 @@ -
-
- Register on Flashii -
-
- - - -
- +
+
+
+ Register on Flashii
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-