diff --git a/conf/db.conf b/conf/db.conf index 58b318d..4bb2e94 100644 --- a/conf/db.conf +++ b/conf/db.conf @@ -76,6 +76,14 @@ define("SMS_DB_TABLE", "sms"); */ define("INAPP_DB_TABLE", "inapp_purchases"); +/* + * The name of the user informations table. + * It is used to store informations about user like firstname, lastname, gender, etc... + * + * Default value: user_info + */ +define("USER_INFO_DB_TABLE", "user_info"); + /* * The delay in minutes before test account expiration. * It is used to delete old test accounts from database; diff --git a/src/objects/user-info.php b/src/objects/user-info.php new file mode 100644 index 0000000..f3a014f --- /dev/null +++ b/src/objects/user-info.php @@ -0,0 +1,226 @@ +. +*/ + +class UserInfo { + private $conn; + + public $id; + public $account_id; + public $firstname; + public $lastname; + public $gender; + public $subscribe; + + public function __construct($db) { + $this->conn = $db; + } + + public function __toString() { + $to_string = "UserInfo: "; + if (!empty($this->id)) { + $to_string = $to_string . "id=" . $this->id . ", "; + } + if (!empty($this->account_id)) { + $to_string = $to_string . "account_id=" . $this->account_id . ", "; + } + if (!empty($this->firstname)) { + $to_string = $to_string . "firstname=" . $this->firstname . ", "; + } + if (!empty($this->lastname)) { + $to_string = $to_string . "lastname=" . $this->lastname . ", "; + } + if (!empty($this->gender)) { + $to_string = $to_string . "gender=" . $this->gender . ", "; + } + if (!empty($this->subscribe)) { + $to_string = $to_string . "subscribe=" . $this->subscribe . ", "; + } + return substr($to_string, 0, -2); + } + + function dropTable() { + $query = "DROP TABLE IF EXISTS " . USER_INFO_DB_TABLE; + + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + $stmt = $this->conn->prepare($query); + + Logger::getInstance()->debug("Dropping table " . USER_INFO_DB_TABLE); + if ($stmt->execute()) { + return true; + } + Logger::getInstance()->error($stmt->errorInfo()); + return false; + } + + function createTable() { + $query = "CREATE TABLE IF NOT EXISTS " . USER_INFO_DB_TABLE . " ( + id INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT, + account_id INTEGER(11) UNSIGNED NOT NULL, + firstname VARCHAR(128) NOT NULL, + lastname VARCHAR(128) NOT NULL, + gender enum('male','female') NOT NULL, + subscribe enum('0','1') NOT NULL DEFAULT '0', + PRIMARY KEY (id))"; + + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + $stmt = $this->conn->prepare($query); + + Logger::getInstance()->debug("Creating table " . USER_INFO_DB_TABLE); + if ($stmt->execute()) { + return true; + } + Logger::getInstance()->error($stmt->errorInfo()); + return false; + } + + function delete() { + $query = "DELETE FROM " . USER_INFO_DB_TABLE . " WHERE id = ?"; + + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + $stmt = $this->conn->prepare($query); + $this->id = htmlspecialchars(strip_tags($this->id)); + $stmt->bindParam(1, $this->id); + + Logger::getInstance()->debug("Deleting " . (string)$this); + if ($stmt->execute()) { + return true; + } + Logger::getInstance()->error($stmt->errorInfo()); + return false; + } + + function create() { + $query = "INSERT INTO " . USER_INFO_DB_TABLE . " SET account_id=:account_id, firstname=:firstname, lastname=:lastname, gender=:gender, subscribe=:subscribe"; + + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + $stmt = $this->conn->prepare($query); + + $this->account_id = htmlspecialchars(strip_tags($this->account_id)); + $this->firstname = htmlspecialchars(strip_tags($this->firstname)); + $this->lastname = htmlspecialchars(strip_tags($this->lastname)); + $this->gender = htmlspecialchars(strip_tags($this->gender)); + $this->subscribe = htmlspecialchars(strip_tags($this->subscribe)); + + $stmt->bindParam(":account_id", $this->account_id); + $stmt->bindParam(":firstname", $this->firstname); + $stmt->bindParam(":lastname", $this->lastname); + $stmt->bindParam(":gender", $this->gender); + $stmt->bindParam(":subscribe", $this->subscribe); + + Logger::getInstance()->debug("Creating " . (string)$this); + if ($stmt->execute()) { + $this->id = $this->conn->lastInsertId(); + return true; + } + Logger::getInstance()->error($stmt->errorInfo()); + return false; + } + + function update() { + $query = "UPDATE " . USER_INFO_DB_TABLE . " SET firstname=:firstname, lastname=:lastname, subscribe=:subscribe"; + + $query = $query . " WHERE id=:id"; + + $stmt = $this->conn->prepare($query); + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + + $this->id = htmlspecialchars(strip_tags($this->id)); + $this->firstname = htmlspecialchars(strip_tags($this->firstname)); + $this->lastname = htmlspecialchars(strip_tags($this->lastname)); + $this->subscribe = htmlspecialchars(strip_tags($this->subscribe)); + + $stmt->bindParam(":firstname", $this->firstname); + $stmt->bindParam(":lastname", $this->lastname); + $stmt->bindParam(":subscribe", $this->subscribe); + $stmt->bindParam(":id", $this->id); + + Logger::getInstance()->debug("Updating " . (string)$this); + if ($stmt->execute()) { + return true; + } + Logger::getInstance()->error($stmt->errorInfo()); + return false; + } + + function getAll() { + $query = "SELECT id, account_id, firstname, lastname, gender, subscribe FROM " . USER_INFO_DB_TABLE; + $stmt = $this->conn->prepare($query); + Logger::getInstance()->debug("GetAll " . (string)$this); + $stmt->execute(); + return $stmt; + } + + function getOne() { + $query = "SELECT id, account_id, firstname, lastname, gender, subscribe FROM " . USER_INFO_DB_TABLE; + + if (!empty($this->id)) { + $query = $query . " WHERE id = ?"; + $this->id = htmlspecialchars(strip_tags($this->id)); + } else if (!empty($this->account_id)) { + $query = $query . " WHERE account_id = ?"; + $this->account_id = htmlspecialchars(strip_tags($this->account_id)); + } else if (!empty($this->lastname)) { + $query = $query . " WHERE lastname = ?"; + $this->lastname = htmlspecialchars(strip_tags($this->lastname)); + if (!empty($this->firstname)) { + $query = $query . " AND firstname = ?"; + $this->firstname = htmlspecialchars(strip_tags($this->firstname)); + } + } else { + return false; + } + + $query = $query . " LIMIT 0,1"; + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + $stmt = $this->conn->prepare($query); + + if (!empty($this->id)) { + $stmt->bindParam(1, $this->id); + } else if (!empty($this->account_id)) { + $stmt->bindParam(1, $this->account_id); + } else if (!empty($this->lastname)) { + $stmt->bindParam(1, $this->lastname); + if (!empty($this->firstname)) { + $stmt->bindParam(2, $this->firstname); + } + } + + Logger::getInstance()->debug("GetOne " . (string)$this); + if ($stmt->execute()) { + $row = $stmt->fetch(PDO::FETCH_ASSOC); + if ($row == null) { + Logger::getInstance()->message("Couldn't find account matching " . (string)$this); + return false; + } + + $this->id = $row['id']; + $this->account_id = $row['account_id']; + $this->firstname = $row['firstname']; + $this->lastname = $row['lastname']; + $this->gender = $row['gender']; + $this->subscribe = $row['subscribe']; + return true; + } + Logger::getInstance()->error($stmt->errorInfo()); + return false; + } +} + +?> \ No newline at end of file diff --git a/src/tools/create_tables.php b/src/tools/create_tables.php index 7da8089..bd7a41c 100644 --- a/src/tools/create_tables.php +++ b/src/tools/create_tables.php @@ -26,6 +26,7 @@ include_once __DIR__ . '/../objects/alias.php'; include_once __DIR__ . '/../objects/device.php'; include_once __DIR__ . '/../objects/password.php'; include_once __DIR__ . '/../objects/sms.php'; +include_once __DIR__ . '/../objects/user-info.php'; $database = new Database(); $db = $database->getConnection(); @@ -54,5 +55,10 @@ $sms = new SMS($db); if (!$sms->createTable()) { Logger::getInstance()->error("Couldn't create sms table"); } + +$user_info = new UserInfo($db); +if (!$user_info->createTable()) { + Logger::getInstance()->error("Couldn't create user_info table"); +} ?> \ No newline at end of file diff --git a/src/tools/drop_tables.php b/src/tools/drop_tables.php index 47f339a..cd3c649 100644 --- a/src/tools/drop_tables.php +++ b/src/tools/drop_tables.php @@ -26,6 +26,7 @@ include_once __DIR__ . '/../objects/alias.php'; include_once __DIR__ . '/../objects/device.php'; include_once __DIR__ . '/../objects/password.php'; include_once __DIR__ . '/../objects/sms.php'; +include_once __DIR__ . '/../objects/user-info.php'; $database = new Database(); $db = $database->getConnection(); @@ -54,5 +55,10 @@ $sms = new SMS($db); if (!$sms->dropTable()) { Logger::getInstance()->error("Couldn't drop sms table"); } + +$user_info = new UserInfo($db); +if (!$user_info->dropTable()) { + Logger::getInstance()->error("Couldn't drop user_info table"); +} ?> \ No newline at end of file diff --git a/src/tools/migrate_accounts.php b/src/tools/migrate_accounts.php index 36c8542..b4f442f 100644 --- a/src/tools/migrate_accounts.php +++ b/src/tools/migrate_accounts.php @@ -26,6 +26,7 @@ include_once __DIR__ . '/../objects/alias.php'; include_once __DIR__ . '/../objects/device.php'; include_once __DIR__ . '/../objects/password.php'; include_once __DIR__ . '/../objects/sms.php'; +include_once __DIR__ . '/../objects/user-info.php'; include_once __DIR__ . '/../misc/utilities.php'; $database = new Database(); @@ -48,7 +49,7 @@ $start_time = time(); Logger::getInstance()->message("Starting accounts migration"); -$query = "SELECT ac.id, ac.login, ac.password, ac.activated, ac.email, ac.confirmation_key, ac.ip_address, ac.date_last_update, ac.user_agent, al.alias FROM " +$query = "SELECT ac.id, ac.login, ac.password, ac.activated, ac.email, ac.confirmation_key, ac.ip_address, ac.date_last_update, ac.user_agent, ac.firstname, ac.name, ac.gender, ac.subscribe, al.alias FROM " . ACCOUNTS_DB_TABLE . " ac LEFT JOIN " . ALIAS_DB_TABLE . " al ON ac.id = al.account_id"; $old_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $old_db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); // For large sets this is mandatory @@ -60,8 +61,6 @@ $account_created_count = 0; $password_created_count = 0; $alias_created_count = 0; -$alias_query = "SELECT alias FROM " . ALIAS_DB_TABLE . " WHERE account_id = ? LIMIT 0,1"; - while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $accounts_to_migrate_count += 1; extract($row); @@ -94,6 +93,16 @@ while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { } else { if ($account->create()) { $account_created_count += 1; + + $user_info = new UserInfo($db); + $user_info->account_id = $account->id; + $user_info->firstname = $firstname; + $user_info->lastname = $name; + $user_info->gender = $gender; + $user_info->subscribe = $subscribe; + if (!$user_info->create()) { + Logger::getInstance()->error("Failed to create user-info !"); + } $pwd = new Password($db); $pwd->account_id = $account->id;