/
tootpress_validate.php
291 lines (238 loc) · 7.37 KB
/
tootpress_validate.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
<?php
/**
* Validate Funtions
*
* @package TootPress
* @since 0.1
*/
// Security: Stops code execution if WordPress is not loaded
if (!defined('ABSPATH')) { exit; }
/**
* Validates the Mastodon Instance
*
* @since 0.1
*
* @param string Input Field Mastodon Instance
* @return string Verified Mastodon Instance (true) or null (false)
*/
function tootpress_validate_mastodon_instance ($instance) {
if($instance<>''){
// Remove http/https & last Slash from input
$instance=str_replace(array('http://','https://'), '', $instance);
$instance=rtrim($instance,'/');
// Request given instance
$status_code=tootpress_mastodon_apirequest_instance_verify ($instance);
// If Request failed
if($status_code<>200) {
add_settings_error( 'tootpress-options', 'invalid-instance', 'Instance: No Mastodon Instance under the given name' );
$instance='';
}
}
return $instance;
}
/**
* Validates the OAUTH Access Token
*
* @since 0.1
*
* @param string Input Field OAUTH Access Token
* @return string Verified Access Token (true) or null (false)
*/
function tootpress_validate_mastodon_oauth_access_token ($accesstoken) {
if($accesstoken<>'') {
// Is Mastodon Instance maintained?
if(get_option('tootpress_mastodon_instance')) {
// Request API & Verify Access Token
$status_code=tootpress_mastodon_apirequest_authcode_verify ($accesstoken);
// If Request has failed
if($status_code<>200) {
add_settings_error( 'tootpress-options', 'invalid-accesstoken', 'OAUTH Access Token: Token is not valid' );
$accesstoken='';
}
} else {
// If no Mastodon Instance is maintained,
// Auth Code will be removed to prevent inconsistant values in TootPress
add_settings_error( 'tootpress-options', 'invalid-accesstoken', 'OAUTH Access Token: Token is linked to Mastodon Instance. Value removed.' );
$accesstoken='';
}
}
return $accesstoken;
}
/**
* Validates the Mastodon Account ID
*
* @since 0.1
*
* @param int Input Field Mastodon Account ID
* @return int Verified Mastodon Account ID (true) or null (false)
*/
function tootpress_validate_mastodon_account_id ($accountid) {
if($accountid<>'') {
if(tootpress_ready_to_authenticate_with_mastodon_api()) {
// Get Account ID from Mastodon API with Verify Credentials
$response=tootpress_mastodon_apirequest_account_verify_credentials();
$response_accountid=$response['id'];
// Validate Account ID
if(!($accountid==$response_accountid)) {
add_settings_error( 'tootpress-options', 'invalid-accountid', 'Account ID: Account ID is not in scope of the OAUTH Access Token' );
$accountid='';
} else {
// Retrieve Account Name
tootpress_retrieve_mastodon_account();
}
} else {
// If no AuthCode is maintained,
// UserID will be removed to prevent inconsistant values
add_settings_error( 'tootpress-options', 'invalid-accountid', 'Account ID: Account ID is linked to OAUTH Access Token. Value removed.' );
$accountid='';
}
}
// Remove Account Name (if verified Account ID is missing)
if($accountid=='') {
update_option('tootpress_mastodon_account_name',"");
}
return $accountid;
}
/**
* Validates the Page ID
*
* @since 0.1
*
* @param int Input Field Page ID
* @return int Verified Page ID
*/
function tootpress_validate_page_id ($pageid) {
if($pageid<>'') {
// Does ID exists?
if ( FALSE === get_post_type( $pageid ) ) {
add_settings_error( 'tootpress-options', 'invalid-pageid', 'Page ID: ID does not exists' );
$pageid='';
}
// Is ID a Page?
elseif (!('page' == get_post_type($pageid))) {
add_settings_error( 'tootpress-options', 'invalid-pageid', 'Page ID: ID is not a page' );
$pageid='';
}
// If new ID is given
elseif ($pageid<>get_option( 'tootpress_page_id')) {
update_option('tootpress_rewrite_update','1');
}
}
return $pageid;
}
/**
* Validates the Amount of Toos on Page
*
* @since 0.1
*
* @param int Input Field Amount of Toots on Page
* @return int Acceptable Amount
*/
function tootpress_validate_amount_toots_page ($amount) {
// Input required
if ($amount == '') {
add_settings_error( 'tootpress-options', 'invalid-amount', 'Amount Toots Page: Input is required' );
$amount=get_option( 'tootpress_amount_toots_page' );
}
// Invalid Input
elseif (!is_numeric($amount) OR $amount < 0 OR $amount==0 OR is_float($amount)) {
add_settings_error( 'tootpress-options', 'invalid-amount', 'Amount Toots Page: Invalid input' );
$amount=get_option( 'tootpress_amount_toots_page' );
}
// Allowed Range
elseif ($amount < 10 OR $amount > 500) {
add_settings_error( 'tootpress-options', 'invalid-amount', 'Amount Toots Page:
Allowed range is between 10 and 500' );
$amount=get_option( 'tootpress_amount_toots_page' );
}
else {
// Make Inputs like "15.0" to "15"
$amount=intval($amount);
}
return $amount;
}
/**
* Validates the Cron Period
*
* @since 0.1
*
* @param string Input Field Cron Period
* @return int Reliable Period
*/
function tootpress_validate_cron_period ($period) {
// Input required
if ($period == '') {
add_settings_error( 'tootpress-options', 'invalid-period', 'Period: Input is required' );
$period=get_option( 'tootpress_cron_period' );
}
// Invalid Input
elseif (!is_numeric($period) OR $period < 0 OR $period == 0 OR is_float($period)) {
add_settings_error( 'tootpress-options', 'invalid-period', 'Period: Invalid input' );
$period=get_option( 'tootpress_cron_period' );
}
// Allowed Range
elseif ($period < 5) {
add_settings_error( 'tootpress-options', 'invalid-period', 'Period:
Smallest permitted value is 5' );
$period=get_option( 'tootpress_cron_period' );
}
else {
// Convert to seconds
$period=$period*60;
}
return $period;
}
/**
* Validates the navigation
*
* @since 0.1
*
* @param string Input Field Navigation
* @return int Reliable Navigation
*/
function tootpress_validate_navigation ($nav) {
return $nav;
}
/**
* Validates the CSS Option
*
* Checkbox Label: Deactivate CSS
* What is the meaning of the values?
* 1 = No (Plugin CSS is used)
* 0 = Yes (Plugin CSS is not used)
*
* @since 0.1
*
* @param string Input Field CSS Option
* @return int Reliable CSS Option
*/
function tootpress_validate_css($input) {
if ($input==0) {
$output=1;
} else {
$output=0;
}
return $output;
}
/**
* Validates the Backlink Option
*
* Checkbox Label: Activate Backlink
* What is the meaning of the values?
* 1 = Yes
* 0 = No
*
* @since 0.3
*
* @param string Input Field Backlink Option
* @return int Reliable Backlink Option
*/
function tootpress_validate_backlink($input) {
if ($input==0) {
$output=0;
} else {
$output=1;
}
return $output;
}
?>