PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/filebrowser/file_info_context_user.php

https://bitbucket.org/moodle/moodle
PHP | 306 lines | 135 code | 44 blank | 127 comment | 31 complexity | af5e76ad9c5dfc1f409973f11068ea24 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Utility class for browsing of user files.
  18. *
  19. * @package core_files
  20. * @copyright 2008 Petr Skoda (http://skodak.org)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Represents a user context in the tree navigated by {@link file_browser}.
  26. *
  27. * @package core_files
  28. * @copyright 2008 Petr Skoda (http://skodak.org)
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class file_info_context_user extends file_info {
  32. /** @var stdClass User object */
  33. protected $user;
  34. /**
  35. * Constructor
  36. *
  37. * @param file_browser $browser
  38. * @param stdClass $context
  39. * @param stdClass $user
  40. */
  41. public function __construct($browser, $context, $user) {
  42. parent::__construct($browser, $context);
  43. $this->user = $user;
  44. }
  45. /**
  46. * Return information about this specific context level
  47. *
  48. * @param string $component componet
  49. * @param string $filearea file area
  50. * @param int $itemid item ID
  51. * @param string $filepath file path
  52. * @param string $filename file name
  53. * @return file_info|null
  54. */
  55. public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
  56. global $USER;
  57. if (!isloggedin() or isguestuser()) {
  58. return null;
  59. }
  60. if (empty($component)) {
  61. // access control: list areas only for myself
  62. if ($this->user->id != $USER->id) {
  63. // no list of areas for other users
  64. return null;
  65. }
  66. return $this;
  67. }
  68. $methodname = "get_area_{$component}_{$filearea}";
  69. if (method_exists($this, $methodname)) {
  70. return $this->$methodname($itemid, $filepath, $filename);
  71. }
  72. return null;
  73. }
  74. /**
  75. * Get a file from user private area
  76. *
  77. * @todo MDL-31070 this method should respect $CFG->userquota
  78. * @param int $itemid item ID
  79. * @param string $filepath file path
  80. * @param string $filename file name
  81. * @return file_info|null
  82. */
  83. protected function get_area_user_private($itemid, $filepath, $filename) {
  84. global $USER, $CFG;
  85. // access control: only my files, nobody else
  86. if ($this->user->id != $USER->id) {
  87. return null;
  88. }
  89. if (is_null($itemid)) {
  90. // go to parent, we do not use itemids here in private area
  91. return $this;
  92. }
  93. $fs = get_file_storage();
  94. $filepath = is_null($filepath) ? '/' : $filepath;
  95. $filename = is_null($filename) ? '.' : $filename;
  96. if (!$storedfile = $fs->get_file($this->context->id, 'user', 'private', 0, $filepath, $filename)) {
  97. if ($filepath === '/' and $filename === '.') {
  98. // root dir does not exist yet
  99. $storedfile = new virtual_root_file($this->context->id, 'user', 'private', 0);
  100. } else {
  101. // not found
  102. return null;
  103. }
  104. }
  105. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  106. //TODO: user quota from $CFG->userquota
  107. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);
  108. }
  109. /**
  110. * Get a file from user profile area
  111. *
  112. * @param int $itemid item ID
  113. * @param string $filepath file path
  114. * @param string $filename file name
  115. * @return file_info|null
  116. */
  117. protected function get_area_user_profile($itemid, $filepath, $filename) {
  118. global $CFG;
  119. $readaccess = has_capability('moodle/user:update', $this->context);
  120. $writeaccess = has_capability('moodle/user:viewalldetails', $this->context);
  121. if (!$readaccess and !$writeaccess) {
  122. // the idea here is that only admins should be able to list/modify files in user profile, the rest has to use profile page
  123. return null;
  124. }
  125. if (is_null($itemid)) {
  126. // go to parent, we do not use itemids here in profile area
  127. return $this;
  128. }
  129. $fs = get_file_storage();
  130. $filepath = is_null($filepath) ? '/' : $filepath;
  131. $filename = is_null($filename) ? '.' : $filename;
  132. if (!$storedfile = $fs->get_file($this->context->id, 'user', 'profile', 0, $filepath, $filename)) {
  133. if ($filepath === '/' and $filename === '.') {
  134. $storedfile = new virtual_root_file($this->context->id, 'user', 'profile', 0);
  135. } else {
  136. // not found
  137. return null;
  138. }
  139. }
  140. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  141. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase,
  142. get_string('areauserprofile', 'repository'), false, $readaccess, $writeaccess, false);
  143. }
  144. /**
  145. * Get a file from user draft area
  146. *
  147. * @param int $itemid item ID
  148. * @param string $filepath file path
  149. * @param string $filename file name
  150. * @return file_info|null
  151. */
  152. protected function get_area_user_draft($itemid, $filepath, $filename) {
  153. global $USER, $CFG;
  154. // access control: only my files
  155. if ($this->user->id != $USER->id) {
  156. return null;
  157. }
  158. if (empty($itemid)) {
  159. // do not browse itemids - you must know the draftid to see what is there
  160. return null;
  161. }
  162. $fs = get_file_storage();
  163. $filepath = is_null($filepath) ? '/' : $filepath;
  164. $filename = is_null($filename) ? '.' : $filename;
  165. if (!$storedfile = $fs->get_file($this->context->id, 'user', 'draft', $itemid, $filepath, $filename)) {
  166. if ($filepath === '/' and $filename === '.') {
  167. $storedfile = new virtual_root_file($this->context->id, 'user', 'draft', $itemid);
  168. } else {
  169. // not found
  170. return null;
  171. }
  172. }
  173. $urlbase = $CFG->wwwroot.'/draftfile.php';
  174. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true);
  175. }
  176. /**
  177. * Get a file from user backup area
  178. *
  179. * @todo MDL-31091 maybe we need new caability for access control
  180. * @param int $itemid item ID
  181. * @param string $filepath file path
  182. * @param string $filename file name
  183. * @return file_info|null
  184. */
  185. protected function get_area_user_backup($itemid, $filepath, $filename) {
  186. global $USER, $CFG;
  187. // access control: only my files, nobody else - TODO: maybe we need new capability here
  188. if ($this->context->instanceid != $USER->id) {
  189. return null;
  190. }
  191. if (is_null($itemid)) {
  192. // go to parent, we do not use itemids here in profile area
  193. return $this;
  194. }
  195. $fs = get_file_storage();
  196. $filepath = is_null($filepath) ? '/' : $filepath;
  197. $filename = is_null($filename) ? '.' : $filename;
  198. if (!$storedfile = $fs->get_file($this->context->id, 'user', 'backup', $itemid, $filepath, $filename)) {
  199. if ($filepath === '/' and $filename === '.') {
  200. $storedfile = new virtual_root_file($this->context->id, 'user', 'backup', 0);
  201. } else {
  202. // not found
  203. return null;
  204. }
  205. }
  206. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  207. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserbackup', 'repository'), false, true, true, false);
  208. }
  209. /**
  210. * Returns localised visible name.
  211. *
  212. * @return string
  213. */
  214. public function get_visible_name() {
  215. return fullname($this->user, true);
  216. }
  217. /**
  218. * Whether or not new files or directories can be added
  219. *
  220. * @return bool
  221. */
  222. public function is_writable() {
  223. return false;
  224. }
  225. /**
  226. * Whether or not this is a directory
  227. *
  228. * @return bool
  229. */
  230. public function is_directory() {
  231. return true;
  232. }
  233. /**
  234. * Returns list of children.
  235. *
  236. * @return array of file_info instances
  237. */
  238. public function get_children() {
  239. $children = array();
  240. if ($child = $this->get_area_user_private(0, '/', '.')) {
  241. $children[] = $child;
  242. }
  243. /*
  244. if ($child = $this->get_area_user_profile(0, '/', '.')) {
  245. $children[] = $child;
  246. }
  247. */
  248. if ($child = $this->get_area_user_backup(0, '/', '.')) {
  249. $children[] = $child;
  250. }
  251. // do not list draft area here - it is browsable only if you know the draft itemid ;-)
  252. return $children;
  253. }
  254. /**
  255. * Returns parent file_info instance
  256. *
  257. * @return file_info|null file_info instance or null for root
  258. */
  259. public function get_parent() {
  260. return $this->browser->get_file_info();
  261. }
  262. }