Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 13... Línea 13...
13
//
13
//
14
// You should have received a copy of the GNU General Public License
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea 16...
16
 
16
 
17
/**
-
 
18
 * Microsoft Live Skydrive Repository Plugin
-
 
19
 *
-
 
20
 * @package    repository_onedrive
-
 
21
 * @copyright  2012 Lancaster University Network Services Ltd
-
 
22
 * @author     Dan Poltawski <dan.poltawski@luns.net.uk>
-
 
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
24
 */
-
 
25
 
-
 
26
defined('MOODLE_INTERNAL') || die();
-
 
27
 
-
 
28
/**
17
/**
29
 * Microsoft onedrive repository plugin.
18
 * Microsoft onedrive repository plugin.
30
 *
19
 *
31
 * @package    repository_onedrive
20
 * @package    repository_onedrive
32
 * @copyright  2012 Lancaster University Network Services Ltd
21
 * @copyright  2012 Lancaster University Network Services Ltd
Línea 451... Línea 440...
451
            $client = $this->get_user_oauth_client();
440
            $client = $this->get_user_oauth_client();
452
        }
441
        }
Línea 453... Línea 442...
453
 
442
 
Línea -... Línea 443...
-
 
443
        $base = 'https://graph.microsoft.com/v1.0/';
454
        $base = 'https://graph.microsoft.com/v1.0/';
444
 
455
 
445
        // Fetch the item info.
-
 
446
        $infourl = (new moodle_url($base . 'me/drive/items/' . $sourceinfo->id))->out(false);
-
 
447
        $response = $client->get($infourl);
-
 
448
        if (!$response) {
-
 
449
            throw new repository_exception('cannotdownload', 'repository');
-
 
450
        }
Línea 456... Línea 451...
456
        $sourceurl = new moodle_url($base . 'me/drive/items/' . $sourceinfo->id . '/content');
451
        $response = json_decode($response, true);
457
        $source = $sourceurl->out(false);
452
        $downloadurl = $response['@microsoft.graph.downloadUrl'];
458
 
453
 
-
 
454
        // We use download_one and not the rest API because it has special timeouts etc.
-
 
455
        $path = $this->prepare_file($filename);
459
        // We use download_one and not the rest API because it has special timeouts etc.
456
        $options = ['filepath' => $path, 'timeout' => 15, 'followlocation' => true, 'maxredirs' => 5];
Línea 460... Línea 457...
460
        $path = $this->prepare_file($filename);
457
        // We cannot send authorization headers in the direct download request, it will fail.
461
        $options = ['filepath' => $path, 'timeout' => 15, 'followlocation' => true, 'maxredirs' => 5];
458
        $c = new curl();
462
        $result = $client->download_one($source, null, $options);
459
        $result = $c->download_one($downloadurl, null, $options);
463
 
460
 
Línea 559... Línea 556...
559
     * @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime)
556
     * @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime)
560
     * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
557
     * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
561
     * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
558
     * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
562
     * @param array $options additional options affecting the file serving
559
     * @param array $options additional options affecting the file serving
563
     */
560
     */
564
    public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) {
561
    public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, ?array $options = null) {
565
        if ($this->disabled) {
562
        if ($this->disabled) {
566
            throw new repository_exception('cannotdownload', 'repository');
563
            throw new repository_exception('cannotdownload', 'repository');
567
        }
564
        }
Línea 568... Línea 565...
568
 
565
 
Línea 879... Línea 876...
879
            throw new repository_exception('errorwhilecommunicatingwith', 'repository', '', $details);
876
            throw new repository_exception('errorwhilecommunicatingwith', 'repository', '', $details);
880
        }
877
        }
Línea 881... Línea 878...
881
 
878
 
Línea -... Línea 879...
-
 
879
        $systemservice = new repository_onedrive\rest($systemauth);
-
 
880
 
-
 
881
        $base = 'https://graph.microsoft.com/v1.0/';
-
 
882
 
-
 
883
        // Fetch the item info.
-
 
884
        $infourl = (new moodle_url($base . 'me/drive/items/' . $source->id))->out(false);
