<?php

namespace Neag\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;

class AdminTestController extends ControllerAbstract
{
    /**
     * @Route("/admin/vk_test")
     * @Template("NeagMainBundle:AdminTest:vk_test.html.twig")
     */
    public function vkTestAction()
    {
        $this->sessionStart();
        
        $request = $this->getRequest();               
        
        $DB = $this->get('database_connection');       
        
        $globals = $this->get('globals');
        
        // Через приложение
        $vk_app_id = '4486474';
        $vk_app_secret = '7wTVwcI8y4irj8495SD0';
        $vk_app_service = '6b0ad71e6b0ad71e6b0ad71e4a6b4ea25466b0a6b0ad71e32ad570de13af8d6755fa7c8';
        
        $vk_bot_login = $globals->getConfigVar('vk_bot_login');
        $vk_bot_pass = $globals->getConfigVar('vk_bot_pass');
        $vk_bot_page_domain = $globals->getConfigVar('vk_bot_page_domain');
        $this->trace($vk_bot_login); 
        $this->trace($vk_bot_pass);
        $this->trace($vk_bot_page_domain);
        
        $cookies = Array();
        /*
        $cookies = Array(
            'remixdt' => '25200',
            'remixflash' => '18.0.0',
            'remixlang' => '0',
            'remixlhk' => '864dddb2a2ee10024c',
            'remixscreen_depth' => '24',
        );
        */
        $cookies_str = '';
        foreach ($cookies as $key => $value) {
            if (!empty($cookies_str)) {
                $cookies_str .= '; ';
            }
            $cookies_str .= $key.'='.$value;
        }
        $this->trace($cookies_str);
        
        //$browser = 'Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0';
        $browser = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36';
        //$browser = 'Opera 10.00';
        
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, "http://oauth.vk.com/authorize?client_id=4486474&display=page&redirect_uri=http://m.vk.com/debri_dv_test");
        //curl_setopt($res, CURLOPT_URL, "https://m.vk.com");
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($res, CURLOPT_CERTINFO, true); 
        curl_setopt($res, CURLOPT_SSL_VERIFYHOST, true); 
        curl_setopt($res, CURLOPT_SSL_VERIFYPEER, true); 
        $result = curl_exec($res);
        $info = curl_getinfo($res);
        $this->trace($info);
        $this->trace($result);
        $cookies = $this->getCookiesFromResponse($result);
        $cookies_str = $this->buildCookiesString($cookies);       
        /*
        $cookies['remixdt'] = '25200';
        $cookies['t'] = '11fce2504def84e2b3ae5ee2';
        $cookies['remixflash'] = '18.0.0';
        $cookies['remixscreen_depth'] = '24';
        */
        $this->trace($cookies);
        $this->trace($cookies_str);
        curl_close($res);
        
        //$action = $this->getStrBetween('<form method="post" action="', '" novalidate>', $result);
        $action = $this->getStrBetween('<form method="POST" id="login_submit" action="', '">', $result);
        $action = str_replace('https://', 'http://', $action);
        $this->trace('result3 action: '.$action);
        
