You are here

protected function ImportTrainingForm::getImportFiles in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/ImportTrainingForm.php \Drupal\opigno_module\Form\ImportTrainingForm::getImportFiles()

Prepare imported files.

1 call to ImportTrainingForm::getImportFiles()
ImportTrainingForm::submitForm in src/Form/ImportTrainingForm.php
Form submission handler.

File

src/Form/ImportTrainingForm.php, line 396

Class

ImportTrainingForm
Import Course form.

Namespace

Drupal\opigno_module\Form

Code

protected function getImportFiles() {

  // Prepare file validators.
  $extensions = [
    'opi',
  ];
  $validators = [
    'file_validate_extensions' => $extensions,
  ];
  $files = [];
  $file = file_save_upload('training_opi', $validators, $this->tmp, NULL, FileSystemInterface::EXISTS_REPLACE);
  if (!empty($file[0])) {
    $path = $this->fileSystem
      ->realpath($file[0]
      ->getFileUri());
    $zip = new \ZipArchive();
    $result = $zip
      ->open($path);
    if ($zip
      ->locateName('.htaccess') !== false) {
      \Drupal::messenger()
        ->addMessage(t('Unsafe files detected.'), 'error');
      $zip
        ->close();
      $this->fileSystem
        ->delete($path);
      \Drupal::service('file_system')
        ->deleteRecursive($this->tmp);
      return [];
    }
    if ($result === TRUE) {
      $zip
        ->extractTo($this->folder);
      $zip
        ->close();
    }
    $this->fileSystem
      ->delete($path);
    $files = scandir($this->folder);
  }
  return $files;
}