PageRenderTime 95ms CodeModel.GetById 51ms RepoModel.GetById 1ms app.codeStats 0ms

/repository/local/lib.php

https://github.com/mylescarrick/moodle
PHP | 210 lines | 133 code | 16 blank | 61 comment | 17 complexity | 91309b1b449d996ac79413c73c7ed3be MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  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. /**
  17. * repository_local class is used to browse moodle files
  18. *
  19. * @since 2.0
  20. * @package repository
  21. * @subpackage local
  22. * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. class repository_local extends repository {
  26. /**
  27. * local plugin doesn't require login, so list all files
  28. * @return mixed
  29. */
  30. public function print_login() {
  31. return $this->get_listing();
  32. }
  33. /**
  34. * Get file listing
  35. *
  36. * @param string $encodedpath
  37. * @return mixed
  38. */
  39. public function get_listing($encodedpath = '') {
  40. global $CFG, $USER, $OUTPUT;
  41. $ret = array();
  42. $ret['dynload'] = true;
  43. $ret['nosearch'] = true;
  44. $ret['nologin'] = true;
  45. $list = array();
  46. if (!empty($encodedpath)) {
  47. $params = unserialize(base64_decode($encodedpath));
  48. if (is_array($params)) {
  49. $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_ALPHAEXT);
  50. $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_ALPHAEXT);
  51. $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
  52. $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);;
  53. $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
  54. $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
  55. }
  56. } else {
  57. $itemid = null;
  58. $filename = null;
  59. $filearea = null;
  60. $filepath = null;
  61. $component = null;
  62. if (!empty($this->context)) {
  63. list($context, $course, $cm) = get_context_info_array($this->context->id);
  64. $courseid = is_object($course) ? $course->id : SITEID;
  65. $context = get_context_instance(CONTEXT_COURSE, $courseid);
  66. } else {
  67. $context = get_system_context();
  68. }
  69. }
  70. $browser = get_file_browser();
  71. if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
  72. // build path navigation
  73. $pathnodes = array();
  74. $encodedpath = base64_encode(serialize($fileinfo->get_params()));
  75. $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
  76. $level = $fileinfo->get_parent();
  77. while ($level) {
  78. $encodedpath = base64_encode(serialize($level->get_params()));
  79. $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
  80. $level = $level->get_parent();
  81. }
  82. if (!empty($pathnodes) && is_array($pathnodes)) {
  83. $pathnodes = array_reverse($pathnodes);
  84. $ret['path'] = $pathnodes;
  85. }
  86. // build file tree
  87. $children = $fileinfo->get_children();
  88. foreach ($children as $child) {
  89. if ($child->is_directory()) {
  90. if ($child->is_empty_area()) {
  91. continue;
  92. }
  93. $params = $child->get_params();
  94. $subdir_children = $child->get_children();
  95. //if (empty($subdir_children)) {
  96. //continue;
  97. //}
  98. $encodedpath = base64_encode(serialize($params));
  99. // hide user_private area from local plugin, user should
  100. // use private file plugin to access private files
  101. //if ($params['filearea'] == 'user_private') {
  102. //continue;
  103. //}
  104. $node = array(
  105. 'title' => $child->get_visible_name(),
  106. 'size' => 0,
  107. 'date' => '',
  108. 'path' => $encodedpath,
  109. 'children'=>array(),
  110. 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
  111. );
  112. $list[] = $node;
  113. } else {
  114. $encodedpath = base64_encode(serialize($child->get_params()));
  115. $node = array(
  116. 'title' => $child->get_visible_name(),
  117. 'size' => 0,
  118. 'date' => '',
  119. 'source'=> $encodedpath,
  120. 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
  121. );
  122. $list[] = $node;
  123. }
  124. }
  125. } else {
  126. // if file doesn't exist, build path nodes root of current context
  127. $pathnodes = array();
  128. $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
  129. $encodedpath = base64_encode(serialize($fileinfo->get_params()));
  130. $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
  131. $level = $fileinfo->get_parent();
  132. while ($level) {
  133. $encodedpath = base64_encode(serialize($level->get_params()));
  134. $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
  135. $level = $level->get_parent();
  136. }
  137. if (!empty($pathnodes) && is_array($pathnodes)) {
  138. $pathnodes = array_reverse($pathnodes);
  139. $ret['path'] = $pathnodes;
  140. }
  141. $list = array();
  142. }
  143. $ret['list'] = array_filter($list, array($this, 'filter'));
  144. return $ret;
  145. }
  146. /**
  147. * Local file don't support to link to external links
  148. *
  149. * @return int
  150. */
  151. public function supported_returntypes() {
  152. return FILE_INTERNAL;
  153. }
  154. /**
  155. * Copy a file to file area
  156. *
  157. * @global object $USER
  158. * @global object $DB
  159. * @param string $encoded The metainfo of file, it is base64 encoded php serialized data
  160. * @param string $draftitemid itemid
  161. * @param string $new_filename The intended name of file
  162. * @param string $new_filepath the new path in draft area
  163. * @return array The information of file
  164. */
  165. public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) {
  166. global $USER, $DB;
  167. $info = array();
  168. $browser = get_file_browser();
  169. $fs = get_file_storage();
  170. $user_context = get_context_instance(CONTEXT_USER, $USER->id);
  171. // the final file
  172. $params = unserialize(base64_decode($encoded));
  173. $contextid = clean_param($params['contextid'], PARAM_INT);
  174. $fileitemid = clean_param($params['itemid'], PARAM_INT);
  175. $filename = clean_param($params['filename'], PARAM_FILE);
  176. $filepath = clean_param($params['filepath'], PARAM_PATH);;
  177. $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
  178. $component = clean_param($params['component'], PARAM_ALPHAEXT);
  179. $context = get_context_instance_by_id($contextid);
  180. if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) {
  181. throw new moodle_exception('fileexists');
  182. }
  183. $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
  184. $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename);
  185. $info['itemid'] = $draftitemid;
  186. $info['title'] = $new_filename;
  187. $info['contextid'] = $user_context->id;
  188. $info['filesize'] = $file_info->get_filesize();
  189. return $info;
  190. }
  191. function get_file_count($contextid) {
  192. global $DB;
  193. }
  194. }