php_ftp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #ifdef HAVE_FTP_SSL
  22. # include <openssl/ssl.h>
  23. #endif
  24. #ifdef HAVE_FTP
  25. #include "ext/standard/info.h"
  26. #include "ext/standard/file.h"
  27. #include "Zend/zend_exceptions.h"
  28. #include "php_ftp.h"
  29. #include "ftp.h"
  30. #include "ftp_arginfo.h"
  31. static zend_class_entry *php_ftp_ce = NULL;
  32. static zend_object_handlers ftp_object_handlers;
  33. zend_module_entry php_ftp_module_entry = {
  34. STANDARD_MODULE_HEADER_EX,
  35. NULL,
  36. NULL,
  37. "ftp",
  38. ext_functions,
  39. PHP_MINIT(ftp),
  40. NULL,
  41. NULL,
  42. NULL,
  43. PHP_MINFO(ftp),
  44. PHP_FTP_VERSION,
  45. STANDARD_MODULE_PROPERTIES
  46. };
  47. #ifdef COMPILE_DL_FTP
  48. ZEND_GET_MODULE(php_ftp)
  49. #endif
  50. typedef struct _php_ftp_object {
  51. ftpbuf_t *ftp;
  52. zend_object std;
  53. } php_ftp_object;
  54. static inline zend_object *ftp_object_to_zend_object(php_ftp_object *obj) {
  55. return ((zend_object*)(obj + 1)) - 1;
  56. }
  57. static inline php_ftp_object *ftp_object_from_zend_object(zend_object *zobj) {
  58. return ((php_ftp_object*)(zobj + 1)) - 1;
  59. }
  60. static zend_object* ftp_object_create(zend_class_entry* ce) {
  61. php_ftp_object *obj = zend_object_alloc(sizeof(php_ftp_object), ce);
  62. zend_object *zobj = ftp_object_to_zend_object(obj);
  63. obj->ftp = NULL;
  64. zend_object_std_init(zobj, ce);
  65. object_properties_init(zobj, ce);
  66. zobj->handlers = &ftp_object_handlers;
  67. return zobj;
  68. }
  69. static zend_function *ftp_object_get_constructor(zend_object *zobj) {
  70. zend_throw_error(NULL, "Cannot directly construct FTP\\Connection, use ftp_connect() or ftp_ssl_connect() instead");
  71. return NULL;
  72. }
  73. static void ftp_object_destroy(zend_object *zobj) {
  74. php_ftp_object *obj = ftp_object_from_zend_object(zobj);
  75. if (obj->ftp) {
  76. ftp_close(obj->ftp);
  77. }
  78. zend_object_std_dtor(zobj);
  79. }
  80. PHP_MINIT_FUNCTION(ftp)
  81. {
  82. #ifdef HAVE_FTP_SSL
  83. #if OPENSSL_VERSION_NUMBER < 0x10101000 && !defined(LIBRESSL_VERSION_NUMBER)
  84. SSL_library_init();
  85. OpenSSL_add_all_ciphers();
  86. OpenSSL_add_all_digests();
  87. OpenSSL_add_all_algorithms();
  88. SSL_load_error_strings();
  89. #endif
  90. #endif
  91. php_ftp_ce = register_class_FTP_Connection();
  92. php_ftp_ce->create_object = ftp_object_create;
  93. memcpy(&ftp_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  94. ftp_object_handlers.offset = XtOffsetOf(php_ftp_object, std);
  95. ftp_object_handlers.get_constructor = ftp_object_get_constructor;
  96. ftp_object_handlers.free_obj = ftp_object_destroy;
  97. ftp_object_handlers.clone_obj = NULL;
  98. REGISTER_LONG_CONSTANT("FTP_ASCII", FTPTYPE_ASCII, CONST_PERSISTENT | CONST_CS);
  99. REGISTER_LONG_CONSTANT("FTP_TEXT", FTPTYPE_ASCII, CONST_PERSISTENT | CONST_CS);
  100. REGISTER_LONG_CONSTANT("FTP_BINARY", FTPTYPE_IMAGE, CONST_PERSISTENT | CONST_CS);
  101. REGISTER_LONG_CONSTANT("FTP_IMAGE", FTPTYPE_IMAGE, CONST_PERSISTENT | CONST_CS);
  102. REGISTER_LONG_CONSTANT("FTP_AUTORESUME", PHP_FTP_AUTORESUME, CONST_PERSISTENT | CONST_CS);
  103. REGISTER_LONG_CONSTANT("FTP_TIMEOUT_SEC", PHP_FTP_OPT_TIMEOUT_SEC, CONST_PERSISTENT | CONST_CS);
  104. REGISTER_LONG_CONSTANT("FTP_AUTOSEEK", PHP_FTP_OPT_AUTOSEEK, CONST_PERSISTENT | CONST_CS);
  105. REGISTER_LONG_CONSTANT("FTP_USEPASVADDRESS", PHP_FTP_OPT_USEPASVADDRESS, CONST_PERSISTENT | CONST_CS);
  106. REGISTER_LONG_CONSTANT("FTP_FAILED", PHP_FTP_FAILED, CONST_PERSISTENT | CONST_CS);
  107. REGISTER_LONG_CONSTANT("FTP_FINISHED", PHP_FTP_FINISHED, CONST_PERSISTENT | CONST_CS);
  108. REGISTER_LONG_CONSTANT("FTP_MOREDATA", PHP_FTP_MOREDATA, CONST_PERSISTENT | CONST_CS);
  109. return SUCCESS;
  110. }
  111. PHP_MINFO_FUNCTION(ftp)
  112. {
  113. php_info_print_table_start();
  114. php_info_print_table_row(2, "FTP support", "enabled");
  115. #ifdef HAVE_FTP_SSL
  116. php_info_print_table_row(2, "FTPS support", "enabled");
  117. #else
  118. php_info_print_table_row(2, "FTPS support", "disabled");
  119. #endif
  120. php_info_print_table_end();
  121. }
  122. #define XTYPE(xtype, mode) { \
  123. if (mode != FTPTYPE_ASCII && mode != FTPTYPE_IMAGE) { \
  124. zend_argument_value_error(4, "must be either FTP_ASCII or FTP_BINARY"); \
  125. RETURN_THROWS(); \
  126. } \
  127. xtype = mode; \
  128. }
  129. /* {{{ Opens a FTP stream */
  130. PHP_FUNCTION(ftp_connect)
  131. {
  132. ftpbuf_t *ftp;
  133. char *host;
  134. size_t host_len;
  135. zend_long port = 0;
  136. zend_long timeout_sec = FTP_DEFAULT_TIMEOUT;
  137. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &host, &host_len, &port, &timeout_sec) == FAILURE) {
  138. RETURN_THROWS();
  139. }
  140. if (timeout_sec <= 0) {
  141. zend_argument_value_error(3, "must be greater than 0");
  142. RETURN_THROWS();
  143. }
  144. /* connect */
  145. if (!(ftp = ftp_open(host, (short)port, timeout_sec))) {
  146. RETURN_FALSE;
  147. }
  148. /* autoseek for resuming */
  149. ftp->autoseek = FTP_DEFAULT_AUTOSEEK;
  150. ftp->usepasvaddress = FTP_DEFAULT_USEPASVADDRESS;
  151. #ifdef HAVE_FTP_SSL
  152. /* disable ssl */
  153. ftp->use_ssl = 0;
  154. #endif
  155. object_init_ex(return_value, php_ftp_ce);
  156. ftp_object_from_zend_object(Z_OBJ_P(return_value))->ftp = ftp;
  157. }
  158. /* }}} */
  159. #ifdef HAVE_FTP_SSL
  160. /* {{{ Opens a FTP-SSL stream */
  161. PHP_FUNCTION(ftp_ssl_connect)
  162. {
  163. ftpbuf_t *ftp;
  164. char *host;
  165. size_t host_len;
  166. zend_long port = 0;
  167. zend_long timeout_sec = FTP_DEFAULT_TIMEOUT;
  168. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &host, &host_len, &port, &timeout_sec) == FAILURE) {
  169. RETURN_THROWS();
  170. }
  171. if (timeout_sec <= 0) {
  172. zend_argument_value_error(3, "must be greater than 0");
  173. RETURN_THROWS();
  174. }
  175. /* connect */
  176. if (!(ftp = ftp_open(host, (short)port, timeout_sec))) {
  177. RETURN_FALSE;
  178. }
  179. /* autoseek for resuming */
  180. ftp->autoseek = FTP_DEFAULT_AUTOSEEK;
  181. ftp->usepasvaddress = FTP_DEFAULT_USEPASVADDRESS;
  182. /* enable ssl */
  183. ftp->use_ssl = 1;
  184. object_init_ex(return_value, php_ftp_ce);
  185. ftp_object_from_zend_object(Z_OBJ_P(return_value))->ftp = ftp;
  186. }
  187. /* }}} */
  188. #endif
  189. #define GET_FTPBUF(ftpbuf, zftp) \
  190. ftpbuf = ftp_object_from_zend_object(Z_OBJ_P(zftp))->ftp; \
  191. if (!ftpbuf) { \
  192. zend_throw_exception(zend_ce_value_error, "FTP\\Connection is already closed", 0); \
  193. RETURN_THROWS(); \
  194. }
  195. /* {{{ Logs into the FTP server */
  196. PHP_FUNCTION(ftp_login)
  197. {
  198. zval *z_ftp;
  199. ftpbuf_t *ftp;
  200. char *user, *pass;
  201. size_t user_len, pass_len;
  202. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss", &z_ftp, php_ftp_ce, &user, &user_len, &pass, &pass_len) == FAILURE) {
  203. RETURN_THROWS();
  204. }
  205. GET_FTPBUF(ftp, z_ftp);
  206. /* log in */
  207. if (!ftp_login(ftp, user, user_len, pass, pass_len)) {
  208. if (*ftp->inbuf) {
  209. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  210. }
  211. RETURN_FALSE;
  212. }
  213. RETURN_TRUE;
  214. }
  215. /* }}} */
  216. /* {{{ Returns the present working directory */
  217. PHP_FUNCTION(ftp_pwd)
  218. {
  219. zval *z_ftp;
  220. ftpbuf_t *ftp;
  221. const char *pwd;
  222. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_ftp, php_ftp_ce) == FAILURE) {
  223. RETURN_THROWS();
  224. }
  225. GET_FTPBUF(ftp, z_ftp);
  226. if (!(pwd = ftp_pwd(ftp))) {
  227. if (*ftp->inbuf) {
  228. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  229. }
  230. RETURN_FALSE;
  231. }
  232. RETURN_STRING((char*) pwd);
  233. }
  234. /* }}} */
  235. /* {{{ Changes to the parent directory */
  236. PHP_FUNCTION(ftp_cdup)
  237. {
  238. zval *z_ftp;
  239. ftpbuf_t *ftp;
  240. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_ftp, php_ftp_ce) == FAILURE) {
  241. RETURN_THROWS();
  242. }
  243. GET_FTPBUF(ftp, z_ftp);
  244. if (!ftp_cdup(ftp)) {
  245. if (*ftp->inbuf) {
  246. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  247. }
  248. RETURN_FALSE;
  249. }
  250. RETURN_TRUE;
  251. }
  252. /* }}} */
  253. /* {{{ Changes directories */
  254. PHP_FUNCTION(ftp_chdir)
  255. {
  256. zval *z_ftp;
  257. ftpbuf_t *ftp;
  258. char *dir;
  259. size_t dir_len;
  260. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &dir, &dir_len) == FAILURE) {
  261. RETURN_THROWS();
  262. }
  263. GET_FTPBUF(ftp, z_ftp);
  264. /* change directories */
  265. if (!ftp_chdir(ftp, dir, dir_len)) {
  266. if (*ftp->inbuf) {
  267. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  268. }
  269. RETURN_FALSE;
  270. }
  271. RETURN_TRUE;
  272. }
  273. /* }}} */
  274. /* {{{ Requests execution of a program on the FTP server */
  275. PHP_FUNCTION(ftp_exec)
  276. {
  277. zval *z_ftp;
  278. ftpbuf_t *ftp;
  279. char *cmd;
  280. size_t cmd_len;
  281. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &cmd, &cmd_len) == FAILURE) {
  282. RETURN_THROWS();
  283. }
  284. GET_FTPBUF(ftp, z_ftp);
  285. /* execute serverside command */
  286. if (!ftp_exec(ftp, cmd, cmd_len)) {
  287. if (*ftp->inbuf) {
  288. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  289. }
  290. RETURN_FALSE;
  291. }
  292. RETURN_TRUE;
  293. }
  294. /* }}} */
  295. /* {{{ Sends a literal command to the FTP server */
  296. PHP_FUNCTION(ftp_raw)
  297. {
  298. zval *z_ftp;
  299. ftpbuf_t *ftp;
  300. char *cmd;
  301. size_t cmd_len;
  302. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &cmd, &cmd_len) == FAILURE) {
  303. RETURN_THROWS();
  304. }
  305. GET_FTPBUF(ftp, z_ftp);
  306. /* execute arbitrary ftp command */
  307. ftp_raw(ftp, cmd, cmd_len, return_value);
  308. }
  309. /* }}} */
  310. /* {{{ Creates a directory and returns the absolute path for the new directory or false on error */
  311. PHP_FUNCTION(ftp_mkdir)
  312. {
  313. zval *z_ftp;
  314. ftpbuf_t *ftp;
  315. char *dir;
  316. zend_string *tmp;
  317. size_t dir_len;
  318. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &dir, &dir_len) == FAILURE) {
  319. RETURN_THROWS();
  320. }
  321. GET_FTPBUF(ftp, z_ftp);
  322. /* create directory */
  323. if (NULL == (tmp = ftp_mkdir(ftp, dir, dir_len))) {
  324. if (*ftp->inbuf) {
  325. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  326. }
  327. RETURN_FALSE;
  328. }
  329. RETURN_STR(tmp);
  330. }
  331. /* }}} */
  332. /* {{{ Removes a directory */
  333. PHP_FUNCTION(ftp_rmdir)
  334. {
  335. zval *z_ftp;
  336. ftpbuf_t *ftp;
  337. char *dir;
  338. size_t dir_len;
  339. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &dir, &dir_len) == FAILURE) {
  340. RETURN_THROWS();
  341. }
  342. GET_FTPBUF(ftp, z_ftp);
  343. /* remove directorie */
  344. if (!ftp_rmdir(ftp, dir, dir_len)) {
  345. if (*ftp->inbuf) {
  346. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  347. }
  348. RETURN_FALSE;
  349. }
  350. RETURN_TRUE;
  351. }
  352. /* }}} */
  353. /* {{{ Sets permissions on a file */
  354. PHP_FUNCTION(ftp_chmod)
  355. {
  356. zval *z_ftp;
  357. ftpbuf_t *ftp;
  358. char *filename;
  359. size_t filename_len;
  360. zend_long mode;
  361. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olp", &z_ftp, php_ftp_ce, &mode, &filename, &filename_len) == FAILURE) {
  362. RETURN_THROWS();
  363. }
  364. GET_FTPBUF(ftp, z_ftp);
  365. if (!ftp_chmod(ftp, mode, filename, filename_len)) {
  366. if (*ftp->inbuf) {
  367. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  368. }
  369. RETURN_FALSE;
  370. }
  371. RETURN_LONG(mode);
  372. }
  373. /* }}} */
  374. /* {{{ Attempt to allocate space on the remote FTP server */
  375. PHP_FUNCTION(ftp_alloc)
  376. {
  377. zval *z_ftp, *zresponse = NULL;
  378. ftpbuf_t *ftp;
  379. zend_long size, ret;
  380. zend_string *response = NULL;
  381. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|z", &z_ftp, php_ftp_ce, &size, &zresponse) == FAILURE) {
  382. RETURN_THROWS();
  383. }
  384. GET_FTPBUF(ftp, z_ftp);
  385. ret = ftp_alloc(ftp, size, zresponse ? &response : NULL);
  386. if (response) {
  387. ZEND_TRY_ASSIGN_REF_STR(zresponse, response);
  388. }
  389. if (!ret) {
  390. RETURN_FALSE;
  391. }
  392. RETURN_TRUE;
  393. }
  394. /* }}} */
  395. /* {{{ Returns an array of filenames in the given directory */
  396. PHP_FUNCTION(ftp_nlist)
  397. {
  398. zval *z_ftp;
  399. ftpbuf_t *ftp;
  400. char **nlist, **ptr, *dir;
  401. size_t dir_len;
  402. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op", &z_ftp, php_ftp_ce, &dir, &dir_len) == FAILURE) {
  403. RETURN_THROWS();
  404. }
  405. GET_FTPBUF(ftp, z_ftp);
  406. /* get list of files */
  407. if (NULL == (nlist = ftp_nlist(ftp, dir, dir_len))) {
  408. RETURN_FALSE;
  409. }
  410. array_init(return_value);
  411. for (ptr = nlist; *ptr; ptr++) {
  412. add_next_index_string(return_value, *ptr);
  413. }
  414. efree(nlist);
  415. }
  416. /* }}} */
  417. /* {{{ Returns a detailed listing of a directory as an array of output lines */
  418. PHP_FUNCTION(ftp_rawlist)
  419. {
  420. zval *z_ftp;
  421. ftpbuf_t *ftp;
  422. char **llist, **ptr, *dir;
  423. size_t dir_len;
  424. bool recursive = 0;
  425. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|b", &z_ftp, php_ftp_ce, &dir, &dir_len, &recursive) == FAILURE) {
  426. RETURN_THROWS();
  427. }
  428. GET_FTPBUF(ftp, z_ftp);
  429. /* get raw directory listing */
  430. if (NULL == (llist = ftp_list(ftp, dir, dir_len, recursive))) {
  431. RETURN_FALSE;
  432. }
  433. array_init(return_value);
  434. for (ptr = llist; *ptr; ptr++) {
  435. add_next_index_string(return_value, *ptr);
  436. }
  437. efree(llist);
  438. }
  439. /* }}} */
  440. /* {{{ Returns a detailed listing of a directory as an array of parsed output lines */
  441. PHP_FUNCTION(ftp_mlsd)
  442. {
  443. zval *z_ftp;
  444. ftpbuf_t *ftp;
  445. char **llist, **ptr, *dir;
  446. size_t dir_len;
  447. zval entry;
  448. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &dir, &dir_len) == FAILURE) {
  449. RETURN_THROWS();
  450. }
  451. GET_FTPBUF(ftp, z_ftp);
  452. /* get raw directory listing */
  453. if (NULL == (llist = ftp_mlsd(ftp, dir, dir_len))) {
  454. RETURN_FALSE;
  455. }
  456. array_init(return_value);
  457. for (ptr = llist; *ptr; ptr++) {
  458. array_init(&entry);
  459. if (ftp_mlsd_parse_line(Z_ARRVAL_P(&entry), *ptr) == SUCCESS) {
  460. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &entry);
  461. } else {
  462. zval_ptr_dtor(&entry);
  463. }
  464. }
  465. efree(llist);
  466. }
  467. /* }}} */
  468. /* {{{ Returns the system type identifier */
  469. PHP_FUNCTION(ftp_systype)
  470. {
  471. zval *z_ftp;
  472. ftpbuf_t *ftp;
  473. const char *syst;
  474. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_ftp, php_ftp_ce) == FAILURE) {
  475. RETURN_THROWS();
  476. }
  477. GET_FTPBUF(ftp, z_ftp);
  478. if (NULL == (syst = ftp_syst(ftp))) {
  479. if (*ftp->inbuf) {
  480. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  481. }
  482. RETURN_FALSE;
  483. }
  484. RETURN_STRING((char*) syst);
  485. }
  486. /* }}} */
  487. /* {{{ Retrieves a file from the FTP server and writes it to an open file */
  488. PHP_FUNCTION(ftp_fget)
  489. {
  490. zval *z_ftp, *z_file;
  491. ftpbuf_t *ftp;
  492. ftptype_t xtype;
  493. php_stream *stream;
  494. char *file;
  495. size_t file_len;
  496. zend_long mode=FTPTYPE_IMAGE, resumepos=0;
  497. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ors|ll", &z_ftp, php_ftp_ce, &z_file, &file, &file_len, &mode, &resumepos) == FAILURE) {
  498. RETURN_THROWS();
  499. }
  500. GET_FTPBUF(ftp, z_ftp);
  501. php_stream_from_res(stream, Z_RES_P(z_file));
  502. XTYPE(xtype, mode);
  503. /* ignore autoresume if autoseek is switched off */
  504. if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
  505. resumepos = 0;
  506. }
  507. if (ftp->autoseek && resumepos) {
  508. /* if autoresume is wanted seek to end */
  509. if (resumepos == PHP_FTP_AUTORESUME) {
  510. php_stream_seek(stream, 0, SEEK_END);
  511. resumepos = php_stream_tell(stream);
  512. } else {
  513. php_stream_seek(stream, resumepos, SEEK_SET);
  514. }
  515. }
  516. if (!ftp_get(ftp, stream, file, file_len, xtype, resumepos)) {
  517. if (*ftp->inbuf) {
  518. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  519. }
  520. RETURN_FALSE;
  521. }
  522. RETURN_TRUE;
  523. }
  524. /* }}} */
  525. /* {{{ Retrieves a file from the FTP server asynchronly and writes it to an open file */
  526. PHP_FUNCTION(ftp_nb_fget)
  527. {
  528. zval *z_ftp, *z_file;
  529. ftpbuf_t *ftp;
  530. ftptype_t xtype;
  531. php_stream *stream;
  532. char *file;
  533. size_t file_len;
  534. zend_long mode=FTPTYPE_IMAGE, resumepos=0, ret;
  535. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ors|ll", &z_ftp, php_ftp_ce, &z_file, &file, &file_len, &mode, &resumepos) == FAILURE) {
  536. RETURN_THROWS();
  537. }
  538. GET_FTPBUF(ftp, z_ftp);
  539. php_stream_from_res(stream, Z_RES_P(z_file));
  540. XTYPE(xtype, mode);
  541. /* ignore autoresume if autoseek is switched off */
  542. if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
  543. resumepos = 0;
  544. }
  545. if (ftp->autoseek && resumepos) {
  546. /* if autoresume is wanted seek to end */
  547. if (resumepos == PHP_FTP_AUTORESUME) {
  548. php_stream_seek(stream, 0, SEEK_END);
  549. resumepos = php_stream_tell(stream);
  550. } else {
  551. php_stream_seek(stream, resumepos, SEEK_SET);
  552. }
  553. }
  554. /* configuration */
  555. ftp->direction = 0; /* recv */
  556. ftp->closestream = 0; /* do not close */
  557. if ((ret = ftp_nb_get(ftp, stream, file, file_len, xtype, resumepos)) == PHP_FTP_FAILED) {
  558. if (*ftp->inbuf) {
  559. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  560. }
  561. RETURN_LONG(ret);
  562. }
  563. RETURN_LONG(ret);
  564. }
  565. /* }}} */
  566. /* {{{ Turns passive mode on or off */
  567. PHP_FUNCTION(ftp_pasv)
  568. {
  569. zval *z_ftp;
  570. ftpbuf_t *ftp;
  571. bool pasv;
  572. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &z_ftp, php_ftp_ce, &pasv) == FAILURE) {
  573. RETURN_THROWS();
  574. }
  575. GET_FTPBUF(ftp, z_ftp);
  576. if (!ftp_pasv(ftp, pasv ? 1 : 0)) {
  577. RETURN_FALSE;
  578. }
  579. RETURN_TRUE;
  580. }
  581. /* }}} */
  582. /* {{{ Retrieves a file from the FTP server and writes it to a local file */
  583. PHP_FUNCTION(ftp_get)
  584. {
  585. zval *z_ftp;
  586. ftpbuf_t *ftp;
  587. ftptype_t xtype;
  588. php_stream *outstream;
  589. char *local, *remote;
  590. size_t local_len, remote_len;
  591. zend_long mode=FTPTYPE_IMAGE, resumepos=0;
  592. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Opp|ll", &z_ftp, php_ftp_ce, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
  593. RETURN_THROWS();
  594. }
  595. GET_FTPBUF(ftp, z_ftp);
  596. XTYPE(xtype, mode);
  597. /* ignore autoresume if autoseek is switched off */
  598. if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
  599. resumepos = 0;
  600. }
  601. #ifdef PHP_WIN32
  602. mode = FTPTYPE_IMAGE;
  603. #endif
  604. if (ftp->autoseek && resumepos) {
  605. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
  606. if (outstream == NULL) {
  607. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
  608. }
  609. if (outstream != NULL) {
  610. /* if autoresume is wanted seek to end */
  611. if (resumepos == PHP_FTP_AUTORESUME) {
  612. php_stream_seek(outstream, 0, SEEK_END);
  613. resumepos = php_stream_tell(outstream);
  614. } else {
  615. php_stream_seek(outstream, resumepos, SEEK_SET);
  616. }
  617. }
  618. } else {
  619. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
  620. }
  621. if (outstream == NULL) {
  622. php_error_docref(NULL, E_WARNING, "Error opening %s", local);
  623. RETURN_FALSE;
  624. }
  625. if (!ftp_get(ftp, outstream, remote, remote_len, xtype, resumepos)) {
  626. php_stream_close(outstream);
  627. VCWD_UNLINK(local);
  628. if (*ftp->inbuf) {
  629. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  630. }
  631. RETURN_FALSE;
  632. }
  633. php_stream_close(outstream);
  634. RETURN_TRUE;
  635. }
  636. /* }}} */
  637. /* {{{ Retrieves a file from the FTP server nbhronly and writes it to a local file */
  638. PHP_FUNCTION(ftp_nb_get)
  639. {
  640. zval *z_ftp;
  641. ftpbuf_t *ftp;
  642. ftptype_t xtype;
  643. php_stream *outstream;
  644. char *local, *remote;
  645. size_t local_len, remote_len;
  646. int ret;
  647. zend_long mode=FTPTYPE_IMAGE, resumepos=0;
  648. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss|ll", &z_ftp, php_ftp_ce, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
  649. RETURN_THROWS();
  650. }
  651. GET_FTPBUF(ftp, z_ftp);
  652. XTYPE(xtype, mode);
  653. /* ignore autoresume if autoseek is switched off */
  654. if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
  655. resumepos = 0;
  656. }
  657. #ifdef PHP_WIN32
  658. mode = FTPTYPE_IMAGE;
  659. #endif
  660. if (ftp->autoseek && resumepos) {
  661. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
  662. if (outstream == NULL) {
  663. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
  664. }
  665. if (outstream != NULL) {
  666. /* if autoresume is wanted seek to end */
  667. if (resumepos == PHP_FTP_AUTORESUME) {
  668. php_stream_seek(outstream, 0, SEEK_END);
  669. resumepos = php_stream_tell(outstream);
  670. } else {
  671. php_stream_seek(outstream, resumepos, SEEK_SET);
  672. }
  673. }
  674. } else {
  675. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
  676. }
  677. if (outstream == NULL) {
  678. php_error_docref(NULL, E_WARNING, "Error opening %s", local);
  679. RETURN_FALSE;
  680. }
  681. /* configuration */
  682. ftp->direction = 0; /* recv */
  683. ftp->closestream = 1; /* do close */
  684. if ((ret = ftp_nb_get(ftp, outstream, remote, remote_len, xtype, resumepos)) == PHP_FTP_FAILED) {
  685. php_stream_close(outstream);
  686. ftp->stream = NULL;
  687. VCWD_UNLINK(local);
  688. if (*ftp->inbuf) {
  689. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  690. }
  691. RETURN_LONG(PHP_FTP_FAILED);
  692. }
  693. if (ret == PHP_FTP_FINISHED){
  694. php_stream_close(outstream);
  695. ftp->stream = NULL;
  696. }
  697. RETURN_LONG(ret);
  698. }
  699. /* }}} */
  700. /* {{{ Continues retrieving/sending a file nbronously */
  701. PHP_FUNCTION(ftp_nb_continue)
  702. {
  703. zval *z_ftp;
  704. ftpbuf_t *ftp;
  705. zend_long ret;
  706. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_ftp, php_ftp_ce) == FAILURE) {
  707. RETURN_THROWS();
  708. }
  709. GET_FTPBUF(ftp, z_ftp);
  710. if (!ftp->nb) {
  711. php_error_docref(NULL, E_WARNING, "No nbronous transfer to continue");
  712. RETURN_LONG(PHP_FTP_FAILED);
  713. }
  714. if (ftp->direction) {
  715. ret=ftp_nb_continue_write(ftp);
  716. } else {
  717. ret=ftp_nb_continue_read(ftp);
  718. }
  719. if (ret != PHP_FTP_MOREDATA && ftp->closestream) {
  720. php_stream_close(ftp->stream);
  721. ftp->stream = NULL;
  722. }
  723. if (ret == PHP_FTP_FAILED) {
  724. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  725. }
  726. RETURN_LONG(ret);
  727. }
  728. /* }}} */
  729. /* {{{ Stores a file from an open file to the FTP server */
  730. PHP_FUNCTION(ftp_fput)
  731. {
  732. zval *z_ftp, *z_file;
  733. ftpbuf_t *ftp;
  734. ftptype_t xtype;
  735. size_t remote_len;
  736. zend_long mode=FTPTYPE_IMAGE, startpos=0;
  737. php_stream *stream;
  738. char *remote;
  739. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osr|ll", &z_ftp, php_ftp_ce, &remote, &remote_len, &z_file, &mode, &startpos) == FAILURE) {
  740. RETURN_THROWS();
  741. }
  742. GET_FTPBUF(ftp, z_ftp);
  743. php_stream_from_zval(stream, z_file);
  744. XTYPE(xtype, mode);
  745. /* ignore autoresume if autoseek is switched off */
  746. if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
  747. startpos = 0;
  748. }
  749. if (ftp->autoseek && startpos) {
  750. /* if autoresume is wanted ask for remote size */
  751. if (startpos == PHP_FTP_AUTORESUME) {
  752. startpos = ftp_size(ftp, remote, remote_len);
  753. if (startpos < 0) {
  754. startpos = 0;
  755. }
  756. }
  757. if (startpos) {
  758. php_stream_seek(stream, startpos, SEEK_SET);
  759. }
  760. }
  761. if (!ftp_put(ftp, remote, remote_len, stream, xtype, startpos)) {
  762. if (*ftp->inbuf) {
  763. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  764. }
  765. RETURN_FALSE;
  766. }
  767. RETURN_TRUE;
  768. }
  769. /* }}} */
  770. /* {{{ Stores a file from an open file to the FTP server nbronly */
  771. PHP_FUNCTION(ftp_nb_fput)
  772. {
  773. zval *z_ftp, *z_file;
  774. ftpbuf_t *ftp;
  775. ftptype_t xtype;
  776. size_t remote_len;
  777. int ret;
  778. zend_long mode=FTPTYPE_IMAGE, startpos=0;
  779. php_stream *stream;
  780. char *remote;
  781. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osr|ll", &z_ftp, php_ftp_ce, &remote, &remote_len, &z_file, &mode, &startpos) == FAILURE) {
  782. RETURN_THROWS();
  783. }
  784. GET_FTPBUF(ftp, z_ftp);
  785. php_stream_from_res(stream, Z_RES_P(z_file));
  786. XTYPE(xtype, mode);
  787. /* ignore autoresume if autoseek is switched off */
  788. if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
  789. startpos = 0;
  790. }
  791. if (ftp->autoseek && startpos) {
  792. /* if autoresume is wanted ask for remote size */
  793. if (startpos == PHP_FTP_AUTORESUME) {
  794. startpos = ftp_size(ftp, remote, remote_len);
  795. if (startpos < 0) {
  796. startpos = 0;
  797. }
  798. }
  799. if (startpos) {
  800. php_stream_seek(stream, startpos, SEEK_SET);
  801. }
  802. }
  803. /* configuration */
  804. ftp->direction = 1; /* send */
  805. ftp->closestream = 0; /* do not close */
  806. if (((ret = ftp_nb_put(ftp, remote, remote_len, stream, xtype, startpos)) == PHP_FTP_FAILED)) {
  807. if (*ftp->inbuf) {
  808. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  809. }
  810. RETURN_LONG(ret);
  811. }
  812. RETURN_LONG(ret);
  813. }
  814. /* }}} */
  815. /* {{{ Stores a file on the FTP server */
  816. PHP_FUNCTION(ftp_put)
  817. {
  818. zval *z_ftp;
  819. ftpbuf_t *ftp;
  820. ftptype_t xtype;
  821. char *remote, *local;
  822. size_t remote_len, local_len;
  823. zend_long mode=FTPTYPE_IMAGE, startpos=0;
  824. php_stream *instream;
  825. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Opp|ll", &z_ftp, php_ftp_ce, &remote, &remote_len, &local, &local_len, &mode, &startpos) == FAILURE) {
  826. RETURN_THROWS();
  827. }
  828. GET_FTPBUF(ftp, z_ftp);
  829. XTYPE(xtype, mode);
  830. if (!(instream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt" : "rb", REPORT_ERRORS, NULL))) {
  831. RETURN_FALSE;
  832. }
  833. /* ignore autoresume if autoseek is switched off */
  834. if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
  835. startpos = 0;
  836. }
  837. if (ftp->autoseek && startpos) {
  838. /* if autoresume is wanted ask for remote size */
  839. if (startpos == PHP_FTP_AUTORESUME) {
  840. startpos = ftp_size(ftp, remote, remote_len);
  841. if (startpos < 0) {
  842. startpos = 0;
  843. }
  844. }
  845. if (startpos) {
  846. php_stream_seek(instream, startpos, SEEK_SET);
  847. }
  848. }
  849. if (!ftp_put(ftp, remote, remote_len, instream, xtype, startpos)) {
  850. php_stream_close(instream);
  851. if (*ftp->inbuf) {
  852. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  853. }
  854. RETURN_FALSE;
  855. }
  856. php_stream_close(instream);
  857. RETURN_TRUE;
  858. }
  859. /* }}} */
  860. /* {{{ Append content of a file a another file on the FTP server */
  861. PHP_FUNCTION(ftp_append)
  862. {
  863. zval *z_ftp;
  864. ftpbuf_t *ftp;
  865. ftptype_t xtype;
  866. char *remote, *local;
  867. size_t remote_len, local_len;
  868. zend_long mode=FTPTYPE_IMAGE;
  869. php_stream *instream;
  870. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Opp|l", &z_ftp, php_ftp_ce, &remote, &remote_len, &local, &local_len, &mode) == FAILURE) {
  871. RETURN_THROWS();
  872. }
  873. GET_FTPBUF(ftp, z_ftp);
  874. XTYPE(xtype, mode);
  875. if (!(instream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt" : "rb", REPORT_ERRORS, NULL))) {
  876. RETURN_FALSE;
  877. }
  878. if (!ftp_append(ftp, remote, remote_len, instream, xtype)) {
  879. php_stream_close(instream);
  880. if (*ftp->inbuf) {
  881. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  882. }
  883. RETURN_FALSE;
  884. }
  885. php_stream_close(instream);
  886. RETURN_TRUE;
  887. }
  888. /* }}} */
  889. /* {{{ Stores a file on the FTP server */
  890. PHP_FUNCTION(ftp_nb_put)
  891. {
  892. zval *z_ftp;
  893. ftpbuf_t *ftp;
  894. ftptype_t xtype;
  895. char *remote, *local;
  896. size_t remote_len, local_len;
  897. zend_long mode=FTPTYPE_IMAGE, startpos=0, ret;
  898. php_stream *instream;
  899. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Opp|ll", &z_ftp, php_ftp_ce, &remote, &remote_len, &local, &local_len, &mode, &startpos) == FAILURE) {
  900. RETURN_THROWS();
  901. }
  902. GET_FTPBUF(ftp, z_ftp);
  903. XTYPE(xtype, mode);
  904. if (!(instream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt" : "rb", REPORT_ERRORS, NULL))) {
  905. RETURN_FALSE;
  906. }
  907. /* ignore autoresume if autoseek is switched off */
  908. if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
  909. startpos = 0;
  910. }
  911. if (ftp->autoseek && startpos) {
  912. /* if autoresume is wanted ask for remote size */
  913. if (startpos == PHP_FTP_AUTORESUME) {
  914. startpos = ftp_size(ftp, remote, remote_len);
  915. if (startpos < 0) {
  916. startpos = 0;
  917. }
  918. }
  919. if (startpos) {
  920. php_stream_seek(instream, startpos, SEEK_SET);
  921. }
  922. }
  923. /* configuration */
  924. ftp->direction = 1; /* send */
  925. ftp->closestream = 1; /* do close */
  926. ret = ftp_nb_put(ftp, remote, remote_len, instream, xtype, startpos);
  927. if (ret != PHP_FTP_MOREDATA) {
  928. php_stream_close(instream);
  929. ftp->stream = NULL;
  930. }
  931. if (ret == PHP_FTP_FAILED) {
  932. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  933. }
  934. RETURN_LONG(ret);
  935. }
  936. /* }}} */
  937. /* {{{ Returns the size of the file, or -1 on error */
  938. PHP_FUNCTION(ftp_size)
  939. {
  940. zval *z_ftp;
  941. ftpbuf_t *ftp;
  942. char *file;
  943. size_t file_len;
  944. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op", &z_ftp, php_ftp_ce, &file, &file_len) == FAILURE) {
  945. RETURN_THROWS();
  946. }
  947. GET_FTPBUF(ftp, z_ftp);
  948. /* get file size */
  949. RETURN_LONG(ftp_size(ftp, file, file_len));
  950. }
  951. /* }}} */
  952. /* {{{ Returns the last modification time of the file, or -1 on error */
  953. PHP_FUNCTION(ftp_mdtm)
  954. {
  955. zval *z_ftp;
  956. ftpbuf_t *ftp;
  957. char *file;
  958. size_t file_len;
  959. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op", &z_ftp, php_ftp_ce, &file, &file_len) == FAILURE) {
  960. RETURN_THROWS();
  961. }
  962. GET_FTPBUF(ftp, z_ftp);
  963. /* get file mod time */
  964. RETURN_LONG(ftp_mdtm(ftp, file, file_len));
  965. }
  966. /* }}} */
  967. /* {{{ Renames the given file to a new path */
  968. PHP_FUNCTION(ftp_rename)
  969. {
  970. zval *z_ftp;
  971. ftpbuf_t *ftp;
  972. char *src, *dest;
  973. size_t src_len, dest_len;
  974. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss", &z_ftp, php_ftp_ce, &src, &src_len, &dest, &dest_len) == FAILURE) {
  975. RETURN_THROWS();
  976. }
  977. GET_FTPBUF(ftp, z_ftp);
  978. /* rename the file */
  979. if (!ftp_rename(ftp, src, src_len, dest, dest_len)) {
  980. if (*ftp->inbuf) {
  981. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  982. }
  983. RETURN_FALSE;
  984. }
  985. RETURN_TRUE;
  986. }
  987. /* }}} */
  988. /* {{{ Deletes a file */
  989. PHP_FUNCTION(ftp_delete)
  990. {
  991. zval *z_ftp;
  992. ftpbuf_t *ftp;
  993. char *file;
  994. size_t file_len;
  995. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &file, &file_len) == FAILURE) {
  996. RETURN_THROWS();
  997. }
  998. GET_FTPBUF(ftp, z_ftp);
  999. /* delete the file */
  1000. if (!ftp_delete(ftp, file, file_len)) {
  1001. if (*ftp->inbuf) {
  1002. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  1003. }
  1004. RETURN_FALSE;
  1005. }
  1006. RETURN_TRUE;
  1007. }
  1008. /* }}} */
  1009. /* {{{ Sends a SITE command to the server */
  1010. PHP_FUNCTION(ftp_site)
  1011. {
  1012. zval *z_ftp;
  1013. ftpbuf_t *ftp;
  1014. char *cmd;
  1015. size_t cmd_len;
  1016. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &cmd, &cmd_len) == FAILURE) {
  1017. RETURN_THROWS();
  1018. }
  1019. GET_FTPBUF(ftp, z_ftp);
  1020. /* send the site command */
  1021. if (!ftp_site(ftp, cmd, cmd_len)) {
  1022. if (*ftp->inbuf) {
  1023. php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
  1024. }
  1025. RETURN_FALSE;
  1026. }
  1027. RETURN_TRUE;
  1028. }
  1029. /* }}} */
  1030. /* {{{ Closes the FTP stream */
  1031. PHP_FUNCTION(ftp_close)
  1032. {
  1033. zval *z_ftp;
  1034. php_ftp_object *obj;
  1035. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_ftp, php_ftp_ce) == FAILURE) {
  1036. RETURN_THROWS();
  1037. }
  1038. obj = ftp_object_from_zend_object(Z_OBJ_P(z_ftp));
  1039. if (obj->ftp) {
  1040. ftp_quit(obj->ftp);
  1041. ftp_close(obj->ftp);
  1042. obj->ftp = NULL;
  1043. }
  1044. RETURN_TRUE;
  1045. }
  1046. /* }}} */
  1047. /* {{{ Sets an FTP option */
  1048. PHP_FUNCTION(ftp_set_option)
  1049. {
  1050. zval *z_ftp, *z_value;
  1051. zend_long option;
  1052. ftpbuf_t *ftp;
  1053. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olz", &z_ftp, php_ftp_ce, &option, &z_value) == FAILURE) {
  1054. RETURN_THROWS();
  1055. }
  1056. GET_FTPBUF(ftp, z_ftp);
  1057. switch (option) {
  1058. case PHP_FTP_OPT_TIMEOUT_SEC:
  1059. if (Z_TYPE_P(z_value) != IS_LONG) {
  1060. zend_argument_type_error(3, "must be of type int for the FTP_TIMEOUT_SEC option, %s given", zend_zval_type_name(z_value));
  1061. RETURN_THROWS();
  1062. }
  1063. if (Z_LVAL_P(z_value) <= 0) {
  1064. zend_argument_value_error(3, "must be greater than 0 for the FTP_TIMEOUT_SEC option");
  1065. RETURN_THROWS();
  1066. }
  1067. ftp->timeout_sec = Z_LVAL_P(z_value);
  1068. RETURN_TRUE;
  1069. break;
  1070. case PHP_FTP_OPT_AUTOSEEK:
  1071. if (Z_TYPE_P(z_value) != IS_TRUE && Z_TYPE_P(z_value) != IS_FALSE) {
  1072. zend_argument_type_error(3, "must be of type bool for the FTP_AUTOSEEK option, %s given", zend_zval_type_name(z_value));
  1073. RETURN_THROWS();
  1074. }
  1075. ftp->autoseek = Z_TYPE_P(z_value) == IS_TRUE ? 1 : 0;
  1076. RETURN_TRUE;
  1077. break;
  1078. case PHP_FTP_OPT_USEPASVADDRESS:
  1079. if (Z_TYPE_P(z_value) != IS_TRUE && Z_TYPE_P(z_value) != IS_FALSE) {
  1080. zend_argument_type_error(3, "must be of type bool for the FTP_USEPASVADDRESS option, %s given", zend_zval_type_name(z_value));
  1081. RETURN_THROWS();
  1082. }
  1083. ftp->usepasvaddress = Z_TYPE_P(z_value) == IS_TRUE ? 1 : 0;
  1084. RETURN_TRUE;
  1085. break;
  1086. default:
  1087. zend_argument_value_error(2, "must be one of FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS");
  1088. RETURN_THROWS();
  1089. break;
  1090. }
  1091. }
  1092. /* }}} */
  1093. /* {{{ Gets an FTP option */
  1094. PHP_FUNCTION(ftp_get_option)
  1095. {
  1096. zval *z_ftp;
  1097. zend_long option;
  1098. ftpbuf_t *ftp;
  1099. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &z_ftp, php_ftp_ce, &option) == FAILURE) {
  1100. RETURN_THROWS();
  1101. }
  1102. GET_FTPBUF(ftp, z_ftp);
  1103. switch (option) {
  1104. case PHP_FTP_OPT_TIMEOUT_SEC:
  1105. RETURN_LONG(ftp->timeout_sec);
  1106. break;
  1107. case PHP_FTP_OPT_AUTOSEEK:
  1108. RETURN_BOOL(ftp->autoseek);
  1109. break;
  1110. case PHP_FTP_OPT_USEPASVADDRESS:
  1111. RETURN_BOOL(ftp->usepasvaddress);
  1112. break;
  1113. default:
  1114. zend_argument_value_error(2, "must be one of FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS");
  1115. RETURN_THROWS();
  1116. }
  1117. }
  1118. /* }}} */
  1119. #endif /* HAVE_FTP */