Ejemplo n.º 1
0
 public function url_for_parent_directory($entry, $parent_id = null)
 {
     if (is_array($entry)) {
         $entry = reset($entry);
     }
     if (!is_object($entry)) {
         $entry = new DirectoryEntry($entry);
     }
     $parent_id = $parent_id ?: FileHelper::getParentId($entry->id) ?: $this->context_id;
     $parent_page = $this->getPageForIndex($entry->indexInparent());
     return $this->url_for('document/files/index/' . $parent_id . '/' . $parent_page);
 }
  /**
   * @param array $entries
   * @return DirectoryEntries
   * @throws \InvalidArgumentException
   */
  public static function fromArray($entries) {
    $cleanEntries = array();

    foreach($entries as $entry) {
      if(!isset($entry['id'], $entry['label'])) {
        throw new \InvalidArgumentException(DirectoryEntry::MSG_ID_LABEL_MANDATORY);
      }
      $cleanEntries[] = $clean = new DirectoryEntry($entry['id'], $entry['label']);

      isset($entry['obsolete']) ? $clean->setObsolete($entry['obsolete']):null;
      isset($entry['ordering']) ? $clean->setOrdering($entry['ordering']):null;
    }

    return new DirectoryEntries($cleanEntries);
  }
Ejemplo n.º 3
0
 /**
  * Returns the Parent from an Entry.
  *
  * @return DirectoryEntry Parent entry
  * @throws Exception if no valid parent is found
  */
 public function getParent()
 {
     $entries = DirectoryEntry::findByFile_id($this->parent_id);
     if (count($entries) === 0) {
         throw new Exception('No parent found');
     }
     return $entries[0];
 }
Ejemplo n.º 4
0
 /**
  * Checks whether the user may access a file or a bunch of files if you
  * pass an array.
  *
  * @param mixed $files Either a single directory entry id or an array of those
  *                     (DirectoryEntry or File object(s) are also valid)
  * @param mixed $user_id Id of the user or null for current user (default)
  * @param bool $throw_exception Throw an AccessDeniedException instead of
  *                              returning false
  * @return bool indicating whether the user may access this file/these files
  * @throws AccessDeniedException if $throw_exception is true and the user
  *                               may not access the file(s)
  */
 public static function CheckAccess($files, $user_id = null, $throw_exception = true)
 {
     if (!is_array($files)) {
         $files = array($files);
     }
     $user_id = $user_id ?: $GLOBALS['user']->id;
     foreach ($files as $file) {
         try {
             if (!is_object($file)) {
                 $file = DirectoryEntry::find($file);
             }
             if ($file instanceof DirectoryEntry) {
                 $file = $file->file;
             }
             if (!$file instanceof File || !$file->checkAccess()) {
                 throw new Exception();
             }
         } catch (Exception $e) {
             if (!is_object($file) && ($file === $GLOBALS['user']->id || $GLOBALS['perm']->have_perm('root'))) {
                 continue;
             }
             if ($throw_exception) {
                 throw new AccessDeniedException(_('Sie dürfen auf dieses Objekt nicht zugreifen.'));
             }
             return false;
         }
     }
     return true;
 }
Ejemplo n.º 5
0
 /**
  * Downloads a single file.
  *
  * @param DirectoryEntry $entry  Directory entry to download
  * @param bool           $inline Download as inline
  */
 protected function download_file(DirectoryEntry $entry, $inline)
 {
     $file = $entry->file;
     $storage = $file->getStorageObject();
     if (!$storage->exists() || !$storage->isReadable()) {
         throw new Exception('Cannot access file "' . $storage->getPath() . '"');
     }
     $entry->downloads += 1;
     $entry->store();
     Metrics::increment('core.personal_files.downloads');
     $this->initiateDownload($inline, $file->filename, $file->mime_type, $file->size, $storage->open('r'));
 }
Ejemplo n.º 6
0
 public function __construct($path, $siteId = null)
 {
     parent::__construct($path, $siteId);
 }
Ejemplo n.º 7
0
 public function __construct($path)
 {
     parent::__construct($path);
 }
Ejemplo n.º 8
0
 /**
  * Deletes a file.
  *
  * @param String $id Directory entry id of the file to delete
  */
 public function delete_action($id)
 {
     if (!$this->full_access) {
         throw new AccessDeniedException();
     }
     $entry = DirectoryEntry::find($id);
     $parent_id = FileHelper::getParentId($id) ?: $this->context_id;
     $entry->checkAccess();
     if (!Request::isPost()) {
         $question = createQuestion2(_('Soll die Datei wirklich gelöscht werden?'), array(), array(), $this->url_for('document/files/delete/' . $id));
         $this->flash['question'] = $question;
     } elseif (Request::isPost() && Request::submitted('yes')) {
         File::get($entry->directory->id)->unlink($entry->name);
         PageLayout::postMessage(MessageBox::success(_('Die Datei wurde gelöscht.')));
     }
     $this->redirect('document/files/index/' . $parent_id);
 }
Ejemplo n.º 9
0
 /**
  * Converts URLs in images so that the webserver can access them without proxy.
  * @param string $url of an image
  * @return string " src=\"".$converted_url."\""
  */
 protected function convertURL($url)
 {
     $convurl = $url;
     $url_elements = @parse_url($url);
     $url = $url_elements['path'] . '?' . $url_elements['query'];
     if (strpos(implode('#', $this->domains), $url_elements['host']) !== false) {
         if (strpos($url, 'dispatch.php/media_proxy?url=') !== false) {
             $targeturl = urldecode(substr($url, 4));
             try {
                 // is file in cache?
                 if (!($metadata = $this->media_proxy->getMetaData($targeturl))) {
                     $convurl = $targeturl;
                 } else {
                     $convurl = $this->config->getValue('MEDIA_CACHE_PATH') . '/' . md5($targeturl);
                 }
             } catch (Exception $e) {
                 $convurl = '';
             }
         } else {
             if (stripos($url, 'dispatch.php/document/download') !== false) {
                 if (preg_match('#([a-f0-9]{32})#', $url, $matches)) {
                     $convurl = DirectoryEntry::find($matches[1])->file->getStorageObject()->getPath();
                 }
             } else {
                 if (stripos($url, 'download') !== false || stripos($url, 'sendfile.php') !== false) {
                     //// get file id
                     if (preg_match('#([a-f0-9]{32})#', $url, $matches)) {
                         $document = new StudipDocument($matches[1]);
                         if ($document->checkAccess($GLOBALS['user']->id)) {
                             $convurl = get_upload_file_path($matches[1]);
                         } else {
                             $convurl = Assets::image_path('messagebox/exception.png');
                         }
                     }
                 }
             }
         }
     }
     return 'src="' . $convurl . '"';
 }
Ejemplo n.º 10
0
 private static function directoryTreeBuilder($dir, $ignore, $mode = Merville::LIVE_MODE, $parents = null)
 {
     if ($dh = opendir($dir)) {
         $node = new DirectoryEntry($dir, $parents);
         $new_parents = $parents;
         if (is_null($new_parents)) {
             $new_parents = array();
         } else {
             $new_parents[] = $node;
         }
         while (($entry = readdir($dh)) !== false) {
             if ($entry == '.' || $entry == '..') {
                 continue;
             }
             $path = $dir . DIRECTORY_SEPARATOR . $entry;
             if (is_dir($path) && in_array($entry, $ignore['folders'])) {
                 continue;
             }
             if (!is_dir($path) && in_array($entry, $ignore['files'])) {
                 continue;
             }
             $file_details = static::pathinfo($path);
             if (is_dir($path)) {
                 $entry = static::directoryTreeBuilder($path, $ignore, $mode, $new_parents);
             } elseif (in_array($file_details['extension'], Merville::$VALID_MARKDOWN_EXTENSIONS)) {
                 $entry = new DirectoryEntry($path, $new_parents);
                 if ($mode === Merville::STATIC_MODE) {
                     $entry->uri .= '.html';
                 }
             }
             if ($entry instanceof DirectoryEntry) {
                 $node->value[$entry->uri] = $entry;
             }
         }
         $node->sort();
         $node->first_page = $node->get_first_page();
         $index_key = $mode === Merville::LIVE_MODE ? 'index' : 'index.html';
         if (isset($node->value[$index_key])) {
             $node->value[$index_key]->first_page = $node->first_page;
             $node->index_page = $node->value[$index_key];
         } else {
             $node->index_page = false;
         }
         return $node;
     }
 }
Ejemplo n.º 11
0
 /**
  * Deletes a folder.
  *
  * @param String $folder_id Directory entry id of the folder
  */
 public function delete_action($folder_id)
 {
     if (!$this->full_access) {
         throw new AccessDeniedException();
     }
     FileHelper::checkAccess($folder_id);
     $parent_id = FileHelper::getParentId($folder_id) ?: $this->context_id;
     if (!Request::isPost()) {
         $message = $folder_id === 'all' ? _('Soll der gesamte Dateibereich inklusive aller Order und Dateien wirklich gelöscht werden?') : _('Soll der Ordner inklusive aller darin enthaltenen Dateien wirklich gelöscht werden?');
         $question = createQuestion2($message, array(), array(), $this->url_for('document/folder/delete/' . $folder_id));
         $this->flash['question'] = $question;
     } elseif (Request::isPost() && Request::submitted('yes')) {
         if ($folder_id === 'all') {
             $entry = RootDirectory::find($this->context_id);
             foreach ($entry->listFiles() as $file) {
                 $entry->unlink($file->name);
             }
             PageLayout::postMessage(MessageBox::success(_('Der Dateibereich wurde geleert.')));
         } else {
             $entry = DirectoryEntry::find($folder_id);
             $entry->directory->unlink($entry->name);
             PageLayout::postMessage(MessageBox::success(_('Der Ordner wurde gelöscht.')));
         }
     }
     $this->redirect('document/files/index/' . $parent_id);
 }
Ejemplo n.º 12
0
 /**
  * Create a new entry in this directory for the given file
  * under the given name. This will increase the link count
  * of the file by one.
  *
  * @param File $file    file to link
  * @param string $name  new file name
  * @param string $description optional description
  *
  * @return DirectoryEntry  created DirectoryEntry object
  */
 public function link(File $file, $name, $description = '')
 {
     $name = FileHelper::CompressFilename($name);
     $name = $this->ensureUniqueFilename($name, $file);
     $entry = new DirectoryEntry();
     $entry->file_id = $file->id;
     $entry->parent_id = $this->file_id;
     $entry->name = $name;
     $entry->description = $description;
     $entry->store();
     return $entry;
 }