discuz_ftp.php 6.5 KB

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