ftp.h 7.8 KB

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