ftp.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Andrew Skalski <askalski@chek.com> |
  14. | Stefan Esser <sesser@php.net> (resume functions) |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifndef FTP_H
  18. #define FTP_H
  19. #include "php_network.h"
  20. #include <stdio.h>
  21. #ifdef HAVE_NETINET_IN_H
  22. #include <netinet/in.h>
  23. #endif
  24. #define FTP_DEFAULT_TIMEOUT 90
  25. #define FTP_DEFAULT_AUTOSEEK 1
  26. #define FTP_DEFAULT_USEPASVADDRESS 1
  27. #define PHP_FTP_FAILED 0
  28. #define PHP_FTP_FINISHED 1
  29. #define PHP_FTP_MOREDATA 2
  30. /* XXX this should be configurable at runtime XXX */
  31. #define FTP_BUFSIZE 4096
  32. typedef enum ftptype {
  33. FTPTYPE_ASCII=1,
  34. FTPTYPE_IMAGE
  35. } ftptype_t;
  36. typedef struct databuf
  37. {
  38. int listener; /* listener socket */
  39. php_socket_t fd; /* data connection */
  40. ftptype_t type; /* transfer type */
  41. char buf[FTP_BUFSIZE]; /* data buffer */
  42. #ifdef HAVE_FTP_SSL
  43. SSL *ssl_handle; /* ssl handle */
  44. int ssl_active; /* flag if ssl is active or not */
  45. #endif
  46. } databuf_t;
  47. typedef struct ftpbuf
  48. {
  49. php_socket_t fd; /* control connection */
  50. php_sockaddr_storage localaddr; /* local address */
  51. int resp; /* last response code */
  52. char inbuf[FTP_BUFSIZE]; /* last response text */
  53. char *extra; /* extra characters */
  54. int extralen; /* number of extra chars */
  55. char outbuf[FTP_BUFSIZE]; /* command output buffer */
  56. char *pwd; /* cached pwd */
  57. char *syst; /* cached system type */
  58. ftptype_t type; /* current transfer type */
  59. int pasv; /* 0=off; 1=pasv; 2=ready */
  60. php_sockaddr_storage pasvaddr; /* passive mode address */
  61. zend_long timeout_sec; /* User configurable timeout (seconds) */
  62. int autoseek; /* User configurable autoseek flag */
  63. int usepasvaddress; /* Use the address returned by the pasv command */
  64. int nb; /* "nonblocking" transfer in progress */
  65. databuf_t *data; /* Data connection for "nonblocking" transfers */
  66. php_stream *stream; /* output stream for "nonblocking" transfers */
  67. int lastch; /* last char of previous call */
  68. int direction; /* recv = 0 / send = 1 */
  69. int closestream;/* close or not close stream */
  70. #ifdef HAVE_FTP_SSL
  71. int use_ssl; /* enable(1) or disable(0) ssl */
  72. int use_ssl_for_data; /* en/disable ssl for the dataconnection */
  73. int old_ssl; /* old mode = forced data encryption */
  74. SSL *ssl_handle; /* handle for control connection */
  75. int ssl_active; /* ssl active on control conn */
  76. #endif
  77. } ftpbuf_t;
  78. /* open a FTP connection, returns ftpbuf (NULL on error)
  79. * port is the ftp port in network byte order, or 0 for the default
  80. */
  81. ftpbuf_t* ftp_open(const char *host, short port, zend_long timeout_sec);
  82. /* quits from the ftp session (it still needs to be closed)
  83. * return true on success, false on error
  84. */
  85. int ftp_quit(ftpbuf_t *ftp);
  86. /* frees up any cached data held in the ftp buffer */
  87. void ftp_gc(ftpbuf_t *ftp);
  88. /* close the FTP connection and return NULL */
  89. ftpbuf_t* ftp_close(ftpbuf_t *ftp);
  90. /* logs into the FTP server, returns true on success, false on error */
  91. int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len);
  92. /* reinitializes the connection, returns true on success, false on error */
  93. int ftp_reinit(ftpbuf_t *ftp);
  94. /* returns the remote system type (NULL on error) */
  95. const char* ftp_syst(ftpbuf_t *ftp);
  96. /* returns the present working directory (NULL on error) */
  97. const char* ftp_pwd(ftpbuf_t *ftp);
  98. /* exec a command [special features], return true on success, false on error */
  99. int ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len);
  100. /* send a raw ftp command, return response as a hashtable, NULL on error */
  101. void ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value);
  102. /* changes directories, return true on success, false on error */
  103. int ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len);
  104. /* changes to parent directory, return true on success, false on error */
  105. int ftp_cdup(ftpbuf_t *ftp);
  106. /* creates a directory, return the directory name on success, NULL on error.
  107. * the return value must be freed
  108. */
  109. zend_string* ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len);
  110. /* removes a directory, return true on success, false on error */
  111. int ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len);
  112. /* Set permissions on a file */
  113. int ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len);
  114. /* Allocate space on remote server with ALLO command
  115. * Many servers will respond with 202 Allocation not necessary,
  116. * however some servers will not accept STOR or APPE until ALLO is confirmed.
  117. * If response is passed, it is estrdup()ed from ftp->inbuf and must be freed
  118. * or assigned to a zval returned to the user */
  119. int ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response);
  120. /* returns a NULL-terminated array of filenames in the given path
  121. * or NULL on error. the return array must be freed (but don't
  122. * free the array elements)
  123. */
  124. char** ftp_nlist(ftpbuf_t *ftp, const char *path, const size_t path_len);
  125. /* returns a NULL-terminated array of lines returned by the ftp
  126. * LIST command for the given path or NULL on error. the return
  127. * array must be freed (but don't
  128. * free the array elements)
  129. */
  130. char** ftp_list(ftpbuf_t *ftp, const char *path, const size_t path_len, int recursive);
  131. /* populates a hashtable with the facts contained in one line of
  132. * an MLSD response.
  133. */
  134. int ftp_mlsd_parse_line(HashTable *ht, const char *input);
  135. /* returns a NULL-terminated array of lines returned by the ftp
  136. * MLSD command for the given path or NULL on error. the return
  137. * array must be freed (but don't
  138. * free the array elements)
  139. */
  140. char** ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len);
  141. /* switches passive mode on or off
  142. * returns true on success, false on error
  143. */
  144. int ftp_pasv(ftpbuf_t *ftp, int pasv);
  145. /* retrieves a file and saves its contents to outfp
  146. * returns true on success, false on error
  147. */
  148. int ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos);
  149. /* stores the data from a file, socket, or process as a file on the remote server
  150. * returns true on success, false on error
  151. */
  152. int ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos);
  153. /* append the data from a file, socket, or process as a file on the remote server
  154. * returns true on success, false on error
  155. */
  156. int ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type);
  157. /* returns the size of the given file, or -1 on error */
  158. zend_long ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len);
  159. /* returns the last modified time of the given file, or -1 on error */
  160. time_t ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len);
  161. /* renames a file on the server */
  162. int ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len);
  163. /* deletes the file from the server */
  164. int ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len);
  165. /* sends a SITE command to the server */
  166. int ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len);
  167. /* retrieves part of a file and saves its contents to outfp
  168. * returns true on success, false on error
  169. */
  170. int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos);
  171. /* stores the data from a file, socket, or process as a file on the remote server
  172. * returns true on success, false on error
  173. */
  174. int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos);
  175. /* continues a previous nb_(f)get command
  176. */
  177. int ftp_nb_continue_read(ftpbuf_t *ftp);
  178. /* continues a previous nb_(f)put command
  179. */
  180. int ftp_nb_continue_write(ftpbuf_t *ftp);
  181. #endif