Pull to refresh

Gmail grabber — класс парсинга контактного листа

Reading time5 min
Views914
Собственно говоря читайте сабж.

Функции:

Login — Вход в gmail. Возвращает стандартно — true/false
Сразу после входа в систему проиходит импорт контактного листа в переменную ContactList

Сам класс:

<?php

class cmgmail
{
  
  var $Header;
  var $UserAgent;
  var $CookieFile;
  
  var $ContactList;
  
  function cmgmail($CookieFile)
  {

    $this->UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MRSPUTNIK 2, 0, 0, 36 SW)" ;
    $this->Header [] = "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*" ;
    $this->Header [] = "Accept_encoding: gzip, deflate";
    $this->Header [] = "Accept_language: ru";
    $this->Header [] = "Connection: Keep-Alive";
    $this->Header [] = "Cache-Control: no-cache";
    $this->Header [] = "Content-Type: application/x-www-form-urlencoded";

    $this->CookieFile = $CookieFile;
    
    @unlink( $this->CookieFile );
  }

  function curlRequest($url, $method = 'get', $post_vars = array(), $refferer = '')
  {

    $ch = curl_init( $url );

    if ($method == 'post') {
      curl_setopt  ( $ch , CURLOPT_POST , 1);
      curl_setopt  ( $ch , CURLOPT_POSTFIELDS , http_build_query($post_vars));
    }

    @curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION, 1);
    @curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ( $ch , CURLOPT_COOKIEJAR, $this->CookieFile);
      curl_setopt ( $ch , CURLOPT_COOKIEFILE, $this->CookieFile);
    @curl_setopt ( $ch , CURLOPT_USERAGENT , $this->UserAgent );
    @curl_setopt ( $ch , CURLOPT_HTTPHEADER , $this->Header );
     curl_setopt ( $ch , CURLOPT_HEADER, 1);
     curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST, 2);
     curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT, 10);
     curl_setopt ( $ch , CURLOPT_TIMEOUT, 25);
    
    $result = curl_exec( $ch );

    return $result;
  }
  
  function getContactList($content_page)
  {
    $result = array();

    preg_match('/"cla".*?D\((.+?)\);/is', $content_page, $all);
    if ( !empty($all[1]) )
    {
      preg_match_all('/\["ce"(.+?)\]/', $all[1], $list);
      
      if ( !empty($list[1]) )
      {
        foreach ( $list[1] as $item )
        {
          $item = explode(',', $item);
          if ( !empty($item[4]) )
          {
            if ( empty($item[2]) ) $item[2] = $item[4];
            
            $item[2] = str_replace('"', '', $item[2]);
            $item[4] = str_replace('"', '', $item[4]);
            
            $result[ $item[2] ] = $item[4];
          }
        }
      }
    }
    
    return $result;
  }
  
  function Login($username, $password)
  {
    $username = str_replace( '@gmail.com', '', $username);
    
    $first = $this->curlRequest('http://mail.google.com/mail/', 'get', false);
    
    $Post = array(
      'ltmpl' => 'default',
      'ltmplcache' => '2',
      'continue' => 'http://mail.google.com/mail/?',
      'service' => 'mail',
      'rm' => 'false',
      'ltmpl' => 'default',
      'ltmpl' => 'default',
      'Email' => $username,
      'Passwd' => $password,
      'rmShown' => '1',
      'asts' => '',
    );
    
    $loginpage = $this->curlRequest('https://www.google.com/accounts/ServiceLoginAuth?service=mail', 'post', $Post, 'https://www.google.com/accounts/ServiceLogin?service=mail&passive=true&rm=false&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy%3Dl&bsv=1k96igf4806cy&ltmpl=default&ltmplcache=2');
    
    if (preg_match('/location\.replace\("(.+?)"/i', $loginpage, $redir)) {
      
      $redir = str_replace('\x3d', '=', $redir[1]);
      $redir = str_replace('\x26', '&', $redir);
      $redir = str_replace('&', '&', $redir);

      $next_login = $this->curlRequest($redir);
      $next_login = $this->curlRequest('http://mail.google.com/mail/?ui=1&view=cl&search=contacts&pnl=a','get',false,$redir);
      
      $this->ContactList = $this->getContactList($next_login);

      return true;
      
    } else return false;
      
  }

}

?>


* This source code was highlighted with Source Code Highlighter.
Tags:
Hubs:
If this publication inspired you and you want to support the author, do not hesitate to click on the button
Total votes 19: ↑11 and ↓8+3
Comments9

Articles