function gmail_login($emailaddress, $accesstoken, $accesstokensecret) { $to = new GmailOAuth(GOOGLE_API_KEY_PUBLIC, GOOGLE_API_KEY_PRIVATE, $accesstoken, $accesstokensecret); $loginstring = $to->getLoginString($emailaddress); $imapinfo = get_imap_info_for_address($emailaddress); if ($imapinfo == null) { die("Can't find info for {$emailaddress}\n"); } $host = $imapinfo['host']; $mailserver = 'ssl://' . $host; $port = $imapinfo['port']; $protocol = $imapinfo['protocol']; $mailbox = '[Gmail]/All Mail'; $connection = handmadeimap_open_connection($mailserver, $port); if ($connection == null) { die("Connection failed: " . handmadeimap_get_error() . "\n"); } handmadeimap_capability($connection); if (!handmadeimap_was_ok()) { die("CAPABILITY failed: " . handmadeimap_get_error() . "\n"); } handmadeimap_login_xoauth($connection, $loginstring); if (!handmadeimap_was_ok()) { die("LOGIN failed: " . handmadeimap_get_error() . "\n"); } return $connection; }
function handle_gmail_oauth() { if (!isset($_SESSION['emailaddress'])) { if (!empty($_REQUEST['emailaddress'])) { $_SESSION['emailaddress'] = $_REQUEST['emailaddress']; } else { ?> <center> <form method="GET" action="index.php"> Gmail address: <input type="text" size="40" name="emailaddress" value="<?php echo $email; ?> "/> <input type="submit" value="Authorize"/> </form> </center> <?php return; } } $emailaddress = $_SESSION['emailaddress']; $oauthstate = get_gmail_oauth_state(); // If there's no oAuth state stored at all, then we need to initialize one with our request // information, ready to create a request URL. if (!isset($oauthstate)) { pete_log('oauth', "No OAuth state found"); $to = new GmailOAuth(GOOGLE_API_KEY_PUBLIC, GOOGLE_API_KEY_PRIVATE); // This call can be unreliable if the Gmail API servers are under a heavy load, so // retry it with an increasing amount of back-off if there's a problem. $maxretrycount = 1; $retrycount = 0; while ($retrycount < $maxretrycount) { $tok = $to->getRequestToken(); if (isset($tok['oauth_token']) && isset($tok['oauth_token_secret'])) { break; } $retrycount += 1; sleep($retrycount * 5); } $tokenpublic = $tok['oauth_token']; $tokenprivate = $tok['oauth_token_secret']; $state = 'start'; // Create a new set of information, initially just containing the keys we need to make // the request. $oauthstate = array('request_token' => $tokenpublic, 'request_token_secret' => $tokenprivate, 'access_token' => '', 'access_token_secret' => '', 'state' => $state); set_gmail_oauth_state($oauthstate); } // If there's an 'oauth_token' in the URL parameters passed into us, and we don't already // have access tokens stored, this is the user being returned from the authorization page. // Retrieve the access tokens and store them, and set the state to 'done'. if (isset($_REQUEST['oauth_token']) && $oauthstate['access_token'] == '') { error_log('$_REQUEST: ' . print_r($_REQUEST, true)); $urlaccesstoken = $_REQUEST['oauth_token']; pete_log('oauth', "Found access tokens in the URL - {$urlaccesstoken}"); $requesttoken = $oauthstate['request_token']; $requesttokensecret = $oauthstate['request_token_secret']; pete_log('oauth', "Creating API with {$requesttoken}, {$requesttokensecret}"); $to = new GmailOAuth(GOOGLE_API_KEY_PUBLIC, GOOGLE_API_KEY_PRIVATE, $requesttoken, $requesttokensecret); $tok = $to->getAccessToken(); $accesstoken = $tok['oauth_token']; $accesstokensecret = $tok['oauth_token_secret']; pete_log('oauth', "Calculated access tokens {$accesstoken}, {$accesstokensecret}"); $oauthstate['access_token'] = $accesstoken; $oauthstate['access_token_secret'] = $accesstokensecret; $oauthstate['state'] = 'done'; set_gmail_oauth_state($oauthstate); } $state = $oauthstate['state']; if ($state == 'start') { // This is either the first time the user has seen this page, or they've refreshed it before // they've authorized us to access their information. Either way, display a link they can // click that will take them to the authorization page. // In a real application, you'd probably have the page automatically redirect, since the // user has already entered their email address once for us already $tokenpublic = $oauthstate['request_token']; $to = new GmailOAuth(GOOGLE_API_KEY_PUBLIC, GOOGLE_API_KEY_PRIVATE); $requestlink = $to->getAuthorizeURL($tokenpublic, get_current_url()); ?> <center><h1>Click this link to authorize accessing messages from <?php echo htmlspecialchars($emailaddress); ?> </h1></center> <br><br> <center><a href="<?php echo $requestlink; ?> "><?php echo $requestlink; ?> </a></center> <?php } else { // We've been given some access tokens, so try and use them to make an API call, and // display the results. $accesstoken = $oauthstate['access_token']; $accesstokensecret = $oauthstate['access_token_secret']; $connection = gmail_login($emailaddress, $accesstoken, $accesstokensecret); $receivedmailbox = 'Inbox'; $received = fetch_senders_and_recipients($connection, $receivedmailbox, 500); $receivedfrom = $received['from']; $sentmailbox = '[Gmail]/Sent Mail'; $sent = fetch_senders_and_recipients($connection, $sentmailbox, 500); $sentto = $sent['to']; $sentfrom = $sent['from']; $rankedfriends = array(); foreach ($receivedfrom as $address => $receivedinfo) { if (!isset($sentto[$address])) { continue; } if (isset($sentfrom[$address])) { continue; } $sentinfo = $sentto[$address]; $senttocount = $sentinfo['count']; $receivedfromcount = $receivedinfo['count']; $score = min($senttocount, $receivedfromcount); if ($score < 1) { continue; } $display = $sentinfo['display']; $rankedfriends[] = array('address' => $address, 'display' => $display, 'score' => $score); } $sortfunction = create_function('$a, $b', 'if ($a["score"]<$b["score"]) return 1; else return -1;'); usort($rankedfriends, $sortfunction); print '<h3>Your friends ranked by how often you email each other</h3>'; print '<br>'; print "\n"; foreach ($rankedfriends as $friendinfo) { $address = $friendinfo['address']; $display = $friendinfo['display']; $score = $friendinfo['score']; print htmlspecialchars('<' . $address . '> "' . $display . '" - ' . $score); print '<br>'; print "\n"; } } }
function handle_gmail_oauth() { if (!isset($_SESSION['emailaddress'])) { if (!empty($_REQUEST['emailaddress'])) { $_SESSION['emailaddress'] = $_REQUEST['emailaddress']; } else { ?> <center> <form method="GET" action="index.php"> Gmail address: <input type="text" size="40" name="emailaddress" value="<?php echo $email; ?> "/> <input type="submit" value="Authorize"/> </form> </center> <?php return; } } $emailaddress = $_SESSION['emailaddress']; $oauthstate = get_gmail_oauth_state(); // If there's no oAuth state stored at all, then we need to initialize one with our request // information, ready to create a request URL. if (!isset($oauthstate)) { pete_log('oauth', "No OAuth state found"); $to = new GmailOAuth(GOOGLE_API_KEY_PUBLIC, GOOGLE_API_KEY_PRIVATE); // This call can be unreliable if the Gmail API servers are under a heavy load, so // retry it with an increasing amount of back-off if there's a problem. $maxretrycount = 1; $retrycount = 0; while ($retrycount < $maxretrycount) { $tok = $to->getRequestToken(); if (isset($tok['oauth_token']) && isset($tok['oauth_token_secret'])) { break; } $retrycount += 1; sleep($retrycount * 5); } $tokenpublic = $tok['oauth_token']; $tokenprivate = $tok['oauth_token_secret']; $state = 'start'; // Create a new set of information, initially just containing the keys we need to make // the request. $oauthstate = array('request_token' => $tokenpublic, 'request_token_secret' => $tokenprivate, 'access_token' => '', 'access_token_secret' => '', 'state' => $state); set_gmail_oauth_state($oauthstate); } // If there's an 'oauth_token' in the URL parameters passed into us, and we don't already // have access tokens stored, this is the user being returned from the authorization page. // Retrieve the access tokens and store them, and set the state to 'done'. if (isset($_REQUEST['oauth_token']) && $oauthstate['access_token'] == '') { error_log('$_REQUEST: ' . print_r($_REQUEST, true)); $urlaccesstoken = $_REQUEST['oauth_token']; pete_log('oauth', "Found access tokens in the URL - {$urlaccesstoken}"); $requesttoken = $oauthstate['request_token']; $requesttokensecret = $oauthstate['request_token_secret']; pete_log('oauth', "Creating API with {$requesttoken}, {$requesttokensecret}"); $to = new GmailOAuth(GOOGLE_API_KEY_PUBLIC, GOOGLE_API_KEY_PRIVATE, $requesttoken, $requesttokensecret); $tok = $to->getAccessToken(); $accesstoken = $tok['oauth_token']; $accesstokensecret = $tok['oauth_token_secret']; pete_log('oauth', "Calculated access tokens {$accesstoken}, {$accesstokensecret}"); $oauthstate['access_token'] = $accesstoken; $oauthstate['access_token_secret'] = $accesstokensecret; $oauthstate['state'] = 'done'; set_gmail_oauth_state($oauthstate); } $state = $oauthstate['state']; if ($state == 'start') { // This is either the first time the user has seen this page, or they've refreshed it before // they've authorized us to access their information. Either way, display a link they can // click that will take them to the authorization page. // In a real application, you'd probably have the page automatically redirect, since the // user has already entered their email address once for us already $tokenpublic = $oauthstate['request_token']; $to = new GmailOAuth(GOOGLE_API_KEY_PUBLIC, GOOGLE_API_KEY_PRIVATE); $requestlink = $to->getAuthorizeURL($tokenpublic, get_current_url()); ?> <center><h1>Click this link to authorize accessing messages from <?php echo htmlspecialchars($emailaddress); ?> </h1></center> <br><br> <center><a href="<?php echo $requestlink; ?> "><?php echo $requestlink; ?> </a></center> <?php } else { // We've been given some access tokens, so try and use them to make an API call, and // display the results. $accesstoken = $oauthstate['access_token']; $accesstokensecret = $oauthstate['access_token_secret']; $to = new GmailOAuth(GOOGLE_API_KEY_PUBLIC, GOOGLE_API_KEY_PRIVATE, $accesstoken, $accesstokensecret); $loginstring = $to->getLoginString($emailaddress); $imapinfo = get_imap_info_for_address($emailaddress); if ($imapinfo == null) { die("Can't find info for {$emailaddress}\n"); } $host = $imapinfo['host']; $mailserver = 'ssl://' . $host; $port = $imapinfo['port']; $protocol = $imapinfo['protocol']; $connection = handmadeimap_open_connection($mailserver, $port); if ($connection == null) { die("Connection failed: " . handmadeimap_get_error() . "\n"); } handmadeimap_capability($connection); if (!handmadeimap_was_ok()) { die("CAPABILITY failed: " . handmadeimap_get_error() . "\n"); } handmadeimap_login_xoauth($connection, $loginstring); if (!handmadeimap_was_ok()) { die("LOGIN failed: " . handmadeimap_get_error() . "\n"); } $startindex = 1; $endindex = 10; $fetchresult = handmadeimap_fetch_envelopes($connection, $startindex, $endindex); if (!handmadeimap_was_ok()) { die("FETCH failed: " . handmadeimap_get_error() . "\n"); } $messagexml = envelopes_to_xml($fetchresult, 'received'); $output = htmlspecialchars($messagexml); $output = str_replace("\n", "<br>", $output); print $output; } }