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