-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
150 lines (126 loc) · 5.38 KB
/
Copy pathplugin.php
File metadata and controls
150 lines (126 loc) · 5.38 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
<?php
/*
Plugin Name: Force Allow Duplicate URLs
Plugin URI: https://github.com/kesslerio/yourls-force-allow-duplicates
Description: Truly allows creating multiple short URLs (with different keywords) for the same destination URL
Version: 1.0.0
Author: Martin Kessler
Author URI: https://github.com/kesslerio
*/
// Block direct calls
if (!defined('YOURLS_ABSPATH')) die();
// Hook into the URL exists filter when the force parameter is present
yourls_add_filter('url_exists', 'force_allow_duplicates', 10, 1);
// Hook into the 'add new URL' function to bypass unique URL checking
yourls_add_filter('shunt_add_new_link', 'force_add_duplicate_url', 10, 4);
// Also keep the original allow_existing_urls filter as a fallback
yourls_add_filter('add_new_link_already_stored_filter', 'force_allow_existing_urls');
/**
* Bypass the unique URL check when 'force=1' is set in the request
*
* @param bool $url_exists Whether the URL exists in the database
* @return bool False if force=1 is set, original value otherwise
*/
function force_allow_duplicates($url_exists) {
// Check if we have the force parameter set to 1
if (isset($_REQUEST['force']) && $_REQUEST['force'] == '1') {
// Return false to indicate URL doesn't exist, allowing a new short URL to be created
return false;
}
// Return original value otherwise
return $url_exists;
}
/**
* Force add a new URL if the 'force' parameter is set
* This bypasses the normal add_new_link function when needed
*
* @param mixed $pre Default value to return
* @param string $url URL to shorten
* @param string $keyword Optional keyword
* @param string $title Optional title
* @return mixed False if force parameter is not present, or the result of the add operation
*/
function force_add_duplicate_url($pre, $url, $keyword, $title) {
global $ydb;
// Only execute if force=1 is set and we're dealing with a duplicate URL
if (isset($_REQUEST['force']) && $_REQUEST['force'] == '1' && yourls_url_exists($url)) {
// Start transaction
$ydb->begin_transaction();
try {
// Generate unique timestamp for log entry
$timestamp = date('Y-m-d H:i:s');
$ip = yourls_get_IP();
// Insert the new link directly into the database
$binds = array(
'keyword' => $keyword,
'url' => $url,
'title' => $title,
'timestamp' => $timestamp,
'ip' => $ip,
);
$table = YOURLS_DB_TABLE_URL;
// Prepare and execute the SQL query
$insert = $ydb->fetchAffected(
"INSERT INTO `$table` (`keyword`, `url`, `title`, `timestamp`, `ip`) VALUES (:keyword, :url, :title, :timestamp, :ip)",
$binds
);
if ($insert !== 1) {
$ydb->rollback();
return false;
}
// Success, commit and return the result
$ydb->commit();
// Prepare result
$return = array(
'status' => 'success',
'code' => '',
'url' => array(
'keyword' => $keyword,
'url' => $url,
'title' => $title,
'date' => $timestamp,
'ip' => $ip,
'clicks' => 0,
),
'message' => 'Duplicate URL forced - new short URL created',
'title' => $title,
'shorturl' => yourls_link($keyword),
);
// Return the custom result instead of letting YOURLS handle it
return yourls_apply_filter('after_force_allow_duplicates', $return, $url, $keyword, $title);
} catch (Exception $e) {
// Something went wrong, rollback
$ydb->rollback();
// Log the error
error_log('YOURLS Force Allow Duplicates Error: ' . $e->getMessage());
// Return false to let YOURLS handle it normally
return false;
}
}
// Return default to let YOURLS handle it normally
return $pre;
}
/**
* Legacy handler from the original plugin, with enhancements
*/
function force_allow_existing_urls($return, $url, $keyword, $title) {
// If the force parameter is set, use the more advanced function above
if (isset($_REQUEST['force']) && $_REQUEST['force'] == '1') {
// The force_add_duplicate_url function will handle this
return $return;
}
// Otherwise, for backward compatibility, modify the error to success
if (isset($return['code'])) {
if ($return['code'] === 'error:url') {
if ($url_exists = yourls_url_exists($url)) {
$return['status'] = 'success';
$return['code'] = '';
$return['errorCode'] = '200';
$return['statusCode'] = '200';
// Add a note to indicate this is using fallback behavior
$return['note'] = 'URL already exists. Use force=1 parameter to create a new short URL.';
}
}
}
return yourls_apply_filter('after_force_allow_existing_urls', $return, $url, $keyword, $title);
}