1--TEST--
2mysqli_real_connect()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11	include("connect.inc");
12
13	$tmp    = NULL;
14	$link   = NULL;
15
16	if (NULL !== ($tmp = @mysqli_real_connect($link)))
17		printf("[001a] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19	if (NULL !== ($tmp = @mysqli_real_connect($link, $link)))
20		printf("[001b] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link)))
23		printf("[001c] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
24
25	if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link)))
26		printf("[001d] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
27
28	if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link, $link)))
29		printf("[001e] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
30
31	if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link, $link, $link)))
32		printf("[001f] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
33
34	if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link, $link, $link, $link)))
35		printf("[001g] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
36
37	//  ( mysqli link [, string hostname [, string username [, string passwd [, string dbname [, int port [, string socket [, int flags]]]]]]]
38	if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link, $link, $link, $link, $link)))
39		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
40
41	if (!$link = mysqli_init())
42		printf("[002] mysqli_init() failed\n");
43
44	if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket))
45		printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
46			$host, $user, $db, $port, $socket);
47
48	mysqli_close($link);
49	if (!$link = mysqli_init())
50		printf("[004] mysqli_init() failed\n");
51
52	if (false !== ($tmp = mysqli_real_connect($link, $host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket)))
53		printf("[005] Expecting boolean/false got %s/%s. Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n", gettype($tmp), $tmp, $host, $user . 'unknown_really', $db, $port, $socket);
54
55	// Run the following tests without an anoynmous MySQL user and use a password for the test user!
56	ini_set('mysqli.default_socket', $socket);
57	if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port)) {
58		printf("[006] Usage of mysqli.default_socket failed\n");
59	} else {
60		mysqli_close($link);
61		if (!$link = mysqli_init())
62			printf("[007] mysqli_init() failed\n");
63	}
64
65	ini_set('mysqli.default_port', $port);
66	if (!mysqli_real_connect($link, $host, $user, $passwd, $db)) {
67		printf("[008] Usage of mysqli.default_port failed\n");
68	} else {
69		mysqli_close($link);
70		if (!$link = mysqli_init())
71			printf("[009] mysqli_init() failed\n");
72	}
73
74	ini_set('mysqli.default_pw', $passwd);
75	if (!mysqli_real_connect($link, $host, $user)) {
76		printf("[010] Usage of mysqli.default_pw failed\n") ;
77	} else {
78		mysqli_close($link);
79		if (!$link = mysqli_init())
80			printf("[011] mysqli_init() failed\n");
81	}
82
83	ini_set('mysqli.default_user', $user);
84	if (!mysqli_real_connect($link, $host)) {
85		printf("[012] Usage of mysqli.default_user failed\n") ;
86	} else {
87		mysqli_close($link);
88		if (!$link = mysqli_init())
89			printf("[011] mysqli_init() failed\n");
90	}
91
92	ini_set('mysqli.default_host', $host);
93	if (!mysqli_real_connect($link)) {
94		printf("[014] Usage of mysqli.default_host failed\n") ;
95	} else {
96		mysqli_close($link);
97		if (!$link = mysqli_init())
98			printf("[015] mysqli_init() failed\n");
99	}
100
101	// CLIENT_MULTI_STATEMENTS - should be disabled silently
102	if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 65536))
103		printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
104
105	if ($res = mysqli_query($link, "SELECT 1 AS a; SELECT 2 AS b")) {
106		printf("[017] Should have failed. CLIENT_MULTI_STATEMENT should have been disabled.\n");
107		var_dump($res->num_rows);
108		mysqli_next_result($link);
109		$res = mysqli_store_result($link);
110		var_dump($res->num_rows);
111	}
112
113
114	mysqli_close($link);
115	if (!$link = mysqli_init())
116		printf("[018] mysqli_init() failed\n");
117
118	if (ini_get('open_basedir')) {
119		// CLIENT_LOCAL_FILES should be blocked - but how to test it ?!
120
121		if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 128))
122			printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
123
124		$filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mysqli_real_connect_phpt';
125		if (!$fp = fopen($filename, 'w'))
126			printf("[020] Cannot open temporary file %s\n", $filename);
127
128		fwrite($fp, '100;z');
129		fclose($fp);
130
131		// how do we test if gets forbidden because of a missing right or the flag, this test is partly bogus ?
132		if (mysqli_query($link, "LOAD DATA LOCAL INFILE '$filename' INTO TABLE test FIELDS TERMINATED BY ';'"))
133			printf("[021] LOAD DATA INFILE should have been forbidden!\n");
134
135		unlink($filename);
136	}
137
138	mysqli_close($link);
139	@var_dump($link);
140
141	if ($IS_MYSQLND) {
142		ini_set('mysqli.default_host', 'p:' . $host);
143		$link = mysqli_init();
144		if (!@mysqli_real_connect($link)) {
145			printf("[022] Usage of mysqli.default_host=p:%s (persistent) failed\n", $host) ;
146		} else {
147			if (!$res = mysqli_query($link, "SELECT 'mysqli.default_host (persistent)' AS 'testing'"))
148				printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
149			$tmp = mysqli_fetch_assoc($res);
150			if ($tmp['testing'] !== 'mysqli.default_host (persistent)') {
151				printf("[024] Result looks strange - check manually, [%d] %s\n",
152					mysqli_errno($link), mysqli_error($link));
153				var_dump($tmp);
154			}
155			mysqli_free_result($res);
156			mysqli_close($link);
157		}
158
159		ini_set('mysqli.default_host', 'p:');
160		$link = mysqli_init();
161		if (@mysqli_real_connect($link)) {
162			printf("[025] Usage of mysqli.default_host=p: did not fail\n") ;
163			mysqli_close($link);
164		}
165		@mysqli_close($link);
166	}
167
168	if (NULL !== ($tmp = mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket)))
169		printf("[026] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
170
171	print "done!";
172?>
173--CLEAN--
174<?php
175	require_once("clean_table.inc");
176?>
177--EXPECTF--
178Warning: mysqli_real_connect(): (%s/%d): Access denied for user '%s'@'%s' (using password: YES) in %s on line %d
179object(mysqli)#%d (%d) {
180  [%u|b%"affected_rows"]=>
181  NULL
182  [%u|b%"client_info"]=>
183  %s
184  [%u|b%"client_version"]=>
185  int(%d)
186  [%u|b%"connect_errno"]=>
187  int(%d)
188  [%u|b%"connect_error"]=>
189  NULL
190  [%u|b%"errno"]=>
191  %s
192  [%u|b%"error"]=>
193  %s
194  [%u|b%"error_list"]=>
195  NULL
196  [%u|b%"field_count"]=>
197  NULL
198  [%u|b%"host_info"]=>
199  NULL
200  [%u|b%"info"]=>
201  NULL
202  [%u|b%"insert_id"]=>
203  NULL
204  [%u|b%"server_info"]=>
205  NULL
206  [%u|b%"server_version"]=>
207  NULL
208  [%u|b%"stat"]=>
209  NULL
210  [%u|b%"sqlstate"]=>
211  NULL
212  [%u|b%"protocol_version"]=>
213  NULL
214  [%u|b%"thread_id"]=>
215  NULL
216  [%u|b%"warning_count"]=>
217  NULL
218}
219
220Warning: mysqli_real_connect(): Couldn't fetch mysqli in %s on line %d
221done!
222