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