1--TEST--
2Test fgetss() function : usage variations - read/write modes, file pointer at EOF
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) != 'WIN') {
6    die('skip.. only valid for Windows');
7}
8?>
9--FILE--
10<?php
11error_reporting(E_ALL & ~E_DEPRECATED);
12
13/*
14 Prototype: string fgetss ( resource $handle [, int $length [, string $allowable_tags]] );
15 Description: Gets line from file pointer and strip HTML tags
16*/
17
18/* try fgetss on files which are opened in read/write modes
19    w+, w+b, w+t,
20    a+, a+b, a+t,
21    x+, x+b, x+t
22*/
23
24
25echo "*** Testing fgetss() : usage variations ***\n";
26
27/* string with html and php tags */
28$string_with_tags = <<<EOT
29<test>Testing fgetss() functions</test>
30<?php echo "this string is within php tag"; ?> {;}<{> this
31is a heredoc string. <pg>ksklnm@@$$&$&^%&^%&^%&</pg>
32<html> html </html> <?php echo "php"; ?>
33this line is without any html and php tags
34this is a line with more than eighty character,want to check line splitting correctly after 80 characters
35this text contains some html tags <body> body </body> <br> br </br>
36this is the line with \n character.
37EOT;
38if(substr(PHP_OS, 0, 3) == "WIN")  {
39	$string_with_tags = str_replace("\r",'', $string_with_tags);
40}
41
42$filename = __DIR__."/fgetss_variation5.tmp";
43
44/* try reading the file opened in different modes of reading */
45$file_modes = array("w+","w+b", "w+t","a+", "a+b", "a+t","x+","x+b","x+t");
46
47for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
48  echo "\n-- Testing fgetss() with file opened using $file_modes[$mode_counter] mode --\n";
49
50  /* create an empty file and write the strings with tags */
51  $file_handle = fopen($filename, $file_modes[$mode_counter]);
52  fwrite($file_handle,$string_with_tags); //writing data to the file
53  if(!$file_handle) {
54    echo "Error: failed to open file $filename!\n";
55    exit();
56  }
57  // rewind the file pointer to beginning of the file
58  var_dump( filesize($filename) );
59  var_dump( rewind($file_handle) );
60  var_dump( ftell($file_handle) );
61  var_dump( feof($file_handle) );
62
63  echo "-- Reading when file pointer points to EOF --\n";
64  var_dump( fseek($file_handle,0,SEEK_END) ); // now file pointer at end
65  var_dump( ftell($file_handle) ); //ensure file pointer at end
66  var_dump( fgetss($file_handle) ); // try to read
67  var_dump( ftell($file_handle) ); // find out file position
68  var_dump( feof($file_handle) );  // ensure that file pointer is at eof
69
70  // now file is at the end try reading with length and allowable tags,expecting false
71  var_dump( fgetss($file_handle, 80, "<test>, <html>, <?>") );
72  var_dump( ftell($file_handle) );  // find out file position
73  var_dump( feof($file_handle) );   // ensure that file pointer is at eof
74
75
76  // close the file
77  fclose($file_handle);
78
79  // delete the file
80  unlink($filename);
81} // end of for - mode_counter
82
83echo "Done\n";
84?>
85--EXPECT--
86*** Testing fgetss() : usage variations ***
87
88-- Testing fgetss() with file opened using w+ mode --
89int(445)
90bool(true)
91int(0)
92bool(false)
93-- Reading when file pointer points to EOF --
94int(0)
95int(445)
96bool(false)
97int(445)
98bool(true)
99bool(false)
100int(445)
101bool(true)
102
103-- Testing fgetss() with file opened using w+b mode --
104int(445)
105bool(true)
106int(0)
107bool(false)
108-- Reading when file pointer points to EOF --
109int(0)
110int(445)
111bool(false)
112int(445)
113bool(true)
114bool(false)
115int(445)
116bool(true)
117
118-- Testing fgetss() with file opened using w+t mode --
119int(453)
120bool(true)
121int(0)
122bool(false)
123-- Reading when file pointer points to EOF --
124int(0)
125int(453)
126bool(false)
127int(453)
128bool(true)
129bool(false)
130int(453)
131bool(true)
132
133-- Testing fgetss() with file opened using a+ mode --
134int(445)
135bool(true)
136int(0)
137bool(false)
138-- Reading when file pointer points to EOF --
139int(0)
140int(445)
141bool(false)
142int(445)
143bool(true)
144bool(false)
145int(445)
146bool(true)
147
148-- Testing fgetss() with file opened using a+b mode --
149int(445)
150bool(true)
151int(0)
152bool(false)
153-- Reading when file pointer points to EOF --
154int(0)
155int(445)
156bool(false)
157int(445)
158bool(true)
159bool(false)
160int(445)
161bool(true)
162
163-- Testing fgetss() with file opened using a+t mode --
164int(453)
165bool(true)
166int(0)
167bool(false)
168-- Reading when file pointer points to EOF --
169int(0)
170int(453)
171bool(false)
172int(453)
173bool(true)
174bool(false)
175int(453)
176bool(true)
177
178-- Testing fgetss() with file opened using x+ mode --
179int(445)
180bool(true)
181int(0)
182bool(false)
183-- Reading when file pointer points to EOF --
184int(0)
185int(445)
186bool(false)
187int(445)
188bool(true)
189bool(false)
190int(445)
191bool(true)
192
193-- Testing fgetss() with file opened using x+b mode --
194int(445)
195bool(true)
196int(0)
197bool(false)
198-- Reading when file pointer points to EOF --
199int(0)
200int(445)
201bool(false)
202int(445)
203bool(true)
204bool(false)
205int(445)
206bool(true)
207
208-- Testing fgetss() with file opened using x+t mode --
209int(453)
210bool(true)
211int(0)
212bool(false)
213-- Reading when file pointer points to EOF --
214int(0)
215int(453)
216bool(false)
217int(453)
218bool(true)
219bool(false)
220int(453)
221bool(true)
222Done
223