"MDL-16698, use non-js filepicker to pick up files"
[moodle.git] / repository / filepicker.php
CommitLineData
761265ad 1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
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/>.
16//
17
18require_once('../config.php');
19require_once($CFG->libdir.'/filelib.php');
20require_once('lib.php');
b6fd5aee 21/// Wait as long as it takes for this script to finish
22set_time_limit(0);
23
24require_login();
761265ad 25
26$page = optional_param('page', '', PARAM_RAW); // page
27$client_id = optional_param('client_id', SITEID, PARAM_RAW); // client ID
28$env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in editor or moodleform
29$file = optional_param('file', '', PARAM_RAW); // file to download
30$title = optional_param('title', '', PARAM_FILE); // new file name
31$itemid = optional_param('itemid', '', PARAM_INT);
32$icon = optional_param('icon', '', PARAM_RAW);
33$action = optional_param('action', '', PARAM_ALPHA);
34$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT); // context ID
35$repo_id = optional_param('repo_id', 1, PARAM_INT); // repository ID
36$req_path = optional_param('p', '', PARAM_RAW); // path
37$callback = optional_param('callback', '', PARAM_CLEANHTML);
38$search_text = optional_param('s', '', PARAM_CLEANHTML);
39
40// init repository plugin
41$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
42 'WHERE i.id=? AND i.typeid=r.id';
43if (!$repository = $DB->get_record_sql($sql, array($repo_id))) {
b6fd5aee 44 print_error('invalidrepositoryid', 'repository');
761265ad 45} else {
46 $type = $repository->type;
47}
48$url = $CFG->httpswwwroot."/repository/filepicker.php?ctx_id=$ctx_id&itemid=$itemid";
49
50if (file_exists($CFG->dirroot.'/repository/'.$type.'/repository.class.php')) {
51 require_once($CFG->dirroot.'/repository/'.$type.'/repository.class.php');
52 $classname = 'repository_' . $type;
53 try {
54 $repo = new $classname($repo_id, $ctx_id, array('ajax'=>false, 'name'=>$repository->name, 'client_id'=>$client_id));
55 } catch (repository_exception $e){
b6fd5aee 56 print_error('pluginerror', 'repository');
761265ad 57 }
58} else {
b6fd5aee 59 print_error('invalidplugin', 'repository');
761265ad 60}
14f3c882 61//$context = get_context_instance_by_id($ctx_id);
62//$PAGE->set_course($context);
b6fd5aee 63
14f3c882 64switch ($action) {
ab9cdbb9 65case 'upload':
66 redirect($url, get_string('uploadsucc','repository'));
67 break;
14f3c882 68case 'deletedraft':
69 if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
b6fd5aee 70 print_error('wrongcontextid', 'error');
14f3c882 71 }
72 $contextid = $context->id;
73 $fs = get_file_storage();
74 if ($file = $fs->get_file($contextid, 'user_draft', $itemid, '/', $title)) {
75 if($result = $file->delete()) {
76 header("Location: $CFG->httpswwwroot/repository/filepicker.php?action=embedded&itemid=$itemid&ctx_id=$ctx_id");
77 } else {
b6fd5aee 78 print_error('cannotdelete', 'repository');
14f3c882 79 }
80 }
81 exit;
82 break;
83case 'list':
84case 'sign':
b6fd5aee 85 print_header();
86 echo "<div><a href='$CFG->httpswwwroot/repository/filepicker.php?action=embedded&itemid=$itemid&ctx_id=$ctx_id'>".get_string('back', 'repository')."</a></div>";
761265ad 87 if ($repo->check_login()) {
88 $list = $repo->get_listing($req_path);
89 $dynload = !empty($list['dynload'])?true:false;
90 if (!empty($list['upload'])) {
ab9cdbb9 91 echo '<form action="'.$url.'" method="post" enctype="multipart/form-data" style="display:inline">';
92 echo '<label>'.$list['upload']['label'].': </label>';
761265ad 93 echo '<input type="file" name="repo_upload_file" /><br />';
ab9cdbb9 94 echo '<input type="hidden" name="action" value="upload" /><br />';
95 echo '<input type="hidden" name="repo_id" value="'.$repo_id.'" /><br />';
b6fd5aee 96 echo '<input type="submit" value="'.get_string('upload', 'repository').'" />';
761265ad 97 echo '</form>';
98 } else {
5e98ab96 99 if (!empty($list['path'])) {
100 foreach ($list['path'] as $p) {
101 echo '<form method="post" style="display:inline">';
102 echo '<input type="hidden" name="p" value="'.$p['path'].'"';
103 echo '<input type="hidden" name="action" value="list"';
104 echo '<input type="submit" value="'.$p['name'].'" />';
105 echo '</form>';
b6fd5aee 106 echo '<strong> / </strong>';
5e98ab96 107 }
761265ad 108 }
109 echo '<table>';
110 foreach ($list['list'] as $item) {
111 echo '<tr>';
112 echo '<td><img src="'.$item['thumbnail'].'" />';
113 echo '</td><td>';
114 if (!empty($item['url'])) {
115 echo '<a href="'.$item['url'].'" target="_blank">'.$item['title'].'</a>';
116 } else {
117 echo $item['title'];
118 }
119 echo '</td>';
120 echo '<td>';
121 if (!isset($item['children'])) {
122 echo '<form method="post">';
123 echo '<input type="hidden" name="file" value="'.$item['source'].'"/>';
124 echo '<input type="hidden" name="action" value="confirm"/>';
125 echo '<input type="hidden" name="title" value="'.$item['title'].'"/>';
126 echo '<input type="hidden" name="icon" value="'.$item['thumbnail'].'"/>';
b6fd5aee 127 echo '<input type="submit" value="'.get_string('select','repository').'" />';
761265ad 128 echo '</form>';
129 } else {
130 echo '<form method="post">';
131 echo '<input type="hidden" name="p" value="'.$item['path'].'"/>';
b6fd5aee 132 echo '<input type="submit" value="'.get_string('enter', 'repository').'" />';
761265ad 133 echo '</form>';
134 }
135 echo '</td>';
136 echo '<td width="100px" align="center">';
137 echo '</td>';
138 echo '</td></tr>';
139 }
140 echo '</table>';
141 }
142 } else {
143 echo '<form method="post">';
144 $repo->print_login();
145 echo '<input type="hidden" name="action" value="sign" />';
146 echo '<input type="hidden" name="repo_id" value="'.$repo_id.'" />';
147 echo '</form>';
148 }
149 print_footer('empty');
14f3c882 150 break;
151case 'download':
152 $filepath = $repo->get_file($file, $title, $itemid);
153 if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $filepath)) {
154 // youtube plugin return a url instead a file path
155 $url = $filepath;
156 echo json_encode(array(
157 /* File picker need to know this is a link
158 * in order to attach title to url
159 */
160 'type'=>'link',
161 'client_id'=>$client_id,
162 'url'=>$url,
163 'id'=>$url,
164 'file'=>$url
165 )
166 );
167 } else if (is_array($filepath)) {
168 // file api don't have real file path, so we need more file api specific info for "local" plugin
169 $fileinfo = $filepath;
170 $info = array();
171 $info['file'] = $fileinfo['title'];
172 $info['id'] = $itemid;
173 $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title'];
174 echo json_encode($info);
175 } else {
176 // normal file path name
177 $info = repository::move_to_filepool($filepath, $title, $itemid);
178 //echo json_encode($info);
179 redirect($url, get_string('downloadsucc','repository'));
180 }
181
182 break;
183case 'confirm':
b6fd5aee 184 print_header();
185 echo '<div><a href="'.me().'">'.get_string('back', 'repository').'</a></div>';
14f3c882 186 echo '<img src="'.$icon.'" />';
187 echo '<form method="post"><table>';
188 echo '<tr>';
189 echo '<td><label>'.get_string('filename', 'repository').'</label></td>';
190 echo '<td><input type="text" name="title" value="'.$title.'" /></td>';
191 echo '<td><input type="hidden" name="file" value="'.$file.'" /></td>';
192 echo '<td><input type="hidden" name="action" value="download" /></td>';
193 echo '<td><input type="hidden" name="itemid" value="'.$itemid.'" /></td>';
194 echo '</tr>';
195 echo '</table>';
196 echo '<div>';
197 echo '<input type="submit" value="'.get_string('download', 'repository').'" />';
198 echo '</div>';
199 echo '</form>';
200 print_footer('empty');
201 break;
ab9cdbb9 202case 'plugins':
203 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
204 $repos = repository::get_instances(array($user_context, get_system_context()), null, true, null, '*', 'ref_id');
205 print_header();
206 echo '<div><ul>';
207 foreach($repos as $repo) {
208 $info = $repo->get_meta();
209 echo '<li><img src="'.$info->icon.'" width="16px" height="16px"/> <a href="'.$url.'&action=list&repo_id='.$info->id.'">'.$info->name.'</a></li>';
210 }
211 echo '</ul></div>';
212 break;
14f3c882 213default:
761265ad 214 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
215 $repos = repository::get_instances(array($user_context, get_system_context()), null, true, null, '*', 'ref_id');
b6fd5aee 216 print_header();
217 $fs = get_file_storage();
218 $context = get_context_instance(CONTEXT_USER, $USER->id);
219 $files = $fs->get_area_files($context->id, 'user_draft', $itemid);
b6fd5aee 220 if (empty($files)) {
221 echo get_string('nofilesattached', 'repository');
222 } else {
223 echo '<ul>';
224 foreach ($files as $file) {
225 if ($file->get_filename()!='.') {
226 $drafturl = $CFG->httpswwwroot.'/draftfile.php/'.$context->id.'/user_draft/'.$itemid.'/'.$file->get_filename();
227 echo '<li><a href="'.$drafturl.'">'.$file->get_filename().'</a> ';
228 echo '<a href="'.$CFG->httpswwwroot.'/repository/filepicker.php?action=deletedraft&itemid='.$itemid.'&ctx_id='.$ctx_id.'&title='.$file->get_filename().'"><img src="'.$CFG->httpswwwroot.'/pix/t/delete.gif" class="iconsmall" /></a></li>';
229 }
230 }
231 echo '</ul>';
232 }
ab9cdbb9 233 echo '<div><a href="'.$url.'&action=plugins">'.get_string('add', 'repository').'</a></div>';
761265ad 234 print_footer('empty');
14f3c882 235 break;
761265ad 236}