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($section) {
switch ($section) {
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($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/signature_forum',
'title' => t('Signatures for Forums'),
'description' => t("Manages users' signatures."),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'signature_forum_admin_settings',
),
'access' => user_access('administer Signatures for Forums'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
function signature_forum_admin_settings() {
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
$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['filter'] = filter_form($settings['signature_forum_format'], NULL, array(
'filter',
));
$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_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']['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'],
);
return system_settings_form($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_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_auto_insert' => module_exists('advanced_forum') ? FALSE : TRUE,
);
}
function signature_forum_admin_settings_submit($form_id, $form_values) {
if ($form_values['op'] == '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_values as $form_value_key => $form_value_value) {
if (substr($form_value_key, 0, strlen('signature_forum_show_for_')) == 'signature_forum_show_for_') {
$settings[$form_value_key] = $form_value_value;
}
}
if ($form_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_values['template'];
$settings['signature_forum_format'] = $form_values['filter'];
$settings['signature_forum_line_limit'] = $form_values['line_limit'];
$settings['signature_forum_min_content_length'] = $form_values['min_content_length'];
$settings['signature_forum_min_content_length_action'] = $form_values['min_content_length_action'];
$settings['signature_forum_min_content_length_filter'] = $form_values['min_content_length_filter'];
$settings['signature_forum_roles'] = $form_values['roles'];
$settings['signature_forum_show_once_options'] = $form_values['show_once_options'];
$settings['signature_forum_show_once_roles'] = $form_values['show_once_roles'];
$settings['signature_forum_auto_insert'] = $form_values['auto_insert'];
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)) != '') {
db_query("UPDATE {users_signature} SET signature='%s' WHERE uid = %d", array(
$edit['signature'],
$account->uid,
));
}
else {
db_query("INSERT INTO {users_signature} (uid, signature) VALUES (%d, '%s')", array(
$account->uid,
$edit['signature'],
));
}
unset($edit['signature']);
break;
case 'load':
$signature = db_result(db_query("SELECT signature FROM {users_signature} WHERE uid = %d", $account->uid));
$account->signature_forum = $signature;
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'],
)));
}
break;
}
}
function signature_forum_form_alter($form_id, &$form) {
$settings = variable_get('signature_forum_settings', signature_forum_defaults());
if ($form_id == 'user_edit') {
$enabled = FALSE;
foreach (node_get_types('names') as $type => $name) {
if ($settings['signature_forum_show_for_' . $type] == 1) {
$enabled = TRUE;
break;
}
}
if (!$enabled) {
unset($form['comment_settings']);
}
else {
$account = $form['_account']['#value'];
$form['comment_settings']['signature']['#default_value'] = $account->signature_forum;
}
}
}
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);
}
}
}
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);
}
}
}
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 (!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)) {
$content_length = strlen(strip_tags($a1->comment));
}
else {
$content_length = 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($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 || signature_forum_user_exception($a1->uid, 'show_once') || !isset($a1->cid)) {
$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 theme_signature_forum($signature) {
if (trim($signature) == '') {
return '';
}
return '<div class="signature">' . $signature . '</div>';
}