/repository/user/lib.php

https://github.com/viggof/moodle · PHP · 135 lines · 80 code · 11 blank · 44 comment · 11 complexity · a7d26804dc68d91a94904b138397932e MD5 · raw file

  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_user class is used to browse user private files
  18. *
  19. * @since 2.0
  20. * @package repository
  21. * @subpackage user
  22. * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. class repository_user extends repository {
  26. /**
  27. * user plugin doesn't require login
  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. $filepath = clean_param($params['filepath'], PARAM_PATH);;
  50. $filename = clean_param($params['filename'], PARAM_FILE);
  51. }
  52. } else {
  53. $itemid = 0;
  54. $filepath = '/';
  55. $filename = null;
  56. }
  57. $filearea = 'private';
  58. $component = 'user';
  59. $itemid = 0;
  60. $context = get_context_instance(CONTEXT_USER, $USER->id);
  61. try {
  62. $browser = get_file_browser();
  63. if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
  64. $pathnodes = array();
  65. $level = $fileinfo;
  66. $params = $fileinfo->get_params();
  67. while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
  68. $encodedpath = base64_encode(serialize($level->get_params()));
  69. $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
  70. $level = $level->get_parent();
  71. $params = $level->get_params();
  72. }
  73. $ret['path'] = array_reverse($pathnodes);
  74. // build file tree
  75. $children = $fileinfo->get_children();
  76. foreach ($children as $child) {
  77. if ($child->is_directory()) {
  78. $encodedpath = base64_encode(serialize($child->get_params()));
  79. $node = array(
  80. 'title' => $child->get_visible_name(),
  81. 'size' => 0,
  82. 'date' => '',
  83. 'path' => $encodedpath,
  84. 'children'=>array(),
  85. 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
  86. );
  87. $list[] = $node;
  88. } else {
  89. $encodedpath = base64_encode(serialize($child->get_params()));
  90. $node = array(
  91. 'title' => $child->get_visible_name(),
  92. 'size' => 0,
  93. 'date' => '',
  94. 'source'=> $encodedpath,
  95. 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
  96. );
  97. $list[] = $node;
  98. }
  99. }
  100. }
  101. } catch (Exception $e) {
  102. throw new repository_exception('emptyfilelist', 'repository_user');
  103. }
  104. $ret['list'] = $list;
  105. $ret['list'] = array_filter($list, array($this, 'filter'));
  106. return $ret;
  107. }
  108. /**
  109. * User file don't support to link to external links
  110. *
  111. * @return int
  112. */
  113. public function supported_returntypes() {
  114. return FILE_INTERNAL;
  115. }
  116. /**
  117. * Does this repository used to browse moodle files?
  118. *
  119. * @return boolean
  120. */
  121. public function has_moodle_files() {
  122. return true;
  123. }
  124. }