PageRenderTime 27ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/lixinyang/weixiao001.com
PHP | 327 lines | 239 code | 67 blank | 21 comment | 53 complexity | 0c67e40a3a542dd5bbacf6edce9533d3 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
  12. * @package WordPress
  13. * @subpackage Filesystem
  14. * @uses WP_Filesystem_Base Extends class
  15. */
  16. class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
  17. var $ftp = false;
  18. var $errors = null;
  19. var $options = array();
  20. 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. $this->ftp = new ftp();
  27. //Set defaults:
  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. 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_AUTOASCII);
  65. $this->ftp->Passive(true);
  66. $this->ftp->setTimeout(FS_TIMEOUT);
  67. return true;
  68. }
  69. function get_contents($file, $type = '', $resumepos = 0) {
  70. if ( ! $this->exists($file) )
  71. return false;
  72. if ( empty($type) )
  73. $type = FTP_AUTOASCII;
  74. $this->ftp->SetType($type);
  75. $temp = wp_tempnam( $file );
  76. if ( ! $temphandle = fopen($temp, 'w+') )
  77. return false;
  78. if ( ! $this->ftp->fget($temphandle, $file) ) {
  79. fclose($temphandle);
  80. unlink($temp);
  81. return ''; //Blank document, File does exist, Its just blank.
  82. }
  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. function get_contents_array($file) {
  92. return explode("\n", $this->get_contents($file) );
  93. }
  94. 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. fwrite($temphandle, $contents);
  101. fseek($temphandle, 0); //Skip back to the start of the file being written to
  102. $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
  103. $this->ftp->SetType($type);
  104. $ret = $this->ftp->fput($file, $temphandle);
  105. fclose($temphandle);
  106. unlink($temp);
  107. $this->chmod($file, $mode);
  108. return $ret;
  109. }
  110. function cwd() {
  111. $cwd = $this->ftp->pwd();
  112. if ( $cwd )
  113. $cwd = trailingslashit($cwd);
  114. return $cwd;
  115. }
  116. function chdir($file) {
  117. return $this->ftp->chdir($file);
  118. }
  119. function chgrp($file, $group, $recursive = false ) {
  120. return false;
  121. }
  122. function chmod($file, $mode = false, $recursive = false ) {
  123. if ( ! $mode ) {
  124. if ( $this->is_file($file) )
  125. $mode = FS_CHMOD_FILE;
  126. elseif ( $this->is_dir($file) )
  127. $mode = FS_CHMOD_DIR;
  128. else
  129. return false;
  130. }
  131. // chmod any sub-objects if recursive.
  132. if ( $recursive && $this->is_dir($file) ) {
  133. $filelist = $this->dirlist($file);
  134. foreach ( (array)$filelist as $filename => $filemeta )
  135. $this->chmod($file . '/' . $filename, $mode, $recursive);
  136. }
  137. // chmod the file or directory
  138. return $this->ftp->chmod($file, $mode);
  139. }
  140. function chown($file, $owner, $recursive = false ) {
  141. return false;
  142. }
  143. function owner($file) {
  144. $dir = $this->dirlist($file);
  145. return $dir[$file]['owner'];
  146. }
  147. function getchmod($file) {
  148. $dir = $this->dirlist($file);
  149. return $dir[$file]['permsn'];
  150. }
  151. function group($file) {
  152. $dir = $this->dirlist($file);
  153. return $dir[$file]['group'];
  154. }
  155. function copy($source, $destination, $overwrite = false, $mode = false) {
  156. if ( ! $overwrite && $this->exists($destination) )
  157. return false;
  158. $content = $this->get_contents($source);
  159. if ( false === $content )
  160. return false;
  161. return $this->put_contents($destination, $content, $mode);
  162. }
  163. function move($source, $destination, $overwrite = false ) {
  164. return $this->ftp->rename($source, $destination);
  165. }
  166. function delete($file, $recursive = false, $type = false) {
  167. if ( empty($file) )
  168. return false;
  169. if ( 'f' == $type || $this->is_file($file) )
  170. return $this->ftp->delete($file);
  171. if ( !$recursive )
  172. return $this->ftp->rmdir($file);
  173. return $this->ftp->mdel($file);
  174. }
  175. function exists($file) {
  176. return $this->ftp->is_exists($file);
  177. }
  178. function is_file($file) {
  179. if ( $this->is_dir($file) )
  180. return false;
  181. if ( $this->exists($file) )
  182. return true;
  183. return false;
  184. }
  185. function is_dir($path) {
  186. $cwd = $this->cwd();
  187. if ( $this->chdir($path) ) {
  188. $this->chdir($cwd);
  189. return true;
  190. }
  191. return false;
  192. }
  193. function is_readable($file) {
  194. //Get dir list, Check if the file is writable by the current user??
  195. return true;
  196. }
  197. function is_writable($file) {
  198. //Get dir list, Check if the file is writable by the current user??
  199. return true;
  200. }
  201. function atime($file) {
  202. return false;
  203. }
  204. function mtime($file) {
  205. return $this->ftp->mdtm($file);
  206. }
  207. function size($file) {
  208. return $this->ftp->filesize($file);
  209. }
  210. function touch($file, $time = 0, $atime = 0 ) {
  211. return false;
  212. }
  213. function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
  214. if ( ! $this->ftp->mkdir($path) )
  215. return false;
  216. if ( ! $chmod )
  217. $chmod = FS_CHMOD_DIR;
  218. $this->chmod($path, $chmod);
  219. if ( $chown )
  220. $this->chown($path, $chown);
  221. if ( $chgrp )
  222. $this->chgrp($path, $chgrp);
  223. return true;
  224. }
  225. function rmdir($path, $recursive = false ) {
  226. $this->delete($path, $recursive);
  227. }
  228. function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
  229. if ( $this->is_file($path) ) {
  230. $limit_file = basename($path);
  231. $path = dirname($path) . '/';
  232. } else {
  233. $limit_file = false;
  234. }
  235. $list = $this->ftp->dirlist($path);
  236. if ( empty($list) && !$this->exists($path) )
  237. return false;
  238. $ret = array();
  239. foreach ( $list as $struc ) {
  240. if ( '.' == $struc['name'] || '..' == $struc['name'] )
  241. continue;
  242. if ( ! $include_hidden && '.' == $struc['name'][0] )
  243. continue;
  244. if ( $limit_file && $struc['name'] != $limit_file )
  245. continue;
  246. if ( 'd' == $struc['type'] ) {
  247. if ( $recursive )
  248. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  249. else
  250. $struc['files'] = array();
  251. }
  252. $ret[ $struc['name'] ] = $struc;
  253. }
  254. return $ret;
  255. }
  256. function __destruct() {
  257. $this->ftp->quit();
  258. }
  259. }
  260. ?>