php_ftp.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andrew Skalski <askalski@chek.com> |
  16. | Stefan Esser <sesser@php.net> (resume functions) |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #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_USEPASVADDRESS", PHP_FTP_OPT_USEPASVADDRESS, CONST_PERSISTENT | CONST_CS);
  270. REGISTER_LONG_CONSTANT("FTP_FAILED", PHP_FTP_FAILED, CONST_PERSISTENT | CONST_CS);
  271. REGISTER_LONG_CONSTANT("FTP_FINISHED", PHP_FTP_FINISHED, CONST_PERSISTENT | CONST_CS);
  272. REGISTER_LONG_CONSTANT("FTP_MOREDATA", PHP_FTP_MOREDATA, CONST_PERSISTENT | CONST_CS);
  273. return SUCCESS;
  274. }
  275. PHP_MINFO_FUNCTION(ftp)
  276. {
  277. php_info_print_table_start();
  278. php_info_print_table_row(2, "FTP support", "enabled");
  279. php_info_print_table_end();
  280. }
  281. #define XTYPE(xtype, mode) { \
  282. if (mode != FTPTYPE_ASCII && mode != FTPTYPE_IMAGE) { \
  283. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Mode must be FTP_ASCII or FTP_BINARY"); \
  284. RETURN_FALSE; \
  285. } \
  286. xtype = mode; \
  287. }
  288. /* {{{ proto resource ftp_connect(string host [, int port [, int timeout]])
  289. Opens a FTP stream */
  290. PHP_FUNCTION(ftp_connect)
  291. {
  292. ftpbuf_t *ftp;
  293. char *host;
  294. int host_len;
  295. long port = 0;
  296. long timeout_sec = FTP_DEFAULT_TIMEOUT;
  297. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &host, &host_len, &port, &timeout_sec) == FAILURE) {
  298. return;
  299. }
  300. if (timeout_sec <= 0) {
  301. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Timeout has to be greater than 0");
  302. RETURN_FALSE;
  303. }
  304. /* connect */
  305. if (!(ftp = ftp_open(host, (short)port, timeout_sec TSRMLS_CC))) {
  306. RETURN_FALSE;
  307. }
  308. /* autoseek for resuming */
  309. ftp->autoseek = FTP_DEFAULT_AUTOSEEK;
  310. ftp->usepasvaddress = FTP_DEFAULT_USEPASVADDRESS;
  311. #if HAVE_OPENSSL_EXT
  312. /* disable ssl */
  313. ftp->use_ssl = 0;
  314. #endif
  315. ZEND_REGISTER_RESOURCE(return_value, ftp, le_ftpbuf);
  316. }
  317. /* }}} */
  318. #if HAVE_OPENSSL_EXT
  319. /* {{{ proto resource ftp_ssl_connect(string host [, int port [, int timeout]])
  320. Opens a FTP-SSL stream */
  321. PHP_FUNCTION(ftp_ssl_connect)
  322. {
  323. ftpbuf_t *ftp;
  324. char *host;
  325. int host_len;
  326. long port = 0;
  327. long timeout_sec = FTP_DEFAULT_TIMEOUT;
  328. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &host, &host_len, &port, &timeout_sec) == FAILURE) {
  329. return;
  330. }
  331. if (timeout_sec <= 0) {
  332. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Timeout has to be greater than 0");
  333. RETURN_FALSE;
  334. }
  335. /* connect */
  336. if (!(ftp = ftp_open(host, (short)port, timeout_sec TSRMLS_CC))) {
  337. RETURN_FALSE;
  338. }
  339. /* autoseek for resuming */
  340. ftp->autoseek = FTP_DEFAULT_AUTOSEEK;
  341. ftp->usepasvaddress = FTP_DEFAULT_USEPASVADDRESS;
  342. /* enable ssl */
  343. ftp->use_ssl = 1;
  344. ZEND_REGISTER_RESOURCE(return_value, ftp, le_ftpbuf);
  345. }
  346. /* }}} */
  347. #endif
  348. /* {{{ proto bool ftp_login(resource stream, string username, string password)
  349. Logs into the FTP server */
  350. PHP_FUNCTION(ftp_login)
  351. {
  352. zval *z_ftp;
  353. ftpbuf_t *ftp;
  354. char *user, *pass;
  355. int user_len, pass_len;
  356. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &z_ftp, &user, &user_len, &pass, &pass_len) == FAILURE) {
  357. return;
  358. }
  359. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  360. /* log in */
  361. if (!ftp_login(ftp, user, pass TSRMLS_CC)) {
  362. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  363. RETURN_FALSE;
  364. }
  365. RETURN_TRUE;
  366. }
  367. /* }}} */
  368. /* {{{ proto string ftp_pwd(resource stream)
  369. Returns the present working directory */
  370. PHP_FUNCTION(ftp_pwd)
  371. {
  372. zval *z_ftp;
  373. ftpbuf_t *ftp;
  374. const char *pwd;
  375. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
  376. return;
  377. }
  378. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  379. if (!(pwd = ftp_pwd(ftp))) {
  380. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  381. RETURN_FALSE;
  382. }
  383. RETURN_STRING((char*) pwd, 1);
  384. }
  385. /* }}} */
  386. /* {{{ proto bool ftp_cdup(resource stream)
  387. Changes to the parent directory */
  388. PHP_FUNCTION(ftp_cdup)
  389. {
  390. zval *z_ftp;
  391. ftpbuf_t *ftp;
  392. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
  393. return;
  394. }
  395. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  396. if (!ftp_cdup(ftp)) {
  397. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  398. RETURN_FALSE;
  399. }
  400. RETURN_TRUE;
  401. }
  402. /* }}} */
  403. /* {{{ proto bool ftp_chdir(resource stream, string directory)
  404. Changes directories */
  405. PHP_FUNCTION(ftp_chdir)
  406. {
  407. zval *z_ftp;
  408. ftpbuf_t *ftp;
  409. char *dir;
  410. int dir_len;
  411. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &dir, &dir_len) == FAILURE) {
  412. return;
  413. }
  414. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  415. /* change directories */
  416. if (!ftp_chdir(ftp, dir)) {
  417. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  418. RETURN_FALSE;
  419. }
  420. RETURN_TRUE;
  421. }
  422. /* }}} */
  423. /* {{{ proto bool ftp_exec(resource stream, string command)
  424. Requests execution of a program on the FTP server */
  425. PHP_FUNCTION(ftp_exec)
  426. {
  427. zval *z_ftp;
  428. ftpbuf_t *ftp;
  429. char *cmd;
  430. int cmd_len;
  431. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &cmd, &cmd_len) == FAILURE) {
  432. return;
  433. }
  434. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  435. /* execute serverside command */
  436. if (!ftp_exec(ftp, cmd)) {
  437. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  438. RETURN_FALSE;
  439. }
  440. RETURN_TRUE;
  441. }
  442. /* }}} */
  443. /* {{{ proto array ftp_raw(resource stream, string command)
  444. Sends a literal command to the FTP server */
  445. PHP_FUNCTION(ftp_raw)
  446. {
  447. zval *z_ftp;
  448. ftpbuf_t *ftp;
  449. char *cmd;
  450. int cmd_len;
  451. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &cmd, &cmd_len) == FAILURE) {
  452. return;
  453. }
  454. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  455. /* execute arbitrary ftp command */
  456. ftp_raw(ftp, cmd, return_value);
  457. }
  458. /* }}} */
  459. /* {{{ proto string ftp_mkdir(resource stream, string directory)
  460. Creates a directory and returns the absolute path for the new directory or false on error */
  461. PHP_FUNCTION(ftp_mkdir)
  462. {
  463. zval *z_ftp;
  464. ftpbuf_t *ftp;
  465. char *dir, *tmp;
  466. int dir_len;
  467. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &dir, &dir_len) == FAILURE) {
  468. return;
  469. }
  470. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  471. /* create directorie */
  472. if (NULL == (tmp = ftp_mkdir(ftp, dir))) {
  473. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  474. RETURN_FALSE;
  475. }
  476. RETURN_STRING(tmp, 0);
  477. }
  478. /* }}} */
  479. /* {{{ proto bool ftp_rmdir(resource stream, string directory)
  480. Removes a directory */
  481. PHP_FUNCTION(ftp_rmdir)
  482. {
  483. zval *z_ftp;
  484. ftpbuf_t *ftp;
  485. char *dir;
  486. int dir_len;
  487. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &dir, &dir_len) == FAILURE) {
  488. return;
  489. }
  490. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  491. /* remove directorie */
  492. if (!ftp_rmdir(ftp, dir)) {
  493. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  494. RETURN_FALSE;
  495. }
  496. RETURN_TRUE;
  497. }
  498. /* }}} */
  499. /* {{{ proto int ftp_chmod(resource stream, int mode, string filename)
  500. Sets permissions on a file */
  501. PHP_FUNCTION(ftp_chmod)
  502. {
  503. zval *z_ftp;
  504. ftpbuf_t *ftp;
  505. char *filename;
  506. int filename_len;
  507. long mode;
  508. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlp", &z_ftp, &mode, &filename, &filename_len) == FAILURE) {
  509. RETURN_FALSE;
  510. }
  511. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  512. if (!ftp_chmod(ftp, mode, filename, filename_len)) {
  513. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  514. RETURN_FALSE;
  515. }
  516. RETURN_LONG(mode);
  517. }
  518. /* }}} */
  519. /* {{{ proto bool ftp_alloc(resource stream, int size[, &response])
  520. Attempt to allocate space on the remote FTP server */
  521. PHP_FUNCTION(ftp_alloc)
  522. {
  523. zval *z_ftp, *zresponse = NULL;
  524. ftpbuf_t *ftp;
  525. long size, ret;
  526. char *response = NULL;
  527. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|z", &z_ftp, &size, &zresponse) == FAILURE) {
  528. RETURN_FALSE;
  529. }
  530. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  531. ret = ftp_alloc(ftp, size, zresponse ? &response : NULL);
  532. if (response) {
  533. zval_dtor(zresponse);
  534. ZVAL_STRING(zresponse, response, 0);
  535. }
  536. if (!ret) {
  537. RETURN_FALSE;
  538. }
  539. RETURN_TRUE;
  540. }
  541. /* }}} */
  542. /* {{{ proto array ftp_nlist(resource stream, string directory)
  543. Returns an array of filenames in the given directory */
  544. PHP_FUNCTION(ftp_nlist)
  545. {
  546. zval *z_ftp;
  547. ftpbuf_t *ftp;
  548. char **nlist, **ptr, *dir;
  549. int dir_len;
  550. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rp", &z_ftp, &dir, &dir_len) == FAILURE) {
  551. return;
  552. }
  553. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  554. /* get list of files */
  555. if (NULL == (nlist = ftp_nlist(ftp, dir TSRMLS_CC))) {
  556. RETURN_FALSE;
  557. }
  558. array_init(return_value);
  559. for (ptr = nlist; *ptr; ptr++) {
  560. add_next_index_string(return_value, *ptr, 1);
  561. }
  562. efree(nlist);
  563. }
  564. /* }}} */
  565. /* {{{ proto array ftp_rawlist(resource stream, string directory [, bool recursive])
  566. Returns a detailed listing of a directory as an array of output lines */
  567. PHP_FUNCTION(ftp_rawlist)
  568. {
  569. zval *z_ftp;
  570. ftpbuf_t *ftp;
  571. char **llist, **ptr, *dir;
  572. int dir_len;
  573. zend_bool recursive = 0;
  574. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|b", &z_ftp, &dir, &dir_len, &recursive) == FAILURE) {
  575. return;
  576. }
  577. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  578. /* get raw directory listing */
  579. if (NULL == (llist = ftp_list(ftp, dir, recursive TSRMLS_CC))) {
  580. RETURN_FALSE;
  581. }
  582. array_init(return_value);
  583. for (ptr = llist; *ptr; ptr++) {
  584. add_next_index_string(return_value, *ptr, 1);
  585. }
  586. efree(llist);
  587. }
  588. /* }}} */
  589. /* {{{ proto string ftp_systype(resource stream)
  590. Returns the system type identifier */
  591. PHP_FUNCTION(ftp_systype)
  592. {
  593. zval *z_ftp;
  594. ftpbuf_t *ftp;
  595. const char *syst;
  596. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
  597. return;
  598. }
  599. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  600. if (NULL == (syst = ftp_syst(ftp))) {
  601. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  602. RETURN_FALSE;
  603. }
  604. RETURN_STRING((char*) syst, 1);
  605. }
  606. /* }}} */
  607. /* {{{ proto bool ftp_fget(resource stream, resource fp, string remote_file, int mode[, int resumepos])
  608. Retrieves a file from the FTP server and writes it to an open file */
  609. PHP_FUNCTION(ftp_fget)
  610. {
  611. zval *z_ftp, *z_file;
  612. ftpbuf_t *ftp;
  613. ftptype_t xtype;
  614. php_stream *stream;
  615. char *file;
  616. int file_len;
  617. long mode, resumepos=0;
  618. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrsl|l", &z_ftp, &z_file, &file, &file_len, &mode, &resumepos) == FAILURE) {
  619. return;
  620. }
  621. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  622. php_stream_from_zval(stream, &z_file);
  623. XTYPE(xtype, mode);
  624. /* ignore autoresume if autoseek is switched off */
  625. if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
  626. resumepos = 0;
  627. }
  628. if (ftp->autoseek && resumepos) {
  629. /* if autoresume is wanted seek to end */
  630. if (resumepos == PHP_FTP_AUTORESUME) {
  631. php_stream_seek(stream, 0, SEEK_END);
  632. resumepos = php_stream_tell(stream);
  633. } else {
  634. php_stream_seek(stream, resumepos, SEEK_SET);
  635. }
  636. }
  637. if (!ftp_get(ftp, stream, file, xtype, resumepos TSRMLS_CC)) {
  638. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  639. RETURN_FALSE;
  640. }
  641. RETURN_TRUE;
  642. }
  643. /* }}} */
  644. /* {{{ proto int ftp_nb_fget(resource stream, resource fp, string remote_file, int mode[, int resumepos])
  645. Retrieves a file from the FTP server asynchronly and writes it to an open file */
  646. PHP_FUNCTION(ftp_nb_fget)
  647. {
  648. zval *z_ftp, *z_file;
  649. ftpbuf_t *ftp;
  650. ftptype_t xtype;
  651. php_stream *stream;
  652. char *file;
  653. int file_len;
  654. long mode, resumepos=0, ret;
  655. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrsl|l", &z_ftp, &z_file, &file, &file_len, &mode, &resumepos) == FAILURE) {
  656. return;
  657. }
  658. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  659. php_stream_from_zval(stream, &z_file);
  660. XTYPE(xtype, mode);
  661. /* ignore autoresume if autoseek is switched off */
  662. if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
  663. resumepos = 0;
  664. }
  665. if (ftp->autoseek && resumepos) {
  666. /* if autoresume is wanted seek to end */
  667. if (resumepos == PHP_FTP_AUTORESUME) {
  668. php_stream_seek(stream, 0, SEEK_END);
  669. resumepos = php_stream_tell(stream);
  670. } else {
  671. php_stream_seek(stream, resumepos, SEEK_SET);
  672. }
  673. }
  674. /* configuration */
  675. ftp->direction = 0; /* recv */
  676. ftp->closestream = 0; /* do not close */
  677. if ((ret = ftp_nb_get(ftp, stream, file, xtype, resumepos TSRMLS_CC)) == PHP_FTP_FAILED) {
  678. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  679. RETURN_LONG(ret);
  680. }
  681. RETURN_LONG(ret);
  682. }
  683. /* }}} */
  684. /* {{{ proto bool ftp_pasv(resource stream, bool pasv)
  685. Turns passive mode on or off */
  686. PHP_FUNCTION(ftp_pasv)
  687. {
  688. zval *z_ftp;
  689. ftpbuf_t *ftp;
  690. zend_bool pasv;
  691. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb", &z_ftp, &pasv) == FAILURE) {
  692. return;
  693. }
  694. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  695. if (!ftp_pasv(ftp, pasv ? 1 : 0)) {
  696. RETURN_FALSE;
  697. }
  698. RETURN_TRUE;
  699. }
  700. /* }}} */
  701. /* {{{ proto bool ftp_get(resource stream, string local_file, string remote_file, int mode[, int resume_pos])
  702. Retrieves a file from the FTP server and writes it to a local file */
  703. PHP_FUNCTION(ftp_get)
  704. {
  705. zval *z_ftp;
  706. ftpbuf_t *ftp;
  707. ftptype_t xtype;
  708. php_stream *outstream;
  709. char *local, *remote;
  710. int local_len, remote_len;
  711. long mode, resumepos=0;
  712. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rppl|l", &z_ftp, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
  713. return;
  714. }
  715. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  716. XTYPE(xtype, mode);
  717. /* ignore autoresume if autoseek is switched off */
  718. if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
  719. resumepos = 0;
  720. }
  721. #ifdef PHP_WIN32
  722. mode = FTPTYPE_IMAGE;
  723. #endif
  724. if (ftp->autoseek && resumepos) {
  725. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
  726. if (outstream == NULL) {
  727. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
  728. }
  729. if (outstream != NULL) {
  730. /* if autoresume is wanted seek to end */
  731. if (resumepos == PHP_FTP_AUTORESUME) {
  732. php_stream_seek(outstream, 0, SEEK_END);
  733. resumepos = php_stream_tell(outstream);
  734. } else {
  735. php_stream_seek(outstream, resumepos, SEEK_SET);
  736. }
  737. }
  738. } else {
  739. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
  740. }
  741. if (outstream == NULL) {
  742. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error opening %s", local);
  743. RETURN_FALSE;
  744. }
  745. if (!ftp_get(ftp, outstream, remote, xtype, resumepos TSRMLS_CC)) {
  746. php_stream_close(outstream);
  747. VCWD_UNLINK(local);
  748. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  749. RETURN_FALSE;
  750. }
  751. php_stream_close(outstream);
  752. RETURN_TRUE;
  753. }
  754. /* }}} */
  755. /* {{{ proto int ftp_nb_get(resource stream, string local_file, string remote_file, int mode[, int resume_pos])
  756. Retrieves a file from the FTP server nbhronly and writes it to a local file */
  757. PHP_FUNCTION(ftp_nb_get)
  758. {
  759. zval *z_ftp;
  760. ftpbuf_t *ftp;
  761. ftptype_t xtype;
  762. php_stream *outstream;
  763. char *local, *remote;
  764. int local_len, remote_len, ret;
  765. long mode, resumepos=0;
  766. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rssl|l", &z_ftp, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
  767. return;
  768. }
  769. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  770. XTYPE(xtype, mode);
  771. /* ignore autoresume if autoseek is switched off */
  772. if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
  773. resumepos = 0;
  774. }
  775. #ifdef PHP_WIN32
  776. mode = FTPTYPE_IMAGE;
  777. #endif
  778. if (ftp->autoseek && resumepos) {
  779. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
  780. if (outstream == NULL) {
  781. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
  782. }
  783. if (outstream != NULL) {
  784. /* if autoresume is wanted seek to end */
  785. if (resumepos == PHP_FTP_AUTORESUME) {
  786. php_stream_seek(outstream, 0, SEEK_END);
  787. resumepos = php_stream_tell(outstream);
  788. } else {
  789. php_stream_seek(outstream, resumepos, SEEK_SET);
  790. }
  791. }
  792. } else {
  793. outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
  794. }
  795. if (outstream == NULL) {
  796. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error opening %s", local);
  797. RETURN_FALSE;
  798. }
  799. /* configuration */
  800. ftp->direction = 0; /* recv */
  801. ftp->closestream = 1; /* do close */
  802. if ((ret = ftp_nb_get(ftp, outstream, remote, xtype, resumepos TSRMLS_CC)) == PHP_FTP_FAILED) {
  803. php_stream_close(outstream);
  804. ftp->stream = NULL;
  805. VCWD_UNLINK(local);
  806. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  807. RETURN_LONG(PHP_FTP_FAILED);
  808. }
  809. if (ret == PHP_FTP_FINISHED){
  810. php_stream_close(outstream);
  811. ftp->stream = NULL;
  812. }
  813. RETURN_LONG(ret);
  814. }
  815. /* }}} */
  816. /* {{{ proto int ftp_nb_continue(resource stream)
  817. Continues retrieving/sending a file nbronously */
  818. PHP_FUNCTION(ftp_nb_continue)
  819. {
  820. zval *z_ftp;
  821. ftpbuf_t *ftp;
  822. long ret;
  823. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
  824. return;
  825. }
  826. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  827. if (!ftp->nb) {
  828. php_error_docref(NULL TSRMLS_CC, E_WARNING, "no nbronous transfer to continue.");
  829. RETURN_LONG(PHP_FTP_FAILED);
  830. }
  831. if (ftp->direction) {
  832. ret=ftp_nb_continue_write(ftp TSRMLS_CC);
  833. } else {
  834. ret=ftp_nb_continue_read(ftp TSRMLS_CC);
  835. }
  836. if (ret != PHP_FTP_MOREDATA && ftp->closestream) {
  837. php_stream_close(ftp->stream);
  838. ftp->stream = NULL;
  839. }
  840. if (ret == PHP_FTP_FAILED) {
  841. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  842. }
  843. RETURN_LONG(ret);
  844. }
  845. /* }}} */
  846. /* {{{ proto bool ftp_fput(resource stream, string remote_file, resource fp, int mode[, int startpos])
  847. Stores a file from an open file to the FTP server */
  848. PHP_FUNCTION(ftp_fput)
  849. {
  850. zval *z_ftp, *z_file;
  851. ftpbuf_t *ftp;
  852. ftptype_t xtype;
  853. int remote_len;
  854. long mode, startpos=0;
  855. php_stream *stream;
  856. char *remote;
  857. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsrl|l", &z_ftp, &remote, &remote_len, &z_file, &mode, &startpos) == FAILURE) {
  858. return;
  859. }
  860. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  861. php_stream_from_zval(stream, &z_file);
  862. XTYPE(xtype, mode);
  863. /* ignore autoresume if autoseek is switched off */
  864. if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
  865. startpos = 0;
  866. }
  867. if (ftp->autoseek && startpos) {
  868. /* if autoresume is wanted ask for remote size */
  869. if (startpos == PHP_FTP_AUTORESUME) {
  870. startpos = ftp_size(ftp, remote);
  871. if (startpos < 0) {
  872. startpos = 0;
  873. }
  874. }
  875. if (startpos) {
  876. php_stream_seek(stream, startpos, SEEK_SET);
  877. }
  878. }
  879. if (!ftp_put(ftp, remote, stream, xtype, startpos TSRMLS_CC)) {
  880. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  881. RETURN_FALSE;
  882. }
  883. RETURN_TRUE;
  884. }
  885. /* }}} */
  886. /* {{{ proto int ftp_nb_fput(resource stream, string remote_file, resource fp, int mode[, int startpos])
  887. Stores a file from an open file to the FTP server nbronly */
  888. PHP_FUNCTION(ftp_nb_fput)
  889. {
  890. zval *z_ftp, *z_file;
  891. ftpbuf_t *ftp;
  892. ftptype_t xtype;
  893. int remote_len, ret;
  894. long mode, startpos=0;
  895. php_stream *stream;
  896. char *remote;
  897. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsrl|l", &z_ftp, &remote, &remote_len, &z_file, &mode, &startpos) == FAILURE) {
  898. return;
  899. }
  900. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  901. php_stream_from_zval(stream, &z_file);
  902. XTYPE(xtype, mode);
  903. /* ignore autoresume if autoseek is switched off */
  904. if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
  905. startpos = 0;
  906. }
  907. if (ftp->autoseek && startpos) {
  908. /* if autoresume is wanted ask for remote size */
  909. if (startpos == PHP_FTP_AUTORESUME) {
  910. startpos = ftp_size(ftp, remote);
  911. if (startpos < 0) {
  912. startpos = 0;
  913. }
  914. }
  915. if (startpos) {
  916. php_stream_seek(stream, startpos, SEEK_SET);
  917. }
  918. }
  919. /* configuration */
  920. ftp->direction = 1; /* send */
  921. ftp->closestream = 0; /* do not close */
  922. if (((ret = ftp_nb_put(ftp, remote, stream, xtype, startpos TSRMLS_CC)) == PHP_FTP_FAILED)) {
  923. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  924. RETURN_LONG(ret);
  925. }
  926. RETURN_LONG(ret);
  927. }
  928. /* }}} */
  929. /* {{{ proto bool ftp_put(resource stream, string remote_file, string local_file, int mode[, int startpos])
  930. Stores a file on the FTP server */
  931. PHP_FUNCTION(ftp_put)
  932. {
  933. zval *z_ftp;
  934. ftpbuf_t *ftp;
  935. ftptype_t xtype;
  936. char *remote, *local;
  937. long remote_len, local_len;
  938. long mode, startpos=0;
  939. php_stream *instream;
  940. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rppl|l", &z_ftp, &remote, &remote_len, &local, &local_len, &mode, &startpos) == FAILURE) {
  941. return;
  942. }
  943. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  944. XTYPE(xtype, mode);
  945. if (!(instream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt" : "rb", REPORT_ERRORS, NULL))) {
  946. RETURN_FALSE;
  947. }
  948. /* ignore autoresume if autoseek is switched off */
  949. if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
  950. startpos = 0;
  951. }
  952. if (ftp->autoseek && startpos) {
  953. /* if autoresume is wanted ask for remote size */
  954. if (startpos == PHP_FTP_AUTORESUME) {
  955. startpos = ftp_size(ftp, remote);
  956. if (startpos < 0) {
  957. startpos = 0;
  958. }
  959. }
  960. if (startpos) {
  961. php_stream_seek(instream, startpos, SEEK_SET);
  962. }
  963. }
  964. if (!ftp_put(ftp, remote, instream, xtype, startpos TSRMLS_CC)) {
  965. php_stream_close(instream);
  966. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  967. RETURN_FALSE;
  968. }
  969. php_stream_close(instream);
  970. RETURN_TRUE;
  971. }
  972. /* }}} */
  973. /* {{{ proto int ftp_nb_put(resource stream, string remote_file, string local_file, int mode[, int startpos])
  974. Stores a file on the FTP server */
  975. PHP_FUNCTION(ftp_nb_put)
  976. {
  977. zval *z_ftp;
  978. ftpbuf_t *ftp;
  979. ftptype_t xtype;
  980. char *remote, *local;
  981. int remote_len, local_len;
  982. long mode, startpos=0, ret;
  983. php_stream *instream;
  984. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rppl|l", &z_ftp, &remote, &remote_len, &local, &local_len, &mode, &startpos) == FAILURE) {
  985. return;
  986. }
  987. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  988. XTYPE(xtype, mode);
  989. if (!(instream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt" : "rb", REPORT_ERRORS, NULL))) {
  990. RETURN_FALSE;
  991. }
  992. /* ignore autoresume if autoseek is switched off */
  993. if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
  994. startpos = 0;
  995. }
  996. if (ftp->autoseek && startpos) {
  997. /* if autoresume is wanted ask for remote size */
  998. if (startpos == PHP_FTP_AUTORESUME) {
  999. startpos = ftp_size(ftp, remote);
  1000. if (startpos < 0) {
  1001. startpos = 0;
  1002. }
  1003. }
  1004. if (startpos) {
  1005. php_stream_seek(instream, startpos, SEEK_SET);
  1006. }
  1007. }
  1008. /* configuration */
  1009. ftp->direction = 1; /* send */
  1010. ftp->closestream = 1; /* do close */
  1011. ret = ftp_nb_put(ftp, remote, instream, xtype, startpos TSRMLS_CC);
  1012. if (ret != PHP_FTP_MOREDATA) {
  1013. php_stream_close(instream);
  1014. ftp->stream = NULL;
  1015. }
  1016. if (ret == PHP_FTP_FAILED) {
  1017. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  1018. }
  1019. RETURN_LONG(ret);
  1020. }
  1021. /* }}} */
  1022. /* {{{ proto int ftp_size(resource stream, string filename)
  1023. Returns the size of the file, or -1 on error */
  1024. PHP_FUNCTION(ftp_size)
  1025. {
  1026. zval *z_ftp;
  1027. ftpbuf_t *ftp;
  1028. char *file;
  1029. int file_len;
  1030. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rp", &z_ftp, &file, &file_len) == FAILURE) {
  1031. return;
  1032. }
  1033. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  1034. /* get file size */
  1035. RETURN_LONG(ftp_size(ftp, file));
  1036. }
  1037. /* }}} */
  1038. /* {{{ proto int ftp_mdtm(resource stream, string filename)
  1039. Returns the last modification time of the file, or -1 on error */
  1040. PHP_FUNCTION(ftp_mdtm)
  1041. {
  1042. zval *z_ftp;
  1043. ftpbuf_t *ftp;
  1044. char *file;
  1045. int file_len;
  1046. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rp", &z_ftp, &file, &file_len) == FAILURE) {
  1047. return;
  1048. }
  1049. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  1050. /* get file mod time */
  1051. RETURN_LONG(ftp_mdtm(ftp, file));
  1052. }
  1053. /* }}} */
  1054. /* {{{ proto bool ftp_rename(resource stream, string src, string dest)
  1055. Renames the given file to a new path */
  1056. PHP_FUNCTION(ftp_rename)
  1057. {
  1058. zval *z_ftp;
  1059. ftpbuf_t *ftp;
  1060. char *src, *dest;
  1061. int src_len, dest_len;
  1062. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &z_ftp, &src, &src_len, &dest, &dest_len) == FAILURE) {
  1063. return;
  1064. }
  1065. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  1066. /* rename the file */
  1067. if (!ftp_rename(ftp, src, dest)) {
  1068. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  1069. RETURN_FALSE;
  1070. }
  1071. RETURN_TRUE;
  1072. }
  1073. /* }}} */
  1074. /* {{{ proto bool ftp_delete(resource stream, string file)
  1075. Deletes a file */
  1076. PHP_FUNCTION(ftp_delete)
  1077. {
  1078. zval *z_ftp;
  1079. ftpbuf_t *ftp;
  1080. char *file;
  1081. int file_len;
  1082. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &file, &file_len) == FAILURE) {
  1083. return;
  1084. }
  1085. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  1086. /* delete the file */
  1087. if (!ftp_delete(ftp, file)) {
  1088. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  1089. RETURN_FALSE;
  1090. }
  1091. RETURN_TRUE;
  1092. }
  1093. /* }}} */
  1094. /* {{{ proto bool ftp_site(resource stream, string cmd)
  1095. Sends a SITE command to the server */
  1096. PHP_FUNCTION(ftp_site)
  1097. {
  1098. zval *z_ftp;
  1099. ftpbuf_t *ftp;
  1100. char *cmd;
  1101. int cmd_len;
  1102. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &cmd, &cmd_len) == FAILURE) {
  1103. return;
  1104. }
  1105. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  1106. /* send the site command */
  1107. if (!ftp_site(ftp, cmd)) {
  1108. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
  1109. RETURN_FALSE;
  1110. }
  1111. RETURN_TRUE;
  1112. }
  1113. /* }}} */
  1114. /* {{{ proto bool ftp_close(resource stream)
  1115. Closes the FTP stream */
  1116. PHP_FUNCTION(ftp_close)
  1117. {
  1118. zval *z_ftp;
  1119. ftpbuf_t *ftp;
  1120. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
  1121. return;
  1122. }
  1123. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  1124. ftp_quit(ftp);
  1125. RETURN_BOOL(zend_list_delete(Z_LVAL_P(z_ftp)) == SUCCESS);
  1126. }
  1127. /* }}} */
  1128. /* {{{ proto bool ftp_set_option(resource stream, int option, mixed value)
  1129. Sets an FTP option */
  1130. PHP_FUNCTION(ftp_set_option)
  1131. {
  1132. zval *z_ftp, *z_value;
  1133. long option;
  1134. ftpbuf_t *ftp;
  1135. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &z_ftp, &option, &z_value) == FAILURE) {
  1136. return;
  1137. }
  1138. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  1139. switch (option) {
  1140. case PHP_FTP_OPT_TIMEOUT_SEC:
  1141. if (Z_TYPE_P(z_value) != IS_LONG) {
  1142. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Option TIMEOUT_SEC expects value of type long, %s given",
  1143. zend_zval_type_name(z_value));
  1144. RETURN_FALSE;
  1145. }
  1146. if (Z_LVAL_P(z_value) <= 0) {
  1147. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Timeout has to be greater than 0");
  1148. RETURN_FALSE;
  1149. }
  1150. ftp->timeout_sec = Z_LVAL_P(z_value);
  1151. RETURN_TRUE;
  1152. break;
  1153. case PHP_FTP_OPT_AUTOSEEK:
  1154. if (Z_TYPE_P(z_value) != IS_BOOL) {
  1155. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Option AUTOSEEK expects value of type boolean, %s given",
  1156. zend_zval_type_name(z_value));
  1157. RETURN_FALSE;
  1158. }
  1159. ftp->autoseek = Z_LVAL_P(z_value);
  1160. RETURN_TRUE;
  1161. break;
  1162. case PHP_FTP_OPT_USEPASVADDRESS:
  1163. if (Z_TYPE_P(z_value) != IS_BOOL) {
  1164. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Option USEPASVADDRESS expects value of type boolean, %s given",
  1165. zend_zval_type_name(z_value));
  1166. RETURN_FALSE;
  1167. }
  1168. ftp->usepasvaddress = Z_LVAL_P(z_value);
  1169. RETURN_TRUE;
  1170. break;
  1171. default:
  1172. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown option '%ld'", option);
  1173. RETURN_FALSE;
  1174. break;
  1175. }
  1176. }
  1177. /* }}} */
  1178. /* {{{ proto mixed ftp_get_option(resource stream, int option)
  1179. Gets an FTP option */
  1180. PHP_FUNCTION(ftp_get_option)
  1181. {
  1182. zval *z_ftp;
  1183. long option;
  1184. ftpbuf_t *ftp;
  1185. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &z_ftp, &option) == FAILURE) {
  1186. return;
  1187. }
  1188. ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
  1189. switch (option) {
  1190. case PHP_FTP_OPT_TIMEOUT_SEC:
  1191. RETURN_LONG(ftp->timeout_sec);
  1192. break;
  1193. case PHP_FTP_OPT_AUTOSEEK:
  1194. RETURN_BOOL(ftp->autoseek);
  1195. break;
  1196. case PHP_FTP_OPT_USEPASVADDRESS:
  1197. RETURN_BOOL(ftp->usepasvaddress);
  1198. break;
  1199. default:
  1200. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown option '%ld'", option);
  1201. RETURN_FALSE;
  1202. break;
  1203. }
  1204. }
  1205. /* }}} */
  1206. #endif /* HAVE_FTP */
  1207. /*
  1208. * Local variables:
  1209. * tab-width: 4
  1210. * c-basic-offset: 4
  1211. * indent-tabs-mode: t
  1212. * End:
  1213. */