Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

[Solved] Help, modifying the moderations.controller.php to fix comment and discussion deletion*

motahcitymotahcity
edited November 2013 in Vanilla 2.0 - 2.8

I tried to change a permission to allow for multiple deletions of old posts. I made changes to class.moderationcontroller.php

Now It sends an error message: Parse error: syntax error, unexpected T_VARIABLE in /hermes/bosoraweb069/b284/sl.lionslair/public_html/Forum/applications/vanilla/controllers/class.moderationcontroller.php on line 325

So I use the "find" feature in Firefox and it shows 3 instances of vanilla.comment in this php file. Obviously I fubbed up, so I have copied the content of the file. Can we get this back to normal?

Text of class.moderationcontroller.php:

<?php if (!defined('APPLICATION')) exit();
/*
Copyright 2008, 2009 Vanilla Forums Inc.
This file is part of Garden.

*
* @since 2.0.18
* @package Vanilla
*/
class ModerationController extends VanillaController {

/**
* Looks at the user's attributes and form postback to see if any comments
* have been checked for administration, and if so, puts an inform message on
* the screen to take action.
*/
public function CheckedComments() {
$this->DeliveryType(DELIVERY_TYPE_BOOL);
$this->DeliveryMethod(DELIVERY_METHOD_JSON);
ModerationController::InformCheckedComments($this);
$this->Render();
}

/**
* Looks at the user's attributes and form postback to see if any discussions
* have been checked for administration, and if so, puts an inform message on
* the screen to take action.
*/
public function CheckedDiscussions() {
$this->DeliveryType(DELIVERY_TYPE_BOOL);
$this->DeliveryMethod(DELIVERY_METHOD_JSON);
ModerationController::InformCheckedDiscussions($this);
$this->Render();
}

/**
* Looks at the user's attributes and form postback to see if any comments
* have been checked for administration, and if so, adds an inform message to
* $Sender to take action.
*/
public static function InformCheckedComments($Sender) {
$Session = Gdn::Session();
$HadCheckedComments = FALSE;
$TransientKey = GetValue('TransientKey', $_POST);
if ($Session->IsValid() && $Session->ValidateTransientKey($TransientKey)) {
// Form was posted, so accept changes to checked items.
$DiscussionID = GetValue('DiscussionID', $_POST, 0);
$CheckIDs = GetValue('CheckIDs', $_POST);
if (empty($CheckIDs))
$CheckIDs = array();
$CheckIDs = (array)$CheckIDs;

     $CheckedComments = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedComments', array());
     if (!is_array($CheckedComments))
        $CheckedComments = array();

     if (!array_key_exists($DiscussionID, $CheckedComments)) {
        $CheckedComments[$DiscussionID] = array();
     } else {
        // Were there checked comments in this discussion before the form was posted?
        $HadCheckedComments = count($CheckedComments[$DiscussionID]) > 0; 
     }
     foreach ($CheckIDs as $Check) {
        if (GetValue('checked', $Check)) {
           if (!ArrayHasValue($CheckedComments, $Check['checkId']))
              $CheckedComments[$DiscussionID][] = $Check['checkId'];
        } else {
           RemoveValueFromArray($CheckedComments[$DiscussionID], $Check['checkId']);
        }
     }

     if (count($CheckedComments[$DiscussionID]) == 0)
        unset($CheckedComments[$DiscussionID]);

     Gdn::UserModel()->SaveAttribute($Session->User->UserID, 'CheckedComments', $CheckedComments);
  } else if ($Session->IsValid()) {
     // No form posted, just retrieve checked items for display
     $DiscussionID = property_exists($Sender, 'DiscussionID') ? $Sender->DiscussionID : 0;
     $CheckedComments = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedComments', array());
     if (!is_array($CheckedComments))
        $CheckedComments = array();

  }

  // Retrieve some information about the checked items
  $CountDiscussions = count($CheckedComments);
  $CountComments = 0;
  foreach ($CheckedComments as $DiscID => $Comments) {
     if ($DiscID == $DiscussionID)
        $CountComments += count($Comments); // Sum of comments in this discussion
  }
  if ($CountComments > 0) {
     $SelectionMessage =  Wrap(sprintf(
        'You have selected %1$s in this discussion.',
        Plural($CountComments, '%s comment', '%s comments')
     ), 'div');
     $ActionMessage = T('Take Action:');
     // Can the user delete the comment?
     $DiscussionModel = new DiscussionModel();
     $Discussion = $DiscussionModel->GetID($DiscussionID);
     if ($Session->CheckPermission('Vanilla.Comment.Delete', TRUE, 'Category', $Discussion->CategoryID))
        $ActionMessage .= ' '.Anchor(T('Delete'), 'vanilla/moderation/confirmcommentdeletes/'.$DiscussionID, 'Delete Popup');

     $Sender->EventArguments['SelectionMessage'] = &$SelectionMessage;
     $Sender->EventArguments['ActionMessage'] = &$ActionMessage;
     $Sender->EventArguments['Discussion'] = $Discussion;
     $Sender->FireEvent('BeforeCheckComments');
     $ActionMessage .= ' '.Anchor(T('Cancel'), 'vanilla/moderation/clearcommentselections/'.$DiscussionID.'/{TransientKey}/?Target={SelfUrl}', 'CancelAction');

     $Sender->InformMessage(
        $SelectionMessage
        .Wrap($ActionMessage, 'div', array('class' => 'Actions')),
        array(
           'CssClass' => 'NoDismiss',
           'id' => 'CheckSummary'
        )
     );
  } else if ($HadCheckedComments) {
     // Remove the message completely if there were previously checked comments in this discussion, but none now
     $Sender->InformMessage('', array('id' => 'CheckSummary'));
  }

}

«1

Comments

