PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Websites/webkit.org/blog/wp-admin/includes/class-wp-filesystem-ftpsockets.php

https://bitbucket.org/zenoalbisser/webkit
PHP | 327 lines | 239 code | 63 blank | 25 comment | 49 complexity | 103ceabdaa0da8e1cb143737370cb691 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 $timeout = 5;
  19. var $errors = null;
  20. var $options = array();
  21. var $permission = null;
  22. function WP_Filesystem_ftpsockets($opt = '') {
  23. $this->method = 'ftpsockets';
  24. $this->errors = new WP_Error();
  25. //Check if possible to use ftp functions.
  26. if( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
  27. return false;
  28. $this->ftp = new ftp();
  29. //Set defaults:
  30. if ( empty($opt['port']) )
  31. $this->options['port'] = 21;
  32. else
  33. $this->options['port'] = $opt['port'];
  34. if ( empty($opt['hostname']) )
  35. $this->errors->add('empty_hostname', __('FTP hostname is required'));
  36. else
  37. $this->options['hostname'] = $opt['hostname'];
  38. if ( isset($opt['base']) && ! empty($opt['base']) )
  39. $this->wp_base = $opt['base'];
  40. // Check if the options provided are OK.
  41. if ( empty ($opt['username']) )
  42. $this->errors->add('empty_username', __('FTP username is required'));
  43. else
  44. $this->options['username'] = $opt['username'];
  45. if ( empty ($opt['password']) )
  46. $this->errors->add('empty_password', __('FTP password is required'));
  47. else
  48. $this->options['password'] = $opt['password'];
  49. }
  50. function connect() {
  51. if ( ! $this->ftp )
  52. return false;
  53. //$this->ftp->Verbose = true;
  54. if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
  55. $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  56. return false;
  57. }
  58. if ( ! $this->ftp->connect() ) {
  59. $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  60. return false;
  61. }
  62. if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
  63. $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
  64. return false;
  65. }
  66. $this->ftp->SetType(FTP_AUTOASCII);
  67. $this->ftp->Passive(true);
  68. return true;
  69. }
  70. function setDefaultPermissions($perm) {
  71. $this->permission = $perm;
  72. }
  73. function get_contents($file, $type = '', $resumepos = 0) {
  74. if( ! $this->exists($file) )
  75. return false;
  76. if( empty($type) )
  77. $type = FTP_AUTOASCII;
  78. $this->ftp->SetType($type);
  79. $temp = wp_tempnam( $file );
  80. if ( ! $temphandle = fopen($temp, 'w+') )
  81. return false;
  82. if ( ! $this->ftp->fget($temphandle, $file) ) {
  83. fclose($temphandle);
  84. unlink($temp);
  85. return ''; //Blank document, File does exist, Its just blank.
  86. }
  87. fseek($temphandle, 0); //Skip back to the start of the file being written to
  88. $contents = '';
  89. while ( ! feof($temphandle) )
  90. $contents .= fread($temphandle, 8192);
  91. fclose($temphandle);
  92. unlink($temp);
  93. return $contents;
  94. }
  95. function get_contents_array($file) {
  96. return explode("\n", $this->get_contents($file) );
  97. }
  98. function put_contents($file, $contents, $type = '' ) {
  99. if( empty($type) )
  100. $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
  101. $this->ftp->SetType($type);
  102. $temp = wp_tempnam( $file );
  103. if ( ! $temphandle = fopen($temp, 'w+') ){
  104. unlink($temp);
  105. return false;
  106. }
  107. fwrite($temphandle, $contents);
  108. fseek($temphandle, 0); //Skip back to the start of the file being written to
  109. $ret = $this->ftp->fput($file, $temphandle);
  110. fclose($temphandle);
  111. unlink($temp);
  112. return $ret;
  113. }
  114. function cwd() {
  115. $cwd = $this->ftp->pwd();
  116. if( $cwd )
  117. $cwd = trailingslashit($cwd);
  118. return $cwd;
  119. }
  120. function chdir($file) {
  121. return $this->ftp->chdir($file);
  122. }
  123. function chgrp($file, $group, $recursive = false ) {
  124. return false;
  125. }
  126. function chmod($file, $mode = false, $recursive = false ) {
  127. if( ! $mode )
  128. $mode = $this->permission;
  129. if( ! $mode )
  130. return false;
  131. //if( ! $this->exists($file) )
  132. // return false;
  133. if( ! $recursive || ! $this->is_dir($file) ) {
  134. return $this->ftp->chmod($file,$mode);
  135. }
  136. //Is a directory, and we want recursive
  137. $filelist = $this->dirlist($file);
  138. foreach($filelist as $filename){
  139. $this->chmod($file . '/' . $filename, $mode, $recursive);
  140. }
  141. return true;
  142. }
  143. function chown($file, $owner, $recursive = false ) {
  144. return false;
  145. }
  146. function owner($file) {
  147. $dir = $this->dirlist($file);
  148. return $dir[$file]['owner'];
  149. }
  150. function getchmod($file) {
  151. $dir = $this->dirlist($file);
  152. return $dir[$file]['permsn'];
  153. }
  154. function group($file) {
  155. $dir = $this->dirlist($file);
  156. return $dir[$file]['group'];
  157. }
  158. function copy($source, $destination, $overwrite = false ) {
  159. if( ! $overwrite && $this->exists($destination) )
  160. return false;
  161. $content = $this->get_contents($source);
  162. if ( false === $content )
  163. return false;
  164. return $this->put_contents($destination, $content);
  165. }
  166. function move($source, $destination, $overwrite = false ) {
  167. return $this->ftp->rename($source, $destination);
  168. }
  169. function delete($file, $recursive = false ) {
  170. if ( empty($file) )
  171. return false;
  172. if ( $this->is_file($file) )
  173. return $this->ftp->delete($file);
  174. if ( !$recursive )
  175. return $this->ftp->rmdir($file);
  176. return $this->ftp->mdel($file);
  177. }
  178. function exists($file) {
  179. return $this->ftp->is_exists($file);
  180. }
  181. function is_file($file) {
  182. return $this->is_dir($file) ? false : true;
  183. }
  184. function is_dir($path) {
  185. $cwd = $this->cwd();
  186. if ( $this->chdir($path) ) {
  187. $this->chdir($cwd);
  188. return true;
  189. }
  190. return false;
  191. }
  192. function is_readable($file) {
  193. //Get dir list, Check if the file is writable by the current user??
  194. return true;
  195. }
  196. function is_writable($file) {
  197. //Get dir list, Check if the file is writable by the current user??
  198. return true;
  199. }
  200. function atime($file) {
  201. return false;
  202. }
  203. function mtime($file) {
  204. return $this->ftp->mdtm($file);
  205. }
  206. function size($file) {
  207. return $this->ftp->filesize($file);
  208. }
  209. function touch($file, $time = 0, $atime = 0 ) {
  210. return false;
  211. }
  212. function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
  213. if( ! $this->ftp->mkdir($path) )
  214. return false;
  215. if( $chmod )
  216. $this->chmod($path, $chmod);
  217. if( $chown )
  218. $this->chown($path, $chown);
  219. if( $chgrp )
  220. $this->chgrp($path, $chgrp);
  221. return true;
  222. }
  223. function rmdir($path, $recursive = false ) {
  224. if( ! $recursive )
  225. return $this->ftp->rmdir($path);
  226. return $this->ftp->mdel($path);
  227. }
  228. function dirlist($path = '.', $incdot = false, $recursive = false ) {
  229. if( $this->is_file($path) ) {
  230. $limitFile = basename($path);
  231. $path = dirname($path) . '/';
  232. } else {
  233. $limitFile = false;
  234. }
  235. $list = $this->ftp->dirlist($path);
  236. if( ! $list )
  237. return false;
  238. if( empty($list) )
  239. return array();
  240. $ret = array();
  241. foreach ( $list as $struc ) {
  242. if ( 'd' == $struc['type'] ) {
  243. $struc['files'] = array();
  244. if ( $incdot ){
  245. //We're including the doted starts
  246. if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
  247. if ($recursive)
  248. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
  249. }
  250. } else { //No dots
  251. if ($recursive)
  252. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
  253. }
  254. }
  255. //File
  256. $ret[$struc['name']] = $struc;
  257. }
  258. return $ret;
  259. }
  260. function __destruct() {
  261. $this->ftp->quit();
  262. }
  263. }
  264. ?>