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)) {
98		echo "failed to create dir '$full'\n";
99		return;
100	}
101
102	$now = get_basename_with_cp($full, $cp, false);
103	if ($now !== $basename) {
104		echo "expected '$basename', got '$now'\n";
105	}
106}
107
108function remove_data($id, $dir = NULL)
109{
110	if (!$dir) {
111		$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $id;
112	}
113
114	if (is_dir($dir)) {
115		$objects = scandir($dir);
116		foreach ($objects as $object) {
117			if ($object != "." && $object != "..") {
118				if (filetype($dir . DIRECTORY_SEPARATOR . $object) == "dir")
119					remove_data($id, $dir . DIRECTORY_SEPARATOR . $object);
120				else
121					unlink($dir . DIRECTORY_SEPARATOR . $object);
122			}
123		}
124		reset($objects);
125		rmdir($dir);
126	}
127}
128
129function create_data($id, $item = "", $cp = 65001, $utf8 = true)
130{
131	if ($utf8) {
132		/* Keep this file ASCII, so zend.multibyte related stuff can be tasted as well. */
133		include dirname(__FILE__) . DIRECTORY_SEPARATOR . "util_utf8.inc";
134		return create_data_from_utf8($id, $item, $cp);
135	} else {
136
137		$prefix = dirname(__FILE__) . DIRECTORY_SEPARATOR . $id;
138
139		if (!is_dir($prefix)) {
140			mkdir($prefix);
141		}
142
143		if (0 === strpos($id, "dir")) {
144			create_verify_dir($prefix, $item, $cp);
145		} else if (0 === strpos($id, "file")) {
146			/* a bit unhandy, but content can be put from outside, if needed */
147			create_verify_file($prefix, $item, "dummy content", $cp);
148		} else {
149			echo "Item has either to start with \"dir\" or \"file\"";
150		}
151	}
152
153	return $prefix;
154}
155