~clint-fewbar/ubuntu/precise/php5/php5-5.4-merge

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
--- a/ext/gd/gd_ctx.c
+++ b/ext/gd/gd_ctx.c
@@ -46,33 +46,6 @@ static void _php_image_output_ctxfree(st
 	}
 }
 
-static void _php_image_stream_putc(struct gdIOCtx *ctx, int c)  {
-	char ch = (char) c;
-	php_stream * stream = (php_stream *)ctx->data;
-	TSRMLS_FETCH();
-	php_stream_write(stream, &ch, 1);
-}
-
-static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
-{
-	php_stream * stream = (php_stream *)ctx->data;
-	TSRMLS_FETCH();
-	return php_stream_write(stream, (void *)buf, l);
-}
-
-static void _php_image_stream_ctxfree(struct gdIOCtx *ctx)
-{
-	TSRMLS_FETCH();
-
-	if(ctx->data) {
-		php_stream_close((php_stream *) ctx->data);
-		ctx->data = NULL;
-	}
-	if(ctx) {
-		efree(ctx);
-	}
-}
-
 /* {{{ _php_image_output_ctx */
 static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
 {
@@ -81,12 +54,11 @@ static void _php_image_output_ctx(INTERN
 	int file_len = 0;
 	long quality, basefilter;
 	gdImagePtr im;
+	FILE *fp = NULL;
 	int argc = ZEND_NUM_ARGS();
 	int q = -1, i;
 	int f = -1;
 	gdIOCtx *ctx = NULL;
-	zval *to_zval = NULL;
-	php_stream *stream;
 
 	/* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
 	 * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
@@ -103,7 +75,7 @@ static void _php_image_output_ctx(INTERN
 		 * PHP_GDIMG_TYPE_WBM 
 		 * PHP_GDIMG_TYPE_WEBP 
 		 * */
-		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z/!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) {
+		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|p!ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
 			return;
 		}
 	}
@@ -117,21 +89,19 @@ static void _php_image_output_ctx(INTERN
 		}
 	}
 
-	if (argc > 1 && to_zval != NULL) {
-		if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
-			php_stream_from_zval_no_verify(stream, &to_zval);
-			if (stream == NULL) {
-				RETURN_FALSE;
-			}
-		} else if (Z_TYPE_P(to_zval) == IS_STRING) {
-			stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
-			if (stream == NULL) {
-				RETURN_FALSE;
-			}
-		} else {
-			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream");
+	if (argc > 1 && file_len) {
+		if (strlen(file) != file_len) {
+			RETURN_FALSE;
+		}
+		PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
+
+		fp = VCWD_FOPEN(file, "wb");
+		if (!fp) {
+			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing: %s", file, strerror(errno));
 			RETURN_FALSE;
 		}
+
+		ctx = gdNewFileCtx(fp);
 	} else {
 		ctx = emalloc(sizeof(gdIOCtx));
 		ctx->putC = _php_image_output_putc;
@@ -145,14 +115,6 @@ static void _php_image_output_ctx(INTERN
 #endif
 	}
 
-	if (!ctx)	{
-		ctx = emalloc(sizeof(gdIOCtx));
-		ctx->putC = _php_image_stream_putc;
-		ctx->putBuf = _php_image_stream_putbuf;
-		ctx->gd_free = _php_image_stream_ctxfree;
-		ctx->data = (void *)stream;
-	}
-
 	switch(image_type) {
 		case PHP_GDIMG_CONVERT_WBM:
 			if(q<0||q>255) {
@@ -189,11 +151,12 @@ static void _php_image_output_ctx(INTERN
 			break;
 	}
 
-#if HAVE_LIBGD204
 	ctx->gd_free(ctx);
-#else
-	ctx->free(ctx);
-#endif
+
+	if(fp) {
+		fflush(fp);
+		fclose(fp);
+	}
 
 	RETURN_TRUE;
 }