2007-07-19 12:40:05 +02:00
< ? php
/**
* Represents an interface for viewing and adding page comments
* Create one , passing the page discussed to the constructor . It can then be
* inserted into a template .
2008-02-25 03:10:37 +01:00
* @ package cms
* @ subpackage comments
2007-07-19 12:40:05 +02:00
*/
2008-10-30 23:03:32 +01:00
class PageCommentInterface extends RequestHandler {
2008-08-15 05:37:26 +02:00
static $url_handlers = array (
'$Item!' => '$Item' ,
);
static $allowed_actions = array (
'PostCommentForm' ,
);
2007-07-19 12:40:05 +02:00
protected $controller , $methodName , $page ;
2008-08-12 04:59:27 +02:00
/**
* @ var boolean If this is true , you must be logged in to post a comment
* ( and therefore , you don 't need to specify a ' Your name ' field unless
* your name is blank )
*/
static $comments_require_login = false ;
/**
* @ var string If this is a valid permission code , you must be logged in
* and have the appropriate permission code on your account before you can
* post a comment .
*/
static $comments_require_permission = " " ;
2007-07-19 12:40:05 +02:00
/**
* Create a new page comment interface
2008-08-12 04:59:27 +02:00
* @ param controller The controller that the interface is used on
2007-07-19 12:40:05 +02:00
* @ param methodName The method to return this PageCommentInterface object
* @ param page The page that we ' re commenting on
*/
function __construct ( $controller , $methodName , $page ) {
$this -> controller = $controller ;
$this -> methodName = $methodName ;
$this -> page = $page ;
}
2008-08-15 05:37:26 +02:00
function Link () {
return Controller :: join_links ( $this -> controller -> Link (), $this -> methodName );
}
2008-08-12 04:59:27 +02:00
/**
* See @ link PageCommentInterface :: $comments_require_login
* @ param boolean state The new state of this static field
*/
static function set_comments_require_login ( $state ) {
self :: $comments_require_login = ( boolean ) $state ;
}
/**
* See @ link PageCommentInterface :: $comments_require_permission
* @ param string permission The permission to check against .
*/
static function set_comments_require_permission ( $permission ) {
self :: $comments_require_permission = $permission ;
}
2007-07-19 12:40:05 +02:00
function forTemplate () {
return $this -> renderWith ( 'PageCommentInterface' );
}
2008-08-12 04:59:27 +02:00
/**
* @ return boolean true if the currently logged in user can post a comment ,
* false if they can ' t . Users can post comments by default , enforce
* security by using
* @ link PageCommentInterface :: set_comments_require_login () and
* @ link { PageCommentInterface :: set_comments_require_permission ()} .
*/
static function CanPostComment () {
$member = Member :: currentUser ();
if ( self :: $comments_require_permission && $member && Permission :: check ( self :: $comments_require_permission )) {
return true ; // Comments require a certain permission, and the user has the correct permission
} elseif ( self :: $comments_require_login && $member && ! self :: $comments_require_permission ) {
return true ; // Comments only require that a member is logged in
} elseif ( ! self :: $comments_require_permission && ! self :: $comments_require_login ) {
return true ; // Comments don't require anything - anyone can add a comment
}
return false ;
}
/**
* @ return boolean true if this page comment form requires users to have a
* valid permission code in order to post ( used to customize the error
* message ) .
*/
function PostingRequiresPermission () {
return self :: $comments_require_permission ;
}
function Page () {
return $this -> page ;
}
2007-07-19 12:40:05 +02:00
function PostCommentForm () {
2008-09-29 20:49:35 +02:00
Requirements :: javascript ( THIRDPARTY_DIR . '/behaviour.js' );
Requirements :: javascript ( THIRDPARTY_DIR . '/prototype.js' );
Requirements :: javascript ( THIRDPARTY_DIR . '/scriptaculous/effects.js' );
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63175 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-28 15:12:20 +02:00
Requirements :: javascript ( CMS_DIR . '/javascript/PageCommentInterface.js' );
2007-07-19 12:40:05 +02:00
2007-07-30 00:56:16 +02:00
$fields = new FieldSet (
2008-08-12 04:59:27 +02:00
new HiddenField ( " ParentID " , " ParentID " , $this -> page -> ID )
);
$member = Member :: currentUser ();
if (( self :: $comments_require_login || self :: $comments_require_permission ) && $member && $member -> FirstName ) {
2008-08-18 02:10:26 +02:00
// note this was a ReadonlyField - which displayed the name in a span as well as the hidden field but
// it was not saving correctly. Have changed it to a hidden field. It passes the data correctly but I
// believe the id of the form field is wrong.
$fields -> push ( new ReadonlyField ( " NameView " , _t ( 'PageCommentInterface.YOURNAME' , 'Your name' ), $member -> getName ()));
$fields -> push ( new HiddenField ( " Name " , " " , $member -> getName ()));
2008-08-12 04:59:27 +02:00
} else {
$fields -> push ( new TextField ( " Name " , _t ( 'PageCommentInterface.YOURNAME' , 'Your name' )));
}
2007-08-14 06:39:29 +02:00
2007-07-30 00:56:16 +02:00
if ( MathSpamProtection :: isEnabled ()){
2008-02-25 03:10:37 +01:00
$fields -> push ( new TextField ( " Math " , sprintf ( _t ( 'PageCommentInterface.SPAMQUESTION' , " Spam protection question: %s " ), MathSpamProtection :: getMathQuestion ())));
2007-07-30 00:56:16 +02:00
}
2008-02-25 03:10:37 +01:00
$fields -> push ( new TextareaField ( " Comment " , _t ( 'PageCommentInterface.YOURCOMMENT' , " Comments " )));
2007-07-30 00:56:16 +02:00
2008-08-15 05:37:26 +02:00
$form = new PageCommentInterface_Form ( $this , " PostCommentForm " , $fields , new FieldSet (
2008-02-25 03:10:37 +01:00
new FormAction ( " postcomment " , _t ( 'PageCommentInterface.POST' , 'Post' ))
2007-07-19 12:40:05 +02:00
));
$form -> loadDataFrom ( array (
" Name " => Cookie :: get ( " PageCommentInterface_Name " ),
));
return $form ;
}
function Comments () {
// Comment limits
if ( isset ( $_GET [ 'commentStart' ])) {
$limit = ( int ) $_GET [ 'commentStart' ] . " , " . PageComment :: $comments_per_page ;
} else {
$limit = " 0, " . PageComment :: $comments_per_page ;
}
2007-08-10 06:33:02 +02:00
$spamfilter = isset ( $_GET [ 'showspam' ]) ? '' : 'AND IsSpam=0' ;
$unmoderatedfilter = Permission :: check ( 'ADMIN' ) ? '' : 'AND NeedsModeration = 0' ;
$comments = DataObject :: get ( " PageComment " , " ParentID = ' " . Convert :: raw2sql ( $this -> page -> ID ) . " ' $spamfilter $unmoderatedfilter " , " Created DESC " , " " , $limit );
2007-07-19 12:40:05 +02:00
if ( is_null ( $comments )) {
return ;
}
// This allows us to use the normal 'start' GET variables as well (In the weird circumstance where you have paginated comments AND something else paginated)
$comments -> setPaginationGetVar ( 'commentStart' );
return $comments ;
}
2007-08-07 06:59:19 +02:00
function CommentRssLink () {
return Director :: absoluteBaseURL () . " PageComment/rss?pageid= " . $this -> page -> ID ;
}
2007-07-19 12:40:05 +02:00
}
2008-02-25 03:10:37 +01:00
/**
* @ package cms
* @ subpackage comments
*/
2007-07-19 12:40:05 +02:00
class PageCommentInterface_Form extends Form {
function postcomment ( $data ) {
// Spam filtering
if ( SSAkismet :: isEnabled ()) {
try {
$akismet = new SSAkismet ();
$akismet -> setCommentAuthor ( $data [ 'Name' ]);
$akismet -> setCommentContent ( $data [ 'Comment' ]);
if ( $akismet -> isCommentSpam ()) {
if ( SSAkismet :: getSaveSpam ()) {
$comment = Object :: create ( 'PageComment' );
$this -> saveInto ( $comment );
$comment -> setField ( " IsSpam " , true );
$comment -> write ();
}
2008-02-25 03:10:37 +01:00
echo " <b> " . _t ( 'PageCommentInterface_Form.SPAMDETECTED' , 'Spam detected!!' ) . " </b><br /><br /> " ;
printf ( " If you believe this was in error, please email %s. " , ereg_replace ( " @ " , " _(at)_ " , Email :: getAdminEmail ()));
echo " <br /><br /> " . _t ( 'PageCommentInterface_Form.MSGYOUPOSTED' , 'The message you posted was:' ) . " <br /><br /> " ;
2007-07-19 12:40:05 +02:00
echo $data [ 'Comment' ];
return ;
}
} catch ( Exception $e ) {
// Akismet didn't work, continue without spam check
}
}
2007-07-30 00:56:16 +02:00
//check if spam question was right.
2007-08-14 06:39:29 +02:00
if ( MathSpamProtection :: isEnabled ()){
2007-07-30 00:56:16 +02:00
if ( ! MathSpamProtection :: correctAnswer ( $data [ 'Math' ])){
2007-08-14 06:39:29 +02:00
if ( ! Director :: is_ajax ()) {
2007-08-08 06:34:12 +02:00
Director :: redirectBack ();
}
2008-08-12 04:59:27 +02:00
return " spamprotectionfailed " ; //used by javascript for checking if the spam question was wrong
2007-07-30 00:56:16 +02:00
}
}
2007-07-19 12:40:05 +02:00
Cookie :: set ( " PageCommentInterface_Name " , $data [ 'Name' ]);
2008-08-12 04:59:27 +02:00
// If commenting can only be done by logged in users, make sure the user is logged in
$member = Member :: currentUser ();
if ( PageCommentInterface :: CanPostComment () && $member ) {
$this -> Fields () -> push ( new HiddenField ( " AuthorID " , " Author ID " , $member -> ID ));
} elseif ( ! PageCommentInterface :: CanPostComment ()) {
echo " You're not able to post comments to this page. Please ensure you are logged in and have an appropriate permission level. " ;
return ;
}
2007-07-19 12:40:05 +02:00
$comment = Object :: create ( 'PageComment' );
$this -> saveInto ( $comment );
$comment -> IsSpam = false ;
2007-08-10 03:29:09 +02:00
$comment -> NeedsModeration = PageComment :: moderationEnabled ();
$comment -> write ();
2007-08-08 01:18:30 +02:00
2007-07-19 12:40:05 +02:00
if ( Director :: is_ajax ()) {
2007-08-14 06:39:29 +02:00
if ( $comment -> NeedsModeration ){
2008-10-03 11:54:59 +02:00
echo _t ( 'PageCommentInterface_Form.AWAITINGMODERATION' , " Your comment has been submitted and is now awaiting moderation. " );
2007-08-14 06:39:29 +02:00
} else {
echo $comment -> renderWith ( 'PageCommentInterface_singlecomment' );
}
2007-07-19 12:40:05 +02:00
} else {
Director :: redirectBack ();
}
}
}
2008-02-25 03:10:37 +01:00
/**
* @ package cms
* @ subpackage comments
*/
2007-08-14 06:39:29 +02:00
class PageCommentInterface_Controller extends ContentController {
function __construct () {
parent :: __construct ( null );
}
function newspamquestion () {
2007-08-17 05:09:46 +02:00
if ( Director :: is_ajax ()) {
2008-02-25 03:10:37 +01:00
echo Convert :: raw2xml ( sprintf ( _t ( 'PageCommentInterface_Controller.SPAMQUESTION' , " Spam protection question: %s " ), MathSpamProtection :: getMathQuestion ()));
2007-08-14 06:39:29 +02:00
}
}
}
2008-12-04 23:41:34 +01:00
?>