1--TEST--
2mysqli_real_connect()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--INI--
9mysqli.allow_local_infile=1
10--FILE--
11<?php
12    include("connect.inc");
13
14    //  ( mysqli link [, string hostname [, string username [, string passwd [, string dbname [, int port [, string socket [, int flags]]]]]]]
15    if (!$link = mysqli_init())
16        printf("[002] mysqli_init() failed\n");
17
18    if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket))
19        printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
20            $host, $user, $db, $port, $socket);
21
22    mysqli_close($link);
23    if (!$link = mysqli_init())
24        printf("[004] mysqli_init() failed\n");
25
26    if (false !== ($tmp = mysqli_real_connect($link, $host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket)))
27        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);
28
29    // Run the following tests without an anoynmous MySQL user and use a password for the test user!
30    ini_set('mysqli.default_socket', $socket);
31    if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port)) {
32        printf("[006] Usage of mysqli.default_socket failed\n");
33    } else {
34        mysqli_close($link);
35        if (!$link = mysqli_init())
36            printf("[007] mysqli_init() failed\n");
37    }
38
39    ini_set('mysqli.default_port', $port);
40    if (!mysqli_real_connect($link, $host, $user, $passwd, $db)) {
41        printf("[008] Usage of mysqli.default_port failed\n");
42    } else {
43        mysqli_close($link);
44        if (!$link = mysqli_init())
45            printf("[009] mysqli_init() failed\n");
46    }
47
48    ini_set('mysqli.default_pw', $passwd);
49    if (!mysqli_real_connect($link, $host, $user)) {
50        printf("[010] Usage of mysqli.default_pw failed\n") ;
51    } else {
52        mysqli_close($link);
53        if (!$link = mysqli_init())
54            printf("[011] mysqli_init() failed\n");
55    }
56
57    ini_set('mysqli.default_user', $user);
58    if (!mysqli_real_connect($link, $host)) {
59        printf("[012] Usage of mysqli.default_user failed\n") ;
60    } else {
61        mysqli_close($link);
62        if (!$link = mysqli_init())
63            printf("[011] mysqli_init() failed\n");
64    }
65
66    ini_set('mysqli.default_host', $host);
67    if (!mysqli_real_connect($link)) {
68        printf("[014] Usage of mysqli.default_host failed\n") ;
69    } else {
70        mysqli_close($link);
71        if (!$link = mysqli_init())
72            printf("[015] mysqli_init() failed\n");
73    }
74
75    // CLIENT_MULTI_STATEMENTS - should be disabled silently
76    if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 65536))
77        printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
78
79    if ($res = mysqli_query($link, "SELECT 1 AS a; SELECT 2 AS b")) {
80        printf("[017] Should have failed. CLIENT_MULTI_STATEMENT should have been disabled.\n");
81        var_dump($res->num_rows);
82        mysqli_next_result($link);
83        $res = mysqli_store_result($link);
84        var_dump($res->num_rows);
85    }
86
87
88    mysqli_close($link);
89    if (!$link = mysqli_init())
90        printf("[018] mysqli_init() failed\n");
91
92    if (ini_get('open_basedir')) {
93        // CLIENT_LOCAL_FILES should be blocked - but how to test it ?!
94
95        if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 128))
96            printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
97
98        $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mysqli_real_connect_phpt';
99        if (!$fp = fopen($filename, 'w'))
100            printf("[020] Cannot open temporary file %s\n", $filename);
101
102        fwrite($fp, '100;z');
103        fclose($fp);
104
105        // how do we test if gets forbidden because of a missing right or the flag, this test is partly bogus ?
106        if (mysqli_query($link, "LOAD DATA LOCAL INFILE '$filename' INTO TABLE test FIELDS TERMINATED BY ';'"))
107            printf("[021] LOAD DATA INFILE should have been forbidden!\n");
108
109        unlink($filename);
110    }
111
112    mysqli_close($link);
113    var_dump($link);
114
115    if ($IS_MYSQLND) {
116        ini_set('mysqli.default_host', 'p:' . $host);
117        $link = mysqli_init();
118        if (!@mysqli_real_connect($link)) {
119            printf("[022] Usage of mysqli.default_host=p:%s (persistent) failed\n", $host) ;
120        } else {
121            if (!$res = mysqli_query($link, "SELECT 'mysqli.default_host (persistent)' AS 'testing'"))
122                printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
123            $tmp = mysqli_fetch_assoc($res);
124            if ($tmp['testing'] !== 'mysqli.default_host (persistent)') {
125                printf("[024] Result looks strange - check manually, [%d] %s\n",
126                    mysqli_errno($link), mysqli_error($link));
127                var_dump($tmp);
128            }
129            mysqli_free_result($res);
130            mysqli_close($link);
131        }
132
133        ini_set('mysqli.default_host', 'p:');
134        $link = mysqli_init();
135        if (@mysqli_real_connect($link)) {
136            printf("[025] Usage of mysqli.default_host=p: did not fail\n") ;
137            mysqli_close($link);
138        }
139        @mysqli_close($link);
140    }
141
142    try {
143        mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket);
144    } catch (Error $exception) {
145        echo $exception->getMessage() . "\n";
146    }
147
148    print "done!";
149?>
150--CLEAN--
151<?php
152	require_once("clean_table.inc");
153?>
154--EXPECTF--
155Warning: mysqli_real_connect(): (%s/%d): Access denied for user '%s'@'%s' (using password: YES) in %s on line %d
156object(mysqli)#%d (%d) {
157  ["client_version"]=>
158  int(%d)
159  ["connect_errno"]=>
160  int(%d)
161  ["connect_error"]=>
162  NULL
163}
164mysqli object is already closed
165done!
166