summaryrefslogtreecommitdiff
path: root/main/streams.c
blob: df562bfc7d298402a65ea7bc4747c474046bc89d (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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/*
   +----------------------------------------------------------------------+
   | PHP Version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2002 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors:                                                             |
   | Wez Furlong (wez@thebrainroom.com)                                   |
   | Borrowed code from:                                                  |
   | Rasmus Lerdorf <rasmus@lerdorf.on.ca>                                |
   | Jim Winstead <jimw@php.net>                                          |
   +----------------------------------------------------------------------+
 */

#define _GNU_SOURCE
#include "php.h"
#include "php_globals.h"
#include "php_network.h"
#include "php_open_temporary_file.h"

#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif

#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif

#define CHUNK_SIZE	8192

#ifdef PHP_WIN32
#define EWOULDBLOCK WSAEWOULDBLOCK
#else
#include "build-defs.h"
#endif

static HashTable url_stream_wrappers_hash;

/* allocate a new stream for a particular ops */
PHPAPI php_stream *php_stream_alloc(php_stream_ops *ops, void *abstract, int persistent, const char *mode) /* {{{ */
{
	php_stream *ret;

	ret = (php_stream*) pemalloc(sizeof(php_stream), persistent);

	memset(ret, 0, sizeof(php_stream));

	ret->ops = ops;
	ret->abstract = abstract;
	ret->is_persistent = persistent;

	strlcpy(ret->mode, mode, sizeof(ret->mode));

	return ret;
}
/* }}} */

PHPAPI int php_stream_free(php_stream *stream, int call_dtor) /* {{{ */
{
	int ret = 1;

	if (stream->wrapper && stream->wrapper->destroy) {
		stream->wrapper->destroy(stream);
		stream->wrapper = NULL;
	}

	if (call_dtor) {
		if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
			/* calling fclose on an fopencookied stream will ultimately
				call this very same function.  If we were called via fclose,
				the cookie_closer unsets the fclose_stdiocast flags, so
				we can be sure that we only reach here when PHP code calls
				php_stream_free.
				Lets let the cookie code clean it all up.
			 */
			return fclose(stream->stdiocast);
		}
		
		php_stream_flush(stream);
	}
	
	ret = stream->ops->close(stream, call_dtor);
	stream->abstract = NULL;
	
	if (call_dtor) {	
		/* tidy up any FILE* that might have been fdopened */
		if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) {
			fclose(stream->stdiocast);
			stream->stdiocast = NULL;
		}
	}

	if (stream->wrapperdata) {
		FREE_ZVAL(stream->wrapperdata);
		stream->wrapperdata = NULL;
	}
	pefree(stream, stream->is_persistent);

	return ret;
}
/* }}} */

/* {{{ generic stream operations */
PHPAPI size_t php_stream_read(php_stream *stream, char *buf, size_t size)
{
	return stream->ops->read(stream, buf, size);
}

PHPAPI int php_stream_eof(php_stream *stream)
{
	/* we define our stream reading function so that it
	   must return EOF when an EOF condition occurs, when
	   working in unbuffered mode and called with these args */
	return stream->ops->read(stream, NULL, 0) == EOF ? 1 : 0;
}

PHPAPI int php_stream_putc(php_stream *stream, int c)
{
	unsigned char buf = c;

	if (php_stream_write(stream, &buf, 1) > 0) {
		return 1;
	}
	return EOF;
}

PHPAPI int php_stream_getc(php_stream *stream)
{
	char buf;

	if (php_stream_read(stream, &buf, 1) > 0) {
		return buf & 0xff;
	}
	return EOF;
}

PHPAPI int php_stream_puts(php_stream *stream, char *buf)
{
	int len;
	char newline[2] = "\n"; /* is this OK for Win? */
	len = strlen(buf);

	if (len > 0 && php_stream_write(stream, buf, len) && php_stream_write(stream, newline, 1)) {
		return 1;
	}
	return 0;
}

PHPAPI char *php_stream_gets(php_stream *stream, char *buf, size_t maxlen)
{

	if (maxlen == 0) {
		buf[0] = 0;
		return buf;
	}

	if (stream->ops->gets) {
		return stream->ops->gets(stream, buf, maxlen);
	} else {
		/* unbuffered fgets - poor performance ! */
		size_t n = 0;
		char *c = buf;

		/* TODO: look at error returns? */

		while(n < maxlen && stream->ops->read(stream, c, 1) > 0) {
			n++;
			if (*c == '\n')	{
				c++;
				break;
			}
			c++;
		}
		*c = 0;
		return buf;
	}
}

