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
235 $form->loadDataFrom(array(
236 "Name" => Cookie::get("PageCommentInterface_Name"),
237 "Comment" => Cookie::get("PageCommentInterface_Comment"),
238 "CommenterURL" => Cookie::get("PageCommentInterface_CommenterURL")
239 ));
240
241 return $form;
242 }
243
244 function () {
245
246 $limit = array();
247 $limit['start'] = isset($_GET['commentStart']) ? (int)$_GET['commentStart'] : 0;
248 $limit['limit'] = PageComment::$comments_per_page;
249
250 $spamfilter = isset($_GET['showspam']) ? '' : "AND \"IsSpam\" = 0";
251 $unmoderatedfilter = Permission::check('ADMIN') ? '' : "AND \"NeedsModeration\" = 0";
252 $order = self::$order_comments_by;
253 $comments = DataObject::get("PageComment", "\"ParentID\" = '" . Convert::raw2sql($this->page->ID) . "' $spamfilter $unmoderatedfilter", $order, "", $limit);
254
255 if(is_null($comments)) {
256 return;
257 }
258
259
260 $comments->setPaginationGetVar('commentStart');
261
262 return $comments;
263 }
264
265 function () {
266 return Director::absoluteBaseURL() . "PageComment/rss?pageid=" . $this->page->ID;
267 }
268
269 270 271 272
273 function DeleteAllLink() {
274 if(Permission::check('CMS_ACCESS_CMSMain')) {
275 return Director::absoluteBaseURL() . "PageComment/deleteallcomments?pageid=" . $this->page->ID;
276 }
277 }
278
279 }
280
281 282 283 284
285 class extends Form {
286 function ($data) {
287
288 Cookie::set("PageCommentInterface_Name", $data['Name']);
289 Cookie::set("PageCommentInterface_CommenterURL", $data['CommenterURL']);
290 Cookie::set("PageCommentInterface_Comment", $data['Comment']);
291
292 if(SSAkismet::isEnabled()) {
293 try {
294 $akismet = new SSAkismet();
295
296 $akismet->setCommentAuthor($data['Name']);
297 $akismet->setCommentContent($data['Comment']);
298
299 if($akismet->isCommentSpam()) {
300 if(SSAkismet::getSaveSpam()) {
301 $comment = Object::create('PageComment');
302 $this->saveInto($comment);
303 $comment->setField("IsSpam", true);
304 $comment->write();
305 }
306 echo "<b>"._t('PageCommentInterface_Form.SPAMDETECTED', 'Spam detected!!') . "</b><br /><br />";
307 printf("If you believe this was in error, please email %s.", ereg_replace("@", " _(at)_", Email::getAdminEmail()));
308 echo "<br /><br />"._t('PageCommentInterface_Form.MSGYOUPOSTED', 'The message you posted was:'). "<br /><br />";
309 echo $data['Comment'];
310
311 return;
312 }
313 } catch (Exception $e) {
314
315 }
316 }
317
318
319 if(MathSpamProtection::isEnabled()){
320 if(!MathSpamProtection::correctAnswer($data['Math'])){
321 if(!Director::is_ajax()) {
322 Director::redirectBack();
323 }
324 return "spamprotectionfailed";
325 }
326 }
327
328
329 $member = Member::currentUser();
330 if(PageCommentInterface::CanPostComment() && $member) {
331 $this->Fields()->push(new HiddenField("AuthorID", "Author ID", $member->ID));
332 } elseif(!PageCommentInterface::CanPostComment()) {
333 echo "You're not able to post comments to this page. Please ensure you are logged in and have an appropriate permission level.";
334 return;
335 }
336
337 $comment = Object::create('PageComment');
338 $this->saveInto($comment);
339
340
341 if($session = Session::get('mollom_user_session_id')) {
342 $comment->SessionID = $session;
343 Session::clear('mollom_user_session_id');
344 }
345 $comment->IsSpam = false;
346 $comment->NeedsModeration = PageComment::moderationEnabled();
347 $comment->write();
348
349 $this->extend('OnAfterPost', $comment, $data);
350
351 Cookie::set("PageCommentInterface_Comment", '');
352
353 $moderationMsg = _t('PageCommentInterface_Form.AWAITINGMODERATION', "Your comment has been submitted and is now awaiting moderation.");
354
355 if(Director::is_ajax()) {
356 if($comment->NeedsModeration){
357 echo $moderationMsg;
358 } else{
359 echo $comment->renderWith('PageCommentInterface_singlecomment');
360 }
361 } else {
362 if($comment->NeedsModeration){
363 $this->sessionMessage($moderationMsg, 'good');
364 }
365
366
367 if($comment->ParentID) {
368 $page = DataObject::get_by_id("Page", $comment->ParentID);
369 if($page) {
370
371 return Director::redirect($page->Link() . '#PageComment_' . $comment->ID);
372 }
373 }
374
375 return Director::redirectBack();
376 }
377 }
378 }
379
380 381 382 383
384 class extends ContentController {
385 function __construct() {
386 parent::__construct(null);
387 }
388
389 function newspamquestion() {
390 if(Director::is_ajax()) {
391 echo Convert::raw2xml(sprintf(_t('PageCommentInterface_Controller.SPAMQUESTION', "Spam protection question: %s"),MathSpamProtection::getMathQuestion()));
392 }
393 }
394 }
395
396 ?>
397
[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.
-