Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- /*
- USERS_LOGIN :
- id INT(11) NOT NULL AUTO_INCREMENT,
- caps INT(5)
- email VARCHAR(150) NOT NULL,
- pwd VARCHAR(20) NOT NULL,
- */
- /*
- USERS_INFORMATIONS :
- hash VARCHAR(20) NOT NULL,
- civility INT(1),
- first_name VARCHAR(150), //PRENOM
- last_name VARCHAR(200),
- date_of_bird TIMESTAMP,
- company VARCHAR(255),
- mailing_address VARCHAR(255),
- zip_code VARCHAR(5),
- town VARCHAR(150),
- country VARCHAR(255),
- phone VARCHAR(20),
- mobile VARCHAR(20),
- fax VARCHAR(20),
- newsletter BOOL,
- */
- //require_once($GLOBALS['e-commerce']['server-root'].'/inc/db.inc.php');
- //require_once($GLOBALS['e-commerce']['server-root'].'/inc/class.errors.php');
- // Création d'une session afin de stocker, si le loggin est réussi, les renseignements
- header('Cache-control: private');
- define('SECRET_KEY', "XUyJAJ");
- // Droits des utilisateurs
- define('CAP_USER_AUTH', 0x00000001);
- define('CAP_USER_ADMIN', 0x00000002);
- define('CAP_ITEM_ADD', 0x00000010);
- define('CAP_ITEM_EDIT', 0x00000020);
- define('CAP_ITEM_DELETE', 0x00000040);
- // Classe des utilisateurs
- class Users_Informations
- {
- var $civility;
- var $first_name;
- var $last_name;
- var $date_of_birth;
- var $company;
- var $mailing_address;
- var $zip_code;
- var $town;
- var $country;
- var $phone;
- var $mobile;
- var $fax;
- var $newsletter;
- function __construct($data)
- {
- $this->civility = $data['civility'];
- $this->first_name = $data['first_name'];
- $this->last_name = $data['last_name'];
- $this->date_of_birth = $data['date_of_birth'];
- $this->company = $data['company'];
- $this->mailing_address = $data['mailing_address'];
- $this->zip_code = $data['zip_code'];
- $this->town = $data['town'];
- $this->country = $data['country'];
- $this->phone = $data['phone'];
- $this->mobile = $data['mobile'];
- $this->fax = $data['fax'];
- if (isset($data['newsletter']))
- $this->newsletter = 1; //bool
- else
- $this->newsletter = 0; //bool
- }
- function &load_informations($hash)
- {
- $query = "SELECT * FROM " . USERS_INFORMATIONS . " WHERE hash = $hash";
- $res = mysql_query($query, db());
- if ($res == false)
- return null;
- $data = mysql_fetch_array($res);
- $ret = &new Users_Informations($data);
- return $ret;
- }
- }
- class User
- {
- var $id;
- var $caps;
- var $email;
- var $_pwd;
- var $personnal_informations;
- function __construct($data)
- {
- $this->id = $data['id'];
- $this->_pwd = $data['pwd'];
- $this->caps = $data['caps'];
- $this->email = $data['email'];
- $this->personnal_informations = null;
- }
- function logout()
- {
- foreach($this as $key => $value)
- unset($this->$key);
- $_SESSION = Array();
- @session_destroy();
- }
- function authenticate($pwd)
- {
- if (!($this->caps & CAP_USER_AUTH))
- return false;
- return $this->_pwd == sha1($pwd);
- }
- function login()
- {
- $_SESSION['umail'] = $this->email;
- }
- function load_personnal_informations()
- {
- $hash = sha1( sha1($this->id) . SECRET_KEY . sha1($this->email) );
- $this->personnal_informations = Users_Informations::load_informations($hash);
- }
- function unload_personnal_informations()
- {
- $this->personnal_informations = null;
- }
- function &load($id)
- {
- $id = db_format_value($id);
- $query = "SELECT * FROM " . USERS_LOGIN . " WHERE id = $id";
- $res = mysql_query($query, db());
- if ($res === false)
- return null;
- $data = mysql_fetch_array($res);
- return new User($data);
- }
- function &search($email = null)
- {
- $query = 'SELECT * FROM ' . USERS_LOGIN;
- if ($email !== null)
- {
- $email = db_format_value($email);
- $query .= " WHERE email = $email";
- }
- $res = mysql_query($query, db());
- if ($res === false)
- return null;
- $users = Array();
- while ($data = mysql_fetch_array($res))
- $users[] = &new User($data);
- return $users;
- }
- function ¤t()
- {
- static $user = null;
- if ($user !== null)
- return $user;
- if (isset($_SESSION['umail']))
- {
- $users = &User::search($_SESSION['umail']);
- if (count($users) > 0)
- $user = $users[0];
- }
- return $user;
- }
- }
- class Users_SQL extends User
- {
- var $_errors;
- function verify()
- {
- $this->_errors = new Errors();
- $exist = &parent::search($this->email);
- if ($exist)
- $this->_errors->add("email", $this->email, "L'adresse mail &value est déjà utilisée.");
- else
- {
- if (empty($this->email))
- $this->_errors->add("email", null, "L'adresse mail doit être renseignée!");
- if (empty($this->_pwd))
- $this->_errors->add("email", null, "Le mot de passe doit être renseigné!");
- $validMail = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";
- $validPassword = "/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/";
- if (!preg_match($validMail, $this->email))
- $this->_errors->add("email", $this->email, "L'adresse mail &value n'est pas valide");
- else
- {
- if (!preg_match($validPassword, $this->_pwd))
- $this->_errors->add("pwd", $this->_pwd, "Le mot de passe n'est pas valide");
- else
- {
- return null;
- }
- }
- }
- return $this->_errors; // NULL IF EMPTY
- }
- function insert()
- {
- $info = array(
- "caps" => db_format_value(CAP_USER_AUTH),
- "email" => db_format_value($this->email),
- "pwd" => db_format_value(sha1($this->_pwd))
- );
- $query = db_insert_sql($info, USERS_LOGIN);
- $res = mysql_query($query, db());
- if (mysql_error())
- {
- $this->_errors->add("mysql", mysql_error(), "Erreur mySQL >> &value");
- return $this->_errors;
- }
- else
- return null;
- }
- }
- class Users_Informations_SQL extends Users_Informations
- {
- var $_errors;
- function verify()
- {
- $this->_errors = new Errors();
- if (empty($this->first_name))
- $this->_errors->add("first_name", null, "Le prénom doit être renseigné!");
- if (empty($this->last_name))
- $this->_errors->add("last_name", null, "Le nom doit être renseigné!");
- if (empty($this->date_of_birth))
- $this->_errors->add("date_of_birth", null, "La date de naissance doit être renseignée!");
- if (empty($this->mailing_address))
- $this->_errors->add("mailing_address", null, "L'adresse doit être renseignée!");
- if (empty($this->zip_code))
- $this->_errors->add("zip_code", null, "Le code postal doit être renseigné!");
- if (empty($this->town))
- $this->_errors->add("town", null, "La ville doit être renseignée!");
- if (empty($this->country))
- $this->_errors->add("country", null, "Le pays doit être renseigné!");
- if ($this->_errors->count() == 0)
- {
- return null;
- }
- else
- return $this->_errors;
- }
- function insert($email)
- {
- $currentUser = User::search($email);
- $currentUser = $currentUser ? $currentUser[0] : exit;
- $hash = sha1( sha1($currentUser->id) . SECRET_KEY . sha1($currentUser->email) );
- date_default_timezone_set('Europe/Brussels');
- $info = array(
- "hash" => db_format_value($hash),
- "civility" => db_format_value($this->civility),
- "first_name" => db_format_value($this->first_name),
- "last_name" => db_format_value($this->last_name),
- "date_of_birth" => db_format_value(date("Y-m-d H:i:s", strtotime(preg_replace('/\//', '.', $this->date_of_birth)))),
- "mailing_address" => db_format_value($this->mailing_address),
- "zip_code" => db_format_value($this->zip_code),
- "town" => db_format_value($this->town),
- "country" => db_format_value($this->country),
- "phone" => db_format_value($this->phone),
- "mobile" => db_format_value($this->mobile),
- "fax" => db_format_value($this->fax),
- "newsletter" => db_format_value(true)
- );
- $query = db_insert_sql($info, USERS_INFORMATIONS);
- $res = mysql_query($query, db());
- if (mysql_error())
- {
- $this->_errors->add("mysql", mysql_error(), "Erreur mySQL >> &value");
- return $this->_errors;
- }
- else
- return null;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement