-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPublicSshKey.php
94 lines (85 loc) · 2.4 KB
/
PublicSshKey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Depage\Fs;
class PublicSshKey
{
// {{{ variables
protected $key = null;
protected $path = null;
protected $temporary = false;
// }}}
// {{{ constructor
public function __construct($data, $tmpDir = false)
{
if ($tmpDir) {
$this->key = $this->parse($data);
$this->path = $this->createTmpFile($tmpDir, $data);
} else {
$this->path = $data;
if (is_file($data) && is_readable($data)) {
$this->key = $this->parse(file_get_contents($data));
} else {
throw new Exceptions\FsException('SSH key file not accessible: "' . $data . '".');
}
}
}
// }}}
// {{{ destructor
public function __destruct()
{
$this->clean();
}
// }}}
// {{{ createTmpFile
protected function createTmpFile($tmpDir, $data)
{
$this->temporary = true;
if (is_dir($tmpDir) && is_writable($tmpDir)) {
$path = tempnam($tmpDir, 'depage-fs');
$bytesWritten = $this->file_put_contents($path, $data);
if ($bytesWritten === false) {
throw new Exceptions\FsException('Cannot create temporary key file "' . $path . '".');
}
} else {
throw new Exceptions\FsException('Cannot write to temporary key file directory "' . $tmpDir . '".');
}
return $path;
}
// }}}
// {{{ parse
protected function parse($keyString)
{
// @todo do proper check
return $keyString;
}
// }}}
// {{{ toString
public function __toString()
{
return $this->path;
}
// }}}
// {{{ clean
public function clean()
{
unset($this->key);
if ($this->temporary) {
if (is_file($this->path) && is_writable($this->path)) {
unlink($this->path);
$this->temporary = false;
} else {
throw new Exceptions\FsException('Cannot delete temporary key file "' . $this->path . '".');
}
}
}
// }}}
// {{{ file_put_contents
/**
* Hook, allows overriding of file_put_contents function
*/
public function file_put_contents($filename, $data, $flags = 0, $context = null)
{
\file_put_contents($filename, $data, $flags, $context);
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker : */