<commit_before><?php

require_once '../repository/UserRepository.php';
require_once '../repository/FileRepository.php';
require_once '../repository/UserFileRepository.php';

/**
* Siehe Dokumentation im DefaultController.
*/
class UserController
{
  public function index()
  {
    $userRepository = new UserRepository();

    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }

    if (!empty($_GET['name'])) {
      $folderName = $_GET['name'];
    } else {
        $folderName = $_SESSION['username'];
    }

    $folderFragments = explode("/", $folderName);
    if (sizeof($folderFragments) > 1) {
      $displayFolderName = $folderFragments[sizeof($folderFragments) - 1]; //this name is only used for displaying
    } else {
      $displayFolderName = $_SESSION['username'];
    }

    $view = new View('user_index');
    $view->title = 'RUN';

    $userRepository = new UserRepository();
    $uid = $userRepository->getId($_SESSION['username']);
    $uid = $uid->id;

    $userFileRepository = new UserFileRepository();
    if (!empty($userFileRepository->getFileIds($uid))) {
      $fids = $userFileRepository->getFileIds($uid);

      $fileRepository = new FileRepository();
      $files = array();

      foreach ($fids as $fid) {
        $files[] = $fileRepository->readById($fid->file_id);
      }

      foreach ($files as $key => $file) {
        //get correct subfolder
        $pathFragments = explode("/", $file->path);
        $pathSize = sizeof($pathFragments);
        $fileSubFolder = $pathFragments[$pathSize - 2];
        if ($displayFolderName !== $fileSubFolder) {
            unset($files[$key]);
        }
      }

      $view->folderName = $folderName; //set subfolder for view
      $view->files = $files;
    }
    $view->display();
  }

  public function folder() {
    $userRepository = new UserRepository();

    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }

    if (!empty($_GET['name'])) {
      $folderName = $_GET['name'];
    } else {
        $this->error('user_index', 'RUN', "Something went wrong");
    }

    $folderFragments = explode("/", $folderName);
    if (sizeof($folderFragments) > 1) {
      $displayFolderName = $folderFragments[sizeof($folderFragments) - 1]; //this name is only used for displaying
    } else {
      $displayFolderName = $folderName;
    }

    $view = new View('user_index');
    $view->title = 'RUN';

    $userRepository = new UserRepository();
    $uid = $userRepository->getId($_SESSION['username']);
    $uid = $uid->id;

    $userFileRepository = new UserFileRepository();
    if (!empty($userFileRepository->getFileIds($uid))) {
      $fids = $userFileRepository->getFileIds($uid);

      $fileRepository = new FileRepository();
      $files = array();

      foreach ($fids as $fid) {
        $files[] = $fileRepository->readById($fid->file_id);
      }

      foreach ($files as $key => $file) {
        //get correct subfolder
        $pathFragments = explode("/", $file->path);
        $pathSize = sizeof($pathFragments);
        $fileSubFolder = $pathFragments[$pathSize - 2];
        if ($displayFolderName !== $fileSubFolder) {
            unset($files[$key]);
        }
      }

      $view->folderName = $folderName; //set subfolder for view
      $view->files = $files;
    }
    $view->display();
  }

  public function create()
  {
    $view = new View('user_create');
    $view->title = 'Register';
    $view->display();
  }

  public function doCreate()
  {
    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }
    if ($_POST['Submit']) {
      $username = $_POST['username'];
      $password = $_POST['password'];
      $passwordRepeat = $_POST['passwordRepeat'];

      if ($username == "" || empty($username) || $password == "" || empty($password)){
        $this->error('user_create', 'Register', "Name and password can't be empty.");
      }

      if ($password !== $passwordRepeat) {
        $this->error('user_create', 'Register', 'Passwords have to match.');
      }

      $userRepository = new UserRepository();

      if (!empty($userRepository->getId($username))) {
        //user with that name already exists
        $this->error('user_create', 'Register', 'A User with that name already exists.');
      }
      $userRepository->create($username, $password);
      $_SESSION['username'] = $username;
      header('Location: /user');
    }
  }

  public function user_settings() {
    $username = $_SESSION['username'];

    $userRepository = new UserRepository();
    $user_id = $userRepository->getId($username);
    $user = $userRepository->readById($user_id->id);

    $view = new View('user_setting');
    $view->user = $user;
    $view->displayOnly();
  }

  public function doEdit()
  {
    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }

    $newUsername = htmlspecialchars($_POST['username']);
    $newPassword = $_POST['password'];
    $repPassword = $_POST['repeat'];

    $userRepository = new UserRepository();
    $id = $userRepository->getId($_SESSION['username']);
    $id = $id->id;

    if($newPassword !== $repPassword) {
      $this->error('user_settings', 'Edit', 'Passwords have to match.');
    }

    $userRepository = new UserRepository();
    $userRepository->changeUser($id, $newUsername, $newPassword);

    header('Location: /user');
  }

  public function delete()
  {
    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }
<commit_msg>Added change-File function<commit_after><?php

require_once '../repository/UserRepository.php';
require_once '../repository/FileRepository.php';
require_once '../repository/UserFileRepository.php';

/**
* Siehe Dokumentation im DefaultController.
*/
class UserController
{
  public function index()
  {
    $userRepository = new UserRepository();

    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }

    if (!empty($_GET['name'])) {
      $folderName = $_GET['name'];
    } else {
        $folderName = $_SESSION['username'];
    }

