Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'libs/Encryptor.php';
- require 'libs/simple_html_dom.php';
- class Register_Model extends Model {
- function __construct() {
- parent::__construct();
- }
- function signUp() {
- if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['pd_id'])) {
- $username = ltrim(rtrim($_POST['username']));
- $password = $_POST['password'];
- $pdId = trim($_POST['pd_id']);
- } else {
- header('Location: register');
- exit();
- }
- $errMessage = '';
- if (empty($username) || empty($password) || empty($pdId)) {
- $errMessage = '<li>заполнены не все поля</li>';
- } else {
- if (preg_match("/[^0-9a-z\-_]/i", $username)) {
- $errMessage .= '<li>недопустимые символы в логине</li>';
- }
- if (strlen($username) > 15) {
- $errMessage .= '<li>слишком длинный логин</li>';
- }
- if ((strlen($password) < 4) || (strlen($password) > 32)) {
- $errMessage .= '<li>недопустимая длина пароля</li>';
- }
- if ((strlen($pdId) > 6) || preg_match("/[^0-9]/", $pdId)) {
- $errMessage .= '<li>введён несуществующий ID</li>';
- }
- }
- if (empty($errMessage)) {
- $sth = $this->db->prepare("SELECT id FROM users WHERE username = :username LIMIT 1");
- $sth->execute(array(
- ':username' => $username,
- ));
- $countUsername = $sth->rowCount();
- $sth = $this->db->prepare("SELECT id FROM users WHERE pd_id = :pd_id LIMIT 1");
- $sth->execute(array(
- ':pd_id' => $pdId,
- ));
- $countPdId = $sth->rowCount();
- if (($countUsername > 0) || ($countPdId > 0)) {
- if (($countUsername > 0) && ($countPdId > 0)) {
- $this->errMessage = '<li>этот логин уже используется</li><li>этот ID уже привязан</li>';
- } else if ($countUsername > 0) {
- $this->errMessage = '<li>этот логин уже используется</li>';
- } else {
- $this->errMessage = '<li>этот ID уже привязан</li>';
- }
- } else {
- $serviceAccLogin = 'Prodota bot';
- $serviceAccPass = '***';
- $confirmationCode = mt_rand(10000, 99999);
- $messageTitle = '[PDbet] Код активации';
- $messageBody = 'Ваш код активации: [b]' . $confirmationCode . '[/b]';
- $pdName = $this->sendMessage($serviceAccLogin, $serviceAccPass, $pdId, $messageTitle, $messageBody);
- if (!empty($pdName)) {
- Session::set('reg_username', $username);
- Session::set('reg_password', $password);
- Session::set('reg_pd_name', $pdName);
- Session::set('reg_pd_id', $pdId);
- Session::set('code', $confirmationCode);
- return TRUE;
- } else {
- $this->errMessage = '<li>введён несуществующий ID</li>';
- return FALSE;
- }
- }
- } else {
- $this->errMessage = $errMessage;
- return FALSE;
- }
- }
- function sendMessage($username, $password, $messageReceiverId, $messageTitle, $messageBody) {
- $cookieID = mt_rand(1, 1000000);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'http://prodota.ru/forum/index.php?app=core&module=global§ion=login');
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies/cookie' . $cookieID);
- curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies/cookie' . $cookieID);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
- $loginPage = curl_exec($ch);
- $loginAuthKey = $this->getAuthKey($loginPage);
- $loginData = array(
- 'auth_key' => $loginAuthKey,
- 'referer' => 'http://prodota.ru/forum/index.php',
- 'ips_username' => $username,
- 'ips_password' => $password,
- 'rememberMe' => 1,
- );
- curl_setopt($ch, CURLOPT_URL, 'http://prodota.ru/forum/index.php?app=core&module=global§ion=login&do=process');
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $loginData);
- curl_exec($ch);
- curl_setopt($ch, CURLOPT_URL, 'http://prodota.ru/forum/index.php?app=members&module=messaging§ion=send&do=form&fromMemberID=' . $messageReceiverId);
- $messageForm = curl_exec($ch);
- $messagePayload = $this->getMessagePayload($messageForm);
- $messageEnteredName = $messagePayload['entered_name'];
- $messageAuthKey = $messagePayload['auth_key'];
- $messagePostKey = $messagePayload['postKey'];
- $messageData = array(
- 'entered_name' => $messageEnteredName,
- 'sendType' => 'invite',
- 'msg_title' => $messageTitle,
- 'isRte' => 0,
- 'Post' => $messageBody,
- 'topicID' => 0,
- 'postKey' => $messagePostKey,
- 'auth_key' => $messageAuthKey,
- 'dosubmit' => 'Отправить',
- );
- curl_setopt($ch, CURLOPT_URL, 'http://prodota.ru/forum/index.php?app=members&module=messaging§ion=send&do=send');
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $messageData);
- curl_exec($ch);
- curl_close($ch);
- unlink(dirname(__FILE__) . '/cookies/cookie' . $cookieID);
- return $messageEnteredName;
- }
- function getAuthKey($page) {
- $html = new simple_html_dom();
- $html->load($page);
- foreach ($html->find('input') as $input) {
- $inputs[$input->name] = $input->value;
- }
- return $inputs['auth_key'];
- }
- function getMessagePayload($page) {
- $html = new simple_html_dom();
- $html->load($page);
- foreach ($html->find('input') as $input) {
- $inputs[$input->name] = $input->value;
- }
- $messagePayload = array(
- 'entered_name' => $inputs['entered_name'],
- 'auth_key' => $inputs['auth_key'],
- 'postKey' => $inputs['postKey'],
- );
- return $messagePayload;
- }
- function confirm() {
- $code = Session::get('code');
- if (!empty($_POST['code']) && !empty($code)) {
- if (trim($_POST['code']) == Session::get('code')) {
- $password_enc = new Encryptor(Session::get('reg_password'));
- $sth = $this->db->prepare("INSERT INTO users (username, password_hash, password_salt, pd_name, pd_id) VALUES (:username, :password_hash, :password_salt, :pd_name, :pd_id)");
- $sth->execute(array(
- ':username' => Session::get('reg_username'),
- ':password_hash' => $password_enc->hash,
- ':password_salt' => $password_enc->salt,
- ':pd_name' => Session::get('reg_pd_name'),
- ':pd_id' => Session::get('reg_pd_id'),
- ));
- Session::destroy();
- return TRUE;
- } else {
- $this->errMessage = '<li>введён неверный код</li>';
- return FALSE;
- }
- } else {
- header('Location: register');
- exit();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment