Mark V
1: <?php
2: namespace MOC\V\Core\SecureKernel\Component\Bridge\Repository\SFTP;
3:
4: use MOC\V\Core\SecureKernel\Component\Bridge\Bridge;
5: use MOC\V\Core\SecureKernel\Component\Bridge\Repository\SFTP;
6: use MOC\V\Core\SecureKernel\Component\Exception\ComponentException;
7: use MOC\V\Core\SecureKernel\Component\IBridgeInterface;
8: use phpseclib\Crypt\RSA;
9:
10: /**
11: * Class Connection
12: *
13: * @package MOC\V\Core\SecureKernel\Component\Bridge\Repository\SFTP
14: */
15: abstract class Connection extends Bridge implements IBridgeInterface
16: {
17:
18: /** @var null|\phpseclib\Net\SFTP */
19: protected $Connection = null;
20:
21: /** @var string $Host */
22: private $Host = 'localhost';
23: /** @var int $Port */
24: private $Port = 22;
25: /** @var int $Timeout */
26: private $Timeout = 10;
27: /** @var string $Username */
28: private $Username = '';
29: /** @var null|string $Password */
30: private $Password = null;
31: /** @var null|string $Key */
32: private $Key = null;
33:
34: /**
35: * @return SFTP
36: */
37: public function closeConnection()
38: {
39:
40: if ($this->Connection->isConnected()) {
41: $this->Connection->disconnect();
42: }
43: $this->Connection = null;
44: return $this;
45: }
46:
47: /**
48: * @return SFTP
49: * @throws ComponentException
50: */
51: public function persistConnection()
52: {
53:
54: if (!$this->Connection->isConnected()) {
55: $this->openConnection($this->Host, $this->Port, $this->Timeout);
56: if (null === $this->Key) {
57: $this->loginCredential($this->Username, $this->Password);
58: } else {
59: $this->loginCredentialKey($this->Username, $this->Key, $this->Password);
60: }
61: }
62: return $this;
63: }
64:
65: /**
66: * @param string $Host
67: * @param int $Port
68: * @param int $Timeout
69: *
70: * @return SFTP
71: */
72: public function openConnection($Host, $Port = 22, $Timeout = 10)
73: {
74:
75: $this->Host = $Host;
76: $this->Port = $Port;
77: $this->Timeout = $Timeout;
78: $this->Connection = new \phpseclib\Net\SFTP($Host, $Port, $Timeout);
79: return $this;
80: }
81:
82: /**
83: * @param string $Username
84: * @param null|string $Password
85: *
86: * @return SFTP
87: * @throws ComponentException
88: */
89: public function loginCredential($Username, $Password = null)
90: {
91:
92: $this->Username = $Username;
93: $this->Password = $Password;
94: if (null === $Password) {
95: if (!$this->Connection->login($Username)) {
96: throw new ComponentException(__METHOD__.': Login failed');
97: }
98: } else {
99: if (!$this->Connection->login($Username, $Password)) {
100: throw new ComponentException(__METHOD__.': Login failed');
101: }
102: }
103: return $this;
104: }
105:
106: /**
107: * @param string $Username
108: * @param string $File
109: * @param null|string $Password
110: *
111: * @return SFTP
112: * @throws ComponentException
113: */
114: public function loginCredentialKey($Username, $File, $Password = null)
115: {
116:
117: $this->Username = $Username;
118: $this->Key = $File;
119: $this->Password = $Password;
120:
121: $Key = new RSA();
122: if (null !== $Password) {
123: $Key->setPassword($Password);
124: }
125: if (!$Key->loadKey(file_get_contents($File))) {
126: throw new ComponentException(__METHOD__.': Key failed');
127: }
128: if (!$this->Connection->login($Username, $Key)) {
129: throw new ComponentException(__METHOD__.': Login failed');
130: }
131: return $this;
132: }
133: }
134: