Ftp

The WordPress Core Ftp class.

Defined (1)

The class is defined in the following location(s).

/lib/Ftp.php  
  1. class Ftp { 
  2.  
  3. private $_server, $_user, $_password, $_dir = '', $_showRootDir = false; 
  4. private $_connId = false, $_loggedIn = false; 
  5. var $status = '', $isReady = false; 
  6.  
  7. function __construct() { 
  8. $options = EzGA::getAllOptions(); 
  9. if (empty($options['ftp_server'])) { 
  10. $this->status = "FTP Server not verified."; 
  11. return; 
  12. else { 
  13. $this->_server = $options['ftp_server']; 
  14. if (false && function_exists('ftp_ssl_connect')) { 
  15. $this->_connId = ftp_ssl_connect($this->_server); 
  16. else { 
  17. $this->_connId = ftp_connect($this->_server); 
  18. if (!$this->_connId) { 
  19. $this->status = "Cannot reach FTP server."; 
  20. return; 
  21. if (empty($options['ftp_user'])) { 
  22. $this->status = "FTP User ID not verified."; 
  23. return; 
  24. else { 
  25. $this->_user = $options['ftp_user']; 
  26. if (empty($options['ftp_password'])) { 
  27. $this->status = "FTP Password not verified."; 
  28. return; 
  29. else { 
  30. $this->_password = $options['ftp_password']; 
  31. $this->_loggedIn = @ftp_login($this->_connId, $this->_user, $this->_password); 
  32. if (!$this->_loggedIn) { 
  33. $this->status = "FTP login fails"; 
  34. return; 
  35. if (empty($options['ftp_rootdir'])) { 
  36. if (!$this->guessDir()) { 
  37. $this->status = "FTP Root Directory needed."; 
  38. $this->_showRootDir = true; 
  39. return; 
  40. else { 
  41. $this->_dir = $options['ftp_rootdir']; 
  42. ftp_chdir($this->_connId, $this->_dir); 
  43. $this->isReady = true; 
  44.  
  45. function guessDir() { 
  46. $dir0 = __DIR__; 
  47. $ds = DIRECTORY_SEPARATOR; 
  48. $parts = explode($ds, $dir0); 
  49. foreach ($parts as $p) { 
  50. if (empty($p)) { 
  51. continue; 
  52. $this->_dir .= $ds . $p; 
  53. $dir = str_replace($this->_dir, '', $dir0); 
  54. $exists = @ftp_chdir($this->_connId, $dir); 
  55. if ($exists) { 
  56. return true; 
  57. return false; 
  58.  
  59. static function isNeeded($path) { 
  60. $target = realpath($path) . DIRECTORY_SEPARATOR . EzGA::randString(); 
  61. if (@file_put_contents($target, "This is a test file") !== false) { 
  62. if (@unlink($target)) { 
  63. return false; 
  64. return true; 
  65.  
  66. function printForm() { 
  67. $table = ''; 
  68. if (self::isNeeded($this->_dir)) { 
  69. if ($this->_loggedIn) { 
  70. $display = "style='display:none'"; 
  71. $table = "<div><h4 style='display:inline'>FTP is in Use</h4>  <a id='showFtp' class='btn-sm btn-primary' title='See FTP Details' data-trigger='hover' data-placement='top' data-toggle='popover' data-content='<p>FTP is needed to update your application. Your credentials and serer details are already stored, and are being used. If you would like to inspect or modify them, please click here.</p>'><i class='glyphicon glyphicon-zoom-in icon-white'></i> See FTP Details</a></div>"; 
  72. else { 
  73. $display = ''; 
  74. if (empty($this->status)) { 
  75. $btn = "<a href='' class='btn-sm btn-success ftp' title='Your FTP credentials look fine and are saved in your database. If you modify them below, click on this button to check again.' data-toggle='tooltip'><i class='glyphicon glyphicon-thumbs-up'></i> All Okay</a>"; 
  76. else { 
  77. $btn = "<a href='' class='btn-sm btn-warning ftp' title='Your FTP credentials do not look right. Please re-enter them and click on this button to check again.' data-toggle='tooltip'><i class='glyphicon glyphicon-thumbs-down'></i> Check Again</a>"; 
  78. $options = array(); 
  79. $options['ftp_server'] = array('name' => __('FTP Server', 'easy-common'),  
  80. 'value' => 'localhost',  
  81. 'validator' => 'notNull',  
  82. 'help' => __('Enter your FPT server name.', 'easy-common'),  
  83. 'dataTpl' => 'none',  
  84. 'dataMode' => ''); 
  85. if (defined('FTP_HOST')) { 
  86. $options['ftp_server']['value'] = FTP_HOST; 
  87. $options['ftp_user'] = array('name' => __('FTP User Name', 'easy-common'),  
  88. 'value' => '',  
  89. 'validator' => 'notNull',  
  90. 'help' => __('Enter your FPT user name.', 'easy-common'),  
  91. 'dataTpl' => 'none',  
  92. 'dataMode' => ''); 
  93. if (defined('FTP_USER')) { 
  94. $options['ftp_user']['value'] = FTP_USER; 
  95. $options['ftp_password'] = array('name' => __('FTP Password', 'easy-common'),  
  96. 'value' => '',  
  97. 'validator' => 'notNull',  
  98. 'help' => __('Enter your FPT password.', 'easy-common'),  
  99. 'dataTpl' => 'none',  
  100. 'dataMode' => ''); 
  101. if (defined('FTP_PASS')) { 
  102. $options['ftp_password']['value'] = FTP_PASS; 
  103. if (!$this->isReady) { 
  104. $this->status .= " (Click on Check Again to verify.)"; 
  105. if ($this->_showRootDir) { 
  106. $options['ftp_rootdir'] = array('name' => __('FTP Root Directory', 'easy-common'),  
  107. 'value' => '',  
  108. 'help' => __('When you logon to your server using FTP, it puts you in a folder. In most cases, we can discover this folder automatically. So you can start by leaving this option empty, but may have to come back and enter the value if prompted later.', 'easy-common'),  
  109. 'dataTpl' => 'none',  
  110. 'dataMode' => ''); 
  111. $table .= "<div id='ftp' $display><h4>FTP Details</h4><p>Looks like FTP is needed to update your application. Please provide or verify the FTP details below. Note that FTP details will be stored in your database in plain text.</p><p class='red'><strong>$this->status</strong>  $btn</p><table class='table table-striped table-bordered responsive'><tbody>"; 
  112. foreach ($options as $pk => $option) { 
  113. $dbVal = EzGA::getGenOption($pk); 
  114. if (empty($dbVal)) { 
  115. EzGA::putGenOption($pk, $option['value']); 
  116. $table .= EzGA::renderOption($pk, $option); 
  117. $table .= "</tbody></table></div>"; 
  118. $table .= "<script>var xeditHandler = 'ajax/options.php'; var xparams ={}; $('#showFtp').click(function() { $(this).parent().hide();$('#ftp').fadeIn();});</script>"; 
  119. return $table; 
  120.  
  121. function copy($source, $target) { // ftp equivalent of php copy 
  122. if (!@copy($source, $target)) { 
  123. if (!$this->isReady) { 
  124. return false; 
  125. $target = str_replace($this->_dir, "", $target); 
  126. return ftp_put($this->_connId, $target, $source, FTP_BINARY); 
  127. else { 
  128. return true; 
  129.  
  130. function rename($source, $target) { 
  131. if (!@rename($source, $target)) { 
  132. if (!$this->isReady) { 
  133. return false; 
  134. $target = str_replace($this->_dir, "", $target); 
  135. return ftp_rename($this->_connId, $target, $source); 
  136. else { 
  137. return true; 
  138.  
  139. function mkdir($pathname) { 
  140. if (!@mkdir($pathname)) { 
  141. if (!$this->isReady) { 
  142. return false; 
  143. $pathname = str_replace($this->_dir, "", $pathname); 
  144. return ftp_mkdir($this->_connId, $pathname); 
  145. else { 
  146. return true; 
  147.  
  148. function unlink($filename) { 
  149. if (!@unlink($filename)) { 
  150. if (!$this->isReady) { 
  151. return false; 
  152. $filename = str_replace($this->_dir, "", $filename); 
  153. return ftp_delete($this->_connId, $filename); 
  154. else { 
  155. return true; 
  156.