Back to index

php5  5.3.10
ftp.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andrew Skalski <askalski@chek.com>                          |
00016    |          Stefan Esser <sesser@php.net> (resume functions)            |
00017    +----------------------------------------------------------------------+
00018  */
00019 
00020 /* $Id: ftp.h 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifndef       FTP_H
00023 #define       FTP_H
00024 
00025 #include "php_network.h"
00026 
00027 #include <stdio.h>
00028 #ifdef HAVE_NETINET_IN_H
00029 #include <netinet/in.h>
00030 #endif
00031 
00032 #define       FTP_DEFAULT_TIMEOUT  90
00033 #define FTP_DEFAULT_AUTOSEEK 1
00034 #define PHP_FTP_FAILED                    0
00035 #define PHP_FTP_FINISHED           1
00036 #define PHP_FTP_MOREDATA           2
00037 
00038 /* XXX this should be configurable at runtime XXX */
00039 #define       FTP_BUFSIZE   4096
00040 
00041 typedef enum ftptype {
00042        FTPTYPE_ASCII=1,
00043        FTPTYPE_IMAGE
00044 } ftptype_t;
00045 
00046 typedef struct databuf
00047 {
00048        int           listener;            /* listener socket */
00049        php_socket_t         fd;                  /* data connection */
00050        ftptype_t     type;                /* transfer type */
00051        char          buf[FTP_BUFSIZE];    /* data buffer */
00052 #if HAVE_OPENSSL_EXT
00053        SSL           *ssl_handle;  /* ssl handle */
00054        int           ssl_active;          /* flag if ssl is active or not */
00055 #endif
00056 } databuf_t;
00057 
00058 typedef struct ftpbuf
00059 {
00060        php_socket_t         fd;                  /* control connection */
00061        php_sockaddr_storage localaddr;    /* local address */
00062        int           resp;                /* last response code */
00063        char          inbuf[FTP_BUFSIZE];  /* last response text */
00064        char          *extra;                     /* extra characters */
00065        int           extralen;            /* number of extra chars */
00066        char          outbuf[FTP_BUFSIZE]; /* command output buffer */
00067        char          *pwd;                /* cached pwd */
00068        char          *syst;               /* cached system type */
00069        ftptype_t     type;                /* current transfer type */
00070        int           pasv;                /* 0=off; 1=pasv; 2=ready */
00071        php_sockaddr_storage pasvaddr;     /* passive mode address */
00072        long   timeout_sec;  /* User configureable timeout (seconds) */
00073        int                  autoseek;     /* User configureable autoseek flag */
00074 
00075        int                         nb;           /* "nonblocking" transfer in progress */
00076        databuf_t            *data; /* Data connection for "nonblocking" transfers */
00077        php_stream           *stream; /* output stream for "nonblocking" transfers */
00078        int                         lastch;              /* last char of previous call */
00079        int                         direction;    /* recv = 0 / send = 1 */
00080        int                         closestream;/* close or not close stream */
00081 #if HAVE_OPENSSL_EXT
00082        int                         use_ssl; /* enable(1) or disable(0) ssl */
00083        int                         use_ssl_for_data; /* en/disable ssl for the dataconnection */
00084        int                         old_ssl;      /* old mode = forced data encryption */
00085        SSL                         *ssl_handle;      /* handle for control connection */
00086        int                         ssl_active;            /* ssl active on control conn */
00087 #endif
00088 
00089 } ftpbuf_t;
00090 
00091 
00092 
00093 /* open a FTP connection, returns ftpbuf (NULL on error)
00094  * port is the ftp port in network byte order, or 0 for the default
00095  */
00096 ftpbuf_t*     ftp_open(const char *host, short port, long timeout_sec TSRMLS_DC);
00097 
00098 /* quits from the ftp session (it still needs to be closed)
00099  * return true on success, false on error
00100  */
00101 int           ftp_quit(ftpbuf_t *ftp);
00102 
00103 /* frees up any cached data held in the ftp buffer */
00104 void          ftp_gc(ftpbuf_t *ftp);
00105 
00106 /* close the FTP connection and return NULL */
00107 ftpbuf_t*     ftp_close(ftpbuf_t *ftp);
00108 
00109 /* logs into the FTP server, returns true on success, false on error */
00110 int           ftp_login(ftpbuf_t *ftp, const char *user, const char *pass TSRMLS_DC);
00111 
00112 /* reinitializes the connection, returns true on success, false on error */
00113 int           ftp_reinit(ftpbuf_t *ftp);
00114 
00115 /* returns the remote system type (NULL on error) */
00116 const char*   ftp_syst(ftpbuf_t *ftp);
00117 
00118 /* returns the present working directory (NULL on error) */
00119 const char*   ftp_pwd(ftpbuf_t *ftp);
00120 
00121 /* exec a command [special features], return true on success, false on error */
00122 int    ftp_exec(ftpbuf_t *ftp, const char *cmd);
00123 
00124 /* send a raw ftp command, return response as a hashtable, NULL on error */
00125 void   ftp_raw(ftpbuf_t *ftp, const char *cmd, zval *return_value);
00126 
00127 /* changes directories, return true on success, false on error */
00128 int           ftp_chdir(ftpbuf_t *ftp, const char *dir);
00129 
00130 /* changes to parent directory, return true on success, false on error */
00131 int           ftp_cdup(ftpbuf_t *ftp);
00132 
00133 /* creates a directory, return the directory name on success, NULL on error.
00134  * the return value must be freed
00135  */
00136 char*         ftp_mkdir(ftpbuf_t *ftp, const char *dir);
00137 
00138 /* removes a directory, return true on success, false on error */
00139 int           ftp_rmdir(ftpbuf_t *ftp, const char *dir);
00140 
00141 /* Set permissions on a file */
00142 int           ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len);
00143 
00144 /* Allocate space on remote server with ALLO command
00145  * Many servers will respond with 202 Allocation not necessary,
00146  * however some servers will not accept STOR or APPE until ALLO is confirmed. 
00147  * If response is passed, it is estrdup()ed from ftp->inbuf and must be freed
00148  * or assigned to a zval returned to the user */
00149 int           ftp_alloc(ftpbuf_t *ftp, const int size, char **response);
00150 
00151 /* returns a NULL-terminated array of filenames in the given path
00152  * or NULL on error.  the return array must be freed (but don't
00153  * free the array elements)
00154  */
00155 char**        ftp_nlist(ftpbuf_t *ftp, const char *path TSRMLS_DC);
00156 
00157 /* returns a NULL-terminated array of lines returned by the ftp
00158  * LIST command for the given path or NULL on error.  the return
00159  * array must be freed (but don't
00160  * free the array elements)
00161  */
00162 char**        ftp_list(ftpbuf_t *ftp, const char *path, int recursive TSRMLS_DC);
00163 
00164 /* switches passive mode on or off
00165  * returns true on success, false on error
00166  */
00167 int           ftp_pasv(ftpbuf_t *ftp, int pasv);
00168 
00169 /* retrieves a file and saves its contents to outfp
00170  * returns true on success, false on error
00171  */
00172 int           ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, ftptype_t type, int resumepos TSRMLS_DC);
00173 
00174 /* stores the data from a file, socket, or process as a file on the remote server
00175  * returns true on success, false on error
00176  */
00177 int           ftp_put(ftpbuf_t *ftp, const char *path, php_stream *instream, ftptype_t type, int startpos TSRMLS_DC);
00178 
00179 /* returns the size of the given file, or -1 on error */
00180 int           ftp_size(ftpbuf_t *ftp, const char *path);
00181 
00182 /* returns the last modified time of the given file, or -1 on error */
00183 time_t        ftp_mdtm(ftpbuf_t *ftp, const char *path);
00184 
00185 /* renames a file on the server */
00186 int           ftp_rename(ftpbuf_t *ftp, const char *src, const char *dest);
00187 
00188 /* deletes the file from the server */
00189 int           ftp_delete(ftpbuf_t *ftp, const char *path);
00190 
00191 /* sends a SITE command to the server */
00192 int           ftp_site(ftpbuf_t *ftp, const char *cmd);
00193 
00194 /* retrieves part of a file and saves its contents to outfp
00195  * returns true on success, false on error
00196  */
00197 int           ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, ftptype_t type, int resumepos TSRMLS_DC);
00198 
00199 /* stores the data from a file, socket, or process as a file on the remote server
00200  * returns true on success, false on error
00201  */
00202 int           ftp_nb_put(ftpbuf_t *ftp, const char *path, php_stream *instream, ftptype_t type, int startpos TSRMLS_DC);
00203 
00204 /* continues a previous nb_(f)get command
00205  */
00206 int           ftp_nb_continue_read(ftpbuf_t *ftp TSRMLS_DC);
00207 
00208 /* continues a previous nb_(f)put command
00209  */
00210 int           ftp_nb_continue_write(ftpbuf_t *ftp TSRMLS_DC);
00211 
00212 
00213 #endif