-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebping.php
More file actions
311 lines (276 loc) · 11.6 KB
/
Copy pathwebping.php
File metadata and controls
311 lines (276 loc) · 11.6 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* Plugin Name: WebPing
* Description: A simple web-accessible ping endpoint. Visitors submit an authcode and phone number; if the authcode matches the admin-configured value, an email notification is sent.
* Version: 1.0
* Author: Dan Fishman
*/
if (!defined('ABSPATH')) {
exit;
}
define('WEBPING_PLUGIN_DIR', plugin_dir_path(__FILE__));
// ---------------------------------------------------------------------------
// Activation: create the /webping page with our shortcode
// ---------------------------------------------------------------------------
register_activation_hook(__FILE__, 'webping_activate');
function webping_activate() {
$slug = 'webping';
$existing = get_page_by_path($slug);
if (!$existing) {
$page_id = wp_insert_post([
'post_title' => 'WebPing',
'post_name' => $slug,
'post_content' => '[webping]',
'post_status' => 'publish',
'post_type' => 'page',
]);
if (!is_wp_error($page_id)) {
update_option('webping_page_id', $page_id);
}
} else {
update_option('webping_page_id', $existing->ID);
}
}
// ---------------------------------------------------------------------------
// Deactivation: optionally clean up the page
// ---------------------------------------------------------------------------
register_deactivation_hook(__FILE__, 'webping_deactivate');
function webping_deactivate() {
$page_id = get_option('webping_page_id');
if ($page_id) {
wp_trash_post($page_id);
delete_option('webping_page_id');
}
}
// ---------------------------------------------------------------------------
// Admin settings page
// ---------------------------------------------------------------------------
add_action('admin_menu', 'webping_admin_menu');
add_action('admin_init', 'webping_register_settings');
function webping_admin_menu() {
add_options_page(
'WebPing Settings',
'WebPing',
'manage_options',
'webping-settings',
'webping_render_settings_page'
);
}
function webping_register_settings() {
register_setting('webping_settings_group', 'webping_authcode', [
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => '',
]);
register_setting('webping_settings_group', 'webping_email', [
'type' => 'string',
'sanitize_callback' => 'sanitize_email',
'default' => '',
]);
add_settings_section(
'webping_main_section',
'WebPing Configuration',
function () {
echo '<p>Configure the authcode and notification email for WebPing.</p>';
},
'webping-settings'
);
add_settings_field(
'webping_authcode',
'Auth Code',
'webping_render_authcode_field',
'webping-settings',
'webping_main_section'
);
add_settings_field(
'webping_email',
'Notification Email',
'webping_render_email_field',
'webping-settings',
'webping_main_section'
);
}
function webping_render_authcode_field() {
$value = get_option('webping_authcode', '');
echo '<input type="text" name="webping_authcode" value="' . esc_attr($value) . '" class="regular-text" />';
echo '<p class="description">The auth code that must be submitted to trigger a notification.</p>';
}
function webping_render_email_field() {
$value = get_option('webping_email', '');
echo '<input type="email" name="webping_email" value="' . esc_attr($value) . '" class="regular-text" />';
echo '<p class="description">The email address that will receive ping notifications.</p>';
}
function webping_render_settings_page() {
if (!current_user_can('manage_options')) {
wp_die('Unauthorized');
}
$page_id = get_option('webping_page_id');
$page_url = $page_id ? get_permalink($page_id) : home_url('/webping');
?>
<div class="wrap">
<h1>WebPing Settings</h1>
<form method="POST" action="options.php">
<?php
settings_fields('webping_settings_group');
do_settings_sections('webping-settings');
submit_button();
?>
</form>
<hr>
<h2>Recent Pings</h2>
<?php
$history = get_option('webping_history', []);
if (empty($history)) {
echo '<p>No pings received yet.</p>';
} else {
?>
<table class="widefat fixed striped" style="max-width: 960px;">
<thead>
<tr>
<th style="width: 18%;">Time</th>
<th style="width: 15%;">Phone</th>
<th style="width: 30%;">Description</th>
<th style="width: 17%;">IP Address</th>
<th style="width: 20%;">Auth Code Valid</th>
</tr>
</thead>
<tbody>
<?php foreach ($history as $entry): ?>
<tr>
<td><?php echo esc_html($entry['time']); ?></td>
<td><?php echo esc_html($entry['phone']); ?></td>
<td><?php echo esc_html($entry['description'] ?? ''); ?></td>
<td><?php echo esc_html($entry['ip']); ?></td>
<td>
<?php if ($entry['matched']): ?>
<span style="color: #00a32a; font-weight: 600;">✓ Yes</span>
<?php else: ?>
<span style="color: #d63638;">✗ No</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php } ?>
<hr>
<h2>Usage</h2>
<p>Public form URL: <a href="<?php echo esc_url($page_url); ?>"><?php echo esc_html($page_url); ?></a></p>
<p>GET request example:</p>
<code><?php echo esc_html($page_url . '?authcode=YOUR_CODE&phone=5551234567&description=Hello'); ?></code>
</div>
<?php
}
// ---------------------------------------------------------------------------
// Front-end shortcode: [webping]
// ---------------------------------------------------------------------------
add_shortcode('webping', 'webping_shortcode');
function webping_shortcode() {
// Check for submission via GET or POST
$authcode = '';
$phone = '';
$description = '';
$submitted = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['webping_submit'])) {
$authcode = sanitize_text_field($_POST['authcode'] ?? '');
$phone = sanitize_text_field($_POST['phone'] ?? '');
$description = mb_substr(sanitize_textarea_field($_POST['description'] ?? ''), 0, 256);
$submitted = true;
} elseif (isset($_GET['authcode']) && isset($_GET['phone'])) {
$authcode = sanitize_text_field($_GET['authcode']);
$phone = sanitize_text_field($_GET['phone']);
$description = mb_substr(sanitize_textarea_field($_GET['description'] ?? ''), 0, 256);
$submitted = true;
}
if ($submitted) {
webping_process_submission($authcode, $phone, $description);
return webping_render_confirmation();
}
return webping_render_form();
}
/**
* Process the submission: log the ping, and if authcode matches, send the notification email.
*/
function webping_process_submission($authcode, $phone, $description = '') {
$stored_authcode = get_option('webping_authcode', '');
$stored_email = get_option('webping_email', '');
$matched = ($stored_authcode !== '' && $authcode === $stored_authcode);
// Log the ping to history (last 10)
webping_log_ping($phone, $matched, $description);
if ($matched && $stored_email !== '') {
$subject = 'webping';
$body = esc_html($phone) . ' accessed your webping';
if ($description !== '') {
$body .= "\n\n" . esc_html($description);
}
wp_mail($stored_email, $subject, $body);
}
}
/**
* Store a ping entry in the history (keeps the most recent 10).
*/
function webping_log_ping($phone, $authcode_matched, $description = '') {
$history = get_option('webping_history', []);
array_unshift($history, [
'phone' => $phone,
'description' => $description,
'matched' => $authcode_matched,
'time' => current_time('mysql'),
'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
]);
// Keep only the last 10
$history = array_slice($history, 0, 10);
update_option('webping_history', $history);
}
/**
* Render the submission form.
*/
function webping_render_form() {
$page_id = get_option('webping_page_id');
$form_url = $page_id ? get_permalink($page_id) : home_url('/webping');
ob_start();
?>
<div style="max-width: 420px; margin: 40px auto; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">
<h2 style="text-align: center; margin-bottom: 24px;">WebPing</h2>
<form method="POST" action="<?php echo esc_url($form_url); ?>"
style="background: #fff; padding: 28px; border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.06);">
<div style="margin-bottom: 16px;">
<label for="webping-authcode" style="display: block; font-weight: 600; margin-bottom: 6px;">Auth Code</label>
<input type="text" id="webping-authcode" name="authcode" required
style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box;" />
</div>
<div style="margin-bottom: 16px;">
<label for="webping-phone" style="display: block; font-weight: 600; margin-bottom: 6px;">Phone Number</label>
<input type="tel" id="webping-phone" name="phone" required
style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box;" />
</div>
<div style="margin-bottom: 20px;">
<label for="webping-description" style="display: block; font-weight: 600; margin-bottom: 6px;">Description</label>
<textarea id="webping-description" name="description" maxlength="256" rows="3"
style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; resize: vertical;"
placeholder="Optional (256 chars max)"></textarea>
</div>
<input type="hidden" name="webping_submit" value="1" />
<button type="submit"
style="width: 100%; padding: 12px; background: #0073aa; color: #fff; border: none; border-radius: 4px; font-size: 16px; cursor: pointer;">
Submit
</button>
</form>
</div>
<?php
return ob_get_clean();
}
/**
* Render the generic confirmation message (shown regardless of authcode validity).
*/
function webping_render_confirmation() {
ob_start();
?>
<div style="max-width: 420px; margin: 40px auto; text-align: center; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">
<div style="background: #fff; padding: 40px 28px; border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.06);">
<p style="font-size: 18px; color: #333; margin: 0;">Submission received.</p>
</div>
</div>
<?php
return ob_get_clean();
}