Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/googleapi.php
Go to the documentation of this file.
00001 <?php
00028 defined('MOODLE_INTERNAL') || die();
00029 
00031 require_once($CFG->libdir.'/filelib.php');
00032 
00045 abstract class google_auth_request extends curl {
00046     protected $token = '';
00047     private $persistantheaders = array();
00048 
00049     // Must be overridden with the authorization header name
00050     public static function get_auth_header_name() {
00051         throw new coding_exception('get_auth_header_name() method needs to be overridden in each subclass of google_auth_request');
00052     }
00053 
00054     protected function request($url, $options = array()){
00055         if($this->token){
00056             // Adds authorisation head to a request so that it can be authentcated
00057             $this->setHeader('Authorization: '. $this->get_auth_header_name().'"'.$this->token.'"');
00058         }
00059 
00060         foreach($this->persistantheaders as $h){
00061             $this->setHeader($h);
00062         }
00063 
00064         $ret = parent::request($url, $options);
00065         // reset headers for next request
00066         $this->header = array();
00067         return $ret;
00068     }
00069 
00070     protected function multi($requests, $options = array()) {
00071         if($this->token){
00072             // Adds authorisation head to a request so that it can be authentcated
00073             $this->setHeader('Authorization: '. $this->get_auth_header_name().'"'.$this->token.'"');
00074         }
00075 
00076         foreach($this->persistantheaders as $h){
00077             $this->setHeader($h);
00078         }
00079 
00080         $ret = parent::multi($requests, $options);
00081         // reset headers for next request
00082         $this->header = array();
00083         return $ret;
00084     }
00085 
00086     public function get_sessiontoken(){
00087         return $this->token;
00088     }
00089 
00090     public function add_persistant_header($header){
00091         $this->persistantheaders[] = $header;
00092     }
00093 }
00094 
00095 /*******
00096  * The following two classes are usd to implement AuthSub google
00097  * authtentication, as documented here:
00098  * http://code.google.com/apis/accounts/docs/AuthSub.html
00099  *******/
00100 
00110 class google_authsub_request extends google_auth_request {
00111     const AUTHSESSION_URL = 'https://www.google.com/accounts/AuthSubSessionToken';
00112 
00118     public function __construct($authtoken){
00119         parent::__construct();
00120         $this->token = $authtoken;
00121     }
00122 
00128     public function get_session_token(){
00129         $content = $this->get(google_authsub_request::AUTHSESSION_URL);
00130 
00131         if( preg_match('/token=(.*)/i', $content, $matches) ){
00132             return $matches[1];
00133         }else{
00134             throw new moodle_exception('could not upgrade google authtoken to session token');
00135         }
00136     }
00137 
00138     public static function get_auth_header_name(){
00139         return 'AuthSub token=';
00140     }
00141 }
00142 
00151 class google_authsub extends google_auth_request {
00152     const LOGINAUTH_URL    = 'https://www.google.com/accounts/AuthSubRequest';
00153     const VERIFY_TOKEN_URL = 'https://www.google.com/accounts/AuthSubTokenInfo';
00154     const REVOKE_TOKEN_URL = 'https://www.google.com/accounts/AuthSubRevokeToken';
00155 
00165     public function __construct($sessiontoken = '', $authtoken = '', $options = array()){
00166         parent::__construct($options);
00167 
00168         if( $authtoken ){
00169             $gauth = new google_authsub_request($authtoken);
00170             $sessiontoken = $gauth->get_session_token();
00171         }
00172 
00173         $this->token = $sessiontoken;
00174         if(! $this->valid_token() ){
00175             throw new moodle_exception('Invalid subauth token');
00176         }
00177     }
00178 
00184     public function valid_token(){
00185         $this->get(google_authsub::VERIFY_TOKEN_URL);
00186 
00187         if($this->info['http_code'] === 200){
00188             return true;
00189         }else{
00190             return false;
00191         }
00192     }
00193 
00199     public function revoke_session_token(){
00200         $this->get(google_authsub::REVOKE_TOKEN_URL);
00201 
00202         if($this->info['http_code'] === 200){
00203             $this->token = '';
00204             return true;
00205         }else{
00206             return false;
00207         }
00208     }
00209 
00217     public static function login_url($returnaddr, $realm){
00218         $uri = google_authsub::LOGINAUTH_URL.'?next='
00219             .urlencode($returnaddr)
00220             .'&scope='
00221             .urlencode($realm)
00222             .'&session=1&secure=0';
00223 
00224         return $uri;
00225     }
00226 
00227     public static function get_auth_header_name(){
00228         return 'AuthSub token=';
00229     }
00230 }
00231 
00242 class google_docs {
00243     // need both docs and the spreadsheets realm
00244     const REALM            = 'https://docs.google.com/feeds/ https://spreadsheets.google.com/feeds/ https://docs.googleusercontent.com/';
00245     const DOCUMENTFEED_URL = 'https://docs.google.com/feeds/default/private/full';
00246     const USER_PREF_NAME   = 'google_authsub_sesskey';
00247 
00248     private $google_curl = null;
00249 
00255     public function __construct($google_curl){
00256         if(is_a($google_curl, 'google_auth_request')){
00257             $this->google_curl = $google_curl;
00258             $this->google_curl->add_persistant_header('GData-Version: 3.0');
00259         }else{
00260             throw new moodle_exception('Google Curl Request object not given');
00261         }
00262     }
00263 
00264     public static function get_sesskey($userid){
00265         return get_user_preferences(google_docs::USER_PREF_NAME, false, $userid);
00266     }
00267 
00268     public static function set_sesskey($value, $userid){
00269         return set_user_preference(google_docs::USER_PREF_NAME, $value, $userid);
00270     }
00271 
00272     public static function delete_sesskey($userid){
00273         return unset_user_preference(google_docs::USER_PREF_NAME, $userid);
00274     }
00275 
00282     #FIXME
00283     public function get_file_list($search = ''){
00284         global $CFG, $OUTPUT;
00285         $url = google_docs::DOCUMENTFEED_URL;
00286 
00287         if($search){
00288             $url.='?q='.urlencode($search);
00289         }
00290         $content = $this->google_curl->get($url);
00291 
00292         $xml = new SimpleXMLElement($content);
00293 
00294 
00295         $files = array();
00296         foreach($xml->entry as $gdoc){
00297             $docid  = (string) $gdoc->children('http://schemas.google.com/g/2005')->resourceId;
00298             list($type, $docid) = explode(':', $docid);
00299 
00300             $title  = '';
00301             $source = '';
00302             // FIXME: We're making hard-coded choices about format here.
00303             // If the repo api can support it, we could let the user
00304             // chose.
00305             switch($type){
00306                 case 'document':
00307                     $title = $gdoc->title.'.rtf';
00308                     $source = 'https://docs.google.com/feeds/download/documents/Export?id='.$docid.'&exportFormat=rtf';
00309                     break;
00310                 case 'presentation':
00311                     $title = $gdoc->title.'.ppt';
00312                     $source = 'https://docs.google.com/feeds/download/presentations/Export?id='.$docid.'&exportFormat=ppt';
00313                     break;
00314                 case 'spreadsheet':
00315                     $title = $gdoc->title.'.xls';
00316                     $source = 'https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key='.$docid.'&exportFormat=xls';
00317                     break;
00318                 case 'pdf':
00319                     $title  = (string)$gdoc->title;
00320                     $source = (string)$gdoc->content[0]->attributes()->src;
00321                     break;
00322             }
00323 
00324             if(!empty($source)){
00325                 $files[] =  array( 'title' => $title,
00326                     'url' => "{$gdoc->link[0]->attributes()->href}",
00327                     'source' => $source,
00328                     'date'   => usertime(strtotime($gdoc->updated)),
00329                     'thumbnail' => (string) $OUTPUT->pix_url(file_extension_icon($title, 32))
00330                 );
00331             }
00332         }
00333 
00334         return $files;
00335     }
00336 
00343     public function send_file($file){
00344         $this->google_curl->setHeader("Content-Length: ". $file->get_filesize());
00345         $this->google_curl->setHeader("Content-Type: ". $file->get_mimetype());
00346         $this->google_curl->setHeader("Slug: ". $file->get_filename());
00347 
00348         $this->google_curl->post(google_docs::DOCUMENTFEED_URL, $file->get_content());
00349 
00350         if($this->google_curl->info['http_code'] === 201){
00351             return true;
00352         }else{
00353             return false;
00354         }
00355     }
00356 
00357     public function download_file($url, $fp){
00358         return $this->google_curl->download(array( array('url'=>$url, 'file' => $fp) ));
00359     }
00360 }
00361 
00372 class google_picasa {
00373     const REALM             = 'http://picasaweb.google.com/data/';
00374     const USER_PREF_NAME    = 'google_authsub_sesskey_picasa';
00375     const UPLOAD_LOCATION   = 'https://picasaweb.google.com/data/feed/api/user/default/albumid/default';
00376     const ALBUM_PHOTO_LIST  = 'https://picasaweb.google.com/data/feed/api/user/default/albumid/';
00377     const PHOTO_SEARCH_URL  = 'https://picasaweb.google.com/data/feed/api/user/default?kind=photo&q=';
00378     const LIST_ALBUMS_URL   = 'https://picasaweb.google.com/data/feed/api/user/default';
00379 
00380     private $google_curl = null;
00381 
00387     public function __construct($google_curl){
00388         if(is_a($google_curl, 'google_auth_request')){
00389             $this->google_curl = $google_curl;
00390             $this->google_curl->add_persistant_header('GData-Version: 2');
00391         }else{
00392             throw new moodle_exception('Google Curl Request object not given');
00393         }
00394     }
00395 
00396     public static function get_sesskey($userid){
00397         return get_user_preferences(google_picasa::USER_PREF_NAME, false, $userid);
00398     }
00399 
00400     public static function set_sesskey($value, $userid){
00401         return set_user_preference(google_picasa::USER_PREF_NAME, $value, $userid);
00402     }
00403 
00404     public static function delete_sesskey($userid){
00405         return unset_user_preference(google_picasa::USER_PREF_NAME, $userid);
00406     }
00407 
00414     public function send_file($file){
00415         $this->google_curl->setHeader("Content-Length: ". $file->get_filesize());
00416         $this->google_curl->setHeader("Content-Type: ". $file->get_mimetype());
00417         $this->google_curl->setHeader("Slug: ". $file->get_filename());
00418 
00419         $this->google_curl->post(google_picasa::UPLOAD_LOCATION, $file->get_content());
00420 
00421         if($this->google_curl->info['http_code'] === 201){
00422             return true;
00423         }else{
00424             return false;
00425         }
00426     }
00427 
00436     public function get_file_list($path = ''){
00437         if(!$path){
00438             return $this->get_albums();
00439         }else{
00440             return $this->get_album_photos($path);
00441         }
00442     }
00443 
00450     public function get_album_photos($albumid){
00451         $albumcontent = $this->google_curl->get(google_picasa::ALBUM_PHOTO_LIST.$albumid);
00452 
00453         return $this->get_photo_details($albumcontent);
00454     }
00455 
00463     public function do_photo_search($query){
00464         $content = $this->google_curl->get(google_picasa::PHOTO_SEARCH_URL.htmlentities($query));
00465 
00466         return $this->get_photo_details($content);
00467     }
00468 
00475     public function get_albums(){
00476         $content = $this->google_curl->get(google_picasa::LIST_ALBUMS_URL);
00477         $xml = new SimpleXMLElement($content);
00478 
00479         $files = array();
00480 
00481         foreach($xml->entry as $album){
00482             $gphoto = $album->children('http://schemas.google.com/photos/2007');
00483 
00484             $mediainfo = $album->children('http://search.yahoo.com/mrss/');
00485             //hacky...
00486             $thumbnailinfo = $mediainfo->group->thumbnail[0]->attributes();
00487 
00488             $files[] = array( 'title' => (string) $gphoto->name,
00489                 'date'  => userdate($gphoto->timestamp),
00490                 'size'  => (int) $gphoto->bytesUsed,
00491                 'path'  => (string) $gphoto->id,
00492                 'thumbnail' => (string) $thumbnailinfo['url'],
00493                 'thumbnail_width' => 160,  // 160 is the native maximum dimension
00494                 'thumbnail_height' => 160,
00495                 'children' => array(),
00496             );
00497 
00498         }
00499 
00500         return $files;
00501     }
00502 
00510     public function get_photo_details($rawxml){
00511 
00512         $xml = new SimpleXMLElement($rawxml);
00513 
00514         $files = array();
00515 
00516         foreach($xml->entry as $photo){
00517             $gphoto = $photo->children('http://schemas.google.com/photos/2007');
00518 
00519             $mediainfo = $photo->children('http://search.yahoo.com/mrss/');
00520             $fullinfo = $mediainfo->group->content->attributes();
00521             //hacky...
00522             $thumbnailinfo = $mediainfo->group->thumbnail[0]->attributes();
00523 
00524             // Derive the nicest file name we can
00525             if (!empty($mediainfo->group->description)) {
00526                 $title = shorten_text((string)$mediainfo->group->description, 20, false, '');
00527                 $title = clean_filename($title).'.jpg';
00528             } else {
00529                 $title = (string)$mediainfo->group->title;
00530             }
00531 
00532             $files[] = array(
00533                 'title' => $title,
00534                 'date'  => userdate($gphoto->timestamp),
00535                 'size' => (int) $gphoto->size,
00536                 'path' => $gphoto->albumid.'/'.$gphoto->id,
00537                 'thumbnail' => (string) $thumbnailinfo['url'],
00538                 'thumbnail_width' => 72,  // 72 is the native maximum dimension
00539                 'thumbnail_height' => 72,
00540                 'source' => (string) $fullinfo['url'],
00541                 'url' => (string) $fullinfo['url']
00542             );
00543         }
00544 
00545         return $files;
00546     }
00547 
00548 }
00549 
00563 class google_authclient extends google_auth_request {
00564     const LOGIN_URL = 'https://www.google.com/accounts/ClientLogin';
00565 
00566     public function __construct($sessiontoken = '', $username = '', $password = '', $options = array() ){
00567         parent::__construct($options);
00568 
00569         if($username and $password){
00570             $param =  array(
00571                 'accountType'=>'GOOGLE',
00572                 'Email'=>$username,
00573                 'Passwd'=>$password,
00574                 'service'=>'writely'
00575             );
00576 
00577             $content = $this->post(google_authclient::LOGIN_URL, $param);
00578 
00579             if( preg_match('/auth=(.*)/i', $content, $matches) ){
00580                 $sessiontoken = $matches[1];
00581             }else{
00582                 throw new moodle_exception('could not upgrade authtoken');
00583             }
00584 
00585         }
00586 
00587         if($sessiontoken){
00588             $this->token = $sessiontoken;
00589         }else{
00590             throw new moodle_exception('no session token specified');
00591         }
00592     }
00593 
00594     public static function get_auth_header_name(){
00595         return 'GoogleLogin auth=';
00596     }
00597 }
 All Data Structures Namespaces Files Functions Variables Enumerations