  • Options

    /**
    * Looks at the user's attributes and form postback to see if any discussions
    * have been checked for administration, and if so, adds an inform message to
    * $Sender to take action.
    */
    public static function InformCheckedDiscussions($Sender) {
    $Session = Gdn::Session();
    $HadCheckedDiscussions = FALSE;
    $TransientKey = GetValue('TransientKey', $_POST);
    if ($Session->IsValid() && $Session->ValidateTransientKey($TransientKey)) {
    // Form was posted, so accept changes to checked items.
    $CheckIDs = GetValue('CheckIDs', $_POST);
    if (empty($CheckIDs))
    $CheckIDs = array();
    $CheckIDs = (array)$CheckIDs;

         $CheckedDiscussions = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedDiscussions', array());
         if (!is_array($CheckedDiscussions))
            $CheckedDiscussions = array();
    
         // Were there checked discussions before the form was posted?
         $HadCheckedDiscussions = count($CheckedDiscussions) > 0;
    
         foreach ($CheckIDs as $Check) {
            if (GetValue('checked', $Check)) {
               if (!ArrayHasValue($CheckedDiscussions, $Check['checkId']))
                  $CheckedDiscussions[] = $Check['checkId'];
            } else {
               RemoveValueFromArray($CheckedDiscussions, $Check['checkId']);
            }
         }
    
         Gdn::UserModel()->SaveAttribute($Session->User->UserID, 'CheckedDiscussions', $CheckedDiscussions);
      } else if ($Session->IsValid()) {
         // No form posted, just retrieve checked items for display
         $CheckedDiscussions = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedDiscussions', array());
         if (!is_array($CheckedDiscussions))
            $CheckedDiscussions = array();
    
      }
    
      // Retrieve some information about the checked items
      $CountDiscussions = count($CheckedDiscussions);
      if ($CountDiscussions > 0) {
         $SelectionMessage =  Wrap(sprintf(
            'You have selected %1$s.',
            Plural($CountDiscussions, '%s discussion', '%s discussions')
         ), 'div');
         $ActionMessage = T('Take Action:');
         $ActionMessage .= ' '.Anchor(T('Delete'), 'vanilla/moderation/confirmdiscussiondeletes/', 'Delete Popup');
    
         $Sender->EventArguments['SelectionMessage'] = &$SelectionMessage;
         $Sender->EventArguments['ActionMessage'] = &$ActionMessage;
         $Sender->FireEvent('BeforeCheckDiscussions');
         $ActionMessage .= ' '.Anchor(T('Cancel'), 'vanilla/moderation/cleardiscussionselections/{TransientKey}/?Target={SelfUrl}', 'CancelAction');
    
         $Sender->InformMessage(
            $SelectionMessage
            .Wrap($ActionMessage, 'div', array('class' => 'Actions')),
            array(
               'CssClass' => 'NoDismiss',
               'id' => 'CheckSummary'
            )
         );
      } else if ($HadCheckedDiscussions) {
         // Remove the message completely if there were previously checked comments in this discussion, but none now
         $Sender->InformMessage('', array('id' => 'CheckSummary'));
      }
    

    }

    /**
    * Remove all comments checked for administration from the user's attributes.
    */
    public function ClearCommentSelections($DiscussionID = '', $TransientKey = '') {
    $Session = Gdn::Session();
    if ($Session->ValidateTransientKey($TransientKey)) {
    $CheckedComments = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedComments', array());
    unset($CheckedComments[$DiscussionID]);
    Gdn::UserModel()->SaveAttribute($Session->UserID, 'CheckedComments', $CheckedComments);
    }

      Redirect(GetIncomingValue('Target', '/discussions'));
    

    }

