View source
<?php
define('PROTECTED_NODE_PER_NODE_PASSWORD', 0);
define('PROTECTED_NODE_PER_NODE_AND_GLOBAL_PASSWORD', 1);
define('PROTECTED_NODE_GLOBAL_PASSWORD', 2);
define('PROTECTED_NODE_PROTECTION_NEVER', 0);
define('PROTECTED_NODE_PROTECTION_PROTECTABLE', 1);
define('PROTECTED_NODE_PROTECTION_PROTECTED', 2);
define('PROTECTED_NODE_PROTECTION_ALWAYS', 3);
function protected_node_help($path, $arg) {
switch ($path) {
case 'admin/modules#description':
return t('With this module anybody who has edit protected node permission can password protect his or her own node.');
}
}
function protected_node_permission() {
$permissions = array(
'access protected node overview page' => array(
'title' => t('Access protected node overview page'),
),
'access protected node password form' => array(
'title' => t('Access protected node password form'),
'description' => t('Access protected node password form page. Without this permission user will be denied access completely.'),
),
'edit any protected node password' => array(
'title' => t('Edit any protected node password'),
'description' => t('Edit the password of any protected node. Grants access to the password fieldset in the node form.'),
),
'view protected content' => array(
'title' => t('View protected content (bypass password)'),
'description' => t('Allow to view any protected node by bypassing the password protection.'),
),
'edit protected content' => array(
'title' => t('Edit protected content (bypass password)'),
'description' => t('Allow to edit any protected node by bypassing the password protection. Do not allow to edit the password. Note: the user will also be able to view the content without entering the password.'),
),
);
foreach (node_type_get_types() as $key => $type) {
$permissions['edit ' . $key . ' password'] = array(
'title' => t('Edit %type_name password', array(
'%type_name' => $key,
)),
'description' => t('Edit password for %type_name nodes', array(
'%type_name' => $key,
)),
);
}
return $permissions;
}
function protected_node_menu() {
module_load_include('settings.inc', 'protected_node');
return protected_node_menu_array();
}
function protected_node_access_callback() {
global $user;
if ($user->uid == 1) {
return TRUE;
}
if (!user_access('access protected node password form')) {
return FALSE;
}
if (empty($_GET['protected_page']) || !is_numeric($_GET['protected_page'])) {
return FALSE;
}
$node = node_load($_GET['protected_page']);
if (!$node) {
return FALSE;
}
if (substr($_GET['destination'], 0, 5) == 'node/') {
if (substr($_GET['destination'], -5) == '/edit') {
if (!node_access('update', $node)) {
return FALSE;
}
}
elseif (substr($_GET['destination'], -7) == '/delete') {
if (!node_access('delete', $node)) {
return FALSE;
}
}
}
return TRUE;
}
function protected_node_init() {
if (function_exists('drush_main')) {
return;
}
if (user_access('edit protected content')) {
return;
}
if (variable_get('protected_node_use_global_password', PROTECTED_NODE_PER_NODE_PASSWORD) == PROTECTED_NODE_GLOBAL_PASSWORD && isset($_SESSION['has_entered_global_password'])) {
return;
}
$nid = FALSE;
$param2 = arg(2);
if (arg(0) == 'node' && is_numeric(arg(1))) {
if ($param2 === NULL) {
$nid = protected_node_is_locked(arg(1), 'view');
if ($nid === -1) {
return;
}
}
elseif ($param2 == 'edit' || $param2 == 'delete') {
$nid = protected_node_is_locked(arg(1), $param2);
}
else {
$nid = protected_node_is_locked(arg(1), 'view');
}
if ($nid === TRUE || $nid === -1) {
drupal_access_denied();
exit;
}
}
elseif (arg(0) == 'system' && arg(1) == 'files') {
$requested_url = drupal_parse_url(request_uri());
$path = urldecode(str_replace('system/files', '', $requested_url['path']));
if (!empty($path)) {
$nid = protected_node_and_attachment($path);
}
}
if ($nid) {
$query = drupal_get_destination();
if (!empty($_SERVER['HTTP_REFERER'])) {
$query['back'] = urlencode($_SERVER['HTTP_REFERER']);
}
$query['protected_page'] = $nid;
drupal_goto('protected-node', array(
'query' => $query,
));
}
}
function protected_node_is_locked($nid, $op = 'access') {
global $user;
$node = node_load($nid);
if (!isset($node->protected_node_is_protected) || !$node->protected_node_is_protected) {
return FALSE;
}
if (!$user->uid) {
if (variable_get('cache', 1)) {
$GLOBALS['conf']['cache'] = 0;
}
}
else {
if ($node->uid === $user->uid) {
return FALSE;
}
}
if (!user_access('access protected node password form')) {
return TRUE;
}
if ($op == 'view') {
if (user_access('view protected content') && node_access('view', $node)) {
return -1;
}
}
elseif ($op == 'edit') {
if (!node_access('update', $node)) {
return TRUE;
}
elseif (user_access('edit protected content') && node_access('update', $node)) {
return -1;
}
}
elseif ($op == 'delete') {
if (!node_access('delete', $node)) {
return TRUE;
}
}
else {
return TRUE;
}
if (isset($_SESSION['_protected_node']['passwords']['global'])) {
$when = $_SESSION['_protected_node']['passwords']['global'];
if ($when > variable_get('protected_node_session_timelimit', 0) && $when > $node->protected_node_passwd_changed) {
return FALSE;
}
unset($_SESSION['_protected_node']['passwords']['global']);
}
else {
if (isset($_SESSION['_protected_node']['passwords'][$nid])) {
$when = $_SESSION['_protected_node']['passwords'][$nid];
if ($when > variable_get('protected_node_session_timelimit', 0) && $when > $node->protected_node_passwd_changed) {
return FALSE;
}
unset($_SESSION['_protected_node']['passwords'][$nid]);
}
}
return $nid;
}
function protected_node_and_attachment($path) {
global $user;
if (user_access('edit protected content')) {
return FALSE;
}
$query = db_select('node', 'n');
$query
->join('file_usage', 'fu', 'n.nid = fu.id');
$query
->join('file_managed', 'fm', 'fm.fid = fu.fid');
$query
->join('protected_nodes', 'pn', 'n.nid = pn.nid');
$query
->fields('n', array(
'nid',
'uid',
));
$query
->fields('pn', array(
'protected_node_passwd_changed',
));
$query
->condition('fu.type', 'node');
$query
->condition('fm.uri', '%' . db_like($path), 'LIKE');
$query
->condition('pn.protected_node_is_protected', '1');
$number_of_results = $query
->countQuery()
->execute()
->fetchField();
if (0 == $number_of_results) {
if (module_exists('field_collection')) {
$query = db_select('file_usage', 'fu');
$query
->join('file_managed', 'fm', 'fu.fid = fm.fid');
$query
->fields('fu', array(
'id',
));
$query
->condition('fu.type', 'field_collection_item');
$query
->condition('fm.uri', '%' . db_like($path), 'LIKE');
$in_field_collection = $query
->countQuery()
->execute()
->fetchField();
if ($in_field_collection != '0') {
$field_collection_ids = $query
->execute()
->fetchCol();
$field_collection_items = entity_load('field_collection_item', $field_collection_ids);
$protected_node_nids = array();
foreach ($field_collection_items as $field_collection_item) {
$protected_node_nids[] = $field_collection_item
->hostEntity()->nid;
}
$query = db_select('node', 'n');
$query
->join('protected_nodes', 'pn', 'n.nid = pn.nid');
$query
->fields('n', array(
'nid',
'uid',
));
$query
->fields('pn', array(
'protected_node_passwd_changed',
));
$query
->condition('n.nid', $protected_node_nids, 'IN');
$query
->condition('pn.protected_node_is_protected', '1');
$number_of_results = $query
->countQuery()
->execute()
->fetchField();
if (0 == $number_of_results) {
return FALSE;
}
}
else {
return FALSE;
}
}
elseif (module_exists('paragraphs')) {
$query = db_select('file_usage', 'fu');
$query
->join('file_managed', 'fm', 'fu.fid = fm.fid');
$query
->fields('fu', array(
'id',
));
$query
->condition('fu.type', 'paragraphs_item');
$query
->condition('fm.uri', '%' . db_like($path), 'LIKE');
$in_paragraphs = $query
->countQuery()
->execute()
->fetchField();
if ($in_paragraphs != '0') {
$paragraphs_ids = $query
->execute()
->fetchCol();
$paragraphs_items = entity_load('paragraphs_item', $paragraphs_ids);
$protected_node_nids = array();
foreach ($paragraphs_items as $paragraphs_item) {
$nid = _protected_node_get_paragraph_node_host_entity_id($paragraphs_item);
if ($nid) {
$protected_node_nids[] = $nid;
}
}
if (!empty($protected_node_nids)) {
$query = db_select('node', 'n');
$query
->join('protected_nodes', 'pn', 'n.nid = pn.nid');
$query
->fields('n', array(
'nid',
'uid',
));
$query
->fields('pn', array(
'protected_node_passwd_changed',
));
$query
->condition('n.nid', $protected_node_nids, 'IN');
$query
->condition('pn.protected_node_is_protected', '1');
$number_of_results = $query
->countQuery()
->execute()
->fetchField();
if (0 == $number_of_results) {
return FALSE;
}
}
else {
return FALSE;
}
}
else {
return FALSE;
}
}
else {
return FALSE;
}
}
$result = $query
->execute();
foreach ($result as $file_info) {
if ($file_info === FALSE || $user->uid && $user->uid == $file_info->uid) {
return FALSE;
}
if (user_access('view protected content', $user)) {
return FALSE;
}
if (isset($_SESSION['_protected_node']['passwords']['global'])) {
$when = $_SESSION['_protected_node']['passwords']['global'];
if ($when > $file_info->protected_node_passwd_changed && $when > variable_get('protected_node_session_timelimit', 0)) {
return FALSE;
}
unset($_SESSION['_protected_node']['passwords']['global']);
}
else {
if (isset($_SESSION['_protected_node']['passwords'][$file_info->nid])) {
$when = $_SESSION['_protected_node']['passwords'][$file_info->nid];
if ($when > $file_info->protected_node_passwd_changed && $when > variable_get('protected_node_session_timelimit', 0)) {
return FALSE;
}
unset($_SESSION['_protected_node']['passwords'][$file_info->nid]);
}
}
return $file_info->nid;
}
}
function protected_node_invoke($hook, &$param) {
foreach (module_implements($hook) as $module) {
call_user_func_array($module . '_' . $hook, array(
&$param,
));
}
}
function protected_node_form_node_type_form_alter(&$form, &$form_state, $form_id) {
if (user_access('edit any protected node password') || user_access('edit ' . $form['type']['#value'] . ' password')) {
form_load_include($form_state, 'settings.inc', 'protected_node');
protected_node_node_type_form_alter($form);
}
}
function protected_node_form_node_form_alter(&$form, &$form_state, $form_id) {
if (user_access('edit any protected node password') || user_access('edit ' . $form['type']['#value'] . ' password')) {
form_load_include($form_state, 'settings.inc', 'protected_node');
protected_node_node_form_alter($form);
}
}
function protected_node_node_load($nodes, $types) {
return protected_node_load($nodes);
}
function protected_node_node_validate($node, $form, &$form_state) {
if (!isset($node->protected_node_is_protected)) {
return;
}
global $_protected_node_emails;
global $_protected_node_random_passwd;
$_protected_node_emails = '';
$_protected_node_random_passwd = '';
if ($node->protected_node_is_protected && (user_access('edit any protected node password') || user_access('edit ' . $node->type . ' password'))) {
$missing_password = FALSE;
if (empty($node->protected_node_passwd)) {
$global_content_type_password = variable_get('protected_node_node_type_password_' . $node->type, '');
if ($global_content_type_password != '') {
}
else {
$result = db_select('protected_nodes')
->fields('protected_nodes', array(
'protected_node_passwd',
))
->condition('nid', $node->nid)
->execute()
->fetchField();
$result = trim($result);
if (empty($result)) {
$missing_password = TRUE;
}
}
}
if (!empty($node->protected_node_emails)) {
if ($node->status) {
$emails = explode(',', str_replace(array(
"\r",
"\n",
), ',', $node->protected_node_emails));
foreach ($emails as $k => $m) {
$m = trim($m);
if ($m) {
if (!valid_email_address($m)) {
form_error($form['protected_node']['protected_node_emails'], t('Invalid email address: @m. Please correct this mistake and try again.', array(
'@m' => $m,
)));
unset($emails[$k]);
}
else {
$emails[$k] = $m;
}
}
else {
unset($emails[$k]);
}
}
$_protected_node_emails = implode(', ', $emails);
if ($_protected_node_emails && $missing_password && variable_get('protected_node_random_password', FALSE)) {
$_protected_node_random_passwd = user_password();
$missing_password = FALSE;
drupal_set_message(t('A random password was generated in order to send the email about this page. Remember that changing the password will prevent users you just emailed from accessing this page.'), 'warning');
}
}
else {
form_error($form['protected_node']['protected_node_emails'], t('The node is not published. Therefore no email will be sent.'));
}
}
if ($missing_password) {
global $user;
if ($user->uid == 0) {
$global_password = PROTECTED_NODE_PER_NODE_PASSWORD;
}
else {
$global_password = variable_get('protected_node_use_global_password', PROTECTED_NODE_PER_NODE_PASSWORD);
}
switch ($global_password) {
case PROTECTED_NODE_PER_NODE_PASSWORD:
form_error($form['protected_node']['protected_node_passwd'], t('To protect this page, please enter a password.'));
break;
}
}
}
elseif (isset($node->protected_node_emails) && trim($node->protected_node_emails)) {
form_error($form['protected_node']['protected_node_emails'], t('No email can be sent by the protected node module when the node is not protected or you do not have permission to set a password.'));
}
}
function protected_node_node_insert($node) {
_protected_node_node_create_or_update($node);
}
function protected_node_node_update($node) {
_protected_node_node_create_or_update($node);
}
function _protected_node_node_create_or_update($node) {
if (!isset($node->protected_node_is_protected)) {
return;
}
global $_protected_node_emails;
global $_protected_node_random_passwd;
if (!empty($_protected_node_random_passwd)) {
$node->protected_node_passwd = $_protected_node_random_passwd;
}
if (!empty($_protected_node_emails)) {
$node->protected_node_emails = $_protected_node_emails;
}
_protected_node_save($node);
if ($node->protected_node_is_protected && !empty($node->protected_node_emails) && $node->status == 1 && isset($node->protected_node_clear_passwd)) {
module_load_include('mail.inc', 'protected_node');
protected_node_send_mail($node);
}
}
function protected_node_node_view($node, $view_mode, $langcode) {
global $user;
if (!empty($node->protected_node_is_protected)) {
if ($view_mode == 'search_index') {
protected_node_invoke('protected_node_hide', $node);
}
elseif (!user_access('view protected content') && _protected_node_check_view_mode($view_mode)) {
if (!$user->uid && variable_get('cache', 1)) {
$GLOBALS['conf']['cache'] = 0;
}
$global_reset_time = variable_get('protected_node_session_timelimit', 0);
$this_page_reset_time = $node->protected_node_passwd_changed;
if ($node->uid !== $user->uid) {
if (isset($_SESSION['_protected_node']['passwords']['global'])) {
$when = $_SESSION['_protected_node']['passwords']['global'];
if ($when <= $global_reset_time || $when <= $this_page_reset_time) {
unset($_SESSION['_protected_node']['passwords']['global']);
}
}
if (isset($_SESSION['_protected_node']['passwords'][$node->nid])) {
$when = $_SESSION['_protected_node']['passwords'][$node->nid];
if ($when <= $global_reset_time || $when <= $this_page_reset_time) {
unset($_SESSION['_protected_node']['passwords'][$node->nid]);
}
}
if (!isset($_SESSION['_protected_node']['passwords'][$node->nid]) && !isset($_SESSION['_protected_node']['passwords']['global'])) {
if (!user_access('access protected node password form')) {
drupal_access_denied();
exit;
}
protected_node_invoke('protected_node_hide', $node);
}
}
}
}
}
function protected_node_node_delete($node) {
db_delete('protected_nodes')
->condition('nid', $node->nid)
->execute();
}
function protected_node_protected_node_hide(&$node) {
if (!$node->protected_node_show_title) {
$node->title = t('Password protected page');
}
$node->body = '';
$content_children = element_children($node->content);
foreach ($content_children as $content_key) {
unset($node->content[$content_key]);
}
}
function protected_node_file_download($uri) {
global $user;
$path = file_uri_target($uri);
if (strpos($path, 'styles/') === 0) {
if (image_get_info($uri)) {
$original_uri = _protected_node_get_original_uri($path, $uri);
$headers = module_invoke_all('file_download', $original_uri);
if (!empty($headers) && !in_array(-1, $headers)) {
return array();
}
}
return array();
}
$files = file_load_multiple(array(), array(
'uri' => $uri,
));
if (count($files)) {
$file = reset($files);
if ($file->status) {
if (strpos($file->uri, '://webform/') !== FALSE) {
$query = db_select('file_usage', 'fu');
$query
->join('webform_submissions', 'ws', 'ws.sid = fu.id');
$query
->join('node', 'n', 'n.nid = ws.nid');
$query
->join('protected_nodes', 'pn', 'n.nid = pn.nid');
$query
->fields('n', array(
'nid',
'uid',
));
$query
->fields('pn', array(
'protected_node_passwd_changed',
));
$query
->condition('fu.module', 'webform');
$query
->condition('fu.type', 'submission');
$query
->condition('fu.fid', $file->fid);
$query
->condition('pn.protected_node_is_protected', '1');
}
else {
$query = db_select('node', 'n');
$query
->join('file_usage', 'fu', 'n.nid = fu.id');
$query
->join('protected_nodes', 'pn', 'n.nid = pn.nid');
$query
->fields('n', array(
'nid',
'uid',
));
$query
->fields('pn', array(
'protected_node_passwd_changed',
));
$query
->condition('fu.fid', $file->fid);
$query
->condition('fu.type', 'node');
$query
->condition('pn.protected_node_is_protected', '1');
}
$number_of_results = $query
->countQuery()
->execute()
->fetchField();
if (0 == $number_of_results) {
return array();
}
$result = $query
->execute();
foreach ($result as $file_info) {
if ($file_info === FALSE || $user->uid && $user->uid == $file_info->uid) {
return array();
}
if (user_access('view protected content', $user)) {
return array();
}
if (isset($_SESSION['_protected_node']['passwords']['global'])) {
$when = $_SESSION['_protected_node']['passwords']['global'];
if ($when > $file_info->protected_node_passwd_changed && $when > variable_get('protected_node_session_timelimit', 0)) {
return array();
}
}
elseif (isset($_SESSION['_protected_node']['passwords'][$file_info->nid])) {
$when = $_SESSION['_protected_node']['passwords'][$file_info->nid];
if ($when > $file_info->protected_node_passwd_changed && $when > variable_get('protected_node_session_timelimit', 0)) {
return array();
}
}
}
return -1;
}
elseif (strpos($file->uri, '://webform/') !== FALSE) {
return array();
}
else {
return array();
}
}
return array();
}
function _protected_node_get_original_uri($path, $uri) {
$args = explode('/', $path);
array_shift($args);
array_shift($args);
array_shift($args);
$original_uri = file_uri_scheme($uri) . '://' . implode('/', $args);
return $original_uri;
}
function _protected_node_save(&$node) {
if (!isset($node->protected_node_is_protected)) {
return;
}
$result = db_select('protected_nodes')
->fields('protected_nodes', array(
'nid',
'protected_node_passwd',
'protected_node_emails',
))
->condition('nid', $node->nid)
->execute()
->fetchAssoc();
if (!empty($result)) {
if (isset($node->protected_node_passwd)) {
$changed = $node->protected_node_passwd != $result['protected_node_passwd'];
if ($changed) {
if (empty($node->protected_node_passwd)) {
$node->protected_node_passwd = $result['protected_node_passwd'];
$changed = FALSE;
}
else {
$node->protected_node_clear_passwd = $node->protected_node_passwd;
$node->protected_node_passwd = hash('sha256', $node->protected_node_passwd);
}
}
}
else {
$changed = FALSE;
$node->protected_node_passwd = $result['protected_node_passwd'];
}
if (empty($node->protected_node_emails)) {
if (!empty($result['protected_node_emails'])) {
$saved_emails = $result['protected_node_emails'];
}
else {
$saved_emails = '';
}
}
else {
$saved_emails = $node->protected_node_emails;
}
$args = array(
'protected_node_is_protected' => (int) $node->protected_node_is_protected,
'protected_node_passwd' => $node->protected_node_passwd,
'protected_node_show_title' => (int) $node->protected_node_show_title,
'protected_node_emails' => $saved_emails,
'protected_node_hint' => isset($node->protected_node_hint) ? $node->protected_node_hint : '',
);
if ($changed) {
$args['protected_node_passwd_changed'] = REQUEST_TIME;
}
db_update('protected_nodes')
->fields($args)
->condition('nid', $node->nid)
->execute();
}
else {
if (!isset($node->protected_node_passwd)) {
$node->protected_node_passwd = '';
}
elseif ($node->protected_node_passwd) {
$node->protected_node_clear_passwd = $node->protected_node_passwd;
$node->protected_node_passwd = hash('sha256', $node->protected_node_passwd);
}
db_insert('protected_nodes')
->fields(array(
'protected_node_is_protected' => (int) $node->protected_node_is_protected,
'protected_node_passwd' => $node->protected_node_passwd,
'protected_node_show_title' => (int) $node->protected_node_show_title,
'nid' => $node->nid,
'protected_node_emails' => isset($node->protected_node_emails) ? $node->protected_node_emails : '',
'protected_node_hint' => isset($node->protected_node_hint) ? $node->protected_node_hint : '',
))
->execute();
}
}
function protected_node_load($nodes) {
foreach ($nodes as &$node) {
if (!is_object($node) || !is_numeric($node->nid)) {
return FALSE;
}
static $default_fields = array(
'protected_node_is_protected' => 0,
'protected_node_passwd' => '',
'protected_node_passwd_changed' => 0,
'protected_node_show_title' => 0,
'protected_node_emails' => '',
'protected_node_hint' => '',
);
$protection = variable_get('protected_node_protection_' . $node->type, PROTECTED_NODE_PROTECTION_PROTECTABLE);
if ($protection == PROTECTED_NODE_PROTECTION_NEVER) {
return $default_fields;
}
$result = db_select('protected_nodes')
->fields('protected_nodes', array(
'protected_node_is_protected',
'protected_node_passwd',
'protected_node_passwd_changed',
'protected_node_show_title',
'protected_node_emails',
'protected_node_hint',
))
->condition('nid', $node->nid)
->execute()
->fetchAssoc();
if (!is_array($result)) {
$result = $default_fields;
}
else {
$result += $default_fields;
}
$result['protected_node_passwd'] = trim($result['protected_node_passwd']);
if ($protection == PROTECTED_NODE_PROTECTION_ALWAYS) {
$result['protected_node_is_protected'] = TRUE;
}
foreach ($result as $property => &$value) {
$node->{$property} = $value;
}
}
}
function protected_node_token_info() {
$info['tokens']['node'] = array(
'is-protected' => array(
'name' => t('Node protected status'),
'description' => t("Whether the node is protected (yes/no)."),
),
'password' => array(
'name' => t('Node protected password'),
'description' => t("The password in clear (only if available, empty otherwise)."),
),
'protected-title' => array(
'name' => t('Node protected show title'),
'description' => t("Whether the title node of the node is protected (yes/no)."),
),
'password-hint' => array(
'name' => t('Node protected password hint'),
'description' => t("The password hint as entered in this node."),
),
);
return $info;
}
function protected_node_node_type_delete($info) {
variable_del('protected_node_fieldset_' . $info->type);
variable_del('protected_node_protection_' . $info->type);
variable_del('protected_node_node_type_password_' . $info->type);
variable_del('protected_node_node_type_password_field_' . $info->type);
}
function protected_node_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'node' && !empty($data['node'])) {
$node = $data['node'];
if (!empty($node->protected_node_is_protected)) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'is-protected':
$replacements[$original] = t('yes');
break;
case 'password':
$replacements[$original] = empty($node->protected_node_clear_passwd) ? '' : $node->protected_node_clear_passwd;
break;
case 'protected-title':
$replacements[$original] = empty($node->protected_node_show_title) ? t('yes') : t('no');
break;
case 'password-hint':
$replacements[$original] = $node->protected_node_hint;
break;
}
}
}
}
return $replacements;
}
function protected_node_autocomplete_off($form_element, &$form_state) {
$form_element['pass1']['#attributes']['autocomplete'] = 'new-password';
$form_element['pass2']['#attributes']['autocomplete'] = 'new-password';
return $form_element;
}
function protected_node_boost_is_cacheable($path) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
return !protected_node_isset_protected(arg(1));
}
return TRUE;
}
function protected_node_set_protected($param, $passwd = NULL) {
$node = node_load($param);
if ($node == FALSE) {
return FALSE;
}
if (empty($node->protected_node_is_protected)) {
$select = db_select('protected_nodes')
->fields('protected_nodes', array(
'nid',
))
->condition('nid', $node->nid)
->execute()
->fetchField();
if ($select) {
if (empty($passwd)) {
$result = db_update('protected_nodes')
->fields(array(
'protected_node_is_protected' => 1,
))
->condition('nid', $node->nid)
->execute() !== FALSE;
}
else {
$result = db_update('protected_nodes')
->fields(array(
'protected_node_is_protected' => 1,
'protected_node_passwd' => hash('sha256', $passwd),
'protected_node_passwd_changed' => REQUEST_TIME,
))
->condition('nid', $node->nid)
->execute() !== FALSE;
}
}
else {
if (empty($passwd)) {
$passwd = '';
}
else {
$passwd = hash('sha256', $passwd);
}
$result = db_insert('protected_nodes')
->fields(array(
'nid' => $node->nid,
'protected_node_is_protected' => 1,
'protected_node_passwd' => $passwd,
'protected_node_show_title' => variable_get('protected_node_show_node_titles', FALSE),
))
->execute() !== FALSE;
}
}
else {
if (empty($passwd)) {
return TRUE;
}
$result = db_update('protected_nodes')
->fields(array(
'protected_node_passwd' => hash('sha256', $passwd),
'protected_node_passwd_changed' => REQUEST_TIME,
))
->condition('nid', $node->nid)
->execute() !== FALSE;
}
return $result;
}
function protected_node_unset_protected($nid) {
$result = db_select('protected_nodes')
->fields('protected_nodes', array(
'protected_node_is_protected',
))
->condition('nid', $nid)
->execute()
->fetchField() == 1;
db_update('protected_nodes')
->fields(array(
'protected_node_is_protected' => 0,
))
->condition('nid', $nid)
->execute();
return $result;
}
function protected_node_isset_protected($nid) {
if (!is_numeric($nid)) {
return FALSE;
}
$result = db_select('protected_nodes')
->fields('protected_nodes', array(
'protected_node_is_protected',
))
->condition('nid', $nid)
->execute()
->fetchField() == 1;
return $result;
}
function protected_node_lock($nid) {
if (is_numeric($nid) && isset($_SESSION['_protected_node']['passwords']['global'])) {
unset($_SESSION['_protected_node']['passwords']['global']);
return TRUE;
}
if (is_numeric($nid) && isset($_SESSION['_protected_node']['passwords'][$nid])) {
unset($_SESSION['_protected_node']['passwords'][$nid]);
return TRUE;
}
return FALSE;
}
function protected_node_unlock($nid) {
if (is_numeric($nid)) {
$node = node_load($nid);
if ($node->protected_node_is_protected) {
if (isset($_SESSION['has_entered_global_password'])) {
$_SESSION['_protected_node']['passwords']['global'] = REQUEST_TIME;
}
else {
$_SESSION['_protected_node']['passwords'][$nid] = REQUEST_TIME;
}
return TRUE;
}
}
return FALSE;
}
function _protected_node_check_view_mode($view_mode) {
$permitted_view_modes = variable_get('protected_node_checked_view_modes', _protected_node_get_default_checked_view_modes());
return in_array($view_mode, $permitted_view_modes);
}
function _protected_node_get_default_checked_view_modes() {
$default_view_modes =& drupal_static(__FUNCTION__);
if (!isset($default_view_modes)) {
$node_info = entity_get_info('node');
$default_view_modes = array();
foreach ($node_info['view modes'] as $id => $item) {
$default_view_modes[] = $id;
}
}
return $default_view_modes;
}
function _protected_node_get_failed_password_ip_limit_options() {
return drupal_map_assoc(array(
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
20,
30,
40,
50,
75,
100,
125,
150,
200,
250,
500,
));
}
function _protected_node_get_failed_password_ip_window_options() {
return array(
0 => t('None (flood control disabled)'),
) + drupal_map_assoc(array(
60,
180,
300,
600,
900,
1800,
2700,
3600,
10800,
21600,
32400,
43200,
86400,
), 'format_interval');
}
function _protected_node_get_paragraph_node_host_entity_id(ParagraphsItemEntity $paragraphs_item) {
paragraphs_item_get_host_entity($paragraphs_item);
$host_entity_type = $paragraphs_item
->hostEntityType();
if ($host_entity_type == 'node') {
return $paragraphs_item
->hostEntity()->nid;
}
elseif ($host_entity_type == 'paragraphs_item') {
$host_id = array(
$paragraphs_item
->hostEntityId(),
);
$host_paragraphs_items = entity_load('paragraphs_item', $host_id);
return _protected_node_get_paragraph_node_host_entity_id(array_shift($host_paragraphs_items));
}
return FALSE;
}