1 <?php
2 3 4 5 6 7 8
9 class extends RequestHandler {
10 static $url_handlers = array(
11 '$Item!' => '$Item',
12 );
13 static $allowed_actions = array(
14 'PostCommentForm',
15 );
16
17 protected $controller, $methodName, $page;
18
19 20 21 22 23 24 25
26 static = false;
27
28 29 30 31 32 33 34
35 static = "";
36
37 38 39 40 41 42 43
44 static = true;
45
46 47 48 49 50 51 52 53 54
55 static = true;
56
57 58 59 60 61 62 63
64 static = "\"Created\" DESC";
65
66 67 68 69 70
71 static $required_fields = array();
72
73 74 75 76 77 78
79 function __construct($controller, $methodName, $page) {
80 $this->controller = $controller;
81 $this->methodName = $methodName;
82 $this->page = $page;
83 parent::__construct();
84 }
85
86 function Link() {
87 return Controller::join_links($this->controller->Link(), $this->methodName);
88 }
89
90 91 92 93 94
95 static function ($state) {
96 self::$comments_require_login = (boolean) $state;
97 }
98
99 100 101 102 103
104 static function ($permission) {
105 self::$comments_require_permission = $permission;
106 }
107
108 109 110 111 112
113 static function ($state) {
114 self::$show_comments_when_disabled = $state;
115 }
116
117 118 119 120 121
122 static function ($order) {
123 self::$order_comments_by = $order;
124 }
125
126 127 128 129 130
131 static function ($state) {
132 self::$use_ajax_commenting = $state;
133 }
134
135 function forTemplate() {
136 return $this->renderWith('PageCommentInterface');
137 }
138
139 140 141 142 143 144 145
146 static function () {
147 $member = Member::currentUser();
148 if(self::$comments_require_permission && $member && Permission::check(self::$comments_require_permission)) {
149 return true;
150 } elseif(self::$comments_require_login && $member && !self::$comments_require_permission) {
151 return true;
152 } elseif(!self::$comments_require_permission && !self::$comments_require_login) {
153 return true;
154 }
155
156 return false;
157 }
158
159 160 161 162 163 164 165
166 function PostingRequiresPermission() {
167 return self::$comments_require_permission;
168 }
169
170 function Page() {
171 return $this->page;
172 }
173
174 function () {
175 if(!$this->page->ProvideComments || !$this->page->allowComments()){
176 return false;
177 }
178 $fields = new FieldSet(
179 new HiddenField("ParentID", "ParentID", $this->page->ID)
180 );
181
182 $member = Member::currentUser();
183
184 if((self::$comments_require_login || self::$comments_require_permission) && $member && $member->FirstName) {
185
186
187
188 $fields->push(new ReadonlyField("NameView", _t('PageCommentInterface.YOURNAME', 'Your name'), $member->getName()));
189 $fields->push(new HiddenField("Name", "", $member->getName()));
190 } else {
191 $fields->push(new TextField("Name", _t('PageCommentInterface.YOURNAME', 'Your name')));
192 }
193
194
195 $fields->push(new TextField("CommenterURL", _t('PageCommentInterface.COMMENTERURL', "Your website URL")));
196
197 if(MathSpamProtection::isEnabled()){
198 $fields->push(new TextField("Math", sprintf(_t('PageCommentInterface.SPAMQUESTION', "Spam protection question: %s"), MathSpamProtection::getMathQuestion())));
199 }
200
201 $fields->push(new TextareaField("Comment", _t('PageCommentInterface.YOURCOMMENT', "Comments")));
202
203 $required_fields = self::$required_fields;
204 $siteConfig = SiteConfig::current_site_config();
205 if ($siteConfig->hasMethod('SiteAgreementField') && ($rulesField = $siteConfig->SiteAgreementField())) {
206 $fields->push($rulesField);
207 $required_fields[] = $rulesField->Name();
208 }
209 $validator = new RequiredFields($required_fields);
210
211 $form = new PageCommentInterface_Form($this, "PostCommentForm", $fields, new FieldSet(
212 new FormAction("postcomment", _t('PageCommentInterface.POST', 'Post'))
213 ), $validator);
214
215
216 $form->setRedirectToFormOnValidationError(true);
217
218
219 if(class_exists('SpamProtectorManager')) {
220 SpamProtectorManager::update_form($form, null, array('Name' => 'author_name', 'CommenterURL' => 'author_url', 'Comment' => 'post_body'),_t('PageCommentInterface.Captcha','SpamProtection'));
221 self::set_use_ajax_commenting(false);
222 }
223
224
225 if(self::$use_ajax_commenting) {
226 Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/behaviour/behaviour.js');
227 Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/prototype/prototype.js');
228 Requirements::javascript(THIRDPARTY_DIR . '/scriptaculous/effects.js');
229 Requirements::javascript(CMS_DIR . '/javascript/PageCommentInterface.js');
230 }
231
232 $this->extend('updatePageCommentForm', $form);
233
234 $form->loadDataFrom(array(
235 "Name" => Cookie::get("PageCommentInterface_Name"),
236 "Comment" => Cookie::get("PageCommentInterface_Comment"),
237 "CommenterURL" => Cookie::get("PageCommentInterface_CommenterURL")
238 ));
239
240 return $form;
241 }
242
243 function () {
244
245 $limit = array();
246 $limit['start'] = isset($_GET['commentStart']) ? (int)$_GET['commentStart'] : 0;
247 $limit['limit'] = PageComment::$comments_per_page;
248
249 $spamfilter = isset($_GET['showspam']) ? '' : "AND \"IsSpam\" = 0";
250 $unmoderatedfilter = Permission::check('ADMIN') ? '' : "AND \"NeedsModeration\" = 0";
251 $order = self::$order_comments_by;
252 $comments = DataObject::get("PageComment", "\"ParentID\" = '" . Convert::raw2sql($this->page->ID) . "' $spamfilter $unmoderatedfilter", $order, "", $limit);
253
254 if(is_null($comments)) {
255 return;
256 }
257
258
259 $comments->setPaginationGetVar('commentStart');
260
261 return $comments;
262 }
263
264 function () {
265 return Director::absoluteBaseURL() . "PageComment/rss?pageid=" . $this->page->ID;
266 }
267
268 269 270 271
272 function DeleteAllLink() {
273 if(Permission::check('CMS_ACCESS_CMSMain')) {
274 return Director::absoluteBaseURL() . "PageComment/deleteallcomments?pageid=" . $this->page->ID;
275 }
276 }
277
278 }
279
280 281 282 283
284 class extends Form {
285 function ($data) {
286
287 Cookie::set("PageCommentInterface_Name", $data['Name']);
288 Cookie::set("PageCommentInterface_CommenterURL", $data['CommenterURL']);
289 Cookie::set("PageCommentInterface_Comment", $data['Comment']);
290
291 if(SSAkismet::isEnabled()) {
292 try {
293 $akismet = new SSAkismet();
294
295 $akismet->setCommentAuthor($data['Name']);
296 $akismet->setCommentContent($data['Comment']);
297
298 if($akismet->isCommentSpam()) {
299 if(SSAkismet::getSaveSpam()) {
300 $comment = SS_Object::create('PageComment');
301 $this->saveInto($comment);
302 $comment->setField("IsSpam", true);
303 $comment->write();
304 }
305 echo "<b>"._t('PageCommentInterface_Form.SPAMDETECTED', 'Spam detected!!') . "</b><br /><br />";
306 printf("If you believe this was in error, please email %s.", preg_replace("!@!", " _(at)_", Email::getAdminEmail()));
307 echo "<br /><br />"._t('PageCommentInterface_Form.MSGYOUPOSTED', 'The message you posted was:'). "<br /><br />";
308 echo $data['Comment'];
309
310 return;
311 }
312 } catch (Exception $e) {
313
314 }
315 }
316
317
318 if(MathSpamProtection::isEnabled()){
319 if(!MathSpamProtection::correctAnswer($data['Math'])){
320 if(!Director::is_ajax()) {
321 Director::redirectBack();
322 }
323 return "spamprotectionfailed";
324 }
325 }
326
327
328 $member = Member::currentUser();
329 if(PageCommentInterface::CanPostComment() && $member) {
330 $this->Fields()->push(new HiddenField("AuthorID", "Author ID", $member->ID));
331 } elseif(!PageCommentInterface::CanPostComment()) {
332 echo "You're not able to post comments to this page. Please ensure you are logged in and have an appropriate permission level.";
333 return;
334 }
335
336 $comment = SS_Object::create('PageComment');
337 $this->saveInto($comment);
338
339
340 if($session = Session::get('mollom_user_session_id')) {
341 $comment->SessionID = $session;
342 Session::clear('mollom_user_session_id');
343 }
344 $comment->IsSpam = false;
345 $comment->NeedsModeration = PageComment::moderationEnabled();
346 $comment->write();
347
348 $this->extend('OnAfterPost', $comment, $data);
349 Cookie::set("PageCommentInterface_Comment", '');
350
351 $moderationMsg = _t('PageCommentInterface_Form.AWAITINGMODERATION', "Your comment has been submitted and is now awaiting moderation.");
352
353 if(Director::is_ajax()) {
354 if($comment->NeedsModeration){
355 echo $moderationMsg;
356 } else{
357 echo $comment->renderWith('PageCommentInterface_singlecomment');
358 }
359 } else {
360 if($comment->NeedsModeration){
361 $this->sessionMessage($moderationMsg, 'good');
362 }
363
364
365 if($comment->ParentID) {
366 $page = DataObject::get_by_id("Page", $comment->ParentID);
367 if($page) {
368
369 return Director::redirect($page->Link() . '#PageComment_' . $comment->ID);
370 }
371 }
372
373 return Director::redirectBack();
374 }
375 }
376 }
377
378 379 380 381
382 class extends ContentController {
383 function __construct() {
384 parent::__construct(null);
385 }
386
387 function newspamquestion() {
388 if(Director::is_ajax()) {
389 echo Convert::raw2xml(sprintf(_t('PageCommentInterface_Controller.SPAMQUESTION', "Spam protection question: %s"),MathSpamProtection::getMathQuestion()));
390 }
391 }
392 }
393
394 ?>
395
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-