addRootCertificate.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. OCP\JSON::checkAppEnabled('files_external');
  3. OCP\JSON::callCheck();
  4. if ( ! ($filename = $_FILES['rootcert_import']['name']) ) {
  5. header("Location: settings/personal.php");
  6. exit;
  7. }
  8. $fh = fopen($_FILES['rootcert_import']['tmp_name'], 'r');
  9. $data = fread($fh, filesize($_FILES['rootcert_import']['tmp_name']));
  10. fclose($fh);
  11. $filename = $_FILES['rootcert_import']['name'];
  12. $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files_external/uploads');
  13. if (!$view->file_exists('')) {
  14. $view->mkdir('');
  15. }
  16. $isValid = openssl_pkey_get_public($data);
  17. //maybe it was just the wrong file format, try to convert it...
  18. if ($isValid == false) {
  19. $data = chunk_split(base64_encode($data), 64, "\n");
  20. $data = "-----BEGIN CERTIFICATE-----\n".$data."-----END CERTIFICATE-----\n";
  21. $isValid = openssl_pkey_get_public($data);
  22. }
  23. // add the certificate if it could be verified
  24. if ( $isValid ) {
  25. $view->file_put_contents($filename, $data);
  26. OC_Mount_Config::createCertificateBundle();
  27. } else {
  28. OCP\Util::writeLog('files_external',
  29. 'Couldn\'t import SSL root certificate ('.$filename.'), allowed formats: PEM and DER',
  30. OCP\Util::WARN);
  31. }
  32. header('Location:' . OCP\Util::linkToRoute( "settings_personal" ));
  33. exit;