Exemple #1
0
    include './include/ajax/examples.php';
    exit;
}
// ----------------------------------------------------------------------
// Dispatch Ajax calls
// ----------------------------------------------------------------------
$PHORUM['ajax_args'] = array();
// JSON data based request (stored in a POST or PUT request body)
if ($_SERVER['REQUEST_METHOD'] == 'POST' || $_SERVER['REQUEST_METHOD'] == 'PUT') {
    // Get the POST body.
    $body = trim(file_get_contents('php://input'));
    if ($body == '') {
        phorum_ajax_error('Ajax POST request misses body');
    }
    // Set the Ajax arguments.
    $PHORUM["ajax_args"] = phorum_api_json_decode($body);
    if ($PHORUM["ajax_args"] == NULL) {
        phorum_ajax_error('Ajax POST request body does not seem to be JSON data');
    }
} elseif (isset($_GET['call'])) {
    // Set the Ajax arguments.
    $PHORUM['ajax_args'] = $_GET;
} elseif (isset($PHORUM['args']['call'])) {
    // Set the Ajax arguments.
    $PHORUM['ajax_args'] = $PHORUM['args'];
}
// Check if we got a valid Ajax request. The request should contain at
// least a "call" field in the Ajax arguments.
if (empty($PHORUM['ajax_args']) || !isset($PHORUM['ajax_args']['call'])) {
    phorum_ajax_error('Illegal Ajax request');
}
Exemple #2
0
/**
 * Retrieve an argument from the call arguments.
 *
 * The Ajax call arguments are stored in the array $PHORUM['ajax_args'].
 * his function can be used to retrieve a single argument from this array.
 *
 * It can check the argument type by providing the $type parameter.
 * Also, a default value can be supplied, which will be used in case the
 * requested argument wasn't included in the call data.
 *
 * If an argument is invalid or missing without providing a default value,
 * then an Ajax error will be returned.
 *
 * @param string $arg
 *     The name of the argument to retrieve.
 *
 * @param mixed $type
 *
 *     The type of data to retrieve. Options are: "int", "int>0", "int>=0",
 *     "string", "boolean". These types can be prefixed with "array:" to
 *     indicate that an array of those types has to be returned. If the input
 *     argument is not an array in this case, then this function will
 *     convert it to a single item array.
 *
 *     The type can also be NULL, in which case the argument is not checked
 *     at all. This is useful for more complex data types, which need to be
 *     checked by the function that uses them.
 *
 * @param mixed $default
 *     The default value for the argument or NULL if no default is available.
 *
 * @return $value - The value for the argument.
 */
function phorum_ajax_getarg($arg, $type = NULL, $default = NULL)
{
    global $PHORUM;
    // Fetch the argument from the Ajax request data.
    if (!isset($PHORUM["ajax_args"][$arg])) {
        if (!is_null($default)) {
            return $default;
        } else {
            phorum_ajax_error("missing Ajax argument: " . htmlspecialchars($arg));
        }
    }
    $value = $PHORUM["ajax_args"][$arg];
    // Handle JSON decoding if the value was JSON encoded.
    // This is determined by the magic marker $JSON$ that can be
    // prefixed to an argument. This encoding method is used
    // for handling arrays in JSONP call parameters from the
    // Phorum JavaScript library.
    if (strlen($value) > 6 && substr($value, 0, 6) == '$JSON$') {
        $value = phorum_api_json_decode(substr($value, 6));
    }
    // Return immediately, if we don't need to do type checking.
    if (is_null($type)) {
        return $value;
    }
    // Check if an array should be returned.
    $array = FALSE;
    if (substr($type, 0, 6) == 'array:') {
        $type = substr($type, 6);
        $array = TRUE;
    }
    if ($array && !is_array($value)) {
        phorum_ajax_error("illegal argument: argument \"{$arg}\" should be an array ({$type})");
    }
    if (!$array && is_array($value)) {
        phorum_ajax_error("illegal argument: argument \"{$arg}\" should not be an array ({$type})");
    }
    if (!is_array($value)) {
        $value = array(0 => $value);
    }
    // Check the argument's data type.
    switch ($type) {
        case 'string':
            break;
        case 'int>0':
        case 'int>=0':
        case 'int':
            foreach ($value as $k => $v) {
                if (!preg_match('/^[-+]?\\d+$/', $v)) {
                    phorum_ajax_error("illegal argument: argument \"{$arg}\" must contain " . ($array ? "only integer values" : "an integer value"));
                }
                if ($type == 'int>0' && $v <= 0) {
                    phorum_ajax_error("illegal argument: argument \"{$arg}\" must contain " . ($array ? "only integer values" : "an integer value") . ", larger than zero");
                }
                if ($type == 'int>=0' && $v < 0) {
                    phorum_ajax_error("illegal argument: argument \"{$arg}\" must contain " . ($array ? "only integer values" : "an integer value") . ", larger than or equal to zero");
                }
            }
            break;
        case 'boolean':
            foreach ($value as $k => $v) {
                $val = strtolower($v);
                if ($v == '1' || $v == 'yes' || $v == 'true') {
                    $value[$k] = TRUE;
                } elseif ($v == '0' || $v == 'no' || $v == 'false') {
                    $value[$k] = FALSE;
                } else {
                    phorum_ajax_error("illegal argument: argument \"{$arg}\" must contain " . ($array ? "only boolean values" : "a boolean value") . " (0, 1, \"yes\", \"no\", \"true\" or \"false\")");
                }
            }
            break;
        default:
            phorum_ajax_error("Internal error: illegal argument type: " . ($array ? "array:" : "") . $type);
    }
    return $array ? $value : $value[0];
}