View source
<?php
define('EDIT_LIMIT_NODE_COUNT_DEFAULT', 3);
define('EDIT_LIMIT_COMMENT_COUNT', 3);
define('EDIT_LIMIT_NODE_TIME_DEFAULT', 86400);
define('EDIT_LIMIT_COMMENT_TIME', 300);
function edit_limit_menu() {
$items['admin/content/edit_limit'] = array(
'title' => 'Edit Limit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'edit_limit_settings',
),
'access arguments' => array(
'administer edit limit',
),
'file' => 'edit_limit.admin.inc',
);
return $items;
}
function edit_limit_perm() {
return array(
'bypass edit limit',
'administer edit limit',
);
}
function edit_limit_menu_alter(&$items) {
$items['node/%node/edit']['access callback'] = 'edit_limit_node_access';
$items['comment/edit']['access callback'] = 'edit_limit_comment_access';
}
function edit_limit_node_access($op, $node, $account = NULL) {
module_load_include('inc', 'node', 'node.pages');
if (node_access($op, $node, $account)) {
switch ($op) {
case 'update':
if (user_access('bypass edit limit')) {
return TRUE;
}
return _edit_limit_user_acccess($node);
}
}
return FALSE;
}
function _edit_limit_user_acccess($node) {
$return = TRUE;
if (empty($node->nid)) {
return $return;
}
$allowed_types = variable_get('edit_limit_node_types', array());
if (empty($allowed_types[$node->type])) {
return $return;
}
$edit_limit_node_time_enabled = variable_get('edit_limit_node_time_enabled', FALSE);
$time_limits['default'] = intval(variable_get('edit_limit_node_time_default', 10)) * 24 * 60 * 60;
$time_limits['node'][$node->type] = intval(variable_get('edit_limit_node_time_' . $node->type, $time_limits['default']));
$edit_limit_node_count_enabled = variable_get('edit_limit_node_count_enabled', FALSE);
$count_limits['default'] = intval(variable_get('edit_limit_node_count_default', EDIT_LIMIT_NODE_COUNT_DEFAULT));
$count_limits['node'][$node->type] = intval(variable_get('edit_limit_node_count_' . $node->type, $count_limits['default']));
if (!$edit_limit_node_time_enabled && !$edit_limit_node_count_enabled) {
return $return;
}
$count_limit_reached == FALSE;
if ($edit_limit_node_count_enabled && $return) {
$edit_count = edit_limit_node_count($node->nid);
$count_limit = $count_limits['node'][$node->type] ? $count_limits['node'][$node->type] : $count_limits['node']['default'];
if ($count_limit <= $edit_count) {
$return = FALSE;
$count_limit_reached = TRUE;
}
}
if ($edit_limit_node_time_enabled && $return) {
$timeleft = intval($node->created) + intval(variable_get('edit_limit_node_time_default', EDIT_LIMIT_NODE_TIME_DEFAULT)) - time();
if ($timeleft <= 0) {
$time_limit_reached = TRUE;
$return = FALSE;
}
elseif (!$count_limit_reached && arg(0) == 'node' && arg(2) == 'edit') {
static $type;
if (empty($type)) {
$type = 'status';
drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
drupal_set_message(t('You have !seconds seconds left to edit this content.', array(
'!seconds' => $timeleft,
)), $type, FALSE);
}
}
}
return $return;
}
function edit_limit_comment_access() {
if (user_access('post comments')) {
if (user_access('bypass edit limit')) {
return TRUE;
}
$return = TRUE;
if (variable_get('edit_limit_comment_time_enabled', FALSE)) {
$cid = intval(arg(2));
$timestamp = db_result(db_query("SELECT timestamp FROM {comments} WHERE cid=%d", $cid));
if ($timestamp + intval(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME)) < time()) {
$return = FALSE;
}
else {
static $seconds;
if (empty($seconds)) {
drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
$seconds = $timestamp + intval(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME)) - time();
}
$type = 'status';
drupal_set_message(t('You have !seconds seconds left to edit this comment.', array(
'!seconds' => $seconds,
)), $type, FALSE);
}
}
return $return;
}
return FALSE;
}
function edit_limit_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
$allowed_types = variable_get('edit_limit_node_types', array());
if (!user_access('bypass edit limit') && !empty($allowed_types[$node->type])) {
if ($op == 'update') {
if (variable_get('edit_limit_node_count_enabled', FALSE)) {
$count_data = db_fetch_array(db_query("SELECT * FROM {edit_limit_node_count} WHERE nid=%d", $node->nid));
$edit_count = 0;
$update = array();
if (!empty($count_data)) {
$edit_count = $count_data['count'];
$update = array(
'nid',
);
}
$edit_count++;
$data = array(
'nid' => $node->nid,
'count' => $edit_count,
);
drupal_write_record('edit_limit_node_count', $data, $update);
}
}
if ($op == 'prepare' && !empty($node->nid)) {
if (variable_get('edit_limit_node_count_enabled', FALSE)) {
$limit = variable_get('edit_limit_node_count_default', EDIT_LIMIT_NODE_COUNT_DEFAULT);
$count = edit_limit_node_count($node->nid);
$type = $count + 2 > $limit ? 'warning' : 'status';
drupal_set_message("This node has been edit {$count} out of {$limit} times, after which it will be locked.", $type);
}
}
}
}
function edit_limit_node_count($nid) {
$count = db_result(db_query("SELECT count FROM {edit_limit_node_count} WHERE nid=%d", $nid));
$count = empty($count) ? 0 : $count;
return $count;
}
function edit_limit_link_alter(&$links, $node, $comment = NULL) {
if (!empty($links['comment_edit'])) {
if (0) {
$results = db_query("SELECT * FROM {edit_limit_comment_time} WHERE cid=%d", $comment->cid);
}
if (!user_access('bypass edit limit') && variable_get('edit_limit_comment_time_enabled', FALSE)) {
$time_limit = $comment->timestamp + intval(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME));
if ($time_limit < time()) {
unset($links['comment_edit']);
}
else {
drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
$links['comment_edit']['title'] .= ' (' . ($time_limit - time()) . ' seconds left)';
}
}
}
}