summaryrefslogtreecommitdiff
path: root/ext/ftp/php_ftp.c
blob: fdffdf7b15d41955f5b4df0064f1623885ef07f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 3.0                     |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-1999 PHP Development Team (See Credits file)      |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses:                 |
   |                                                                      |
   |  A) the GNU General Public License as published by the Free Software |
   |     Foundation; either version 2 of the License, or (at your option) |
   |     any later version.                                               |
   |                                                                      |
   |  B) the PHP License as published by the PHP Development Team and     |
   |     included in the distribution in the file: LICENSE                |
   |                                                                      |
   | This program is distributed in the hope that it will be useful,      |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of both licenses referred to here.   |
   | If you did not, or have any questions about PHP licensing, please    |
   | contact core@php.net.                                                |
   +----------------------------------------------------------------------+
   | Authors:                                                             |
   |          Andrew Skalski      <askalski@chek.com>                     |
   +----------------------------------------------------------------------+
 */

#include "php.h"

#if HAVE_FTP

#ifndef ZEND_VERSION
#include "internal_functions.h"
#include "php3_list.h"
#define php_error php3_error
#endif

#include "php_ftp.h"
#include "ftp.h"

static int	le_ftpbuf;


function_entry php3_ftp_functions[] = {
	PHP_FE(ftp_connect,			NULL)
	PHP_FE(ftp_login,			NULL)
	PHP_FE(ftp_pwd,				NULL)
	PHP_FE(ftp_cdup,			NULL)
	PHP_FE(ftp_chdir,			NULL)
	PHP_FE(ftp_mkdir,			NULL)
	PHP_FE(ftp_rmdir,			NULL)
	PHP_FE(ftp_nlist,			NULL)
	PHP_FE(ftp_rawlist,			NULL)
	PHP_FE(ftp_systype,			NULL)
	PHP_FE(ftp_get,				NULL)
	PHP_FE(ftp_put,				NULL)
	PHP_FE(ftp_quit,			NULL)
	{NULL, NULL, NULL}
};

php3_module_entry php3_ftp_module_entry = {
	"FTP Functions",
	php3_ftp_functions,
#ifdef ZEND_VERSION
	PHP_MINIT(ftp),
#else
	PHP_MINIT_FUNCTION,
#endif
	NULL,
	NULL,
	NULL,
	NULL,
	STANDARD_MODULE_PROPERTIES
};

static void ftp_destructor_ftpbuf(ftpbuf_t *ftp)
{
	ftp_close(ftp);
}

#ifdef ZEND_VERSION
PHP_MINIT_FUNCTION(ftp)
#else
int PHP_MINIT_FUNCTION(INIT_FUNC_ARGS)
#endif
{
	le_ftpbuf = register_list_destructors(ftp_destructor_ftpbuf, NULL);
	REGISTER_MAIN_LONG_CONSTANT("FTP_ASCII", FTPTYPE_ASCII,
		CONST_PERSISTENT | CONST_CS);
	REGISTER_MAIN_LONG_CONSTANT("FTP_BINARY", FTPTYPE_IMAGE,
		CONST_PERSISTENT | CONST_CS);
	REGISTER_MAIN_LONG_CONSTANT("FTP_IMAGE", FTPTYPE_IMAGE,
		CONST_PERSISTENT | CONST_CS);
	REGISTER_MAIN_LONG_CONSTANT("FTP_TEXT", FTPTYPE_ASCII,
		CONST_PERSISTENT | CONST_CS);
	return SUCCESS;
}

/* {{{ proto int ftp_connect(string host)
   Open a FTP stream */
PHP_FUNCTION(ftp_connect)
{
	pval		*arg1;
	int		id;
	ftpbuf_t	*ftp;

	/* arg1 - hostname
	 */
	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_string(arg1);

	/* connect */
	ftp = ftp_open(arg1->value.str.val, 0);
	if (ftp == NULL)
		RETURN_FALSE;

	id = php3_list_insert(ftp, le_ftpbuf);
	RETURN_LONG(id);
}
/* }}} */

/* {{{ proto int ftp_login(int stream, string username, string password)
   Logs into the FTP server. */
