]> git.mjollnir.org Git - moodle.git/commitdiff
submission in post.php cleanup, fixes for bugs #5530, #5484 and SC#252; merged from...
authorskodak <skodak>
Fri, 19 May 2006 20:18:05 +0000 (20:18 +0000)
committerskodak <skodak>
Fri, 19 May 2006 20:18:05 +0000 (20:18 +0000)
mod/forum/post.html
mod/forum/post.php
mod/forum/prune.html

index dc3d899870ad64500de07748306d53ba5e1af8d0..c9ce6f2787548960c2a273ba20b4c6302f738798 100644 (file)
@@ -158,7 +158,6 @@ if (!isset($discussion->timeend)) {
     <input type="hidden" name="discussion" value="<?php p($post->discussion) ?>" />
     <input type="hidden" name="parent"     value="<?php p($post->parent) ?>" />
     <input type="hidden" name="userid"     value="<?php p($post->userid) ?>" />
-    <input type="hidden" name="groupid"    value="<?php p($post->groupid) ?>" />
     <input type="hidden" name="edit"       value="<?php p($post->edit) ?>" />
     <input type="submit" value="<?php p(($post->edit) ? get_string('savechanges') : get_string('posttoforum', 'forum')); ?>" />
     </td>
index d420360fa44b20635628b5a2d8eb65a8d06ff55d..f60855f4a65b0ba9649368c16f39c17f71f995cd 100644 (file)
@@ -19,7 +19,7 @@
             $wwwroot = str_replace('http:','https:', $wwwroot);
         }
 
