1<?php
2    /* Utility function for mysqli_set_local_infile*.phpt tests */
3    function shutdown_clean($file) {
4        if ($file) {
5            unlink($file);
6        }
7    }
8
9    function check_local_infile_support($link, $engine, $table_name = 'test') {
10
11        if (!$res = mysqli_query($link, 'SHOW VARIABLES LIKE "local_infile"'))
12            return "Cannot check if Server variable 'local_infile' is set to 'ON'";
13
14        $row = mysqli_fetch_assoc($res);
15        mysqli_free_result($res);
16        if ('ON' != $row['Value'])
17            return sprintf("Server variable 'local_infile' seems not set to 'ON', found '%s'", $row['Value']);
18
19        if (!mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name))) {
20            return "Failed to drop old test table";
21        }
22
23        if (!mysqli_query($link, $sql = sprintf('CREATE TABLE %s(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE=%s',
24            $table_name, $engine)))
25            return "Failed to create test table: $sql";
26
27        $file = create_standard_csv(1, false);
28        if (!$file) {
29            mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
30            return "Cannot create CSV file";
31        }
32
33        if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
34            INTO TABLE %s
35            FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
36            LINES TERMINATED BY '\n'",
37            mysqli_real_escape_string($link, $file),
38            $table_name))) {
39              if (1148 == mysqli_errno($link)) {
40                  mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
41                  return "Cannot test LOAD DATA LOCAL INFILE, [1148] The used command is not allowed with this MySQL version";
42              } else if ($link->errno) {
43                  return $link->error;
44              }
45        }
46        mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
47        return "";
48    }
49
50    function create_standard_csv($offset, $verbose = true) {
51        // create a CVS file
52        $file = tempnam(sys_get_temp_dir(), 'mysqli_test');
53        if (!$fp = fopen($file, 'w')) {
54            if ($verbose)
55                printf("[%03d + 1] Cannot create CVS file '%s'\n", $offset, $file);
56            return NULL;
57        } else {
58            /* Looks ugly? No, handy if you have crashes... */
59            register_shutdown_function("shutdown_clean", $file);
60        }
61
62        if (!fwrite($fp, "97;'x';\n") ||
63            !fwrite($fp, "98;'y';\n") ||
64            !fwrite($fp, "99;'z';\n")) {
65            if ($verbose)
66                printf("[%03d + 3] Cannot write CVS file '%s'\n", $offset, $file);
67            return NULL;
68        }
69
70        fclose($fp);
71
72        if (!chmod($file, 0644)) {
73            if ($verbose)
74                printf("[%03d + 4] Cannot change the file perms of '%s' from 0600 to 0644, MySQL might not be able to read it\n",
75                    $offset, $file);
76            return NULL;
77        }
78        return $file;
79    }
80
81    function try_handler($offset, $link, $file, $handler, $expected = null) {
82
83        if ('default' == $handler) {
84            mysqli_set_local_infile_default($link);
85        } else if (!mysqli_set_local_infile_handler($link, $handler)) {
86            printf("[%03d] Cannot set infile handler to '%s'\n", $offset, $handler);
87            return false;
88        }
89        printf("Callback set to '%s'\n", $handler);
90
91        if (!mysqli_query($link, sprintf("DELETE FROM test"))) {
92            printf("[%03d] Cannot remove records, [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
93            return false;
94        }
95
96        if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
97            INTO TABLE test
98            FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
99            LINES TERMINATED BY '\n'",
100            mysqli_real_escape_string($link, $file)))) {
101            printf("[%03d] LOAD DATA failed, [%d] %s\n",
102                $offset + 2,
103                mysqli_errno($link), mysqli_error($link));
104        }
105
106        if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id")) {
107            printf("[%03d] [%d] %s\n", $offset + 3, mysqli_errno($link), mysqli_error($link));
108            return false;
109        }
110
111        if (!is_array($expected))
112            return true;
113
114        foreach ($expected as $k => $values) {
115            if (!$tmp = mysqli_fetch_assoc($res)) {
116                printf("[%03d/%d] [%d] '%s'\n", $offset + 4, $k, mysqli_errno($link), mysqli_error($link));
117                return false;
118            }
119            if ($values['id'] != $tmp['id']) {
120                printf("[%03d/%d] Expecting %s got %s\n",
121                    $offset + 5, $k,
122                    $values['id'], $tmp['id']);
123                    return false;
124            }
125            if ($values['label'] != $tmp['label']) {
126                printf("[%03d/%d] Expecting %s got %s\n",
127                    $offset + 6, $k,
128                    $values['label'], $tmp['label']);
129                    return false;
130            }
131        }
132
133        if ($res && $tmp = mysqli_fetch_assoc($res)) {
134            printf("[%03d] More results than expected!\n", $offset + 7);
135            do {
136                var_dump($tmp);
137            } while ($tmp = mysqli_fetch_assoc($res));
138            return false;
139        }
140
141        if ($res)
142            mysqli_free_result($res);
143
144        return true;
145    }
146?>
147