SSH.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. namespace ZN\Services;
  3. class __USE_STATIC_ACCESS__SSH implements SSHInterface
  4. {
  5. //----------------------------------------------------------------------------------------------------
  6. //
  7. // Yazar : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
  8. // Site : www.zntr.net
  9. // Lisans : The MIT License
  10. // Telif Hakkı: Copyright (c) 2012-2016, zntr.net
  11. //
  12. //----------------------------------------------------------------------------------------------------
  13. //----------------------------------------------------------------------------------------------------
  14. // Const CONFIG_NAME
  15. //----------------------------------------------------------------------------------------------------
  16. //
  17. // @const string
  18. //
  19. //----------------------------------------------------------------------------------------------------
  20. const CONFIG_NAME = 'Services:ssh';
  21. //----------------------------------------------------------------------------------------------------
  22. // Protected $connect
  23. //----------------------------------------------------------------------------------------------------
  24. //
  25. // @const resource
  26. //
  27. //----------------------------------------------------------------------------------------------------
  28. protected $connect = NULL;
  29. //----------------------------------------------------------------------------------------------------
  30. // Protected $login
  31. //----------------------------------------------------------------------------------------------------
  32. //
  33. // @const resource
  34. //
  35. //----------------------------------------------------------------------------------------------------
  36. protected $login = NULL;
  37. //----------------------------------------------------------------------------------------------------
  38. // Protected $stream
  39. //----------------------------------------------------------------------------------------------------
  40. //
  41. // @const resource
  42. //
  43. //----------------------------------------------------------------------------------------------------
  44. protected $stream = NULL;
  45. //----------------------------------------------------------------------------------------------------
  46. // Protected $command
  47. //----------------------------------------------------------------------------------------------------
  48. //
  49. // @const string
  50. //
  51. //----------------------------------------------------------------------------------------------------
  52. protected $command = '';
  53. //----------------------------------------------------------------------------------------------------
  54. // __construct()
  55. //----------------------------------------------------------------------------------------------------
  56. //
  57. // @param array $config: empty
  58. //
  59. //----------------------------------------------------------------------------------------------------
  60. public function __construct($config = [])
  61. {
  62. if( ! function_exists('ssh2_connect') )
  63. {
  64. die(getErrorMessage('Error', 'undefinedFunctionExtension', 'SSH(Secure Shell)'));
  65. }
  66. $this->config($config);
  67. $this->connect();
  68. }
  69. //----------------------------------------------------------------------------------------------------
  70. // Call Undefined Method
  71. //----------------------------------------------------------------------------------------------------
  72. //
  73. // __call()
  74. //
  75. //----------------------------------------------------------------------------------------------------
  76. use \CallUndefinedMethodTrait;
  77. //----------------------------------------------------------------------------------------------------
  78. // Config Method
  79. //----------------------------------------------------------------------------------------------------
  80. //
  81. // config()
  82. //
  83. //----------------------------------------------------------------------------------------------------
  84. use \ConfigMethodTrait;
  85. //----------------------------------------------------------------------------------------------------
  86. // Error Control
  87. //----------------------------------------------------------------------------------------------------
  88. //
  89. // $error
  90. // $success
  91. //
  92. // error()
  93. // success()
  94. //
  95. //----------------------------------------------------------------------------------------------------
  96. use \ErrorControlTrait;
  97. //----------------------------------------------------------------------------------------------------
  98. // connect()
  99. //----------------------------------------------------------------------------------------------------
  100. //
  101. // @param array $config: empty
  102. //
  103. //----------------------------------------------------------------------------------------------------
  104. public function connect($config = [])
  105. {
  106. if( ! is_array($config) )
  107. {
  108. \Errors::set('Error', 'arrayParameter', 'config');
  109. return $this;
  110. }
  111. if( ! empty($config) )
  112. {
  113. $this->config($config);
  114. }
  115. // Config/Ftp.php dosyasından ftp ayarları alınıyor.
  116. $config = $this->config;
  117. // ----------------------------------------------------------------------------
  118. // SSH BAĞLANTI AYARLARI YAPILANDIRILIYOR
  119. // ----------------------------------------------------------------------------
  120. $host = $config['host'];
  121. $port = $config['port'];
  122. $user = $config['user'];
  123. $password = $config['password'];
  124. $methods = $config['methods'];
  125. $callbacks = $config['callbacks'];
  126. // ----------------------------------------------------------------------------
  127. // Bağlantı türü ayarına göre ssl veya normal
  128. // bağlatı yapılıp yapılmayacağı belirlenir.
  129. if( ! empty($methods) && ! empty($callbacks))
  130. {
  131. $this->connect = ssh2_connect($host, $port, $methods, $callbacks);
  132. }
  133. elseif( ! empty($methods) )
  134. {
  135. $this->connect = ssh2_connect($host, $port, $methods);
  136. }
  137. else
  138. {
  139. $this->connect = ssh2_connect($host, $port);
  140. }
  141. if( empty($this->connect) )
  142. {
  143. \Errors::set('Error', 'emptyVariable', '@this->connect');
  144. return $this;
  145. }
  146. if( ! empty($user) )
  147. {
  148. $this->login = ssh2_auth_password($this->connect, $user, $password);
  149. }
  150. if( empty($this->login) )
  151. {
  152. \Errors::set('Error', 'emptyVariable', '@this->login');
  153. }
  154. return $this;
  155. }
  156. //----------------------------------------------------------------------------------------------------
  157. // close()
  158. //----------------------------------------------------------------------------------------------------
  159. //
  160. // @param void
  161. //
  162. //----------------------------------------------------------------------------------------------------
  163. public function close()
  164. {
  165. if( ! empty($this->connect) )
  166. {
  167. ssh2_exec($this->connect, 'exit');
  168. $this->connect = NULL;
  169. }
  170. }
  171. //----------------------------------------------------------------------------------------------------
  172. // command()
  173. //----------------------------------------------------------------------------------------------------
  174. //
  175. // @param void
  176. //
  177. //----------------------------------------------------------------------------------------------------
  178. public function command($command = '')
  179. {
  180. $this->command .= $command.' ';
  181. return $this;
  182. }
  183. //----------------------------------------------------------------------------------------------------
  184. // run()
  185. //----------------------------------------------------------------------------------------------------
  186. //
  187. // @param void
  188. //
  189. //----------------------------------------------------------------------------------------------------
  190. public function run($command = '')
  191. {
  192. if( ! empty($this->connect) )
  193. {
  194. if( ! empty($this->command) )
  195. {
  196. $command = rtrim($this->command);
  197. }
  198. $this->_defaultVariables();
  199. $this->stream = ssh2_exec($this->connect, $command);
  200. return $this->stream;
  201. }
  202. return false;
  203. }
  204. //----------------------------------------------------------------------------------------------------
  205. // output()
  206. //----------------------------------------------------------------------------------------------------
  207. //
  208. // @param void
  209. //
  210. //----------------------------------------------------------------------------------------------------
  211. public function output($length = 4096)
  212. {
  213. $stream = $this->stream;
  214. stream_set_blocking($stream, true);
  215. $data = "";
  216. while( $buffer = fread($stream, $length) )
  217. {
  218. $data .= $buffer;
  219. }
  220. fclose($stream);
  221. return $data;
  222. }
  223. //----------------------------------------------------------------------------------------------------
  224. // upload()
  225. //----------------------------------------------------------------------------------------------------
  226. //
  227. // @param string $localPath : empty
  228. // @param string $remotePath: empty
  229. //
  230. //----------------------------------------------------------------------------------------------------
  231. public function upload($localPath = '', $remotePath = '')
  232. {
  233. if( ! is_string($localPath) || ! is_string($remotePath) )
  234. {
  235. \Errors::set('Error', 'stringParameter', 'localPath');
  236. \Errors::set('Error', 'stringParameter', 'remotePath');
  237. return false;
  238. }
  239. if( @ssh2_scp_send($this->connect, $localPath, $remotePath) )
  240. {
  241. return true;
  242. }
  243. else
  244. {
  245. return \Errors::set('File', 'remoteUploadError', $localPath);
  246. }
  247. }
  248. //----------------------------------------------------------------------------------------------------
  249. // dowload()
  250. //----------------------------------------------------------------------------------------------------
  251. //
  252. // @param string $remotePath: empty
  253. // @param string $localPath : empty
  254. //
  255. //----------------------------------------------------------------------------------------------------
  256. public function download($remotePath = '', $localPath = '')
  257. {
  258. if( ! is_string($localPath) || ! is_string($remotePath) )
  259. {
  260. \Errors::set('Error', 'stringParameter', 'remotePath');
  261. \Errors::set('Error', 'stringParameter', 'localPath');
  262. return false;
  263. }
  264. if( @ssh2_scp_recv($this->connect, $remotePath, $localPath) )
  265. {
  266. return true;
  267. }
  268. else
  269. {
  270. return \Errors::set('File', 'remoteDownloadError', $localPath);
  271. }
  272. }
  273. //----------------------------------------------------------------------------------------------------
  274. // createFolder()
  275. //----------------------------------------------------------------------------------------------------
  276. //
  277. // @param string $path: empty
  278. //
  279. //----------------------------------------------------------------------------------------------------
  280. public function createFolder($path = '', $mode = 0777, $recursive = true)
  281. {
  282. if( ! is_string($path) )
  283. {
  284. return \Errors::set('Error', 'stringParameter', 'path');
  285. }
  286. if( @ssh2_sftp_mkdir($this->connect, $path, $mode, $recursive) )
  287. {
  288. return true;
  289. }
  290. else
  291. {
  292. return \Errors::set('Folder', 'alreadyFileError', $path);
  293. }
  294. }
  295. //----------------------------------------------------------------------------------------------------
  296. // deleteFolder()
  297. //----------------------------------------------------------------------------------------------------
  298. //
  299. // @param string $path: empty
  300. //
  301. //----------------------------------------------------------------------------------------------------
  302. public function deleteFolder($path = '')
  303. {
  304. if( ! is_string($path) )
  305. {
  306. return \Errors::set('Error', 'stringParameter', 'path');
  307. }
  308. if( @ssh2_sftp_rmdir($this->connect, $path) )
  309. {
  310. return true;
  311. }
  312. else
  313. {
  314. return \Errors::set('Folder', 'notFoundError', $path);
  315. }
  316. }
  317. //----------------------------------------------------------------------------------------------------
  318. // rename()
  319. //----------------------------------------------------------------------------------------------------
  320. //
  321. // @param string $oldName: empty
  322. // @param string $newName: empty
  323. //
  324. //----------------------------------------------------------------------------------------------------
  325. public function rename($oldName = '', $newName = '')
  326. {
  327. if( ! is_string($oldName) || ! is_string($newName) )
  328. {
  329. \Errors::set('Error', 'stringParameter', 'oldName');
  330. \Errors::set('Error', 'stringParameter', 'newName');
  331. return false;
  332. }
  333. if( @ssh2_sftp_rename($this->connect, $oldName, $newName) )
  334. {
  335. return true;
  336. }
  337. else
  338. {
  339. return \Errors::set('Folder', 'changeFolderNameError', $oldName);
  340. }
  341. }
  342. //----------------------------------------------------------------------------------------------------
  343. // deleteFile()
  344. //----------------------------------------------------------------------------------------------------
  345. //
  346. // @param string $path: empty
  347. //
  348. //----------------------------------------------------------------------------------------------------
  349. public function deleteFile($path = '')
  350. {
  351. if( ! is_string($path) )
  352. {
  353. return \Errors::set('Error', 'stringParameter', 'path');
  354. }
  355. if( @ssh2_sftp_unlink($this->connect, $path) )
  356. {
  357. return true;
  358. }
  359. else
  360. {
  361. return \Errors::set('File', 'notFoundError', $path);
  362. }
  363. }
  364. //----------------------------------------------------------------------------------------------------
  365. // permission()
  366. //----------------------------------------------------------------------------------------------------
  367. //
  368. // @param string $path: empty
  369. // @param int $type : 0755
  370. //
  371. //----------------------------------------------------------------------------------------------------
  372. public function permission($path = '', $type = 0755)
  373. {
  374. if( ! is_string($path) )
  375. {
  376. return \Errors::set('Error', 'stringParameter', 'path');
  377. }
  378. if( ! is_numeric($type) )
  379. {
  380. $type = 0755;
  381. }
  382. if( @ssh2_sftp_chmod($this->connect, $path, $type) )
  383. {
  384. return true;
  385. }
  386. else
  387. {
  388. return \Errors::set('Error', 'emptyVariable', '@this->connect');
  389. }
  390. }
  391. //----------------------------------------------------------------------------------------------------
  392. // Protected _defaultVariables()
  393. //----------------------------------------------------------------------------------------------------
  394. //
  395. // @param void
  396. //
  397. //----------------------------------------------------------------------------------------------------
  398. public function _defaultVariables()
  399. {
  400. $this->command = '';
  401. }
  402. //----------------------------------------------------------------------------------------------------
  403. // __destruct()
  404. //----------------------------------------------------------------------------------------------------
  405. //
  406. // @param void
  407. //
  408. //----------------------------------------------------------------------------------------------------
  409. public function __destruct()
  410. {
  411. $this->close();
  412. }
  413. }