    /**
    * Remove all discussions checked for administration from the user's attributes.
    */
    public function ClearDiscussionSelections($TransientKey = '') {
    $Session = Gdn::Session();
    if ($Session->ValidateTransientKey($TransientKey))
    Gdn::UserModel()->SaveAttribute($Session->UserID, 'CheckedDiscussions', FALSE);

      Redirect(GetIncomingValue('Target', '/discussions'));
    

    }

    /**
    * Form to confirm that the administrator wants to delete the selected
    * comments (and has permission to do so).
    */
    public function ConfirmCommentDeletes($DiscussionID = '') {
    $Session = Gdn::Session();
    $this->Form = new Gdn_Form();
    $DiscussionModel = new DiscussionModel();
    $Discussion = $DiscussionModel->GetID($DiscussionID);
    if (!$Discussion)
    return;

      // Verify that the user has permission to perform the delete
      $this->Permission('Vanilla.Comments.Delete', TRUE, 'Category', $Discussion->CategoryID);
      $this->Title(T('Confirm'));
    
      $CheckedComments = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedComments', array());
      if (!is_array($CheckedComments))
         $CheckedComments = array();
    
      $CommentIDs = array();
      $DiscussionIDs = array();
      foreach ($CheckedComments as $DiscID => $Comments) {
         foreach ($Comments as $Comment) {
            if (substr($Comment, 0, 11) == 'Discussion_')
               $DiscussionIDs[] = str_replace('Discussion_', '', $Comment);
            else if ($DiscID == $DiscussionID)
               $CommentIDs[] = str_replace('Comment_', '', $Comment);
         }
      }
      $CountCheckedComments = count($CommentIDs);  
      $this->SetData('CountCheckedComments', $CountCheckedComments);
    
      if ($this->Form->AuthenticatedPostBack()) {
         // Delete the selected comments
         $CommentModel = new CommentModel();
         foreach ($CommentIDs as $CommentID) {
            $CommentModel->Delete($CommentID);
         }
    
         // Clear selections
         unset($CheckedComments[$DiscussionID]);
         Gdn::UserModel()->SaveAttribute($Session->UserID, 'CheckedComments', $CheckedComments);
         ModerationController::InformCheckedComments($this);
         $this->RedirectUrl = 'discussions';
      }
    
      $this->Render();
    

    }