PHPAPI int php_stream_flush(php_stream *stream)
{
	if (stream->ops->flush) {
		return stream->ops->flush(stream);
	}
	return 0;
}

PHPAPI size_t php_stream_write(php_stream *stream, const char *buf, size_t count)
{
	return stream->ops->write(stream, buf, count);
}

PHPAPI off_t php_stream_tell(php_stream *stream)
{
	off_t ret = -1;

	if (stream->ops->seek) {
		ret = stream->ops->seek(stream, 0, SEEK_CUR);
	}
	return ret;
}

PHPAPI int php_stream_seek(php_stream *stream, off_t offset, int whence)
{
	if (stream->ops->seek) {	
		return stream->ops->seek(stream, offset, whence);
	}

	/* emulate forward moving seeks with reads */
	if (whence == SEEK_CUR && offset > 0) {
		char tmp[1024];
		while(offset >= sizeof(tmp)) {
			if (php_stream_read(stream, tmp, sizeof(tmp)) == 0)
				return -1;
			offset -= sizeof(tmp);
		}
		if (offset)	{
			if (php_stream_read(stream, tmp, offset) == 0)
				return -1;
		}
		return 0;
	}
	
	zend_error(E_WARNING, "streams of type %s do not support seeking", stream->ops->label);
	return -1;
}

PHPAPI size_t php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, int persistent)
{
	size_t ret = 0;
	char *ptr;
	size_t len = 0, max_len;
	int step = CHUNK_SIZE;
	int min_room = CHUNK_SIZE / 4;
#if HAVE_MMAP
	int srcfd;
#endif

	if (buf)
		*buf = NULL;
	
	if (maxlen == 0)
		return 0;

	if (maxlen == PHP_STREAM_COPY_ALL)
		maxlen = 0;

#if HAVE_MMAP
	/* try and optimize the case where we are copying from the start of a plain file.
	 * We could probably make this work in more situations, but I don't trust the stdio
	 * buffering layer.
	 * */
	if ( php_stream_is(src, PHP_STREAM_IS_STDIO) &&
			php_stream_tell(src) == 0 && 
			SUCCESS == php_stream_cast(src, PHP_STREAM_AS_FD, (void**)&srcfd, 0))
	{
		struct stat sbuf;

		if (fstat(srcfd, &sbuf) == 0) {
			void *srcfile;

			if (maxlen > sbuf.st_size || maxlen == 0)
				maxlen = sbuf.st_size;
				
			srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0);
			if (srcfile != (void*)MAP_FAILED) {

				*buf = pemalloc(persistent, maxlen);

				if (*buf)	{
					memcpy(*buf, srcfile, maxlen);
					ret = maxlen;
				}
				
				munmap(srcfile, maxlen);
				return ret;
			}
		}
		/* fall through - we might be able to copy in smaller chunks */
	}
#endif
	
	ptr = *buf = pemalloc(persistent, step);
	max_len = step;

	while((ret = php_stream_read(src, ptr, max_len - len)))	{
		len += ret;
		if (len + min_room >= max_len) {
			*buf = perealloc(*buf, max_len + step, persistent);
			max_len += step;
			ptr = *buf + len;
		}
	}
	if (len) {
		*buf = perealloc(*buf, len, persistent);
	} else {
		pefree(*buf, persistent);
		*buf = NULL;
	}
	return len;
}

PHPAPI size_t php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen)
{
	char buf[CHUNK_SIZE];
	size_t readchunk;
	size_t haveread = 0;
	size_t didread;
#if HAVE_MMAP
	int srcfd;
#endif

	if (maxlen == 0)
		return 0;

	if (maxlen == PHP_STREAM_COPY_ALL)
		maxlen = 0;
	
#if HAVE_MMAP
	/* try and optimize the case where we are copying from the start of a plain file.
	 * We could probably make this work in more situations, but I don't trust the stdio
	 * buffering layer.
	 * */
	if ( php_stream_is(src, PHP_STREAM_IS_STDIO) &&
			php_stream_tell(src) == 0 && 
			SUCCESS == php_stream_cast(src, PHP_STREAM_AS_FD, (void**)&srcfd, 0))
	{
		struct stat sbuf;

		if (fstat(srcfd, &sbuf) == 0) {
			void *srcfile;
		
			if (maxlen > sbuf.st_size || maxlen == 0)
				maxlen = sbuf.st_size;
			
			srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0);
			if (srcfile != (void*)MAP_FAILED) {
				haveread = php_stream_write(dest, srcfile, maxlen);
				munmap(srcfile, maxlen);
				return haveread;
			}
		}
		/* fall through - we might be able to copy in smaller chunks */
	}
