~ubuntu-branches/ubuntu/trusty/moodle/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/0016-MDL-29872-Dropbox-repository-add-message-handler-to-.patch/repository/dropbox/lib.php

  • Committer: Package Import Robot
  • Author(s): Didier Raboud
  • Date: 2012-11-12 10:00:00 UTC
  • Revision ID: package-import@ubuntu.com-20121112100000-4c9fw4r1olbshfr9
Tags: 2.2.3.dfsg-2.6
* Non-maintainer upload.

* Backport multiple security issues from upstream's MOODLE_22_STABLE
  branch.
  - MSA-12-0057: MDL-29872 - Access issue through repository
    Fixes CVE-2012-5471
  - MSA-12-0058: MDL-32785 - Possible form data manipulation issue
    Fixes CVE-2012-5472
  - MSA-12-0059: MDL-34448 - Information leak in Database activity module
    Fixes CVE-2012-5473
  - MSA-12-0061: MDL-33791 - Remote code execution through Portfolio API
    Fixes CVE-2012-5479
  - MSA-12-0062: MDL-35558 - Information leak in Database activity module
    Fixes CVE-2012-5480

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
// This file is part of Moodle - http://moodle.org/
 
4
//
 
5
// Moodle is free software: you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation, either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// Moodle is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
/**
 
19
 * repository_dropbox class
 
20
 * This plugin is used to access user's dropbox files
 
21
 *
 
22
 * @since 2.0
 
23
 * @package    repository
 
24
 * @subpackage dropbox
 
25
 * @copyright  2010 Dongsheng Cai
 
26
 * @author     Dongsheng Cai <dongsheng@moodle.com>
 
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 
28
 */
 
29
 
 
30
require_once(dirname(__FILE__).'/locallib.php');
 
31
 
 
32
class repository_dropbox extends repository {
 
33
    private $dropbox;
 
34
    public $files;
 
35
    public $logged=false;
 
36
 
 
37
    /**
 
38
     * Constructor of dropbox plugin
 
39
     * @param int $repositoryid
 
40
     * @param object $context
 
41
     * @param array $options
 
42
     */
 
43
    public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
 
44
        global $SESSION, $CFG;
 
45
        $options['page']    = optional_param('p', 1, PARAM_INT);
 
46
        parent::__construct($repositoryid, $context, $options);
 
47
 
 
48
        $this->setting = 'dropbox_';
 
49
 
 
50
        $this->dropbox_key = $this->get_option('dropbox_key');
 
51
        $this->dropbox_secret  = $this->get_option('dropbox_secret');
 
52
 
 
53
        $this->access_key    = get_user_preferences($this->setting.'_access_key', '');
 
54
        $this->access_secret = get_user_preferences($this->setting.'_access_secret', '');
 
55
 
 
56
        if (!empty($this->access_key) && !empty($this->access_secret)) {
 
57
            $this->logged = true;
 
58
        }
 
59
 
 
60
        $this->callback = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array(
 
61
            'callback'=>'yes',
 
62
            'repo_id'=>$repositoryid
 
63
            ));
 
64
 
 
65
        $args = array(
 
66
            'oauth_consumer_key'=>$this->dropbox_key,
 
67
            'oauth_consumer_secret'=>$this->dropbox_secret,
 
68
            'oauth_callback' => $this->callback->out(false),
 
69
            'api_root' => 'https://www.dropbox.com/1/oauth',
 
70
        );
 
71
 
 
72
        $this->dropbox = new dropbox($args);
 
73
    }
 
74
 
 
75
    /**
 
76
     * Check if moodle has got access token and secret
 
77
     * @return bool
 
78
     */
 
79
    public function check_login() {
 
80
        return !empty($this->logged);
 
81
    }
 
82
 
 
83
    /**
 
84
     * Generate dropbox login url
 
85
     * @return array
 
86
     */
 
