Search
Search
Search
Search
Information
Information
Light
Dark
Open actions menu
Basic upload method
Bypass upload method
Tips!
If you encounter an error (by firewall) while uploading using both methods,
try changing extension of the file before uploading it and rename it right after.
This uploader supports multiple file upload.
Submit
~
var
www
nea-2020.wpress.dk
httpdocs
wp-content
plugins
roar
File Content:
AJAXHandler.php
<?php /** * Utility class to simplify tranditional AJAX requests (i.e. admin-ajax.php). * Accepts JSON-encoded request data and sends back a JSON-encoded response. * * @package Roar * @author LION Interactive <https://lioninteractive.com> * @license https://opensource.org/licenses/MIT MIT License */ namespace Roar; use const Roar\PLUGIN_PREFIX; /** * Helper class that simplifies interactions with old WP_AJAX route handlers */ class AJAXHandler { /** * The AJAX action to trigger * @var String */ protected $action = ''; /** * The callback function to execute when the action is triggered * @var Callable */ protected $callback = null; /** * An array of expected arguments * @var Array */ protected $args = array(); /** * Whether the action is available to unauthenticated users * @var Boolean */ protected $no_priv = false; /** * Instantiates a new AJAX Handler * @param String $action The name of the request action. This should * match the action field defined in the body of * the request with the prefix 'roar__' * prepended, e.g. 'roar__my_action' * @param Function $callback The callback function to execute when the * action is triggered. * @param Array $args Key => value pairs of argument names and * definitions that are passed with the request * @param Boolean $no_priv If true, the action will be available to * unauthenticated users */ public function __construct( $action, $callback, $args = array(), $no_priv = false ) { $this->action = $action; $this->callback = $callback; $this->args = $args; $this->no_priv = $no_priv; add_action('wp_ajax_' . PLUGIN_PREFIX . $action, array( $this, 'parse_request' )); if ($no_priv) { add_action('wp_ajax_no_priv' . PLUGIN_PREFIX . $action, array( $this, 'parse_request' )); } } /** * Parses the incoming request and calls the user-defined callback function. * Sends the result of the callback function (success or error) * @return void */ public function parse_request() { $method = filter_input( INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING ); // Use query-string params for GET requests if ('GET' === $method) { $request = $_GET; // JSON payload for all other request methods } else { $request = json_decode(file_get_contents('php://input'), true); } $args = $this->_parse_args($request); $response = call_user_func_array($this->callback, array($args)); if (is_wp_error($response)) { $code = $response->get_error_code(); $msg = $response->get_error_message($code); $data = $response->get_error_data($code); $status = isset($data['status']) ? $data['status'] : 500; $this->send_error($code, $msg, $status, $data); } else { $this->send_response($response); } } /** * Sends a success (status code 200) JSON-encoded response. NOTE: Calling this * function halts further execution. * @param Mixed $res The response data to send * @return void */ public function send_response($res) { http_response_code(200); header('Content-type: application/json'); die(json_encode($res)); } /** * Sends an error JSON-encoded response. NOTE: Calling this function halts * further execution. * @param String $code A code representing the error that occurred * @param String $msg A descriptive message about what error occurred * @param Integer $status The status code to set on the response * @param Array $data An array of additional data to include with the * response * @return void */ public function send_error($code, $msg, $status = 500, $data = array()) { http_response_code($status); header('Content-type: application/json'); die( json_encode(array( 'code' => $code, 'message' => $msg, 'data' => array_merge(array('status' => $status), $data) )) ); } /** * Parses the arguments array and returns a clean version of the arg values. * Automatically returns an error response if the arg validation fails. * @param Array $req An array of the JSON-decoded request body * @return Array An array of the parsed and validated args */ private function _parse_args($req) { $parsed = array(); foreach ($this->args as $name => $arg) { // Parse the arg settings vs. the default values $arg = wp_parse_args($arg, array( 'required' => false, 'type' => '', 'default' => null, 'validate' => null, 'sanitize' => null )); // Check for required field if ($arg['required'] && !isset($req[$name])) { $msg = "The arg `{$name}` is required."; $this->send_error('required_arg', $msg, 400); } // Check for type match if ($arg['type']) { $valid = true; switch ($arg['type']) { case 'integer': $valid = is_numeric($req[$name]) && intval($req[$name]) == floatval($req[$name]); break; case 'float': $valid = is_numeric($req[$name]); break; case 'boolean': $bool = array('true', 'false', '1', '0', 'yes', 'no'); $valid = is_bool($req[$name]) || in_array(strtolower($req[$name]), $bool, true); break; default: $valid = $arg['type'] === gettype($req[$name]); } if (!$valid) { $msg = "The arg `{$name}` must be of type {$arg['type']}."; $this->send_error('invalid_type', $msg, 400); } } // Check for custom validation function if (isset($arg['validate']) && is_callable($arg['validate'])) { $valid = call_user_func_array($arg['validate'], array( $req[$name], $req, $arg )); if (true !== $valid) { // If the validation function returns an WP_Error, use it if (is_wp_error($valid)) { $code = $valid->get_error_code(); $msg = $valid->get_error_message($code); $data = $valid->get_error_data($code); $status = isset($data['status']) ? $data['status'] : 400; $this->send_error($code, $msg, $status, $data); // Otherwise, return a generic error } else { $msg = "The arg `{$name}` is invalid."; $this->send_error('validation_error', $msg, 400); } } } // If the arg has been set if (isset($req[$name])) { // Sanitize the arg if a custom sanitization function has been provided if (isset($arg['sanitize']) && is_callable($arg['sanitize'])) { $parsed[$name] = call_user_func_array( $arg['sanitize'], array($req[$name], $req, $arg) ); // Otherwise, use the raw value } else { $parsed[$name] = $req[$name]; } // The arg isn't set, but there's a default value } elseif (isset($arg['default'])) { $parsed[$name] = $arg['default']; // Arg isn't set and no default value } else { $parsed[$name] = null; } } return $parsed; } }
Edit
Download
Unzip
Chmod
Delete