#endif
	
	while(1) {
		readchunk = sizeof(buf);

		if (maxlen && (maxlen - haveread) < readchunk)
			readchunk = maxlen - haveread;

		didread = php_stream_read(src, buf, readchunk);
		if (didread) {
			/* extra paranoid */
			size_t didwrite, towrite;
			char *writeptr;

			towrite = didread;
			writeptr = buf;
			haveread += didread;
			
			while(towrite) {
				didwrite = php_stream_write(dest, writeptr, towrite);

				if (didwrite == 0)
					return 0;	/* error */
				
				towrite -= didwrite;
				writeptr += didwrite;
			}
		} else {
			return 0; /* error */
		}

		if (maxlen - haveread == 0) {
			break;
		}
	}

	return haveread;
	
}
/* }}} */



/* {{{ ------- STDIO stream implementation -------*/

typedef struct {
	FILE *file;
	int is_pipe;	/* use pclose */
#if HAVE_FLUSHIO
	char last_op;
#endif
} php_stdio_stream_data;

PHPAPI php_stream *php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path TSRMLS_DC)
{
	FILE *fp = php_open_temporary_file(dir, pfx, opened_path TSRMLS_CC);

	if (fp)	{
		php_stream *stream = php_stream_fopen_from_file(fp, "wb");
		if (stream) {
			return stream;
		}
		fclose(fp);

		zend_error(E_WARNING, "%s(): unable to allocate stream", get_active_function_name(TSRMLS_C));
		
		return NULL;
	}
	return NULL;
}

PHPAPI php_stream *php_stream_fopen_tmpfile(void)
{
	FILE *fp;
	php_stream *stream;
	
	fp = tmpfile();
	if (fp == NULL) {
		zend_error(E_WARNING, "tmpfile(): %s", strerror(errno));
		return NULL;
	}
	stream = php_stream_fopen_from_file(fp, "r+");
	if (stream == NULL)	{
		zend_error(E_WARNING, "tmpfile(): %s", strerror(errno));
		fclose(fp);
		return NULL;
	}
	return stream;
}



PHPAPI php_stream *php_stream_fopen_from_file(FILE *file, const char *mode)
{
	php_stdio_stream_data *self;

	self = emalloc(sizeof(*self));
	self->file = file;
	self->is_pipe = 0;
	return php_stream_alloc(&php_stream_stdio_ops, self, 0, mode);
}

PHPAPI php_stream *php_stream_fopen_from_pipe(FILE *file, const char *mode)
{
	php_stdio_stream_data *self;

	self = emalloc(sizeof(*self));
	self->file = file;
	self->is_pipe = 1;
	return php_stream_alloc(&php_stream_stdio_ops, self, 0, mode);
}
static size_t php_stdiop_write(php_stream *stream, const char *buf, size_t count)
{
	php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;

	assert(data != NULL);
	
#if HAVE_FLUSHIO
	if (data->last_op == 'r') {
		fseek(data->file, 0, SEEK_CUR);
	}
	data->last_op = 'w';
#endif

	return fwrite(buf, 1, count, data->file);
}

static size_t php_stdiop_read(php_stream *stream, char *buf, size_t count)
{
	php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;

	assert(data != NULL);
	
	if (buf == NULL && count == 0)	{
		/* check for EOF condition */
		if (feof(data->file))	{
			return EOF;
		}
		return 0;
	}

#if HAVE_FLUSHIO
	if (data->last_op == 'w')
		fseek(data->file, 0, SEEK_CUR);
	data->last_op = 'r';
#endif
	
	return fread(buf, 1, count, data->file);
}

