gradebook MDL-22522 made the nav drop down on the scales page remember the currently...
[moodle.git] / repository / filesystem / repository.class.php
CommitLineData
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
DC
24 * @since 2.0
25 * @package moodlecore
26 * @subpackage repository
27 * @copyright 2009 Dongsheng Cai
fdcf5320 28 * @author Dongsheng Cai <dongsheng@moodle.com>
10d53fd3 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
fdcf5320 30 */
520de343 31class 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)) {
93e9aa27 40 $created = mkdir($this->root_path, 0700);
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(),
fb4ee704 87 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($this->root_path.$file, 32)).''
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(),
fb4ee704 98 'thumbnail' => $OUTPUT->pix_url('f/folder-32').'',
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 }
520de343 117 // move file to local moodle
118 public function get_file($file, $title = '') {
119 global $CFG;
120 if ($file{0} == '/') {
121 $file = $this->root_path.substr($file, 1, strlen($file)-1);
122 }
123 // this is a hack to prevent move_to_file deleteing files
124 // in local repository
125 $CFG->repository_no_delete = true;
1dce6261 126 return array('path'=>$file);
520de343 127 }
128
129 public function logout() {
130 return true;
131 }
132
133 public static function get_instance_option_names() {
93e9aa27 134 return array('fs_path');
520de343 135 }
136
137 public static function get_type_option_names() {
138 return array();
139 }
aea5595c 140 public function type_config_form($mform) {
520de343 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 }
aea5595c 147 public function instance_config_form($mform) {
93e9aa27 148 global $CFG;
149 $path = $CFG->dataroot . '/repository/';
fb4ee704
DC
150 if (!is_dir($path)) {
151 mkdir($path);
152 }
93e9aa27 153 if ($handle = opendir($path)) {
154 $fieldname = get_string('path', 'repository_filesystem');
07ce9b64 155 $choices = array();
93e9aa27 156 while (false !== ($file = readdir($handle))) {
157 if (is_dir($path.$file) && $file != '.' && $file!= '..') {
07ce9b64 158 $choices[$file] = $file;
93e9aa27 159 $fieldname = '';
160 }
161 }
873f2e0f
DC
162 if (empty($choices)) {
163 $mform->addElement('static', '', '', get_string('nosubdir', 'repository_filesystem', $path));
164 } else {
165 $mform->addElement('select', 'fs_path', $fieldname, $choices);
166 $mform->addElement('static', null, '', get_string('information','repository_filesystem', $path));
167 }
93e9aa27 168 closedir($handle);
169 }
93e9aa27 170 }
41076c58
DC
171 public function supported_returntypes() {
172 return FILE_INTERNAL;
173 }
520de343 174}