1--TEST--
2PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY vs access denied
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9MySQLPDOTest::skipInfileNotAllowed();
10if (!defined('PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY')) {
11    die("skip No MYSQL_ATTR_LOCAL_INFILE_DIRECTORY support");
12}
13?>
14--FILE--
15<?php
16	function exec_and_count($offset, &$db, $sql, $exp) {
17		try {
18			$ret = $db->exec($sql);
19			if ($ret !== $exp) {
20				printf("[%03d] Expecting '%s'/%s got '%s'/%s when running '%s', [%s] %s\n",
21					$offset, $exp, gettype($exp), $ret, gettype($ret), $sql,
22					$db->errorCode(), implode(' ', $db->errorInfo()));
23				return false;
24			}
25		} catch (PDOException $e) {
26			printf("[%03d] '%s' has failed, [%s] %s\n",
27				$offset, $sql, $db->errorCode(), implode(' ', $db->errorInfo()));
28			return false;
29		}
30
31		return true;
32	}
33
34	require_once __DIR__ . '/inc/mysql_pdo_test.inc';
35	$db = MySQLPDOTest::factoryWithAttr([
36		PDO::MYSQL_ATTR_LOCAL_INFILE=>false,
37		PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY=>__DIR__."/foo/bar",
38	]);
39
40	try {
41		exec_and_count(2, $db, sprintf('CREATE TABLE test_local_inifile_dir_denied(id INT NOT NULL PRIMARY KEY, col1 CHAR(10)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE), 0);
42
43		$filepath = str_replace('\\', '/', __DIR__.'/foo/foo.data');
44
45		$sql = sprintf("LOAD DATA LOCAL INFILE %s INTO TABLE test_local_inifile_dir_denied FIELDS TERMINATED BY ';' LINES TERMINATED  BY '\n'", $db->quote($filepath));
46		if (exec_and_count(3, $db, $sql, false)) {
47			$stmt = $db->query('SELECT id, col1 FROM test_local_inifile_dir_denied ORDER BY id ASC');
48			$expected = array();
49			$ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
50			if ($ret != $expected) {
51				printf("Results seem wrong, check manually\n");
52				echo "------ EXPECTED OUTPUT ------\n";
53				var_dump($expected);
54				echo "------ ACTUAL OUTPUT ------\n";
55				var_dump($ret);
56			}
57		}
58	} catch (PDOException $e) {
59		printf("[001] %s, [%s] %s\n",
60			$e->getMessage(),
61			$db->errorCode(), implode(' ', $db->errorInfo()));
62	}
63
64	print "done!";
65?>
66--CLEAN--
67<?php
68require_once __DIR__ . '/inc/mysql_pdo_test.inc';
69$db = MySQLPDOTest::factory();
70$db->exec('DROP TABLE IF EXISTS test_local_inifile_dir_denied');
71?>
72--EXPECTF--
73Warning: PDO::exec(): SQLSTATE[HY000]: General error: 2068 LOAD DATA LOCAL INFILE %s in %s on line %d
74done!
75