"MDL-13766, improve file picker pagination"
[moodle.git] / repository / filesystem / repository.class.php
CommitLineData
520de343 1<?php // $Id$
2
3class repository_filesystem extends repository {
03a46b90 4 public static $block_list = array(
5 '/etc',
6 '/',
7 'c:\windows',
8 'c:/windows'
9 );
520de343 10 public function __construct($repositoryid, $context = SITEID, $options = array()) {
11 parent::__construct($repositoryid, $context, $options);
e91f9c39 12 if (empty($this->root_path)) {
13 return;
14 }
520de343 15 $this->root_path = trim($this->root_path);
e437618b 16 if (!empty($options['ajax'])) {
520de343 17 // if created from filepicker
18 if (empty($this->root_path)) {
19 $ret = array();
20 $ret['msg'] = get_string('invalidpath', 'repository_filesystem');
21 $ret['nosearch'] = true;
22 echo json_encode($ret);
23 exit;
24 } else {
25 if (!is_dir($this->root_path)) {
26 $ret = array();
27 $ret['msg'] = get_string('invalidpath', 'repository_filesystem');
28 $ret['nosearch'] = true;
29 if ($options['ajax']) {
30 echo json_encode($ret);
31 exit;
32 }
33 }
34 }
35 if ($this->root_path{strlen($this->root_path)-1} !== '/') {
36 $this->root_path .= '/';
37 }
38 }
39 }
64115dc8 40 public function security_check($path) {
41 $blocked = false;
03a46b90 42 foreach (self::$block_list as $item) {
64115dc8 43 if ($path == $item or $path == $item.'/') {
44 $blocked = true;
45 break;
46 }
47 }
48 return $blocked;
49 }
520de343 50 public function get_listing($path = '', $page = '') {
51 global $CFG;
64115dc8 52
53 if ($this->security_check($this->root_path)) {
54 $ret = array();
55 $ret['msg'] = get_string('blockedpath', 'repository_filesystem');
56 $ret['nosearch'] = true;
57 echo json_encode($ret);
58 exit;
59 }
60
520de343 61 $list = array();
62 $list['list'] = array();
63 // process breacrumb trail
64 $list['path'] = array(
65 array('name'=>'Root','path'=>'')
66 );
67 $trail = '';
68 if (!empty($path)) {
69 $parts = explode('/', $path);
70 if (count($parts) > 1) {
71 foreach ($parts as $part) {
72 $trail .= ('/'.$part);
73 $list['path'][] = array('name'=>$part, 'path'=>$trail);
74 }
75 } else {
76 $list['path'][] = array('name'=>$path, 'path'=>$path);
77 }
78 $this->root_path .= ($path.'/');
79 }
80 // set options
81 $list['manage'] = false;
82 // dynamically loading
83 $list['dynload'] = true;
84 // the current path of this list.
85 // set to true, the login link will be removed
86 $list['nologin'] = true;
87 // set to true, the search button will be removed
88 $list['nosearch'] = true;
89 if ($dh = opendir($this->root_path)) {
90 while (($file = readdir($dh)) != false) {
91 if ( $file != '.' and $file !='..') {
92 if (filetype($this->root_path.$file) == 'file') {
93 $list['list'][] = array(
94 'title' => $file,
95 'source' => $path.'/'.$file,
96 'size' => filesize($this->root_path.$file),
97 'date' => time(),
98 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $this->root_path.$file)
99 );
100 } else {
101 if (!empty($path)) {
102 $current_path = $path . '/'. $file;
103 } else {
104 $current_path = $file;
105 }
106 $list['list'][] = array(
107 'title' => $file,
108 'children' => array(),
109 'thumbnail' => $CFG->pixpath .'/f/folder-32.png',
110 'path' => $current_path
111 );
112 }
113 }
114 }
115 }
116 return $list;
117 }
118 // login
119 public function check_login() {
120 return true;
121 }
122 // if check_login returns false,
123 // this function will be called to print a login form.
124 public function print_login() {
125 return true;
126 }
127 //search
128 // if this plugin support global search, if this function return
129 // true, search function will be called when global searching working
130 public function global_search() {
131 return false;
132 }
133 public function search($text) {
134 $search_result = array();
135 $search_result['list'] = array();
136 return $search_result;
137 }
138 // move file to local moodle
139 public function get_file($file, $title = '') {
140 global $CFG;
141 if ($file{0} == '/') {
142 $file = $this->root_path.substr($file, 1, strlen($file)-1);
143 }
144 // this is a hack to prevent move_to_file deleteing files
145 // in local repository
146 $CFG->repository_no_delete = true;
147 return $file;
148 }
149
150 public function logout() {
151 return true;
152 }
153
154 public static function get_instance_option_names() {
155 return array('root_path');
156 }
157
158 public function instance_config_form(&$mform) {
159 $mform->addElement('text', 'root_path', get_string('path', 'repository_filesystem'), array('value'=>'','size' => '40'));
64115dc8 160 $warning = get_string('donotusesysdir', 'repository_filesystem');
161 $warning .= '<ul>';
03a46b90 162 foreach (self::$block_list as $item) {
64115dc8 163 $warning .= '<li>'.$item.'</li>';
164 }
165 $warning .= '</ul>';
166 $mform->addElement('static', null, '', $warning);
520de343 167 }
168
169 public static function get_type_option_names() {
170 return array();
171 }
172 public function type_config_form(&$mform) {
173 }
174}