ncftp.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * 缓存文件
  4. *
  5. * @package
  6. */
  7. defined('InShopNC') or exit('Access Invalid!');
  8. class NcFtp{
  9. const FTP_ERR_SERVER_DISABLED = -100;
  10. const FTP_ERR_CONFIG_OFF = -101;
  11. const FTP_ERR_CONNECT_TO_SERVER = -102;
  12. const FTP_ERR_USER_NO_LOGGIN = -103;
  13. const FTP_ERR_CHDIR = -104;
  14. const FTP_ERR_MKDIR = -105;
  15. const FTP_ERR_SOURCE_READ = -106;
  16. const FTP_ERR_TARGET_WRITE = -107;
  17. public $enabled = false;
  18. public $config = array();
  19. public $func;
  20. public $connectid;
  21. public $_error;
  22. public static function &instance($config = array()) {
  23. static $object;
  24. if(empty($object)) {
  25. $object = new NcFtp($config);
  26. }
  27. return $object;
  28. }
  29. public function __construct($config = array()) {
  30. $this->set_error(0);
  31. if (!($this->config = $config)){
  32. $this->config['on'] = C('ftp_open');
  33. $this->config['host'] = C('ftp_server');
  34. $this->config['ssl'] = C('ftp_ssl_state');
  35. $this->config['port'] = C('ftp_port');
  36. $this->config['username'] = C('ftp_username');
  37. $this->config['password'] = C('ftp_password');
  38. $this->config['pasv'] = C('ftp_pasv');
  39. $this->config['attachdir'] = C('ftp_attach_dir');
  40. $this->config['attachurl'] = C('ftp_access_url');
  41. $this->config['timeout'] = C('ftp_timeout');
  42. }
  43. $this->enabled = false;
  44. if(empty($this->config['on']) || empty($this->config['host'])) {
  45. $this->set_error(self::FTP_ERR_CONFIG_OFF);
  46. } else {
  47. $this->func = $this->config['ssl'] && function_exists('ftp_ssl_connect') ? 'ftp_ssl_connect' : 'ftp_connect';
  48. if($this->func == 'ftp_connect' && !function_exists('ftp_connect')) {
  49. $this->set_error(self::FTP_ERR_SERVER_DISABLED);
  50. } else {
  51. $this->config['host'] = NcFtp::clear($this->config['host']);
  52. $this->config['port'] = intval($this->config['port']);
  53. $this->config['ssl'] = intval($this->config['ssl']);
  54. $this->config['host'] = NcFtp::clear($this->config['host']);
  55. $this->config['password'] = $this->config['password'];
  56. $this->config['timeout'] = intval($this->config['timeout']);
  57. $this->enabled = true;
  58. }
  59. }
  60. }
  61. public function upload($source, $target) {
  62. if($this->error()) {
  63. return 0;
  64. }
  65. $old_dir = $this->ftp_pwd();
  66. $dirname = dirname($target);
  67. $filename = basename($target);
  68. if(!$this->ftp_chdir($dirname)) {
  69. if($this->ftp_mkdir($dirname)) {
  70. $this->ftp_chmod($dirname);
  71. if(!$this->ftp_chdir($dirname)) {
  72. $this->set_error(self::FTP_ERR_CHDIR);
  73. }
  74. $this->ftp_put('index.html', BASE_PATH.'/upload/index.html', FTP_BINARY);
  75. } else {
  76. $this->set_error(self::FTP_ERR_MKDIR);
  77. }
  78. }
  79. $res = 0;
  80. if(!$this->error()) {
  81. if($fp = @fopen($source, 'rb')) {
  82. $res = $this->ftp_fput($filename, $fp, FTP_BINARY);
  83. @fclose($fp);
  84. !$res && $this->set_error(self::FTP_ERR_TARGET_WRITE);
  85. } else {
  86. $this->set_error(self::FTP_ERR_SOURCE_READ);
  87. }
  88. }
  89. $this->ftp_chdir($old_dir);
  90. return $res ? 1 : 0;
  91. }
  92. public function connect() {
  93. if(!$this->enabled || empty($this->config)) {
  94. return 0;
  95. } else {
  96. return $this->ftp_connect(
  97. $this->config['host'],
  98. $this->config['username'],
  99. $this->config['password'],
  100. $this->config['attachdir'],
  101. $this->config['port'],
  102. $this->config['timeout'],
  103. $this->config['ssl'],
  104. $this->config['pasv']
  105. );
  106. }
  107. }
  108. public function ftp_connect($ftphost, $username, $password, $ftppath, $ftpport = 21, $timeout = 30, $ftpssl = 0, $ftppasv = 0) {
  109. $res = 0;
  110. $fun = $this->func;
  111. if($this->connectid = $fun($ftphost, $ftpport, 20)) {
  112. $timeout && $this->set_option(FTP_TIMEOUT_SEC, $timeout);
  113. if($this->ftp_login($username, $password)) {
  114. $this->ftp_pasv($ftppasv);
  115. if($this->ftp_chdir($ftppath)) {
  116. $res = $this->connectid;
  117. } else {
  118. $this->set_error(self::FTP_ERR_CHDIR);
  119. }
  120. } else {
  121. $this->set_error(self::FTP_ERR_USER_NO_LOGGIN);
  122. }
  123. } else {
  124. $this->set_error(self::FTP_ERR_CONNECT_TO_SERVER);
  125. }
  126. if($res > 0) {
  127. $this->set_error();
  128. $this->enabled = 1;
  129. } else {
  130. $this->enabled = 0;
  131. $this->ftp_close();
  132. }
  133. return $res;
  134. }
  135. public function set_error($code = 0) {
  136. $this->_error = $code;
  137. }
  138. public function error() {
  139. return $this->_error;
  140. }
  141. public function clear($str) {
  142. return str_replace(array( "\n", "\r", '..'), '', $str);
  143. }
  144. public function set_option($cmd, $value) {
  145. if(function_exists('ftp_set_option')) {
  146. return @ftp_set_option($this->connectid, $cmd, $value);
  147. }
  148. }
  149. public function ftp_mkdir($directory) {
  150. $directory = NcFtp::clear($directory);
  151. $epath = explode('/', $directory);
  152. $dir = '';$comma = '';
  153. foreach($epath as $path) {
  154. $dir .= $comma.$path;
  155. $comma = '/';
  156. $return = @ftp_mkdir($this->connectid, $dir);
  157. $this->ftp_chmod($dir);
  158. }
  159. return $return;
  160. }
  161. public function ftp_rmdir($directory) {
  162. $directory = NcFtp::clear($directory);
  163. return @ftp_rmdir($this->connectid, $directory);
  164. }
  165. public function ftp_put($remote_file, $local_file, $mode = FTP_BINARY) {
  166. $remote_file = NcFtp::clear($remote_file);
  167. $local_file = NcFtp::clear($local_file);
  168. $mode = intval($mode);
  169. return @ftp_put($this->connectid, $remote_file, $local_file, $mode);
  170. }
  171. public function ftp_fput($remote_file, $sourcefp, $mode = FTP_BINARY) {
  172. $remote_file = NcFtp::clear($remote_file);
  173. $mode = intval($mode);
  174. return @ftp_fput($this->connectid, $remote_file, $sourcefp, $mode);
  175. }
  176. public function ftp_size($remote_file) {
  177. $remote_file = NcFtp::clear($remote_file);
  178. return @ftp_size($this->connectid, $remote_file);
  179. }
  180. public function ftp_close() {
  181. return @ftp_close($this->connectid);
  182. }
  183. public function ftp_delete($path) {
  184. $path = NcFtp::clear($path);
  185. return @ftp_delete($this->connectid, $path);
  186. }
  187. public function ftp_get($local_file, $remote_file, $mode, $resumepos = 0) {
  188. $remote_file = NcFtp::clear($remote_file);
  189. $local_file = NcFtp::clear($local_file);
  190. $mode = intval($mode);
  191. $resumepos = intval($resumepos);
  192. return @ftp_get($this->connectid, $local_file, $remote_file, $mode, $resumepos);
  193. }
  194. public function ftp_login($username, $password) {
  195. $username = $this->clear($username);
  196. $password = str_replace(array("\n", "\r"), array('', ''), $password);
  197. return @ftp_login($this->connectid, $username, $password);
  198. }
  199. public function ftp_pasv($pasv) {
  200. return @ftp_pasv($this->connectid, $pasv ? true : false);
  201. }
  202. public function ftp_chdir($directory) {
  203. $directory = NcFtp::clear($directory);
  204. return @ftp_chdir($this->connectid, $directory);
  205. }
  206. public function ftp_site($cmd) {
  207. $cmd = NcFtp::clear($cmd);
  208. return @ftp_site($this->connectid, $cmd);
  209. }
  210. public function ftp_chmod($filename, $mod = 0777) {
  211. $filename = NcFtp::clear($filename);
  212. if(function_exists('ftp_chmod')) {
  213. return @ftp_chmod($this->connectid, $mod, $filename);
  214. } else {
  215. return @ftp_site($this->connectid, 'CHMOD '.$mod.' '.$filename);
  216. }
  217. }
  218. public function ftp_pwd() {
  219. return @ftp_pwd($this->connectid);
  220. }
  221. }
  222. ?>