-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexerpt.php
More file actions
107 lines (90 loc) · 3.17 KB
/
exerpt.php
File metadata and controls
107 lines (90 loc) · 3.17 KB
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
<?php
/* exerpt.php
* does some magic and returns an exerpt of the selected file
*/
require_once('includes/search.php');
/* get variables */
$fileName = filter_input(INPUT_GET, 'file');
$fileNamePath = '../' . DIRECTORY_SEPARATOR . $fileName;
$startLineNum = filter_input(INPUT_GET, 'start');
$endLineNum = filter_input(INPUT_GET, 'end');
$queryLineNum = filter_input(INPUT_GET, 'query');
/* AJAX check */
if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
$ajax = TRUE;
} else {
$ajax = FALSE;
}
/* if not an AJAX request, style the webpage */
if ($ajax == FALSE) {
echo "<html>";
echo "<head>";
echo "<meta charset='UTF-8'>";
echo "<title>IRC Log Exerpt - $fileName</title>";
echo "<link rel='stylesheet' type='text/css' href='index.css' />";
echo "</head>";
echo "<body>";
echo "<div id='container'>";
echo "<header>";
echo "<h1>IRC Log Exerpt - $fileName</h1>";
echo "</header>";
}
if (file_exists($fileNamePath)) {
$file = new SplFileObject($fileNamePath, 'r');
/* check that our queried lines fit within the range */
if ($startLineNum < 0) {
$startLineNum = 0;
}
$file->seek($startLineNum);
/* get the next 5 lines from the startPosition */
while (($file->key() <= $endLineNum) && !$file->eof()) {
$lines[$file->key()] = $file->current();
$file->next();
}
$nextQueryData = array(
'start' => $startLineNum,
'end' => $endLineNum + 6,
'query' => $queryLineNum,
'file' => $fileName
);
$nextQuery = http_build_query($nextQueryData, '', '&');
$prevQueryData = array(
'start' => $startLineNum - 6,
'end' => $endLineNum,
'query' => $queryLineNum,
'file' => $fileName
);
$prevQuery = http_build_query($prevQueryData, '', '&');
/* check for start */
if ($startLineNum == 0) {
echo "START" . PHP_EOL;
} else {
/* link to get more from start */
echo "<a href='exerpt?$prevQuery' class='getExerptLink'>MORE</a>" . PHP_EOL;
}
$numberOfLines = $endLineNum - $startLineNum;
echo "<div class='exerpt' data-lines='$numberOfLines'>" . PHP_EOL;
echo "<ol class='exerpt' start='$startLineNum'>" . PHP_EOL;
foreach ($lines as $lineNum => $line) {
/* if our line number is the select line number we add the 'selected' class, to bold it. */
$selected = ($lineNum == $queryLineNum) ? "class='selected'" : '';
$htmlSafeLine = htmlspecialchars($line);
$linkedLine = autoLink($htmlSafeLine);
echo "<li $selected data-line='$lineNum'><pre>$linkedLine</pre></li>" . PHP_EOL;
}
echo "</ol>" . PHP_EOL;
echo "</div>" . PHP_EOL;
/* check for end */
if ($file->eof()) {
echo "END" . PHP_EOL;
} else {
/* link to get more from end */
echo "<a href='exerpt?$nextQuery' class='getExerptLink'>MORE</a>" . PHP_EOL;
}
}
/* close tags if not ajax request */
if ($ajax == FALSE) {
echo "</div>";
echo "</body>";
echo "</html>";
}