Project

General

Profile

Download (1.87 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * live_logs.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2023-2025 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
/*
23
 * Uses bytes to read the file unless line number is specified; reading using
24
 * bytes is slightly quicker, but line numbers can be helpful.
25
 */
26
function checkForAjaxLog($file) {
27
	$response = [];
28
	$maxRead = (isset($_REQUEST['maxRead'])) ? $_REQUEST['maxRead'] : null;
29

    
30
	if (isset($_REQUEST['line'])) {
31
		$response['linesRead'] = 0;
32
		$response['output'] = '';
33

    
34
		if ($maxRead != 0)  {
35
			$line = $_REQUEST['line'];
36
			$fp = new SplFileObject($file);
37
			$fp->seek($line);
38
			for ($i = 0; !$fp->eof(); $i++) {
39
				$response['output'] .= $fp->current();
40
				$fp->next();
41
				$response['linesRead']++;
42
				if ($maxRead != null && $maxRead >= $response['linesRead']) {
43
					break;
44
				}
45
			}
46
			$fp = null;
47
		}
48
	} else {
49
		$response['bytesRead'] = 0;
50
		$response['output'] = '';
51

    
52
		if ($maxRead != 0) {
53
			$byte = (isset($_REQUEST['byte'])) ? $_REQUEST['byte'] : 0;
54
			$fp = fopen($file, 'rb');
55
			fseek($fp, $byte);
56

    
57
			if ($maxRead != null) {
58
				$response['output'] = fread($fp, $maxRead);
59
			} else {
60
				while (!feof($fp)) {
61
					$response['output'] .= fread($fp, 8192);
62
				}
63
			}
64

    
65
			$response['bytesRead'] = ftell($fp) - $byte;
66
			fclose($fp);
67
		}
68
	}
69

    
70
	echo json_encode($response);
71
}
(29-29/61)