From 96fef79f5f72988bd36b5270cf4e235d159e813d Mon Sep 17 00:00:00 2001
From: skodak <skodak>
Date: Sun, 7 Sep 2008 10:06:26 +0000
Subject: [PATCH] MDL-14589 /. files are not created anymore when browsing
 empty areas, yay!

---
 lib/file/file_browser.php         |  13 +--
 lib/file/file_info_coursefile.php |   6 +-
 lib/file/file_info_stored.php     |  20 ++--
 lib/file/virtual_root_file.php    | 173 ++++++++++++++++++++++++++++++
 4 files changed, 191 insertions(+), 21 deletions(-)
 create mode 100644 lib/file/virtual_root_file.php

diff --git a/lib/file/file_browser.php b/lib/file/file_browser.php
index d4281112fa..79f8fa1722 100644
--- a/lib/file/file_browser.php
+++ b/lib/file/file_browser.php
@@ -8,6 +8,7 @@ require_once("$CFG->libdir/file/file_info_user.php");
 require_once("$CFG->libdir/file/file_info_coursecat.php");
 require_once("$CFG->libdir/file/file_info_course.php");
 require_once("$CFG->libdir/file/file_info_coursefile.php");
+require_once("$CFG->libdir/file/virtual_root_file.php");
 
 /**
  * Main interface for browsing of file tree (local files, areas, virtual files, etc.).
@@ -57,7 +58,7 @@ class file_browser {
 
                     if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
                         if ($filepath === '/' and $filename === '.') {
-                            $storedfile = $fs->create_directory($context->id, $filearea, 0, $filepath, $USER->id);
+                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
                         } else {
                             // not found
                             return null;
@@ -110,7 +111,7 @@ class file_browser {
                     $urlbase = $CFG->wwwroot.'/pluginfile.php';
                     if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
                         if ($filepath === '/' and $filename === '.') {
-                            $storedfile = $fs->create_directory($context->id, $filearea, 0, $filepath);
+                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
                         } else {
                             // not found
                             return null;
@@ -152,7 +153,7 @@ class file_browser {
                     $urlbase = $CFG->wwwroot.'/pluginfile.php';
                     if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
                         if ($filepath === '/' and $filename === '.') {
-                            $storedfile = $fs->create_directory($context->id, $filearea, 0, $filepath);
+                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
                         } else {
                             // not found
                             return null;
@@ -169,7 +170,7 @@ class file_browser {
                     $urlbase = $CFG->wwwroot.'/pluginfile.php';
                     if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
                         if ($filepath === '/' and $filename === '.') {
-                            $storedfile = $fs->create_directory($context->id, $filearea, 0, $filepath);
+                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
                         } else {
                             // not found
                             return null;
@@ -188,7 +189,7 @@ class file_browser {
 
                     if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
                         if ($filepath === '/' and $filename === '.') {
-                            $storedfile = $fs->create_directory($context->id, $filearea, 0, $filepath);
+                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
                         } else {
                             // not found
                             return null;
@@ -247,7 +248,7 @@ class file_browser {
                 $urlbase = $CFG->wwwroot.'/pluginfile.php';
                 if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
                     if ($filepath === '/' and $filename === '.') {
-                        $storedfile = $fs->create_directory($context->id, $filearea, 0, $filepath);
+                        $storedfile = new virtual_root_file($context->id, $filearea, 0);
                     } else {
                         // not found
                         return null;
diff --git a/lib/file/file_info_coursefile.php b/lib/file/file_info_coursefile.php
index 9ebc3b4846..17041b35f1 100644
--- a/lib/file/file_info_coursefile.php
+++ b/lib/file/file_info_coursefile.php
@@ -14,7 +14,7 @@ class file_info_coursefile extends file_info_stored {
             return null;
         }
 
-        if ($this->lf->get_filename() === '.') {
+        if ($this->lf->is_directory()) {
             return null;
         }
 
@@ -28,8 +28,8 @@ class file_info_coursefile extends file_info_stored {
     }
 
     public function get_children() {
-        if ($this->lf->get_filename() !== '.') {
-            return array(); //not a dir
+        if (!$this->lf->is_directory()) {
+            return array();
         }
         return $this->browser->build_coursefile_children($this->context, $this->lf->get_filepath());
     }
diff --git a/lib/file/file_info_stored.php b/lib/file/file_info_stored.php
index dc92a38d2b..981b84be7a 100644
--- a/lib/file/file_info_stored.php
+++ b/lib/file/file_info_stored.php
@@ -102,23 +102,19 @@ class file_info_stored extends file_info {
     }
 
     public function is_directory() {
-        if (!$this->lf) {
-            return true;
-        }
-
-        return ($this->lf->get_filename() === '.');
+        return $this->lf->is_directory();
     }
 
     public function get_children() {
-        if ($this->lf->get_filename() !== '.') {
-            return array(); //not a dir
+        if (!$this->lf->is_directory()) {
+            return array();
         }
         return $this->browser->build_stored_file_children($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(),
                                                          $this->urlbase, $this->areavisiblename, $this->itemidused, $this->readaccess, $this->writeaccess);
     }
 
     public function get_parent() {
-        if ($this->lf->get_filename() !== '.') {
+        if (!$this->lf->is_directory()) {
             return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), '.');
         }
 
@@ -141,7 +137,7 @@ class file_info_stored extends file_info {
     }
 
     public function create_directory($newdirname, $userid=null) {
-        if (!$this->is_writable() or $this->lf->get_filename() !== '.') {
+        if (!$this->is_writable() or !$this->lf->is_directory()) {
             return null;
         }
 
@@ -162,7 +158,7 @@ class file_info_stored extends file_info {
 
 
     public function create_file_from_string($newfilename, $content, $userid=null) {
-        if (!$this->is_writable() or $this->lf->get_filename() !== '.') {
+        if (!$this->is_writable() or !$this->lf->is_directory()) {
             return null;
         }
 
@@ -194,7 +190,7 @@ class file_info_stored extends file_info {
     }
 
     public function create_file_from_pathname($newfilename, $pathname, $userid=null) {
-        if (!$this->is_writable() or $this->lf->get_filename() !== '.') {
+        if (!$this->is_writable() or !$this->lf->is_directory()) {
             return null;
         }
 
@@ -258,7 +254,7 @@ class file_info_stored extends file_info {
     }
 
     public function delete() {
-        if (!$this->lf or !$this->is_writable()) {
+        if (!$this->is_writable()) {
             return false;
         }
 
diff --git a/lib/file/virtual_root_file.php b/lib/file/virtual_root_file.php
new file mode 100644
index 0000000000..9fc53b385f
--- /dev/null
+++ b/lib/file/virtual_root_file.php
@@ -0,0 +1,173 @@
+<?php  //$Id$
+
+/**
+ * Root directory in empty file area
+ */
+class virtual_root_file {
+    protected $contextid;
+    protected $filearea;
+    protected $itemid;
+
+    /**
+     * Constructor
+     */
+    public function __construct($contextid, $filearea, $itemid) {
+        $this->contextid = $contextid;
+        $this->filearea  = $filearea;
+        $this->itemid    = $itemid;
+    }
+
+    /**
+     * Is this a directory?
+     * @return bool
+     */
+    public function is_directory() {
+        return true;
+    }
+
+    /**
+     * Delete file
+     * @return success
+     */
+    public function delete() {
+        return true;
+    }
+
+    /**
+    * adds this file path to a curl request (POST only)
+    *
+    * @param curl $curlrequest the curl request object
+    * @param string $key what key to use in the POST request
+    */
+    public function add_to_curl_request(&$curlrequest, $key) {
+        return;
+    }
+
+    /**
+     * Returns file handle - read only mode, no writing allowed into pool files!
+     * @return file handle
+     */
+    public function get_content_file_handle() {
+        return null;
+    }
+
+    /**
+     * Dumps file content to page
+     * @return file handle
+     */
+    public function readfile() {
+        return;
+    }
+
+    /**
+     * Returns file content as string
+     * @return string content
+     */
+    public function get_content() {
+        return '';
+    }
+
+    /**
+     * Copy content of file to give npathname
+     * @param string $pathnema rela path to new file
+     * @return bool success
+     */
+    public function copy_content_to($pathname) {
+        return false;
+    }
+
+    /**
+     * List contents of archive
+     * @param object $file_packer
+     * @return array of file infos
+     */
+    public function list_files(file_packer $packer) {
+        return null;
+    }
+
+    /**
+     * Extract file to given file path (real OS filesystem), existing files are overwrited
+     * @param object $file_packer
+     * @param string $pathname target directory
+     * @return mixed list of processed files; false if error
+     */
+    public function extract_to_pathname(file_packer $packer, $pathname) {
+        return false;
+    }
+
+    /**
+     * Extract file to given file path (real OS filesystem), existing files are overwrited
+     * @param object $file_packer
+     * @param int $contextid
+     * @param string $filearea
+     * @param int $itemid
+     * @param string $pathbase
+     * @param int $userid
+     * @return mixed list of processed files; false if error
+     */
+    public function extract_to_storage(file_packer $packer, $contextid, $filearea, $itemid, $pathbase, $userid=null) {
+        return false;
+    }
+
+    /**
+     * Add file/directory into archive
+     * @param object $filearch
+     * @param string $archivepath pathname in archive
+     * @return bool success
+     */
+    public function archive_file(file_archive $filearch, $archivepath) {
+        return false;
+    }
+
+    public function get_contextid() {
+        return $this->contextid;
+    }
+
+    public function get_filearea() {
+        return $this->filearea;
+    }
+
+    public function get_itemid() {
+        return $this->itemid;
+    }
+
+    public function get_filepath() {
+        return '/';
+    }
+
+    public function get_filename() {
+        return '.';
+    }
+
+    public function get_userid() {
+        return null;
+    }
+
+    public function get_filesize() {
+        return 0;
+    }
+
+    public function get_mimetype() {
+        return null;
+    }
+
+    public function get_timecreated() {
+        return 0;
+    }
+
+    public function get_timemodified() {
+        return 0;
+    }
+
+    public function get_status() {
+        return 0;
+    }
+
+    public function get_id() {
+        return 0;
+    }
+
+    public function get_contenthash() {
+        return sha1('');
+    }
+}
-- 
2.39.5