        /*
        $start_tag = '<form method="post"';
        $end_tag = '</form>';
        $result2 = $this->getStrBetween($start_tag, $end_tag, $result);
        $this->trace('result2: '.$result2);
        */
        $post = Array();
        $post['email'] = $vk_bot_login;
        $post['pass'] = $vk_bot_pass;
        $post['ip_h'] = $this->getStrBetween('<input type="hidden" name="ip_h" value="', '" />', $result);
        $post['lg_h'] = $this->getStrBetween('<input type="hidden" name="lg_h" value="', '" />', $result);
        $post['_origin'] = $this->getStrBetween('<input type="hidden" name="_origin" value="', '" />', $result);
        $post['to'] = $this->getStrBetween('<input type="hidden" name="to" value="', '" />', $result);
        $post['expire'] = $this->getStrBetween('<input type="hidden" name="expire" value="', '" />', $result);
        $this->trace($post);
        
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, $action);
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($res, CURLINFO_HEADER_OUT, true);
        curl_setopt($res, CURLOPT_POST, 1);
        curl_setopt($res, CURLOPT_VERBOSE, 1); 
        curl_setopt($res, CURLOPT_CERTINFO, 1); 
        curl_setopt($res, CURLOPT_POSTFIELDS, $post);
        curl_setopt($res, CURLOPT_ENCODING, 'gzip');
        $result3 = curl_exec($res);
        $info = curl_getinfo($res);
        $this->trace('result3:'); 
        $this->trace($info);        
        $this->trace($result3);
        curl_close($res);
        
        $cookies2 = $this->getCookiesFromResponse($result3);
        $cookies = array_merge($cookies, $cookies2);
        unset($cookies['h']);
        unset($cookies['s']);
        unset($cookies['l']);
        unset($cookies['p']);
        $cookies_str = $this->buildCookiesString($cookies);
        $this->trace($cookies);
        
        $this->trace('$result3 redirect url: '.$info['redirect_url']);
        
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, $info['redirect_url']);
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($res, CURLINFO_HEADER_OUT, true);
        curl_setopt($res, CURLOPT_POST, 1);
        curl_setopt($res, CURLOPT_VERBOSE, 1); 
        curl_setopt($res, CURLOPT_CERTINFO, 1); 
        //curl_setopt($res, CURLOPT_POSTFIELDS, $post);
        curl_setopt($res, CURLOPT_ENCODING, 'gzip');
        $result3_2 = curl_exec($res);
        $info = curl_getinfo($res);
        $this->trace('result3_2:'); 
        $this->trace($info);        
        $this->trace($result3_2);
        curl_close($res);
        
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, 'http://m.vk.com/debri_dv_test');
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($res, CURLINFO_HEADER_OUT, true);
        curl_setopt($res, CURLOPT_POST, 1);
        curl_setopt($res, CURLOPT_VERBOSE, 1); 
        curl_setopt($res, CURLOPT_CERTINFO, 1); 
        //curl_setopt($res, CURLOPT_POSTFIELDS, $post);
        curl_setopt($res, CURLOPT_ENCODING, 'gzip');
        $result3_3 = curl_exec($res);
        $info = curl_getinfo($res);
        $this->trace('result3_3:'); 
        $this->trace($info);        
        $this->trace($result3_3);
        curl_close($res);
        
        //$cookies2 = $this->getCookiesFromResponse($result3);
        //$cookies = array_merge($cookies, $cookies2);        
        
        if (!empty($info['redirect_url'])) {
            $res = curl_init();
            curl_setopt($res, CURLOPT_URL, $info['redirect_url']);
            //curl_setopt($res, CURLOPT_URL, "http://vk.com/login.php?act=slogin&role=fast&no_redirect=1&to=&s=0");
            curl_setopt($res, CURLOPT_USERAGENT, $browser);
            curl_setopt($res, CURLOPT_HEADER, 1);
            curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
            curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($res, CURLINFO_HEADER_OUT, true);
            curl_setopt($res, CURLOPT_VERBOSE, 1); 
            curl_setopt($res, CURLOPT_CERTINFO, 1); 
            curl_setopt($res, CURLOPT_ENCODING, 'gzip');
            $result4 = curl_exec($res);
            $info = curl_getinfo($res);
            //$this->trace('result4:');
            //$this->trace($info);        
            //$this->trace($result4);
            curl_close($res);
            $cookies2 = $this->getCookiesFromResponse($result4);
            $cookies = array_merge($cookies, $cookies2);
            unset($cookies['remixlhk']);
            unset($cookies['remixtemp_sid']);
            foreach ($cookies as $key => $value) {
                if (substr($key, 0, 7) == 'remixq_') {
                    unset($cookies[$key]);                    
                }
            }
            $cookies_str = $this->buildCookiesString($cookies); 
            if (!empty($info['redirect_url'])) {
                // Успешная авторизация
                /*
                $res = curl_init();
                curl_setopt($res, CURLOPT_URL, $info['redirect_url']);                
                curl_setopt($res, CURLOPT_USERAGENT, $browser);
                curl_setopt($res, CURLOPT_HEADER, 1);
                curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
                curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($res, CURLINFO_HEADER_OUT, true);
                curl_setopt($res, CURLOPT_VERBOSE, 1); 
                curl_setopt($res, CURLOPT_CERTINFO, 1); 
                curl_setopt($res, CURLOPT_ENCODING, 'gzip');
                $result5 = curl_exec($res);
                $info = curl_getinfo($res);
                $this->trace('result5:');
                $this->trace($info);        
                $this->trace($result5);
                */
                // Стена сообщества
                $res = curl_init();
                curl_setopt($res, CURLOPT_URL, 'http://m.vk.com/debri_dv_test');                
                curl_setopt($res, CURLOPT_USERAGENT, $browser);
                curl_setopt($res, CURLOPT_HEADER, 1);
                curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
                curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($res, CURLINFO_HEADER_OUT, true);
                curl_setopt($res, CURLOPT_VERBOSE, 1); 
                curl_setopt($res, CURLOPT_CERTINFO, 1); 
                curl_setopt($res, CURLOPT_ENCODING, 'gzip');
                $result6 = curl_exec($res);
                $info = curl_getinfo($res);
                $this->trace('result6:');
                $this->trace($info);        
                $this->trace($result6);
                $action = $this->getStrBetween('form action="', '" method="post">', $result6);
                $action = 'http://m.vk.com'.$action;
                //$this->trace($action);
                
                // Отправка публикации
                $post = Array();
                $post['message'] = 'test'."\n".'http://debri-dv.com/article/12212/na_rodine_primorskih_partizan';
                //$this->trace($post);
                
                $res = curl_init();
                curl_setopt($res, CURLOPT_URL, $action);
                curl_setopt($res, CURLOPT_USERAGENT, $browser);
                curl_setopt($res, CURLOPT_HEADER, 1);
                curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
                curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($res, CURLINFO_HEADER_OUT, true);
                curl_setopt($res, CURLOPT_POST, 1);
                curl_setopt($res, CURLOPT_VERBOSE, 1); 
                curl_setopt($res, CURLOPT_CERTINFO, 1); 
                curl_setopt($res, CURLOPT_POSTFIELDS, $post);
                curl_setopt($res, CURLOPT_ENCODING, 'gzip');
                $result7 = curl_exec($res);
                $info = curl_getinfo($res);
                $this->trace('result7:'); 
                $this->trace($info);        
                $this->trace($result7);
                curl_close($res);
            }    
        }
        
        /*
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, "http://login.vk.com/?act=login");
        //curl_setopt($res, CURLOPT_URL, "http://vk.com/login.php?act=slogin&role=fast&no_redirect=1&to=&s=0");
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($res, CURLINFO_HEADER_OUT, true);
        curl_setopt($res, CURLOPT_POST, 1);
        curl_setopt($res, CURLOPT_VERBOSE, 1); 
        curl_setopt($res, CURLOPT_CERTINFO, 1); 
        curl_setopt($res, CURLOPT_POSTFIELDS, $post);
        curl_setopt($res, CURLOPT_ENCODING, 'gzip');
        $result3 = curl_exec($res);
        $info = curl_getinfo($res);
        $this->trace($info);        
        $this->trace($result3);
        curl_close($res);
        */
        
        /*
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, "http://vk.com");
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);        
        $result = curl_exec($res);
        $info = curl_getinfo($res);
        $this->trace($info);
        //$this->trace($result);
        $cookies = $this->getCookiesFromResponse($result);
        $cookies_str = $this->buildCookiesString($cookies);
        $cookies['remixdt'] = '25200';
        $cookies['t'] = '11fce2504def84e2b3ae5ee2';
        $cookies['remixflash'] = '18.0.0';
        $cookies['remixscreen_depth'] = '24';
        $this->trace($cookies);
        $this->trace($cookies_str);
        curl_close($res);
        
        //$this->trace($_SERVER);
        
        $start_tag = '<form method="POST" name="login" id="quick_login_form" action="https://login.vk.com/?act=login" onsubmit="if (vklogin) {return true} else {quick_login();return false;}">';
        $end_tag = '</form>';
        $result2 = $this->getStrBetween($start_tag, $end_tag, $result);
        $this->trace('result2: '.$result2);
        $post = Array();
        $before = '<input type="hidden" name="act" value="';
        $after = '" />';
        $post['act'] = $this->getStrBetween($before, $after, $result2);
        $before = '<input type="hidden" name="role" value="';
        $after = '" />';
        $post['role'] = $this->getStrBetween($before, $after, $result2);
        $before = '<input type="hidden" name="expire" id="quick_expire_input" value="';
        $after = '" />';
        $post['expire'] = $this->getStrBetween($before, $after, $result2);
        $before = '<input type="hidden" name="captcha_sid" id="quick_captcha_sid" value="';
        $after = '" />';
        $post['captcha_sid'] = $this->getStrBetween($before, $after, $result2);
        $before = '<input type="hidden" name="captcha_key" id="quick_captcha_key" value="';
        $after = '" />';
        $post['captcha_key'] = $this->getStrBetween($before, $after, $result2);
        $before = '<input type="hidden" name="_origin" value="';
        $after = '" />';
        $post['_origin'] = $this->getStrBetween($before, $after, $result2);
        $before = '<input type="hidden" name="ip_h" value="';
        $after = '" />';
        $post['ip_h'] = $this->getStrBetween($before, $after, $result2);
        $before = '<input type="hidden" name="lg_h" value="';
        $after = '" />';
        $post['lg_h'] = $this->getStrBetween($before, $after, $result2);
        $post['email'] = $vk_bot_login;
        $post['pass'] = $vk_bot_pass;
        $this->trace($post);
        
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, "http://login.vk.com/?act=login");
        //curl_setopt($res, CURLOPT_URL, "http://vk.com/login.php?act=slogin&role=fast&no_redirect=1&to=&s=0");
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($res, CURLINFO_HEADER_OUT, true);
        curl_setopt($res, CURLOPT_POST, 1);
        curl_setopt($res, CURLOPT_VERBOSE, 1); 
        curl_setopt($res, CURLOPT_CERTINFO, 1); 
        curl_setopt($res, CURLOPT_POSTFIELDS, $post);
        curl_setopt($res, CURLOPT_ENCODING, 'gzip');
        $result3 = curl_exec($res);
        $info = curl_getinfo($res);
        $this->trace($info);        
        $this->trace($result3);
        curl_close($res);
        
        $cookies2 = $this->getCookiesFromResponse($result3);
        $cookies = array_merge($cookies, $cookies2);
        unset($cookies['h']);
        unset($cookies['s']);
        unset($cookies['l']);
        unset($cookies['p']);
        //$this->trace($cookies);
        $cookies_str = $this->buildCookiesString($cookies);
        //$this->trace($info['redirect_url']);
        if (!empty($info['redirect_url'])) {
            $res = curl_init();
            curl_setopt($res, CURLOPT_URL, $info['redirect_url']);
            //curl_setopt($res, CURLOPT_URL, "http://vk.com/login.php?act=slogin&role=fast&no_redirect=1&to=&s=0");
            curl_setopt($res, CURLOPT_USERAGENT, $browser);
            curl_setopt($res, CURLOPT_HEADER, 1);
            curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
            curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($res, CURLINFO_HEADER_OUT, true);
            curl_setopt($res, CURLOPT_VERBOSE, 1); 
            curl_setopt($res, CURLOPT_CERTINFO, 1); 
            curl_setopt($res, CURLOPT_ENCODING, 'gzip');
            $result4 = curl_exec($res);
            $info = curl_getinfo($res);
            $this->trace($info);        
            $this->trace($result4);
            curl_close($res);
            $cookies2 = $this->getCookiesFromResponse($result4);
            $cookies = array_merge($cookies, $cookies2);
            unset($cookies['remixlhk']);
            unset($cookies['remixreg_sid']);
            unset($cookies['remixapi_sid']);
            unset($cookies['remixrec_sid']);
            foreach ($cookies as $key => $value) {
                if (substr($key, 0, 7) == 'remixq_') {
                    unset($cookies[$key]);                    
                }
            }
            $this->trace($cookies);
            // notifier
            $this->trace('notifier:');
            $res = curl_init();
            curl_setopt($res, CURLOPT_URL, "http://vk.com/notifier.php?act=storage_frame&from=vk.com&4");
            curl_setopt($res, CURLOPT_USERAGENT, $browser);
            curl_setopt($res, CURLOPT_HEADER, 1);
            curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
            curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($res, CURLINFO_HEADER_OUT, true);
            curl_setopt($res, CURLOPT_VERBOSE, 1); 
            curl_setopt($res, CURLOPT_CERTINFO, 1); 
            curl_setopt($res, CURLOPT_ENCODING, 'gzip');
            $result5 = curl_exec($res);
            $info = curl_getinfo($res);
            $this->trace($info);        
            $this->trace($result5);
            curl_close($res);
            // Конец авторизации
            $res = curl_init();
            curl_setopt($res, CURLOPT_URL, 'http://vk.com/id322203687');
            curl_setopt($res, CURLOPT_USERAGENT, $browser);
            curl_setopt($res, CURLOPT_HEADER, 1);
            curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
            curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($res, CURLINFO_HEADER_OUT, true);
            curl_setopt($res, CURLOPT_VERBOSE, 1); 
            curl_setopt($res, CURLOPT_CERTINFO, 1); 
            curl_setopt($res, CURLOPT_ENCODING, 'gzip');
            $result5 = iconv('windows-1251', 'utf-8', curl_exec($res));
            $info = curl_getinfo($res);
            $this->trace($info);        
            $this->trace($result5);
            curl_close($res);
        }
        
        */
        
        //$result = 'result';          
        
        //$this->trace($this->getLineContent($result, 190));       
        //$this->trace($result);
        //$this->trace($array);  
        
        return array(
        );
    }
    
    /**
     * @Route("/admin/ok_test")
     * @Template("NeagMainBundle:AdminTest:ok_test.html.twig")
     */
    public function okTestAction()
    {
        $this->sessionStart();
        
        $request = $this->getRequest();               
        
        $DB = $this->get('database_connection');       
        
        $globals = $this->get('globals');  
        
        $ok_bot_login = $globals->getConfigVar('ok_bot_login');
        $ok_bot_pass = $globals->getConfigVar('ok_bot_pass');
        $ok_bot_page_domain = $globals->getConfigVar('ok_bot_page_domain');
        $this->trace($ok_bot_login); 
        $this->trace($ok_bot_pass);
        $this->trace($ok_bot_page_domain);
        
        $cookies = Array();
        $cookies_str = '';
        foreach ($cookies as $key => $value) {
            if (!empty($cookies_str)) {
                $cookies_str .= '; ';
            }
            $cookies_str .= $key.'='.$value;
        }
        $this->trace($cookies_str);
        
        //$browser = 'Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0';
        $browser = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36';
        //$browser = 'Opera 10.00';
        
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, "http://m.ok.ru");
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);        
        $result = curl_exec($res);
        $info = curl_getinfo($res);
        //$this->trace($info);
        //$this->trace($result);
        $time = strtotime("-1 month");
        $date_i = (int)date('i', $time);
        $date_s = (int)date('s', $time);
        $cookies = $this->getCookiesFromResponse($result);
        $cookies['ClientTimeDiff'] = '-4210347';
        $cookies['ClientTimeStr'] = date('j_n_Y_G_', $time).$date_i.'_'.$date_s;
        $cookies['DCAPS'] = 'dpr%5E1%7Cvw%5E2008%7Csw%5E2048%7C';
        $cookies['TimezoneOffset'] = '-660';
        $cookies['__dc'] = 'on';
        $cookies_str = $this->buildCookiesString($cookies);       
        //$cookies['remixdt'] = '25200';
        //$cookies['t'] = '11fce2504def84e2b3ae5ee2';
        //$cookies['remixflash'] = '18.0.0';
        //$cookies['remixscreen_depth'] = '24';
        $this->trace($cookies);
        $this->trace($cookies_str);
        curl_close($res);        
       
        $action = $this->getStrBetween('<form action="', '" method="post" class="nf nc"', $result);
        $action = str_replace('&amp;', '&', $action);
        //$action = str_replace('https://', 'http://', $action);
        $this->trace('action: '.$action);
        
        //$start_tag = '<form';
        //$end_tag = '</form>';
        //$result2 = $this->getStrBetween($start_tag, $end_tag, $result);
        //$this->trace('result2: '.$result2);
        $post = Array(); 
        $post['fr.login'] = $ok_bot_login;
        $post['fr.password'] = $ok_bot_pass;
        $post['fr.posted'] = $this->getStrBetween('<input type="hidden" name="fr.posted" value="', '" id="" />', $result);
        $post['fr.needCaptcha'] = $this->getStrBetween('<input type="hidden" name="fr.needCaptcha" value="', '" id="field_needCaptcha" />', $result);
        $post['fr.proto'] = $this->getStrBetween('<input type="hidden" name="fr.proto" value="', '" id="field_proto" />', $result);
        $post['fr.posted'] = $this->getStrBetween('<input type="hidden" name="fr.posted" value="', '" id="" />', $result);
        $post['fr.https_switch'] = '';
        $post['button_login'] = $this->getStrBetween('<input value="', '" class="base-button_target" name="button_login" type="submit">', $result);        
        $this->trace($post);
        
        $res = curl_init();
        curl_setopt($res, CURLOPT_URL, $action);
        curl_setopt($res, CURLOPT_USERAGENT, $browser);
        curl_setopt($res, CURLOPT_HEADER, 1);
        curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
        curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($res, CURLINFO_HEADER_OUT, true);
        curl_setopt($res, CURLOPT_POST, 1);
        curl_setopt($res, CURLOPT_VERBOSE, 1); 
        curl_setopt($res, CURLOPT_CERTINFO, 1); 
        curl_setopt($res, CURLOPT_POSTFIELDS, http_build_query($post));
        curl_setopt($res, CURLOPT_ENCODING, 'gzip');
        $result3 = curl_exec($res);
        $info = curl_getinfo($res);
        $this->trace('result3:'); 
        $this->trace($info);        
        $this->trace($result3);
        curl_close($res);
        
        $cookies2 = $this->getCookiesFromResponse($result3);
        $cookies = array_merge($cookies, $cookies2);
        //unset($cookies['h']);
        $cookies_str = $this->buildCookiesString($cookies);
        $this->trace($cookies);
        
        if (!empty($info['redirect_url'])) {
            $res = curl_init();
            curl_setopt($res, CURLOPT_URL, $info['redirect_url']);
            //curl_setopt($res, CURLOPT_URL, "http://ok.com/login.php?act=slogin&role=fast&no_redirect=1&to=&s=0");
            curl_setopt($res, CURLOPT_USERAGENT, $browser);
            curl_setopt($res, CURLOPT_HEADER, 1);
            curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
            curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($res, CURLINFO_HEADER_OUT, true);
            curl_setopt($res, CURLOPT_VERBOSE, 1); 
            curl_setopt($res, CURLOPT_CERTINFO, 1); 
            curl_setopt($res, CURLOPT_ENCODING, 'gzip');
            $result4 = curl_exec($res);
            $info = curl_getinfo($res);
            $this->trace('result4:', $result4);
            $this->trace($info);        
            curl_close($res);
            $cookies2 = $this->getCookiesFromResponse($result4);
            $cookies = array_merge($cookies, $cookies2);
            $cookies_str = $this->buildCookiesString($cookies); 
            if (!empty($info['redirect_url'])) {
                // Проверка телефона
                $res = curl_init();
                curl_setopt($res, CURLOPT_URL, $info['redirect_url']);
                //curl_setopt($res, CURLOPT_URL, "http://ok.com/login.php?act=slogin&role=fast&no_redirect=1&to=&s=0");
                curl_setopt($res, CURLOPT_USERAGENT, $browser);
                curl_setopt($res, CURLOPT_HEADER, 1);
                curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
                curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($res, CURLINFO_HEADER_OUT, true);
                curl_setopt($res, CURLOPT_VERBOSE, 1); 
                curl_setopt($res, CURLOPT_CERTINFO, 1); 
                curl_setopt($res, CURLOPT_ENCODING, 'gzip');
                $result5 = curl_exec($res);
                $info = curl_getinfo($res);
                $this->trace('result5:', $result5);
                $this->trace($info);        
                curl_close($res);
                $cookies2 = $this->getCookiesFromResponse($result5);
                $cookies = array_merge($cookies, $cookies2);
                $cookies_str = $this->buildCookiesString($cookies); 
            }    
            /*
            if (!empty($info['redirect_url'])) {
                // Успешная авторизация
                $res = curl_init();
                curl_setopt($res, CURLOPT_URL, $info['redirect_url']);                
                curl_setopt($res, CURLOPT_USERAGENT, $browser);
                curl_setopt($res, CURLOPT_HEADER, 1);
                curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
                curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($res, CURLINFO_HEADER_OUT, true);
                curl_setopt($res, CURLOPT_VERBOSE, 1); 
                curl_setopt($res, CURLOPT_CERTINFO, 1); 
                curl_setopt($res, CURLOPT_ENCODING, 'gzip');
                $result5 = curl_exec($res);
                $info = curl_getinfo($res);
                $this->trace('result5:');
                $this->trace($info);        
                $this->trace($result5);
                // Стена сообщества
                $res = curl_init();
                curl_setopt($res, CURLOPT_URL, 'http://m.ok.com/debri_dv_test');                
                curl_setopt($res, CURLOPT_USERAGENT, $browser);
                curl_setopt($res, CURLOPT_HEADER, 1);
                curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
                curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($res, CURLINFO_HEADER_OUT, true);
                curl_setopt($res, CURLOPT_VERBOSE, 1); 
                curl_setopt($res, CURLOPT_CERTINFO, 1); 
                curl_setopt($res, CURLOPT_ENCODING, 'gzip');
                $result6 = curl_exec($res);
                $info = curl_getinfo($res);
                $this->trace('result6:');
                $this->trace($info);        
                $this->trace($result6);
                $action = $this->getStrBetween('form action="', '" method="post">', $result6);
                $action = 'http://m.ok.com'.$action;
                //$this->trace($action);
                
                // Отправка публикации
                $post = Array();
                $post['message'] = 'test'."\n".'http://debri-dv.com/article/12212/na_rodine_primorskih_partizan';
                //$this->trace($post);
                
                $res = curl_init();
                curl_setopt($res, CURLOPT_URL, $action);
                curl_setopt($res, CURLOPT_USERAGENT, $browser);
                curl_setopt($res, CURLOPT_HEADER, 1);
                curl_setopt($res, CURLOPT_COOKIE, $cookies_str);
                curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($res, CURLINFO_HEADER_OUT, true);
                curl_setopt($res, CURLOPT_POST, 1);
                curl_setopt($res, CURLOPT_VERBOSE, 1); 
                curl_setopt($res, CURLOPT_CERTINFO, 1); 
                curl_setopt($res, CURLOPT_POSTFIELDS, $post);
                curl_setopt($res, CURLOPT_ENCODING, 'gzip');
                $result7 = curl_exec($res);
                $info = curl_getinfo($res);
                $this->trace('result7:'); 
                $this->trace($info);        
                $this->trace($result7);
                curl_close($res);
            }
            */    
        }  
        
        return array(
        );
    }
    
    /**
     * Получает содержимое указанной строки
     */
    public function getLineContent($str, $str_n)
    {
        $lines = explode("\n", $str);
        //$this->trace('lines total: '.count($lines));
        return $lines[($str_n-1)];
    }
    
    /**
     * Получает фрагмент между задаными строками
     */
    public function getStrBetween($before, $after, $str)
    {
        $start_tag = $before;
        $end_tag = $after;
        $pattern = '/'.preg_quote($start_tag, '/').'(.*)'.preg_quote($end_tag, '/').'/misU';
        preg_match_all($pattern, $str, $matches);  
        return !empty($matches[1][0]) ? $matches[1][0] : false;
    }
    
    /**
     * Получает содержимое указанной строки
     */
    public function getCookiesFromResponse($str)
    {
        preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $str, $matches);
        $cookies = array();
        foreach($matches[1] as $item) {
            parse_str($item, $cookie);
            $cookies = array_merge($cookies, $cookie);
        }
        return $cookies;
    }
    
    /**
     * Получает содержимое указанной строки
     */
    public function buildCookiesString($cookies)
    {
        $cookies_str = '';
        foreach ($cookies as $key => $value) {
            if (!empty($cookies_str)) {
                $cookies_str .= '; ';
            }
            $cookies_str .= $key.'='.$value;
        }
        return $cookies_str;
    }
    
    /**
     * Очищает кеш
     */
    public function clearCache()
    {
        $cm = $this->cacheManager();
        //$cm->clear('main_short_news');
    }    

}
