/
functions.php
179 lines (142 loc) · 4.37 KB
/
functions.php
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
<?php
/**
* Full Name: Iszak Bryan
* ITS Username: ibryan02
* Module Name: Web Programming using PHP
* Tutor Name: Tobi Brodie
*/
/**
* Determines if file is a log file
*
* @return boolean
*/
function isLogFile($path) {
return pathinfo($path, PATHINFO_EXTENSION) == 'log';
}
/**
* Read the redirectory looking for .log files and return found
* log files
*
* @param string $path The path to the log directory with the trailing slash
* @return string[]|false
*/
function findLogs($path) {
if (!is_dir($path) && is_readable($path)) {
return false;
}
$logs = array();
if ($dir = opendir($path)) {
while (($file = readdir($dir)) !== false) {
$fullPath = $path.$file;
// Check file
if (!is_file($fullPath) || !is_readable($fullPath)) {
continue;
}
// You are required to only examine files with the .log suffix, any other files or directories in this folder must be ignored.
if (isLogFile($fullPath)) {
$logs[] = $fullPath;
}
}
closedir($dir);
}
return $logs;
}
/**
* Parse a log file and return statistics about it
*
* @param string $logFile The path to the log file
* @return array|false An array of the statistics generated
*/
function parseLogFile($logFile) {
$handle = fopen($logFile, 'r');
if (!$handle) {
return false;
}
// 1. The total number of file requests in the month.
$totalRequests = 0;
// 2. The number of file requests from the articles directory.
$articleRequests = 0;
// 3. The TOTAL bandwidth consumed by the file requests over the month.
$totalBandwidth = 0;
// 4. The number of requests that resulted in 404 status errors.
$totalNotFound = 0;
// 4. Display a list of the filenames that produced these 404 errors
$filesNotFound = array();
while (feof($handle) === false) {
$line = fgets($handle);
if (strlen($line) === 0) {
continue;
}
$filename = parseFilename($line);
$statusCode = parseStatusCode($line);
$bandwidth = parseBandwidth($line);
if ($statusCode == 404) {
$totalNotFound++;
$filesNotFound[] = $filename;
}
if (strpos($filename, 'articles/') === 0) {
$articleRequests += 1;
}
$totalBandwidth += $bandwidth;
$totalRequests++;
}
fclose($handle);
// 4. (try not to repeat filenames if the same wrong filename was requested more than once)
$filesNotFound = array_unique($filesNotFound);
return array(
'month' => parseMonthName($logFile),
'totalRequests' => $totalRequests,
'articleRequests' => $articleRequests,
'totalBandwidth' => $totalBandwidth,
'totalNotFound' => $totalNotFound,
'filesNotFound' => $filesNotFound,
);
}
/**
* Parse month name from path
*
* @param string $path
* @return string
*/
function parseMonthName($path) {
return ucfirst(
pathinfo($path, PATHINFO_FILENAME)
);
}
/**
* Parse an requested file from an access log
*
* @param string $line The line from an access log
* @return string The file requested parsed from the line
*/
function parseFilename($line) {
$startOfRequest = strpos($line, '"');
$endOfMethod = strpos($line, ' ', $startOfRequest);
$endOfFilename = strpos($line, ' ', $endOfMethod + 1);
$filename = substr($line, $endOfMethod + 1, $endOfFilename - $endOfMethod - 1);
return $filename;
}
/**
* Parse an HTTP status code from an access log
*
* @param string $line The line from an access log
* @return string The HTTP status code parsed from the line
*/
function parseStatusCode($line) {
$endOfDate = strpos($line, ']') + 2;
$startOfRequest = strpos($line, '"') - 1;
$statusCode = substr($line, $endOfDate, $startOfRequest - $endOfDate);
return $statusCode;
}
/**
* Parse an bandwidth in bytes from an access log
*
* @param string $line The line from an access log
* @return string The bandwidth in bytes parsed from the line
*/
function parseBandwidth($line) {
$startOfBandwidth = strpos($line, '"', strpos($line, '"') + 1) + 2;
$endOfBandwidth = strpos($line, ' ', $startOfBandwidth);
$bandwidth = substr($line, $startOfBandwidth, $endOfBandwidth - $startOfBandwidth);
return $bandwidth;
}