From 5914a4c223168a44ad84b7552b295e87fbacc9aa Mon Sep 17 00:00:00 2001 From: nedlir Date: Wed, 2 Mar 2022 01:24:28 +0200 Subject: [PATCH] Java FX initial structure --- .gitignore | 3 +- Controller.java | 185 +++++++++++++++++++++++++++++++++++++++++++ FileManipulator.java | 14 ++-- OfficerBreaker.java | 32 ++++++++ View.fxml | 34 ++++++++ XMLParser.java | 1 + img/excel.png | Bin 0 -> 102683 bytes img/opening.png | Bin 0 -> 1035638 bytes img/powerpoint.png | Bin 0 -> 119555 bytes img/word.png | Bin 0 -> 99159 bytes 10 files changed, 261 insertions(+), 8 deletions(-) create mode 100644 Controller.java create mode 100644 OfficerBreaker.java create mode 100644 View.fxml create mode 100644 img/excel.png create mode 100644 img/opening.png create mode 100644 img/powerpoint.png create mode 100644 img/word.png diff --git a/.gitignore b/.gitignore index 08962db..84200f8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ test.xml Tester.java Testing.java tempCodeRunnerFile.java -makefile \ No newline at end of file +makefile +*.class \ No newline at end of file diff --git a/Controller.java b/Controller.java new file mode 100644 index 0000000..5ed1512 --- /dev/null +++ b/Controller.java @@ -0,0 +1,185 @@ +package officerunlocker; + +import java.awt.Desktop; +import java.io.File; +import javafx.scene.image.Image; +import java.io.FileInputStream; +import java.io.InputStream; +import java.net.URL; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.geometry.Insets; +import javafx.scene.image.ImageView; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.Background; +import javafx.scene.layout.BackgroundFill; +import javafx.scene.layout.CornerRadii; +import javafx.scene.paint.Color; +import javafx.scene.text.Text; +import javafx.stage.FileChooser; + +public class Controller { + + private final String EXCEL = "xlsx"; + private final String POWERPOINT = "pptx"; + private final String WORD = "docx"; + + @FXML + private AnchorPane anchorPane; + + @FXML + private Text filePathText; + private File file; + private FileChooser fileChooser; + private String filePath; + private String fileName; + private String fileType; + + @FXML + private ImageView image; + private InputStream stream; + private Image wordImage; + private Image excelImage; + private Image powerpointImage; + private Image openingImage; + + // opening screen instructions + @FXML + private Text textInstructions; + + // opening screen title + @FXML + private Text textTitle; + + @FXML + private Text invalidFileText; + + @FXML + public void initialize() { + anchorPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY))); + invalidFileText.setVisible(false); + + textInstructions.setText(instructions()); + + try { + openingImage = new Image(getClass().getResourceAsStream("img/opening.png")); + + + wordImage = new Image(getClass().getResourceAsStream("img/word.png")); + + powerpointImage = new Image(getClass().getResourceAsStream("img/powerpoint.png")); + + excelImage = new Image(getClass().getResourceAsStream("img/excel.png")); + + image.setImage(openingImage); + image.setVisible(true); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + @FXML + void browsePressed(ActionEvent event) { + + // remove text from screen + textTitle.setVisible(false); + textInstructions.setVisible(false); + invalidFileText.setVisible(false); + + fileChooser = new FileChooser(); + fileChooser.setTitle("Please Select a File"); + file = fileChooser.showOpenDialog(null); + filePath = file.toString(); + fileName = file.getName(); + filePathText.setText(filePath); + fileType = getFileType(); + + if (fileType != null) { + setImageByFileType(); + + } + } + + @FXML + void removePasswordPressed(ActionEvent event) { + if (file == null) { + return; + } + if (!isFileSupported()) { + invalidFileText.setVisible(true); + return; + } + try { + FileManipulator manipulator = new FileManipulator(filePath, "./"); // create new object to manipulate the file + + manipulator.extractFile(); // extract XML from file + + XMLParser parser = new XMLParser(manipulator); // create new object to parse the XML + + if (parser.isExistSecurityElement()) // if the file has a password + { + parser.removeElementOfSecurity(); // remove hash node from XML file + + parser.writeToXMLFile(); // write to XML the change + + manipulator.insertFile(); // write back to file + + manipulator.removeXMLFile(); + + System.out.println("Finished"); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + @FXML + void openCioccolatodorimaURL(ActionEvent event) { + try { + Desktop.getDesktop().browse(new URL("https://twitter.com/cioccolato_kun").toURI()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private boolean isFileSupported() { + if (fileType.equals(EXCEL) || fileType.equals(WORD) || fileType.equals(POWERPOINT)) { + return true; + } else { + return false; + } + } + + private void setImageByFileType() { + switch (fileType) { + case EXCEL: + image.setImage(excelImage); + break; + case WORD: + image.setImage(wordImage); + break; + case POWERPOINT: + image.setImage(powerpointImage); + break; + default: + break; + } + } + + private String getFileType() { + int indexOfLastDot = this.fileName.lastIndexOf("."); + if (indexOfLastDot == -1) // dot not found, invalid file + { + return null; + } + return this.fileName.substring(indexOfLastDot + 1); + } + + private String instructions() { + return "Welcome to Office Breaker!\n" + "This tool is deisgned to help remove read-only protection from .pptx, .xlsx. .docx filetypes."; + } + +} diff --git a/FileManipulator.java b/FileManipulator.java index 6a10d1b..bb0b241 100644 --- a/FileManipulator.java +++ b/FileManipulator.java @@ -1,23 +1,18 @@ +package officerunlocker; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Enumeration; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; public class FileManipulator { public final static String EXCEL_FILE = "xlsx"; public final static String POWER_POINT_FILE = "pptx"; - public final static String WORD_FILE = "pptx"; + public final static String WORD_FILE = "docx"; public final static String EXCEL_XML = "workbook.xml"; public final static String POWER_POINT_XML = "presentation.xml"; @@ -103,6 +98,11 @@ public class FileManipulator { } } + public void removeXMLFile() throws Exception { + File file = new File(this.XMLTargetFile); + Files.deleteIfExists(file.toPath()); + } + public String getXMLTargetFile() { return this.XMLTargetFile; } diff --git a/OfficerBreaker.java b/OfficerBreaker.java new file mode 100644 index 0000000..7edf892 --- /dev/null +++ b/OfficerBreaker.java @@ -0,0 +1,32 @@ +package officerunlocker; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +/** + * + * @author nedlir + */ +public class OfficerBreaker extends Application { + + @Override + public void start(Stage stage) throws Exception { + Parent root = FXMLLoader.load(getClass().getResource("View.fxml")); + + Scene scene = new Scene(root); + + stage.setScene(scene); + stage.show(); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + launch(args); + } + +} diff --git a/View.fxml b/View.fxml new file mode 100644 index 0000000..170dc8c --- /dev/null +++ b/View.fxml @@ -0,0 +1,34 @@ + + + + + + + + + + + +