This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-haveibeenpwned-api.php
155 lines (135 loc) · 3.42 KB
/
wp-haveibeenpwned-api.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
<?php
/**
* WP Have I Been Pwned API
*
* @package WP-Have I Been Pwned-API
*/
/*
* Plugin Name: WP Have I Been Pwned API
* Plugin URI: https://github.com/wp-api-libraries/wp-haveibeenpwned-api
* Description: Perform API requests to Have I Been Pwned in WordPress.
* Author: WP API Libraries
* Version: 1.0.0
* Author URI: https://wp-api-libraries.com
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-haveibeenpwned-api
* GitHub Branch: master
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) { exit; }
if ( ! class_exists( 'HaveIBeenPwnedAPI' ) ) {
/**
* HaveIBeenPwnedAPIclass.
*/
class HaveIBeenPwnedAPI {
/**
* URL to the API.
*
* @var string
*/
private $base_uri = 'https://haveibeenpwned.com/api/v2/';
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
}
/**
* Fetch the request from the API.
*
* @access private
* @param mixed $request Request URL.
* @return $body Body.
*/
private function fetch( $request ) {
$request .= '?api_token=' .static::$api_token;
$response = wp_remote_get( $request );
$code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $code ) {
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'text-domain' ), $code ) );
}
$body = wp_remote_retrieve_body( $response );
return json_decode( $body );
}
/**
* get_all_breaches function.
*
* @access public
* @return void
*/
public function get_all_breaches() {
$request = $this->base_uri . 'breaches';
return $this->fetch( $request );
}
/**
* Get Account Breaches.
*
* @access public
* @param mixed $account
* @return void
*/
public function get_acct_breaches( $account ) {
$request = $this->base_uri . 'breachedaccount/' . $account;
return $this->fetch( $request );
}
/**
* get_acct_pastes function.
*
* @access public
* @param mixed $account
* @return void
*/
public function get_acct_pastes( $account ) {
$request = $this->base_uri . 'pasteaccount/' . $account;
return $this->fetch( $request );
}
/**
* get_breach function.
*
* @access public
* @param mixed $breach_name
* @return void
*/
public function get_breach( $breach_name ) {
$request = $this->base_uri . 'breach/' . $breach_name;
return $this->fetch( $request );
}
/**
* get_data_classes function.
*
* @access public
* @return void
*/
public function get_data_classes() {
$request = $this->base_uri . 'dataclasses';
return $this->fetch( $request );
}
/**
* Response code message.
*
* @param [String] $code : Response code to get message from.
* @return [String] : Message corresponding to response code sent in.
*/
public function response_code_msg( $code = '' ) {
switch ( $code ) {
case 200:
$msg = __( 'OK.', $this->textdomain );
break;
case 400:
$msg = __( 'Bad Request.', $this->textdomain );
break;
case 403:
$msg = __( 'Forbidden — no user agent has been specified in the request.', $this->textdomain );
break;
case 404:
$msg = __( 'Not found — the account could not be found and has therefore not been pwned.', $this->textdomain );
break;
case 429:
$msg = __( 'Too many requests — the rate limit has been exceeded.', $this->textdomain );
break;
}
return $msg;
}
}
}