View source
<?php
define('PRIVATE_DISABLED', 0);
define('PRIVATE_ALLOWED', 1);
define('PRIVATE_AUTOMATIC', 2);
define('PRIVATE_ALWAYS', 3);
function private_enable() {
node_access_rebuild(TRUE);
}
function private_disable() {
private_disabling(TRUE);
node_access_rebuild(TRUE);
}
function private_disabling($set = NULL) {
static $disabling = FALSE;
if ($set !== NULL) {
$disabling = $set;
}
return $disabling;
}
function private_perm() {
return array(
'mark content as private',
'access private content',
'edit private content',
);
}
function private_node_grants($account, $op) {
if ($op == 'view' && user_access('access private content', $account)) {
$grants['private'] = array(
1,
);
}
if (($op == 'update' || $op == 'delete') && user_access('edit private content', $account)) {
$grants['private'] = array(
1,
);
}
$grants['private_author'] = array(
$account->uid,
);
return $grants;
}
function private_node_access_records($node) {
if (private_disabling()) {
return;
}
if ($node->private) {
$grants = array();
$grants[] = array(
'realm' => 'private',
'gid' => TRUE,
'grant_view' => TRUE,
'grant_update' => FALSE,
'grant_delete' => FALSE,
'priority' => 0,
);
$grants[] = array(
'realm' => 'private_author',
'gid' => $node->uid,
'grant_view' => TRUE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'priority' => 0,
);
return $grants;
}
}
function private_form_alter(&$form, $form_state, $form_id) {
if ($form['#id'] == 'node-form') {
$node = $form['#node'];
$default = variable_get('private_' . $node->type, PRIVATE_ALLOWED);
if ($default != PRIVATE_DISABLED || !empty($node->privacy)) {
if (empty($node->nid)) {
$privacy = $default > PRIVATE_ALLOWED;
}
else {
$privacy = $node->private;
}
if (user_access('mark content as private') && $default != PRIVATE_ALWAYS) {
$form['private'] = array(
'#type' => 'checkbox',
'#title' => t('Make this post private'),
'#return_value' => 1,
'#description' => t('When checked, only users with proper access permissions will be able to see this post.'),
'#default_value' => $privacy,
);
}
else {
$form['private'] = array(
'#type' => 'value',
'#value' => $privacy,
);
}
}
}
elseif ($form_id == 'node_type_form') {
$node_type = (array) $form['#node_type'];
$type = $node_type['type'];
$form['workflow']['private'] = array(
'#type' => 'radios',
'#title' => t('Privacy'),
'#options' => array(
PRIVATE_DISABLED => t('Disabled (always public)'),
PRIVATE_ALLOWED => t('Enabled (public by default)'),
PRIVATE_AUTOMATIC => t('Enabled (private by default)'),
PRIVATE_ALWAYS => t('Hidden (always private)'),
),
'#default_value' => variable_get('private_' . $type, PRIVATE_ALLOWED),
);
}
}
function private_nodeapi(&$node, $op, $arg = 0) {
switch ($op) {
case 'load':
$result = db_fetch_object(db_query('SELECT * FROM {private} WHERE nid = %d', $node->nid));
$node->private = $result->private;
break;
case 'delete':
db_query('DELETE FROM {private} WHERE nid = %d', $node->nid);
break;
case 'insert':
case 'update':
db_query('UPDATE {private} SET private = %d WHERE nid = %d', $node->private, $node->nid);
if (!db_affected_rows()) {
db_query('INSERT INTO {private} (nid, private) VALUES (%d, %d)', $node->nid, $node->private);
}
break;
}
}
function private_file_download($file) {
$file = file_create_path($file);
$result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file);
if ($file = db_fetch_object($result)) {
$node = node_load($file->nid);
if ($node->private == 1) {
if (node_access('view', $node) == FALSE) {
return -1;
}
}
}
}
function private_link($type, $node = NULL, $teaser = FALSE) {
if ($type == 'node' && $node->private) {
$links['private_icon']['title'] = theme('private_node_link', $node);
$links['private_icon']['html'] = TRUE;
return $links;
}
}
function private_theme() {
return array(
'private_node_link' => array(
'arguments' => array(
'node' => NULL,
),
),
);
}
function theme_private_node_link($node) {
return theme('image', drupal_get_path('module', 'private') . '/icon_key.gif', t('Private'), t('This content is private'));
}
function private_action_info() {
return array(
'private_set_private_action' => array(
'type' => 'node',
'description' => t('Make post private'),
'configurable' => FALSE,
'hooks' => array(
'nodeapi' => array(
'insert',
'update',
),
),
),
'private_set_public_action' => array(
'type' => 'node',
'description' => t('Make post public'),
'configurable' => FALSE,
'hooks' => array(
'nodeapi' => array(
'insert',
'update',
),
),
),
);
}
function private_set_public_action(&$node, $context = array()) {
$node->private = FALSE;
$nids = array(
$node->nid,
);
private_node_mark_public($nids);
}
function private_set_private_action(&$node, $context = array()) {
$node->private = TRUE;
$nids = array(
$node->nid,
);
private_node_mark_private($nids);
}
function private_node_operations() {
$operations = array(
'private_mark_as_private' => array(
'label' => t('Mark as private'),
'callback' => 'private_node_mark_private',
),
'private_mark_as_public' => array(
'label' => t('Mark as public'),
'callback' => 'private_node_mark_public',
),
);
return $operations;
}
function private_node_mark_private($nids) {
foreach ($nids as $nid) {
db_query('UPDATE {private} SET private = %d WHERE nid = %d', 1, $nid);
if (!db_affected_rows()) {
db_query('INSERT INTO {private} (nid, private) VALUES (%d, %d)', $nid, 1);
}
}
}
function private_node_mark_public($nids) {
foreach ($nids as $nid) {
db_query('UPDATE {private} SET private = %d WHERE nid = %d', 0, $nid);
if (!db_affected_rows()) {
db_query('INSERT INTO {private} (nid, private) VALUES (%d, %d)', $nid, 0);
}
}
}
function private_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'private'),
);
}