/**
* OLAT - Online Learning and Training
* http://www.olat.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),
* University of Zurich, Switzerland.
*
*/
package org.olat.core.commons.modules.bc.commands;
import java.util.ArrayList;
import java.util.List;
import org.olat.core.commons.modules.bc.FileSelection;
import org.olat.core.commons.modules.bc.FolderEvent;
import org.olat.core.commons.modules.bc.components.FolderComponent;
import org.olat.core.commons.services.vfs.VFSRepositoryService;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.form.flexible.FormItemContainer;
import org.olat.core.gui.components.form.flexible.elements.TextElement;
import org.olat.core.gui.components.form.flexible.impl.FormBasicController;
import org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.translator.Translator;
import org.olat.core.logging.AssertException;
import org.olat.core.util.FileUtils;
import org.olat.core.util.ZipUtil;
import org.olat.core.util.vfs.VFSConstants;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem;
import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.filters.VFSSystemItemFilter;
import org.springframework.beans.factory.annotation.Autowired;
/**
*
* Description:
* Provides a CreateItemForm and creates a zip file if input valid.
*
*
* Initial Date: 30.01.2008
* @author Lavinia Dumitrescu
*/
public class CmdZip extends FormBasicController implements FolderCommand {
private int status = FolderCommandStatus.STATUS_SUCCESS;
private VFSContainer currentContainer;
private FileSelection selection;
private TextElement textElement;
@Autowired
private VFSRepositoryService vfsRepositoryService;
protected CmdZip(UserRequest ureq, WindowControl wControl) {
super(ureq, wControl);
}
@Override
public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wControl, Translator trans) {
setTranslator(trans);
currentContainer = folderComponent.getCurrentContainer();
if (currentContainer.canWrite() != VFSConstants.YES) {
throw new AssertException("Cannot write to current folder.");
}
status = FolderCommandHelper.sanityCheck(wControl, folderComponent);
if(status == FolderCommandStatus.STATUS_FAILED) {
return null;
}
// BUG: CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
// selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
// FIXED:
selection = new FileSelection(ureq, folderComponent.getCurrentContainer(), folderComponent.getCurrentContainerPath());
status = FolderCommandHelper.sanityCheck3(wControl, folderComponent, selection);
if(status == FolderCommandStatus.STATUS_FAILED) {
return null;
}
if(selection.getFiles().isEmpty()) {
status = FolderCommandStatus.STATUS_FAILED;
wControl.setWarning(trans.translate("warning.file.selection.empty"));
return null;
}
initForm(ureq);
return this;
}
@Override
protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
String files = selection.renderAsHtml();
uifactory.addStaticExampleText("zip.confirm", files, formLayout);
textElement = uifactory.addTextElement("fileName", "zip.name", 20, "", formLayout);
textElement.setMandatory(true);
uifactory.addStaticTextElement("extension", null, translate("zip.extension"), formLayout);
FormLayoutContainer formButtons = FormLayoutContainer.createButtonLayout("formButton", getTranslator());
formLayout.add(formButtons);
uifactory.addFormSubmitButton("submit","zip.button", formButtons);
uifactory.addFormCancelButton("cancel", formButtons, ureq, getWindowControl());
}
@Override
protected void doDispose() {
// nothing to do
}
@Override
public int getStatus() {
return status;
}
@Override
public boolean runsModal() {
return false;
}
@Override
public String getModalTitle() {
return translate("zip.header");
}
@Override
protected void formCancelled(UserRequest ureq) {
status = FolderCommandStatus.STATUS_CANCELED;
fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
}
/**
* Creates a zipFile by using ZipUtil and fires Event.DONE_EVENT if successful.
*/
@Override
protected void formOK(UserRequest ureq) {
String name = textElement.getValue();
if(!name.toLowerCase().endsWith(".zip")) {
name += ".zip";
}
VFSLeaf zipFile = currentContainer.createChildLeaf(name);
if (zipFile == null) {
fireEvent(ureq, Event.FAILED_EVENT);
return;
}
List