Guest User

Untitled

a guest
Jul 3rd, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.95 KB | None | 0 0
  1. <?php
  2.  
  3. require 'libs/Encryptor.php';
  4. require 'libs/simple_html_dom.php';
  5.  
  6. class Register_Model extends Model {
  7.  
  8.     function __construct() {
  9.         parent::__construct();
  10.     }
  11.  
  12.     function signUp() {
  13.         if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['pd_id'])) {
  14.             $username = ltrim(rtrim($_POST['username']));
  15.             $password = $_POST['password'];
  16.             $pdId = trim($_POST['pd_id']);
  17.         } else {
  18.             header('Location: register');
  19.             exit();
  20.         }
  21.  
  22.         $errMessage = '';
  23.  
  24.         if (empty($username) || empty($password) || empty($pdId)) {
  25.             $errMessage = '<li>заполнены не все поля</li>';
  26.         } else {
  27.             if (preg_match("/[^0-9a-z\-_]/i", $username)) {
  28.                 $errMessage .= '<li>недопустимые символы в логине</li>';
  29.             }
  30.             if (strlen($username) > 15) {
  31.                 $errMessage .= '<li>слишком длинный логин</li>';
  32.             }
  33.             if ((strlen($password) < 4) || (strlen($password) > 32)) {
  34.                 $errMessage .= '<li>недопустимая длина пароля</li>';
  35.             }
  36.             if ((strlen($pdId) > 6) || preg_match("/[^0-9]/", $pdId)) {
  37.                 $errMessage .= '<li>введён несуществующий ID</li>';
  38.             }
  39.         }
  40.  
  41.         if (empty($errMessage)) {
  42.             $sth = $this->db->prepare("SELECT id FROM users WHERE username = :username LIMIT 1");
  43.             $sth->execute(array(
  44.                 ':username' => $username,
  45.             ));
  46.             $countUsername = $sth->rowCount();
  47.  
  48.             $sth = $this->db->prepare("SELECT id FROM users WHERE pd_id = :pd_id LIMIT 1");
  49.             $sth->execute(array(
  50.                 ':pd_id' => $pdId,
  51.             ));
  52.             $countPdId = $sth->rowCount();
  53.  
  54.             if (($countUsername > 0) || ($countPdId > 0)) {
  55.                 if (($countUsername > 0) && ($countPdId > 0)) {
  56.                     $this->errMessage = '<li>этот логин уже используется</li><li>этот ID уже привязан</li>';
  57.                 } else if ($countUsername > 0) {
  58.                     $this->errMessage = '<li>этот логин уже используется</li>';
  59.                 } else {
  60.                     $this->errMessage = '<li>этот ID уже привязан</li>';
  61.                 }
  62.             } else {
  63.                 $serviceAccLogin = 'Prodota bot';
  64.                 $serviceAccPass = '***';
  65.  
  66.                 $confirmationCode = mt_rand(10000, 99999);
  67.  
  68.                 $messageTitle = '[PDbet] Код активации';
  69.                 $messageBody = 'Ваш код активации: [b]' . $confirmationCode . '[/b]';
  70.  
  71.                 $pdName = $this->sendMessage($serviceAccLogin, $serviceAccPass, $pdId, $messageTitle, $messageBody);
  72.  
  73.                 if (!empty($pdName)) {
  74.                     Session::set('reg_username', $username);
  75.                     Session::set('reg_password', $password);
  76.                     Session::set('reg_pd_name', $pdName);
  77.                     Session::set('reg_pd_id', $pdId);
  78.                     Session::set('code', $confirmationCode);
  79.                     return TRUE;
  80.                 } else {
  81.                     $this->errMessage = '<li>введён несуществующий ID</li>';
  82.                     return FALSE;
  83.                 }
  84.             }
  85.         } else {
  86.             $this->errMessage = $errMessage;
  87.             return FALSE;
  88.         }
  89.     }
  90.  
  91.     function sendMessage($username, $password, $messageReceiverId, $messageTitle, $messageBody) {
  92.         $cookieID = mt_rand(1, 1000000);
  93.  
  94.         $ch = curl_init();
  95.  
  96.         curl_setopt($ch, CURLOPT_URL, 'http://prodota.ru/forum/index.php?app=core&module=global&section=login');
  97.         curl_setopt($ch, CURLOPT_HEADER, false);
  98.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  99.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  100.         curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies/cookie' . $cookieID);
  101.         curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies/cookie' . $cookieID);
  102.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  103.         $loginPage = curl_exec($ch);
  104.  
  105.         $loginAuthKey = $this->getAuthKey($loginPage);
  106.  
  107.         $loginData = array(
  108.             'auth_key' => $loginAuthKey,
  109.             'referer' => 'http://prodota.ru/forum/index.php',
  110.             'ips_username' => $username,
  111.             'ips_password' => $password,
  112.             'rememberMe' => 1,
  113.         );
  114.  
  115.         curl_setopt($ch, CURLOPT_URL, 'http://prodota.ru/forum/index.php?app=core&module=global&section=login&do=process');
  116.         curl_setopt($ch, CURLOPT_POST, true);
  117.         curl_setopt($ch, CURLOPT_POSTFIELDS, $loginData);
  118.         curl_exec($ch);
  119.  
  120.         curl_setopt($ch, CURLOPT_URL, 'http://prodota.ru/forum/index.php?app=members&module=messaging&section=send&do=form&fromMemberID=' . $messageReceiverId);
  121.         $messageForm = curl_exec($ch);
  122.  
  123.         $messagePayload = $this->getMessagePayload($messageForm);
  124.         $messageEnteredName = $messagePayload['entered_name'];
  125.         $messageAuthKey = $messagePayload['auth_key'];
  126.         $messagePostKey = $messagePayload['postKey'];
  127.  
  128.         $messageData = array(
  129.             'entered_name' => $messageEnteredName,
  130.             'sendType' => 'invite',
  131.             'msg_title' => $messageTitle,
  132.             'isRte' => 0,
  133.             'Post' => $messageBody,
  134.             'topicID' => 0,
  135.             'postKey' => $messagePostKey,
  136.             'auth_key' => $messageAuthKey,
  137.             'dosubmit' => 'Отправить',
  138.         );
  139.  
  140.         curl_setopt($ch, CURLOPT_URL, 'http://prodota.ru/forum/index.php?app=members&module=messaging&section=send&do=send');
  141.         curl_setopt($ch, CURLOPT_POST, true);
  142.         curl_setopt($ch, CURLOPT_POSTFIELDS, $messageData);
  143.         curl_exec($ch);
  144.  
  145.         curl_close($ch);
  146.  
  147.         unlink(dirname(__FILE__) . '/cookies/cookie' . $cookieID);
  148.  
  149.         return $messageEnteredName;
  150.     }
  151.  
  152.     function getAuthKey($page) {
  153.         $html = new simple_html_dom();
  154.         $html->load($page);
  155.  
  156.         foreach ($html->find('input') as $input) {
  157.             $inputs[$input->name] = $input->value;
  158.         }
  159.  
  160.         return $inputs['auth_key'];
  161.     }
  162.  
  163.     function getMessagePayload($page) {
  164.         $html = new simple_html_dom();
  165.         $html->load($page);
  166.  
  167.         foreach ($html->find('input') as $input) {
  168.             $inputs[$input->name] = $input->value;
  169.         }
  170.  
  171.         $messagePayload = array(
  172.             'entered_name' => $inputs['entered_name'],
  173.             'auth_key' => $inputs['auth_key'],
  174.             'postKey' => $inputs['postKey'],
  175.         );
  176.  
  177.         return $messagePayload;
  178.     }
  179.  
  180.     function confirm() {
  181.         $code = Session::get('code');
  182.  
  183.         if (!empty($_POST['code']) && !empty($code)) {
  184.             if (trim($_POST['code']) == Session::get('code')) {
  185.                 $password_enc = new Encryptor(Session::get('reg_password'));
  186.  
  187.                 $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)");
  188.                 $sth->execute(array(
  189.                     ':username' => Session::get('reg_username'),
  190.                     ':password_hash' => $password_enc->hash,
  191.                     ':password_salt' => $password_enc->salt,
  192.                     ':pd_name' => Session::get('reg_pd_name'),
  193.                     ':pd_id' => Session::get('reg_pd_id'),
  194.                 ));
  195.  
  196.                 Session::destroy();
  197.                 return TRUE;
  198.             } else {
  199.                 $this->errMessage = '<li>введён неверный код</li>';
  200.                 return FALSE;
  201.             }
  202.         } else {
  203.             header('Location: register');
  204.             exit();
  205.         }
  206.     }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment