weekly release 2.4dev
[moodle.git] / repository / filesystem / lib.php
CommitLineData
4317f92f 1<?php
10d53fd3
DC
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
67233725
DC
17/**
18 * This plugin is used to access files on server file system
19 *
20 * @since 2.0
21 * @package repository_filesystem
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25require_once($CFG->dirroot . '/repository/lib.php');
26require_once($CFG->libdir . '/filelib.php');
27
fdcf5320 28/**
29 * repository_filesystem class
67233725 30 *
fdcf5320 31 * Create a repository from your local filesystem
32 * *NOTE* for security issue, we use a fixed repository path
33 * which is %moodledata%/repository
34 *
d078f6d3 35 * @package repository
67233725 36 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
fdcf5320 38 */
520de343 39class repository_filesystem extends repository {
67233725
DC
40
41 /**
42 * Constructor
43 *
44 * @param int $repositoryid repository ID
45 * @param int $context context ID
46 * @param array $options
47 */
447c7a19 48 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
fdcf5320 49 global $CFG;
520de343 50 parent::__construct($repositoryid, $context, $options);
873f2e0f
DC
51 $root = $CFG->dataroot.'/repository/';
52 $subdir = $this->get_option('fs_path');
53 $this->root_path = $root . $subdir . '/';
e437618b 54 if (!empty($options['ajax'])) {
fdcf5320 55 if (!is_dir($this->root_path)) {
8512eebe 56 $created = mkdir($this->root_path, $CFG->directorypermissions, true);
520de343 57 $ret = array();
58 $ret['msg'] = get_string('invalidpath', 'repository_filesystem');
59 $ret['nosearch'] = true;
93e9aa27 60 if ($options['ajax'] && !$created) {
fdcf5320 61 echo json_encode($ret);
62 exit;
520de343 63 }
64 }
520de343 65 }
66 }
67 public function get_listing($path = '', $page = '') {
390baf46 68 global $CFG, $OUTPUT;
520de343 69 $list = array();
70 $list['list'] = array();
71 // process breacrumb trail
72 $list['path'] = array(
5bdf63cc 73 array('name'=>get_string('root', 'repository_filesystem'), 'path'=>'')
520de343 74 );
75 $trail = '';
76 if (!empty($path)) {
77 $parts = explode('/', $path);
78 if (count($parts) > 1) {
79 foreach ($parts as $part) {
4a9aff79 80 if (!empty($part)) {
81 $trail .= ('/'.$part);
82 $list['path'][] = array('name'=>$part, 'path'=>$trail);
83 }
520de343 84 }
85 } else {
86 $list['path'][] = array('name'=>$path, 'path'=>$path);
87 }
88 $this->root_path .= ($path.'/');
89 }
520de343 90 $list['manage'] = false;
520de343 91 $list['dynload'] = true;
520de343 92 $list['nologin'] = true;
520de343 93 $list['nosearch'] = true;
94 if ($dh = opendir($this->root_path)) {
95 while (($file = readdir($dh)) != false) {
96 if ( $file != '.' and $file !='..') {
97 if (filetype($this->root_path.$file) == 'file') {
98 $list['list'][] = array(
99 'title' => $file,
100 'source' => $path.'/'.$file,
101 'size' => filesize($this->root_path.$file),
5bdf63cc
MG
102 'datecreated' => filectime($this->root_path.$file),
103 'datemodified' => filemtime($this->root_path.$file),
559276b1
MG
104 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file, 90))->out(false),
105 'icon' => $OUTPUT->pix_url(file_extension_icon($file, 24))->out(false)
520de343 106 );
107 } else {
108 if (!empty($path)) {
109 $current_path = $path . '/'. $file;
110 } else {
111 $current_path = $file;
112 }
113 $list['list'][] = array(
114 'title' => $file,
115 'children' => array(),
5bdf63cc
MG
116 'datecreated' => filectime($this->root_path.$file),
117 'datemodified' => filemtime($this->root_path.$file),
559276b1 118 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
520de343 119 'path' => $current_path
120 );
121 }
122 }
123 }
124 }
fb4ee704 125 $list['list'] = array_filter($list['list'], array($this, 'filter'));
520de343 126 return $list;
127 }
520de343 128 public function check_login() {
129 return true;
130 }
520de343 131 public function print_login() {
132 return true;
133 }
520de343 134 public function global_search() {
135 return false;
136 }
67233725 137
951d2d55
DC
138 /**
139 * Return file path
140 * @return array
141 */
520de343 142 public function get_file($file, $title = '') {
143 global $CFG;
144 if ($file{0} == '/') {
145 $file = $this->root_path.substr($file, 1, strlen($file)-1);
951d2d55
DC
146 } else {
147 $file = $this->root_path.$file;
520de343 148 }
149 // this is a hack to prevent move_to_file deleteing files
150 // in local repository
151 $CFG->repository_no_delete = true;
85b5ed5d 152 return array('path'=>$file, 'url'=>'');
520de343 153 }
154
d6453211
DC
155 /**
156 * Return the source information
157 *
158 * @param stdClass $filepath
159 * @return string|null
160 */
161 public function get_file_source_info($filepath) {
162 return $filepath;
163 }
164
520de343 165 public function logout() {
166 return true;
167 }
168
169 public static function get_instance_option_names() {
93e9aa27 170 return array('fs_path');
520de343 171 }
172
b2f8adf4 173 public function set_option($options = array()) {
174 $options['fs_path'] = clean_param($options['fs_path'], PARAM_PATH);
175 $ret = parent::set_option($options);
176 return $ret;
177 }
8e5af6cf 178
4a126f17 179 public static function instance_config_form($mform) {
49d20def 180 global $CFG, $PAGE;
8e5af6cf 181 if (has_capability('moodle/site:config', get_system_context())) {
49d20def
DC
182 $path = $CFG->dataroot . '/repository/';
183 if (!is_dir($path)) {
8512eebe 184 mkdir($path, $CFG->directorypermissions, true);
93e9aa27 185 }
49d20def
DC
186 if ($handle = opendir($path)) {
187 $fieldname = get_string('path', 'repository_filesystem');
188 $choices = array();
189 while (false !== ($file = readdir($handle))) {
190 if (is_dir($path.$file) && $file != '.' && $file!= '..') {
191 $choices[$file] = $file;
192 $fieldname = '';
193 }
194 }
195 if (empty($choices)) {
196 $mform->addElement('static', '', '', get_string('nosubdir', 'repository_filesystem', $path));
6b172cdc 197 $mform->addElement('hidden', 'fs_path', '');
49d20def
DC
198 } else {
199 $mform->addElement('select', 'fs_path', $fieldname, $choices);
200 $mform->addElement('static', null, '', get_string('information','repository_filesystem', $path));
201 }
202 closedir($handle);
873f2e0f 203 }
49d20def
DC
204 } else {
205 $mform->addElement('static', null, '', get_string('nopermissions', 'error', get_string('configplugin', 'repository_filesystem')));
206 return false;
93e9aa27 207 }
93e9aa27 208 }
8e5af6cf 209
49d20def
DC
210 public static function create($type, $userid, $context, $params, $readonly=0) {
211 global $PAGE;
8e5af6cf 212 if (has_capability('moodle/site:config', get_system_context())) {
49d20def
DC
213 return parent::create($type, $userid, $context, $params, $readonly);
214 } else {
8e5af6cf 215 require_capability('moodle/site:config', get_system_context());
49d20def
DC
216 return false;
217 }
218 }
6b172cdc
DC
219 public static function instance_form_validation($mform, $data, $errors) {
220 if (empty($data['fs_path'])) {
221 $errors['fs_path'] = get_string('invalidadminsettingname', 'error', 'fs_path');
222 }
223 return $errors;
224 }
67233725
DC
225
226 /**
227 * User cannot use the external link to dropbox
228 *
229 * @return int
230 */
231 public function supported_returntypes() {
232 return FILE_INTERNAL | FILE_REFERENCE;
233 }
234
235 /**
0b2bfbd1 236 * Returns information about file in this repository by reference
67233725
DC
237 * {@link repository::get_file_reference()}
238 * {@link repository::get_file()}
239 *
0b2bfbd1
MG
240 * Returns null if file not found or is not readable
241 *
67233725 242 * @param stdClass $reference file reference db record
0b2bfbd1
MG
243 * @return stdClass|null contains one of the following:
244 * - 'contenthash' and 'filesize'
245 * - 'filepath'
246 * - 'handle'
247 * - 'content'
67233725
DC
248 */
249 public function get_file_by_reference($reference) {
250 $ref = $reference->reference;
251 if ($ref{0} == '/') {
252 $filepath = $this->root_path.substr($ref, 1, strlen($ref)-1);
253 } else {
254 $filepath = $this->root_path.$ref;
255 }
0b2bfbd1
MG
256 if (file_exists($filepath) && is_readable($filepath)) {
257 return (object)array('filepath' => $filepath);
258 } else {
259 return null;
260 }
67233725
DC
261 }
262
263 /**
0b2bfbd1
MG
264 * Repository method to serve the referenced file
265 *
266 * @see send_stored_file
67233725 267 *
0b2bfbd1 268 * @param stored_file $storedfile the file that contains the reference
67233725
DC
269 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
270 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
271 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
272 * @param array $options additional options affecting the file serving
273 */
274 public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
275 $reference = $storedfile->get_reference();
276 if ($reference{0} == '/') {
277 $file = $this->root_path.substr($reference, 1, strlen($reference)-1);
278 } else {
279 $file = $this->root_path.$reference;
280 }
0b2bfbd1
MG
281 if (is_readable($file)) {
282 $filename = $storedfile->get_filename();
283 if ($options && isset($options['filename'])) {
284 $filename = $options['filename'];
285 }
286 $dontdie = ($options && isset($options['dontdie']));
287 send_file($file, $filename, $lifetime , $filter, false, $forcedownload, '', $dontdie);
288 } else {
289 send_file_not_found();
290 }
67233725 291 }
520de343 292}