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