MDL-21769 fixed input validation
[moodle.git] / blog / edit.php
CommitLineData
cae83708 1<?php
2
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/>.
4a173181 17
cae83708 18
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 */
0a941490 27require_once(dirname(dirname(__FILE__)).'/config.php');
4a173181 28include_once('lib.php');
cae83708 29include_once('locallib.php');
eccfc1ca 30
e96f2a77 31$action = required_param('action', PARAM_ALPHA);
9366362a 32$id = optional_param('entryid', 0, PARAM_INT);
e96f2a77 33$confirm = optional_param('confirm', 0, PARAM_BOOL);
1c7b8b93
NC
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
e96f2a77 36
a6855934 37$PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
cae83708 38
1c7b8b93
NC
39// If action is add, we ignore $id to avoid any further problems
40if (!empty($id) && $action == 'add') {
41 $id = null;
42}
43
a6855934 44$returnurl = new moodle_url('/blog/index.php');
1c7b8b93
NC
45
46if (!empty($courseid) && empty($modid)) {
47 $returnurl->param('courseid', $courseid);
48 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
49}
50
51// If a modid is given, guess courseid
52if (!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}
58
59$blogheaders = blog_get_headers();
b0e90a0c 60
673bc55d 61require_login($courseid);
eccfc1ca 62
9366362a 63if ($action == 'edit') {
64 $id = required_param('entryid', PARAM_INT);
65}
66
ab2f17b0 67if (empty($CFG->bloglevel)) {
fae11dca 68 print_error('blogdisable', 'blog');
ab2f17b0 69}
70
ec7c4f11 71if (isguestuser()) {
cae83708 72 print_error('noguestentry', 'blog');
4a173181 73}
74
d9d16e56 75$sitecontext = get_context_instance(CONTEXT_SYSTEM);
cae83708 76if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
77 print_error('cannoteditentryorblog');
4a173181 78}
79
1c7b8b93 80// Make sure that the person trying to edit has access right
e96f2a77 81if ($id) {
1c7b8b93 82 if (!$entry = new blog_entry($id)) {
cae83708 83 print_error('wrongentryid', 'blog');
4a173181 84 }
6524adcf 85
1c7b8b93 86 if (!blog_user_can_edit_entry($entry)) {
5a2a5331 87 print_error('notallowedtoedit', 'blog');
e96f2a77 88 }
1c7b8b93 89 $userid = $entry->userid;
32148582
PS
90 $entry->subject = clean_text($entry->subject);
91 $entry->summary = clean_text($entry->summary, $entry->format);
92
4a173181 93} else {
e96f2a77 94 if (!has_capability('moodle/blog:create', $sitecontext)) {
cae83708 95 print_error('noentry', 'blog'); // manageentries is not enough for adding
e96f2a77 96 }
1c7b8b93
NC
97 $entry = new stdClass();
98 $entry->id = null;
99 $userid = $USER->id;
cae83708 100}
1c7b8b93 101$returnurl->param('userid', $userid);
48e79fd1 102
e96f2a77 103$strblogs = get_string('blogs','blog');
4a173181 104
45df7de3 105if ($action === 'delete'){
1c7b8b93 106 if (empty($entry->id)) {
cae83708 107 print_error('wrongentryid', 'blog');
f07b9627 108 }
cae83708 109 if (data_submitted() && $confirm && confirm_sesskey()) {
1c7b8b93
NC
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)) {
9366362a 118 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
1c7b8b93 119 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
e640790e 120 $PAGE->set_title("$SITE->shortname: $strblogs");
121 $PAGE->set_heading($SITE->fullname);
122 echo $OUTPUT->header();
1c7b8b93 123 $entry->print_html();
f07b9627 124 echo '<br />';
7d00c7bc 125 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
033e4aff 126 echo $OUTPUT->footer();
f07b9627 127 die;
128 }
1c7b8b93
NC
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);
f07b9627 135}
6524adcf 136
1c7b8b93
NC
137if (!empty($entry->id)) {
138 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
cae83708 139
140 foreach ($blogassociations as $assocrec) {
141 $contextrec = $DB->get_record('context', array('id' => $assocrec->contextid));
142
143 switch ($contextrec->contextlevel) {
144 case CONTEXT_COURSE:
1c7b8b93 145 $entry->courseassoc = $assocrec->contextid;
cae83708 146 break;
147 case CONTEXT_MODULE:
1c7b8b93 148 $entry->modassoc = $assocrec->contextid;
cae83708 149 break;
150 }
151 }
152 }
153}
154
1c7b8b93
NC
155require_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);
158
159$blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
cae83708 160
1c7b8b93
NC
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);
163
164if (!empty($CFG->usetags) && !empty($entry->id)) {
165 include_once($CFG->dirroot.'/tag/lib.php');
166 $entry->tags = tag_get_tags_array('post', $entry->id);
167}
b73d1ca4 168
1c7b8b93
NC
169$entry->action = $action;
170// set defaults
171$blogeditform->set_data($entry);
f07b9627 172
1c7b8b93 173if ($blogeditform->is_cancelled()) {
f07b9627 174 redirect($returnurl);
cae83708 175
1c7b8b93
NC
176} else if ($data = $blogeditform->get_data()){
177
f07b9627 178 switch ($action) {
179 case 'add':
1c7b8b93 180 $blogentry = new blog_entry(null, $data, $blogeditform);
b73d1ca4 181 $blogentry->add();
1c7b8b93 182 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
f07b9627 183 break;
184
185 case 'edit':
1c7b8b93 186 if (empty($entry->id)) {
cae83708 187 print_error('wrongentryid', 'blog');
e96f2a77 188 }
1c7b8b93
NC
189
190 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
f07b9627 191 break;
1c7b8b93 192
f07b9627 193 default :
fae11dca 194 print_error('invalidaction');
f07b9627 195 }
1c7b8b93 196
f07b9627 197 redirect($returnurl);
198}
48e79fd1 199
f07b9627 200
201// gui setup
202switch ($action) {
203 case 'add':
204 // prepare new empty form
cae83708 205 $entry->publishstate = 'site';
e96f2a77 206 $strformheading = get_string('addnewentry', 'blog');
cae83708 207 $entry->action = $action;
208
1c7b8b93
NC
209 if ($CFG->useblogassociations) {
210
211 //pre-select the course for associations
212 if ($courseid) {
213 $context = get_context_instance(CONTEXT_COURSE, $courseid);
214 $entry->courseassoc = $context->id;
215 }
cae83708 216
1c7b8b93
NC
217 //pre-select the mod for associations
218 if ($modid) {
219 $context = get_context_instance(CONTEXT_MODULE, $modid);
220 $entry->modassoc = $context->id;
221 }
cae83708 222 }
223 break;
4a173181 224
e96f2a77 225 case 'edit':
1c7b8b93 226 if (empty($entry->id)) {
cae83708 227 print_error('wrongentryid', 'blog');
e96f2a77 228 }
1c7b8b93 229 $entry->tags = tag_get_tags_array('post', $entry->id);
e96f2a77 230 $strformheading = get_string('updateentrywithid', 'blog');
4a173181 231
cae83708 232 break;
1c7b8b93 233
f07b9627 234 default :
fae11dca 235 print_error('unknowaction');
4a173181 236}
237
cae83708 238$entry->modid = $modid;
239$entry->courseid = $courseid;
e640790e 240
241echo $OUTPUT->header();
1d284fbd 242$blogeditform->display();
033e4aff 243echo $OUTPUT->footer();
e96f2a77 244
245die;