ftp.h 8.8 KB

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