Регистрация (curl, https, captcha)

Discussion in 'PHP' started by NotAffraid, 28 Feb 2013.

  1. NotAffraid

    NotAffraid New Member

    Joined:
    24 Feb 2013
    Messages:
    1
    Likes Received:
    0
    Reputations:
    0
    Здравствуйте, прошу помощи, так как после длительного времени просиживания, ничего не получилось.
    Все время отказ от сервера.

    Представляю вашему вниманию регистрацию аккаунта на World of Tanks.ru .
    Помогите доделать его, потому-что сервер не хочет принимать пост-данные.

    В данном скрипте использую CURL для получения содержимого и отправки POST-запросов.
    Капчу вводим ручками...

    файл index.php:
    PHP:
    <?php
     
    // Кодировка
    header('Content-Type: text/html; charset=utf-8');
     
    ?>
     
    <html>
    <head>
        <title>RegAccWOT v0.1</title>
    </head>
    <body>
     
    <?php
     
    // Настройка локали
    setlocale(LC_ALL'ru_RU.65001''rus_RUS.65001''Russian_Russia. 65001''russian');
     
    // Устанавливаем часовой пояс, Украина/Киев
    date_default_timezone_set('Europe/Kiev');
     
    // Инклюдим класс ассистента
    include_once ("reg.class.php");
     
    // Экземпляр класса
    $regClass = new regClass;
     
    // Отправляем данные
    $regClass->reg_login "yrtytryrtyr1171@gmail.com";
    $regClass->name "yrtytryrtyr1117";
    $regClass->reg_password "yrtytryrtyr1112227";
    $regClass->reg_re_password "yrtytryrtyr1112227";
     
    if (isset(
    $_POST['submit']))
    {
        
    // Отправляем данные
        
    $regClass->cap_1 $_POST['captcha'];
     
        
    // Выполняем
        
    $regClass->registration();
    }
    else
    {
        
    // Выполняем
        
    $regClass->main();
    }
     
    ?>
    </body>
    </html>
    файл reg.class.php:
    PHP:
    <?php
     
    // Класс для регистрации WorldOfTanks.ru
    class regClass
    {
     
        
    // Для регистрации: reg_login, name, reg_password, reg_re_password, cap_0 (captcha).
        // Отправка запроса -> /registration/ru/
     
        // Требуемые данные
        
    public $reg_login$name$reg_password$reg_re_password;
        
    // Спратанные токены
        
    public $csrfmiddlewaretoken$form_token$cap_0;
        
    // Каптча
        
    public $cap_1;
     
        
    // Главный метод
        
    public function main()
        {
            
    // Ищем данные
            
    $this->search_data();
            
    // Показываем полученные данные и каптчу для ввода
            
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
            echo 
    '<table align="center" style="border: gray 1px solid">';
            echo 
    '<th colspan="2" bgcolor="lightgray">Регистрация аккаунтов World of Tanks</th>';
            echo 
    '<tr><td><strong>E-mail:</strong></td><td align="center"><input type="text" name="email" value="'.$this->reg_login.'" maxlength="50" style="width: 100%; border: gray 1px solid" disabled></td></tr>';
            echo 
    '<tr><td><strong>Имя в игре:</strong></td><td align="center"><input type="text" name="login" value="'.$this->name.'" maxlength="50" style="width: 100%; border: gray 1px solid" disabled></td></tr>';
            echo 
    '<tr><td><strong>Пароль:</strong></td><td align="center"><input type="text" name="password" value="'.$this->reg_password.'" maxlength="50" style="width: 100%; border: gray 1px solid" disabled></td></tr>';
            echo 
    '<th colspan="2" bgcolor="lightgray"><hr></th>';
            echo 
    '<tr><td><strong>csrfmiddlewaretoken:</strong></td><td style="border: gray 1px solid">'.$this->csrfmiddlewaretoken.'</td></tr>';
            echo 
    '<tr><td><strong>form_token:</strong></td><td style="border: gray 1px solid">'.$this->form_token.'</td></tr>';
            echo 
    '<tr><td><strong>cap_0:</strong></td><td style="border: gray 1px solid">'.$this->cap_0.'</td></tr>';
            echo 
    '<th colspan="2" bgcolor="lightgray"><hr></th>';
            echo 
    '<tr><td><img src="'.$this->captcha($this->cap_0).'" title="Каптча"></td><td align="center"><input type="text" name="captcha" maxlength="6" size="6" style="width: 100%; border: gray 1px solid; text-align: center; font-size: 2em"></td></tr>';
            echo 
    '<th colspan="2" bgcolor="lightgray"><input type="submit" name="refresh" value="Обновить"><input type="submit" name="submit" value="Регистрировать"></th>';
            echo 
    '</table>';
            echo 
    '</form>';
            echo 
    '<p align="center"><small>&copy; 2013 <strong>RegAccWOT</strong></small></p>';
        }
     
        public function 
    registration()
        {
            
    // Отправем данные для регистрации
            
    $html $this->get_page_https('https://worldoftanks.ru/registration/ru/','csrfmiddlewaretoken='.$this->csrfmiddlewaretoken.'&form_token='.$this->form_token.'&reg_login='.$this->reg_login.'&name='.$this->name.'&reg_password='.$this->reg_password.'&reg_re_password='.$this->reg_re_password.'&cap_0='.$this->cap_0.'&cap_1='.$this->cap_1);;
            echo 
    "Запрос отправлен.";
            echo 
    $html;
        }
     
        private function 
    search_data()
        {
            
    // Получаем содержимое страницы регистрации
            
    $html $this->get_page_https('http://worldoftanks.ru/registration/ru/''');
            
    // Ищем csrfmiddlewaretoken
            
    preg_match_all("~<input type='hidden' name='csrfmiddlewaretoken' value='(.*?)' />~is"$html$matchesPREG_PATTERN_ORDER);
            
    // Ищем form_token
            
    preg_match_all('~<input type="hidden" name="form_token" value="(.*?)" id="id_form_token" />~is'$html$matches2PREG_PATTERN_ORDER);
            
    // Ищем cap_0 (captcha)
            
    preg_match_all('~<input tabindex="5" type="hidden" name="cap_0" value="(.*?)" id="id_cap_0" />~is'$html$matches3PREG_PATTERN_ORDER);
            
    // Заносим данные в глобальные переменные
            
    $this->csrfmiddlewaretoken $matches[1][0];
            
    $this->form_token $matches2[1][0];
            
    $this->cap_0 $matches3[1][0];
        }
     
        private function 
    captcha($hash)
        {
            return 
    $result 'https://worldoftanks.ru/captcha/?session_key='.$hash;
        }
     
        
    // Получение страницы, отправка post-запроса (соеденение https)
        
    private function get_page_https($url$postdata)
        {
            
    // User-agent
            
    $uagent "Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.14";
            
    // Хост
            
    $host "worldoftanks.ru";
            
    // Реферер (откуда мы пришли, для маскировки)
            
    $referer "http://worldoftanks.ru/registration/ru/";
      
            
    // Формируем заголовки
            
    $headers = array   ('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                                
    'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
                                
    'Accept-Encoding: gzip, deflate');
            
    // Инициализация
            
    $ch curl_init($url);
            
    // Отключаем проверку SSL
            
    curl_setopt($chCURLFTPAUTH_SSL0);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
            
    curl_setopt($chCURLOPT_SSL_VERIFYHOST0);    
            
    // Указываем нужные параметры CURL
            
    curl_setopt($chCURLOPT_URL$url);
            
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
            
    curl_setopt($chCURLOPT_HTTPHEADER$headers); 
            
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
            
    curl_setopt($chCURLOPT_ENCODING"chunked");
            
    curl_setopt($chCURLOPT_USERAGENT$uagent);
            
    curl_setopt($chCURLOPT_REFERER$referer);
            
    curl_setopt($chCURLOPT_TIMEOUT120);
     
            if (!empty(
    $postdata))
            {
                
    curl_setopt($chCURLOPT_POST1);
                
    curl_setopt($chCURLOPT_POSTFIELDS$postdata);
            }
     
            
    // Используем для хранения кукисов
            
    curl_setopt($chCURLOPT_COOKIEJAR"cookies.txt");
            
    curl_setopt($chCURLOPT_COOKIEFILE,"cookies.txt");
     
            
    // Получаем только контент
            
    $content curl_exec($ch);
            
            
    // Разрываем соеденение
            
    curl_close($ch);
     
            return 
    $content;
        }
    }
     
    ?>
     
    #1 NotAffraid, 28 Feb 2013
    Last edited: 28 Feb 2013