MDL-21769 fixed input validation
[moodle.git] / blog / edit.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
19 /**
20  * Blog entry edit page
21  *
22  * @package    moodlecore
23  * @subpackage blog
24  * @copyright  2009 Nicolas Connault
25  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26  */
27 require_once(dirname(dirname(__FILE__)).'/config.php');
28 include_once('lib.php');
29 include_once('locallib.php');
31 $action   = required_param('action', PARAM_ALPHA);
32 $id       = optional_param('entryid', 0, PARAM_INT);
33 $confirm  = optional_param('confirm', 0, PARAM_BOOL);
34 $modid    = optional_param('modid', 0, PARAM_INT); // To associate the entry with a module instance
35 $courseid = optional_param('courseid', 0, PARAM_INT); // To associate the entry with a course
37 $PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
39 // If action is add, we ignore $id to avoid any further problems
40 if (!empty($id) && $action == 'add') {
41     $id = null;
42 }
44 $returnurl = new moodle_url('/blog/index.php');
46 if (!empty($courseid) && empty($modid)) {
47     $returnurl->param('courseid', $courseid);
48     $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
49 }
51 // If a modid is given, guess courseid
52 if (!empty($modid)) {
53     $returnurl->param('modid', $modid);
54     $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
55     $returnurl->param('courseid', $courseid);
56     $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
57 }
59 $blogheaders = blog_get_headers();
61 require_login($courseid);
63 if ($action == 'edit') {
64     $id = required_param('entryid', PARAM_INT);
65 }
67 if (empty($CFG->bloglevel)) {
68     print_error('blogdisable', 'blog');
69 }
71 if (isguestuser()) {
72     print_error('noguestentry', 'blog');
73 }
75 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
76 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
77     print_error('cannoteditentryorblog');
78 }
80 // Make sure that the person trying to edit has access right
81 if ($id) {
82     if (!$entry = new blog_entry($id)) {
83         print_error('wrongentryid', 'blog');
84     }
86     if (!blog_user_can_edit_entry($entry)) {
87         print_error('notallowedtoedit', 'blog');
88     }
89     $userid = $entry->userid;
90     $entry->subject      = clean_text($entry->subject);
91     $entry->summary      = clean_text($entry->summary, $entry->format);
92     
93 } else {
94     if (!has_capability('moodle/blog:create', $sitecontext)) {
95         print_error('noentry', 'blog'); // manageentries is not enough for adding
96     }
97     $entry  = new stdClass();
98     $entry->id = null;
99     $userid = $USER->id;
101 $returnurl->param('userid', $userid);
103 $strblogs = get_string('blogs','blog');
105 if ($action === 'delete'){
106     if (empty($entry->id)) {
107         print_error('wrongentryid', 'blog');
108     }
109     if (data_submitted() && $confirm && confirm_sesskey()) {
110         // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
111         if (!blog_user_can_edit_entry($entry)) {
112             print_error('nopermissionstodeleteentry', 'blog');
113         } else {
114             $entry->delete();
115             redirect($returnurl);
116         }
117     } else if (blog_user_can_edit_entry($entry)) {
118         $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
119         $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
120         $PAGE->set_title("$SITE->shortname: $strblogs");
121         $PAGE->set_heading($SITE->fullname);
122         echo $OUTPUT->header();
123         $entry->print_html();
124         echo '<br />';
125         echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
126         echo $OUTPUT->footer();
127         die;
128     }
129 } else if ($action == 'add') {
130     $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
131     $PAGE->set_heading($SITE->shortname);
132 } else if ($action == 'edit') {
133     $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
134     $PAGE->set_heading($SITE->shortname);
137 if (!empty($entry->id)) {
138     if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
140         foreach ($blogassociations as $assocrec) {
141             $contextrec = $DB->get_record('context', array('id' => $assocrec->contextid));
143             switch ($contextrec->contextlevel) {
144                 case CONTEXT_COURSE:
145                     $entry->courseassoc = $assocrec->contextid;
146                     break;
147                 case CONTEXT_MODULE:
148                     $entry->modassoc = $assocrec->contextid;
149                     break;
150             }
151         }
152     }
155 require_once('edit_form.php');
156 $summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext);
157 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
159 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
161 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog_post', $entry->id);
162 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog_attachment', $entry->id);
164 if (!empty($CFG->usetags) && !empty($entry->id)) {
165     include_once($CFG->dirroot.'/tag/lib.php');
166     $entry->tags = tag_get_tags_array('post', $entry->id);
169 $entry->action = $action;
170 // set defaults
171 $blogeditform->set_data($entry);
173 if ($blogeditform->is_cancelled()) {
174     redirect($returnurl);
176 } else if ($data = $blogeditform->get_data()){
178     switch ($action) {
179         case 'add':
180             $blogentry = new blog_entry(null, $data, $blogeditform);
181             $blogentry->add();
182             $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
183         break;
185         case 'edit':
186             if (empty($entry->id)) {
187                 print_error('wrongentryid', 'blog');
188             }
190             $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
191         break;
193         default :
194             print_error('invalidaction');
195     }
197     redirect($returnurl);
201 // gui setup
202 switch ($action) {
203     case 'add':
204         // prepare new empty form
205         $entry->publishstate = 'site';
206         $strformheading = get_string('addnewentry', 'blog');
207         $entry->action       = $action;
209         if ($CFG->useblogassociations) {
211             //pre-select the course for associations
212             if ($courseid) {
213                 $context = get_context_instance(CONTEXT_COURSE, $courseid);
214                 $entry->courseassoc = $context->id;
215             }
217             //pre-select the mod for associations
218             if ($modid) {
219                 $context = get_context_instance(CONTEXT_MODULE, $modid);
220                 $entry->modassoc = $context->id;
221             }
222         }
223         break;
225     case 'edit':
226         if (empty($entry->id)) {
227             print_error('wrongentryid', 'blog');
228         }
229         $entry->tags = tag_get_tags_array('post', $entry->id);
230         $strformheading = get_string('updateentrywithid', 'blog');
232         break;
234     default :
235         print_error('unknowaction');
238 $entry->modid = $modid;
239 $entry->courseid = $courseid;
241 echo $OUTPUT->header();
242 $blogeditform->display();
243 echo $OUTPUT->footer();
245 die;