1--TEST--
2PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY vs access allowed
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
8MySQLPDOTest::skip();
9require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipifinfilenotallowed.inc');
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(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
35	putenv('PDOTEST_ATTR='.serialize([
36		PDO::MYSQL_ATTR_LOCAL_INFILE=>false,
37		PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY=>__DIR__."/foo"
38		]));
39	$db = MySQLPDOTest::factory();
40	MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db));
41
42	try {
43		exec_and_count(1, $db, 'DROP TABLE IF EXISTS test', 0);
44		exec_and_count(2, $db, sprintf('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, col1 CHAR(10)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE), 0);
45
46		$filepath = str_replace('\\', '/', __DIR__.'/foo/bar/bar.data');
47
48		$sql = sprintf("LOAD DATA LOCAL INFILE %s INTO TABLE test FIELDS TERMINATED BY ';' LINES TERMINATED  BY '\n'", $db->quote($filepath));
49		if (exec_and_count(3, $db, $sql, 3)) {
50			$stmt = $db->query('SELECT id, col1 FROM test ORDER BY id ASC');
51			$expected = array(
52				array("id" => 97, "col1" => "first"),
53				array("id" => 98, "col1" => "second"),
54				array("id" => 99, "col1" => "third"),
55				);
56			$ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
57			foreach ($expected as $offset => $exp) {
58				foreach ($exp as $key => $value) {
59					$actual_value = trim(strval($ret[$offset][$key]));
60					if ($actual_value != $value) {
61						printf("Results seem wrong, check manually\n");
62						echo "------ EXPECTED OUTPUT ------\n";
63						var_dump($expected);
64						echo "------ ACTUAL OUTPUT ------\n";
65						var_dump($ret);
66						break 2;
67					}
68				}
69			}
70		}
71	} catch (PDOException $e) {
72		printf("[001] %s, [%s] %s\n",
73			$e->getMessage(),
74			$db->errorCode(), implode(' ', $db->errorInfo()));
75	}
76
77	print "done!";
78?>
79--CLEAN--
80<?php
81require dirname(__FILE__) . '/mysql_pdo_test.inc';
82$db = MySQLPDOTest::factory();
83$db->exec('DROP TABLE IF EXISTS test');
84?>
85--EXPECT--
86done!
87