Index: lib/weblib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/weblib.php,v retrieving revision 1.1346 diff -u -r1.1346 weblib.php --- lib/weblib.php 31 Oct 2009 22:02:06 -0000 1.1346 +++ lib/weblib.php 9 Nov 2009 14:33:31 -0000 @@ -598,14 +598,48 @@ } /** + * Reports whether the current request is a HTTP POST request. + * By default, this also checks that the sesskey is valid. + * $param boolean $checksesskey Optional, and you are stronly recommended to accept + * the default of true. Only return true if the sesskey is also valid. + * @return boolean + */ +function is_post($checksesskey = true) { + $ispost = array_key_exists('REQUEST_METHOD', $_SERVER) && + $_SERVER['REQUEST_METHOD'] == 'POST'; + if ($checksesskey) { + $ispost = $ispost && confirm_sesskey(); + } + return $ispost; +} + +/** + * Causes an error if the current request is not a HTTP POST request. + * By default, this also does {@link require_sesskey()}. + * $param boolean $checksesskey Optional, and you are stronly recommended to accept + * the default of true. Also cause an error if the sesskey is not valid. + */ +function require_post($checksesskey = true) { + if ($checksesskey) { + require_sesskey(); + } + if (!is_post(false)) { + print_error('mustbeapostrequest'); + } +} + +/** * Determine if there is data waiting to be processed from a form * - * Used on most forms in Moodle to check for data - * Returns the data as an object, if it's found. - * This object can be used in foreach loops without - * casting because it's cast to (array) automatically + * This function may be deprecated at some piont. Please consider using one of + * these alternatives: + * 1. If you are dealing with a HTML form, use a {@link moodleform}. + * 2. If you just want to know whether this was a HTTP POST request, + * use {@link is_post()}. + * 3. If you want to get data submitted by the user, use + * {@link required_param()} or {@link optional_param()}. * - * Checks that submitted POST data exists and returns it as object. + * Returns the $_POST as an object, if it is an object, otherwise returns false. * * @uses $_POST * @return mixed false or object