From: rajbot Date: Thu, 7 Oct 2010 22:43:30 +0000 (+0000) Subject: add OLAuth class. X-Git-Url: http://git.rot13.org/?a=commitdiff_plain;h=1a4a5423656729d7629f1b1d48f70984d1105bf3;p=bookreader.git add OLAuth class. --- diff --git a/BookReaderIA/datanode/BookReaderJSIA.php b/BookReaderIA/datanode/BookReaderJSIA.php index e20c44e..8ee834f 100644 --- a/BookReaderIA/datanode/BookReaderJSIA.php +++ b/BookReaderIA/datanode/BookReaderJSIA.php @@ -18,6 +18,8 @@ This file is part of BookReader. along with BookReader. If not, see . */ +header('Content-Type: application/javascript'); + $id = $_REQUEST['id']; $itemPath = $_REQUEST['itemPath']; $subPrefix = $_REQUEST['subPrefix']; @@ -402,10 +404,16 @@ br.archiveFormat = ''; # Load some values from meta.xml if ('' != $metaData->{'page-progression'}) { - echo "br.pageProgression = '" . $metaData->{"page-progression"} . "';"; + echo "br.pageProgression = '" . $metaData->{"page-progression"} . "';\n"; } else { // Assume page progression is Left To Right - echo "br.pageProgression = 'lr';"; + echo "br.pageProgression = 'lr';\n"; +} + +if ('browserlending' == $metaData->{'collection'}) { + echo "br.olAuth = true;\n"; +} else { + echo "br.olAuth = false; //" . $metaData->{'collection'} ."\n"; } # Special cases @@ -443,9 +451,89 @@ if (typeof(brConfig) != 'undefined') { } } // brConfig -br.cleanupMetadata(); -br.init(); +function OLAuth() { + this.authUrl = 'http://home.us.archive.org/~rkumar/olauth.php?id=' + br.bookId; + return this; +} + +OLAuth.prototype.init = function() { + var htmlStr = '

Authenticating in-browser loan with openlibrary.org!

'; + htmlStr += '

Please wait...

'; + + this.showPopup("#ddd", "#000", htmlStr); + $.ajax({url:this.authUrl, dataType:'jsonp', jsonpCallback:'initCallback'}); +} + +OLAuth.prototype.showPopup = function(bgColor, textColor, msg) { + this.popup = document.createElement("div"); + $(this.popup).css({ + position: 'absolute', + top: '20px', + left: ($('#BookReader').attr('clientWidth')-400)/2 + 'px', + width: '400px', + padding: "20px", + border: "3px double #999999", + zIndex: 3, + backgroundColor: bgColor, + color: textColor + }).appendTo('#BookReader'); + + this.popup.innerHTML = msg; + +} + +OLAuth.prototype.initCallback = function(obj) { + if (false == obj.success) { + $(this.popup).css({ + backgroundColor: "#f00", + color: "#fff" + }); + + this.popup.innerHTML = obj.msg; + return; + } + + //user is authenticated + this.setCookie(obj.token); + this.startPolling(); + br.init(); +} + +OLAuth.prototype.callback = function(obj) { + if (false == obj.success) { + this.showPopup("#f00", "#fff", obj.msg); + clearInterval(this.poller); + this.ttsPoller = null; + } else { + this.setCookie(obj.token); + } +} + +OLAuth.prototype.setCookie = function(value) { + var date = new Date(); + date.setTime(date.getTime()+(24*60*60*1000)); //one day expiry + var expiry = date.toGMTString(); + var cookie = 'loan-'+br.bookId+'='+value; + cookie += '; expires='+expiry; + cookie += '; path=/; domain=.archive.org;'; + document.cookie = cookie; +} + +OLAuth.prototype.startPolling = function () { + var self = this; + this.poller=setInterval(function(){ + $.ajax({url:self.authUrl, dataType:'jsonp', jsonpCallback:'callback'}); + },300000); +} + +br.cleanupMetadata(); +if (br.olAuth) { + var olAuth = new OLAuth(); + olAuth.init(); +} else { + br.init(); +} +