-        if (isset($forum)) {      // User is starting a new discussion in a forum
+        if (!empty($forum)) {      // User is starting a new discussion in a forum
             if (! $forum = get_record('forum', 'id', $forum)) {
                 error('The forum number was incorrect');
             }
     require_login(0, false);   // Script is useless unless they're logged in
 
     if ($post = data_submitted()) {
-        if (empty($post->course)) {
-            error('No course was defined!');
+        if (! $forum = get_record('forum', 'id', $forum)) {
+            error('The forum number was incorrect');
         }
 
-        if (!$course = get_record('course', 'id', $post->course)) {
+        if (!$course = get_record('course', 'id', $forum->course)) {
             error('Could not find specified course!');
         }
 
+        require_login($course->id, false);
+        $adminedit = (isadmin() and !empty($CFG->admineditalways));
+
         if (!empty($course->lang)) {           // Override current language
             $CFG->courselang = $course->lang;
         }
 
         if (empty($SESSION->fromurl)) {
-            $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$post->forum";
+            $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
         } else {
             $errordestination = $SESSION->fromurl;
         }
 
-        $post->subject = clean_param(strip_tags($post->subject, '<lang><span>'), PARAM_CLEAN);        // Strip all tags except lang
+        $post->subject = clean_param(strip_tags($post->subject, '<lang><span>'), PARAM_CLEAN); // Strip all tags except multilang
 
-        //$post->message = clean_text($post->message, $post->format);   // Clean up any bad tags
+        //$post->message will be cleaned later before display
 
         $post->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
 
-        if (!$cm = get_coursemodule_from_instance("forum", $post->forum, $course->id)) { // For the logs
+        if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { // For the logs
             $cm->id = 0;
         }
 
-        if (!$post->subject or !$post->message) {
+        if (($post->subject == '') or ($post->message == '')) {
             $post->error = get_string("emptymessage", "forum");
 
-        } else if ($post->edit) {           // Updating a post
-            $post->id = $post->edit;
-            $message = '';
-
-            //fix for bug #4314
-            if (!$realpost = get_record('forum_posts','id',$post->id)){
-                $realpost = new object;
-                $realpost->userid = -1;
-            }
+        } else if ($post->edit) {
+/// Updating a post
+           if (! $oldpost = forum_get_post_full($post->edit)) {
+                   error("Post ID was incorrect");
+               }
+               if (($oldpost->userid <> $USER->id) and !$adminedit) {
+                   error("You can't edit other people's posts!");
+               }
+               if (! $discussion = get_record("forum_discussions", "id", $oldpost->discussion)) {
+                   error("This post is not part of a discussion!");
+               }
+               if ($discussion->forum != $forum->id) {
+                   error("The forum number is incorrect");
+               }
+               if ($discussion->course != $course->id) {
+                   error("The course number is incorrect");
+               }
+               if (!($forum->type == 'news' && !$oldpost->parent && $discussion->timestart > time())) {
+                   if (((time() - $oldpost->created) > $CFG->maxeditingtime) and !$adminedit) {
+                       error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
+                   }
+               }
+
+            $updatepost = new object;
+            $updatepost->id = $oldpost->id;
+            $updatepost->parent = $oldpost->parent;
+            $updatepost->forum = $oldpost->forum;
+            $updatepost->discussion = $oldpost->discussion;
+            $updatepost->userid = $oldpost->userid;
+
+            $updatepost->subject = $post->subject; //already cleaned
+            $updatepost->message = $post->message; //cleaning only before display
+            $updatepost->format = $post->format;
+            $updatepost->attachment = $post->attachment;
+
+            $updatepost->course = $course->id;
+            $updatepost->subscribe = optional_param('subscribe', 0, PARAM_BOOL);
+            $updatepost->unsubscribe = optional_param('unsubscribe', 0, PARAM_BOOL);
 
-            if ($realpost->userid <> $USER->id && !isadmin()){
-                error("You can not update this post");
-            }
+            $message = '';
 
-            if (get_field('forum', 'type', 'id', $forum) == 'news' && !$post->parent) {
-                $updatediscussion->id = $post->discussion;
+            if (get_field('forum', 'type', 'id', $forum) == 'news' && !$oldpost->parent) {
+                $updatediscussion = new object;
+                $updatediscussion->id = $oldpost->discussion;
                 if (empty($post->timestartdisabled)) {
                     $updatediscussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
                 } else {
 
             if (!isset($post->error)) {
 
-                if (forum_update_post($post,$message)) {
+                if (forum_update_post($updatepost,$message)) {
 
                     add_to_log($course->id, "forum", "update post",
-                            "discuss.php?d=$post->discussion&amp;parent=$post->id", "$post->id", $cm->id);
+                            "discuss.php?d=$updatepost->discussion&amp;parent=$updatepost->id", "$updatepost->id", $cm->id);
 
                     $timemessage = 2;
                     if (!empty($message)) { // if we're printing stuff about the file upload
                     }
                     $message .= '<br />'.get_string("postupdated", "forum");
 
-                    if ($subscribemessage = forum_post_subscription($post)) {
+                    if ($subscribemessage = forum_post_subscription($updatepost)) {
                         $timemessage = 4;
                     }
-                    redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage);
+                    redirect(forum_go_back_to("discuss.php?d=$updatepost->discussion#$updatepost->id"), $message.$subscribemessage, $timemessage);
 
                 } else {
                     error(get_string("couldnotupdate", "forum"), $errordestination);
                 exit;
 
             }
-        } else if ($post->discussion) { // Adding a new post to an existing discussion
+         } else if ($post->discussion) {
+/// Adding a new post to an existing discussion
+               if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
+                   error("This post is not part of a discussion!");
+               }
+               if ($discussion->forum != $forum->id) {
+                   error("The forum number is incorrect");
+               }
+               if ($discussion->course != $course->id) {
+                   error("The course number is incorrect");
+               }
+               if (! $parent = forum_get_post_full($post->parent)) {
+                   error("Parent post does not exist");
+               }
+            if ($parent->discussion != $discussion->id) {
+                   error("Parent not in this discussion");
+            }
+            if (! forum_user_can_post($forum)) {
+                error("Sorry, but you can not post in this forum.");
+            }
+
+
+            $newpost = new object;
+            $newpost->parent = $post->parent;
+            $newpost->forum = $forum->id;
+            $newpost->discussion = $discussion->id;
+            $newpost->parent = $parent->id;
+
+            $newpost->subject = $post->subject; //already cleaned
+            $newpost->message = $post->message; //cleaning only before display
+            $newpost->format = $post->format;
+            $newpost->mailnow = optional_param('mailnow', 0, PARAM_BOOL);
+
+            $newpost->course = $course->id;
+            $newpost->subscribe = optional_param('subscribe', 0, PARAM_BOOL);
+            $newpost->unsubscribe = optional_param('unsubscribe', 0, PARAM_BOOL);
+
             $message = '';
-            if ($post->id = forum_add_new_post($post,$message)) {
+
+            if ($newpost->id = forum_add_new_post($newpost,$message)) {
 
                 add_to_log($course->id, "forum", "add post",
-                          "discuss.php?d=$post->discussion&amp;parent=$post->id", "$post->id", $cm->id);
+                          "discuss.php?d=$newpost->discussion&amp;parent=$newpost->id", "$newpost->id", $cm->id);
 
                 $timemessage = 2;
                 if (!empty($message)) { // if we're printing stuff about the file upload
                 }
                 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
 
-                if ($subscribemessage = forum_post_subscription($post)) {
+                if ($subscribemessage = forum_post_subscription($newpost)) {
                     $timemessage = 4;
                 }
 
-                if ($post->mailnow) {
+                if ($newpost->mailnow) {
                     $message .= get_string("postmailnow", "forum");
                     $timemessage = 4;
                 }
 
-                redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage);
+                redirect(forum_go_back_to("discuss.php?d=$newpost->discussion#$newpost->id"), $message.$subscribemessage, $timemessage);
 
             } else {
                 error(get_string("couldnotadd", "forum"), $errordestination);
             }
             exit;
 
-        } else {                     // Adding a new discussion
-            $post->mailnow = empty($post->mailnow) ? 0 : 1;
-            $discussion = $post;
-            $discussion->name  = $post->subject;
+        } else {
+/// Adding a new discussion
+            if (! forum_user_can_post_discussion($forum)) {
+                error("Sorry, but you can not post a new discussion in this forum.");
+            }
+
+            $discussion = new object;
+            $discussion->forum = $forum->id;
+            $discussion->course = $course->id;
+
+            $discussion->mailnow = optional_param('mailnow', 0, PARAM_BOOL);
+            $discussion->name = $post->subject;
             $discussion->intro = $post->message;
+            $discussion->format = $post->format;
+            $discussion->groupid = get_current_group($course->id);
+            if (isteacheredit($course->id) and $discussion->groupid == 0) {
+                $discussion->groupid = -1;
+            }
+
+            $discussion->course = $course->id;
+            $discussion->subscribe = optional_param('subscribe', 0, PARAM_BOOL);
+            $discussion->unsubscribe = optional_param('unsubscribe', 0, PARAM_BOOL);
+
+            if (! forum_user_can_post_discussion($forum)) {
+                error("Sorry, but you can not post a new discussion in this forum.");
+            }
+
             $newstopic = false;
-            if (get_field('forum', 'type', 'id', $forum) == 'news' && !$post->parent) {
+            if (get_field('forum', 'type', 'id', $forum) == 'news') {
                 $newstopic = true;
             }
             if ($newstopic && empty($post->timestartdisabled)) {
                     }
                     $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
 
-                    if ($post->mailnow) {
+                    if ($discussion->mailnow) {
                         $message .= get_string("postmailnow", "forum");
                         $timemessage = 4;
                     }
                         $timemessage = 4;
                     }
 
-                    redirect(forum_go_back_to("view.php?f=$post->forum"), $message.$subscribemessage, $timemessage);
+                    redirect(forum_go_back_to("view.php?f=$discussion->forum"), $message.$subscribemessage, $timemessage);
 
                 } else {
                     error(get_string("couldnotadd", "forum"), $errordestination);
         $defaultformat = FORMAT_MOODLE;
     }
 
-    if (isset($post->error)) {     // User is re-editing a failed posting
+    if (!empty($post->error)) {
+/// User is re-editing a failed posting
 
         // Set up all the required objects again, and reuse the same $post
 
             }
         }
 
-    } else if (!empty($forum)) {      // User is starting a new discussion in a forum
+    } else if (!empty($forum)) {
+/// User is starting a new discussion in a forum
 
         $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
 
         $post->message = "";
         $post->format = $defaultformat;
 
-        $post->groupid = get_current_group($course->id);
-        if (isteacheredit($course->id) and $post->groupid == 0) {
-            $post->groupid = -1;
-        }
-
         forum_set_return();
 
-    } else if (!empty($reply)) {      // User is writing a new reply
+    } else if (!empty($reply)) {
+/// User is writing a new reply
 
         if (! $parent = forum_get_post_full($reply)) {
             error("Parent post ID was incorrect");
 
         unset($SESSION->fromdiscussion);
 
-    } else if (!empty($edit)) {  // User is editing their own post
+    } else if (!empty($edit)) {
+/// User is editing their own post
 
         $adminedit = (isadmin() and !empty($CFG->admineditalways));
 
         }
         if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
             if (((time() - $post->created) > $CFG->maxeditingtime) and !$adminedit) {
-                error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
+                error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)),
+                       "$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id#$post->id" );
             }
         }
         if (! $course = get_record("course", "id", $discussion->course)) {
         unset($SESSION->fromdiscussion);
 
 
-    } else if (!empty($delete)) {  // User is deleting a post
+    } else if (!empty($delete)) {
+/// User is deleting a post
 
         if (! $post = forum_get_post_full($delete)) {
             error("Post ID was incorrect");
 
         $replycount = forum_count_replies($post);
 
-        if (!empty($confirm)) {    // User has confirmed the delete
+        if (!empty($confirm) and confirm_sesskey()) {    // User has confirmed the delete
 
             if ($post->totalscore) {
                 notice(get_string("couldnotdeleteratings", "forum"),
             }
 
 
-        } else { // User just asked to delete something
+        } else {
+// User just asked to delete something
 
             forum_set_return();
 
                 }
                 print_header();
                 notice_yesno(get_string("deletesureplural", "forum", $replycount+1),
-                             "post.php?delete=$delete&amp;confirm=$delete",
+                             "post.php?delete=$delete&amp;confirm=$delete&amp;sesskey=".sesskey(),
                              $_SERVER["HTTP_REFERER"]);
 
                 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false);
             } else {
                 print_header();
                 notice_yesno(get_string("deletesure", "forum", $replycount),
-                             "post.php?delete=$delete&amp;confirm=$delete",
+                             "post.php?delete=$delete&amp;confirm=$delete&amp;sesskey=".sesskey(),
                              $_SERVER["HTTP_REFERER"]);
                 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
             }
         die;
 
 
-    } else if (!empty($prune)) {  // Teacher is pruning
+    } else if (!empty($prune)) {
+// Teacher is pruning
 
         if (!$post = forum_get_post_full($prune)) {
             error("Post ID was incorrect");
             $cm->id = 0;
         }
 
-        if (!empty($name)) {    // User has confirmed the prune
+        if (!empty($name) and confirm_sesskey()) {    // User has confirmed the prune
 
+            $newdiscussion = new object;
             $newdiscussion->course = $discussion->course;
             $newdiscussion->forum = $discussion->forum;
-            $newdiscussion->name = $name;
+            $newdiscussion->name = strip_tags($name, '<lang><span>'); // Strip all tags except multilang
             $newdiscussion->firstpost = $post->id;
             $newdiscussion->userid = $discussion->userid;
             $newdiscussion->groupid = $discussion->groupid;
 
             $newpost->id = $post->id;
             $newpost->parent = 0;
-            $newpost->subject = $name;
+            $newpost->subject = $newdiscussion->name;
 
             if (!update_record("forum_posts", $newpost)) {
                 error('Could not update the original post');
index da09e93c20a0dbcdb77c0cfd554b4c2dcdc48fa1..aaa931117d56c329b04e2b453034e5f706f2ba2f 100644 (file)
@@ -11,6 +11,7 @@
     <td align="center" colspan="2">
     <input type="hidden" name="prune"   value="<?php p($prune) ?>" />
     <input type="hidden" name="confirm" value="<?php p($prune) ?>" />
+    <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
     <input type="submit" value="<?php print_string('prune', 'forum'); ?>" />
     </td>
 </tr>