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.. Not 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;
38
39$filename = __DIR__."/fgetss_variation5.tmp";
40
41/* try reading the file opened in different modes of reading */
42$file_modes = array("w+","w+b", "w+t","a+", "a+b", "a+t","x+","x+b","x+t");
43
44for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
45  echo "\n-- Testing fgetss() with file opened using $file_modes[$mode_counter] mode --\n";
46
47  /* create an empty file and write the strings with tags */
48  $file_handle = fopen($filename, $file_modes[$mode_counter]);
49  fwrite($file_handle,$string_with_tags); //writing data to the file
50  if(!$file_handle) {
51    echo "Error: failed to open file $filename!\n";
52    exit();
53  }
54  // rewind the file pointer to beginning of the file
55  var_dump( filesize($filename) );
56  var_dump( rewind($file_handle) );
57  var_dump( ftell($file_handle) );
58  var_dump( feof($file_handle) );
59
60  echo "-- Reading when file pointer points to EOF --\n";
61  var_dump( fseek($file_handle,0,SEEK_END) ); // now file pointer at end
62  var_dump( ftell($file_handle) ); //ensure file pointer at end
63  var_dump( fgetss($file_handle) ); // try to read
64  var_dump( ftell($file_handle) ); // find out file position
65  var_dump( feof($file_handle) );  // ensure that file pointer is at eof
66
67  // now file is at the end try reading with length and allowable tags,expecting false
68  var_dump( fgetss($file_handle, 80, "<test>, <html>, <?>") );
69  var_dump( ftell($file_handle) );  // find out file position
70  var_dump( feof($file_handle) );   // ensure that file pointer is at eof
71
72
73  // close the file
74  fclose($file_handle);
75
76  // delete the file
77  unlink($filename);
78} // end of for - mode_counter
79
80echo "Done\n";
81?>
82--EXPECT--
83*** Testing fgetss() : usage variations ***
84
85-- Testing fgetss() with file opened using w+ mode --
86int(445)
87bool(true)
88int(0)
89bool(false)
90-- Reading when file pointer points to EOF --
91int(0)
92int(445)
93bool(false)
94int(445)
95bool(true)
96bool(false)
97int(445)
98bool(true)
99
100-- Testing fgetss() with file opened using w+b mode --
101int(445)
102bool(true)
103int(0)
104bool(false)
105-- Reading when file pointer points to EOF --
106int(0)
107int(445)
108bool(false)
109int(445)
110bool(true)
111bool(false)
112int(445)
113bool(true)
114
115-- Testing fgetss() with file opened using w+t mode --
116int(445)
117bool(true)
118int(0)
119bool(false)
120-- Reading when file pointer points to EOF --
121int(0)
122int(445)
123bool(false)
124int(445)
125bool(true)
126bool(false)
127int(445)
128bool(true)
129
130-- Testing fgetss() with file opened using a+ mode --
131int(445)
132bool(true)
133int(0)
134bool(false)
135-- Reading when file pointer points to EOF --
136int(0)
137int(445)
138bool(false)
139int(445)
140bool(true)
141bool(false)
142int(445)
143bool(true)
144
145-- Testing fgetss() with file opened using a+b mode --
146int(445)
147bool(true)
148int(0)
149bool(false)
150-- Reading when file pointer points to EOF --
151int(0)
152int(445)
153bool(false)
154int(445)
155bool(true)
156bool(false)
157int(445)
158bool(true)
159
160-- Testing fgetss() with file opened using a+t mode --
161int(445)
162bool(true)
163int(0)
164bool(false)
165-- Reading when file pointer points to EOF --
166int(0)
167int(445)
168bool(false)
169int(445)
170bool(true)
171bool(false)
172int(445)
173bool(true)
174
175-- Testing fgetss() with file opened using x+ mode --
176int(445)
177bool(true)
178int(0)
179bool(false)
180-- Reading when file pointer points to EOF --
181int(0)
182int(445)
183bool(false)
184int(445)
185bool(true)
186bool(false)
187int(445)
188bool(true)
189
190-- Testing fgetss() with file opened using x+b mode --
191int(445)
192bool(true)
193int(0)
194bool(false)
195-- Reading when file pointer points to EOF --
196int(0)
197int(445)
198bool(false)
199int(445)
200bool(true)
201bool(false)
202int(445)
203bool(true)
204
205-- Testing fgetss() with file opened using x+t mode --
206int(445)
207bool(true)
208int(0)
209bool(false)
210-- Reading when file pointer points to EOF --
211int(0)
212int(445)
213bool(false)
214int(445)
215bool(true)
216bool(false)
217int(445)
218bool(true)
219Done
220