View source
<?php
define('SITE_DISCLAIMER_NODE_TITLE', t('I prefer to specify the title of the post'));
define('SITE_DISCLAIMER_NODE_ID', t('I prefer to specify the node id'));
function _site_disclaimer_checkbox_label_all_translations() {
$ret = array();
if (module_exists('i18n')) {
$result = db_query("SELECT * FROM {i18n_variable} WHERE name='%s'", 'site_disclaimer_checkbox_label');
while ($variable = db_fetch_object($result)) {
if ($variable->language) {
$ret[$variable->language] = filter_xss_admin(unserialize($variable->value));
}
}
}
else {
global $language;
$ret[$language->language] = filter_xss_admin(variable_get('site_disclaimer_checkbox_label', SITE_DISCLAIMER_DEFAULT_CHECKBOX_LABEL));
}
return $ret;
}
function _site_disclaimer_admin_decode_settings(&$form_state, $validate = FALSE) {
$form_state += array(
'values' => array(),
);
$form_state += array(
'storage' => array(),
);
$is_form_submitted = !empty($form_state['values']['op']);
$orig_mode = isset($form_state['storage']['#site_disclaimer_mode']) ? $form_state['storage']['#site_disclaimer_mode'] : (isset($form_state['values']['site_disclaimer_node_id']) ? 'node_id' : 'node_title');
$mode = $orig_mode;
$form_state['storage']['#site_disclaimer_mode_switch'] = FALSE;
if ($is_form_submitted) {
if ($form_state['values']['op'] == SITE_DISCLAIMER_NODE_ID) {
$mode = 'node_id';
$form_state['storage']['#site_disclaimer_mode_switch'] = $mode;
}
elseif ($form_state['values']['op'] == SITE_DISCLAIMER_NODE_TITLE) {
$mode = 'node_title';
$form_state['storage']['#site_disclaimer_mode_switch'] = $mode;
}
if (!$validate) {
$mode = $orig_mode;
}
}
if (!$is_form_submitted && !$validate) {
$mode = 'node_title';
}
$form_state['storage']['#site_disclaimer_mode'] = $mode;
$title = '';
$nid = '';
if ($orig_mode == 'node_title') {
if (!$is_form_submitted) {
$title = variable_get('site_disclaimer_node_title', '');
}
elseif (!empty($form_state['values']['site_disclaimer_node_title'])) {
$title = $form_state['values']['site_disclaimer_node_title'];
}
if (!empty($title)) {
$node = node_load(array(
'title' => $title,
));
if (empty($node)) {
if ($is_form_submitted) {
form_set_error('site_disclaimer_node_title', t('No post was found with "!title" title.', array(
'!title' => $title,
)));
}
}
else {
$nid = $node->nid;
}
}
}
else {
if (!$is_form_submitted) {
$nid = variable_get('site_disclaimer_node_id', '');
}
elseif (!empty($form_state['values']['site_disclaimer_node_id'])) {
$nid = $form_state['values']['site_disclaimer_node_id'];
}
if (!empty($nid)) {
$node = node_load($nid);
if (empty($node)) {
if ($is_form_submitted) {
form_set_error('site_disclaimer_node_id', t('No post was found with nid !nid.', array(
'!nid' => $nid,
)));
}
}
else {
$title = $node->title;
}
}
}
$form_state['values']['site_disclaimer_node_title'] = $title;
$form_state['values']['site_disclaimer_node_id'] = $nid;
$form_state['storage']['#site_disclaimer_nid'] = $nid;
}
function site_disclaimer_admin_settings(&$form_state) {
_site_disclaimer_admin_decode_settings($form_state);
$form['site_disclaimer_text'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="fieldset-wrapper">',
'#suffix' => '</div>',
'#title' => t('Node to include with Site Disclaimer'),
'#description' => t('This node typically contains legal statements such as Site Disclaimer, Terms of Use, etc. It is either included entirely in the Site Disclaimer form, or linked from the [x] ckeckbox label (if @link token is used). The node should already exist.'),
);
if ($form_state['storage']['#site_disclaimer_mode'] == 'node_title') {
$form['site_disclaimer_text']['site_disclaimer_node_title'] = array(
'#type' => 'textfield',
'#title' => t('Node title'),
'#default_value' => $form_state['values']['site_disclaimer_node_title'],
'#description' => t('Enter the <em>title</em> of an existing node. Leave empty and also use no token in the [x] checkbox label below to disable Site Disclaimer.'),
'#autocomplete_path' => 'site_disclaimer/autocomplete',
);
$form['site_disclaimer_text']['site_disclaimer_pick_node_id'] = array(
'#type' => 'button',
'#weight' => 10,
'#value' => SITE_DISCLAIMER_NODE_ID,
'#ahah' => array(
'path' => 'site_disclaimer/js',
'wrapper' => 'fieldset-wrapper',
),
);
}
else {
$form['site_disclaimer_text']['site_disclaimer_node_id'] = array(
'#type' => 'textfield',
'#title' => t('Node id'),
'#default_value' => $form_state['values']['site_disclaimer_node_id'],
'#description' => t('Enter the <em>id</em> of an existing node. Leave empty and also use no token in the [x] checkbox label below to disable Site Disclaimer.'),
);
$form['site_disclaimer_text']['site_disclaimer_pick_node_title'] = array(
'#type' => 'button',
'#weight' => 10,
'#value' => SITE_DISCLAIMER_NODE_TITLE,
'#ahah' => array(
'path' => 'site_disclaimer/js',
'wrapper' => 'fieldset-wrapper',
),
);
}
$form['site_disclaimer_form'] = array(
'#type' => 'fieldset',
'#title' => t('Site Disclaimer form settings'),
);
$form['site_disclaimer_form']['site_disclaimer_title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => isset($form_state['values']['site_disclaimer_title']) ? $form_state['values']['site_disclaimer_title'] : variable_get('site_disclaimer_title', SITE_DISCLAIMER_DEFAULT_TITLE),
'#description' => t('Enter a title to put on the Site Disclaimer form.'),
);
$form['site_disclaimer_form']['site_disclaimer_fieldset'] = array(
'#type' => 'checkbox',
'#title' => t('Enclose in a fieldset'),
'#default_value' => isset($form_state['values']['site_disclaimer_fieldset']) ? $form_state['values']['site_disclaimer_fieldset'] : variable_get('site_disclaimer_fieldset', 1),
'#description' => t('Enclose the Site Disclaimer form and the [x] checkbox in a fieldset. Helps visually group the form as typically themes display fieldset in a box / add borders.'),
);
$form['site_disclaimer_form']['site_disclaimer_checkbox_label'] = array(
'#type' => 'textfield',
'#maxlength' => 256,
'#title' => t('[x] checkbox label'),
'#default_value' => isset($form_state['values']['site_disclaimer_checkbox_label']) ? $form_state['values']['site_disclaimer_checkbox_label'] : variable_get('site_disclaimer_checkbox_label', SITE_DISCLAIMER_DEFAULT_CHECKBOX_LABEL),
'#description' => t('Enter a Site Disclaimer statement that visitors have to accept, something like <b>I agree with these terms</b>, or <b>I CERTIFY THAT I AM OVER THE AGE OF 18 YEARS OLD</b>.!tokens!example<p>You may want to link to the Terms page if you prefer not to show the full text of the Terms in the registration form.</p><p>If you use any tokens, the full text of the Terms will not be shown.</p>', array(
'!tokens' => t('<p>The following tokens are substituted on the label:</p><ul><li>@link - a link to the Site Disclaimer node (must be provided above).</li><li>@"Post Title" - a link to a post with title <b>Post Title</b>. If post title contains a double quote, prepend it with a backslash \\".</li></ul>'),
'!example' => t('<p>For example, the label can be: "I agree with the @link", without quotes.</p>'),
)),
);
$form['site_disclaimer_form']['site_disclaimer_node_height'] = array(
'#type' => 'textfield',
'#title' => t('Max height'),
'#default_value' => isset($form_state['values']['site_disclaimer_node_height']) ? $form_state['values']['site_disclaimer_node_height'] : variable_get('site_disclaimer_node_height', ''),
'#description' => t('Leave empty to show Site Disclaimer form with text in full height. Enter something like <b>20em</b> or <b>120px</b> to set the height of the Site Disclaimer form. Vertical scrollbar is added automatically if text height exceeds the max height setting.'),
);
$form['site_disclaimer_advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Site Disclaimer version'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$version = variable_get('site_disclaimer_version', 1);
$options = array(
$version => t('@version (current version)', array(
'@version' => $version,
)),
$version + 1 => t('@version (add a new version)', array(
'@version' => $version + 1,
)),
);
$form['site_disclaimer_advanced']['site_disclaimer_version'] = array(
'#type' => 'radios',
'#title' => t('Add new version'),
'#description' => t('WARNING: Changing Site Disclaimer version is not reversible. It will require every already registered user to accept the new Site Disclaimer terms upon their login before they can do anything else on the website.'),
'#options' => $options,
'#default_value' => isset($form_state['values']['site_disclaimer_version']) ? $form_state['values']['site_disclaimer_version'] : $version,
);
$form['site_disclaimer_advanced']['site_disclaimer_version_details'] = array(
'#type' => 'textarea',
'#title' => t('Changes'),
'#description' => t('Describe changes in the latest version. Note: if you are not changing version, this description will be shown to all users who have not accepted the current version.'),
'#default_value' => isset($form_state['values']['site_disclaimer_version_details']) ? $form_state['values']['site_disclaimer_version_details'] : variable_get('site_disclaimer_version_details', ''),
);
$form['site_disclaimer_preview'] = array(
'#type' => 'fieldset',
'#title' => t('Preview'),
'#description' => t('This is a preview of current configuration. To see how your changes will look, you have to save configuration.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['site_disclaimer_preview']['user_register'] = array(
'#type' => 'fieldset',
'#title' => t('User registration page'),
'#description' => '',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$preview_form = array(
'#parameters' => array(
'user_register',
),
);
$form_state_tmp = array();
site_disclaimer_form_user_register_alter($preview_form, $form_state_tmp, NULL, TRUE);
if (!empty($preview_form)) {
$preview_form['#type'] = 'item';
$form['site_disclaimer_preview']['user_register']['form']['#value'] = drupal_render($preview_form);
}
else {
$form['site_disclaimer_preview']['user_register']['#description'] = '<br />' . t('(Site Disclaimer disabled)');
}
$form['site_disclaimer_preview']['accept_new'] = array(
'#type' => 'fieldset',
'#title' => t('Accept changes page'),
'#description' => '',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
module_load_include('inc', 'site_disclaimer', 'site_disclaimer.pages');
$form_state_tmp = array();
$preview_confirm = site_disclaimer_confirm($form_state_tmp, TRUE);
if (!empty($preview_confirm)) {
$preview_confirm['#type'] = 'item';
$form['site_disclaimer_preview']['accept_new']['form_confirm']['#value'] = drupal_render($preview_confirm);
}
else {
$form['site_disclaimer_preview']['accept_new']['form_confirm']['#value'] = '<br />' . t('(Site Disclaimer disabled)');
}
$form = system_settings_form($form);
$form['#submit'][] = 'site_disclaimer_admin_settings_submit';
return $form;
}
function site_disclaimer_admin_settings_validate($form, &$form_state) {
_site_disclaimer_admin_decode_settings($form_state, TRUE);
if ($form_state['storage']['#site_disclaimer_mode_switch']) {
return;
}
$checkbox_label = filter_xss_admin($form_state['values']['site_disclaimer_checkbox_label']);
$use_link = strpos($checkbox_label, '@link') !== FALSE;
$use_named_links = strpos($checkbox_label, '@"') !== FALSE;
$output = _site_disclaimer_checkbox_label_substitute_links($checkbox_label, $linked_nodes, $linked_errors);
foreach ($linked_errors as $titla => $error) {
form_set_error('site_disclaimer_checkbox_label', $error);
}
$form_state['storage']['#site_disclaimer_named_links'] = $linked_nodes;
if ($form_state['storage']['#site_disclaimer_mode'] = 'node_id') {
if (empty($form_state['values']['site_disclaimer_node_id'])) {
if ($use_link) {
form_set_error('site_disclaimer_node_id', t('You used @link token. You must specify a node <em>nid</em>.'));
}
else {
drupal_set_message($use_named_links ? t('Site Disclaimer will use links in the checkbox.') : t('Site Disclaimer disabled.'));
}
}
}
elseif (empty($form_state['values']['site_disclaimer_node_title'])) {
if ($use_link) {
form_set_error('site_disclaimer_node_title', t('You used @link token. You must specify a node title.'));
}
else {
drupal_set_message($use_named_links ? t('Site Disclaimer will use links in the checkbox.') : t('Site Disclaimer disabled.'));
}
}
}
function site_disclaimer_admin_settings_submit($form, &$form_state) {
if ($form_state['storage']['#site_disclaimer_mode'] = 'node_id') {
$nid = $form_state['values']['site_disclaimer_node_id'];
}
else {
$nid = $form_state['storage']['#site_disclaimer_nid'];
}
unset($form_state['storage']);
_site_disclaimer_prep_allowed_paths($nid);
}
function site_disclaimer_autocomplete($string = '') {
$matches = array();
if ($string != '') {
$result = db_query_range(db_rewrite_sql("SELECT nr.title FROM {node_revisions} nr WHERE LOWER(nr.title) LIKE LOWER('%%%s%%')", 'nr'), $string, 0, 10);
while ($node = db_fetch_object($result)) {
$matches[$node->title] = $node->title;
}
}
drupal_json($matches);
}
function site_disclaimer_js() {
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);
$form_build_id = check_plain($_POST['form_build_id']);
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
$form_state['submitted'] = TRUE;
$form_state['rebuild'] = TRUE;
if (!isset($_POST['op'])) {
$form['#validate'] = NULL;
$form['#submit'] = NULL;
$form_state['submit_handlers'] = NULL;
$form_state['validate_handlers'] = NULL;
_site_disclaimer_disable_validation($form);
}
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
$javascript = drupal_add_js(NULL, NULL, 'header');
$settings = call_user_func_array('array_merge_recursive', $javascript['setting']);
$pos = isset($_SESSION['messages']['error']) ? array_search(t('The settings have not been saved because of the errors.'), $_SESSION['messages']['error']) : FALSE;
if ($pos !== FALSE) {
unset($_SESSION['messages']['error'][$pos]);
}
$output = theme('status_messages') . drupal_render($form['site_disclaimer_text']);
drupal_json(array(
'status' => TRUE,
'data' => $output,
'settings' => array(
'ahah' => $settings['ahah'],
),
));
}
function _site_disclaimer_disable_validation(&$form) {
foreach (element_children($form) as $child) {
$form[$child]['#validated'] = TRUE;
_site_disclaimer_disable_validation($form[$child]);
}
}
function _site_disclaimer_absolute_content_path($url) {
global $base_url;
$uri = @parse_url($url);
if ($uri == FALSE) {
return NULL;
}
if (!isset($uri['scheme'])) {
return NULL;
}
if (!in_array($uri['scheme'], array(
'http',
'https',
))) {
return NULL;
}
$scheme = isset($uri['scheme']) ? $uri['scheme'] . '://' : '';
$user = isset($uri['user']) ? $uri['user'] . ($uri['pass'] ? ':' . $uri['pass'] : '') . '@' : '';
$port = isset($uri['port']) ? $uri['port'] : 80;
$host = $uri['host'] . ($port != 80 ? ':' . $port : '');
$path = isset($uri['path']) ? $uri['path'] : '/';
$absolute_url = $scheme . $user . $host . $path;
if (strpos($base_url . '/', $absolute_url) === 0) {
return $base_url . '/';
}
$last_slash = strrpos($absolute_url, '/');
$absolute_content_url = drupal_substr($absolute_url, 0, $last_slash + 1);
return $absolute_content_url;
}
function _site_disclaimer_extract_links($text = '', $content_path = NULL) {
global $base_root;
$matches_a = array(
1 => NULL,
);
if (1 || variable_get('linkchecker_extract_from_a', 1) == 1) {
$pattern_a = '/<(?:a|area)\\s[^>]*href=["\']([^"\']*)["\'][^>]*>/i';
preg_match_all($pattern_a, $text, $matches_a);
}
$matches_audio = array(
1 => NULL,
);
if (0 && variable_get('linkchecker_extract_from_audio', 1) == 1) {
$pattern_audio = '/<audio\\s[^>]*src=["\']([^"\']*)["\'][^>]*>/i';
preg_match_all($pattern_audio, $text, $matches_audio);
}
$matches_embed = array();
if (0 && variable_get('linkchecker_extract_from_embed', 0) == 1) {
$pattern_embed_src = '/<embed\\s[^>]*src=["\']([^"\']*)["\'][^>]*>/i';
$pattern_embed_pluginurl = '/<embed\\s[^>]*pluginurl=["\']([^"\']*)["\'][^>]*>/i';
$pattern_embed_pluginspage = '/<embed\\s[^>]*pluginspage=["\']([^"\']*)["\'][^>]*>/i';
preg_match_all($pattern_embed_src, $text, $matches_embed_src);
preg_match_all($pattern_embed_pluginurl, $text, $matches_embed_pluginurl);
preg_match_all($pattern_embed_pluginspage, $text, $matches_embed_pluginspage);
$matches_embed = array_merge((array) $matches_embed_src[1], (array) $matches_embed_pluginurl[1], (array) $matches_embed_pluginspage[1]);
}
$matches_iframe = array(
1 => NULL,
);
if (1 || variable_get('linkchecker_extract_from_iframe', 0) == 1) {
$pattern_iframe = '/<iframe\\s[^>]*src=["\']([^"\']*)["\'][^>]*>/i';
preg_match_all($pattern_iframe, $text, $matches_iframe);
}
$matches_img = array(
1 => NULL,
);
if (1 || variable_get('linkchecker_extract_from_img', 0) == 1) {
$pattern_img = '/<img\\s[^>]*src=["\']([^"\']*)["\'][^>]*>/i';
preg_match_all($pattern_img, $text, $matches_img);
}
$matches_object = array();
if (0 && variable_get('linkchecker_extract_from_object', 0) == 1) {
$pattern_object_data = '/<object\\s[^>]*data=["\']([^"\']*)["\'][^>]*>/i';
$pattern_object_codebase = '/<object\\s[^>]*codebase=["\']([^"\']*)["\'][^>]*>/i';
$pattern_param = '/<param\\s[^>]*((name|src)=["\'](archive|filename|href|movie|src|url)["\']\\s[^>]*)+value=["\']([^"\']*)["\'][^>]*>/i';
preg_match_all($pattern_object_data, $text, $matches_object_data);
preg_match_all($pattern_object_codebase, $text, $matches_object_codebase);
preg_match_all($pattern_param, $text, $matches_param);
$matches_object = array_merge((array) $matches_object_data[1], (array) $matches_object_codebase[1], (array) $matches_param[4]);
}
$matches_source = array(
1 => NULL,
);
if (0 || variable_get('linkchecker_extract_from_source', 0) == 1) {
$pattern_source = '/<source\\s[^>]*src=["\']([^"\']*)["\'][^>]*>/i';
preg_match_all($pattern_source, $text, $matches_source);
}
$matches_video = array();
if (0 || variable_get('linkchecker_extract_from_video', 0) == 1) {
$pattern_video_poster = '/<video\\s[^>]*poster=["\']([^"\']*)["\'][^>]*>/i';
$pattern_video_src = '/<video\\s[^>]*src=["\']([^"\']*)["\'][^>]*>/i';
preg_match_all($pattern_video_poster, $text, $matches_video_poster);
preg_match_all($pattern_video_src, $text, $matches_video_src);
$matches_video = array_merge((array) $matches_video_poster[1], (array) $matches_video_src[1]);
}
$urls = array_merge((array) $matches_a[1], (array) $matches_audio[1], (array) $matches_embed, (array) $matches_iframe[1], (array) $matches_img[1], (array) $matches_object, (array) $matches_source[1], (array) $matches_video);
$urls = array_filter($urls);
$urls = array_unique($urls);
$linkchecker_check_links_types = variable_get('linkchecker_fqdn_only', 1);
$links = array();
foreach ($urls as $url) {
$url_decoded = decode_entities($url);
$url_encoded = str_replace(' ', '%20', $url_decoded);
if ((1 || $linkchecker_check_links_types != 2) && valid_url($url_encoded, TRUE)) {
$links[$url_decoded][] = $url;
}
elseif (preg_match('/^\\w[\\w.+]*:/', $url_decoded)) {
continue;
}
elseif ((1 || $linkchecker_check_links_types != 1) && valid_url($url_encoded, FALSE)) {
$absolute_content_path = _site_disclaimer_absolute_content_path($content_path);
if (preg_match('!^/!', $url_decoded)) {
$links[$base_root . $url_decoded][] = $url;
}
elseif (!empty($content_path) && preg_match('!^[?#]!', $url_decoded)) {
$links[$content_path . $url_decoded][] = $url;
}
elseif (!empty($absolute_content_path) && preg_match('!^\\.{1,2}/!', $url_decoded)) {
$path = substr_replace($absolute_content_path . $url_decoded, '', 0, drupal_strlen($base_root));
$path = str_replace('/./', '/', $path);
$last = '';
while ($path != $last) {
$last = $path;
$path = preg_replace('`(^|/)(?!\\.\\./)([^/]+)/\\.\\./`', '$1', $path);
}
$links[$base_root . $path][] = $url;
}
elseif (!empty($absolute_content_path) && preg_match('!^[^/]!', $url_decoded)) {
$links[$absolute_content_path . $url_decoded][] = $url;
}
else {
}
}
}
return $links;
}
function _site_disclaimer_prep_allowed_paths($nid, $delete_nid = NULL) {
$allow_nids = array(
$nid,
);
$languages = language_list();
$nids = _site_disclaimer_nid_all_translations($nid);
$checkbox_labels = _site_disclaimer_checkbox_label_all_translations();
foreach ($checkbox_labels as $langcode => $checkbox_label) {
$use_link = strpos($checkbox_label, '@link') !== FALSE;
if ($use_link && !empty($nids[$langcode])) {
$allow_nids[] = $nids[$langcode];
}
_site_disclaimer_checkbox_label_substitute_links($checkbox_label, $linked_nodes);
$allow_nids += $linked_nodes;
}
$links = array();
foreach ($nids as $langcode => $tnid) {
$node = node_load($tnid);
if ($node) {
$node = node_prepare($node);
$node_language = !empty($node->language) ? $node->language : 'und';
$path = url('node/' . $node->nid, array(
'language' => $node_language == 'und' ? NULL : $languages[$node_language],
'absolute' => TRUE,
));
$urls = array_keys(_site_disclaimer_extract_links($node->body, $path));
$links += array(
$langcode => array(),
);
$links[$langcode] += $urls;
}
}
global $base_url;
$len = drupal_strlen($base_url) + 1;
foreach ($links as $langcode => $urls) {
foreach ($urls as $link) {
if (strpos($link, $base_url . '/') === 0) {
$start = $len;
$no_clean_url = strpos($link, '?q=');
if ($no_clean_url !== FALSE) {
$start = $no_clean_url + 3;
}
$end = strpos($link, '?', $start);
if ($end === FALSE) {
$end = strpos($link, '#', $start);
}
if ($end === FALSE) {
$end = drupal_strlen($link);
}
$link = drupal_substr($link, $start, $end - $start);
$path = drupal_get_normal_path($link, $langcode);
if (!$path || $path == $link) {
$path = drupal_get_normal_path($link, '');
}
if (!$path || $path == $link) {
$path = drupal_get_normal_path($link, language_default('language'));
}
$ret = preg_match('!^node/([0-9]+)!i', $path, $matches);
if ($ret == 1 && is_numeric($matches[1]) && (!isset($delete_nid) || $delete_nid != $matches[1])) {
$allow_nids[] = $matches[1];
}
}
}
}
$allow_nids = array_unique($allow_nids);
variable_set('site_disclaimer_allow_nodes', $allow_nids);
}