static int php_stdiop_close(php_stream *stream, int close_handle)
{
	int ret;
	php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;

	assert(data != NULL);

	if (close_handle) {
		if (data->is_pipe) {
			ret = pclose(data->file);
		} else {
			ret = fclose(data->file);
		}
	}
	else
		ret = 0;

	efree(data);
	
	return ret;
}

static int php_stdiop_flush(php_stream *stream)
{
	php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;

	assert(data != NULL);
	
	return fflush(data->file);
}

static int php_stdiop_seek(php_stream *stream, off_t offset, int whence)
{
	php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;

	assert(data != NULL);

	return fseek(data->file, offset, whence);
}

static char *php_stdiop_gets(php_stream *stream, char *buf, size_t size)
{
	php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;

	assert(data != NULL);
#if HAVE_FLUSHIO
	if (data->last_op == 'w') {
		fseek(data->file, 0, SEEK_CUR);
	}
	data->last_op = 'r';
#endif
	
	return fgets(buf, size, data->file);
}
static int php_stdiop_cast(php_stream *stream, int castas, void **ret)
{
	int fd;
	php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
 
	assert(data != NULL);

	switch (castas)	{
		case PHP_STREAM_AS_STDIO:
			if (ret) {
				*ret = data->file;
			}
			return SUCCESS;

		case PHP_STREAM_AS_FD:
			fd = fileno(data->file);
			if (fd < 0) {
				return FAILURE;
			}
			if (ret) {
				*ret = (void*)fd;
			}
			return SUCCESS;
		default:
			return FAILURE;
	}
}

php_stream_ops	php_stream_stdio_ops = {
	php_stdiop_write, php_stdiop_read,
	php_stdiop_close, php_stdiop_flush, php_stdiop_seek,
	php_stdiop_gets, php_stdiop_cast,
	"STDIO"
};

PHPAPI php_stream *php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path TSRMLS_DC) /* {{{ */
{
	/* code ripped off from fopen_wrappers.c */
	char *pathbuf, *ptr, *end;
	char *exec_fname;
	char trypath[MAXPATHLEN];
	struct stat sb;
	php_stream *stream;
	int path_length;
	int filename_length;
	int exec_fname_length;

	if (opened_path) {
		*opened_path = NULL;
	}

	if(!filename) {
		return NULL;
	}

	filename_length = strlen(filename);

	/* Relative path open */
	if (*filename == '.') {
		if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
			return NULL;
		}
		return php_stream_fopen(filename, mode, opened_path TSRMLS_CC);
	}

	/*
	 * files in safe_mode_include_dir (or subdir) are excluded from
	 * safe mode GID/UID checks
	 */

	/* Absolute path open */
	if (IS_ABSOLUTE_PATH(filename, filename_length)) {
		if ((php_check_safe_mode_include_dir(filename TSRMLS_CC)) == 0)
			/* filename is in safe_mode_include_dir (or subdir) */
			return php_stream_fopen(filename, mode, opened_path TSRMLS_CC);

		if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM)))
			return NULL;

		return php_stream_fopen(filename, mode, opened_path TSRMLS_CC);
	}

	if (!path || (path && !*path)) {
		if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
			return NULL;
		}
		return php_stream_fopen(filename, mode, opened_path TSRMLS_CC);
	}

	/* check in provided path */
	/* append the calling scripts' current working directory
	 * as a fall back case
	 */
	if (zend_is_executing(TSRMLS_C)) {
		exec_fname = zend_get_executed_filename(TSRMLS_C);
		exec_fname_length = strlen(exec_fname);
		path_length = strlen(path);

		while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
		if ((exec_fname && exec_fname[0] == '[')
				|| exec_fname_length<=0) {
			/* [no active file] or no path */
			pathbuf = estrdup(path);
		} else {		
			pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
			memcpy(pathbuf, path, path_length);
			pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
			memcpy(pathbuf+path_length+1, exec_fname, exec_fname_length);
			pathbuf[path_length + exec_fname_length +1] = '\0';
		}
	} else {
		pathbuf = estrdup(path);
	}

	ptr = pathbuf;

	while (ptr && *ptr) {
		end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
		if (end != NULL) {
			*end = '\0';
			end++;
		}
		snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename);
		if (PG(safe_mode)) {
			if (VCWD_STAT(trypath, &sb) == 0) {
				/* file exists ... check permission */
				if ((php_check_safe_mode_include_dir(trypath TSRMLS_CC) == 0) ||
						php_checkuid(trypath, mode, CHECKUID_CHECK_MODE_PARAM))
					/* UID ok, or trypath is in safe_mode_include_dir */
					stream = php_stream_fopen(trypath, mode, opened_path TSRMLS_CC);
				else
					stream = NULL;

				efree(pathbuf);
				return stream;
			}
		}
		stream = php_stream_fopen(trypath, mode, opened_path TSRMLS_CC);
		if (stream) {
			efree(pathbuf);
			return stream;
		}
		ptr = end;
	} /* end provided path */

	efree(pathbuf);
	return NULL;

}
/* }}} */

