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