示例#1
0
function fetch_senders_and_recipients($connection, $mailbox, $count)
{
    $selectresult = handmadeimap_select($connection, $mailbox);
    if (!handmadeimap_was_ok()) {
        die("SELECT failed: " . handmadeimap_get_error() . "\n");
    }
    $totalcount = $selectresult['totalcount'];
    $startindex = $totalcount - $count;
    $endindex = $totalcount;
    $fetchresult = handmadeimap_fetch_envelopes($connection, $startindex, $endindex);
    if (!handmadeimap_was_ok()) {
        die("FETCH failed: " . handmadeimap_get_error() . "\n");
    }
    $addresslist = array('from' => array(), 'to' => array(), 'cc' => array(), 'bcc' => array());
    $addresstodisplay = array();
    foreach ($fetchresult as $envelope) {
        $from = $envelope['from'];
        $fromcomponents = $from[0];
        $fromaddress = $fromcomponents['address'];
        $fromdisplay = $fromcomponents['display'];
        $addresstodisplay[$fromaddress] = $fromdisplay;
        $addresslist['from'][] = $fromaddress;
        foreach ($envelope['to'] as $tocomponents) {
            $toaddress = $tocomponents['address'];
            $todisplay = $tocomponents['display'];
            $addresstodisplay[$toaddress] = $todisplay;
            $addresslist['to'][] = $toaddress;
        }
        foreach ($envelope['cc'] as $cccomponents) {
            $ccaddress = $cccomponents['address'];
            $ccdisplay = $cccomponents['display'];
            $addresstodisplay[$ccaddress] = $ccdisplay;
            $addresslist['cc'][] = $ccaddress;
        }
        foreach ($envelope['bcc'] as $bcccomponents) {
            $bccaddress = $bcccomponents['address'];
            $bccdisplay = $bcccomponents['display'];
            $addresstodisplay[$bccaddress] = $bccdisplay;
            $addresslist['bcc'][] = $bccaddress;
        }
    }
    $addresscounts = array('from' => array_count_values($addresslist['from']), 'to' => array_count_values($addresslist['to']), 'cc' => array_count_values($addresslist['cc']), 'bcc' => array_count_values($addresslist['bcc']));
    $result = array();
    foreach ($addresscounts as $role => $countmap) {
        $result[$role] = array();
        foreach ($countmap as $address => $count) {
            $result[$role][$address] = array('count' => $count, 'display' => $addresstodisplay[$address]);
        }
    }
    return $result;
}
     $listresult = handmadeimap_list($connection);
     if (!handmadeimap_was_ok()) {
         die("LIST failed: " . handmadeimap_get_error() . "\n");
     }
     exit;
 }
 $selectresult = handmadeimap_select($connection, $mailbox);
 if (!handmadeimap_was_ok()) {
     die("SELECT failed: " . handmadeimap_get_error() . "\n");
 }
 if ($action == 'totalcount') {
     $totalcount = $selectresult['totalcount'];
     echo $totalcount;
 } else {
     if ($action == 'fetchheaders') {
         $fetchresult = handmadeimap_fetch_envelopes($connection, $startindex, $endindex);
         if (!handmadeimap_was_ok()) {
             die("FETCH failed: " . handmadeimap_get_error() . "\n");
         }
         $messagexml = envelopes_to_xml($fetchresult, 'received');
         print '<?xml version="1.0" encoding="ISO-8859-1"?><messagelist>' . "\n";
         //'
         print $messagexml;
         print "</messagelist>\n";
     } else {
         if ($action == 'fetchsince') {
             $searchresult = handmadeimap_search_since_date($connection, $date);
             if (!handmadeimap_was_ok()) {
                 die("SEARCH failed: " . handmadeimap_get_error() . "\n");
             }
             print_r($searchresult);
示例#3
0
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;
    }
}