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