-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentibyte.php
190 lines (167 loc) · 6.05 KB
/
identibyte.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
<?php
/*
* Plugin Name: Identibyte for Contact Form 7
* Plugin URI: https://identibyte.com/wordpress-plugin
* Description: Detect and block disposable email addresses in your Contact 7 Forms.
* Author: Identibyte
* Author URI: https://identibyte.com
* Version: 1.0.0
*/
/* Ensure Contact Form 7 is installed and activated. */
add_action('admin_init', 'cf7_identibyte_validate_install');
function cf7_identibyte_validate_install() {
if (is_admin() &&
current_user_can('activate_plugins') &&
!is_plugin_active('contact-form-7/wp-contact-form-7.php')) {
add_action('admin_notices', 'cf7_identibyte_validate_install_no_cf7');
deactivate_plugins(plugin_basename(__FILE__));
if (isset($_GET['active'])) {
unset($_GET['active']);
}
}
}
function cf7_identibyte_validate_install_no_cf7() {
?>
<div class="error">
<p>
Contact form 7 must be installed and active for Identibyte to work.
<a href="<?php admin_url('plugin-install.php?tab=search&s=contact+form+7')?>">
Install here
</a>
</p>
</div>
<?php
}
/* Validate cf7 email/email* fields with Identibyte */
add_filter('wpcf7_validate_email*', 'cf7_identibyte_validate_email_filter', 20, 2);
add_filter('wpcf7_validate_email', 'cf7_identibyte_validate_email_filter', 20, 2);
/**
* Validation for email, email*, url, and url* tags. A check to
* Identibyte is made if the tag has the option: `identibyte:true`. If
* the email or URL is disposable, the form validation fails.
*/
function cf7_identibyte_validate_email_filter($result, $tag) {
$should_validate = in_array("identibyte:true", $tag->options);
if($should_validate) {
$value_ = $_POST[$tag->name];
$value = trim($value_);
$check = cf7_identibyte_make_check($value);
$disposable = $check->email->disposable;
if($disposable === true) {
$result->invalidate($tag, "Please check your email address and try again.");
}
}
return $result;
}
/**
* Make a check to the Identibyte API. Return the full response.
*/
function cf7_identibyte_make_check($data) {
$ua = cf7_identibyte_user_agent();
$token = get_option("cf7_identibyte_token", "");
$api_token = !empty($token) ? "?api_token={$token}" : "";
$options = array("http" => array(
"method" => "GET",
"header" => "User-Agent: " . $ua
));
$context = stream_context_create($options);
$url = esc_url("https://identibyte.com/check/" . $data . $api_token);
$request = file_get_contents($url, false, $context);
$response = json_decode($request);
return $response;
}
function cf7_identibyte_user_agent() {
$plugin_version = "1.0.0";
$blog_name = get_bloginfo('name');
$blog_url = get_bloginfo('url');
$user_agent = "cf7-identibyte/{$plugin_version} ({$blog_name} {$blog_url})";
return $user_agent;
}
/* Register admin page and settings */
if (is_admin()) {
add_action('admin_init', 'cf7_identibyte_register_admin_settings');
add_action('admin_menu', 'cf7_identibyte_add_to_admin_menu');
}
function cf7_identibyte_register_admin_settings() {
register_setting("cf7_identibyte_settings", "cf7_identibyte_token");
}
function cf7_identibyte_add_to_admin_menu() {
add_options_page(
'Identibyte Settings',
'Identibyte',
'manage_options',
'identibyte.php',
'cf7_identibyte_render_admin_page'
);
}
function cf7_identibyte_render_admin_page() {
$identibyte_logo = plugin_dir_url(__FILE__) . "img/identibyte-logo-120x120.png"
?>
<div class="wrap">
<div class="message"></div>
<br/>
<h1>
<img
src="<?php echo $identibyte_logo; ?>"
width="35"
title="Identibyte logo"
alt="Identibyte logo"
style="vertical-align:middle;margin-right:10px"
/>
Identibyte for Contact Form 7 · Admin settings
</h1>
<br/>
<div style="padding-left:50px">
<a href="https://identibyte.com" target="_blank">Website</a>
·
<a href="https://identibyte.com/#docs" target="_blank">Developer API</a>
·
<a href="https://identibyte.com/#plans" target="_blank">Plans</a>
</div>
<hr/>
<div>
<form method="post" action="options.php">
<?php settings_fields( 'cf7_identibyte_settings'); ?>
<h2>Account credentials</h2>
<p>
You can use Identibyte for free without an API
token. Entering an API token <br/> allows you to
use Identibyte more often. You can get an API
token in your
<a href="https://identibyte.com/dashboard" target="_blank">
your <br/> dashboard
</a>.
Learn more about what free and paid options are
available
<a href="https://identibyte.com/signup" target="_blank">
here
</a>.
</p>
<div>
<label for="cf7_identibyte_token">
<strong>Your User-Agent: </strong>
</label>
<br/>
<p>
<code>
<?php echo cf7_identibyte_user_agent(); ?>
</code>
</p>
</div>
<div>
<label for="cf7_identibyte_token">
<strong>API token (optional): </strong>
</label>
<br/>
<input
value="<?php echo get_option("cf7_identibyte_token") ?>"
type="text"
name="cf7_identibyte_token"
/>
</div>
<?php submit_button(); ?>
</form>
</div>
</div>
<?php
}