-
 
885
        $response = $userauth->get($infourl);
-
 
886
        if (!$response) {
-
 
887
            throw new repository_exception('cannotdownload', 'repository');
-
 
888
        }
-
 
889
        $response = json_decode($response, true);
882
        $systemservice = new repository_onedrive\rest($systemauth);
890
        $downloadurl = $response['@microsoft.graph.downloadUrl'];
883
 
891
 
884
        // Download the file.
892
        // Download the file.
Línea -... Línea 893...
-
 
893
        $tmpfilename = clean_param($source->id, PARAM_PATH);
-
 
894
        $temppath = make_request_directory() . $tmpfilename;
885
        $tmpfilename = clean_param($source->id, PARAM_PATH);
895
 
886
        $temppath = make_request_directory() . $tmpfilename;
-
 
887
 
-
 
888
        $options = ['filepath' => $temppath, 'timeout' => 60, 'followlocation' => true, 'maxredirs' => 5];
-
 
889
        $base = 'https://graph.microsoft.com/v1.0/';
-
 
890
        $sourceurl = new moodle_url($base . 'me/drive/items/' . $source->id . '/content');
896
        // We cannot send authorization headers in the direct download request, it will fail.
Línea 891... Línea 897...
891
        $sourceurl = $sourceurl->out(false);
897
        $c = new curl();
892
 
898
        $options = ['filepath' => $temppath, 'timeout' => 60, 'followlocation' => true, 'maxredirs' => 5];
893
        $result = $userauth->download_one($sourceurl, null, $options);
899
        $result = $c->download_one($downloadurl, null, $options);
Línea 1008... Línea 1014...
1008
            return $info->name;
1014
            return $info->name;
1009
        }
1015
        }
1010
    }
1016
    }
Línea 1011... Línea 1017...
1011
 
1017
 
1012
    /**
-
 
1013
     * Return true if any instances of the skydrive repo exist - and we can import them.
-
 
1014
     *
-
 
1015
     * @return bool
1018
    /**
1016
     * @deprecated since Moodle 4.0
-
 
1017
     * @todo MDL-72620 This will be deleted in Moodle 4.4.
1019
     * @deprecated since Moodle 4.0
-
 
1020
     */
1018
     */
1021
    #[\core\attribute\deprecated(null, reason: 'It is no longer used', since: '4.0', final: true)]
1019
    public static function can_import_skydrive_files() {
-
 
1020
        global $DB;
-
 
1021
 
-
 
1022
        $skydrive = $DB->get_record('repository', ['type' => 'skydrive'], 'id', IGNORE_MISSING);
-
 
1023
        $onedrive = $DB->get_record('repository', ['type' => 'onedrive'], 'id', IGNORE_MISSING);
-
 
1024
 
-
 
1025
        if (empty($skydrive) || empty($onedrive)) {
-
 
1026
            return false;
-
 
1027
        }
-
 
1028
 
-
 
1029
        $ready = true;
-
 
1030
        try {
-
 
1031
            $issuer = \core\oauth2\api::get_issuer(get_config('onedrive', 'issuerid'));
-
 
1032
            if (!$issuer->get('enabled')) {
-
 
1033
                $ready = false;
-
 
1034
            }
-
 
1035
            if (!$issuer->is_configured()) {
-
 
1036
                $ready = false;
-
 
1037
            }
-
 
1038
        } catch (dml_missing_record_exception $e) {
-
 
1039
            $ready = false;
-
 
1040
        }
-
 
1041
        if (!$ready) {
-
 
1042
            return false;
-
 
1043
        }
-
 
1044
 
-
 
1045
        $sql = "SELECT count('x')
-
 
1046
                  FROM {repository_instances} i, {repository} r
-
 
1047
                 WHERE r.type=:plugin AND r.id=i.typeid";
-
 
1048
        $params = array('plugin' => 'skydrive');
1022
    public static function can_import_skydrive_files() {
1049
        return $DB->count_records_sql($sql, $params) > 0;
1023
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
Línea 1050... Línea 1024...
1050
    }
1024
    }
1051
 
