| 1 |
<?php
|
| 2 |
// $Id: modr8.module,v 1.25 2008/10/21 15:14:07 pwolanin Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Easy dedicated content moderation
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* A random value that should not conflict with core or other modules.
|
| 11 |
*/
|
| 12 |
|
| 13 |
define('NODE_BUILD_MODR8_TEASER', 1242526499);
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_help().
|
| 17 |
*/
|
| 18 |
function modr8_help($path, $arg) {
|
| 19 |
switch ($path) {
|
| 20 |
case 'admin/help#modr8':
|
| 21 |
return '<p>'. t("Easy, dedicated moderation of content. Assign the 'moderate content' permission to one or mode user roles. Set up the default moderation option for each node type.") .'</p>';
|
| 22 |
// OPTIONAL: Add additional cases for other paths that should display help text.
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* An access function for Moderation Menu Tab
|
| 28 |
*/
|
| 29 |
function modr8_moderation_access($nid) {
|
| 30 |
return user_access('moderate content') && db_result(db_query("SELECT COUNT(*) FROM {modr8_log} ml WHERE ml.nid = %d", $nid));
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_menu().
|
| 35 |
*/
|
| 36 |
function modr8_menu() {
|
| 37 |
$items = array();
|
| 38 |
|
| 39 |
$items['admin/content/modr8'] = array(
|
| 40 |
'title' => 'Moderated content',
|
| 41 |
'description' => 'Approve or delete moderated content.',
|
| 42 |
'access callback' => 'user_access',
|
| 43 |
'access arguments' => array('moderate content'),
|
| 44 |
'page callback' => 'modr8_page',
|
| 45 |
'file' => 'modr8_admin.inc',
|
| 46 |
);
|
| 47 |
$items['admin/reports/modr8'] = array(
|
| 48 |
'title' => 'Content moderation log',
|
| 49 |
'description' => 'Show log of all actions on moderated content.',
|
| 50 |
'access callback' => 'user_access',
|
| 51 |
'access arguments' => array('moderate content'),
|
| 52 |
'page callback' => 'modr8_log_view',
|
| 53 |
'file' => 'modr8_admin.inc',
|
| 54 |
);
|
| 55 |
$items['admin/settings/modr8'] = array(
|
| 56 |
'title' => 'Modr8 settings',
|
| 57 |
'description' => 'Configure content moderation.',
|
| 58 |
'page callback' => 'modr8_settings',
|
| 59 |
'access callback' => 'user_access',
|
| 60 |
'access arguments' => array('administer site configuration'),
|
| 61 |
'file' => 'modr8_admin.inc',
|
| 62 |
);
|
| 63 |
$items['node/%/modr8'] = array(
|
| 64 |
'title' => 'Moderation',
|
| 65 |
'page callback' => 'modr8_log_view',
|
| 66 |
'page arguments' => array('node', 1),
|
| 67 |
'access callback' => 'modr8_moderation_access',
|
| 68 |
'access arguments' => array(1),
|
| 69 |
'file' => 'modr8_admin.inc',
|
| 70 |
'weight' => 10,
|
| 71 |
'type' => MENU_LOCAL_TASK
|
| 72 |
);
|
| 73 |
$items['node/%node/log/response/%'] = array(
|
| 74 |
'title' => 'Moderation response',
|
| 75 |
'page callback' => 'modr8_response_page',
|
| 76 |
'page arguments' => array(1),
|
| 77 |
'access callback' => 'modr8_response_access',
|
| 78 |
'access arguments' => array(1, 4),
|
| 79 |
'file' => 'modr8_admin.inc',
|
| 80 |
'type' => MENU_CALLBACK,
|
| 81 |
);
|
| 82 |
|
| 83 |
return $items;
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Access callback.
|
| 88 |
*/
|
| 89 |
function modr8_response_access($node, $token) {
|
| 90 |
|
| 91 |
return ($token == modr8_response_token($node->nid, $node->uid));
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Generate a token for responding to a node in moderation.
|
| 96 |
*
|
| 97 |
* Calculates a HMAC-MD5 according to RFC2104 (http://www.ietf.org/rfc/rfc2104.txt).
|
| 98 |
*/
|
| 99 |
function modr8_response_token($nid, $uid) {
|
| 100 |
$key = md5(drupal_get_private_key() .'modr8token');
|
| 101 |
|
| 102 |
return bin2hex(
|
| 103 |
pack("H*", md5((str_pad($key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) .
|
| 104 |
pack("H*", md5((str_pad($key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) .
|
| 105 |
$nid .':'. $key .':'. $uid))))
|
| 106 |
);
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Implementation of hook_perm().
|
| 111 |
*/
|
| 112 |
function modr8_perm() {
|
| 113 |
return array('moderate content');
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* menu callback for settings form.
|
| 118 |
*/
|
| 119 |
function modr8_settings() {
|
| 120 |
|
| 121 |
return drupal_get_form('modr8_settings_form');
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Implementation of hook_nodeapi().
|
| 126 |
*/
|
| 127 |
function modr8_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 128 |
// add messages here..
|
| 129 |
switch ($op) {
|
| 130 |
case 'load':
|
| 131 |
return db_fetch_array(db_query('SELECT n.moderate FROM {node} n WHERE n.nid = %d', $node->nid));
|
| 132 |
case 'prepare':
|
| 133 |
if (!isset($node->nid)) {//a new node
|
| 134 |
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
|
| 135 |
$node->moderate = in_array('moderate', $node_options);
|
| 136 |
}
|
| 137 |
break;
|
| 138 |
case 'view':
|
| 139 |
if ($node->moderate && empty($node->modr8_form_teaser) && ($node->build_mode == NODE_BUILD_NORMAL) && ($teaser || $page)) {
|
| 140 |
$node->content['modr8_message'] = array(
|
| 141 |
'#value' => theme('modr8_message', $teaser, $node->type, 'view'),
|
| 142 |
'#weight' => -100,
|
| 143 |
);
|
| 144 |
}
|
| 145 |
break;
|
| 146 |
case 'update' :
|
| 147 |
case 'insert' :
|
| 148 |
db_query('UPDATE {node} SET moderate = %d WHERE nid = %d', $node->moderate, $node->nid);
|
| 149 |
if ($node->moderate) { //cut this?
|
| 150 |
theme('modr8_message', FALSE, $node->type, $op);
|
| 151 |
}
|
| 152 |
break;
|
| 153 |
case 'delete':
|
| 154 |
// Delete log entries when a node is deleted, unless it's deleted while
|
| 155 |
// in moderation. In the latter case, we want to retain the log to see
|
| 156 |
// which moderator deleted the node and any message they sent.
|
| 157 |
if (!$node->moderate) {
|
| 158 |
db_query("DELETE FROM {modr8_log} WHERE nid = %d", $node->nid);
|
| 159 |
}
|
| 160 |
break;
|
| 161 |
}
|
| 162 |
}
|
| 163 |
|
| 164 |
function modr8_form_alter(&$form, $form_state, $form_id) {
|
| 165 |
|
| 166 |
if (isset($form['type']['#value']) && $form['type']['#value'] .'_node_form' == $form_id) {
|
| 167 |
|
| 168 |
$moderate_checkbox = array(
|
| 169 |
'#type' => 'checkbox',
|
| 170 |
'#title' => t('In moderation queue'),
|
| 171 |
'#default_value' => $form['#node']->moderate,
|
| 172 |
'#weight' => 24,
|
| 173 |
'#description' => t('This %type will be placed in moderation if the %moderate checkbox is selected.', array('%type' => node_get_types('name', $form['#node']), '%moderate' => t('In moderation queue'))),
|
| 174 |
);
|
| 175 |
if (user_access('administer nodes')) {
|
| 176 |
$form['options']['moderate'] = $moderate_checkbox;
|
| 177 |
}
|
| 178 |
elseif (user_access('moderate content')) {
|
| 179 |
$form['moderate'] = $moderate_checkbox;
|
| 180 |
}
|
| 181 |
else {
|
| 182 |
$form['moderate'] = array(
|
| 183 |
'#type' => 'value',
|
| 184 |
'#value' => $form['#node']->moderate,
|
| 185 |
);
|
| 186 |
if ($form['#node']->moderate) {
|
| 187 |
$form['modr8_message'] = array(
|
| 188 |
'#value' => theme('modr8_message', FALSE, $form['#node']->type, 'node_form'),
|
| 189 |
'#weight' => -100,
|
| 190 |
);
|
| 191 |
}
|
| 192 |
}
|
| 193 |
}
|
| 194 |
elseif ($form_id == 'node_type_form') {
|
| 195 |
$form['workflow']['node_options']['#options']['moderate'] = t('In moderation queue');
|
| 196 |
}
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Implementation of hook_db_rewrite_sql().
|
| 201 |
*/
|
| 202 |
function modr8_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
|
| 203 |
switch ($primary_field) {
|
| 204 |
case 'nid':
|
| 205 |
// this query deals with node objects
|
| 206 |
$access = (user_access('administer nodes') || user_access('moderate content'));
|
| 207 |
if (!$access) {
|
| 208 |
global $user;
|
| 209 |
$return = array();
|
| 210 |
if ($primary_table != 'n') {
|
| 211 |
$return['join'] = "LEFT JOIN {node} n ON $primary_table.nid = n.nid";
|
| 212 |
}
|
| 213 |
if ($user->uid == 0) {
|
| 214 |
$return['where'] = "(n.moderate != 1)";
|
| 215 |
}
|
| 216 |
else {
|
| 217 |
$return['where'] = "(n.moderate != 1 OR n.uid = ". (int)$user->uid .")";
|
| 218 |
}
|
| 219 |
return $return;
|
| 220 |
}
|
| 221 |
break;
|
| 222 |
|
| 223 |
}
|
| 224 |
}
|
| 225 |
|
| 226 |
/**
|
| 227 |
* Implementation of hook_cron().
|
| 228 |
*
|
| 229 |
* Remove expired moderation log events.
|
| 230 |
*/
|
| 231 |
function modr8_cron() {
|
| 232 |
if ($log_clear = variable_get('modr8_log_clear', 0)) {
|
| 233 |
db_query('DELETE FROM {modr8_log} WHERE timestamp < %d', time() - $log_clear);
|
| 234 |
}
|
| 235 |
}
|
| 236 |
|
| 237 |
/**
|
| 238 |
* Implementation of hook_block().
|
| 239 |
*/
|
| 240 |
function modr8_block($op = 'list', $delta = 0) {
|
| 241 |
if ($op == 'list') {
|
| 242 |
$blocks[0]['info'] = t("Modr8 moderator's block");
|
| 243 |
return $blocks;
|
| 244 |
}
|
| 245 |
elseif ($op == 'view') {
|
| 246 |
if (user_access('moderate content')) {
|
| 247 |
$block['subject'] = t('Moderation queue');
|
| 248 |
$is_published = '';
|
| 249 |
if (!user_access('administer nodes')) {
|
| 250 |
// Users who don't have the 'administer nodes' permission can only see published nodes.
|
| 251 |
$is_published = 'n.status = 1 AND ';
|
| 252 |
}
|
| 253 |
$count = db_result(db_query(db_rewrite_sql('SELECT COUNT(*) FROM {node} n WHERE '. $is_published .' n.moderate = 1')));
|
| 254 |
$content = '<p>'. l(t('@items in moderation', array('@items' => format_plural($count, '1 post', '@count posts'))), 'admin/content/modr8') .'</p>';
|
| 255 |
if ($count) {
|
| 256 |
$sql = db_rewrite_sql('SELECT n.nid, n.title FROM {node} n WHERE '. $is_published .' n.moderate = 1 ORDER BY n.changed DESC');
|
| 257 |
$result = db_query_range($sql, 0, 6);
|
| 258 |
$content .= node_title_list($result, t('Recent additions:'));
|
| 259 |
}
|
| 260 |
$block['content'] = $content;
|
| 261 |
return $block;
|
| 262 |
}
|
| 263 |
}
|
| 264 |
}
|
| 265 |
/**
|
| 266 |
* Implementation of hook_theme().
|
| 267 |
*/
|
| 268 |
function modr8_theme() {
|
| 269 |
return array(
|
| 270 |
'modr8_message' => array('arguments' => array('teaser', 'nodetype', 'op')),
|
| 271 |
'modr8_form' => array('arguments' => array('form')),
|
| 272 |
'moderation_event' => array('arguments' => array('event')),
|
| 273 |
'modr8_note' => array('arguments' => array('note')),
|
| 274 |
|
| 275 |
);
|
| 276 |
}
|
| 277 |
/**
|
| 278 |
* Theming function for messages.
|
| 279 |
*/
|
| 280 |
function theme_modr8_message($teaser = FALSE, $nodetype = 'page', $op = 'view') {
|
| 281 |
static $already_messaged = FALSE;
|
| 282 |
// Don't add the message more than once per page load.
|
| 283 |
if ($already_messaged) {
|
| 284 |
return;
|
| 285 |
}
|
| 286 |
if ($teaser) {
|
| 287 |
return ' <div class="marker">'. t('Pending moderation') .'</div>';
|
| 288 |
}
|
| 289 |
else {
|
| 290 |
switch ($op) {
|
| 291 |
case 'view':
|
| 292 |
drupal_set_message(t("The post has been submitted for moderation and won't be listed publicly until it has been approved."), 'warning');
|
| 293 |
break;
|
| 294 |
case 'node_form':
|
| 295 |
drupal_set_message(t('This %type will be submitted for moderation and will not be accessible to other users until it has been approved.', array('%type' => node_get_types('name', $nodetype))));
|
| 296 |
break;
|
| 297 |
}
|
| 298 |
return '';
|
| 299 |
}
|
| 300 |
$already_messaged = TRUE;
|
| 301 |
}
|