breadcrumb MDL-14133 One function missed during fix, got it this time
[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');
21
22$page = optional_param('page', '', PARAM_RAW); // page
23$client_id = optional_param('client_id', SITEID, PARAM_RAW); // client ID
24$env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in editor or moodleform
25$file = optional_param('file', '', PARAM_RAW); // file to download
26$title = optional_param('title', '', PARAM_FILE); // new file name
27$itemid = optional_param('itemid', '', PARAM_INT);
28$icon = optional_param('icon', '', PARAM_RAW);
29$action = optional_param('action', '', PARAM_ALPHA);
30$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT); // context ID
31$repo_id = optional_param('repo_id', 1, PARAM_INT); // repository ID
32$req_path = optional_param('p', '', PARAM_RAW); // path
33$callback = optional_param('callback', '', PARAM_CLEANHTML);
34$search_text = optional_param('s', '', PARAM_CLEANHTML);
35
36// init repository plugin
37$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
38 'WHERE i.id=? AND i.typeid=r.id';
39if (!$repository = $DB->get_record_sql($sql, array($repo_id))) {
40 $err->e = get_string('invalidrepositoryid', 'repository');
41 die(json_encode($err));
42} else {
43 $type = $repository->type;
44}
45$url = $CFG->httpswwwroot."/repository/filepicker.php?ctx_id=$ctx_id&itemid=$itemid";
46
47if (file_exists($CFG->dirroot.'/repository/'.$type.'/repository.class.php')) {
48 require_once($CFG->dirroot.'/repository/'.$type.'/repository.class.php');
49 $classname = 'repository_' . $type;
50 try {
51 $repo = new $classname($repo_id, $ctx_id, array('ajax'=>false, 'name'=>$repository->name, 'client_id'=>$client_id));
52 } catch (repository_exception $e){
53 $err->e = $e->getMessage();
54 die(json_encode($err));
55 }
56} else {
57 $err->e = get_string('invalidplugin', 'repository', $type);
58 die(json_encode($err));
59}
60
61if ($action == 'download') {
62 $filepath = $repo->get_file($file, $title, $itemid);
63 if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $filepath)) {
64 // youtube plugin return a url instead a file path
65 $url = $filepath;
66 echo json_encode(array(
67 /* File picker need to know this is a link
68 * in order to attach title to url
69 */
70 'type'=>'link',
71 'client_id'=>$client_id,
72 'url'=>$url,
73 'id'=>$url,
74 'file'=>$url
75 )
76 );
77 } else if (is_array($filepath)) {
78 // file api don't have real file path, so we need more file api specific info for "local" plugin
79 $fileinfo = $filepath;
80 $info = array();
81 $info['file'] = $fileinfo['title'];
82 $info['id'] = $itemid;
83 $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title'];
84 echo json_encode($info);
85 } else {
86 // normal file path name
87 $info = repository::move_to_filepool($filepath, $title, $itemid);
88 //echo json_encode($info);
89 redirect($url, get_string('downloadsucc','repository'));
90 }
91 echo $filepath;
92} else if ($action == 'confirm') {
93 print_header(get_string('download', 'repository'), get_string('download', 'repository'));
94 echo '<img src="'.$icon.'" />';
95 echo '<form method="post"><table>';
96 echo '<tr>';
97 echo '<td><label>'.get_string('filename', 'repository').'</label></td>';
98 echo '<td><input type="text" name="title" value="'.$title.'" /></td>';
99 echo '<td><input type="hidden" name="file" value="'.$file.'" /></td>';
100 echo '<td><input type="hidden" name="action" value="download" /></td>';
101 echo '<td><input type="hidden" name="itemid" value="'.$itemid.'" /></td>';
102 echo '</tr>';
103 echo '</table>';
104 echo '<div>';
105 echo '<input type="submit" value="'.get_string('download', 'repository').'" />';
106 echo '</div>';
107 echo '</form>';
108 print_footer('empty');
109
110} else if ($action == 'list' or $action == 'sign') {
111 $navlinks = array();
112 $navlinks[] = array('name' => 'filepicker', 'link' => $url, 'type' => 'activityinstance');
113 $navlinks[] = array('name' => $repo->get_name());
114
115 $navigation = build_navigation($navlinks);
116 print_header(get_string('accessiblefilepicker', 'repository'), get_string('accessiblefilepicker', 'repository'), $navigation);
117 if ($repo->check_login()) {
118 $list = $repo->get_listing($req_path);
119 $dynload = !empty($list['dynload'])?true:false;
120 if (!empty($list['upload'])) {
121 echo '<form method="post" style="display:inline">';
122 echo '<label>'.$list['upload']['label'].'</label>';
123 echo '<input type="file" name="repo_upload_file" /><br />';
124 echo '<input type="submit" value="Upload" />';
125 echo '</form>';
126 } else {
127 foreach ($list['path'] as $p) {
128 echo '<form method="post" style="display:inline">';
129 echo '<input type="hidden" name="p" value="'.$p['path'].'"';
130 echo '<input type="hidden" name="action" value="list"';
131 echo '<input type="submit" value="'.$p['name'].'" />';
132 echo '</form>';
133 echo ' <strong>/</strong> ';
134 }
135 echo '<table>';
136 foreach ($list['list'] as $item) {
137 echo '<tr>';
138 echo '<td><img src="'.$item['thumbnail'].'" />';
139 echo '</td><td>';
140 if (!empty($item['url'])) {
141 echo '<a href="'.$item['url'].'" target="_blank">'.$item['title'].'</a>';
142 } else {
143 echo $item['title'];
144 }
145 echo '</td>';
146 echo '<td>';
147 if (!isset($item['children'])) {
148 echo '<form method="post">';
149 echo '<input type="hidden" name="file" value="'.$item['source'].'"/>';
150 echo '<input type="hidden" name="action" value="confirm"/>';
151 echo '<input type="hidden" name="title" value="'.$item['title'].'"/>';
152 echo '<input type="hidden" name="icon" value="'.$item['thumbnail'].'"/>';
153 echo '<input type="submit" value="Download" />';
154 echo '</form>';
155 } else {
156 echo '<form method="post">';
157 echo '<input type="hidden" name="p" value="'.$item['path'].'"/>';
158 echo '<input type="submit" value="Enter" />';
159 echo '</form>';
160 }
161 echo '</td>';
162 echo '<td width="100px" align="center">';
163 echo '</td>';
164 echo '</td></tr>';
165 }
166 echo '</table>';
167 }
168 } else {
169 echo '<form method="post">';
170 $repo->print_login();
171 echo '<input type="hidden" name="action" value="sign" />';
172 echo '<input type="hidden" name="repo_id" value="'.$repo_id.'" />';
173 echo '</form>';
174 }
175 print_footer('empty');
176} else {
177 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
178 $repos = repository::get_instances(array($user_context, get_system_context()), null, true, null, '*', 'ref_id');
179 $navlinks = array();
180 $navlinks[] = array('name' => get_string('accessiblefilepicker', 'repository'), 'link' => $url, 'type' => 'activityinstance');
181 $navigation = build_navigation($navlinks);
182 print_header(get_string('accessiblefilepicker', 'repository'), get_string('accessiblefilepicker', 'repository'), $navigation);
183 echo '<div><ul>';
184 foreach($repos as $repo) {
185 $info = $repo->get_meta();
186 echo '<li><img src="'.$info->icon.'" width="16px" height="16px"/> <a href="'.$url.'&action=list&repo_id='.$info->id.'">'.$info->name.'</a></li>';
187 }
188 echo '</ul></div>';
189 print_footer('empty');
190}
191