-
 
1052
    /**
-
 
1053
     * Import all the files that were created with the skydrive repo to this repo.
-
 
1054
     *
1025
 
1055
     * @return bool
-
 
1056
     * @deprecated since Moodle 4.0
1026
    /**
-
 
1027
     * @deprecated since Moodle 4.0
1057
     * @todo MDL-72620 This will be deleted in Moodle 4.4.
1028
     */
1058
     */
-
 
1059
    public static function import_skydrive_files() {
-
 
1060
        global $DB;
-
 
1061
 
-
 
1062
        debugging('import_skydrive_files() is deprecated. Please migrate your files from repository_skydrive to ' .
-
 
1063
            'repository_onedrive before it will be completely removed.', DEBUG_DEVELOPER);
-
 
1064
 
-
 
1065
        if (!self::can_import_skydrive_files()) {
-
 
1066
            return false;
-
 
1067
        }
-
 
1068
        // Should only be one of each.
-
 
1069
        $skydrivetype = repository::get_type_by_typename('skydrive');
-
 
1070
 
-
 
1071
        $skydriveinstances = repository::get_instances(['type' => 'skydrive']);
-
 
1072
        $skydriveinstance = reset($skydriveinstances);
-
 
1073
        $onedriveinstances = repository::get_instances(['type' => 'onedrive']);
-
 
1074
        $onedriveinstance = reset($onedriveinstances);
-
 
1075
 
-
 
1076
        // Update all file references.
-
 
1077
        $DB->set_field('files_reference', 'repositoryid', $onedriveinstance->id, ['repositoryid' => $skydriveinstance->id]);
-
 
1078
 
-
 
1079
        // Delete and disable the skydrive repo.
-
 
1080
        $skydrivetype->delete();
-
 
1081
        core_plugin_manager::reset_caches();
-
 
1082
 
-
 
1083
        $sql = "SELECT count('x')
-
 
1084
                  FROM {repository_instances} i, {repository} r
-
 
1085
                 WHERE r.type=:plugin AND r.id=i.typeid";
1029
    #[\core\attribute\deprecated(null, reason: 'It is no longer used', since: '4.0', final: true)]
1086
        $params = array('plugin' => 'skydrive');
1030
    public static function import_skydrive_files() {
Línea 1087... Línea 1031...
1087
        return $DB->count_records_sql($sql, $params) == 0;
1031
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
1088
    }
1032
    }
1089
 
1033
 
Línea 1099... Línea 1043...
1099
        $url = new moodle_url('/admin/tool/oauth2/issuers.php');
1043
        $url = new moodle_url('/admin/tool/oauth2/issuers.php');
1100
        $url = $url->out();
1044
        $url = $url->out();
Línea 1101... Línea 1045...
1101
 
1045
 
Línea 1102... Línea -...
1102
        $mform->addElement('static', null, '', get_string('oauth2serviceslink', 'repository_onedrive', $url));
-
 
1103
 
-
 
1104
        if (self::can_import_skydrive_files()) {
-
 
1105
            debugging('can_import_skydrive_files() is deprecated. Please migrate your files from repository_skydrive to ' .
-
 
1106
            'repository_onedrive before it will be completely removed.', DEBUG_DEVELOPER);
-
 
1107
 
-
 
1108
            $notice = get_string('skydrivefilesexist', 'repository_onedrive');
-
 
1109
            $url = new moodle_url('/repository/onedrive/importskydrive.php');
-
 
1110
            $attrs = ['class' => 'btn btn-primary'];
-
 
1111
            $button = $OUTPUT->action_link($url, get_string('importskydrivefiles', 'repository_onedrive'), null, $attrs);
-
 
1112
            $mform->addElement('static', null, '', $OUTPUT->notification($notice) . $button);
-
 
1113
        }
1046
        $mform->addElement('static', null, '', get_string('oauth2serviceslink', 'repository_onedrive', $url));
1114
 
1047
 
1115
        parent::type_config_form($mform);
1048
        parent::type_config_form($mform);
Línea 1116... Línea 1049...
1116
        $options = [];
1049
        $options = [];