PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/public_html/clfc/wp-admin/includes/class-wp-filesystem-ftpsockets.php

https://gitlab.com/Henaway/CLFC
PHP | 352 lines | 252 code | 79 blank | 21 comment | 57 complexity | 38e7c3d15f1185d747565ec8e0333863 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress FTP Sockets Filesystem.
  4. *
  5. * @package WordPress
  6. * @subpackage Filesystem
  7. */
  8. /**
  9. * WordPress Filesystem Class for implementing FTP Sockets.
  10. *
  11. * @since 2.5.0
  12. * @package WordPress
  13. * @subpackage Filesystem
  14. * @uses WP_Filesystem_Base Extends class
  15. */
  16. class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
  17. public $ftp = false;
  18. public $errors = null;
  19. public $options = array();
  20. public function __construct($opt = '') {
  21. $this->method = 'ftpsockets';
  22. $this->errors = new WP_Error();
  23. // Check if possible to use ftp functions.
  24. if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
  25. return false;
  26. }
  27. $this->ftp = new ftp();
  28. if ( empty($opt['port']) )
  29. $this->options['port'] = 21;
  30. else
  31. $this->options['port'] = $opt['port'];
  32. if ( empty($opt['hostname']) )
  33. $this->errors->add('empty_hostname', __('FTP hostname is required'));
  34. else
  35. $this->options['hostname'] = $opt['hostname'];
  36. if ( ! empty($opt['base']) )
  37. $this->wp_base = $opt['base'];
  38. // Check if the options provided are OK.
  39. if ( empty ($opt['username']) )
  40. $this->errors->add('empty_username', __('FTP username is required'));
  41. else
  42. $this->options['username'] = $opt['username'];
  43. if ( empty ($opt['password']) )
  44. $this->errors->add('empty_password', __('FTP password is required'));
  45. else
  46. $this->options['password'] = $opt['password'];
  47. }
  48. public function connect() {
  49. if ( ! $this->ftp )
  50. return false;
  51. $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
  52. if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
  53. $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  54. return false;
  55. }
  56. if ( ! $this->ftp->connect() ) {
  57. $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  58. return false;
  59. }
  60. if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
  61. $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
  62. return false;
  63. }
  64. $this->ftp->SetType( FTP_BINARY );
  65. $this->ftp->Passive( true );
  66. $this->ftp->setTimeout( FS_TIMEOUT );
  67. return true;
  68. }
  69. public function get_contents( $file ) {
  70. if ( ! $this->exists($file) )
  71. return false;
  72. $temp = wp_tempnam( $file );
  73. if ( ! $temphandle = fopen($temp, 'w+') )
  74. return false;
  75. mbstring_binary_safe_encoding();
  76. if ( ! $this->ftp->fget($temphandle, $file) ) {
  77. fclose($temphandle);
  78. unlink($temp);
  79. reset_mbstring_encoding();
  80. return ''; // Blank document, File does exist, It's just blank.
  81. }
  82. reset_mbstring_encoding();
  83. fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
  84. $contents = '';
  85. while ( ! feof($temphandle) )
  86. $contents .= fread($temphandle, 8192);
  87. fclose($temphandle);
  88. unlink($temp);
  89. return $contents;
  90. }
  91. public function get_contents_array($file) {
  92. return explode("\n", $this->get_contents($file) );
  93. }
  94. public function put_contents($file, $contents, $mode = false ) {
  95. $temp = wp_tempnam( $file );
  96. if ( ! $temphandle = @fopen($temp, 'w+') ) {
  97. unlink($temp);
  98. return false;
  99. }
  100. // The FTP class uses string functions internally during file download/upload
  101. mbstring_binary_safe_encoding();
  102. $bytes_written = fwrite( $temphandle, $contents );
  103. if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
  104. fclose( $temphandle );
  105. unlink( $temp );
  106. reset_mbstring_encoding();
  107. return false;
  108. }
  109. fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
  110. $ret = $this->ftp->fput($file, $temphandle);
  111. reset_mbstring_encoding();
  112. fclose($temphandle);
  113. unlink($temp);
  114. $this->chmod($file, $mode);
  115. return $ret;
  116. }
  117. public function cwd() {
  118. $cwd = $this->ftp->pwd();
  119. if ( $cwd )
  120. $cwd = trailingslashit($cwd);
  121. return $cwd;
  122. }
  123. public function chdir($file) {
  124. return $this->ftp->chdir($file);
  125. }
  126. public function chgrp($file, $group, $recursive = false ) {
  127. return false;
  128. }
  129. public function chmod($file, $mode = false, $recursive = false ) {
  130. if ( ! $mode ) {
  131. if ( $this->is_file($file) )
  132. $mode = FS_CHMOD_FILE;
  133. elseif ( $this->is_dir($file) )
  134. $mode = FS_CHMOD_DIR;
  135. else
  136. return false;
  137. }
  138. // chmod any sub-objects if recursive.
  139. if ( $recursive && $this->is_dir($file) ) {
  140. $filelist = $this->dirlist($file);
  141. foreach ( (array)$filelist as $filename => $filemeta )
  142. $this->chmod($file . '/' . $filename, $mode, $recursive);
  143. }
  144. // chmod the file or directory
  145. return $this->ftp->chmod($file, $mode);
  146. }
  147. public function owner($file) {
  148. $dir = $this->dirlist($file);
  149. return $dir[$file]['owner'];
  150. }
  151. public function getchmod($file) {
  152. $dir = $this->dirlist($file);
  153. return $dir[$file]['permsn'];
  154. }
  155. public function group($file) {
  156. $dir = $this->dirlist($file);
  157. return $dir[$file]['group'];
  158. }
  159. public function copy($source, $destination, $overwrite = false, $mode = false) {
  160. if ( ! $overwrite && $this->exists($destination) )
  161. return false;
  162. $content = $this->get_contents($source);
  163. if ( false === $content )
  164. return false;
  165. return $this->put_contents($destination, $content, $mode);
  166. }
  167. public function move($source, $destination, $overwrite = false ) {
  168. return $this->ftp->rename($source, $destination);
  169. }
  170. public function delete($file, $recursive = false, $type = false) {
  171. if ( empty($file) )
  172. return false;
  173. if ( 'f' == $type || $this->is_file($file) )
  174. return $this->ftp->delete($file);
  175. if ( !$recursive )
  176. return $this->ftp->rmdir($file);
  177. return $this->ftp->mdel($file);
  178. }
  179. public function exists( $file ) {
  180. $list = $this->ftp->nlist( $file );
  181. return !empty( $list ); //empty list = no file, so invert.
  182. // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
  183. }
  184. public function is_file($file) {
  185. if ( $this->is_dir($file) )
  186. return false;
  187. if ( $this->exists($file) )
  188. return true;
  189. return false;
  190. }
  191. public function is_dir($path) {
  192. $cwd = $this->cwd();
  193. if ( $this->chdir($path) ) {
  194. $this->chdir($cwd);
  195. return true;
  196. }
  197. return false;
  198. }
  199. public function is_readable($file) {
  200. return true;
  201. }
  202. public function is_writable($file) {
  203. return true;
  204. }
  205. public function atime($file) {
  206. return false;
  207. }
  208. public function mtime($file) {
  209. return $this->ftp->mdtm($file);
  210. }
  211. public function size($file) {
  212. return $this->ftp->filesize($file);
  213. }
  214. public function touch($file, $time = 0, $atime = 0 ) {
  215. return false;
  216. }
  217. public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
  218. $path = untrailingslashit($path);
  219. if ( empty($path) )
  220. return false;
  221. if ( ! $this->ftp->mkdir($path) )
  222. return false;
  223. if ( ! $chmod )
  224. $chmod = FS_CHMOD_DIR;
  225. $this->chmod($path, $chmod);
  226. if ( $chown )
  227. $this->chown($path, $chown);
  228. if ( $chgrp )
  229. $this->chgrp($path, $chgrp);
  230. return true;
  231. }
  232. public function rmdir($path, $recursive = false ) {
  233. $this->delete($path, $recursive);
  234. }
  235. public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
  236. if ( $this->is_file($path) ) {
  237. $limit_file = basename($path);
  238. $path = dirname($path) . '/';
  239. } else {
  240. $limit_file = false;
  241. }
  242. mbstring_binary_safe_encoding();
  243. $list = $this->ftp->dirlist($path);
  244. if ( empty( $list ) && ! $this->exists( $path ) ) {
  245. reset_mbstring_encoding();
  246. return false;
  247. }
  248. $ret = array();
  249. foreach ( $list as $struc ) {
  250. if ( '.' == $struc['name'] || '..' == $struc['name'] )
  251. continue;
  252. if ( ! $include_hidden && '.' == $struc['name'][0] )
  253. continue;
  254. if ( $limit_file && $struc['name'] != $limit_file )
  255. continue;
  256. if ( 'd' == $struc['type'] ) {
  257. if ( $recursive )
  258. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  259. else
  260. $struc['files'] = array();
  261. }
  262. // Replace symlinks formatted as "source -> target" with just the source name
  263. if ( $struc['islink'] )
  264. $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
  265. $ret[ $struc['name'] ] = $struc;
  266. }
  267. reset_mbstring_encoding();
  268. return $ret;
  269. }
  270. public function __destruct() {
  271. $this->ftp->quit();
  272. }
  273. }