PHPAPI php_stream *php_stream_fopen(const char *filename, const char *mode, char **opened_path TSRMLS_DC)
{
	FILE *fp;
	char *realpath = NULL;

	realpath = expand_filepath(filename, NULL TSRMLS_CC);
	
	fp = fopen(realpath, mode);

	if (fp)	{
		php_stream *ret = php_stream_fopen_from_file(fp, mode);

		if (ret)	{
			if (opened_path)	{
				*opened_path = realpath;
				realpath = NULL;
			}
			if (realpath)
				efree(realpath);

			return ret;
		}

		fclose(fp);
	}
	efree(realpath);
	return NULL;
}
/* }}} */

/* {{{ STDIO with fopencookie */
#if HAVE_FOPENCOOKIE
static ssize_t stream_cookie_reader(void *cookie, char *buffer, size_t size)
{
   return php_stream_read(((php_stream *)cookie), buffer, size);
}

static ssize_t stream_cookie_writer(void *cookie, const char *buffer, size_t size) {
   return php_stream_write(((php_stream *)cookie), (char *)buffer, size);
}

static int stream_cookie_seeker(void *cookie, off_t position, int whence) {
   return php_stream_seek(((php_stream *)cookie), position, whence);
}

static int stream_cookie_closer(void *cookie) {
	php_stream *stream = (php_stream*)cookie;
	/* prevent recursion */
	stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
	return php_stream_close(stream);
}

static COOKIE_IO_FUNCTIONS_T stream_cookie_functions =
{
	stream_cookie_reader, stream_cookie_writer,
	stream_cookie_seeker, stream_cookie_closer
};
#else
/* TODO: use socketpair() to emulate fopencookie, as suggested by Hartmut ? */
#endif
/* }}} */

PHPAPI int php_stream_cast(php_stream *stream, int castas, void **ret, int show_err) /* {{{ */
{
	int flags = castas & PHP_STREAM_CAST_MASK;
	castas &= ~PHP_STREAM_CAST_MASK;
	
	if (castas == PHP_STREAM_AS_STDIO) {
		if (stream->stdiocast) {
			if (ret) {
				*ret = stream->stdiocast;
			}
			goto exit_success;
		}

		if (stream->ops->cast && stream->ops->cast(stream, castas, ret) == SUCCESS)
			goto exit_success;


#if HAVE_FOPENCOOKIE
		/* if just checking, say yes we can be a FILE*, but don't actually create it yet */
		if (ret == NULL)
			goto exit_success;

		*ret = fopencookie(stream, stream->mode, stream_cookie_functions);

		if (*ret != NULL) {
			stream->fclose_stdiocast = PHP_STREAM_FCLOSE_FOPENCOOKIE;
			goto exit_success;
		}

		{
			TSRMLS_FETCH();

			/* must be either:
		  		a) programmer error
		  		b) no memory
		  		-> lets bail
			 */
			zend_error(E_ERROR, "%s(): fopencookie failed", get_active_function_name(TSRMLS_C));
			return FAILURE;
		}
#endif

		goto exit_fail;
	}
	if (stream->ops->cast && stream->ops->cast(stream, castas, ret) == SUCCESS)
		goto exit_success;


exit_fail:
	if (show_err) {
		/* these names depend on the values of the PHP_STREAM_AS_XXX defines in php_streams.h */
		static const char *cast_names[3] = {
			"STDIO FILE*", "File Descriptor", "Socket Descriptor"
		};
		TSRMLS_FETCH();

		zend_error(E_WARNING, "%s(): cannot represent a stream of type %s as a %s",
			get_active_function_name(TSRMLS_C),
			stream->ops->label,
			cast_names[castas]
			);
	}

	return FAILURE;

exit_success:
	if (castas == PHP_STREAM_AS_STDIO && ret)
		stream->stdiocast = *ret;

	if (flags & PHP_STREAM_CAST_RELEASE) {
		/* Something other than php_stream_close will be closing
		 * the underlying handle, so we should free the stream handle/data
		 * here now.  The stream may not be freed immediately (in the case
		 * of fopencookie), but the caller should still not touch their
		 * original stream pointer in any case. */
		if (stream->fclose_stdiocast != PHP_STREAM_FCLOSE_FOPENCOOKIE) {
			/* ask the implementation to release resources other than
			 * the underlying handle */
			php_stream_free(stream, 0);
		}
	}
	
	return SUCCESS;

} /* }}} */

