-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonDataRetriever.php
117 lines (110 loc) · 5.1 KB
/
CommonDataRetriever.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
<?php
namespace phpWTL;
use phpWTL\aBasicDataRetriever;
use phpWTL\LogWriter\FLW\FileLogWriterHelper;
require_once 'aBasicDataRetriever.php';
require_once 'LogWriter/FLW/FileLogWriterHelper.php';
/**
* Data retriever for the common log format.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.2.5
* @api
*/
class CommonDataRetriever extends aBasicDataRetriever {
/**
* @param array $inject Array containing objects for LoggerContent [0] and RetrievalPolicies [1] (may be null). The FieldDescriptor is derived from the LoggerContent object.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.1
*/
protected function __construct($inject= null) {
if ($inject && count($inject)==2) {
static::$loggerContent= $inject[0];
if ($inject[1]) static::$retrievalPolicies= $inject[1];
static::$fieldDescriptor= static::$loggerContent->getFormatDescriptor();
}
}
/**
* Retrieve data for a single log field and store it in the associated LoggerContent object.
*
* @param string $field_name ID of log format field.
* @param string $value Provide an (optional) value to pass thru to the LoggerContent object, so allowing for the injection of external data.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.6
* @api
*/
public function retrieveField($field_name, $value= null) {
if (static::$fieldDescriptor && static::$loggerContent) {
if ($value==null) {
switch ($field_name) {
case "host_ip":
$value= $_SERVER['REMOTE_ADDR'];
break;
case "client_identity":
$value= "-";
break;
case "user_id":
$value= (array_key_exists ('REMOTE_USER', $_SERVER) ? $_SERVER['REMOTE_USER'] : "-");
break;
case "timestamp":
$value= FormatDescriptorHelper::timestamp2datetimeString($_SERVER['REQUEST_TIME']);
break;
case "request_line":
if (DataRetrievalPolicyHelper::existsDataRetrievalPolicy(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL) &&
DataRetrievalPolicyHelper::getDataRetrievalPolicyFlag(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL)==DRP::DRP_CLR_CUSTOM) {
$requestTarget= DataRetrievalPolicyHelper::getDataRetrievalPolicyParameter(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL);
$scriptBaseFull= str_replace(basename($_SERVER["SCRIPT_FILENAME"]), "", $_SERVER['SCRIPT_FILENAME']);
$scriptBaseRel= str_replace($_SERVER['DOCUMENT_ROOT'], "", $scriptBaseFull);
$rtParts= FileLogWriterHelper::separatePathAndFile($requestTarget);
if (FileLogWriterHelper::isAbsolutePath($requestTarget)) {
// absolute target (with webserver root ("htdocs") as absolute root)
if (!FileLogWriterHelper::pathLeavesOrEqualsRoot($rtParts['pathname'], FileLogWriterHelper::FOLDER_SEPARATOR)) {
$target= FileLogWriterHelper::sanitizePath($rtParts['pathname']);
} else {
$target= $scriptBaseRel;
}
} else {
// relative target
$target= FileLogWriterHelper::sanitizePath($scriptBaseRel.$rtParts['pathname']);
}
$final_path= FileLogWriterHelper::FOLDER_SEPARATOR.FileLogWriterHelper::cleanupPath($target).$rtParts['filename'];
$value= $_SERVER['REQUEST_METHOD']." ".$final_path." ".$_SERVER['SERVER_PROTOCOL'];
} else {
$value= $_SERVER['REQUEST_METHOD']." ".$_SERVER['REQUEST_URI']." ".$_SERVER['SERVER_PROTOCOL'];
}
break;
case "status_code":
$value= http_response_code();
break;
case "content_size":
$value= "0";
if (DataRetrievalPolicyHelper::existsDataRetrievalPolicy(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL) &&
DataRetrievalPolicyHelper::getDataRetrievalPolicyFlag(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL)==DRP::DRP_CLR_CUSTOM) {
$requestTarget= DataRetrievalPolicyHelper::getDataRetrievalPolicyParameter(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL);
$basepath= str_replace(basename($_SERVER["SCRIPT_FILENAME"]), "", $_SERVER['SCRIPT_FILENAME']);
if (FileLogWriterHelper::isAbsolutePath($requestTarget)) {
$requestpath= $basepath.substr($requestTarget, 1, strlen($requestTarget));
} else {
$requestpath= $basepath.$requestTarget;
}
if (file_exists($requestpath)) {
$value= filesize($requestpath);
} else {
static::$loggerContent->__set("status_code", "404");
}
} elseif (DataRetrievalPolicyHelper::existsDataRetrievalPolicy(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL) &&
DataRetrievalPolicyHelper::getDataRetrievalPolicyFlag(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL)==DRP::DRP_CLR_BUFFER) {
$value= DataRetrievalPolicyHelper::getDataRetrievalPolicyParameter(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL);
} else {
$value= filesize($_SERVER['SCRIPT_FILENAME']);
}
break;
}
if ($value!="") static::$loggerContent->__set($field_name, $value);
} else static::$loggerContent->__set($field_name, $value);
}
}
}
?>