Updated the HEAD build version to 20090619
[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;
78ff2983 83case 'search':
84 try {
85 $search_result = $repo->search($search_text);
86 $search_result['search_result'] = true;
87 $search_result['repo_id'] = $repo_id;
88 echo '<table>';
89 foreach ($search_result['list'] as $item) {
90 echo '<tr>';
91 echo '<td><img src="'.$item['thumbnail'].'" />';
92 echo '</td><td>';
93 if (!empty($item['url'])) {
94 echo '<a href="'.$item['url'].'" target="_blank">'.$item['title'].'</a>';
95 } else {
96 echo $item['title'];
97 }
98 echo '</td>';
99 echo '<td>';
100 if (!isset($item['children'])) {
101 echo '<form method="post">';
102 echo '<input type="hidden" name="file" value="'.$item['source'].'"/>';
103 echo '<input type="hidden" name="action" value="confirm"/>';
104 echo '<input type="hidden" name="title" value="'.$item['title'].'"/>';
105 echo '<input type="hidden" name="icon" value="'.$item['thumbnail'].'"/>';
106 echo '<input type="submit" value="'.get_string('select','repository').'" />';
107 echo '</form>';
108 } else {
109 echo '<form method="post">';
110 echo '<input type="hidden" name="p" value="'.$item['path'].'"/>';
111 echo '<input type="submit" value="'.get_string('enter', 'repository').'" />';
112 echo '</form>';
113 }
114 echo '</td>';
115 echo '<td width="100px" align="center">';
116 echo '</td>';
117 echo '</td></tr>';
118 }
119 echo '</table>';
120 } catch (repository_exception $e) {
121 }
122 break;
14f3c882 123case 'list':
124case 'sign':
b6fd5aee 125 print_header();
126 echo "<div><a href='$CFG->httpswwwroot/repository/filepicker.php?action=embedded&itemid=$itemid&ctx_id=$ctx_id'>".get_string('back', 'repository')."</a></div>";
761265ad 127 if ($repo->check_login()) {
128 $list = $repo->get_listing($req_path);
129 $dynload = !empty($list['dynload'])?true:false;
130 if (!empty($list['upload'])) {
ab9cdbb9 131 echo '<form action="'.$url.'" method="post" enctype="multipart/form-data" style="display:inline">';
132 echo '<label>'.$list['upload']['label'].': </label>';
761265ad 133 echo '<input type="file" name="repo_upload_file" /><br />';
ab9cdbb9 134 echo '<input type="hidden" name="action" value="upload" /><br />';
135 echo '<input type="hidden" name="repo_id" value="'.$repo_id.'" /><br />';
b6fd5aee 136 echo '<input type="submit" value="'.get_string('upload', 'repository').'" />';
761265ad 137 echo '</form>';
138 } else {
5e98ab96 139 if (!empty($list['path'])) {
140 foreach ($list['path'] as $p) {
141 echo '<form method="post" style="display:inline">';
142 echo '<input type="hidden" name="p" value="'.$p['path'].'"';
143 echo '<input type="hidden" name="action" value="list"';
144 echo '<input type="submit" value="'.$p['name'].'" />';
145 echo '</form>';
b6fd5aee 146 echo '<strong> / </strong>';
5e98ab96 147 }
761265ad 148 }
149 echo '<table>';
150 foreach ($list['list'] as $item) {
151 echo '<tr>';
152 echo '<td><img src="'.$item['thumbnail'].'" />';
153 echo '</td><td>';
154 if (!empty($item['url'])) {
155 echo '<a href="'.$item['url'].'" target="_blank">'.$item['title'].'</a>';
156 } else {
157 echo $item['title'];
158 }
159 echo '</td>';
160 echo '<td>';
161 if (!isset($item['children'])) {
162 echo '<form method="post">';
163 echo '<input type="hidden" name="file" value="'.$item['source'].'"/>';
164 echo '<input type="hidden" name="action" value="confirm"/>';
165 echo '<input type="hidden" name="title" value="'.$item['title'].'"/>';
166 echo '<input type="hidden" name="icon" value="'.$item['thumbnail'].'"/>';
b6fd5aee 167 echo '<input type="submit" value="'.get_string('select','repository').'" />';
761265ad 168 echo '</form>';
169 } else {
170 echo '<form method="post">';
171 echo '<input type="hidden" name="p" value="'.$item['path'].'"/>';
b6fd5aee 172 echo '<input type="submit" value="'.get_string('enter', 'repository').'" />';
761265ad 173 echo '</form>';
174 }
175 echo '</td>';
176 echo '<td width="100px" align="center">';
177 echo '</td>';
178 echo '</td></tr>';
179 }
180 echo '</table>';
181 }
182 } else {
183 echo '<form method="post">';
761265ad 184 echo '<input type="hidden" name="action" value="sign" />';
185 echo '<input type="hidden" name="repo_id" value="'.$repo_id.'" />';
78ff2983 186 $repo->print_login();
761265ad 187 echo '</form>';
188 }
189 print_footer('empty');
14f3c882 190 break;
191case 'download':
192 $filepath = $repo->get_file($file, $title, $itemid);
193 if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $filepath)) {
194 // youtube plugin return a url instead a file path
195 $url = $filepath;
196 echo json_encode(array(
197 /* File picker need to know this is a link
198 * in order to attach title to url
199 */
200 'type'=>'link',
201 'client_id'=>$client_id,
202 'url'=>$url,
203 'id'=>$url,
204 'file'=>$url
205 )
206 );
207 } else if (is_array($filepath)) {
208 // file api don't have real file path, so we need more file api specific info for "local" plugin
209 $fileinfo = $filepath;
210 $info = array();
211 $info['file'] = $fileinfo['title'];
212 $info['id'] = $itemid;
213 $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title'];
214 echo json_encode($info);
215 } else {
216 // normal file path name
217 $info = repository::move_to_filepool($filepath, $title, $itemid);
218 //echo json_encode($info);
219 redirect($url, get_string('downloadsucc','repository'));
220 }
221
222 break;
223case 'confirm':
b6fd5aee 224 print_header();
225 echo '<div><a href="'.me().'">'.get_string('back', 'repository').'</a></div>';
14f3c882 226 echo '<img src="'.$icon.'" />';
227 echo '<form method="post"><table>';
228 echo '<tr>';
229 echo '<td><label>'.get_string('filename', 'repository').'</label></td>';
230 echo '<td><input type="text" name="title" value="'.$title.'" /></td>';
231 echo '<td><input type="hidden" name="file" value="'.$file.'" /></td>';
232 echo '<td><input type="hidden" name="action" value="download" /></td>';
233 echo '<td><input type="hidden" name="itemid" value="'.$itemid.'" /></td>';
234 echo '</tr>';
235 echo '</table>';
236 echo '<div>';
237 echo '<input type="submit" value="'.get_string('download', 'repository').'" />';
238 echo '</div>';
239 echo '</form>';
240 print_footer('empty');
241 break;
ab9cdbb9 242case 'plugins':
243 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
244 $repos = repository::get_instances(array($user_context, get_system_context()), null, true, null, '*', 'ref_id');
245 print_header();
246 echo '<div><ul>';
247 foreach($repos as $repo) {
248 $info = $repo->get_meta();
249 echo '<li><img src="'.$info->icon.'" width="16px" height="16px"/> <a href="'.$url.'&action=list&repo_id='.$info->id.'">'.$info->name.'</a></li>';
250 }
251 echo '</ul></div>';
252 break;
14f3c882 253default:
761265ad 254 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
255 $repos = repository::get_instances(array($user_context, get_system_context()), null, true, null, '*', 'ref_id');
b6fd5aee 256 print_header();
257 $fs = get_file_storage();
258 $context = get_context_instance(CONTEXT_USER, $USER->id);
259 $files = $fs->get_area_files($context->id, 'user_draft', $itemid);
b6fd5aee 260 if (empty($files)) {
261 echo get_string('nofilesattached', 'repository');
262 } else {
263 echo '<ul>';
264 foreach ($files as $file) {
265 if ($file->get_filename()!='.') {
266 $drafturl = $CFG->httpswwwroot.'/draftfile.php/'.$context->id.'/user_draft/'.$itemid.'/'.$file->get_filename();
267 echo '<li><a href="'.$drafturl.'">'.$file->get_filename().'</a> ';
268 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>';
269 }
270 }
271 echo '</ul>';
272 }
ab9cdbb9 273 echo '<div><a href="'.$url.'&action=plugins">'.get_string('add', 'repository').'</a></div>';
761265ad 274 print_footer('empty');
14f3c882 275 break;
761265ad 276}