View source
<?php
define('MIN_CONTENT_ACTION_DO_NOT_DISPLAY', 0);
define('MIN_CONTENT_ACTION_ADDITIONAL_FILTER', 1);
define('SHOW_ONCE_OPTIONS_ALWAYS', 0);
define('SHOW_ONCE_OPTIONS_ONCE', 1);
function signature_forum_help($path, $arg) {
switch ($path) {
case "admin/modules#description":
return t("Tweaks signatures in ways inspired by other traditional forum software. Allows much longer signatures than the Drupal default; also users may be allowed to use different formats like BBCode (with the BBCode module downloadable from drupal.org) or HTML in their signatures.");
}
}
function signature_forum_perm() {
return array(
'administer Signatures for Forums',
);
}
function signature_forum_menu() {
$items['admin/settings/signature_forum'] = array(
'title' => 'Signatures for Forums',
'description' => "Manages users' signatures.",
'page callback' => 'drupal_get_form',
'page arguments' => array(
'signature_forum_admin_settings',
),
'access arguments' => array(
'administer Signatures for Forums',
),
);
return $items;
}
function signature_forum_admin_settings() {
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
if (!variable_get('user_signatures', FALSE)) {
drupal_set_message(t('Note: signatures are switched off, enable them under <a href="@settings-page">Administer -> User management -> User settings</a>', array(
'@settings-page' => url('admin/user/settings'),
)));
}
$form['signature'] = array(
'#type' => 'fieldset',
'#title' => t('Show signatures with nodes and comments for'),
);
foreach (node_get_types('names') as $type => $name) {
$form['signature']['signature_forum_show_for_' . $type] = array(
'#type' => 'checkbox',
'#title' => $name,
'#return_value' => 1,
'#default_value' => isset($settings['signature_forum_show_for_' . $type]) ? $settings['signature_forum_show_for_' . $type] : FALSE,
);
}
$form['template'] = array(
'#type' => 'textarea',
'#title' => t('Template for signatures'),
'#default_value' => $settings['signature_forum_template'],
'#description' => t("%s will be replaced with the user's signature."),
);
$form['format'] = filter_form($settings['signature_forum_format']);
$form['content_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Minimum content length'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['content_settings']['min_content_length'] = array(
'#type' => 'textfield',
'#title' => t('Minimum content length'),
'#size' => 3,
'#maxlength' => 10,
'#default_value' => $settings['signature_forum_min_content_length'],
'#description' => t('The minimum number of characters in the content a signature is being attached to. 0 means no limit.'),
);
$form['content_settings']['min_content_length_action'] = array(
'#type' => 'radios',
'#title' => t('Minimum content action'),
'#default_value' => $settings['signature_forum_min_content_length_action'],
'#options' => array(
MIN_CONTENT_ACTION_DO_NOT_DISPLAY => t('Do not display signature'),
MIN_CONTENT_ACTION_ADDITIONAL_FILTER => t('Run through an additional filter'),
),
'#description' => t('What to do if the content is under the minimum length. Set the filter below.'),
);
$form['content_settings']['min_content_length_filter'] = filter_form($settings['signature_forum_min_content_length_filter'], NULL, array(
'min_content_length_filter',
));
$form['content_settings']['min_content_length_filter']['#title'] = t('Minimum content additional filter format (if enabled)');
if ($settings['signature_forum_min_content_length_action'] == MIN_CONTENT_ACTION_ADDITIONAL_FILTER) {
$form['content_settings']['min_content_length_filter']['#collapsed'] = FALSE;
}
$roles = user_roles(TRUE);
$form['content_settings']['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Exempt roles'),
'#default_value' => isset($settings['signature_forum_roles']) ? $settings['signature_forum_roles'] : array(),
'#options' => $roles,
'#description' => t('Members of these roles will be exempt from content length settings.'),
);
$form['show_once'] = array(
'#type' => 'fieldset',
'#title' => t('Per-conversation signatures'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['show_once']['show_once_options'] = array(
'#type' => 'radios',
'#title' => t("Show a user's signature"),
'#default_value' => $settings['signature_forum_show_once_options'],
'#options' => array(
SHOW_ONCE_OPTIONS_ALWAYS => t('Always'),
SHOW_ONCE_OPTIONS_ONCE => t('Once per conversation'),
),
);
$form['show_once']['show_once_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Exempt roles'),
'#default_value' => isset($settings['signature_forum_show_once_roles']) ? $settings['signature_forum_show_once_roles'] : array(),
'#options' => $roles,
'#description' => t('Members of these roles will have their signatures shown in every post.'),
);
$form['signature_defaults'] = array(
'#type' => 'fieldset',
'#title' => t('Per-post settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Users will see a checkbox below the comment form allowing them to choose whether or not to use their signature. If signatures are not enabled for a content type, this setting will have no effect.'),
);
if (module_exists('comment')) {
$form['signature_defaults']['signature_forum_default_comments'] = array(
'#type' => 'checkbox',
'#title' => t('Core Comments'),
'#return_value' => 1,
'#default_value' => isset($settings['signature_forum_default_comments']) ? $settings['signature_forum_default_comments'] : FALSE,
);
}
foreach (node_get_types('names') as $type => $name) {
$form['signature_defaults']['signature_forum_default_for_' . $type] = array(
'#type' => 'checkbox',
'#title' => $name,
'#return_value' => 1,
'#default_value' => isset($settings['signature_forum_default_for_' . $type]) ? $settings['signature_forum_default_for_' . $type] : FALSE,
);
}
$form['signature_defaults']['signature_forum_allow_user_default'] = array(
'#type' => 'radios',
'#title' => t('Allow users to choose their own global default'),
'#default_value' => isset($settings['signature_forum_allow_user_default']) ? $settings['signature_forum_allow_user_default'] : 0,
'#options' => array(
t('No'),
t('Yes'),
),
'#description' => t("This will add an option in a user's profile settings where they can choose whether their signature should be used by default."),
);
$form['signature_other'] = array(
'#type' => 'fieldset',
'#title' => t('Other options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['signature_other']['line_limit'] = array(
'#type' => 'textfield',
'#title' => t('Line limit'),
'#size' => 3,
'#maxlength' => 10,
'#default_value' => $settings['signature_forum_line_limit'],
'#description' => t('The maximum number of lines allowed in a signature. 0 means no limit. Note: existing signatures that are too long will not be changed.'),
);
$form['signature_other']['char_limit'] = array(
'#type' => 'textfield',
'#title' => t('Character limit'),
'#size' => 5,
'#maxlength' => 10,
'#default_value' => $settings['signature_forum_char_limit'],
'#description' => t('The maximum number of characters allowed in a signature. 0 means no limit. Note: existing signatures that are too long will not be changed.'),
);
$form['signature_other']['delete_signatures'] = array(
'#type' => 'checkbox',
'#title' => t('Delete embedded signatures'),
'#description' => t('Deletes signatures that are embedded in existing comments (warning: cannot be undone!).'),
'#default_value' => FALSE,
);
$form['signature_other']['auto_insert'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically add signatures to content'),
'#description' => t('If this is switched on signatures will automatically be added to content. Themers may wish to switch this off, so the signature may be positioned in the templates manually.'),
'#default_value' => $settings['signature_forum_auto_insert'],
);
$form = system_settings_form($form);
unset($form['#submit']);
$form['#submit'][] = 'signature_forum_admin_settings_submit';
return $form;
}
function signature_forum_defaults() {
return array(
'signature_forum_template' => "__________________\n<p>%s</p>",
'signature_forum_format' => FILTER_FORMAT_DEFAULT,
'signature_forum_line_limit' => 0,
'signature_forum_char_limit' => 0,
'signature_forum_min_content_length' => 0,
'signature_forum_min_content_length_action' => MIN_CONTENT_ACTION_DO_NOT_DISPLAY,
'signature_forum_min_content_length_filter' => FILTER_FORMAT_DEFAULT,
'signature_forum_show_once_options' => SHOW_ONCE_OPTIONS_ALWAYS,
'signature_forum_allow_user_default' => 1,
'signature_forum_auto_insert' => module_exists('advanced_forum') ? FALSE : TRUE,
);
}
function signature_forum_admin_settings_submit($form, &$form_state) {
if ($form_state['clicked_button']['#value'] == 'Reset to defaults') {
variable_set('signature_forum_settings', signature_forum_defaults());
drupal_set_message(t('The configuration options have been reset to their default values.'));
return;
}
$settings = array();
foreach ($form_state['values'] as $form_value_key => $form_value_value) {
if (drupal_substr($form_value_key, 0, drupal_strlen('signature_forum_show_for_')) == 'signature_forum_show_for_') {
$settings[$form_value_key] = $form_value_value;
}
elseif (drupal_substr($form_value_key, 0, drupal_strlen('signature_forum_default_for_')) == 'signature_forum_default_for_') {
$settings[$form_value_key] = $form_value_value;
}
elseif ($form_value_key == 'signature_forum_default_comments') {
$settings[$form_value_key] = $form_value_value;
}
}
if ($form_state['values']['delete_signatures']) {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("UPDATE {comments} c INNER JOIN {users_signature} u ON c.uid=u.uid\n SET c.comment=left(c.comment, length(c.comment) - length(u.signature))\n WHERE right(c.comment, length(u.signature)) LIKE u.signature;");
break;
case 'pgsql':
db_query("UPDATE {comments} SET comment=substring({comments}.comment from 1 for length({comments}.comment)-length({users_signature}.signature))\n FROM {users_signature}\n WHERE {comments}.comment LIKE '%' || {users_signature}.signature");
break;
}
cache_clear_all(NULL, 'cache_page');
cache_clear_all(NULL, 'cache_filter');
drupal_set_message(t('Signatures in comments deleted.'));
}
$settings['signature_forum_template'] = $form_state['values']['template'];
$settings['signature_forum_format'] = $form_state['values']['format'];
$settings['signature_forum_line_limit'] = $form_state['values']['line_limit'];
$settings['signature_forum_char_limit'] = $form_state['values']['char_limit'];
$settings['signature_forum_min_content_length'] = $form_state['values']['min_content_length'];
$settings['signature_forum_min_content_length_action'] = $form_state['values']['min_content_length_action'];
$settings['signature_forum_min_content_length_filter'] = $form_state['values']['min_content_length_filter'];
$settings['signature_forum_roles'] = $form_state['values']['roles'];
$settings['signature_forum_show_once_options'] = $form_state['values']['show_once_options'];
$settings['signature_forum_show_once_roles'] = $form_state['values']['show_once_roles'];
$settings['signature_forum_auto_insert'] = $form_state['values']['auto_insert'];
$settings['signature_forum_allow_user_default'] = $form_state['values']['signature_forum_allow_user_default'];
variable_set('signature_forum_settings', $settings);
drupal_set_message(t('The configuration options have been saved.'));
}
function signature_forum_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'submit':
if (!isset($edit['signature'])) {
break;
}
if (db_result(db_query("SELECT uid FROM {users_signature} WHERE uid = %d", $account->uid)) != FALSE) {
db_query("UPDATE {users_signature} SET signature = '%s', status = %d WHERE uid = %d", array(
$edit['signature'],
$edit['signature_default_status'],
$account->uid,
));
}
else {
db_query("INSERT INTO {users_signature} (uid, signature, status) VALUES (%d, '%s', %d)", array(
$account->uid,
$edit['signature'],
$edit['signature_default_status'],
));
}
unset($edit['signature']);
break;
case 'load':
$signature = db_fetch_object(db_query("SELECT signature, status FROM {users_signature} WHERE uid = %d", $account->uid));
$account->signature_forum = isset($signature->signature) ? $signature->signature : '';
$account->signature_default_status = isset($signature->status) ? $signature->status : 1;
break;
case 'validate':
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
if ($settings['signature_forum_line_limit'] > 0 && substr_count($edit['signature'], "\n") > $settings['signature_forum_line_limit']) {
form_set_error('signature', t('Maximum number of !max_lines lines allowed in signature exceeded.', array(
'!max_lines' => $settings['signature_forum_line_limit'],
)));
}
if ($settings['signature_forum_char_limit'] > 0 && drupal_strlen($edit['signature']) > $settings['signature_forum_char_limit']) {
form_set_error('signature', t('Maximum number of @max_chars characters allowed in signature exceeded.', array(
'@max_chars' => $settings['signature_forum_char_limit'],
)));
}
break;
}
}
function signature_forum_form_alter(&$form, &$form_state, $form_id) {
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
if ($form_id == 'user_profile_form' && variable_get('user_signatures', FALSE)) {
$enabled = FALSE;
foreach (node_get_types('names') as $type => $name) {
if (!empty($settings['signature_forum_show_for_' . $type]) && $settings['signature_forum_show_for_' . $type] == 1) {
$enabled = TRUE;
break;
}
}
if (!$enabled) {
unset($form['signature_settings']);
}
elseif ($form['_category']['#value'] == 'account') {
unset($form['signature_settings']['signature_format']);
$extra = theme('filter_tips_more_info');
$value = filter_resolve_format($settings['signature_forum_format']);
$tips = _filter_tips($value, FALSE);
$form['signature_settings']['format'][$settings['signature_forum_format']] = array(
'#type' => 'value',
'#value' => $settings['signature_forum_format'],
'#parents' => array(
'format',
),
);
$form['signature_settings']['format']['format']['guidelines'] = array(
'#title' => t('Formatting guidelines'),
'#value' => theme('filter_tips', $tips, FALSE, $extra),
);
$form_format['signature_settings']['format'][] = array(
'#value' => $extra,
);
$account = $form['_account']['#value'];
$form['signature_settings']['signature']['#default_value'] = $account->signature_forum;
if ($settings['signature_forum_allow_user_default']) {
$form['signature_settings']['signature_default_status'] = array(
'#type' => 'checkbox',
'#title' => t('Show signature by default'),
'#description' => t('Whenever you add a comment you can choose not to use your signature. Use this to turn off your signature by default.'),
'#default_value' => $account->signature_default_status,
);
}
}
}
elseif (variable_get('user_signatures', FALSE) && (($form_id == 'forum_reply_node_form' || isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) && $settings['signature_forum_show_for_' . $form['type']['#value']] == 1) || $form_id == 'comment_form' && $settings['signature_forum_default_comments'] == 1) {
global $user;
if (isset($form['cid']) && $form['cid']['#value'] > 0) {
$use_signature = signature_forum_get_status($form['cid']['#value'], 'comment');
}
elseif ($form['nid']['#value'] > 0 && isset($form['type']['#value'])) {
$use_signature = signature_forum_get_status($form['nid']['#value'], 'node');
}
elseif (isset($user->signature_default_status)) {
$use_signature = $user->signature_default_status;
}
else {
$use_signature = $settings['signature_forum_default_for_' . $form['type']['#value']];
}
$form['signature_forum'] = array(
'#type' => 'checkbox',
'#title' => t('Attach signature'),
'#description' => t('Signature can be changed in <a href="@signature_link">account settings</a>.', array(
'@signature_link' => url('user/' . $user->uid . '/edit'),
)),
'#default_value' => $use_signature,
);
}
}
function signature_forum_nodeapi(&$node, $op, $teaser, $page) {
if (!$teaser && $op == 'view') {
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
if ($settings['signature_forum_auto_insert']) {
$node->content['body']['#value'] .= signature_forum_get_signature($node);
}
}
elseif ($op == 'insert' || $op == 'update') {
$delta = $node->nid;
$status = $node->signature_forum;
signature_forum_save_comment_status($delta, 'node', $status);
}
}
function signature_forum_comment(&$comment, $op) {
if ($op == 'view') {
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
if ($settings['signature_forum_auto_insert']) {
$comment->comment .= signature_forum_get_signature($comment);
}
}
elseif ($op == 'insert' || $op == 'update') {
$delta = $comment['cid'];
$status = $comment['signature_forum'];
signature_forum_save_comment_status($delta, 'comment', $status);
}
}
function signature_forum_user_exception($uid = 0, $op) {
static $cache = array();
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
if (!isset($cache[$uid]) || !is_array($cache[$uid])) {
$result = db_query("SELECT r.rid AS rid FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d", $uid);
$cache[$uid] = array();
while ($role = db_fetch_object($result)) {
$cache[$uid][$role->rid] = $role->rid;
}
if ($uid > 0) {
$cache[$uid][DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID;
}
}
foreach ($cache[$uid] as $rid) {
switch ($op) {
case 'min_length':
if ($settings['signature_forum_roles'][$rid] == $rid) {
return TRUE;
}
break;
case 'show_once':
if ($settings['signature_forum_show_once_roles'][$rid] == $rid) {
return TRUE;
}
break;
}
}
return FALSE;
}
function signature_forum_get_signature($a1) {
static $node_type;
static $cache = array();
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
if (!variable_get('user_signatures', FALSE)) {
return;
}
if (!isset($node_type)) {
if (!isset($a1->type)) {
$a1->type = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $a1->nid));
}
$node_type = $a1->type;
}
if (!$settings['signature_forum_show_for_' . $node_type]) {
return;
}
if (isset($a1->cid) || !isset($a1->revision_timestamp)) {
$content_length = drupal_strlen(strip_tags($a1->comment));
}
else {
$content_length = drupal_strlen(strip_tags($a1->content['body']['#value']));
}
if ($content_length >= $settings['signature_forum_min_content_length'] || signature_forum_user_exception($a1->uid, 'min_length')) {
$load_signature = TRUE;
}
elseif ($settings['signature_forum_min_content_length_action'] == MIN_CONTENT_ACTION_DO_NOT_DISPLAY) {
return theme('signature_forum', '');
}
if (isset($a1->cid)) {
$id = $a1->cid;
$type = 'comment';
}
else {
$id = $a1->nid;
$type = 'node';
}
$signature_status = signature_forum_get_status($id, $type);
if (!$signature_status) {
return theme('signature_forum', '');
}
if (isset($cache[$a1->uid])) {
if ($settings['signature_forum_show_once_options'] == SHOW_ONCE_OPTIONS_ALWAYS || signature_forum_user_exception($a1->uid, 'show_once')) {
$signature = (string) $cache[$a1->uid];
$load_signature = FALSE;
}
else {
$signature = '';
$load_signature = FALSE;
}
}
else {
if (isset($_GET['page']) || is_numeric(arg(2))) {
if ($settings['signature_forum_show_once_options'] == SHOW_ONCE_OPTIONS_ALWAYS || !isset($a1->cid) || signature_forum_user_exception($a1->uid, 'show_once')) {
$load_signature = TRUE;
}
else {
if (!isset($a1->type)) {
$fake_node = new stdClass();
$fake_node->type = db_result(db_query("SELECT type from {node} WHERE nid = %d", $a1->nid));
}
else {
$fake_node = (object) $a1;
}
static $mode;
static $order;
if (is_null($mode) || is_null($order)) {
$mode = _comment_get_display_setting('mode', $fake_node);
$order = _comment_get_display_setting('sort', $fake_node);
}
unset($fake_node);
if (!isset($a1->thread)) {
$a1->thread = db_result(db_query("SELECT thread FROM {comments} WHERE cid = %d", $a1->cid));
}
$sql = "SELECT cid FROM {comments} WHERE nid = %d AND uid = %d AND status = %d";
$query_args = array(
$a1->nid,
$a1->uid,
COMMENT_PUBLISHED,
);
if ($settings['signature_forum_min_content_length'] > 0 && !signature_forum_user_exception($a1->uid, 'min_length')) {
$sql .= " AND LENGTH(comment) > %d";
$query_args[] = $settings['signature_forum_min_content_length'];
}
if ($order == COMMENT_ORDER_NEWEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$sql .= " AND cid > %d\n ORDER BY cid DESC";
$query_args[] = $a1->cid;
}
else {
$sql .= " AND thread > '%s'\n ORDER BY thread DESC";
$query_args[] = $a1->thread;
}
}
elseif ($order == COMMENT_ORDER_OLDEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$sql .= " AND cid < %d\n ORDER BY cid";
$query_args[] = $a1->cid;
}
else {
$sql .= " AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < SUBSTRING('%s', 1, (LENGTH('%s') - 1))\n ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1))";
$query_args[] = $a1->thread;
$query_args[] = $a1->thread;
}
}
if (!db_result(db_query_range($sql, $query_args, 0, 1))) {
$load_signature = TRUE;
}
else {
$signature = '';
$cache[$a1->uid] = '';
$load_signature = FALSE;
}
}
}
else {
$load_signature = TRUE;
}
}
if ($load_signature == TRUE) {
$signature = db_result(db_query("SELECT signature FROM {users_signature} WHERE uid = %d", $a1->uid));
$cache[$a1->uid] = (string) $signature;
}
if ($signature == '') {
return theme('signature_forum', $signature);
}
$signature = check_markup($signature, $settings['signature_forum_format'], FALSE);
if ($content_length < $settings['signature_forum_min_content_length'] && !signature_forum_user_exception($a1->uid, 'min_length')) {
$signature = check_markup($signature, $settings['signature_forum_min_content_length_filter'], FALSE);
}
$signature = sprintf($settings['signature_forum_template'], trim($signature));
return theme('signature_forum', $signature);
}
function signature_forum_theme() {
return array(
'signature_forum' => array(
'arguments' => array(
'signature' => NULL,
),
'template' => 'signature',
),
);
}
function signature_forum_save_comment_status($delta, $type, $status = 0) {
db_query("UPDATE {signature_post} SET status = %d WHERE delta = %d AND type = '%s'", array(
$status,
$delta,
$type,
));
if (!db_affected_rows()) {
db_query("INSERT INTO {signature_post} (delta, type, status) VALUES (%d, '%s', %d)", array(
$delta,
$type,
$status,
));
}
}
function signature_forum_get_status($delta, $type) {
return db_result(db_query("SELECT status from {signature_post} WHERE delta = %d AND type = '%s'", $delta, $type));
}