    /**
    * Form to confirm that the administrator wants to delete the selected
    * discussions (and has permission to do so).
    */
    public function ConfirmDiscussionDeletes() {
    $Session = Gdn::Session();
    $this->Form = new Gdn_Form();
    $DiscussionModel = new DiscussionModel();

      // Verify that the user has permission to perform the deletes
      $this->Permission('Vanilla.Comments.Delete', TRUE, 'Category', 'any');
      $this->Title(T('Confirm'));
    
      $CheckedDiscussions = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedDiscussions', array());
      if (!is_array($CheckedDiscussions))
         $CheckedDiscussions = array();
    
      $DiscussionIDs = $CheckedDiscussions;
      $CountCheckedDiscussions = count($DiscussionIDs);  
      $this->SetData('CountCheckedDiscussions', $CountCheckedDiscussions);
    
      // Check permissions on each discussion to make sure the user has permission to delete them
      $AllowedDiscussions = array();
      $DiscussionData = $DiscussionModel->SQL->Select('DiscussionID, CategoryID')->From('Discussion')->WhereIn('DiscussionID', $DiscussionIDs)->Get();
      foreach ($DiscussionData->Result() as $Discussion) {
         if ($Session->CheckPermission('Vanilla.Discussions.Delete', TRUE, 'Category', $Discussion->CategoryID))
            $AllowedDiscussions[] = $Discussion->DiscussionID;
      }
      $this->SetData('CountAllowed', count($AllowedDiscussions));
      $CountNotAllowed = $CountCheckedDiscussions - count($AllowedDiscussions);
      $this->SetData('CountNotAllowed', $CountNotAllowed);
    
      if ($this->Form->AuthenticatedPostBack()) {
         // Delete the selected discussions (that the user has permission to delete).
         foreach ($AllowedDiscussions as $DiscussionID) {
            $DiscussionModel->Delete($DiscussionID);
         }
    
  • Options

    // Clear selections
    Gdn::UserModel()->SaveAttribute($Session->UserID, 'CheckedDiscussions', FALSE);
    ModerationController::InformCheckedDiscussions($this);
    $this->RedirectUrl = 'discussions';
    }_____

      $this->Render();
    

    }
    }

  • Options

    attaching the file ... thanks for helping with fat fingered faux pas

  • Options

    do you have a question? If so you should have posted it in the title instead of help.

    in any event what do you want? somebody to fix your changes. or a way to restore your files to normal?

    or are you providing an answer of some kind?

    you might post your changes in a zip format and upload them. since your formatting has gone awry.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    whu606whu606 I'm not a SuperHero; I just like wearing tights... MVP

    @motahcity

    Is there a reason you don't want to download another copy of Vanilla, and overwrite your 'fubbed' file with the 'unfubbed' version?

    Only, I would guess that would be the quickest fix.

  • Options

    No, I absolutely need help. I made some changes that probably shouldn't have been made. In the class.moderationcontroller.php file.

    Pretty limited as to the change(s) ... changed Vanilla.Comment.Delete(s) attribute. Some were plural, some not. I changed them all to plural, and now I get this error: Parse error: syntax error, unexpected T_VARIABLE in /hermes/bosoraweb069/b284/sl.lionslair/public_html/Forum/applications/vanilla/controllers/class.moderationcontroller.php on line 325

    If you can assist I'd be most grateful. Site is inoperative.

  • Options
    peregrineperegrine MVP
    edited November 2013

    that was my question does he want a fix to his own modifications. and the reason whay he has a syntax error. He probably has a missing ; or missing bracket some common syntax error

    or does he want to restore to original.

    this is a good reference for php error message tokens

    http://php.net/manual/en/tokens.php

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    @motahcity said:
    I made changes to class.moderationcontroller.php

    You shouldn't do that, but you've learned it the hard way right now, I assume...

    If you've made changes, than go back to the original version. If you are using 2.0.18.8, than this will be what you are looking for: https://raw.github.com/vanillaforums/Garden/2.0/applications/vanilla/controllers/class.moderationcontroller.php

  • Options

    Well, as long as I am getting attention in the Dunce Cap Corner I'll try again. Just want corrections made to the original. After I get the site working again I'll go to the server and do a backup, then upgrade Vanilla.

    Why was this done? Another member received an e-mail advising how to make a change. That went over like a turd in the party punch bowl.

    I'm an analogue dinosaur .. your help is appreciated.

  • Options
    peregrineperegrine MVP
    edited November 2013

    you've got dashes here


    should be


    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    That fixed it. I'd shout YOU ROCK!!!! but it wouldn't convey enough praise.

    Next bowl has your name on it, and if you're in the neighborhood you should stop in and enjoy it!

    Thanks.

  • Options
    peregrineperegrine MVP
    edited November 2013

    That fixed it.

    what fixed it. did you do? restore or change the code to remove ___

    try clicking on insightful or awesome once in a while too!

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    whu606whu606 I'm not a SuperHero; I just like wearing tights... MVP

    @motahcity said:

    Next bowl has your name on it,

    Hopefully, without the floater...

  • Options

    Removed the dashes, saved and reloaded the page. No error and the forum is running fine again.

  • Options

    No floaters ... but this is what was advised to an associate. Naturally, I fubbed it up.

    @whu606 said:
    (...) This (...) may well work (...)

      Line 291 of applications/vanilla/controllers/class.moderationcontroller.php (...)   $this->Permission('Vanilla.Comment.Delete', TRUE, 'Category', 'any');
    

    (...)
    $this->Permission('Vanilla.Comments.Delete', TRUE, 'Category', 'any');
    Making that change should do the trick.

  • Options
    whu606whu606 I'm not a SuperHero; I just like wearing tights... MVP
    edited November 2013

    Note my careful use of the 'may well'!

    The only change suggested was to try changing

    Vanilla.Comment

    to

    Vanilla.Comments

  • Options

    Yea, that was too easy. I need to take the difficult road, and when all is lost ... beg for help. : )

  • Options
    peregrineperegrine MVP
    edited November 2013

    you are referring to this maybe.

    http://vanillaforums.org/discussion/comment/177120/#Comment_177120

    since there is no permission regarding the field 'Vanilla.Comment.Delete' in the permission table.

    however in the permission table this field "Vanilla.Comments.Delete" exists.

    checking for a permission that doesn't exist in the permission table would be futile.

    so all you did or needed to do was correct the this-> permission field to march the correct permissions table column.

    just a bug in vanilla.

    so essentially any reference anywhere in the vanilla software that references a check permission setting for Vanilla.Comment.Delete needs to be change to Vanilla.Comments.Delete

    I noticed the problem in one of 422's premium themes also.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    Thank you, 'that' logic doesn't elude me. Happy you shared Peregrine.

    Messing with unfamiliar code is a no no, and I know better .... but that looked pretty simple. Line 291 ... gave me a map too.

    Maybe my trail marking bread crumbs had garlic on them?

  • Options
    peregrineperegrine MVP
    edited November 2013

    maybe a moderator can change the title of the thread.

    to something "Help, modifying the moderations.controller.php to fix comment and discussion deletion in 2.0.1.18"

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

Sign In or Register to comment.