You are here

public function CertManager::writeCerts in Lockr 4.x

Writes the given key and cert files to a directory.

Parameters

string $dir: The dir to write the files to.

string $cert_text: The certificate file.

string $key_text: The key file.

Return value

bool Returns TRUE if the write was successful.

File

src/CertManager.php, line 164

Class

CertManager
Helper class for managing Lockr certificates.

Namespace

Drupal\lockr

Code

public function writeCerts($dir, $cert_text, $key_text) {
  if (is_dir($dir)) {
    return FALSE;
  }
  $parent = dirname($dir);
  if (!is_dir($parent) || !is_writable($parent)) {
    return FALSE;
  }

  // The temporary directory is created in the parent of our desired
  // final direcotry because rename only works on the same filesystem.
  $tmpdir = $this
    ->tmpdir($parent);
  if ($tmpdir === FALSE) {
    return FALSE;
  }
  $key_file = "{$tmpdir}/key.pem";
  if (file_put_contents($key_file, $key_text) === FALSE) {
    return FALSE;
  }
  if (!chmod($key_file, 0640)) {
    return FALSE;
  }
  $cert_file = "{$tmpdir}/crt.pem";
  if (file_put_contents($cert_file, $cert_text) === FALSE) {
    return FALSE;
  }
  if (!chmod($cert_file, 0640)) {
    return FALSE;
  }
  $pair_file = "{$tmpdir}/pair.pem";
  if (file_put_contents($pair_file, [
    $key_text,
    $cert_text,
  ]) === FALSE) {
    return FALSE;
  }
  if (!chmod($pair_file, 0640)) {
    return FALSE;
  }
  if (!rename($tmpdir, $dir)) {
    return FALSE;
  }
  return TRUE;
}