Esempio n. 1
0
            goto run_plugin;
        }
    } else {
        _wc_jsonapi_log("Found HTML in the contents, so going to run_plugin");
        goto run_plugin;
    }
    run_plugin:
    _wc_jsonapi_log("Entering run plugin");
    @ob_clean();
    if (!defined('REDE_PLUGIN_BASE_PATH')) {
        define('REDE_PLUGIN_BASE_PATH', plugin_dir_path(__FILE__));
    }
    if (!defined('REDENOTSET')) {
        define('REDENOTSET', '__RED_E_NOTSET__');
        // because sometimes false, 0 etc are
        // expected but consistently dealing with these situations is tiresome.
    }
    _wc_jsonapi_log("Requiring Files");
    require_once plugin_dir_path(__FILE__) . 'classes/class-rede-helpers.php';
    require_once plugin_dir_path(__FILE__) . 'woocommerce-json-api-core.php';
    _wc_jsonapi_log("Calling template redirect");
    woocommerce_json_api_template_redirect();
    output_the_buffer:
    $contents = ob_get_contents();
    ob_end_clean();
    _wc_jsonapi_log("Sending header");
    header("Content-Type: application/json");
    die($contents);
} else {
    die(0);
}
function woocommerce_json_api_template_redirect()
{
    global $wpdb;
    global $logout_redirect;
    $helpers = new JSONAPIHelpers();
    $headers = woocommerce_json_api_parse_headers();
    if (function_exists('_wc_jsonapi_log')) {
        _wc_jsonapi_log(__FILE__ . ' In template Redirect');
    }
    if (isset($headers['Content-Type']) && $headers['Content-Type'] == 'application/json' && !isset($_REQUEST['json'])) {
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Parsing Request Body');
        }
        $fp = @fopen('php://input', 'r');
        $body = '';
        if ($fp) {
            while (!feof($fp)) {
                $buf = fread($fp, 1024);
                if (is_string($buf)) {
                    $body .= $buf;
                }
            }
            fclose($fp);
        }
        $hash = json_decode($body, true);
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Parsed Hash Is: ' . print_r($hash, true));
        }
        foreach ($hash as $key => $value) {
            $_REQUEST[$key] = $value;
        }
    } elseif (isset($_REQUEST['json']) && !empty($_REQUEST['json'])) {
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Parsing json out of request ');
        }
        $hash = json_decode(stripslashes($_REQUEST['json']), true);
        foreach ($hash as $key => $value) {
            $_REQUEST[$key] = $value;
        }
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Now request is ');
        }
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log($_REQUEST);
        }
    }
    if (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'woocommerce_json_api') {
        //die("action not set");
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Action is not set');
        }
        return;
    }
    if (is_user_logged_in()) {
        //die("user is logged in");
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' User is logged in! ');
        }
        if (!defined('WCJSONAPI_USER_NO_CARE')) {
            return;
        }
    }
    JSONAPIHelpers::debug(var_export($headers, true));
    if (isset($_REQUEST['action']) && 'woocommerce_json_api' == $_REQUEST['action']) {
        $enabled = get_option($helpers->getPluginPrefix() . '_enabled');
        $require_https = get_option($helpers->getPluginPrefix() . '_require_https');
        if ($enabled != 'no') {
            if ($require_https == 'yes' && $helpers->isHTTPS() == false) {
                JSONAPIHelpers::debug("Cannot continue, HTTPS is required.");
                return;
            }
            if (defined('WC_JSON_API_DEBUG')) {
                JSONAPIHelpers::truncateDebug();
            }
            remove_filter('wp_logout', array($logout_redirect, 'redirect'));
            $api = new WooCommerce_JSON_API();
            $api->setOut('HTTP');
            $api->setUser(null);
            $params = array();
            // maybe we had to serialize some subarrays, so we'll have to unserialize them here
            foreach ($_REQUEST as $key => $value) {
                $params[$key] = $value;
            }
            foreach (array('payload', 'arguments', 'model_filters', 'wordpress_filters', 'image_sizes') as $key) {
                if (isset($_REQUEST[$key]) && is_string($_REQUEST[$key])) {
                    $params[$key] = json_decode(stripslashes($_REQUEST[$key]), true);
                }
            }
            $api->route($params);
        } else {
            JSONAPIHelpers::debug("JSON API is not set to enabled.");
        }
    } else {
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Action is still not set!');
        }
    }
}