| 1 |
<?php |
| 2 |
/** |
| 3 |
* External API |
| 4 |
* |
| 5 |
* @package NewStatpress |
| 6 |
*/ |
| 7 |
|
| 8 |
// Make sure plugin remains secure if called directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
if ( ! headers_sent() ) { |
| 11 |
header( 'HTTP/1.1 403 Forbidden' ); |
| 12 |
} |
| 13 |
die( esc_html( __( 'ERROR: This plugin requires WordPress and will not function if called directly.', 'newstatpress' ) ) ); |
| 14 |
} |
| 15 |
|
| 16 |
require 'nsp-api-version.php'; |
| 17 |
require 'nsp-api-wpversion.php'; |
| 18 |
require 'nsp-api-dashboard.php'; |
| 19 |
require 'nsp-api-overview.php'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Body function of external API Nonce |
| 23 |
*/ |
| 24 |
function newstatpress_external_api_ajax_n() { |
| 25 |
// check to see if the submitted nonce matches with the |
| 26 |
// generated nonce we created earlier. |
| 27 |
if ( ! ( isset( $_POST['postCommentNonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['postCommentNonce'] ) ), 'newstatpress-nsp_external-nonce' ) ) ) { |
| 28 |
die( 'Busted!' ); |
| 29 |
} |
| 30 |
|
| 31 |
newstatpress_external_api_ajax(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Body function of external API |
| 36 |
*/ |
| 37 |
function newstatpress_external_api_ajax() { |
| 38 |
global $_newstatpress; |
| 39 |
global $wpdb; |
| 40 |
|
| 41 |
header( 'HTTP/1.0 200 Ok' ); |
| 42 |
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' !== $_SERVER['REQUEST_METHOD'] ) { |
| 43 |
header( 'HTTP/1.0 403 Forbidden' ); |
| 44 |
die( 'Invalid use of API' ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( get_option( 'newstatpress_externalapi' ) !== 'checked' ) { |
| 48 |
header( 'HTTP/1.0 403 Forbidden' ); |
| 49 |
die( 'API not activated' ); |
| 50 |
} |
| 51 |
|
| 52 |
// read key from WordPress option. |
| 53 |
$api_key = get_option( 'newstatpress_apikey' ); |
| 54 |
$api_key = md5( gmdate( 'm-d-y H i' ) . $api_key ); |
| 55 |
|
| 56 |
// get the parameter from URL. |
| 57 |
|
| 58 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- external API, access controlled via API key, not nonce. |
| 59 |
if ( isset( $_REQUEST['VAR'] ) ) { |
| 60 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 61 |
$var = substr( preg_replace( '/[^a-z]+/', '', sanitize_text_field( wp_unslash( $_REQUEST['VAR'] ) ) ), 0, 9 ); |
| 62 |
} |
| 63 |
|
| 64 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- external API, access controlled via API key, not nonce. |
| 65 |
if ( isset( $_REQUEST['KEY'] ) ) { |
| 66 |
// key read is md5(date('m-d-y H i').'Key'). |
| 67 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 68 |
$key = preg_replace( '/[^a-z0-9]+/', '', sanitize_text_field( wp_unslash( $_REQUEST['KEY'] ) ) ); |
| 69 |
} |
| 70 |
|
| 71 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- external API, access controlled via API key, not nonce. |
| 72 |
if ( isset( $_REQUEST['PAR'] ) ) { |
| 73 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 74 |
$par = intval( $_REQUEST['PAR'] ); // can be empty. |
| 75 |
} |
| 76 |
|
| 77 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- external API, access controlled via API key, not nonce. |
| 78 |
if ( isset( $_REQUEST['TYP'] ) ) { |
| 79 |
// can be empty. |
| 80 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 81 |
$typ = substr( preg_replace( '/[^A-Z]+/', '', sanitize_text_field( wp_unslash( $_REQUEST['TYP'] ) ) ), 0, 4 ); |
| 82 |
} |
| 83 |
|
| 84 |
if ( null === $typ ) { |
| 85 |
$typ = 'JSON'; |
| 86 |
} |
| 87 |
|
| 88 |
if ( 'JSON' !== $typ && 'HTML' !== $typ ) { |
| 89 |
header( 'HTTP/1.0 403 Forbidden' ); |
| 90 |
die( 'Return type not available' ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! preg_match( '/^[a-f0-9]{32}$/', $key ) ) { |
| 94 |
header( 'HTTP/1.0 403 Forbidden' ); |
| 95 |
die( 'Invalid key' ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( null === $var && null === $key ) { |
| 99 |
header( 'HTTP/1.0 403 Forbidden' ); |
| 100 |
die( 'API needs parameters' ); |
| 101 |
} |
| 102 |
|
| 103 |
// test if can use API. |
| 104 |
if ( $key !== $api_key ) { |
| 105 |
header( 'HTTP/1.0 403 Forbidden' ); |
| 106 |
die( 'Not authorized API access' ); |
| 107 |
} |
| 108 |
|
| 109 |
switch ( $var ) { |
| 110 |
case 'version': |
| 111 |
$result = newstatpress_api_version( $typ ); |
| 112 |
break; |
| 113 |
case 'wpversion': |
| 114 |
$result = newstatpress_api_wp_version( $typ ); |
| 115 |
break; |
| 116 |
case 'dashboard': |
| 117 |
$result = newstatpress_api_dashboard( $typ ); |
| 118 |
break; |
| 119 |
case 'overview': |
| 120 |
$result = newstatpress_api_overview( $typ, $par ); |
| 121 |
break; |
| 122 |
default: |
| 123 |
header( 'HTTP/1.0 403 Forbidden' ); |
| 124 |
die( 'Not recognized API.' ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( 'JSON' === $typ ) { |
| 128 |
// response output. |
| 129 |
header( 'Content-Type: application/json' ); |
| 130 |
// gives the complete output according to $resultJ. |
| 131 |
echo wp_json_encode( |
| 132 |
$result |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
if ( 'HTML' === $typ ) { |
| 137 |
// response output. |
| 138 |
header( 'Content-Type: application/html' ); |
| 139 |
// gives the complete output according to $resultH. |
| 140 |
echo wp_kses( |
| 141 |
$result, |
| 142 |
array( |
| 143 |
'table' => array( |
| 144 |
'class' => array(), |
| 145 |
), |
| 146 |
'tbody' => array( |
| 147 |
'class' => array(), |
| 148 |
), |
| 149 |
'tr' => array( |
| 150 |
'class' => array(), |
| 151 |
), |
| 152 |
'div' => array( |
| 153 |
'class' => array(), |
| 154 |
'style' => array(), |
| 155 |
'title' => array(), |
| 156 |
), |
| 157 |
'td' => array( |
| 158 |
'class' => array(), |
| 159 |
'width' => array(), |
| 160 |
'valign' => array(), |
| 161 |
), |
| 162 |
'th' => array( |
| 163 |
'scope' => array(), |
| 164 |
'colspan' => array(), |
| 165 |
), |
| 166 |
'thead' => array(), |
| 167 |
'span' => array(), |
| 168 |
'br' => array(), |
| 169 |
'p' => array(), |
| 170 |
'i' => array(), |
| 171 |
) |
| 172 |
); |
| 173 |
} |
| 174 |
wp_die(); |
| 175 |
} |
| 176 |
|
| 177 |
|