Version: current

      1 <?php
      2 /**
      3  * Represents a single comment on a page
      4  *
      5  * @package cms
      6  * @subpackage comments
      7  */
      8 class PageComment extends DataObject {
      9
     10     static $db = array(
     11         "Name" => "Varchar(200)",
     12         "Comment" => "Text",
     13         "IsSpam" => "Boolean",
     14         "NeedsModeration" => "Boolean",
     15         "CommenterURL" => "Varchar(255)",
     16         "SessionID" => "Varchar(255)"
     17     );
     18
     19     static $has_one = array(
     20         "Parent" => "SiteTree",
     21         "Author" => "Member" // Only set when the user is logged in when posting
     22     );
     23
     24     static $has_many = array();
     25
     26     static $many_many = array();
     27
     28     static $defaults = array();
     29
     30     static $casting = array(
     31         "RSSTitle" => "Varchar",
     32     );
     33
     34     // Number of comments to show before paginating
     35     static $comments_per_page = 10;
     36
     37     static $moderate = false;
     38
     39     static $bbcode = false;
     40
     41     /**
     42      * Return a link to this comment
     43      * @return string link to this comment.
     44      */
     45     function Link() {
     46         return $this->Parent()->Link() . '#PageComment_'. $this->ID;
     47     }
     48
     49     function getRSSName() {
     50         if($this->Name) {
     51             return $this->Name;
     52         } elseif($this->Author()) {
     53             return $this->Author()->getName();
     54         }
     55     }
     56
     57     function ParsedBBCode(){
     58         $parser = new BBCodeParser($this->Comment);
     59         return $parser->parse();
     60     }
     61
     62     function DeleteLink() {
     63         return (Permission::check('CMS_ACCESS_CMSMain')) ? "PageComment_Controller/deletecomment/$this->ID" : false;
     64     }
     65
     66     function CommentTextWithLinks() {
     67         $pattern = '|([a-zA-Z]+://)([a-zA-Z0-9?&%.;:/=+_-]*)|is';
     68         $replace = '<a rel="nofollow" href="$1$2">$1$2</a>';
     69         return preg_replace($pattern, $replace, $this->Comment);
     70     }
     71
     72     function SpamLink() {
     73         return (Permission::check('CMS_ACCESS_CMSMain') && !$this->IsSpam) ? "PageComment_Controller/reportspam/$this->ID" : false;
     74     }
     75
     76     function HamLink() {
     77         return (Permission::check('CMS_ACCESS_CMSMain') && $this->IsSpam) ? "PageComment_Controller/reportham/$this->ID" : false;
     78     }
     79
     80     function ApproveLink() {
     81         return (Permission::check('CMS_ACCESS_CMSMain') && $this->NeedsModeration) ? "PageComment_Controller/approve/$this->ID" : false;
     82     }
     83
     84     function DisapproveLink() {
     85         return (Permission::check('CMS_ACCESS_CMSMain') && self::moderationEnabled() && !$this->NeedsModeration) ? "PageComment_Controller/disapprove/$this->ID" : false;
     86     }
     87
     88     function SpamClass() {
     89         if($this->getField('IsSpam')) {
     90             return 'spam';
     91         } else if($this->getField('NeedsModeration')) {
     92             return 'unmoderated';
     93         } else {
     94             return 'notspam';
     95         }
     96     }
     97
     98
     99     function RSSTitle() {
    100         return sprintf(
    101             _t('PageComment.COMMENTBY', "Comment by '%s' on %s", PR_MEDIUM, 'Name, Page Title'),
    102             Convert::raw2xml($this->getRSSName()),
    103             $this->Parent()->Title
    104         );
    105     }
    106
    107
    108
    109
    110     function PageTitle() {
    111         return $this->Parent()->Title;
    112     }
    113
    114     static function enableModeration() {
    115         self::$moderate = true;
    116     }
    117
    118     static function moderationEnabled() {
    119         return self::$moderate;
    120     }
    121
    122     static function enableBBCode() {
    123         self::$bbcode = true;
    124     }
    125
    126     static function bbCodeEnabled() {
    127         return self::$bbcode;
    128     }
    129
    130     /**
    131      *
    132      * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
    133      *
    134      */
    135     function fieldLabels($includerelations = true) {
    136         $labels = parent::fieldLabels($includerelations);
    137         $labels['Name'] = _t('PageComment.Name', 'Author Name');
    138         $labels['Comment'] = _t('PageComment.Comment', 'Comment');
    139         $labels['IsSpam'] = _t('PageComment.IsSpam', 'Spam?');
    140         $labels['NeedsModeration'] = _t('PageComment.NeedsModeration', 'Needs Moderation?');
    141
    142         return $labels;
    143     }
    144
    145     /**
    146      * This method is called just before this object is
    147      * written to the database.
    148      *
    149      * Specifically, make sure "http://" exists at the start
    150      * of the URL, if it doesn't have https:// or http://
    151      */
    152     public function onBeforeWrite() {
    153         parent::onBeforeWrite();
    154
    155         $url = $this->CommenterURL;
    156
    157         if($url) {
    158             if(strtolower(substr($url, 0, 8)) != 'https://' && strtolower(substr($url, 0, 7)) != 'http://') {
    159                 $this->CommenterURL = 'http://' . $url;
    160             }
    161         }
    162     }
    163
    164     function getFrontendFields($params = NULL) {
    165         $fields = parent::getFrontendFields($params);
    166         $fields->removeByName('SessionID');
    167         $fields->removeByName('ParentID');
    168         $fields->removeByName('AuthorID');
    169
    170         return $fields;
    171     }
    172 }
    173
    174
    175 /**
    176  * @package cms
    177  * @subpackage comments
    178  */
    179 class PageComment_Controller extends Controller {
    180     function rss() {
    181         $parentcheck = isset($_REQUEST['pageid']) ? "\"ParentID\" = " . (int) $_REQUEST['pageid'] : "\"ParentID\" > 0";
    182         $unmoderatedfilter = Permission::check('ADMIN') ? '' : "AND \"NeedsModeration\" = 0";
    183         $comments = DataObject::get("PageComment", "$parentcheck AND \"IsSpam\" = 0 $unmoderatedfilter", "\"Created\" DESC", "", 10);
    184         if(!isset($comments)) {
    185             $comments = new DataObjectSet();
    186         }
    187
    188         $rss = new RSSFeed($comments, "home/", "Page comments", "", "RSSTitle", "Comment", "RSSName");
    189         $rss->outputToBrowser();
    190     }
    191
    192     /**
    193      * Deletes all comments on the page referenced by the url param pageid
    194      */
    195     function deleteallcomments() {
    196         if(Permission::check('CMS_ACCESS_CMSMain')) {
    197             $pageId = $_REQUEST['pageid'];
    198             if(preg_match('/^\d+$/', $pageId)) {
    199                 $comments = DataObject::get("PageComment", "\"ParentID\" = $pageId");
    200                 if($comments) foreach($comments as $c) {
    201                     $c->delete();
    202                 }
    203             }
    204         }
    205
    206         if(Director::is_ajax()) {
    207             echo "";
    208         } else {
    209             Director::redirectBack();
    210         }
    211     }
    212
    213     function deletecomment() {
    214         if(Permission::check('CMS_ACCESS_CMSMain')) {
    215             $comment = DataObject::get_by_id("PageComment", $this->urlParams['ID']);
    216             if($comment) {
    217                 $comment->delete();
    218             }
    219         }
    220
    221         if(Director::is_ajax()) {
    222             echo "";
    223         } else {
    224             Director::redirectBack();
    225         }
    226     }
    227
    228     function approve() {
    229         if(Permission::check('CMS_ACCESS_CMSMain')) {
    230             $comment = DataObject::get_by_id("PageComment", $this->urlParams['ID']);
    231
    232             if($comment) {
    233                 $comment->NeedsModeration = false;
    234                 $comment->write();
    235
    236                 // @todo Report to spamprotecter this is true
    237
    238                 if(Director::is_ajax()) {
    239                     echo $comment->renderWith('PageCommentInterface_singlecomment');
    240                 } else {
    241                     Director::redirectBack();
    242                 }
    243             }
    244         }
    245     }
    246
    247     function disapprove() {
    248         if(Permission::check('CMS_ACCESS_CMSMain')) {
    249             $comment = DataObject::get_by_id("PageComment", $this->urlParams['ID']);
    250
    251             if($comment) {
    252                 $comment->NeedsModeration = true;
    253                 $comment->write();
    254
    255                 // @todo Report to spamprotecter this is true
    256
    257                 if(Director::is_ajax()) {
    258                     echo $comment->renderWith('PageCommentInterface_singlecomment');
    259                 } else {
    260                     Director::redirectBack();
    261                 }
    262             }
    263         }
    264     }
    265
    266     function reportspam() {
    267         $comment = DataObject::get_by_id("PageComment", $this->urlParams['ID']);
    268         if($comment) {
    269             // check they have access
    270             if(Permission::check('CMS_ACCESS_CMSMain')) {
    271
    272                 // if spam protection module exists
    273                 if(class_exists('SpamProtectorManager')) {
    274                     SpamProtectorManager::send_feedback($comment, 'spam');
    275                 }
    276
    277                 // If Akismet is enabled
    278                 else if(SSAkismet::isEnabled()) {
    279                     try {
    280                         $akismet = new SSAkismet();
    281                         $akismet->setCommentAuthor($comment->getField('Name'));
    282                         $akismet->setCommentContent($comment->getField('Comment'));
    283                         $akismet->submitSpam();
    284                     } catch (Exception $e) {
    285                         // Akismet didn't work, most likely the service is down.
    286                     }
    287                 }
    288
    289                 $comment->IsSpam = true;
    290                 $comment->NeedsModeration = false;
    291                 $comment->write();
    292             }
    293         }
    294         if(Director::is_ajax()) {
    295             if(SSAkismet::isEnabled() && SSAkismet::getSaveSpam()) {
    296                 echo $comment->renderWith('PageCommentInterface_singlecomment');
    297             } else {
    298                 echo '';
    299             }
    300         } else {
    301             Director::redirectBack();
    302         }
    303     }
    304     /**
    305      * Report a Spam Comment as valid comment (not spam)
    306      */
    307     function reportham() {
    308         $comment = DataObject::get_by_id("PageComment", $this->urlParams['ID']);
    309         if($comment) {
    310             if(Permission::check('CMS_ACCESS_CMSMain')) {
    311
    312                 // if spam protection module exists
    313                 if(class_exists('SpamProtectorManager')) {
    314                     SpamProtectorManager::send_feedback($comment, 'ham');
    315                 }
    316
    317                 if(SSAkismet::isEnabled()) {
    318                     try {
    319                         $akismet = new SSAkismet();
    320                         $akismet->setCommentAuthor($comment->getField('Name'));
    321                         $akismet->setCommentContent($comment->getField('Comment'));
    322                         $akismet->submitHam();
    323                     } catch (Exception $e) {
    324                         // Akismet didn't work, most likely the service is down.
    325                     }
    326                 }
    327                 $comment->setField('IsSpam', false);
    328                 $comment->write();
    329             }
    330         }
    331         if(Director::is_ajax()) {
    332             echo $comment->renderWith('PageCommentInterface_singlecomment');
    333         } else {
    334             Director::redirectBack();
    335         }
    336     }
    337
    338 }
    339
    340 ?>
    341

[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. -