<?    /**
     *    Authenticate from Google so that we can grab the docs feed
     *    on the user's behalf...
     */
     
$domain     $_SERVER['HTTP_HOST'];
     
$returnUrl "http://{$domain}/google-docs-feed/";
     if ( 
$authToken $_COOKIE['gdat'] ) {
         
        
/**
         *     All good..
          */
        
     
} else if ( $preAuthToken $_GET['token'] ) { 
         
        
$msg "Fetching perma-token with pre-auth token : $preAuthToken";
        
// message('green',$msg);
        /**
         *    Use CURL to fetch the feed..
         */
        
$hCurl curl_init('https://www.google.com/accounts/AuthSubSessionToken');
        
/**
         *    Set up our request
         */
        
curl_setopt$hCurlCURLOPT_HTTPHEADER, array(
            
'Authorization: AuthSub token="' $preAuthToken '"'
        
));
        
curl_setopt$hCurlCURLOPT_RETURNTRANSFERtrue ); // Return the result..
        /**
         *    Go make our request..
         */
        
$sResult curl_exec$hCurl );
        if ( 
$sResult ) {
            if ( 
preg_match("/Token=([\w\-]+)/"$sResult$mx ) ) {
                
$authToken $mx[1];
                
setcookie 'gdat'$authTokentime()+60*60*24*365'/' );
                
$showSuccessPage true;
                
            } else {
                print 
"RESULT : $sResult";
                print 
message('red''Error - Please consult <a href="mailto:opensource@icommunicate.co.uk&Subject=Google Docs Token Parse Error">support</a>');
            }
        } else {
            print 
message('red''Authentication Error - Please consult <a href="mailto:opensource@icommunicate.co.uk&Subject=Google Docs Auth Error">support</a>');
        }
        
// ~~ Just authenticated...  drop cookie for future use..
         // 
        
     
}
     
     
/**
      *        If we've got an authToken fetch the feed..
      */
     
     
if ( $authToken && !$showSuccessPage && !$_GET['show-page'] && !$_GET['token'] ) {
         
        
// ~~  Setup the feed fetch..
        
$hCurl curl_init('http://docs.google.com/feeds/documents/private/full');
        
// ~~  Authentication Header...
        
curl_setopt$hCurlCURLOPT_HTTPHEADER, array(
            
'Authorization: AuthSub token="' $authToken '"'
        
));
        
// ~~ Want the content back?
        
curl_setopt$hCurlCURLOPT_RETURNTRANSFERtrue ); // Return the result..
        // ~~ execute..
        
$sResult curl_exec$hCurl );
        if ( 
$sResult ) {
            
header("Expires: 0");
            
header("Content-Type: text/xml");
            print 
$sResult;
            exit;
        } else {
            print 
message('red''Feed Error - Please consult <a href="mailto:opensource@icommunicate.co.uk&Subject=Feed Error">support</a>');
        }
     } 
     
     if ( 
$_GET['show-page'] || $showSuccessPage || $_GET['token'] ) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>GoogleAuth</title>
    <link rel="stylesheet" href="/r/css/base.css">
    <style>
        body {
            font-family : Trebuchet MS, Tahoma, Verdana, Arial;
            font-size    : 12px;
            color        : #444444;
        }
        a, 
        a:visited {
            color : #bf0000;
        }
        .auth-link {
            font-size    : 14px;
            font-weight : bold;
        }
    </style>
</head>

<body>

