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    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    try {
142        mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket);
143    } catch (Error $exception) {
144        echo $exception->getMessage() . "\n";
145    }
146
147    print "done!";
148?>
149--EXPECTF--
150Warning: mysqli_real_connect(): (%s/%d): Access denied for user '%s'@'%s' %r(\(using password: \w+\) ){0,1}%rin %s on line %d
151object(mysqli)#%d (%d) {
152  ["client_info"]=>
153  string(%d) "%s"
154  ["client_version"]=>
155  int(%d)
156  ["connect_errno"]=>
157  int(%d)
158  ["connect_error"]=>
159  NULL
160}
161mysqli object is already closed
162done!
163