87
    public function print_login() {
 
88
        $result = $this->dropbox->request_token();
 
89
        set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']);
 
90
        $url = $result['authorize_url'];
 
91
        if ($this->options['ajax']) {
 
92
            $ret = array();
 
93
            $popup_btn = new stdClass();
 
94
            $popup_btn->type = 'popup';
 
95
            $popup_btn->url = $url;
 
96
            $ret['login'] = array($popup_btn);
 
97
            return $ret;
 
98
        } else {
 
99
            echo '<a target="_blank" href="'.$url.'">'.get_string('login', 'repository').'</a>';
 
100
        }
 
101
    }
 
102
 
 
103
    /**
 
104
     * Request access token
 
105
     * @return array
 
106
     */
 
107
    public function callback() {
 
108
        $token  = optional_param('oauth_token', '', PARAM_TEXT);
 
109
        $secret = get_user_preferences($this->setting.'_request_secret', '');
 
110
        $access_token = $this->dropbox->get_access_token($token, $secret);
 
111
        set_user_preference($this->setting.'_access_key', $access_token['oauth_token']);
 
112
        set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']);
 
113
    }
 
114
 
 
115
    /**
 
116
     * Get dropbox files
 
117
     * @param string $path
 
118
     * @param int $page
 
119
     * @return array
 
120
     */
 
121
    public function get_listing($path = '', $page = '1') {
 
122
        global $OUTPUT;
 
123
        if (empty($path) || $path=='/') {
 
124
            $path = '/';
 
125
        } else {
 
126
            $path = file_correct_filepath($path);
 
127
        }
 
128
        $encoded_path = str_replace("%2F", "/", rawurlencode($path));
 
129
 
 
130
        $list = array();
 
131
        $list['list'] = array();
 
132
        $list['manage'] = false;
 
133
        $list['dynload'] = true;
 
134
        $list['nosearch'] = true;
 
135
        // process breadcrumb trail
 
136
        $list['path'] = array(
 
137
            array('name'=>get_string('dropbox', 'repository_dropbox'), 'path'=>'/')
 
138
        );
 
139
 
 
140
        $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret);
 
141
 
 
142
        if (!is_object($result) || empty($result)) {
 
143
            return $list;
 
144
        }
 
145
        if (empty($result->path)) {
 
146
            $current_path = '/';
 
147
        } else {
 
148
            $current_path = file_correct_filepath($result->path);
 
149
        }
 
150
 
 
151
        $trail = '';
 
152
        if (!empty($path)) {
 
153
            $parts = explode('/', $path);
 
154
            if (count($parts) > 1) {
 
155
                foreach ($parts as $part) {
 
156
                    if (!empty($part)) {
 
157
                        $trail .= ('/'.$part);
 
158
                        $list['path'][] = array('name'=>$part, 'path'=>$trail);
 
159
                    }
 
160
                }
 
161
            } else {
 
162
                $list['path'][] = array('name'=>$path, 'path'=>$path);
 
163
            }
 
164
        }
 
165
 
 
166
        if (!empty($result->error)) {
 
167
            // reset access key
 
168
            set_user_preference($this->setting.'_access_key', '');
 
169
            set_user_preference($this->setting.'_access_secret', '');
 
170
            throw new repository_exception('repositoryerror', 'repository', '', $result->error);
 
171
        }
 
172
        if (empty($result->contents) or !is_array($result->contents)) {
 
173
            return $list;
 
174
        }
 
175
        $files = $result->contents;
 
176
        foreach ($files as $file) {
 
177
            if ($file->is_dir) {
 
178
                $list['list'][] = array(
 
179
                    'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
 
180
                    'path' => file_correct_filepath($file->path),
 
181
                    'size' => $file->size,
 
182
                    'date' => $file->modified,
 
183
                    'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false),
 
184
                    'children' => array(),
 
185
                );
 
186
            } else {
 
187
                $list['list'][] = array(
 
188
                    'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
 
189
                    'source' => $file->path,
 
190
                    'size' => $file->size,
 
191
                    'date' => $file->modified,
 
192
                    'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 32))->out(false)
 
193
                );
 
194
            }
 
195
        }
 
