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