Example #1
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];
}
Example #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", "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];
    // 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':
            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");
                }
            }
            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];
}
Example #3
0
    { call      : "getforumsettings",
      forum_id  : 10 }
    Retrieve the default forum settings:
    { call      : "getforumsettings",
      forum_id  : 0 }
RETURN VALUE
    An object containing the settings data for a forum.
ERRORS
    The call will return an error if no forum exists for the provided
    forum_id or if the active user does not have read access for the
    forum.
AUTHOR
    Maurice Makaay <*****@*****.**>
*/
if (!defined('PHORUM')) {
    return;
}
require_once PHORUM_PATH . '/include/api/forums.php';
// Process the arguments.
$forum_id = phorum_ajax_getarg('forum_id', 'int');
// For forum_id = 0, we return the default settings.
if ($forum_id == 0) {
    phorum_ajax_return($PHORUM['default_forum_options']);
}
// Retrieve and return the forum data. No permission checking is needed.
// The forum_id parameter is checked from common.php already.
$data = phorum_api_forums_get($forum_id);
if (!$data) {
    phorum_ajax_error("Forum {$forum_id} does not exist");
}
phorum_ajax_return($data);