196
        return $list;
 
197
    }
 
198
    /**
 
199
     * Logout from dropbox
 
200
     * @return array
 
201
     */
 
202
    public function logout() {
 
203
        set_user_preference($this->setting.'_access_key', '');
 
204
        set_user_preference($this->setting.'_access_secret', '');
 
205
        $this->access_key    = '';
 
206
        $this->access_secret = '';
 
207
        return $this->print_login();
 
208
    }
 
209
 
 
210
    /**
 
211
     * Set dropbox option
 
212
     * @param array $options
 
213
     * @return mixed
 
214
     */
 
215
    public function set_option($options = array()) {
 
216
        if (!empty($options['dropbox_key'])) {
 
217
            set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
 
218
        }
 
219
        if (!empty($options['dropbox_secret'])) {
 
220
            set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
 
221
        }
 
222
        unset($options['dropbox_key']);
 
223
        unset($options['dropbox_secret']);
 
224
        $ret = parent::set_option($options);
 
225
        return $ret;
 
226
    }
 
227
 
 
228
    /**
 
229
     * Get dropbox options
 
230
     * @param string $config
 
231
     * @return mixed
 
232
     */
 
233
    public function get_option($config = '') {
 
234
        if ($config==='dropbox_key') {
 
235
            return trim(get_config('dropbox', 'dropbox_key'));
 
236
        } elseif ($config==='dropbox_secret') {
 
237
            return trim(get_config('dropbox', 'dropbox_secret'));
 
238
        } else {
 
239
            $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
 
240
            $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
 
241
        }
 
242
        $options = parent::get_option($config);
 
243
        return $options;
 
244
    }
 
245
 
 
246
    /**
 
247
     *
 
248
     * @param string $photo_id
 
249
     * @param string $file
 
250
     * @return string
 
251
     */
 
252
    public function get_file($filepath, $saveas = '') {
 
253
        $this->dropbox->set_access_token($this->access_key, $this->access_secret);
 
254
        $saveas = $this->prepare_file($saveas);
 
255
        return $this->dropbox->get_file($filepath, $saveas);
 
256
    }
 
257
    /**
 
258
     * Add Plugin settings input to Moodle form
 
259
     * @param object $mform
 
260
     */
 
261
    public function type_config_form($mform) {
 
262
        global $CFG;
 
263
        parent::type_config_form($mform);
 
264
        $key    = get_config('dropbox', 'dropbox_key');
 
265
        $secret = get_config('dropbox', 'dropbox_secret');
 
266
 
 
267
        if (empty($key)) {
 
268
            $key = '';
 
269
        }
 
270
        if (empty($secret)) {
 
271
            $secret = '';
 
272
        }
 
273
 
 
274
        $strrequired = get_string('required');
 
275
 
 
276
        $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40'));
 
277
        $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40'));
 
278
 
 
279
        $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client');
 
280
        $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client');
 
281
        $str_getkey = get_string('instruction', 'repository_dropbox');
 
282
        $mform->addElement('static', null, '',  $str_getkey);
 
283
    }
 
284
 
 
285
    /**
 
286
     * Option names of dropbox plugin
 
287
     * @return array
 
288
     */
 
289
    public static function get_type_option_names() {
 
290
        return array('dropbox_key', 'dropbox_secret', 'pluginname');
 
291
    }
 
292
 
 
293
    /**
 
294
     * Dropbox plugin supports all kinds of files
 
295
     * @return array
 
296
     */
 
297
    public function supported_filetypes() {
 
298
        return '*';
 
299
    }
 
300
 
 
301
    /**
 
302
     * User cannot use the external link to dropbox
 
303
     * @return int
 
304
     */
 
305
    public function supported_returntypes() {
 
306
        return FILE_INTERNAL;
 
307
    }
 
308
}