<? if ( $showSuccessPage ) { ?>

    <?=message('green''<strong>Authentication Success</strong>'); ?>

    <h1>Congratulations</h1>
    
    <p>Great - you're now authenticated.   The feed in your button updates every <u>15 minutes</u> or so...</p>
    
    <p>... in the meantime <a href="<?=$returnUrl?>">click here for your list of documents</a>.</p>
    
<? ?>


<? if ( !$authFailed && !$authToken ) { ?>
    
    <h1>Authentication Required</h1>

    <p>To obtain a feed of documents you will need to grant access for us to fetch it on your behalf.</p>
    
    <p>Only do this if you <u>trust us</u> to have access to your document feed.</p>

        <div align="center">
    <a class="auth-link"  href="https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fdocs.google.com%2Ffeeds%2Fdocuments&session=1&secure=0&next=<?=urlencode($returnUrl)?>">
        Authenticate with Google &#187;
    </a>
    </div>
<? ?>



    <h2>What's happening?</h2>
    
    <p>If you choose to auth we'll send you off to Google where you'll sign into your account and be asked whether you trust us.</p>
    
    <p>If you agree that you do Google will send you back here to this page.  In the address bar will be a little token which we then store in your browser as a cookie.</p>

    <p>Whenever your Google Toolbar wants to build the dropdown list of documents it will come here to this page.  We will ask your browse for the token stored in our cookie.  We then ask Google Docs for the list of documents - giving the token to Google Docs as proof that you trust us to have the list.</p>
    
    <div align="center">
<a class="auth-link"  href="https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fdocs.google.com%2Ffeeds%2Fdocuments&session=1&secure=0&next=<?=urlencode($returnUrl)?>">
    <? if ( $authToken ) { ?>Re-<? ?>Authenticate with Google &#187;
</a>
    </div>

    <h2>Who are we and should we trust you.</h2>
    
    <p>We are <a href="http://www.icommunicate.co.uk/">Isotope Communications Ltd</a>, a digital agency in Bath, UK.</p>
    
    <p>Whether you trust us with access to you really!  For the techies amongst you here's exactly what happens with your authentication 'tokens'.  You follow the link above and Google sends you back to us with a token which can be used <u>only once</u>.  This token is visible in your browser address bar - and as a consequence will end up in our access logs on the server.  However, it's a single-use token.  We use it to grab a more permenant token (so you don't have to auth every five minutes) and we store this second 'session' token in your browser.  We <u>do not store</u> your session token anywhere within our own infrastructure.  When your toolbar requests the feed we pick up the token, use it to grab the feed and again, <u>do not store it</u>.</p>

    <h2>Want to know more or how we do this?</h2>
    
    <p>Have a look at the <a href="http://code.google.com/apis/documents/developers_guide_protocol.html#Auth">Google Docs API</a></p>
    
    <h2>Want to get in touch?</h2>
    
    <p>Please do: <a href="opensource@icommunicate.co.uk">opensource@icommunicate.co.uk</a></p>

</body>
</html>


<?     } else { 
    
    
header("Expires: 0");
    
header("Content-Type: text/xml");
    print 
'<?xml version="1.0" encoding="utf-8"?>';
    print 
'<?xml-stylesheet href="/resources/css/rss.css" type="text/css"?>';
    
    
?>
<rss version="2.0">
  <channel>
    <title>Google Docs RSS Feed</title>
    <description>Google Docs RSS Feed :: Authentication Required </description>
    <generator>Spirit Application Suite :: Version 3.91</generator>
    <generator-link>www.spiritsuite.com</generator-link>
    <help>
        <![CDATA[
            This is an RSS formatted XML site feed.  It is intended to be used by the Google Toolbar Calendar Button.  Perhaps visit toolbar.google.com to find out 
more.
        ]]>
    </help>
    <link>http://fiji.mediaisotope.com/google-docs-feed/?show-page=true</link>
    <language>en</language>
    <copyright>2006</copyright>
        <item>
            <title><![CDATA[Click here to authenticate...]]></title>
            <description><![CDATA[Click here to authenticate...]]></description>
            <pubDate>Fri, 07 Jul 2006 12:00:00 +0100</pubDate>
            <guid permalink="true">http://fiji.mediaisotope.com/google-docs-feed/?show-page=true</guid>
            <link>http://fiji.mediaisotope.com/google-docs-feed/?show-page=true</link>
        </item>
    </channel>
</rss>

        <?
        
    
}
    exit;
    
    
    
/** Message Template **/
    
    
function message$color$sMessage ){ ?>
<div class="ccbox-outer ccbox-outer-<?=$color?>" style="display:<?=$hidden?'none':''?>;" id="msg_show_<?=$msgid?>">
        <div class="ccbox ccb-<?=$color?>">
            <em id="ctl"><b>&bull;</b></em><em id="cbl"><b>&bull;</b></em><em id="ctr"><b>&bull;</b></em><em id="cbr"><b>&bull;</b></em>
            <p><?=$sMessage?></p>
        </div>
    </div>
<? ?>