Skip to content

Instantly share code, notes, and snippets.

@typhonius
Created February 26, 2013 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typhonius/5036726 to your computer and use it in GitHub Desktop.
Save typhonius/5036726 to your computer and use it in GitHub Desktop.
What is essentially a prebuilt module that allows the user to create tokens (perhaps to be sent to an email address) which contain links they can click to delete spammy comments without logging in. Necessity for this was born out of receiving "new comment" notifications on my phone and then being annoyed about having to log in on a device that I…
<?php
/**
* Implements hook_menu()
*/
function mymodule_menu() {
$items['comment/%/fastdelete/%'] = array(
'title' => 'Fast Comment Deletion',
'page callback' => 'mymodule_comment_fastdelete',
'page arguments' => array(1, 3),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Authentication function for menu callback determining if the
* comment should be deleted
*/
function mymodule_comment_fastdelete($cid, $hash) {
$comment = comment_load($cid);
// First check to see if the comment actually exists
if ($comment) {
// Add in a timeout so the comment can be deleted only in the
// first 24 hours after posting.
$timeout = variable_get('user_password_reset_timeout', 86400);
$current = REQUEST_TIME;
if ($current - $timeout > $comment->created) {
drupal_set_message(t('You have tried to use a comment delete link that has expired. To have the comment deleted please contact the site administrator.'), 'warning');
drupal_goto('contact-me');
}
else {
// Load part of the user object of the node author for a secret string to send to user_pass_rehash
$author = mymodule_node_author_pass_from_cid($cid);
if ($hash == user_pass_rehash($cid, $comment->created, $author->pass) && $current >= $comment->created) {
watchdog('mymodule', 'Comment Autodelete link used', array(), WATCHDOG_NOTICE);
comment_delete($cid);
drupal_set_message('Comment successfully deleted!');
drupal_goto('node/' . $comment->nid);
}
else {
drupal_set_message('You have tried to use an invalid comment deletion link.', 'warning');
drupal_goto('node/' . $comment->nid);
}
}
}
else {
drupal_set_message('You have tried to use an invalid comment deletion link.', 'warning');
drupal_goto('');
}
}
/**
* Generates the deletion link for a specific comment.
*/
function mymodule_comment_fastdelete_link($cid) {
$comment = comment_load($cid);
$author = mymodule_node_author_pass_from_cid($cid);
// Combine a number of variables to construct a private hash that will be validated in order to delete the comment.
return url("comment/$cid/fastdelete/" . user_pass_rehash($cid, $comment->created, $author->pass), array('absolute' => TRUE));
}
/**
* Returns the hashed password of the node author the comment is posted on.
* Used for an unknown part of the hash that an anonymous user could not guess
*/
function mymodule_node_author_pass_from_cid($cid) {
$result = db_query('SELECT u.pass FROM {comment} c JOIN {node} n on n.nid = c.nid JOIN {users} u ON n.uid = u.uid WHERE c.cid = :cid', array(':cid' => $cid));
return $result->fetchObject();
}
/**
* Implements hook_token_info_alter()
*/
function mymodule_token_info_alter(&$data) {
$data['tokens']['comment']['comment_fastdelete_link'] = array(
'name' => t("Comment Delete Link"),
'description' => t("A link to immediately delete a comment."),
);
}
/**
* Implements hook_tokens()
*
*/
function mymodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'comment') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'comment_fastdelete_link':
$cid = $data['comment']->cid;
$link = mymodule_comment_fastdelete_link($cid);
if (isset($cid)) {
$replacements[$original] = $link;
}
else {
$replacements[$original] = '';
}
break;
}
}
}
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment