Mark V
1: <?php
2: namespace MOC\V\Core\SecureKernel\Component\Bridge\Repository;
3:
4: use MOC\V\Core\AutoLoader\AutoLoader;
5: use MOC\V\Core\SecureKernel\Component\Bridge\Repository\SFTP\Connection;
6: use MOC\V\Core\SecureKernel\Component\Bridge\Repository\SFTP\Directory;
7: use MOC\V\Core\SecureKernel\Component\Bridge\Repository\SFTP\File;
8: use MOC\V\Core\SecureKernel\Component\Exception\ComponentException;
9:
10: /**
11: * Class SFTP
12: *
13: * @package MOC\V\Core\SecureKernel\Component\Bridge\Repository
14: */
15: class SFTP extends Connection
16: {
17:
18: /**
19: *
20: */
21: public function __construct()
22: {
23:
24: AutoLoader::getNamespaceAutoLoader('phpseclib', __DIR__.'/../../../Vendor/PhpSecLib/2.0.0');
25: }
26:
27: /**
28: * @param string $Name
29: *
30: * @return bool
31: */
32: public function existsDirectory($Name)
33: {
34:
35: $List = $this->listDirectory();
36: if (array_key_exists($Name, $List)) {
37: if ($List[$Name] instanceof Directory) {
38: return true;
39: }
40: }
41: return false;
42: }
43:
44: /**
45: * @param null|string $Name
46: *
47: * @return SFTP\Directory[]|SFTP\File[]
48: */
49: public function listDirectory($Name = null)
50: {
51:
52: $this->persistConnection();
53:
54: if (null === $Name) {
55: $Name = $this->Connection->pwd();
56: }
57:
58: $List = $this->Connection->rawlist($Name);
59: $Return = array();
60:
61: foreach ($List as $Item => $Attributes) {
62: if ($this->Connection->is_dir($Item)) {
63: $Return[$Item] = new Directory($this, $Attributes);
64: }
65: if ($this->Connection->is_file($Item)) {
66: $Return[$Item] = new File($this, $Attributes);
67: }
68: }
69:
70: return $Return;
71: }
72:
73: /**
74: * @param string $Name
75: *
76: * @return bool
77: */
78: public function existsFile($Name)
79: {
80:
81: $List = $this->listDirectory();
82: if (array_key_exists($Name, $List)) {
83: if ($List[$Name] instanceof File) {
84: return true;
85: }
86: }
87: return false;
88: }
89:
90: /**
91: * @param string $Name
92: *
93: * @return SFTP
94: * @throws ComponentException
95: */
96: public function changeDirectory($Name)
97: {
98:
99: $this->persistConnection();
100:
101: if (!$this->Connection->chdir($Name)) {
102: throw new ComponentException(__METHOD__.': Failed');
103: }
104: return $this;
105: }
106:
107: /**
108: * @param string $Name
109: *
110: * @return SFTP
111: * @throws ComponentException
112: */
113: public function createDirectory($Name)
114: {
115:
116: $this->persistConnection();
117:
118: if (!$this->Connection->mkdir($Name)) {
119: throw new ComponentException(__METHOD__.': Failed');
120: }
121: return $this;
122: }
123:
124: /**
125: * @param $File
126: *
127: * @return SFTP
128: * @throws ComponentException
129: */
130: public function uploadFile($File)
131: {
132:
133: $this->persistConnection();
134:
135: if (!$this->Connection->put(basename($File), file_get_contents($File))) {
136: throw new ComponentException(__METHOD__.': Failed');
137: }
138: return $this;
139: }
140:
141: /**
142: * @param $File
143: *
144: * @return SFTP
145: * @throws ComponentException
146: */
147: public function downloadFile($File)
148: {
149:
150: $this->persistConnection();
151:
152: if (!$this->Connection->get($File, $File)) {
153: throw new ComponentException(__METHOD__.': Failed');
154: }
155: return $this;
156: }
157: }
158: