Skip to content
Snippets Groups Projects
file.php 4.74 KiB
Newer Older
<?php

class FileController extends PluginController
{

    function validate_args(&$args, $types = NULL)
    {
        reset($args);
    }

    /**
     * The action for editing a file reference.
     */
    public function edit_action($file_ref_id)
    {
        $file_id = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], "plugins.php/NextcloudPlugin/file/edit/") + strlen("plugins.php/NextcloudPlugin/file/edit/"));
        if (strpos($file_id, "?") !== false) {
            $file_id = substr($file_id, 0, strpos($file_id, "?"));
        }
        $plugin = PluginManager::getInstance()->getPlugin("NextcloudPlugin");

        $this->file = $plugin->getPreparedFile($file_id);
        $this->from_plugin = Request::get("from_plugin");


        $this->folder = $this->file->getFoldertype();

        if (!$this->folder || !$this->folder->isFileEditable($this->file->getId(), $GLOBALS['user']->id)) {
            throw new AccessDeniedException();
        }


        if (Request::isPost()) {
            //form was sent
            CSRFProtection::verifyUnsafeRequest();
            $this->errors = [];

            $this->name = trim(Request::get('name'));

            $result = $this->folder->editFile(
                $file_id,
                $this->name
            );

            if (!($result instanceof FileType)) {
                $this->errors = array_merge($this->errors, $result);
            }

            if ($this->errors) {
                PageLayout::postError(
                    sprintf(
                        dgettext('NextcloudPlugin', 'Ein Fehler trat auf beim Ändern der Datei „%s“.'),
                        htmlReady($this->file->getFilename())
                    ),
                    $this->errors
                );
            } else {
                PageLayout::postSuccess(dgettext('NextcloudPlugin', 'Die Änderungen wurden gespeichert.'));
                $this->redirect(URLHelper::getURL("dispatch.php/files/system/".$this->plugin->getPluginId()."/".$this->folder->getId()));
            }
        }

        $this->name = $this->file->getFilename();

        $tf = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH']."/app/views");
        $this->aside_template = $tf->open("file/_file_aside.php");
        $this->aside_template->file = $this->file;
    }

    /**
     * This action is responsible for updating a file reference.
     */
    public function update_action($file_ref_id)
    {
        $file_id = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], "plugins.php/NextcloudPlugin/file/update/") + strlen("plugins.php/NextcloudPlugin/file/update/"));
        if (strpos($file_id, "?") !== false) {
            $file_id = substr($file_id, 0, strpos($file_id, "?"));
        }
        $plugin = PluginManager::getInstance()->getPlugin(Request::get("from_plugin"));
        if (!$plugin) {
            throw new Trails_Exception(404, dgettext('NextcloudPlugin', 'Das Plugin existiert nicht.'));
        }

        $this->file = $plugin->getPreparedFile($file_id);
        $this->from_plugin = Request::get("from_plugin");
        $this->folder = $this->file->getFolderType();

        if (!$this->file->isEditable($GLOBALS['user']->id)) {
            throw new AccessDeniedException();
        }

        $this->errors = [];

        if (Request::submitted('confirm')) {
            CSRFProtection::verifyUnsafeRequest();

            //Form was sent
            if (Request::isPost() && is_array($_FILES['file'])) {

                $this->file->update($_FILES['file']['tmp_name']);

                if (Request::submitted("update_filename")) {
                    $new_name = ($this->folder->id ? $this->folder->id."/" : "").$_FILES['file']['name'];
                    $this->folder->editFile(
                        $file_id,
                        $new_name
                    );
                }
            } else {
                $this->errors[] = dgettext('NextcloudPlugin', 'Es wurde keine neue Dateiversion gewählt.');
            }

            if ($this->errors) {
                PageLayout::postError(
                    sprintf(
                        dgettext('NextcloudPlugin', 'Ein Fehler trat auf beim Aktualisieren der Datei „%s“.'),
                        htmlReady($this->file->getFilename())
                    ),
                    $this->errors
                );
            } else {
                PageLayout::postSuccess(
                    sprintf(
                        dgettext('NextcloudPlugin', 'Die Datei „%s“ wurde aktualisiert!'),
                        htmlReady($this->file->getFilename())
                    )
                );
            }
            $this->redirect(URLHelper::getURL("dispatch.php/files/system/".$this->plugin->getPluginId()."/".$this->folder->getId()));
        }
    }
}