int php_init_stream_wrappers(TSRMLS_D)
{
	if (PG(allow_url_fopen))
		return zend_hash_init(&url_stream_wrappers_hash, 0, NULL, NULL, 1);
	return SUCCESS;
}

int php_shutdown_stream_wrappers(TSRMLS_D)
{
	if (PG(allow_url_fopen))
		zend_hash_destroy(&url_stream_wrappers_hash);
	return SUCCESS;
}

PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC)
{
	if (PG(allow_url_fopen))
		return zend_hash_add(&url_stream_wrappers_hash, protocol, strlen(protocol), wrapper, sizeof(wrapper), NULL);
	return FAILURE;
}
PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC)
{
	if (PG(allow_url_fopen))
		return zend_hash_del(&url_stream_wrappers_hash, protocol, strlen(protocol));
	return SUCCESS;
}

static php_stream *php_stream_open_url(char *path, char *mode, int options, char **opened_path TSRMLS_DC)
{
	php_stream_wrapper *wrapper;
	const char *p, *protocol = NULL;
	int n = 0;

	for (p = path; isalnum((int)*p); p++) {
		n++;
	}

	if ((*p == ':') && (n > 1)) {
		protocol = path;
	}

	if (protocol)	{
		if (FAILURE == zend_hash_find(&url_stream_wrappers_hash, (char*)protocol, n, (void**)&wrapper))	{
			wrapper = NULL;
			protocol = NULL;
		}
		if (wrapper)	{
			php_stream *stream = wrapper->create(path, mode, options, opened_path TSRMLS_CC);
			if (stream)
				stream->wrapper = wrapper;
			return stream;
		}
	}

	if (!protocol || !strncasecmp(protocol, "file", n))	{
		if (protocol && path[n+1] == '/' && path[n+2] == '/')	{
			zend_error(E_WARNING, "remote host file access not supported, %s", path);
			return NULL;
		}
		if (protocol)
			path += n + 1;

		/* fall back on regular file access */
		return php_stream_open_wrapper(path, mode, (options & ~REPORT_ERRORS) | IGNORE_URL,
				opened_path TSRMLS_CC);
	}
	return NULL;
}

PHPAPI php_stream *php_stream_open_wrapper(char *path, char *mode, int options, char **opened_path TSRMLS_DC)
{
	php_stream *stream = NULL;
	
	if (opened_path)
		*opened_path = NULL;
	
	if (!path || !*path)
		return NULL;

	if (PG(allow_url_fopen) && !(options & IGNORE_URL))	{
		stream = php_stream_open_url(path, mode, options, opened_path TSRMLS_CC);
		goto out;
	}
		
	if ((options & USE_PATH) && PG(include_path) != NULL)	{
		stream = php_stream_fopen_with_path(path, mode, PG(include_path), opened_path TSRMLS_CC);
		goto out;
	}

	if ((options & ENFORCE_SAFE_MODE) && PG(safe_mode) && (!php_checkuid(path, mode, CHECKUID_CHECK_MODE_PARAM)))
		return NULL;
	
	stream = php_stream_fopen(path, mode, opened_path TSRMLS_CC);
out:
	if (stream == NULL && (options & REPORT_ERRORS))	{
		char *tmp = estrdup(path);
		php_strip_url_passwd(tmp);
		zend_error(E_WARNING, "%s(\"%s\") - %s", get_active_function_name(TSRMLS_C), tmp, strerror(errno));
		efree(tmp);
	}
	return stream;
}


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */