PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/user/lib.php

https://bitbucket.org/ngmares/moodle
PHP | 170 lines | 99 code | 12 blank | 59 comment | 14 complexity | 9cdada6a720119f41e57df479b593ca0 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, 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. * This plugin is used to access user's private files
  18. *
  19. * @since 2.0
  20. * @package repository_user
  21. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once($CFG->dirroot . '/repository/lib.php');
  25. /**
  26. * repository_user class is used to browse user private files
  27. *
  28. * @since 2.0
  29. * @package repository_user
  30. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class repository_user extends repository {
  34. /**
  35. * user plugin doesn't require login
  36. *
  37. * @return mixed
  38. */
  39. public function print_login() {
  40. return $this->get_listing();
  41. }
  42. /**
  43. * Get file listing
  44. *
  45. * @param string $encodedpath
  46. * @return mixed
  47. */
  48. public function get_listing($encodedpath = '', $page = '') {
  49. global $CFG, $USER, $OUTPUT;
  50. $ret = array();
  51. $ret['dynload'] = true;
  52. $ret['nosearch'] = true;
  53. $ret['nologin'] = true;
  54. $list = array();
  55. if (!empty($encodedpath)) {
  56. $params = unserialize(base64_decode($encodedpath));
  57. if (is_array($params)) {
  58. $filepath = clean_param($params['filepath'], PARAM_PATH);;
  59. $filename = clean_param($params['filename'], PARAM_FILE);
  60. }
  61. } else {
  62. $itemid = 0;
  63. $filepath = '/';
  64. $filename = null;
  65. }
  66. $filearea = 'private';
  67. $component = 'user';
  68. $itemid = 0;
  69. $context = get_context_instance(CONTEXT_USER, $USER->id);
  70. try {
  71. $browser = get_file_browser();
  72. if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
  73. $pathnodes = array();
  74. $level = $fileinfo;
  75. $params = $fileinfo->get_params();
  76. while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
  77. $encodedpath = base64_encode(serialize($level->get_params()));
  78. $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
  79. $level = $level->get_parent();
  80. $params = $level->get_params();
  81. }
  82. $ret['path'] = array_reverse($pathnodes);
  83. // build file tree
  84. $children = $fileinfo->get_children();
  85. foreach ($children as $child) {
  86. if ($child->is_directory()) {
  87. $encodedpath = base64_encode(serialize($child->get_params()));
  88. $node = array(
  89. 'title' => $child->get_visible_name(),
  90. 'datemodified' => $child->get_timemodified(),
  91. 'datecreated' => $child->get_timecreated(),
  92. 'path' => $encodedpath,
  93. 'children'=>array(),
  94. 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)
  95. );
  96. $list[] = $node;
  97. } else {
  98. $encodedpath = base64_encode(serialize($child->get_params()));
  99. $node = array(
  100. 'title' => $child->get_visible_name(),
  101. 'size' => $child->get_filesize(),
  102. 'datemodified' => $child->get_timemodified(),
  103. 'datecreated' => $child->get_timecreated(),
  104. 'author' => $child->get_author(),
  105. 'license' => $child->get_license(),
  106. 'isref' => $child->is_external_file(),
  107. 'source'=> $encodedpath,
  108. 'icon' => $OUTPUT->pix_url(file_file_icon($child, 24))->out(false),
  109. 'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false)
  110. );
  111. if ($child->get_status() == 666) {
  112. $node['originalmissing'] = true;
  113. }
  114. if ($imageinfo = $child->get_imageinfo()) {
  115. $fileurl = new moodle_url($child->get_url());
  116. $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified()));
  117. $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified()));
  118. $node['image_width'] = $imageinfo['width'];
  119. $node['image_height'] = $imageinfo['height'];
  120. }
  121. $list[] = $node;
  122. }
  123. }
  124. }
  125. } catch (Exception $e) {
  126. throw new repository_exception('emptyfilelist', 'repository_user');
  127. }
  128. $ret['list'] = $list;
  129. $ret['list'] = array_filter($list, array($this, 'filter'));
  130. return $ret;
  131. }
  132. /**
  133. * Does this repository used to browse moodle files?
  134. *
  135. * @return boolean
  136. */
  137. public function has_moodle_files() {
  138. return true;
  139. }
  140. /**
  141. * User cannot use the external link to dropbox
  142. *
  143. * @return int
  144. */
  145. public function supported_returntypes() {
  146. return FILE_INTERNAL | FILE_REFERENCE;
  147. }
  148. /**
  149. * Return reference file life time
  150. *
  151. * @param string $ref
  152. * @return int
  153. */
  154. public function get_reference_file_lifetime($ref) {
  155. // this should be realtime
  156. return 0;
  157. }
  158. }