class Model_OAuth_Gmail { // OAuth public function Connect( $callback ); // Access Token ( ) public function getConnection($accessToken); // const MODE_NONE = 0; const MODE_MESSAGES = 1; const MODE_THREAD = 2; // : ( getConnection ), public function searchMessages($imapConnection, $params, $mode = 0); }
public function Connect( $callback ) { $this -> urls['callbackUrl'] = $callback; $session = new Zend_Session_Namespace('OAuth'); $OAuth_Consumer = new Zend_Oauth_Consumer(array_merge($this->config, $this->urls)); try { if (!isset($session -> accessToken)) { if (!isset($session -> requestToken)) { $session -> requestToken = $OAuth_Consumer -> getRequestToken(array('scope' => $this -> scopes), "GET"); $OAuth_Consumer -> redirect(); } else { $session -> accessToken = $OAuth_Consumer -> getAccessToken($_GET, $session -> requestToken); } } $accessToken = $session -> accessToken; $session -> unsetAll(); unset($session); return $accessToken; } catch( exception $e) { $session -> unsetAll(); throw new Zend_Exception("Error occurred. try to reload this page", 5); } }
protected $config = array( 'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER, 'version' => '1.0', 'consumerKey' => 'anonymous', 'signatureMethod' => 'HMAC-SHA1', 'consumerSecret' => 'anonymous', ); protected $urls = array('callbackUrl' => "", 'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken', 'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken', 'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken' ); protected $scopes = 'https://mail.google.com/ https://www.googleapis.com/auth/userinfo#email';
public function getConnection($accessToken) { $config = new Zend_Oauth_Config(); $config -> setOptions($this::config); $config -> setToken(unserialize($user::accessToken)); $config -> setRequestMethod('GET'); $url = 'https://mail.google.com/mail/b/' . $user -> email . '/imap/'; $urlWithXoauth = $url . '?xoauth_requestor_id=' . urlencode($user -> email); $httpUtility = new Zend_Oauth_Http_Utility(); /** * Get an unsorted array of oauth params, * including the signature based off those params. */ $params = $httpUtility -> assembleParams($url, $config, array('xoauth_requestor_id' => $user -> email)); /** * Sort parameters based on their names, as required * by OAuth. */ ksort($params); /** * Construct a comma-deliminated,ordered,quoted list of * OAuth params as required by XOAUTH. * * Example: oauth_param1="foo",oauth_param2="bar" */ $first = true; $oauthParams = ''; foreach ($params as $key => $value) { // only include standard oauth params if (strpos($key, 'oauth_') === 0) { if (!$first) { $oauthParams .= ','; } $oauthParams .= $key . '="' . urlencode($value) . '"'; $first = false; } } /** * Generate SASL client request, using base64 encoded * OAuth params */ $initClientRequest = 'GET ' . $urlWithXoauth . ' ' . $oauthParams; $initClientRequestEncoded = base64_encode($initClientRequest); /** * Make the IMAP connection and send the auth request */ $imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true); $authenticateParams = array('XOAUTH', $initClientRequestEncoded); $imap -> requestAndResponse('AUTHENTICATE', $authenticateParams); return $imap; }
$searchString = 'X-GM-RAW "'; foreach ($params as $key => $value) switch ($key) { // this is dates case "before" : case "after" : $searchString .= $key . ":" . date("Y/m/d", $value) . " "; break; // this is simple strings default : $searchString .= $key . ":" . $value . " "; break; } $searchString = trim($searchString) . '"';
$messages = $imapConnection -> search(array($searchString));
if (isset($params['in'])){ $imapConnection->examine(strtoupper(($params['in']))); } else { $imapConnection->examine("INBOX"); } $messages = $imapConnection -> search(array($searchString));
protected function getFolder($imap, $folder) { $response = $imap -> requestAndResponse('XLIST "" "*"'); $folders = array(); foreach ($response AS $item) { if ($item[0] != "XLIST") { continue; } $folders[strtoupper(str_replace('\\', '', end($item[1])))] = $item[3]; } return $folders[$folder]; }
if (isset($params['in'])) $imapConnection -> select($this -> getFolder($imapConnection, strtoupper($params['in']))); $messages = $imapConnection -> search(array($searchString));
switch ( $mode ) { case $this::MODE_NONE : return $messages; case $this::MODE_MESSAGES : // fetching (get content of messages) $messages = $imapConnection -> requestAndResponse("FETCH " . implode(',', $messages) . " (X-GM-THRID)"); return $messages; case $this::MODE_THREAD : $messages = $imapConnection -> requestAndResponse("FETCH " . implode(',', $messages) . " (X-GM-THRID)"); $storage = new Zend_Mail_Storage_Imap($imapConnection); $storage -> selectFolder( $this -> getFolder($imapConnection, strtoupper($params['in'])) ); $threads = array(); if ($messages) foreach ($messages AS $message) { if (isset($message[2][1])) { $thread_id = $message[2][1]; if (!isset($threads[$thread_id])) { $threads[$thread_id] = array('all' => $imapConnection -> requestAndResponse("SEARCH X-GM-THRID $thread_id"), 'my' => array()); unset($threads[$thread_id]['all'][0][0]); } $threads[$thread_id]['my'][] = $message[0]; } } $result = array(); foreach ($threads as $thread) if (!array_slice($thread['all'], array_search(max($thread['my']), $thread['all']) + 1)) $result[$storage -> getUniqueId(max($thread['my']))] = $storage -> getMessage(max($thread['my'])); return array_reverse($result); // for right order }
Source: https://habr.com/ru/post/149748/
All Articles