-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
485 lines (424 loc) · 17 KB
/
plugin.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/**
* Plugin Name: Awin Affiliate
* Description: Add your Awin affiliate parameters to URLs from configured merchants
* Version: 2.3
* Author: Bjorn Lammers
* Author URI: https://github.com/lammersbjorn/YOURLS-Awin
* License: BSD 3-Clause
* License URI: https://opensource.org/licenses/BSD-3-Clause
*/
// Prevent direct access to this file
if (!defined('YOURLS_ABSPATH')) die();
class AwinAffiliatePlugin
{
private const AWIN_BASE_URL = 'https://www.awin1.com/cread.php';
private const CLICKREF_COUNT = 6;
private const OPTION_NAME = 'awin_affiliate_settings';
private $settings;
private $merchants;
public function __construct()
{
// Initialize the plugin
yourls_add_action('plugins_loaded', [$this, 'init']);
// Admin page hooks
yourls_add_action('plugins_loaded', [$this, 'addAdminPage']);
yourls_add_action('admin_page_awin_affiliate_settings', [$this, 'displayAdminPage']);
// Process form submissions
yourls_add_action('admin_init', [$this, 'processFormSubmission']);
// URL processing hook
yourls_add_action('pre_redirect', [$this, 'processUrl']);
}
/**
* Initialize plugin settings
*/
public function init(): void
{
// Load settings from YOURLS options
$this->settings = yourls_get_option(self::OPTION_NAME);
if ($this->settings === false) {
// Default settings
$this->settings = [
'awinaffid' => '',
'default_campaign' => '',
'default_clickref' => '',
'merchants' => []
];
// Add default merchants
$this->settings['merchants'] = [
'coolblue' => [
'name' => 'Coolblue',
'awinmid' => '',
'domains' => [
'coolblue.nl',
'coolblue.be',
'coolblue.de',
'coolblue.fr'
],
'enabled' => true
]
];
yourls_add_option(self::OPTION_NAME, $this->settings);
}
}
/**
* Add admin page to YOURLS menu
*/
public function addAdminPage(): void
{
yourls_register_plugin_page('awin_affiliate_settings', 'Awin Affiliate Settings', [$this, 'displayAdminPage']);
}
/**
* Display admin settings page
*/
public function displayAdminPage(): void
{
if (!yourls_is_admin()) die('Access denied');
$nonce = yourls_create_nonce('awin_affiliate_settings');
?>
<h2>Awin Affiliate Settings</h2>
<form method="post">
<input type="hidden" name="nonce" value="<?php echo $nonce; ?>">
<input type="hidden" name="action" value="update_awin_settings">
<h3>Global Settings</h3>
<div class="settings-group">
<p>
<label for="awinaffid">Awin Affiliate ID (required):</label><br>
<input type="text" id="awinaffid" name="awinaffid" value="<?php echo htmlspecialchars($this->settings['awinaffid']); ?>" class="text" required>
</p>
</div>
<h3>Merchants</h3>
<div class="merchants-container">
<?php foreach ($this->settings['merchants'] as $id => $merchant): ?>
<div class="merchant-settings">
<div class="merchant-header">
<h4><?php echo htmlspecialchars($merchant['name']); ?></h4>
<label class="merchant-toggle">
<input type="checkbox" name="merchant_enabled[<?php echo $id; ?>]" value="1"
<?php echo $merchant['enabled'] ? 'checked' : ''; ?>>
Enable
</label>
</div>
<div class="merchant-content">
<div class="merchant-row">
<div class="merchant-col">
<label for="merchant_awinmid_<?php echo $id; ?>">Awin Merchant ID:</label>
<input type="text" id="merchant_awinmid_<?php echo $id; ?>"
name="merchant_awinmid[<?php echo $id; ?>]"
value="<?php echo htmlspecialchars($merchant['awinmid']); ?>"
class="text">
</div>
<div class="merchant-col">
<label for="merchant_campaign_<?php echo $id; ?>">Default Campaign:</label>
<input type="text" id="merchant_campaign_<?php echo $id; ?>"
name="merchant_campaign[<?php echo $id; ?>]"
value="<?php echo htmlspecialchars($merchant['campaign'] ?? ''); ?>"
class="text">
</div>
<div class="merchant-col">
<label for="merchant_clickref_<?php echo $id; ?>">Default Clickref:</label>
<input type="text" id="merchant_clickref_<?php echo $id; ?>"
name="merchant_clickref[<?php echo $id; ?>]"
value="<?php echo htmlspecialchars($merchant['clickref'] ?? ''); ?>"
class="text">
</div>
</div>
<div class="merchant-row">
<div class="merchant-col full">
<label>Domains (one per line):</label>
<textarea name="merchant_domains[<?php echo $id; ?>]" rows="3" class="text"><?php
echo htmlspecialchars(implode("\n", $merchant['domains']));
?></textarea>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<h3>Add New Merchant</h3>
<div class="settings-group">
<div class="merchant-row">
<div class="merchant-col">
<label for="new_merchant_name">Merchant Name:</label>
<input type="text" id="new_merchant_name" name="new_merchant_name" class="text">
</div>
<div class="merchant-col">
<label for="new_merchant_awinmid">Awin Merchant ID:</label>
<input type="text" id="new_merchant_awinmid" name="new_merchant_awinmid" class="text">
</div>
</div>
<div class="merchant-row">
<div class="merchant-col">
<label for="new_merchant_campaign">Default Campaign:</label>
<input type="text" id="new_merchant_campaign" name="new_merchant_campaign" class="text">
</div>
<div class="merchant-col">
<label for="new_merchant_clickref">Default Clickref:</label>
<input type="text" id="new_merchant_clickref" name="new_merchant_clickref" class="text">
</div>
</div>
<div class="merchant-row">
<div class="merchant-col full">
<label for="new_merchant_domains">Domains (one per line):</label>
<textarea id="new_merchant_domains" name="new_merchant_domains" rows="3" class="text"></textarea>
</div>
</div>
</div>
<p><input type="submit" value="Save Settings" class="button primary"></p>
</form>
<style>
.settings-group {
margin: 20px 0;
padding: 15px;
border: 1px solid rgba(128, 128, 128, 0.2);
border-radius: 5px;
}
.merchants-container {
margin: 20px 0;
display: flex;
flex-direction: column;
gap: 15px;
}
.merchant-settings {
border: 1px solid rgba(128, 128, 128, 0.2);
border-radius: 5px;
}
.merchant-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px;
border-bottom: 1px solid rgba(128, 128, 128, 0.2);
}
.merchant-header h4 {
margin: 0;
}
.merchant-content {
padding: 15px;
}
.merchant-row {
display: flex;
gap: 15px;
margin-bottom: 15px;
}
.merchant-row:last-child {
margin-bottom: 0;
}
.merchant-col {
flex: 1;
}
.merchant-col.full {
flex: 0 0 100%;
}
.merchant-col label {
display: block;
margin-bottom: 5px;
}
input.text,
textarea.text {
width: 100%;
padding: 5px;
border: 1px solid rgba(128, 128, 128, 0.2);
border-radius: 3px;
background: transparent;
color: inherit;
}
.merchant-toggle {
display: flex;
align-items: center;
gap: 5px;
}
</style>
<?php
}
/**
* Process admin form submission
*/
public function processFormSubmission(): void
{
if (!isset($_POST['action']) || $_POST['action'] !== 'update_awin_settings') {
return;
}
// Verify nonce
yourls_verify_nonce('awin_affiliate_settings');
// Update global settings
$this->settings['awinaffid'] = $_POST['awinaffid'];
// Update existing merchants
foreach ($this->settings['merchants'] as $id => &$merchant) {
$merchant['enabled'] = isset($_POST['merchant_enabled'][$id]);
$merchant['awinmid'] = $_POST['merchant_awinmid'][$id];
$merchant['campaign'] = $_POST['merchant_campaign'][$id];
$merchant['clickref'] = $_POST['merchant_clickref'][$id];
$merchant['domains'] = array_filter(array_map('trim', explode("\n", $_POST['merchant_domains'][$id])));
}
// Add new merchant if provided
if (!empty($_POST['new_merchant_name']) && !empty($_POST['new_merchant_awinmid'])) {
$id = yourls_sanitize_title($_POST['new_merchant_name']);
$domains = array_filter(array_map('trim', explode("\n", $_POST['new_merchant_domains'])));
$this->settings['merchants'][$id] = [
'name' => $_POST['new_merchant_name'],
'awinmid' => $_POST['new_merchant_awinmid'],
'campaign' => $_POST['new_merchant_campaign'],
'clickref' => $_POST['new_merchant_clickref'],
'domains' => $domains,
'enabled' => true
];
}
// Save settings
yourls_update_option(self::OPTION_NAME, $this->settings);
yourls_add_notice('Settings updated successfully');
}
/**
* Process URL for redirection
* @param array $args Arguments from YOURLS
*/
public function processUrl($args): void
{
try {
$url = $args[0] ?? '';
if (empty($url)) return;
$merchant = $this->findMatchingMerchant($url);
if ($merchant && $merchant['enabled']) {
$this->handleRedirect($url, $merchant);
}
} catch (Exception $e) {
error_log('Awin Affiliate Plugin Error: ' . $e->getMessage());
}
}
/**
* Find matching merchant for URL
* @param string $url
* @return array|null
*/
private function findMatchingMerchant(string $url): ?array
{
$parsedUrl = parse_url($url);
if (!$parsedUrl || empty($parsedUrl['host'])) {
return null;
}
$host = strtolower(preg_replace('/^www\./', '', $parsedUrl['host']));
foreach ($this->settings['merchants'] as $merchant) {
if (in_array($host, $merchant['domains'])) {
return $merchant;
}
}
return null;
}
/**
* Handle the redirect process
*/
private function handleRedirect(string $url, array $merchant): void
{
$urlParts = parse_url($url);
$baseUrl = $this->buildBaseUrl($urlParts);
// Get existing parameters
$existingParams = [];
if (isset($urlParts['query'])) {
parse_str($urlParts['query'], $existingParams);
}
// Build Awin parameters
$params = [
'awinmid' => $merchant['awinmid'],
'awinaffid' => $this->settings['awinaffid']
];
// Add campaign if provided in URL or merchant settings
if (isset($existingParams['campaign'])) {
$params['campaign'] = $existingParams['campaign'];
} elseif (!empty($merchant['campaign'])) {
$params['campaign'] = $merchant['campaign'];
}
// Add clickrefs
if (isset($existingParams['clickref'])) {
$params['clickref'] = $existingParams['clickref'];
} elseif (!empty($merchant['clickref'])) {
$params['clickref'] = $merchant['clickref'];
}
// Add additional clickrefs if provided in URL
for ($i = 2; $i <= self::CLICKREF_COUNT; $i++) {
$key = "clickref{$i}";
if (isset($existingParams[$key])) {
$params[$key] = $existingParams[$key];
}
}
$this->outputRedirectPage($baseUrl, $params);
die();
}
/**
* Build the base URL without query parameters
* @param array $urlParts Parsed URL parts
* @return string
*/
private function buildBaseUrl(array $urlParts): string
{
$scheme = $urlParts['scheme'] ?? 'https';
$host = $urlParts['host'];
$path = $urlParts['path'] ?? '';
return sprintf('%s://%s%s', $scheme, $host, $path);
}
/**
* Output the redirect page
* @param string $url Original URL
* @param array $params Awin parameters
*/
private function outputRedirectPage(string $url, array $params): void
{
// Build query parameters
$queryParams = array_map(
fn($key, $value) => $key . '=' . urlencode($value),
array_keys($params),
array_values($params)
);
// Add encoded destination URL
$queryParams[] = 'ued=' . urlencode($url);
// Build final URL
$finalUrl = self::AWIN_BASE_URL . '?' . implode('&', $queryParams);
// Output redirect page
header('Content-Type: text/html; charset=utf-8');
header('X-Robots-Tag: noindex, nofollow');
echo $this->getRedirectTemplate($finalUrl);
}
/**
* Get the HTML template for redirect page
* @param string $finalUrl The final Awin URL
* @return string
*/
private function getRedirectTemplate(string $finalUrl): string
{
$escapedUrl = htmlspecialchars($finalUrl, ENT_QUOTES, 'UTF-8');
$jsonUrl = json_encode($finalUrl);
return <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="refresh" content="0; url={$escapedUrl}">
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
line-height: 1.5; padding: 2rem; text-align: center; }
.container { max-width: 600px; margin: 0 auto; }
.spinner { width: 40px; height: 40px; margin: 20px auto;
border: 3px solid #f3f3f3; border-top: 3px solid #3498db;
border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { 0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); } }
</style>
<script>
window.location.href = {$jsonUrl};
</script>
</head>
<body>
<div class="container">
<h1>Redirecting...</h1>
<div class="spinner"></div>
<p>If you are not redirected automatically, <a href="{$escapedUrl}">click here</a>.</p>
</div>
</body>
</html>
HTML;
}
}
// Initialize the plugin
new AwinAffiliatePlugin();
?>