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