Repository navigation
Expand file tree
/
Copy pathIP-plugin.php
More file actions
248 lines (217 loc) · 8.76 KB
/
Copy pathIP-plugin.php
File metadata and controls
248 lines (217 loc) · 8.76 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
$plugin['name'] = 'VIS_IP_LOG';
$plugin['version'] = '1';
$plugin['author'] = 'C.S. Wilson';
$plugin['author_uri'] = 'https://www.hobbiesfordays.com/';
$plugin['description'] = 'Logs visitor IP addresses (IPv4/IPv6) to txp_log.ip_address and displays them on the Visitor logs panel.';
$plugin['type'] = 1; // 4.8.x: 0=public, 1=admin+public, 2=library, 3=admin, 4=admin+ajax, 5=admin+public+ajax
$plugin['order'] = 5;
$plugin['flags'] = 2; // PLUGIN_LIFECYCLE_NOTIFY (so install/enable callbacks fire)
if (0) {
?>
# --- BEGIN PLUGIN HELP ---
vis_ip_log
==========
- Ensures `txp_log.ip_address` exists (VARCHAR 45 + index) on install/enable.
- Captures IPv4/IPv6 (CF / X-Forwarded-For / X-Real-IP / REMOTE_ADDR).
- Shows an **IP Address** column on **Admin → Visitor logs** (server-side), with a silent DOM fallback/fill.
- IPs start storing when the plugin becomes active - it is not retroactive. So your IP Address column on Visitor Logs will be blank until you get visitors.
# --- END PLUGIN HELP ---
<?php
}
# --- BEGIN PLUGIN CODE ---
if (!defined('txpinterface')) {
die('txp');
}
/* =========================================================
ADMIN: INSTALL / ENSURE COLUMN (runs on install & enable)
========================================================= */
if (txpinterface === 'admin') {
// Ensure column on install/enable
register_callback('vis_ip_install_or_enable', 'plugin_lifecycle.vis_ip_log', 'installed');
register_callback('vis_ip_install_or_enable', 'plugin_lifecycle.vis_ip_log', 'enabled');
// Safety net: also verify on each admin page load
register_callback('vis_ip_admin_ensure_column', 'admin_side', 'head_end');
}
function vis_ip_install_or_enable($evt, $step){ vis_ip_ensure_column(); }
function vis_ip_admin_ensure_column($evt, $step){ vis_ip_ensure_column(); }
function vis_ip_ensure_column()
{
$tbl = safe_pfx('txp_log');
$rs = safe_query("SHOW COLUMNS FROM `{$tbl}` LIKE 'ip_address'");
if (!($rs && nextRow($rs))) {
safe_alter('txp_log', "ADD COLUMN `ip_address` VARCHAR(45) NULL DEFAULT NULL");
@safe_query("ALTER TABLE `{$tbl}` ADD INDEX `ip_address` (`ip_address`)");
}
}
/* =========================================================
PUBLIC: CAPTURE & STORE IP
========================================================= */
if (txpinterface === 'public') {
register_callback('vis_ip_log_capture', 'log_hit'); // tolerant (2 or 3 args)
register_callback('vis_ip_after_render', 'textpattern_end'); // post-insert fallback
}
/* Pre-insert attempt (if TXP passes the log row by-ref). */
function vis_ip_log_capture($evt=null, $stp=null, &$row=null)
{
$ip = vis_ip_detect();
if (!$ip) return;
$GLOBALS['vis_ip_address'] = $ip; // stash for fallback
if (is_array($row)) {
$row['ip_address'] = $ip;
return $row;
}
}
/* Post-insert fallback: update newest matching row if needed. */
function vis_ip_after_render($evt=null, $stp=null)
{
$ip = isset($GLOBALS['vis_ip_address']) ? $GLOBALS['vis_ip_address'] : vis_ip_detect();
if (!$ip) return;
$tbl = safe_pfx('txp_log');
$page = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$ipq = doSlash($ip);
$pgq = doSlash($page);
@safe_query("
UPDATE `{$tbl}`
SET `ip_address` = '{$ipq}'
WHERE (`ip_address` IS NULL OR `ip_address` = '')
AND `page` = '{$pgq}'
AND `time` >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
ORDER BY id DESC
LIMIT 1
");
}
/* Determine client IP with proxy awareness (IPv4/IPv6). */
function vis_ip_detect()
{
$c = array();
if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) $c[] = $_SERVER['HTTP_CF_CONNECTING_IP'];
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
foreach (explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']) as $x) {
$x = trim($x);
if ($x !== '') $c[] = $x;
}
}
if (!empty($_SERVER['HTTP_X_REAL_IP'])) $c[] = $_SERVER['HTTP_X_REAL_IP'];
if (!empty($_SERVER['REMOTE_ADDR'])) $c[] = $_SERVER['REMOTE_ADDR'];
foreach ($c as $ip) { // prefer public
$ip = trim($ip, " []");
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) return $ip;
}
foreach ($c as $ip) { // else any valid
$ip = trim($ip, " []");
if (filter_var($ip, FILTER_VALIDATE_IP)) return $ip;
}
return null;
}
/* =========================================================
ADMIN: VISITOR LOGS COLUMN (server-side + silent DOM fallback/fill)
========================================================= */
if (txpinterface === 'admin') {
// Server-side hooks across common variants
foreach (array('log','log_ui','lore','lore_ui') as $event) {
foreach (array('list.head','list_head') as $s) register_callback('vis_ip_head_cell', $event, $s);
foreach (array('list.row','list_row') as $s) register_callback('vis_ip_row_cell', $event, $s);
}
// DOM fallback + client-side fill (no debug UI)
register_callback('vis_ip_dom_helpers', 'admin_side', 'body_end');
}
/* Helper: am I on Visitor logs? (4.8 = log, 4.9 = lore) */
function vis_is_logs_panel()
{
$ev = gps('event');
return ($ev === 'log' || $ev === 'lore');
}
/* Header cell: “IP Address” */
function vis_ip_head_cell($evt, $stp, $data, $rs)
{
if (!vis_is_logs_panel()) return $data;
if (strpos($data, 'txp-list-col-ip_address') !== false) return $data;
$th = "\n\t".'<th class="txp-list-col-ip_address" data-col="ip_address" scope="col">IP Address</th>';
$out = preg_replace('~(<th[^>]*class="[^"]*txp-list-col-status[^"]*"[^>]*>.*?</th>)~i', '$1'.$th, $data, 1);
return ($out !== null && $out !== $data) ? $out : str_replace('</tr>', $th.'</tr>', $data);
}
/* Row cell: populate with ip_address (robust row id detection). */
function vis_ip_row_cell($evt, $stp, $data, $rs)
{
if (!vis_is_logs_panel()) return $data;
if (strpos($data, 'txp-list-col-ip_address') !== false) return $data;
// 1) Try id from recordset
$id = !empty($rs['id']) ? (int)$rs['id'] : 0;
// 2) Fallback: parse id from checkbox value in the rendered row
if (!$id) {
if (preg_match('~name=[\'"]selected\[\][\'"][^>]*\bvalue=[\'"](\d+)[\'"]~i', $data, $m)) {
$id = (int)$m[1];
} elseif (preg_match('~\bvalue=[\'"](\d+)[\'"]~i', $data, $m)) {
$id = (int)$m[1];
}
}
// 3) Fetch IP by id
$ip = ($id > 0) ? (string) safe_field('ip_address', 'txp_log', 'id = '.$id) : '';
$td = "\n\t".'<td class="txp-list-col-ip_address">'.txpspecialchars($ip).'</td>';
$out = preg_replace('~(<td[^>]*class="[^"]*txp-list-col-status[^"]*"[^>]*>.*?</td>)~i', '$1'.$td, $data, 1);
return ($out !== null && $out !== $data) ? $out : str_replace('</tr>', $td.'</tr>', $data);
}
/* DOM fallback + client-side fill: ensure header/cells exist; fill from a compact JSON map. */
function vis_ip_dom_helpers($evt, $stp)
{
if (!vis_is_logs_panel()) return;
// Build a compact id->ip map for recent rows (no Ajax needed)
$map = array();
$rs = safe_rows_start('id, ip_address', safe_pfx('txp_log'),
"ip_address IS NOT NULL AND ip_address <> '' ORDER BY id DESC LIMIT 400");
if ($rs) {
while ($a = nextRow($rs)) {
$map[(int)$a['id']] = (string)$a['ip_address'];
}
}
$json = json_encode($map);
echo <<<HTML
<script>
(function(){
try{
var table=document.querySelector('.txp-listtables table.txp-list');
if(!table) return;
// Ensure header exists
if(!table.querySelector('th.txp-list-col-ip_address')){
var headRow=table.querySelector('thead tr');
if(headRow){
var th=document.createElement('th');
th.className='txp-list-col-ip_address';
th.setAttribute('data-col','ip_address');
th.setAttribute('scope','col');
th.textContent='IP Address';
headRow.appendChild(th);
}
}
// Ensure cells exist & collect id->cell map
var rows=table.querySelectorAll('tbody tr');
var idToCell={}, ids=[];
rows.forEach(function(tr){
var cb=tr.querySelector('input[name="selected[]"]');
if(!cb || !cb.value) return;
var id=cb.value.trim();
var cell=tr.querySelector('td.txp-list-col-ip_address');
if(!cell){
cell=document.createElement('td');
cell.className='txp-list-col-ip_address';
tr.appendChild(cell);
}
idToCell[id]=cell;
ids.push(id);
});
// Fill from server-emitted map
var MAP = $json || {};
ids.forEach(function(id){
var ip = MAP[id];
if(ip && idToCell[id] && !idToCell[id].textContent){
idToCell[id].textContent = ip;
}
});
}catch(e){}
})();
</script>
HTML;
}
# --- END PLUGIN CODE ---
?>