<commit_before><?php
/**
 * Google Docs Plugin
 *
 * @author <NAME> <<EMAIL>>
 * @version $Id$
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 */

require_once($CFG->libdir.'/googleapi.php');

class repository_googledocs extends repository {
    private $subauthtoken = '';

    public function __construct($repositoryid, $context = SITEID, $options = array()) {
        global $USER;
        parent::__construct($repositoryid, $context, $options);

        // TODO: I wish there was somewhere we could explicitly put this outside of constructor..
        $googletoken = optional_param('token', false, PARAM_RAW);
        if($googletoken){
            $gauth = new google_authsub(false, $googletoken); // will throw exception if fails
            google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id);
        }

        # fixme - we are not checking login before all functions in the repo api.. eg search
        # MDL-17474
        $this->check_login();
    }

    public function check_login() {
        global $USER;

        $sesskey = google_docs::get_sesskey($USER->id);

        if($sesskey){
            try{
                $gauth = new google_authsub($sesskey);
                $this->subauthtoken = $sesskey;
                return true;
            }catch(Exception $e){
                // sesskey is not valid, delete store and re-auth
                google_docs::delete_sesskey($USER->id);
            }
        }

        return false;
    }

    public function print_login($ajax = true){ 
        global $CFG;
        if($ajax){
            $ret = array(); 
            $popup_btn = new stdclass; 
            $popup_btn->type = 'popup'; 
            $returnurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&repo_id='.$this->id;
            $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM);
            $ret['login'] = array($popup_btn); 
            return $ret; 
        }
    }

    public function get_listing($path='', $page = '') {
        $gdocs = new google_docs(new google_authsub($this->subauthtoken));

        $ret = array();
        $ret['dynload'] = true;
        $ret['list'] = $gdocs->get_file_list();
        return $ret;
    }

    public function search($query){
        $gdocs = new google_docs(new google_authsub($this->subauthtoken));

        $ret = array();
        $ret['dynload'] = true;
        $ret['list'] = $gdocs->get_file_list($query);
        return $ret;
    }

    public function logout(){
        global $USER;

        $token = google_docs::get_sesskey($USER->id);

        $gauth = new google_authsub($token);
        // revoke token from google
        $gauth->revoke_session_token();

        google_docs::delete_sesskey($USER->id);
        $this->subauthtoken = '';

        return parent::logout();
    }

    public function get_name(){
        return get_string('repositoryname', 'repository_googledocs');
    }

    public function get_file($url, $file) {
        global $CFG;


        //FIXME: Why does every repo plugin.. do this mktemp file itself..

        if (!file_exists($CFG->dataroot.'/repository/download')) {
            mkdir($CFG->dataroot.'/repository/download/', 0777, true);
        }

        if(is_dir($CFG->dataroot.'/repository/download')) {
            $dir = $CFG->dataroot.'/repository/download/';
        }

        if (empty($file)){
            $file = time();
        }

        $fp = fopen($dir.$file, 'w');
        $gdocs = new google_docs(new google_authsub($this->subauthtoken));
        $gdocs->download_file($url, $fp);

        return $dir.$file;
     }


}
//Icon from: http://www.iconspedia.com/icon/google-2706.html
<commit_msg>"REPOSITORY, GDOCS/MDL-16383, tell filepicker gdocs support documents only"<commit_after><?php
/**
 * Google Docs Plugin
 *
 * @author <NAME> <<EMAIL>>
 * @version $Id$
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 */

require_once($CFG->libdir.'/googleapi.php');

class repository_googledocs extends repository {
    private $subauthtoken = '';

    public function __construct($repositoryid, $context = SITEID, $options = array()) {
        global $USER;
        parent::__construct($repositoryid, $context, $options);

        // TODO: I wish there was somewhere we could explicitly put this outside of constructor..
        $googletoken = optional_param('token', false, PARAM_RAW);
        if($googletoken){
            $gauth = new google_authsub(false, $googletoken); // will throw exception if fails
            google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id);
        }

        # fixme - we are not checking login before all functions in the repo api.. eg search
        # MDL-17474
        $this->check_login();
    }

    public function check_login() {
        global $USER;

        $sesskey = google_docs::get_sesskey($USER->id);

        if($sesskey){
            try{
                $gauth = new google_authsub($sesskey);
                $this->subauthtoken = $sesskey;
                return true;
            }catch(Exception $e){
                // sesskey is not valid, delete store and re-auth
                google_docs::delete_sesskey($USER->id);
            }
        }

        return false;
    }

    public function print_login($ajax = true){ 
        global $CFG;
        if($ajax){
            $ret = array(); 
            $popup_btn = new stdclass; 
            $popup_btn->type = 'popup'; 
            $returnurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&repo_id='.$this->id;
            $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM);
            $ret['login'] = array($popup_btn); 
            return $ret; 
        }
    }

    public function get_listing($path='', $page = '') {
        $gdocs = new google_docs(new google_authsub($this->subauthtoken));

        $ret = array();
        $ret['dynload'] = true;
        $ret['list'] = $gdocs->get_file_list();
        return $ret;
    }

    public function search($query){
        $gdocs = new google_docs(new google_authsub($this->subauthtoken));

        $ret = array();
        $ret['dynload'] = true;
        $ret['list'] = $gdocs->get_file_list($query);
        return $ret;
    }

    public function logout(){
        global $USER;

        $token = google_docs::get_sesskey($USER->id);

        $gauth = new google_authsub($token);
        // revoke token from google
        $gauth->revoke_session_token();

        google_docs::delete_sesskey($USER->id);
        $this->subauthtoken = '';

        return parent::logout();
    }

    public function get_name(){
        return get_string('repositoryname', 'repository_googledocs');
    }

    public function get_file($url, $file) {
        global $CFG;


        //FIXME: Why does every repo plugin.. do this mktemp file itself..

        if (!file_exists($CFG->dataroot.'/repository/download')) {
            mkdir($CFG->dataroot.'/repository/download/', 0777, true);
        }

        if(is_dir($CFG->dataroot.'/repository/download')) {
            $dir = $CFG->dataroot.'/repository/download/';
        }

        if (empty($file)){
            $file = time();
        }

        $fp = fopen($dir.$file, 'w');
        $gdocs = new google_docs(new google_authsub($this->subauthtoken));
        $gdocs->download_file($url, $fp);

        return $dir.$file;
     }

     public function supported_filetypes() {
        return array('document');
     }
}
//Icon from: http://www.iconspedia.com/icon/google-2706.html

{'fraction_non_alphanumeric': 0.14536041417761847, 'fraction_numerical': 0.00411522633744856, 'mean_word_length': 2.239036973344798, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1549473', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2234, 'n_words': 474}