File: reverseRead.php

package info (click to toggle)
simplesamlphp 1.14.11-1%2Bdeb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 15,024 kB
  • sloc: php: 72,337; xml: 1,078; python: 376; sh: 220; perl: 185; makefile: 57
file content (220 lines) | stat: -rw-r--r-- 5,221 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
 * Functionatility for line by line reverse reading of a file. It is done by blockwise
 * fetching the file from the end and putting the lines into an array.
 * 
 * @author Thomas Graff<thomas.graff@uninett.no>
 *
 */
class sspmod_logpeek_File_reverseRead{
	// 8192 is max number of octets limited by fread.
	private $blockSize;
	private $blockStart;
	private $fileHandle;
	// fileSize may be changed after initial file size check
	private $fileSize;
	private $fileMtime;
	// Array containing file lines
	private $content;
	// Leftover before first complete line
	private $remainder;
	// Count read lines from the end
	private $readPointer;
	
	/**
	 * File is checked and file handle to file is opend. But no data is read
	 * from the file.
	 * 
	 * @param string $fileUrl Path and filename to file to be read
	 * @param int $blockSize File read block size in byte
	 * @return bool Success
	 */
	public function __construct($fileUrl, $blockSize = 8192){
		if(!is_readable($fileUrl)){
			return FALSE;
		}
		
		$this->blockSize = $blockSize;
		$this->content = array();
		$this->remainder = '';
		$this->readPointer = 0;
		
		$fileInfo = stat($fileUrl);
		$this->fileSize = $this->blockStart = $fileInfo['size'];
		$this->fileMtime = $fileInfo['mtime'];
		
		if($this->fileSize > 0){
			$this->fileHandle = fopen($fileUrl, 'rb');
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	
	public function __destruct(){
		if(is_resource($this->fileHandle)){
			fclose($this->fileHandle);
		}
	}
	
	/**
	 * Fetch chunk of data from file.
	 * Each time this function is called, will it fetch a chunk
	 * of data from the file. It starts from the end of the file
	 * and work towards the beginning of the file.
	 * 
	 * @return string buffer with datablock.
	 * Will return bool FALSE when there is no more data to get.
	 */
	private function readChunk(){
		$splits = $this->blockSize;
		
		$this->blockStart -= $splits;
		if($this->blockStart < 0){
			$splits += $this->blockStart;
			$this->blockStart = 0;
		}
		
		// Return false if nothing more to read
		if($splits === 0){
			return FALSE;
		}
		
		fseek($this->fileHandle, $this->blockStart, SEEK_SET);
		$buff = fread($this->fileHandle, $splits);

		return $buff;
	}
	
	/**
	 * Get one line of data from the file, starting from the end of the file.
	 * 
	 * @return string One line of data from the file.
	 * Bool FALSE when there is no more data to get.
	 */
	public function getPreviousLine(){
		if(count($this->content) === 0 || $this->readPointer < 1){
			
			do {
				$buff = $this->readChunk();
				
				if($buff !== FALSE){
					$eolPos = strpos($buff, "\n");
				}else{
					// Empty buffer, no more to read.
					if(strlen($this->remainder) > 0){
						$buff = $this->remainder;
						$this->remainder = '';
						// Exit from while-loop
						break;
					}else{
						// Remainder also empty.
						return FALSE;
					}
				}
				
				if($eolPos === FALSE){
					// No eol found. Make buffer head of remainder and empty buffer.
					$this->remainder = $buff . $this->remainder;
					$buff = '';
				}elseif($eolPos !== 0){
					// eol found.
					$buff .= $this->remainder;
					$this->remainder = substr($buff, 0, $eolPos);
					$buff = substr($buff, $eolPos+1);
				}elseif($eolPos === 0){
					$buff .= $this->remainder;
					$buff = substr($buff, 1);
					$this->remainder = '';
				}
				
			}while(($buff !== FALSE) && ($eolPos === FALSE));
			
			$this->content = explode("\n", $buff);
			$this->readPointer = count($this->content);
		}
		
		if(count($this->content) > 0){
			return $this->content[--$this->readPointer];
		}else{
			return FALSE;
		}
	}
	
	
	private function cutHead(&$haystack, $needle, $exit){
		$pos = 0;
		$cnt = 0;
		// Holder på inntill antall ønskede linjer eller vi ikke finner flere linjer
		while($cnt < $exit && ($pos = strpos($haystack, $needle, $pos)) !==false ){
			$pos++;
			$cnt++;
		}   
		return ($pos === false) ? false : substr($haystack, $pos, strlen($haystack));
	}
	
	
	// FIXME: This function hawe som error, do not use before auditing and testing
	public function getTail($lines = 10){
		$this->blockStart = $this->fileSize;
		$buff1 = Array();
		$lastLines = array();
		
		while($this->blockStart){
			$buff = $this->readChunk();
			if(!$buff)break;
			
			$lines -= substr_count($buff, "\n");
			
			if($lines <= 0)
			{
				$buff1[] = $this->cutHead($buff, "\n", abs($lines)+1);
				break;
			}
			$buff1[] = $buff;
		}
		
		for($i = count($buff1); $i >= 0; $i--){
			$lastLines = array_merge($lastLines, explode("\n", $buff1[$i]));
		}
		
		return $lastLines;
	}
	
	
	private function getLineAtPost($pos){
		if($pos < 0 || $pos > $this->fileSize){
			return FALSE;
		}
		
		$seeker = $pos;
		fseek($this->fileHandle, $seeker, SEEK_SET);
		while($seeker > 0 && fgetc($this->fileHandle) !== "\n"){
			fseek($this->fileHandle, --$seeker, SEEK_SET);
		}
		
		return rtrim(fgets($this->fileHandle));
	}
	
	
	public function getFirstLine(){
		return $this->getLineAtPost(0);
	}
	
	
	public function getLastLine(){
		return $this->getLineAtPost($this->fileSize-2);
	}
	
	
	public function getFileSize(){
		return $this->fileSize;
	}
	
	
	public function getFileMtime(){
		return $this->fileMtime;
	}
	
}