depage-fs
Loading...
Searching...
No Matches
PublicSshKey.php
Go to the documentation of this file.
1<?php
2
3namespace Depage\Fs;
4
6{
7 protected $key = null;
8 protected $path = null;
9 protected $temporary = false;
10 public function __construct($data, $tmpDir = false)
11 {
12 if ($tmpDir) {
13 $this->key = $this->parse($data);
14 $this->path = $this->createTmpFile($tmpDir, $data);
15 } else {
16 $this->path = $data;
17 if (is_file($data) && is_readable($data)) {
18 $this->key = $this->parse(file_get_contents($data));
19 } else {
20 throw new Exceptions\FsException('SSH key file not accessible: "' . $data . '".');
21 }
22 }
23 }
24 public function __destruct()
25 {
26 $this->clean();
27 }
28
29 protected function createTmpFile($tmpDir, $data)
30 {
31 $this->temporary = true;
32
33 if (is_dir($tmpDir) && is_writable($tmpDir)) {
34 $path = tempnam($tmpDir, 'depage-fs');
35 $bytesWritten = $this->file_put_contents($path, $data);
36
37 if ($bytesWritten === false) {
38 throw new Exceptions\FsException('Cannot create temporary key file "' . $path . '".');
39 }
40 } else {
41 throw new Exceptions\FsException('Cannot write to temporary key file directory "' . $tmpDir . '".');
42 }
43
44 return $path;
45 }
46
47 protected function parse($keyString)
48 {
49 // @todo do proper check
50 return $keyString;
51 }
52 public function __toString()
53 {
54 return $this->path;
55 }
56 public function clean()
57 {
58 unset($this->key);
59 if ($this->temporary) {
60 if (is_file($this->path) && is_writable($this->path)) {
61 unlink($this->path);
62 $this->temporary = false;
63 } else {
64 throw new Exceptions\FsException('Cannot delete temporary key file "' . $this->path . '".');
65 }
66 }
67 }
68
72 public function file_put_contents($filename, $data, $flags = 0, $context = null)
73 {
74 \file_put_contents($filename, $data, $flags, $context);
75 }
76}
77
78/* vim:set ft=php sw=4 sts=4 fdm=marker : */
__construct($data, $tmpDir=false)
file_put_contents($filename, $data, $flags=0, $context=null)
Hook, allows overriding of file_put_contents function.
createTmpFile($tmpDir, $data)