Commit | Line | Data |
---|---|---|
4317f92f | 1 | <?php |
520de343 | 2 | |
fdcf5320 | 3 | /** |
4 | * repository_filesystem class | |
5 | * Create a repository from your local filesystem | |
6 | * *NOTE* for security issue, we use a fixed repository path | |
7 | * which is %moodledata%/repository | |
8 | * | |
9 | * @author Dongsheng Cai <dongsheng@moodle.com> | |
10 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License | |
11 | */ | |
520de343 | 12 | class repository_filesystem extends repository { |
13 | public function __construct($repositoryid, $context = SITEID, $options = array()) { | |
fdcf5320 | 14 | global $CFG; |
520de343 | 15 | parent::__construct($repositoryid, $context, $options); |
fdcf5320 | 16 | $this->root_path = $CFG->dataroot.'/repository/'; |
b2f8adf4 | 17 | if (!empty($options['fs_path'])) { |
18 | $this->root_path .= ($this->options['fs_path'] . '/'); | |
19 | } | |
e437618b | 20 | if (!empty($options['ajax'])) { |
fdcf5320 | 21 | if (!is_dir($this->root_path)) { |
93e9aa27 | 22 | $created = mkdir($this->root_path, 0700); |
520de343 | 23 | $ret = array(); |
24 | $ret['msg'] = get_string('invalidpath', 'repository_filesystem'); | |
25 | $ret['nosearch'] = true; | |
93e9aa27 | 26 | if ($options['ajax'] && !$created) { |
fdcf5320 | 27 | echo json_encode($ret); |
28 | exit; | |
520de343 | 29 | } |
30 | } | |
520de343 | 31 | } |
32 | } | |
33 | public function get_listing($path = '', $page = '') { | |
390baf46 | 34 | global $CFG, $OUTPUT; |
520de343 | 35 | $list = array(); |
36 | $list['list'] = array(); | |
37 | // process breacrumb trail | |
38 | $list['path'] = array( | |
39 | array('name'=>'Root','path'=>'') | |
40 | ); | |
41 | $trail = ''; | |
42 | if (!empty($path)) { | |
43 | $parts = explode('/', $path); | |
44 | if (count($parts) > 1) { | |
45 | foreach ($parts as $part) { | |
4a9aff79 | 46 | if (!empty($part)) { |
47 | $trail .= ('/'.$part); | |
48 | $list['path'][] = array('name'=>$part, 'path'=>$trail); | |
49 | } | |
520de343 | 50 | } |
51 | } else { | |
52 | $list['path'][] = array('name'=>$path, 'path'=>$path); | |
53 | } | |
54 | $this->root_path .= ($path.'/'); | |
55 | } | |
520de343 | 56 | $list['manage'] = false; |
520de343 | 57 | $list['dynload'] = true; |
520de343 | 58 | $list['nologin'] = true; |
520de343 | 59 | $list['nosearch'] = true; |
60 | if ($dh = opendir($this->root_path)) { | |
61 | while (($file = readdir($dh)) != false) { | |
62 | if ( $file != '.' and $file !='..') { | |
63 | if (filetype($this->root_path.$file) == 'file') { | |
64 | $list['list'][] = array( | |
65 | 'title' => $file, | |
66 | 'source' => $path.'/'.$file, | |
67 | 'size' => filesize($this->root_path.$file), | |
68 | 'date' => time(), | |
b5d0cafc | 69 | 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($this->root_path.$file, 32)) |
520de343 | 70 | ); |
71 | } else { | |
72 | if (!empty($path)) { | |
73 | $current_path = $path . '/'. $file; | |
74 | } else { | |
75 | $current_path = $file; | |
76 | } | |
77 | $list['list'][] = array( | |
78 | 'title' => $file, | |
79 | 'children' => array(), | |
b5d0cafc | 80 | 'thumbnail' => $OUTPUT->pix_url('f/folder-32'), |
520de343 | 81 | 'path' => $current_path |
82 | ); | |
83 | } | |
84 | } | |
85 | } | |
86 | } | |
87 | return $list; | |
88 | } | |
520de343 | 89 | public function check_login() { |
90 | return true; | |
91 | } | |
520de343 | 92 | public function print_login() { |
93 | return true; | |
94 | } | |
520de343 | 95 | public function global_search() { |
96 | return false; | |
97 | } | |
520de343 | 98 | // move file to local moodle |
99 | public function get_file($file, $title = '') { | |
100 | global $CFG; | |
101 | if ($file{0} == '/') { | |
102 | $file = $this->root_path.substr($file, 1, strlen($file)-1); | |
103 | } | |
104 | // this is a hack to prevent move_to_file deleteing files | |
105 | // in local repository | |
106 | $CFG->repository_no_delete = true; | |
107 | return $file; | |
108 | } | |
109 | ||
110 | public function logout() { | |
111 | return true; | |
112 | } | |
113 | ||
114 | public static function get_instance_option_names() { | |
93e9aa27 | 115 | return array('fs_path'); |
520de343 | 116 | } |
117 | ||
118 | public static function get_type_option_names() { | |
119 | return array(); | |
120 | } | |
121 | public function type_config_form(&$mform) { | |
122 | } | |
b2f8adf4 | 123 | public function set_option($options = array()) { |
124 | $options['fs_path'] = clean_param($options['fs_path'], PARAM_PATH); | |
125 | $ret = parent::set_option($options); | |
126 | return $ret; | |
127 | } | |
93e9aa27 | 128 | public function instance_config_form(&$mform) { |
129 | global $CFG; | |
130 | $path = $CFG->dataroot . '/repository/'; | |
131 | if ($handle = opendir($path)) { | |
132 | $fieldname = get_string('path', 'repository_filesystem'); | |
07ce9b64 | 133 | $choices = array(); |
93e9aa27 | 134 | while (false !== ($file = readdir($handle))) { |
135 | if (is_dir($path.$file) && $file != '.' && $file!= '..') { | |
07ce9b64 | 136 | $choices[$file] = $file; |
93e9aa27 | 137 | $fieldname = ''; |
138 | } | |
139 | } | |
07ce9b64 | 140 | $mform->addElement('select', 'fs_path', $fieldname, $choices); |
93e9aa27 | 141 | closedir($handle); |
142 | } | |
143 | $mform->addElement('static', null, '', get_string('information','repository_filesystem')); | |
144 | } | |
41076c58 DC |
145 | public function supported_returntypes() { |
146 | return FILE_INTERNAL; | |
147 | } | |
520de343 | 148 | } |