    $folderFragments = explode("/", $folderName);
    if (sizeof($folderFragments) > 1) {
      $displayFolderName = $folderFragments[sizeof($folderFragments) - 1]; //this name is only used for displaying
    } else {
      $displayFolderName = $_SESSION['username'];
    }

    $view = new View('user_index');
    $view->title = 'RUN';

    $userRepository = new UserRepository();
    $uid = $userRepository->getId($_SESSION['username']);
    $uid = $uid->id;

    $userFileRepository = new UserFileRepository();
    if (!empty($userFileRepository->getFileIds($uid))) {
      $fids = $userFileRepository->getFileIds($uid);

      $fileRepository = new FileRepository();
      $files = array();

      foreach ($fids as $fid) {
        $files[] = $fileRepository->readById($fid->file_id);
      }

      foreach ($files as $key => $file) {
        //get correct subfolder
        $pathFragments = explode("/", $file->path);
        $pathSize = sizeof($pathFragments);
        $fileSubFolder = $pathFragments[$pathSize - 2];
        if ($displayFolderName !== $fileSubFolder) {
            unset($files[$key]);
        }
      }

      $view->folderName = $folderName; //set subfolder for view
      $view->files = $files;
    }
    $view->display();
  }

  public function folder() {
    $userRepository = new UserRepository();

    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }

    if (!empty($_GET['name'])) {
      $folderName = $_GET['name'];
    } else {
        $this->error('user_index', 'RUN', "Something went wrong");
    }

    $folderFragments = explode("/", $folderName);
    if (sizeof($folderFragments) > 1) {
      $displayFolderName = $folderFragments[sizeof($folderFragments) - 1]; //this name is only used for displaying
    } else {
      $displayFolderName = $folderName;
    }

    $view = new View('user_index');
    $view->title = 'RUN';

    $userRepository = new UserRepository();
    $uid = $userRepository->getId($_SESSION['username']);
    $uid = $uid->id;

    $userFileRepository = new UserFileRepository();
    if (!empty($userFileRepository->getFileIds($uid))) {
      $fids = $userFileRepository->getFileIds($uid);

      $fileRepository = new FileRepository();
      $files = array();

      foreach ($fids as $fid) {
        $files[] = $fileRepository->readById($fid->file_id);
      }

      foreach ($files as $key => $file) {
        //get correct subfolder
        $pathFragments = explode("/", $file->path);
        $pathSize = sizeof($pathFragments);
        $fileSubFolder = $pathFragments[$pathSize - 2];
        if ($displayFolderName !== $fileSubFolder) {
            unset($files[$key]);
        }
      }

      $view->folderName = $folderName; //set subfolder for view
      $view->files = $files;
    }
    $view->display();
  }

  public function create()
  {
    $view = new View('user_create');
    $view->title = 'Register';
    $view->display();
  }

  public function doCreate()
  {
    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }
    if ($_POST['Submit']) {
      $username = $_POST['username'];
      $password = $_POST['password'];
      $passwordRepeat = $_POST['<PASSWORD>'];

      if ($username == "" || empty($username) || $password == "" || empty($password)){
        $this->error('user_create', 'Register', "Name and password can't be empty.");
      }

      if ($password !== $passwordRepeat) {
        $this->error('user_create', 'Register', 'Passwords have to match.');
      }

      $userRepository = new UserRepository();

      if (!empty($userRepository->getId($username))) {
        //user with that name already exists
        $this->error('user_create', 'Register', 'A User with that name already exists.');
      }
      $userRepository->create($username, $password);
      $_SESSION['username'] = $username;
      header('Location: /user');
    }
  }

  public function user_settings() {
    $username = $_SESSION['username'];

    $userRepository = new UserRepository();
    $user_id = $userRepository->getId($username);
    $user = $userRepository->readById($user_id->id);

    $view = new View('user_setting');
    $view->user = $user;
    $view->displayOnly();
  }

  public function doEdit()
  {
    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }

    $newUsername = htmlspecialchars($_POST['username']);
    $newPassword = $_POST['password'];
    $repPassword = $_POST['<PASSWORD>'];

    $userRepository = new UserRepository();
    $id = $userRepository->getId($_SESSION['username']);
    $id = $id->id;

    if($newPassword !== $repPassword) {
      $this->error('user_settings', 'Edit', 'Passwords have to match.');
    }

    $userRepository = new UserRepository();
    $userRepository->changeUser($id, $newUsername, $newPassword);

    header('Location: /user');
  }

  public function doUpdate() {
      if (!isset($_SESSION['username'])){
        $this->error('user_login', 'Login', 'Acces denied.');
      }

      if (isset($_POST['submit'])) {
        $newName = htmlspecialchars($_POST['filename']);

        if (empty($newName) || $newName == "") {
          $this->error('user_index', 'RUN', 'File Name cant be empty');
          return;
        }

        if (empty($_GET['id'])) {
          $this->error('user_index', 'RUN', 'Id not set');
          return;
        }

        $fileId = $_GET['id'];
        $fileRepository = new FileRepository();

        $oldFile = $fileRepository->readById($fileId);

        if(!empty($fileRepository->getId($newName))) {
          $this->error('user_index', 'RUN', 'A file with that name already exists');
          return;
        }
        $fileRepository->changeFile($newName, $fileId);

        header('Location: /user');
      }
  }

  public function delete()
  {
    if (!isset($_SESSION['username'])){
      $this->error('user_login', 'Login', 'Acces denied.');
    }

{'fraction_non_alphanumeric': 0.1802996806681405, 'fraction_numerical': 0.0009825595676737902, 'mean_word_length': 3.0323539121822383, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 108, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3840525', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3590, 'n_words': 754}