PageRenderTime 73ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext_zend_compat/ftp/php_ftp.cpp

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