]> git.mjollnir.org Git - moodle.git/commitdiff
buggy_referer is dead! Long live buggy_referer!
authormoodler <moodler>
Thu, 2 Jan 2003 14:49:23 +0000 (14:49 +0000)
committermoodler <moodler>
Thu, 2 Jan 2003 14:49:23 +0000 (14:49 +0000)
Should be fixed now.  As a bonus, I've removed all the uses of
HTTP_POST_VARS from all scripts.

All forms should use the new data_submitted() function to collect
form data (it does the match_referer thing internally now).

Much nicer.

26 files changed:
admin/auth.php
admin/config.php
admin/site.php
course/categories.php
course/edit.php
course/editsection.php
course/enrol.php
course/teachers.php
files/index.php
lib/weblib.php
login/change_password.php
login/forgot_password.php
login/index.php
login/signup.php
mod/assignment/submissions.php
mod/choice/view.php
mod/forum/post.php
mod/journal/edit.php
mod/journal/report.php
mod/quiz/attempt.php
mod/quiz/category.php
mod/quiz/edit.php
mod/quiz/question.php
mod/resource/details.php
mod/survey/details.php
user/edit.php

index 13e5dd29e624b5bbc4789ea761bbadbe5f43fc65..ea3da976e1e28a3faefe68f41649b205e1f783ba 100644 (file)
@@ -16,9 +16,7 @@
 
 /// If data submitted, then process and store.
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
-
-        $config = (object)$HTTP_POST_VARS;
+       if ($config = data_submitted()) {
 
         validate_form($config, $err);
 
index 67a34d6f0e9208b2ac6a5e49b08f24f40068680b..48a9b9c1f432459d6cb4a6dd480b456b7380be8d 100644 (file)
@@ -35,9 +35,7 @@
 
 /// If data submitted, then process and store.
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
-
-        $config = (object)$HTTP_POST_VARS;
+       if ($config = data_submitted()) {
 
         validate_form($config, $err);
 
index 201f8f53075151d70e4b33c1073c1d44e8a2bd35..b1bdc50e0d7b187aa45ba6566fce0c14aec5dd9c 100644 (file)
@@ -11,9 +11,7 @@
 
 /// If data submitted, then process and store.
 
-    if (match_referer() && isset($HTTP_POST_VARS)) {
-
-        $form = (object)$HTTP_POST_VARS;
+       if ($form = data_submitted()) {
 
         validate_form($form, $err);
 
index 921455003aba59a15bcb67dfcb7a181294b7f1c2..07506a07d8dd46a662c89a391b83098cc3304028 100644 (file)
 
 /// If data submitted, then process and store.
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
+       if ($form = data_submitted()) {
 
         $categories = array();
 
         // Peel out all the data from variable names.
-        foreach ($HTTP_POST_VARS as $key => $val) {
+        foreach ($form as $key => $val) {
             if ($key == "new" and $val != "") {
                 $cat->name = $val;
                 if (!insert_record("course_categories", $cat)) {
index 2552dc8bae28f799bfc69ee78776533244cdf795..ce988de87348b9b7d82e5e8f3958b43e13f47b32 100644 (file)
@@ -31,9 +31,7 @@
 
 /// If data submitted, then process and store.
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
-
-        $form = (object)$HTTP_POST_VARS;
+       if ($form = data_submitted()) {
 
         $form->startdate = make_timestamp($form->startyear, $form->startmonth, $form->startday);
 
index beae84dcb3c11ecfe698456c1109d61f01335179..2d2169cf96749f35e5379b0ac88a56862d3d3dc4 100644 (file)
 
 /// If data submitted, then process and store.
 
-    if (match_referer() && isset($HTTP_POST_VARS)) {
+    if ($form = data_submitted()) {
 
         $timenow = time();
 
-        if (! set_field("course_sections", "summary", $summary, "id", $section->id)) {
+        if (! set_field("course_sections", "summary", $form->summary, "id", $section->id)) {
             error("Could not update the summary!");
         }
 
index c321f29ed272659ebdf6706b140e4cde6af51fa0..5072a703df91ffad0862ff3ac0918dcc09a5949f 100644 (file)
@@ -11,9 +11,9 @@
         error("That's an invalid course id");
     }
 
-    if (match_referer() && isset($HTTP_POST_VARS)) {    // form submitted
+    if ($form = data_submitted()) {
 
-        if ($password == $course->password) {
+        if ($form->password == $course->password) {
 
             if (isguest()) {
                 add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$REMOTE_ADDR, $REMOTE_HOST");
index e9b0c5f6b3290a522fdfc426415ec88b1680af06..e4e992f5c126cab73e320714d2951033c461a2b8 100644 (file)
 
 /// If data submitted, then process and store.
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
+    if ($form = data_submitted()) {
 
         $rank = array();
 
         // Peel out all the data from variable names.
-        foreach ($HTTP_POST_VARS as $key => $val) {
+        foreach ($form as $key => $val) {
             if ($key <> "id") {
                 $type = substr($key,0,1);
                 $num  = substr($key,1);
index 3bcb2aa279132ca5e682be50d1ccc61ca74fabef..cee36dd0c38654d9f4eae22729d2413eab190b71 100644 (file)
         $action = "";
     }    
 
-
-    if (!match_referer("$baseweb/files/index.php")) {   // To stop spoofing 
-        $action="cancel";
-        $wdir="/";
-    }
-
     if (!$wdir) {
         $wdir="/";
     }
 
 
-
     switch ($action) {
 
         case "upload":
index 51da7cadb37ef730458ae3df35d5a247b0e480c3..dc7655adcaec238fe671c2a033e71ac1cce6fdc6 100644 (file)
@@ -179,6 +179,30 @@ function match_referer($good_referer = "") {
     return $good_referer == get_referer();
 }
 
+function data_submitted($url="") {
+/// Used on most forms in Moodle to check for data
+/// Returns the data as an object, if it's found.
+/// 
+/// Checks that submitted POST data exists, and also 
+/// checks the referer against the given url (it uses 
+/// the current page if none was specified.
+
+    global $HTTP_POST_VARS, $CFG;
+
+    if (empty($HTTP_POST_VARS)) {
+        return false;
+    } else {
+        if (match_referer($url)) {
+            return (object)$HTTP_POST_VARS;
+        } else {
+            if ($CFG->debug > 10) {
+                notice("The form did not come from this page! (referer = ".get_referer().")");
+            }
+            return false;
+        }
+    }
+}
+
 
 function stri_replace($find, $replace, $string ) {
 /// This does a search and replace, ignoring case
index d4fbe961922fa8cc86e57026db45012e52074d5e..e9a6071e9f01fd4efc25519922a12288e1d032ad 100644 (file)
@@ -10,9 +10,7 @@
         }
     }
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
-
-               $frm = (object) $HTTP_POST_VARS;
+    if ($frm = data_submitted()) {
 
                validate_form($frm, $err);
 
index 8da98a7be39a2b9f8febdd20797693543e124413..94cf72793a705a71096d2e37969d2cdfc6584e50 100644 (file)
@@ -2,9 +2,7 @@
 
        include("../config.php");
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
-
-               $frm = (object)$HTTP_POST_VARS;
+       if ($frm = data_submitted()) {
 
                validate_form($frm, $err);
 
index bbaf7d43030b371456ea08b7225feda0af8edc5f..5bb1b83826f60903e2f160a22b18e0c20d4941e0 100644 (file)
@@ -19,9 +19,8 @@
     }
 
 
-    if (match_referer() && isset($HTTP_POST_VARS)) {    // form submitted
+       if ($frm = data_submitted()) {
 
-        $frm = (object)$HTTP_POST_VARS;
         $user = authenticate_user_login($frm->username, $frm->password);
 
            update_login_count();
index 6911550198bcc74e7e1ac0be657d4550920cfde8..b3fa090b1fba39b216abb3901c786156a3e7fa55 100644 (file)
@@ -3,8 +3,7 @@
        require("../config.php");
        require("../lib/countries.php");
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
-               $user = (object) $HTTP_POST_VARS;
+       if ($user = data_submitted()) {
 
                validate_form($user, $err);
 
index 42f0be30e8e2bcf95bdeef37b26f343fcef3aba4..bd3a9d49fb01c557d7467a4568b5e9ec1f1884ac 100644 (file)
 
 
 /// If data is being submitted, then process it
-    if (match_referer() && isset($HTTP_POST_VARS)) { 
+    if ($data = data_submitted()) {
        
         $feedback = array();
+        $data = (array)$data;
 
         // Peel out all the data from variable names.
-        foreach ($HTTP_POST_VARS as $key => $val) {
+        foreach ($data as $key => $val) {
             if ($key <> "id") {
                 $type = substr($key,0,1);
                 $num  = substr($key,1); 
index 8dd8a54d4d4fd7f3e716466e270391c5f4a724b3..ddc66da931ec4fc167095192a3df793fd284601c 100644 (file)
@@ -26,8 +26,7 @@
         $answerchecked[$current->answer] = "CHECKED";
     }
 
-    if (match_referer() && isset($HTTP_POST_VARS)) {    // form submitted
-        $form = (object)$HTTP_POST_VARS;
+    if ($form = data_submitted()) {
         $timenow = time();
         if ($current) {
             $newanswer = $current;
index 3b9ed91c54e7ff2e1d23036bebd78357ae7312e5..7109ed734aaebd676d758e55258cc9433048f917 100644 (file)
@@ -10,8 +10,7 @@
         error(get_string("noguestpost", "forum"), $HTTP_REFERER);
     }
 
-    if (match_referer() && isset($HTTP_POST_VARS)) {    // form submitted
-        $post = (object)$HTTP_POST_VARS;
+    if ($post = data_submitted()) {
 
         $post->subject = strip_tags($post->subject);  // Strip all tags
         $post->message = clean_text($post->message, $post->format);   // Clean up any bad tags
index 9cc792b430658089d21839a710a14365ee320bdb..63900492f3eb01563b524bbbcd42c93e6cfd6538 100644 (file)
 
 /// If data submitted, then process and store.
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
+    if ($form = data_submitted()) {
 
                $timenow = time();
 
-        $text = clean_text($text, $format);
+        $form->text = clean_text($form->text, $form->format);
 
                if ($entry) {
             $newentry->id = $entry->id;
-            $newentry->text = $text;
+            $newentry->text = $form->text;
+            $newentry->format = $form->format;
             $newentry->modified = $timenow;
-            $newentry->format = $format;
                        if (! update_record("journal_entries", $newentry)) {
                                error("Could not update your journal");
                        }
@@ -45,9 +45,9 @@
                } else {
             $newentry->userid = $USER->id;
             $newentry->journal = $journal->id;
+            $newentry->text = $form->text;
+            $newentry->format = $form->format;
             $newentry->modified = $timenow;
-            $newentry->text = $text;
-            $newentry->format = $format;
                        if (! $newentry->id = insert_record("journal_entries", $newentry)) {
                                error("Could not insert a new journal entry");
                        }
index 808975461d2dee19eb8960f42a6b0d9829048836..87d9e38da90be94186f94b118a4fdcbfbc1d4dc1 100644 (file)
                   <A HREF=view.php?id=$cm->id>$journal->name</A> -> Responses", "",
                   "", true);
 
-    if (match_referer() && isset($HTTP_POST_VARS)) { // Feedback submitted
+    if ($data = data_submitted()) {
        
         $feedback = array();
+        $data = (array)$data;
 
         // Peel out all the data from variable names.
-        foreach ($HTTP_POST_VARS as $key => $val) {
+        foreach ($data as $key => $val) {
             if ($key <> "id") {
                 $type = substr($key,0,1);
                 $num  = substr($key,1); 
index a3831756545acfad9d8b56ad23eb68982d6d4598..e7658b2adceae3a2372d4e200a01279fef2d607d 100644 (file)
     $available = ($quiz->timeopen < $timenow and $timenow < $quiz->timeclose);
 
 /// Check to see if they are submitting answers
-    if (match_referer() && isset($HTTP_POST_VARS)) {
+    if ($rawanswers = data_submitted()) {
         add_to_log($course->id, "quiz", "submit", "attempt.php?id=$cm->id", "$quiz->id");
 
-        $rawanswers = $HTTP_POST_VARS;
+        $rawanswers = (array)$rawanswers;
+
         unset($rawanswers["q"]);  // quiz id
         if (! count($rawanswers)) {
             print_heading(get_string("noanswers", "quiz"));
index eb55e6521b386c0951e1a7f329b6f53fddddefcf..dcc90386595cef4798a43def95835ff063db91b1 100644 (file)
@@ -88,9 +88,9 @@
 
 /// If data submitted, then process and store.
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
+    if ($form = data_submitted()) {
 
-        $form = $HTTP_POST_VARS;
+        $form = (array)$form;
 
         // Peel out all the data from variable names.
         foreach ($form as $key => $val) {
index 511055b1183f32fbe28342de7c2af549c9402fce..65fce5a20ec93a097432e8669963723a3c95ba24 100644 (file)
@@ -9,8 +9,9 @@
         $destination = "";
     }
 
-    if (match_referer($destination) && isset($course) && isset($HTTP_POST_VARS)) {    // form submitted from mod.html
-        $modform = (object)$HTTP_POST_VARS;
+    $modform = data_submitted($destination);
+
+    if ($modform and !empty($modform->course)) {    // form submitted from mod.html
 
         if (empty($modform->name) or empty($modform->intro)) {
             error(get_string("filloutallfields"), $HTTP_REFERER);
index 1806adfa97017f8f011ee2de952eff052177a38a..2fa064c39461f91b87f27e6b189eee548e67e3c7 100644 (file)
         }
     }
 
-    if (match_referer() and isset($HTTP_POST_VARS)) {    // question submitted
+    if ($form = data_submitted()) { 
 
-        $form = (object)$HTTP_POST_VARS;
-        
         // First, save the basic question itself
         $question->name         = $form->name;
         $question->questiontext = $form->questiontext;
index 36de44678a9eadda110305662c213cc50f2ea4d2..d1cd83ead155a4f02f28e2585a2cffe4fe25b6b2 100644 (file)
@@ -7,8 +7,7 @@
 
     $usehtmleditor = can_use_richtext_editor();
 
-    if (match_referer("$destination") && isset($HTTP_POST_VARS)) {    // form submitted
-        $form = (object)$HTTP_POST_VARS;
+    if ($form = data_submitted($destination)) { 
 
         if (! $course = get_record("course", "id", $form->course)) {
             error("This course doesn't exist");
index 5a0625400adbdba4319e77854d704d588433693a..aab504a9870f5aac03ec8a22eed61350241ff44e 100644 (file)
@@ -2,8 +2,7 @@
 
     require("../../config.php");
 
-    if (match_referer("$destination") && isset($HTTP_POST_VARS)) {    // form submitted
-        $form = (object)$HTTP_POST_VARS;
+    if ($form = data_submitted($destination)) { 
 
         if (! $course = get_record("course", "id", $form->course)) {
             error("This course doesn't exist");
index 011bc2cdc58719369bf8e7256b1e0383971cc05c..8ff2dc1e40520eca6c524790e50e95eb2a5740a6 100644 (file)
@@ -42,9 +42,7 @@
 
 /// If data submitted, then process and store.
 
-       if (match_referer() && isset($HTTP_POST_VARS)) {
-
-        $usernew = (object)$HTTP_POST_VARS;
+       if ($usernew = data_submitted()) {
 
         $usernew->firstname = strip_tags($usernew->firstname);
         $usernew->lastname  = strip_tags($usernew->lastname);