1<?php
2
3function get_active_cp($kind = "")
4{
5	if (version_compare(PHP_VERSION, '7.1', '<')) {
6		$s = exec("chcp");
7		preg_match(",.*: (\d+),", $s, $m);
8
9		return $m[1];
10	} else {
11		return sapi_windows_cp_get($kind);
12	}
13}
14
15function set_active_cp($cp, $echo = true)
16{
17	if (version_compare(PHP_VERSION, '7.1', '<')) {
18		$ret = exec("chcp $cp");
19	} else {
20		if (!sapi_windows_cp_set($cp)) {
21			echo "Failed to set cp $cp\n";
22			return;
23		}
24
25		if ($echo) echo "Active code page: ", get_active_cp(), "\n";
26	}
27}
28
29function get_basename_with_cp($path, $cp, $echo = true)
30{
31	$old_cp = get_active_cp();
32	set_active_cp($cp, $echo);
33
34	if ($echo) echo "getting basename of $path\n";
35
36	$cmd = "powershell -command \"Get-Item -Path '$path' | Format-Table -HideTableHeaders Name\"";
37	$out = trim(shell_exec($cmd));
38
39	if ($echo) var_dump($out, $out == basename($path));
40	if ($echo) var_dump(realpath($path));
41
42	set_active_cp($old_cp, $echo);
43
44	return $out;
45}
46
47function skip_if_wrong_cp($cp, $kind = "")
48{
49	if (get_active_cp($kind) != $cp) {
50		die("skip this test expect codepage $cp");
51	}
52}
53
54function skip_if_no_required_exts()
55{
56	$exts = func_get_args();
57	$exts[] = "iconv";
58
59	foreach ($exts as $ext) {
60		if (!extension_loaded($ext)) {
61			die("skip $ext is not loaded");
62		}
63	}
64}
65
66function skip_if_not_win()
67{
68	if(substr(PHP_OS, 0, 3) != 'WIN' ) {
69		die('skip windows only test');
70	}
71}
72
73function create_verify_file($prefix, $basename, $content = "", $cp = 65001)
74{
75	$full = $prefix . DIRECTORY_SEPARATOR . $basename;
76
77	if (!touch($full)) {
78		echo "failed to touch create $full\n";
79		return;
80	}
81
82	$now = get_basename_with_cp($full, $cp, false);
83	if ($now !== $basename) {
84		echo "expected '$basename', got '$now'\n";
85		return;
86	}
87
88	if ($content) {
89		file_put_contents($full, $content);
90	}
91}
92
93function create_verify_dir($prefix, $basename, $cp = 65001)
94{
95	$full = $prefix . DIRECTORY_SEPARATOR . $basename;
96
97	if (!mkdir($full) || get_basename_with_cp($full, $cp, false) !== $basename) {
98		echo "failed to create dir '$full'\n";
99	}
100}
101
102function remove_data($id, $dir = NULL)
103{
104	if (!$dir) {
105		$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $id;
106	}
107
108	if (is_dir($dir)) {
109		$objects = scandir($dir);
110		foreach ($objects as $object) {
111			if ($object != "." && $object != "..") {
112				if (filetype($dir . DIRECTORY_SEPARATOR . $object) == "dir")
113					remove_data($id, $dir . DIRECTORY_SEPARATOR . $object);
114				else
115					unlink($dir . DIRECTORY_SEPARATOR . $object);
116			}
117		}
118		reset($objects);
119		rmdir($dir);
120	}
121}
122
123function create_data($id, $item = "", $cp = 65001, $utf8 = true)
124{
125	if ($utf8) {
126		/* Keep this file ASCII, so zend.multibyte related stuff can be tasted as well. */
127		include dirname(__FILE__) . DIRECTORY_SEPARATOR . "util_utf8.inc";
128		return create_data_from_utf8($id, $item, $cp);
129	} else {
130
131		$prefix = dirname(__FILE__) . DIRECTORY_SEPARATOR . $id;
132
133		if (!is_dir($prefix)) {
134			mkdir($prefix);
135		}
136
137		if (0 === strpos($id, "dir")) {
138			create_verify_dir($prefix, $item, $cp);
139		} else if (0 === strpos($id, "file")) {
140			/* a bit unhandy, but content can be put from outside, if needed */
141			create_verify_file($prefix, $item, "dummy content", $cp);
142		} else {
143			echo "Item has either to start with \"dir\" or \"file\"";
144		}
145	}
146
147	return $prefix;
148}
149