forked from YOURLS/plugin-sample
-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugin.php
107 lines (98 loc) · 3.99 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
<?php
/*
Plugin Name: Admin reCaptcha
Plugin URI: https://github.com/armujahid/Admin-reCaptcha.git
Description: This plugin enable reCapcha on Admin login screen
Version: 1.2
Author: Abdul Rauf
Author URI: http://armujahid.me/
*/
if( !defined( 'YOURLS_ABSPATH' ) ) die();
yourls_add_action( 'pre_login_username_password', 'abdulrauf_adminreCaptcha_validatereCaptcha' );
// Validates reCaptcha
function abdulrauf_adminreCaptcha_validatereCaptcha()
{
include('captcha.php');
if ($resp != null && $resp->success)
{
//reCaptcha validated
return true;
}
else
{
yourls_do_action( 'login_failed' );
yourls_login_screen( $error_msg = 'reCaptcha validation failed' );
die();
return false;
}
}
// Register plugin on admin page
yourls_add_action( 'plugins_loaded', 'abdulrauf_adminreCaptcha_init' );
function abdulrauf_adminreCaptcha_init() {
yourls_register_plugin_page( 'adminreCaptcha', 'Admin reCaptcha Settings', 'adminreCaptcha_config_page' );
}
// The function that will draw the config page
function adminreCaptcha_config_page() {
if( isset( $_POST['abdulrauf_adminreCaptcha_public_key'] ) ) {
yourls_verify_nonce( 'abdulrauf_adminreCaptcha_nonce' );
abdulrauf_adminreCaptcha_save_admin();
}
$nonce = yourls_create_nonce( 'abdulrauf_adminreCaptcha_nonce' );
$pubkey = yourls_get_option( 'abdulrauf_adminreCaptcha_pub_key', "" );
$privkey = yourls_get_option( 'abdulrauf_adminreCaptcha_priv_key', "" );
echo '<h2>Admin reCaptcha plugin settings</h2>';
echo '<form method="post">';
echo '<input type="hidden" name="nonce" value="' . $nonce . '" />';
echo '<p><label for="abdulrauf_adminreCaptcha_public_key">reCaptcha site key: </label>';
echo '<input type="text" id="abdulrauf_adminreCaptcha_public_key" name="abdulrauf_adminreCaptcha_public_key" value="' . $pubkey . '"></p>';
echo '<p><label for="abdulrauf_adminreCaptcha_private_key">reCaptcha secret key: </label>';
echo '<input type="text" id="abdulrauf_adminreCaptcha_private_key" name="abdulrauf_adminreCaptcha_private_key" value="' . $privkey . '"></p>';
echo '<input type="submit" value="Save"/>';
echo '</form>';
}
// Save reCaptcha keys in database
function abdulrauf_adminreCaptcha_save_admin()
{
$pubkey = $_POST['abdulrauf_adminreCaptcha_public_key'];
$privkey = $_POST['abdulrauf_adminreCaptcha_private_key'];
if ( yourls_get_option( 'abdulrauf_adminreCaptcha_pub_key' ) !== false ) {
yourls_update_option( 'abdulrauf_adminreCaptcha_pub_key', $pubkey );
}
else {
yourls_add_option( 'abdulrauf_adminreCaptcha_pub_key', $pubkey );
}
if ( yourls_get_option( 'abdulrauf_adminreCaptcha_priv_key' ) !== false ) {
yourls_update_option( 'abdulrauf_adminreCaptcha_priv_key', $privkey );
}
else {
yourls_add_option( 'abdulrauf_adminreCaptcha_priv_key', $privkey );
}
echo "Saved";
}
// Add the JavaScript for reCaptcha widget
yourls_add_action( 'html_head', 'abdulrauf_adminreCaptcha_addjs' );
function abdulrauf_adminreCaptcha_addjs() {
$siteKey = yourls_get_option( 'abdulrauf_adminreCaptcha_pub_key' );
?>
<script type="text/javascript">
//JQuery function to add div for reCaptcha widget and load js only on login screen
$(document).ready(function() {
var logindiv = document.getElementById('login');
if (logindiv != null) { //check if we are on login screen
//getting reCaptcha script by jquery only on login screen
$.getScript( "https://recaptcha.net/recaptcha/api.js?onload=loadCaptcha&render=explicit");
var form = logindiv.innerHTML;
var index = form.indexOf('<p style="text-align: right;">'); //finding tag before which reCaptcha widget should appear
document.getElementById('login').innerHTML = form.slice(0, index) + '<div id="captcha_container"></div>' + form.slice(index);
}
});
// JavaScript function to explicitly render the reCAPTCHA widget
var loadCaptcha = function() {
captchaContainer = grecaptcha.render('captcha_container', {
'sitekey' : '<?php echo $siteKey?>'
});
};
</script>
<?php
}
?>