| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_perm().
|
| 6 |
*/
|
| 7 |
function restricted_content_perm() {
|
| 8 |
return array('restrict content access', 'restrict own content access');
|
| 9 |
}
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_menu().
|
| 13 |
*/
|
| 14 |
function restricted_content_menu() {
|
| 15 |
$items['admin/content/restricted'] = array(
|
| 16 |
'title' => 'Restricted content',
|
| 17 |
'page callback' => 'drupal_get_form',
|
| 18 |
'page arguments' => array('restricted_content_settings_form'),
|
| 19 |
'access arguments' => array('restrict content access'),
|
| 20 |
'file' => 'restricted_content.admin.inc',
|
| 21 |
);
|
| 22 |
|
| 23 |
return $items;
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implementation of hook_form_alter().
|
| 28 |
*/
|
| 29 |
function restricted_content_form_alter(&$form, $form_state, $form_id) {
|
| 30 |
if ($form_id == 'node_type_form') {
|
| 31 |
//restricted_content_node_form($form);
|
| 32 |
}
|
| 33 |
elseif ($form['#id'] == 'node-form') {
|
| 34 |
$default = unserialize(db_result(db_query("SELECT rids FROM {restricted_content} WHERE nid = %d", $form['nid']['#value'])));
|
| 35 |
restricted_content_node_form($form, $default);
|
| 36 |
$form['#submit'][] = 'restricted_content_node_form_submit';
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
/**
|
| 41 |
* Option elements to add to node forms.
|
| 42 |
*/
|
| 43 |
function restricted_content_node_form(&$form, $default) {
|
| 44 |
$form['restricted_content'] = array(
|
| 45 |
'#type' => 'fieldset',
|
| 46 |
'#title' => t('Restricted Access'),
|
| 47 |
'#collapsible' => TRUE,
|
| 48 |
'#collapsed' => TRUE,
|
| 49 |
'#tree' => TRUE,
|
| 50 |
'#access' => restricted_content_form_access($form['uid']['#value']),
|
| 51 |
);
|
| 52 |
$form['restricted_content']['rids'] = array(
|
| 53 |
'#type' => 'checkboxes',
|
| 54 |
'#title' => t('Restrict access to users with the following user roles'),
|
| 55 |
'#description' => t('If no roles are selected, the node will be viewable by all users.'),
|
| 56 |
'#options' => user_roles(),
|
| 57 |
'#default_value' => is_array($default) ? $default : array(),
|
| 58 |
);
|
| 59 |
$form['#submit'][] = 'restricted_content_node_form_submit';
|
| 60 |
}
|
| 61 |
|
| 62 |
function restricted_content_node_form_submit($form, $form_state) {
|
| 63 |
$nid = $form_state['values']['nid'];
|
| 64 |
$rids = array_keys(array_filter($form_state['values']['restricted_content']['rids']));
|
| 65 |
db_query("DELETE FROM {restricted_content} WHERE nid = %d", $nid);
|
| 66 |
if ($rids) {
|
| 67 |
db_query("INSERT INTO {restricted_content} VALUES (%d, '%s')", $nid, serialize($rids));
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
function restricted_content_form_access($uid) {
|
| 72 |
global $user;
|
| 73 |
return user_access('restrict content access') || ($uid == $user->uid && user_access('restrict own content access'));
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Checks if a node is restricted from the current user.
|
| 78 |
*
|
| 79 |
* @param $nid
|
| 80 |
* A node ID.
|
| 81 |
* @param $account
|
| 82 |
* An optional user account to check, defaults to the current user.
|
| 83 |
* @return
|
| 84 |
* TRUE if the user has access to the node, or FALSE if the user is
|
| 85 |
* restricted from the node.
|
| 86 |
*/
|
| 87 |
function restricted_content_node_access($nid, $account = NULL) {
|
| 88 |
global $user;
|
| 89 |
if (!$account) {
|
| 90 |
$account = $user;
|
| 91 |
}
|
| 92 |
$rids = db_result(db_query("SELECT rids FROM {restricted_content} WHERE nid = %d", $nid));
|
| 93 |
return !$rids || array_intersect(unserialize($rids), array_keys($account->roles));
|
| 94 |
}
|
| 95 |
|
| 96 |
function restricted_content_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 97 |
global $user;
|
| 98 |
|
| 99 |
if ($op == 'delete') {
|
| 100 |
db_query("DELETE FROM {restricted_content} WHERE nid = %d", $node->nid);
|
| 101 |
}
|
| 102 |
elseif ($op == 'load' && !restricted_content_form_access($node->uid) && !restricted_content_node_access($node->nid)) {
|
| 103 |
$message = restricted_content_var('message');
|
| 104 |
$node->restricted = TRUE;
|
| 105 |
$node->comment = COMMENT_NODE_DISABLED;
|
| 106 |
|
| 107 |
// Add a register link of the user is anonymous and can register for an
|
| 108 |
// account.
|
| 109 |
if (!$user->uid && variable_get('user_register', 1)) {
|
| 110 |
$message .= ' '. restricted_content_var('message_anon');
|
| 111 |
}
|
| 112 |
|
| 113 |
// Perform token replacement if token module is available.
|
| 114 |
if (module_exists('token')) {
|
| 115 |
$message = token_replace($message, 'node', $node);
|
| 116 |
}
|
| 117 |
|
| 118 |
$node->teaser = $message;
|
| 119 |
$node->body = $message;
|
| 120 |
}
|
| 121 |
/*elseif ($op == 'alter' && !empty($node->restricted)) {
|
| 122 |
// Send a 403 if this is an individual page view.
|
| 123 |
if ($page) {
|
| 124 |
drupal_set_header('HTTP/1.1 403 Forbidden');
|
| 125 |
}
|
| 126 |
}*/
|
| 127 |
}
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Implementation of MODULE_preprocess_node().
|
| 131 |
*/
|
| 132 |
function restricted_content_preprocess_node(&$vars) {
|
| 133 |
if (!empty($vars['node']->restricted)) {
|
| 134 |
$vars['submitted'] = FALSE;
|
| 135 |
$vars['picture'] = FALSE;
|
| 136 |
$vars['taxonomy'] = FALSE;
|
| 137 |
$vars['terms'] = FALSE;
|
| 138 |
$vars['links'] = FALSE;
|
| 139 |
$vars['node_url'] = request_uri();
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* Implementation of hook_token_list().
|
| 145 |
*/
|
| 146 |
function restricted_content_token_list($type = 'all') {
|
| 147 |
$tokens['global']['site-register-url'] = t('The URL of the register user page');
|
| 148 |
if ($type == 'node' || $type == 'all') {
|
| 149 |
$tokens['node']['type-name-lower'] = t('Node type (user-friendly version lowercased)');
|
| 150 |
}
|
| 151 |
return $tokens;
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
| 155 |
* Implementation of hook_token_values().
|
| 156 |
*/
|
| 157 |
function restricted_content_token_values($type, $object = NULL) {
|
| 158 |
$tokens['site-register-url'] = url('user/register');
|
| 159 |
if ($type == 'node') {
|
| 160 |
$tokens['type-name-lower'] = drupal_strtolower(node_get_types('name', $object));
|
| 161 |
}
|
| 162 |
return $tokens;
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Internal default variables for admin_links_var().
|
| 167 |
*/
|
| 168 |
function restricted_content_variables() {
|
| 169 |
return array(
|
| 170 |
'restricted_content_message' => t('This !token-type-name has been restricted to certain users.', array('!token-type-name' => module_exists('token') ? '[type-name-lower]' : t('content'))),
|
| 171 |
'restricted_content_message_anon' => t('Please <a href="!token-register">register for a user account</a> to view this !token-type-name.', array('!token-type-name' => module_exists('token') ? '[type-name-lower]' : t('content'), '!token-register' => module_exists('token') ? '[site-url-register]' : url('user/register'))),
|
| 172 |
);
|
| 173 |
}
|
| 174 |
|
| 175 |
/**
|
| 176 |
* Internal implementation of variable_get().
|
| 177 |
*/
|
| 178 |
function restricted_content_var($name) {
|
| 179 |
static $defaults = NULL;
|
| 180 |
if (!isset($defaults)) {
|
| 181 |
$defaults = restricted_content_variables();
|
| 182 |
}
|
| 183 |
|
| 184 |
$name = 'restricted_content_'. $name;
|
| 185 |
|
| 186 |
// @todo Remove when I stop making spelling errors in variable names.
|
| 187 |
if (!isset($defaults[$name])) {
|
| 188 |
watchdog('restricted_conte', 'Default variable for %variable not found.', array('%variable' => $name), WATCHDOG_WARNING);
|
| 189 |
}
|
| 190 |
|
| 191 |
return variable_get($name, isset($defaults[$name]) ? $defaults[$name] : NULL);
|
| 192 |
}
|