PHP_FUNCTION(ftp_login)
{
	pval		*arg1, *arg2, *arg3;
	int		id, type;
	ftpbuf_t	*ftp;

	/* arg1 - ftp
	 * arg2 - username
	 * arg3 - password
	 */
	if (	ARG_COUNT(ht) != 3 ||
		getParameters(ht, 3, &arg1, &arg2, &arg3) == FAILURE)
	{
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	convert_to_string(arg2);
	convert_to_string(arg3);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	/* log in */
	if (!ftp_login(ftp, arg2->value.str.val, arg3->value.str.val)) {
		php_error(E_WARNING, "ftp_login: %s", ftp->inbuf);
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto string ftp_pwd(int stream)
   Returns the present working directory. */
PHP_FUNCTION(ftp_pwd)
{
	pval		*arg1;
	int		id, type;
	ftpbuf_t	*ftp;
	const char	*pwd;

	/* arg1 - ftp
	 */
	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	pwd = ftp_pwd(ftp);
	if (pwd == NULL) {
		php_error(E_WARNING, "ftp_pwd: %s", ftp->inbuf);
		RETURN_FALSE;
	}

	RETURN_STRING((char*) pwd, 1);
}
/* }}} */

/* {{{ proto int ftp_cdup(int stream)
   Changes to the parent directory */
PHP_FUNCTION(ftp_cdup)
{
	pval		*arg1;
	int		id, type;
	ftpbuf_t	*ftp;

	/* arg1 - ftp
	 */
	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	if (!ftp_cdup(ftp)) {
		php_error(E_WARNING, "ftp_cdup: %s", ftp->inbuf);
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto int ftp_chdir(int stream, string directory)
   Changes directories */
PHP_FUNCTION(ftp_chdir)
{
	pval		*arg1, *arg2;
	int		id, type;
	ftpbuf_t	*ftp;

	/* arg1 - ftp
	 * arg2 - directory
	 */
	if (	ARG_COUNT(ht) != 2 ||
		getParameters(ht, 2, &arg1, &arg2) == FAILURE)
	{
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	convert_to_string(arg2);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	/* change directories */
	if (!ftp_chdir(ftp, arg2->value.str.val)) {
		php_error(E_WARNING, "ftp_chdir: %s", ftp->inbuf);
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto string ftp_mkdir(int stream, string directory)
   Creates a directory */
PHP_FUNCTION(ftp_mkdir)
{
	pval		*arg1, *arg2;
	int		id, type;
	ftpbuf_t	*ftp;
	char		*ret, *tmp;

	/* arg1 - ftp
	 * arg2 - directory
	 */
	if (	ARG_COUNT(ht) != 2 ||
		getParameters(ht, 2, &arg1, &arg2) == FAILURE)
	{
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	convert_to_string(arg2);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	/* change directories */
	tmp = ftp_mkdir(ftp, arg2->value.str.val);
	if (tmp == NULL) {
		php_error(E_WARNING, "ftp_mkdir: %s", ftp->inbuf);
		RETURN_FALSE;
	}

	if ((ret = estrdup(tmp)) == NULL) {
		free(tmp);
		php_error(E_WARNING, "estrdup failed");
		RETURN_FALSE;
	}

	RETURN_STRING(ret, 0);
}
/* }}} */

/* {{{ proto int ftp_rmdir(int stream, string directory)
   Removes a directory */
PHP_FUNCTION(ftp_rmdir)
{
	pval		*arg1, *arg2;
	int		id, type;
	ftpbuf_t	*ftp;

	/* arg1 - ftp
	 * arg2 - directory
	 */
	if (	ARG_COUNT(ht) != 2 ||
		getParameters(ht, 2, &arg1, &arg2) == FAILURE)
	{
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	convert_to_string(arg2);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	/* change directories */
	if (!ftp_rmdir(ftp, arg2->value.str.val)) {
		php_error(E_WARNING, "ftp_rmdir: %s", ftp->inbuf);
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto array ftp_nlist(int stream, string directory)
   Returns an array of filenames in the given directory */
PHP_FUNCTION(ftp_nlist)
{
	pval		*arg1, *arg2;
	int		id, type;
	ftpbuf_t	*ftp;
	char		**nlist, **ptr;

	/* arg1 - ftp
	 * arg2 - directory
	 */
	if (	ARG_COUNT(ht) != 2 ||
		getParameters(ht, 2, &arg1, &arg2) == FAILURE)
	{
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	convert_to_string(arg2);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	/* get list of files */
	nlist = ftp_nlist(ftp, arg2->value.str.val);
	if (nlist == NULL) {
		RETURN_FALSE;
	}

	array_init(return_value);
	for (ptr = nlist; *ptr; ptr++)
		add_next_index_string(return_value, *ptr, 1);
	free(nlist);
}
/* }}} */

/* {{{ proto array ftp_rawlist(int stream, string directory)
   Returns a detailed listing of a directory as an array of output lines */
PHP_FUNCTION(ftp_rawlist)
{
	pval		*arg1, *arg2;
	int		id, type;
	ftpbuf_t	*ftp;
	char		**llist, **ptr;

	/* arg1 - ftp
	 * arg2 - directory
	 */
	if (	ARG_COUNT(ht) != 2 ||
		getParameters(ht, 2, &arg1, &arg2) == FAILURE)
	{
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	convert_to_string(arg2);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	/* get directory listing */
	llist = ftp_list(ftp, arg2->value.str.val);
	if (llist == NULL) {
		RETURN_FALSE;
	}

	array_init(return_value);
	for (ptr = llist; *ptr; ptr++)
		add_next_index_string(return_value, *ptr, 1);
	free(llist);
}
/* }}} */

/* {{{ proto string ftp_systype(int stream)
   Returns the system type identifier */
PHP_FUNCTION(ftp_systype)
{
	pval		*arg1;
	int		id, type;
	ftpbuf_t	*ftp;
	const char	*syst;


	/* arg1 - ftp
	 * arg2 - directory
	 */
	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	syst = ftp_syst(ftp);
	if (syst == NULL) {
		php_error(E_WARNING, "ftp_syst: %s", ftp->inbuf);
		RETURN_FALSE;
	}

	RETURN_STRING((char*) syst, 1);
}
/* }}} */

/* {{{ proto int ftp_get(int stream, string local_file, string remote_file, int mode)
   Retrieves a file from the FTP server. */
PHP_FUNCTION(ftp_get)
{
	pval		*arg1, *arg2, *arg3, *arg4;
	int		id, type;
	ftpbuf_t	*ftp;
	ftptype_t	xtype;
	FILE		*outfp, *tmpfp;
	int		ch;


	/* arg1 - ftp
	 * arg2 - destination (local) file
	 * arg3 - source (remote) file
	 * arg4 - transfer mode
	 */
	if (	ARG_COUNT(ht) != 4 ||
		getParameters(ht, 4, &arg1, &arg2, &arg3, &arg4) == FAILURE)
	{
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	convert_to_string(arg2);
	convert_to_string(arg3);
	convert_to_long(arg4);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	if (	arg4->value.lval != FTPTYPE_ASCII &&
		arg4->value.lval != FTPTYPE_IMAGE)
	{
		php_error(E_WARNING, "arg4 must be FTP_ASCII or FTP_IMAGE");
		RETURN_FALSE;
	}

	xtype = arg4->value.lval;

	/* get to temporary file, so if there is an error, no existing
	 * file gets clobbered
	 */
	if ((tmpfp = tmpfile()) == NULL) {
		php_error(E_WARNING, "error opening tmpfile");
		RETURN_FALSE;
	}

	if (	!ftp_get(ftp, tmpfp, arg3->value.str.val, xtype) ||
		ferror(tmpfp))
	{
		fclose(tmpfp);
		php_error(E_WARNING, "ftp_get: %s", ftp->inbuf);
		RETURN_FALSE;
	}

	if ((outfp = fopen(arg2->value.str.val, "w")) == NULL) {
		fclose(tmpfp);
		php_error(E_WARNING, "error opening %s", arg2->value.str.val);
		RETURN_FALSE;
	}

	rewind(tmpfp);
	while ((ch = getc(tmpfp)) != EOF)
		putc(ch, outfp);

	if (ferror(tmpfp) || ferror(outfp)) {
		fclose(tmpfp);
		fclose(outfp);
		php_error(E_WARNING, "error writing %s", arg2->value.str.val);
		RETURN_FALSE;
	}

	fclose(tmpfp);
	fclose(outfp);

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto int ftp_put(int stream, string remote_file, string local_file, int mode)
   Stores a file on the FTP server */
PHP_FUNCTION(ftp_put)
{
	pval		*arg1, *arg2, *arg3, *arg4;
	int		id, type;
	ftpbuf_t	*ftp;
	ftptype_t	xtype;
	FILE		*infp;


	/* arg1 - ftp
	 * arg2 - destination (remote) file
	 * arg3 - source (local) file
	 * arg4 - transfer mode
	 */
	if (	ARG_COUNT(ht) != 4 ||
		getParameters(ht, 4, &arg1, &arg2, &arg3, &arg4) == FAILURE)
	{
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	convert_to_string(arg2);
	convert_to_string(arg3);
	convert_to_long(arg4);

	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	if (	arg4->value.lval != FTPTYPE_ASCII &&
		arg4->value.lval != FTPTYPE_IMAGE)
	{
		php_error(E_WARNING, "arg4 must be FTP_ASCII or FTP_IMAGE");
		RETURN_FALSE;
	}

	xtype = arg4->value.lval;

	if ((infp = fopen(arg3->value.str.val, "r")) == NULL) {
		php_error(E_WARNING, "error opening %s", arg3->value.str.val);
		RETURN_FALSE;
	}
	if (	!ftp_put(ftp, arg2->value.str.val, infp, xtype) ||
		ferror(infp))
	{
		fclose(infp);
		php_error(E_WARNING, "ftp_put: %s", ftp->inbuf);
		RETURN_FALSE;
	}
	fclose(infp);

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto int ftp_quit(int stream)
   Closes the FTP stream */
PHP_FUNCTION(ftp_quit)
{
	pval		*arg1;
	int		id, type;
	ftpbuf_t	*ftp;

	/* arg1 - ftp
	 */
	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(arg1);
	id = arg1->value.lval;
	ftp = php3_list_find(id, &type);
	if (!ftp || type != le_ftpbuf) {
		php_error(E_WARNING, "Unable to find ftpbuf %d", id);
		RETURN_FALSE;
	}

	php3_list_delete(id);

	RETURN_TRUE;
}
/* }}} */

#endif /* HAVE_FTP */