Commit | Line | Data |
---|---|---|
4317f92f | 1 | <?php |
520de343 | 2 | |
10d53fd3 DC |
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 | ||
fdcf5320 | 18 | /** |
19 | * repository_filesystem class | |
20 | * Create a repository from your local filesystem | |
21 | * *NOTE* for security issue, we use a fixed repository path | |
22 | * which is %moodledata%/repository | |
23 | * | |
10d53fd3 | 24 | * @since 2.0 |
d078f6d3 PS |
25 | * @package repository |
26 | * @subpackage filesystem | |
27 | * @copyright 2009 Dongsheng Cai | |
28 | * @author Dongsheng Cai <dongsheng@moodle.com> | |
29 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
fdcf5320 | 30 | */ |
520de343 | 31 | class repository_filesystem extends repository { |
447c7a19 | 32 | public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { |
fdcf5320 | 33 | global $CFG; |
520de343 | 34 | parent::__construct($repositoryid, $context, $options); |
873f2e0f DC |
35 | $root = $CFG->dataroot.'/repository/'; |
36 | $subdir = $this->get_option('fs_path'); | |
37 | $this->root_path = $root . $subdir . '/'; | |
e437618b | 38 | if (!empty($options['ajax'])) { |
fdcf5320 | 39 | if (!is_dir($this->root_path)) { |
8512eebe | 40 | $created = mkdir($this->root_path, $CFG->directorypermissions, true); |
520de343 | 41 | $ret = array(); |
42 | $ret['msg'] = get_string('invalidpath', 'repository_filesystem'); | |
43 | $ret['nosearch'] = true; | |
93e9aa27 | 44 | if ($options['ajax'] && !$created) { |
fdcf5320 | 45 | echo json_encode($ret); |
46 | exit; | |
520de343 | 47 | } |
48 | } | |
520de343 | 49 | } |
50 | } | |
51 | public function get_listing($path = '', $page = '') { | |
390baf46 | 52 | global $CFG, $OUTPUT; |
520de343 | 53 | $list = array(); |
54 | $list['list'] = array(); | |
55 | // process breacrumb trail | |
56 | $list['path'] = array( | |
873f2e0f | 57 | array('name'=>'Root', 'path'=>'') |
520de343 | 58 | ); |
59 | $trail = ''; | |
60 | if (!empty($path)) { | |
61 | $parts = explode('/', $path); | |
62 | if (count($parts) > 1) { | |
63 | foreach ($parts as $part) { | |
4a9aff79 | 64 | if (!empty($part)) { |
65 | $trail .= ('/'.$part); | |
66 | $list['path'][] = array('name'=>$part, 'path'=>$trail); | |
67 | } | |
520de343 | 68 | } |
69 | } else { | |
70 | $list['path'][] = array('name'=>$path, 'path'=>$path); | |
71 | } | |
72 | $this->root_path .= ($path.'/'); | |
73 | } | |
520de343 | 74 | $list['manage'] = false; |
520de343 | 75 | $list['dynload'] = true; |
520de343 | 76 | $list['nologin'] = true; |
520de343 | 77 | $list['nosearch'] = true; |
78 | if ($dh = opendir($this->root_path)) { | |
79 | while (($file = readdir($dh)) != false) { | |
80 | if ( $file != '.' and $file !='..') { | |
81 | if (filetype($this->root_path.$file) == 'file') { | |
82 | $list['list'][] = array( | |
83 | 'title' => $file, | |
84 | 'source' => $path.'/'.$file, | |
85 | 'size' => filesize($this->root_path.$file), | |
86 | 'date' => time(), | |
ede72522 | 87 | 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($this->root_path.$file, 32))->out(false) |
520de343 | 88 | ); |
89 | } else { | |
90 | if (!empty($path)) { | |
91 | $current_path = $path . '/'. $file; | |
92 | } else { | |
93 | $current_path = $file; | |
94 | } | |
95 | $list['list'][] = array( | |
96 | 'title' => $file, | |
97 | 'children' => array(), | |
ede72522 | 98 | 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false), |
520de343 | 99 | 'path' => $current_path |
100 | ); | |
101 | } | |
102 | } | |
103 | } | |
104 | } | |
fb4ee704 | 105 | $list['list'] = array_filter($list['list'], array($this, 'filter')); |
520de343 | 106 | return $list; |
107 | } | |
520de343 | 108 | public function check_login() { |
109 | return true; | |
110 | } | |
520de343 | 111 | public function print_login() { |
112 | return true; | |
113 | } | |
520de343 | 114 | public function global_search() { |
115 | return false; | |
116 | } | |
951d2d55 DC |
117 | /** |
118 | * Return file path | |
119 | * @return array | |
120 | */ | |
520de343 | 121 | public function get_file($file, $title = '') { |
122 | global $CFG; | |
123 | if ($file{0} == '/') { | |
124 | $file = $this->root_path.substr($file, 1, strlen($file)-1); | |
951d2d55 DC |
125 | } else { |
126 | $file = $this->root_path.$file; | |
520de343 | 127 | } |
128 | // this is a hack to prevent move_to_file deleteing files | |
129 | // in local repository | |
130 | $CFG->repository_no_delete = true; | |
85b5ed5d | 131 | return array('path'=>$file, 'url'=>''); |
520de343 | 132 | } |
133 | ||
134 | public function logout() { | |
135 | return true; | |
136 | } | |
137 | ||
138 | public static function get_instance_option_names() { | |
93e9aa27 | 139 | return array('fs_path'); |
520de343 | 140 | } |
141 | ||
b2f8adf4 | 142 | public function set_option($options = array()) { |
143 | $options['fs_path'] = clean_param($options['fs_path'], PARAM_PATH); | |
144 | $ret = parent::set_option($options); | |
145 | return $ret; | |
146 | } | |
8e5af6cf | 147 | |
aea5595c | 148 | public function instance_config_form($mform) { |
49d20def | 149 | global $CFG, $PAGE; |
8e5af6cf | 150 | if (has_capability('moodle/site:config', get_system_context())) { |
49d20def DC |
151 | $path = $CFG->dataroot . '/repository/'; |
152 | if (!is_dir($path)) { | |
8512eebe | 153 | mkdir($path, $CFG->directorypermissions, true); |
93e9aa27 | 154 | } |
49d20def DC |
155 | if ($handle = opendir($path)) { |
156 | $fieldname = get_string('path', 'repository_filesystem'); | |
157 | $choices = array(); | |
158 | while (false !== ($file = readdir($handle))) { | |
159 | if (is_dir($path.$file) && $file != '.' && $file!= '..') { | |
160 | $choices[$file] = $file; | |
161 | $fieldname = ''; | |
162 | } | |
163 | } | |
164 | if (empty($choices)) { | |
165 | $mform->addElement('static', '', '', get_string('nosubdir', 'repository_filesystem', $path)); | |
6b172cdc | 166 | $mform->addElement('hidden', 'fs_path', ''); |
49d20def DC |
167 | } else { |
168 | $mform->addElement('select', 'fs_path', $fieldname, $choices); | |
169 | $mform->addElement('static', null, '', get_string('information','repository_filesystem', $path)); | |
170 | } | |
171 | closedir($handle); | |
873f2e0f | 172 | } |
49d20def DC |
173 | } else { |
174 | $mform->addElement('static', null, '', get_string('nopermissions', 'error', get_string('configplugin', 'repository_filesystem'))); | |
175 | return false; | |
93e9aa27 | 176 | } |
93e9aa27 | 177 | } |
8e5af6cf | 178 | |
41076c58 DC |
179 | public function supported_returntypes() { |
180 | return FILE_INTERNAL; | |
181 | } | |
8e5af6cf | 182 | |
49d20def DC |
183 | public static function create($type, $userid, $context, $params, $readonly=0) { |
184 | global $PAGE; | |
8e5af6cf | 185 | if (has_capability('moodle/site:config', get_system_context())) { |
49d20def DC |
186 | return parent::create($type, $userid, $context, $params, $readonly); |
187 | } else { | |
8e5af6cf | 188 | require_capability('moodle/site:config', get_system_context()); |
49d20def DC |
189 | return false; |
190 | } | |
191 | } | |
6b172cdc DC |
192 | public static function instance_form_validation($mform, $data, $errors) { |
193 | if (empty($data['fs_path'])) { | |
194 | $errors['fs_path'] = get_string('invalidadminsettingname', 'error', 'fs_path'); | |
195 | } | |
196 | return $errors; | |
197 | } | |
520de343 | 198 | } |