You are here

guestbook.module in Guestbook 5.2

File

guestbook.module
View source
<?php

if (module_exists("spam") == true) {
  if (file_exists("modules/spam/spam.module")) {
    include_once "modules/spam/spam.module";
  }
  else {
    if (file_exists("sites/all/modules/spam/spam.module")) {
      include_once "sites/all/modules/spam/spam.module";
    }
  }
}

/**
 * Flags for use in the "guestbook_mode" variable.
 */
define('GUESTBOOK_SITE_GUESTBOOK', 0x1);
define('GUESTBOOK_USER_GUESTBOOKS', 0x2);

/**
 * Flags for use in the "guestbook_pager_position" variable.
 */
define('GUESTBOOK_PAGER_ABOVE', 0x1);
define('GUESTBOOK_PAGER_BELOW', 0x2);
define('GUESTBOOK_TEXTAREA_ROWS', 8);

/**
 * Implementation of hook_menu().
 */
function guestbook_menu($may_cache) {
  global $user;
  $items = array();
  $guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/guestbook',
      'title' => t('Guestbook'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'guestbook_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
    );

    // User guestbooks.
    if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
      $items[] = array(
        'path' => 'guestbook',
        'title' => t('Guestbooks'),
        'callback' => 'guestbook_list',
        'access' => user_access('access site guestbook') || user_access('access user guestbooks'),
      );
    }
    else {
      $items[] = array(
        'path' => 'guestbook',
        'title' => variable_get('guestbook_site_title', t('Site guestbook')),
        'callback' => 'guestbook_page',
        'access' => user_access('access site guestbook'),
      );
    }
  }
  else {
    if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
      if ($user->uid > 0 && _guestbook_exists($user->uid)) {
        $unread = _guestbook_newentries();
        $unread = $unread ? ' (' . $unread . ')' : "";
        $items[] = array(
          'path' => 'guestbook/' . $user->uid,
          'title' => t('My guestbook') . $unread,
          'type' => MENU_DYNAMIC_ITEM,
        );
      }
    }
    if (arg(0) == 'guestbook' && is_numeric(arg(1))) {
      $uid = arg(1);
      $title = _guestbook_info($uid, 'title');
      if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
        $items[] = array(
          'path' => 'guestbook/' . $uid,
          'title' => $title,
          'callback' => 'guestbook_page',
          'callback arguments' => array(
            $uid,
          ),
          'access' => $uid == 0 ? user_access('access site guestbook') : user_access('access user guestbooks'),
          'type' => MENU_CALLBACK,
        );
      }
      $items[] = array(
        'path' => 'guestbook/' . $uid . '/form',
        'title' => t('Add guestbook entry'),
        'callback' => 'guestbook_page_form',
        'callback arguments' => array(
          $uid,
        ),
        'access' => $uid == 0 ? user_access('post in site guestbook') : user_access('post in user guestbooks'),
        'type' => MENU_CALLBACK,
      );
    }
  }
  return $items;
}

/**
 * Implementation of hook_user().
 */
function guestbook_user($op, &$edit, &$user, $category = '') {
  $guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
  if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
    switch ($op) {
      case 'view':
        if (user_access('access user guestbooks') && empty($user->guestbook_status)) {
          $title = t("Read @username's guestbook.", array(
            '@username' => $user->name,
          ));
          $link = l(t('View recent guestbook entries'), "guestbook/{$user->uid}", array(
            'title' => $title,
          ));
          $items[] = array(
            'title' => t('Guestbook'),
            'value' => $link,
            'class' => 'guestbook',
          );
          return array(
            t('Guestbook') => $items,
          );
        }
        break;
      case 'form':
        if ($category == 'account') {
          $form['guestbook'] = array(
            '#type' => 'fieldset',
            '#title' => t('User guestbook'),
          );
          $form['guestbook']['guestbook_status'] = array(
            '#type' => 'radios',
            '#title' => t('Status'),
            '#default_value' => isset($edit['guestbook_status']) ? $edit['guestbook_status'] : 0,
            '#options' => array(
              t('Enabled'),
              t('Disabled'),
            ),
          );
          $form['guestbook']['guestbook_send_email'] = array(
            '#type' => 'checkbox',
            '#title' => t('Send email notification'),
            '#description' => t("Uncheck if you don't wish to be notified of new entries to your guestbook."),
            '#default_value' => isset($edit['guestbook_send_email']) ? $edit['guestbook_send_email'] : 0,
          );
          $form['guestbook']['guestbook_intro'] = array(
            '#type' => 'textarea',
            '#title' => t('Intro text'),
            '#default_value' => isset($edit['guestbook_intro']) ? $edit['guestbook_intro'] : '',
            '#cols' => 70,
            '#rows' => GUESTBOOK_TEXTAREA_ROWS,
            '#description' => t('The text that appears on top of your guestbook.'),
          );
          return $form;
        }
        break;
      case 'delete':
        db_query("DELETE FROM {guestbook} WHERE recipient = %d", $user->uid);
        db_query("UPDATE {guestbook} SET author = 0 WHERE author = %d", $user->uid);
        db_query("UPDATE {guestbook} SET commentauthor = 0 WHERE commentauthor = %d", $user->uid);
        break;
    }
  }
}

/**
 * Implementation of hook_perm().
 */
function guestbook_perm() {
  return array(
    'access site guestbook',
    'access user guestbooks',
    'post in site guestbook',
    'post in user guestbooks',
    'moderate all guestbooks',
    'moderate own guestbook',
  );
}

/**
 * Implementation of hook_help().
 */
function guestbook_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Adds a site guestbook and individual user guestbooks.');
  }
}

/**
 * Implementation of hook_settings().
 */
function guestbook_admin_settings() {
  $form['guestbook_mode'] = array(
    '#type' => 'radios',
    '#title' => t('Mode'),
    '#default_value' => variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS),
    '#options' => array(
      GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS => t('Site and user guestbooks'),
      GUESTBOOK_SITE_GUESTBOOK => t('Site guestbook only'),
      GUESTBOOK_USER_GUESTBOOKS => t('User guestbooks only'),
    ),
  );

  // Site guestbook.
  $form['site_guestbook'] = array(
    '#type' => 'fieldset',
    '#title' => t('Site guestbook'),
  );
  $form['site_guestbook']['guestbook_site_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => variable_get('guestbook_site_title', 'Site guestbook'),
    '#size' => 30,
    '#maxlength' => 128,
    '#description' => t("The site guestbook's page title."),
  );
  $form['site_guestbook']['guestbook_site_intro'] = array(
    '#type' => 'textarea',
    '#title' => t('Intro text'),
    '#default_value' => variable_get('guestbook_site_intro', ''),
    '#cols' => 70,
    '#rows' => GUESTBOOK_TEXTAREA_ROWS,
    '#description' => t('The text that appears on top of the site guestbook.'),
  );
  $form['site_guestbook']['guestbook_send_email'] = array(
    '#type' => 'textfield',
    '#title' => t('Send an notification to the following e-mail address about new guestbook entries'),
    '#description' => t("Leave blank if you don't wish to be notified"),
    '#size' => 30,
    '#maxlength' => 128,
    '#default_value' => variable_get('guestbook_send_email', ''),
  );

  // User guestbooks.
  $form['user_guestbooks'] = array(
    '#type' => 'fieldset',
    '#title' => t('User guestbooks'),
    '#description' => t('Users can individually disable their guestbook or add an intro text on the user account page.'),
  );
  $form['user_guestbooks']['guestbook_user_link_to'] = array(
    '#type' => 'radios',
    '#title' => t('User link to profile or guestbook'),
    '#description' => t('When displaying a user should the link show the user profile or the user guestbook?'),
    '#options' => array(
      'profile' => t('User profile'),
      'guestbook' => t('User guestbook'),
    ),
    '#default_value' => variable_get('guestbook_user_link_to', 'profile'),
  );

  // Display options.
  $form['display_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display options'),
  );
  $form['display_options']['guestbook_entries_per_page'] = array(
    '#type' => 'textfield',
    '#title' => t('Entries per page'),
    '#default_value' => variable_get('guestbook_entries_per_page', 20),
    '#size' => 3,
    '#maxlength' => 3,
    '#description' => t('The number of guestbook entries per page.'),
  );
  $form['display_options']['guestbook_display'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Toggle display'),
    '#default_value' => variable_get('guestbook_display', array(
      'date',
      'email',
      'website',
      'comments',
    )),
    '#options' => array(
      'date' => t('Submission date'),
      'email' => t('Anonymous poster e-mail'),
      'website' => t('Anonymous poster website'),
      'comments' => t('Comments'),
    ),
  );
  $form['display_options']['guestbook_pager_position'] = array(
    '#type' => 'radios',
    '#title' => t('Position of pager'),
    '#default_value' => variable_get('guestbook_pager_position', GUESTBOOK_PAGER_BELOW),
    '#options' => array(
      GUESTBOOK_PAGER_ABOVE => t('Above the entries'),
      GUESTBOOK_PAGER_BELOW => t('Below the entries'),
      GUESTBOOK_PAGER_ABOVE | GUESTBOOK_PAGER_BELOW => t('Above and below the entries'),
    ),
  );

  // Posting settings.
  $form['posting_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Posting settings'),
  );
  $form['posting_settings']['guestbook_input_format'] = filter_form(variable_get('guestbook_input_format', 0), NULL, array(
    'guestbook_input_format',
  ));
  $form['posting_settings']['guestbook_input_format']['#type'] = 'item';
  $form['posting_settings']['guestbook_filter_tips'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display filter tips'),
    '#default_value' => variable_get('guestbook_filter_tips', TRUE),
    '#description' => t('If enabled filter tips are displayed below the message textarea.'),
  );
  $form['posting_settings']['guestbook_anonymous_fields'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Anonymous poster fields'),
    '#default_value' => variable_get('guestbook_anonymous_fields', array(
      'email',
      'website',
    )),
    '#description' => t('Additional information that anonymous posters may supply.'),
    '#options' => array(
      'email' => 'E-mail',
      'website' => 'Website',
    ),
  );
  $form['posting_settings']['guestbook_form_location'] = array(
    '#type' => 'radios',
    '#title' => t('Location of entry submission form'),
    '#default_value' => variable_get('guestbook_form_location', 'above'),
    '#options' => array(
      'above' => t('Above entries'),
      'below' => t('Below entries'),
      'separate page' => t('Separate page'),
    ),
  );
  $form['array_filter'] = array(
    '#type' => 'value',
    '#value' => TRUE,
  );
  return system_settings_form($form);
}

/**
 * Output a guestbook page; menu callback.
 */
function guestbook_page($uid = 0, $op = NULL, $op_id = NULL, $page = TRUE) {
  global $user;
  if (!_guestbook_exists($uid)) {
    if ($page) {
      drupal_not_found();
    }
    return;
  }

  // Set last visited time for own guestbook
  if ($uid > 0 && $user->uid == $uid) {
    user_save($user, array(
      'guestbook_visited' => time(),
    ));
  }

  // Delete or comment an entry
  $comment_entry = $sql_where = '';
  if (_guestbook_access('moderate', $uid) && is_numeric($op_id)) {
    switch ($op) {
      case 'edit':
        return drupal_get_form('guestbook_form_entry_form', $uid, 'page', $op_id);
      case 'delete':
        return guestbook_delete_entry_confirm_page($uid, $op_id);
      case 'comment':
        $comment_entry = $op_id;
        $sql_where = ' AND g.id = %d';
        break;
    }
  }

  // Fetch guestbook entries
  $limit = variable_get('guestbook_entries_per_page', 20);
  $sql = "SELECT g.*, u1.uid, u1.name, u1.data, u1.picture, u2.name as commentby \n    FROM {guestbook} g \n    LEFT JOIN {users} u1 ON g.author = u1.uid \n    LEFT JOIN {users} u2 ON g.commentauthor = u2.uid \n    WHERE g.recipient = %d {$sql_where}\n    ORDER BY g.created DESC";
  if (!empty($comment_entry)) {

    // Fetch only guestbook entry for administrative comment.
    $result = db_query($sql, $uid, $comment_entry);
  }
  else {
    $result = pager_query($sql, $limit, 0, "SELECT COUNT(*) FROM {guestbook} WHERE recipient = %d", $uid);
  }
  $entries = array();
  while ($entry = db_fetch_array($result)) {
    $entries[] = $entry;
  }
  return theme('guestbook', $uid, $entries, $comment_entry, $limit);
}

/**
 * Display the guestbook form on a separate page; menu callback.
 */
function guestbook_page_form($uid) {
  if (!_guestbook_exists($uid)) {
    drupal_not_found();
    return;
  }
  return guestbook_form_entry($uid, 'page');
}

/**
 * Output a list of all guestbooks; menu callback.
 */
function guestbook_list() {
  $limit = 40;
  $guestbooks = array();
  $guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
  $header = array(
    array(
      'data' => t('user'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('entries'),
      'field' => 'num',
    ),
    array(
      'data' => t('last update'),
      'field' => 'created',
      'sort' => 'desc',
    ),
  );
  $result = pager_query("SELECT u.uid, u.name, u.data, MAX(g.created) as created, COUNT(g.recipient) as num \n    FROM {users} u \n    LEFT OUTER JOIN {guestbook} g ON u.uid = g.recipient \n    GROUP BY u.uid, u.name, u.data, g.recipient" . tablesort_sql($header), $limit, 0, "SELECT COUNT(*) FROM {users}");
  while ($guestbook = db_fetch_array($result)) {
    if ($guestbook['uid'] == 0 && user_access('access site guestbook') && $guestbook_mode & GUESTBOOK_SITE_GUESTBOOK) {

      // Site guestbook.
      $guestbooks[0] = $guestbook;
    }
    else {
      if ($guestbook['uid'] > 0 && user_access('access user guestbooks')) {

        // User guestbooks.
        $data = unserialize($guestbook['data']);
        if (empty($data['guestbook_status'])) {
          $guestbooks[$guestbook['uid']] = $guestbook;
        }
      }
    }
  }
  return theme('guestbook_list', $guestbooks, $header, $limit);
}

/**
 * Retrieve a guestbook post form.
 */
function guestbook_form_entry($uid, $display = '') {
  return drupal_get_form('guestbook_form_entry_form', $uid, $display);
}

/**
 * Form builder function for guestbook post form.
 */
function guestbook_form_entry_form($uid, $display = '', $entry_id = NULL) {
  global $user;
  $entry = array();
  if (isset($entry_id) && _guestbook_access('moderate', $uid) && user_access('moderate own guestbook')) {
    $entry = db_fetch_array(db_query("SELECT * FROM {guestbook} WHERE id = %d", $entry_id));
  }
  $form = array();
  if (!empty($entry)) {
    $form['entry_id'] = array(
      '#type' => 'value',
      '#value' => $entry['id'],
    );
    $form['author'] = array(
      '#type' => 'value',
      '#value' => $entry['author'],
    );

    // Re-route form submission to guestbook entry edit form submit handler.
    $form['#submit'] = array(
      'guestbook_form_entry_form_edit_submit' => array(),
    );
  }
  if ($user->uid == 0 || isset($entry['author']) && $entry['author'] == 0) {

    // fields for anonymous poster
    $form['anonname'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#size' => 32,
      '#maxlength' => 64,
      '#required' => TRUE,
      '#default_value' => !empty($entry['anonname']) ? $entry['anonname'] : '',
    );
    $anonymous_fields = (array) variable_get('guestbook_anonymous_fields', array(
      'email',
      'website',
    ));
    if (in_array('email', $anonymous_fields)) {
      $form['anonemail'] = array(
        '#type' => 'textfield',
        '#title' => t('E-mail'),
        '#size' => 32,
        '#maxlength' => 128,
        '#default_value' => !empty($entry['anonemail']) ? $entry['anonemail'] : '',
      );
    }
    if (in_array('website', $anonymous_fields)) {
      $form['anonwebsite'] = array(
        '#type' => 'textfield',
        '#title' => t('Homepage'),
        '#size' => 32,
        '#maxlength' => 128,
        '#default_value' => !empty($entry['anonwebsite']) ? $entry['anonwebsite'] : '',
      );
    }
  }
  $filter_tips = variable_get('guestbook_filter_tips', TRUE) ? _guestbook_form_filter_tips() : NULL;
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#cols' => 32,
    '#rows' => GUESTBOOK_TEXTAREA_ROWS,
    '#description' => $filter_tips,
    '#required' => TRUE,
    '#default_value' => !empty($entry['message']) ? $entry['message'] : '',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send'),
  );
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $uid,
  );
  if (!empty($entry)) {

    // Need to explicitly check for moderate when editing an existing post,
    // because FAPI will output a WSOD (NULL) otherwise.
    $form['#access'] = _guestbook_access('moderate', $uid) ? TRUE : FALSE;
  }
  else {
    $form['#access'] = _guestbook_access('post', $uid) == 'allowed' ? TRUE : FALSE;
  }
  if (variable_get('guestbook_form_location', 'above') == 'separate page') {
    $form['#redirect'] = !empty($_GET['destination']) ? $_GET['destination'] : 'guestbook/' . $uid;
  }
  $form['display'] = array(
    '#type' => 'value',
    '#value' => $display,
  );
  return $form;
}
function guestbook_form_entry_form_submit($form_id, $edit) {
  global $user;
  $uid = $edit['uid'];
  $message = $edit['message'];

  // Make sure this isn't a dupe.
  $result = db_query("SELECT message FROM {guestbook} WHERE recipient = %d ORDER BY id DESC LIMIT 1", $uid);
  $entry = db_fetch_array($result);
  if ($entry['message'] == $message) {
    return;
  }

  // No empty entries.
  if ($message == '') {
    return;
  }
  if (module_exists('spam')) {

    // Is this spam?
    $spamcheck = $edit['anonname'] . ' ' . $edit['anonemail'] . ' ' . $edit['anonwebsite'];
    if (spam_content_filter('guestbook', 1, $spamcheck, $message, '_guestbook_spam')) {
      return;
    }
  }

  // E-mail notification.
  $iSendEmail = '';
  $guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
  if ($uid == 0 && $guestbook_mode & GUESTBOOK_SITE_GUESTBOOK) {
    $iSendEmail = variable_get('guestbook_send_email', '');
  }
  else {
    if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
      $guestbook_user = $uid != $user->uid ? user_load(array(
        'uid' => $uid,
        'status' => 1,
      )) : $user;
      if ($guestbook_user->uid && empty($guestbook_user->guestbook_status) && !empty($guestbook_user->guestbook_send_email)) {
        $iSendEmail = $guestbook_user->mail;
      }
    }
  }
  $from = variable_get('site_mail', ini_get('sendmail_from'));
  if ($iSendEmail) {
    drupal_mail('guestbook_notification', $iSendEmail, 'New guestbook entry', $message, $from);
  }

  // Insert new message
  if ($user->uid == 0) {

    // Anonymous user.
    $entryid = db_next_id('{guestbook}_id');
    db_query("INSERT INTO {guestbook} (id, anonname, anonemail, anonwebsite, author, recipient, message, created)\n      VALUES(%d, '%s', '%s', '%s', %d, %d, '%s', %d)", $entryid, $edit['anonname'], $edit['anonemail'], $edit['anonwebsite'], 0, $uid, $message, time());
  }
  else {

    // Authenticated user.
    $entryid = db_next_id('{guestbook}_id');
    db_query("INSERT INTO {guestbook} (id, author, recipient, message, created)\n      VALUES(%d, %d, %d, '%s', %d)", $entryid, $user->uid, $uid, $message, time());
  }

  // Notify other modules of the new guestbook entry.
  $entry = db_fetch_array(db_query("SELECT * FROM {guestbook} WHERE id = %d", $entryid));
  module_invoke_all('guestbook', 'insert', $entry);
  drupal_set_message(t('Your message has been added.'));
}

/**
 * Submit handler for editing guestbook entries.
 */
function guestbook_form_entry_form_edit_submit($form_id, $form_values) {
  if (_guestbook_access('moderate', $form_values['uid']) && $form_values['submit'] == t('Send') && user_access('moderate own guestbook')) {
    if ($form_values['author'] == 0) {

      // Post's author is an anonymous user.
      db_query("UPDATE {guestbook} SET anonname = '%s', anonemail = '%s', anonwebsite = '%s', message = '%s' WHERE id = %d", $form_values['anonname'], $form_values['anonemail'], $form_values['anonwebsite'], $form_values['message'], $form_values['entry_id']);
    }
    else {
      if ($form_values['author'] > 0) {

        // Post's author is a registered user.
        db_query("UPDATE {guestbook} SET message = '%s' WHERE id = %d", $form_values['message'], $form_values['entry_id']);
      }
    }
  }

  // Notify other modules of the new guestbook entry.
  $entry = db_fetch_array(db_query("SELECT * FROM {guestbook} WHERE id = %d", $form_values['entry_id']));
  module_invoke_all('guestbook', 'update', $entry);
  return 'guestbook/' . $form_values['uid'];
}
function theme_guestbook_form_entry_form($form) {
  $output = '';

  // @todo Since #access is set on the overall form now, this function along
  // with the switch below is no longer executed.
  $access = $form['access']['#value'];
  $display = $form['display']['#value'];
  $uid = $form['uid']['#value'];
  switch ($access) {
    case 'allowed':
      if ($display == 'link') {

        // Output only a link to a page with the form.
        $output .= '<p>&raquo; ' . l(t('Add guestbook entry'), "guestbook/{$uid}/form") . '</p>';
      }
      else {
        $output .= $display == 'page' ? '' : '<h3>' . t('Add guestbook entry') . '</h3>';
        $output .= drupal_render($form);
      }
      break;
    case 'own guestbook':
      if (isset($form['entry_id'])) {
        drupal_set_title(t('Edit guestbook entry'));
        $output .= drupal_render($form);
      }
      else {
        $output .= ' ';
      }
      break;
    case 'not logged in':
      $output .= '<p class="links">&raquo; ' . t('You must be logged in to post a comment.') . '</p>';
      break;
    case 'not allowed':
      $output .= '<p class="links">&raquo; ' . t('You are not allowed to post in this guestbook.') . '</p>';
      break;
  }
  return $output;
}
function guestbook_form_comment($uid, $entry) {
  return drupal_get_form('guestbook_form_comment_form', $uid, $entry);
}
function guestbook_form_comment_form($uid, $entry) {
  $form = array();
  $form['comment'] = array(
    '#type' => 'textarea',
    '#default_value' => $entry['comment'],
    '#description' => variable_get('guestbook_filter_tips', TRUE) ? _guestbook_form_filter_tips() : NULL,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Reply'),
  );
  $form['entry_id'] = array(
    '#type' => 'value',
    '#value' => $entry['id'],
  );
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $uid,
  );
  $form['#redirect'] = !empty($_GET['destination']) ? $_GET['destination'] : 'guestbook/' . $uid;
  return $form;
}
function guestbook_form_comment_form_submit($form_id, $edit) {
  global $user;
  if (_guestbook_access('moderate', $edit['uid'])) {
    db_query("UPDATE {guestbook} SET comment = '%s', commentauthor = %d WHERE id = %d", $edit['comment'], $user->uid, $edit['entry_id']);
  }
}
function guestbook_delete_entry_confirm_page($uid, $entry_id) {
  return drupal_get_form('guestbook_delete_entry_confirm', $uid, $entry_id);
}
function guestbook_delete_entry_confirm($uid, $entry_id) {
  $entry = db_fetch_array(db_query("SELECT g.*, u1.name, u1.data, u1.picture, u2.name as commentby \n    FROM {guestbook} g \n    LEFT JOIN {users} u1 ON g.author = u1.uid \n    LEFT JOIN {users} u2 ON g.commentauthor = u2.uid \n    WHERE g.id = %d", $entry_id));
  $form = array();
  $form['entry_id'] = array(
    '#type' => 'value',
    '#value' => $entry_id,
  );
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $uid,
  );
  $form['#redirect'] = !empty($_GET['destination']) ? $_GET['destination'] : 'guestbook/' . $uid;
  return confirm_form($form, t('Are you sure you want to delete this guestbook entry?'), !empty($_GET['destination']) ? $_GET['destination'] : referer_uri(), theme('guestbook_entry', $uid, $entry, NULL, NULL, TRUE), t('Delete'), t('Cancel'));
}
function guestbook_delete_entry_confirm_submit($form_id, $form_values) {
  if (_guestbook_access('moderate', $form_values['uid']) && $form_values['confirm']) {
    db_query("DELETE FROM {guestbook} WHERE id = %d", $form_values['entry_id']);
  }
}

/**
 * Render a guestbook.
 */
function theme_guestbook($uid, $entries, $comment_entry, $limit = 20) {
  global $user;
  $form_location = variable_get('guestbook_form_location', 'above');
  $pager_position = variable_get('guestbook_pager_position', GUESTBOOK_PAGER_BELOW);

  // Intro text.
  $intro = _guestbook_info($uid, 'intro');
  $output = $intro ? check_markup($intro) : '';
  if ($_GET['q'] != 'user/' . $uid) {
    $output .= _guestbook_user_profile_link($uid);
  }

  // Form on separate page.
  $output .= $form_location == 'separate page' ? guestbook_form_entry($uid, 'link') : '';

  // Form and pager above entries.
  $output .= $form_location == 'above' ? guestbook_form_entry($uid) : '';
  $output .= $pager_position & GUESTBOOK_PAGER_ABOVE ? theme('pager', NULL, $limit, 0) : '';
  $i = 0;
  foreach ($entries as $entry) {
    $zebra = $i % 2 ? 'odd' : 'even';
    $output .= theme('guestbook_entry', $uid, $entry, $comment_entry, $zebra);
    $i++;
  }

  // Form and pager below entries.
  $output .= $pager_position & GUESTBOOK_PAGER_BELOW ? theme('pager', NULL, $limit, 0) : '';
  $output .= $form_location == 'below' ? guestbook_form_entry($uid) : '';
  if ($output == '') {
    $output = '<div class="guestbook-empty">' . t('Nobody has signed this guestbook yet.') . '</div>';
  }
  return '<div class="guestbook">' . $output . "</div>\n";
}
function theme_guestbook_entry($uid, $entry, $comment_entry = NULL, $zebra, $confirm_delete = false) {
  global $user;
  $output = '';
  $display = (array) variable_get('guestbook_display', array(
    'date',
    'email',
    'website',
    'comments',
  ));
  $output .= "\n<div class=\"guestbook-entry clear-block {$zebra}\">\n";
  if ($comment_entry == $entry['id']) {
    $output .= '<a name="comment-entry"></a>';
  }

  // Author.
  if ($entry['author'] == 0) {
    $output .= "<b>" . check_plain($entry['anonname']) . "</b>";
  }
  else {
    $output .= theme('username', (object) $entry, 'guestbook');
  }

  // Date, email, website.
  $output .= '<div class="submitted">';
  if (in_array('date', $display)) {
    $output .= format_date($entry['created'], 'medium');
  }
  if (in_array('email', $display) && !empty($entry['anonemail'])) {
    $output .= '&nbsp;|&nbsp;<a href="mailto:' . check_url($entry['anonemail']) . '">' . t('E-mail') . '</a>';
  }
  if (in_array('website', $display) && !empty($entry['anonwebsite'])) {

    // Auto-prepend HTTP protocol if website contains no protocol.
    if (strpos($entry['anonwebsite'], '://') === FALSE) {
      $entry['anonwebsite'] = 'http://' . $entry['anonwebsite'];
    }
    $output .= '&nbsp;|&nbsp;<a href="' . check_url($entry['anonwebsite']) . '">' . t('Website') . '</a>&nbsp;';
  }
  $output .= '</div>';

  // Message.
  $output .= '<div class="guestbook-message">' . check_markup($entry['message'], variable_get('guestbook_input_format', 1), FALSE) . '</div>';

  // Guestbook owner comment.
  $output .= theme('guestbook_entry_comment', $uid, $entry, $comment_entry);

  // Links.
  if (_guestbook_access('moderate', $uid) && !$confirm_delete) {
    if ($comment_entry != $entry['id']) {
      $links = array();
      $pager = !empty($_GET['page']) ? '&page=' . $_GET['page'] : '';
      if (user_access('moderate own guestbook') || user_access('moderate all guestbooks')) {
        $links['delete'] = array(
          'title' => t('Delete entry'),
          'href' => "guestbook/{$uid}/delete/{$entry['id']}",
          'query' => 'destination=' . url($_GET['q']) . $pager,
        );
        $links['edit'] = array(
          'title' => t('Edit entry'),
          'href' => "guestbook/{$uid}/edit/{$entry['id']}",
          'query' => 'destination=' . url($_GET['q']) . $pager,
        );
      }
      $links['comment'] = array(
        'title' => $entry['comment'] == '' ? t('Add comment') : t('Edit comment'),
        'href' => "guestbook/{$uid}/comment/{$entry['id']}",
        'query' => 'destination=' . url($_GET['q']) . $pager,
        'fragment' => 'comment-entry',
      );
      $output .= theme('links', $links, array(
        'class' => 'guestbook-links',
      ));
    }
  }
  $output .= "\n</div>";
  return $output;
}
function theme_guestbook_entry_comment($uid, $entry, $comment_entry) {
  $display = (array) variable_get('guestbook_display', array(
    'date',
    'email',
    'website',
    'comments',
  ));
  $output = '';
  if ($comment_entry == $entry['id']) {

    // Display owner comment edit form.
    $output .= guestbook_form_comment($uid, $entry);
  }
  else {
    if (in_array('comments', $display) && $entry['comment'] != '') {

      // Display owner comment.
      $author = user_access('access user profiles') ? l($entry['commentby'], "user/{$entry['commentauthor']}") : $entry['commentby'];
      $output .= '<div class="guestbook-comment-submitted">';
      $output .= t('Comment by') . ' ' . $author;
      $output .= '</div>';
      $output .= '<div class="guestbook-comment-content">';
      $output .= check_markup($entry['comment'], variable_get('guestbook_input_format', 1), FALSE);
      $output .= '</div>';
    }
  }
  return !empty($output) ? '<div class="guestbook-comment">' . $output . '</div>' : '';
}
function theme_guestbook_list($guestbooks, $header, $limit = 40) {
  $output = '';

  // Site guestbook.
  if (isset($guestbooks[0])) {
    $output .= '<p>' . l(variable_get('guestbook_site_title', t('Site guestbook')), 'guestbook/0');
    $output .= ' (' . format_plural($guestbooks[0]['num'], '1 entry', '@count entries') . ', ' . t('last update') . ': ' . _guestbook_timeinterval($guestbooks[0]['created']) . ')</p>';
    unset($guestbooks[0]);
  }

  // User guestbooks.
  if (count($guestbooks)) {
    $output .= '<h4>' . t('User guestbooks') . '</h4>';
    $rows = array();
    foreach ($guestbooks as $guestbook) {
      $rows[] = array(
        l($guestbook['name'], 'guestbook/' . $guestbook['uid']),
        format_plural($guestbook['num'], '1 entry', '@count entries'),
        array(
          'data' => _guestbook_timeinterval($guestbook['created']),
          'align' => 'right',
        ),
      );
    }
    $output .= theme('table', $header, $rows);
  }
  $output .= theme('pager', NULL, $limit, 0);
  return $output;
}

/**
 * Returns the title or the intro text of the guestbook specified by $uid.
 */
function _guestbook_info($uid, $data) {
  global $user;
  static $info;
  $guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
  if (!isset($info[$uid])) {
    if ($uid == 0 && $guestbook_mode & GUESTBOOK_SITE_GUESTBOOK) {
      $info[$uid]['title'] = variable_get('guestbook_site_title', t('Site guestbook'));
      $info[$uid]['intro'] = variable_get('guestbook_site_intro', '');
    }
    else {
      if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
        $guestbook_user = $uid != $user->uid ? user_load(array(
          'uid' => $uid,
        )) : $user;
        if ($guestbook_user->uid && ($guestbook_user->status || user_access('administer users')) && empty($guestbook_user->guestbook_status)) {
          $info[$uid]['title'] = t("@username's guestbook", array(
            '@username' => $guestbook_user->name,
          ));
          $info[$uid]['intro'] = !empty($guestbook_user->guestbook_intro) ? $guestbook_user->guestbook_intro : '';
        }
      }
    }
  }
  return $info[$uid][$data];
}

/**
 * Return a link to $uid's profile if context allows it.
 */
function _guestbook_user_profile_link($uid) {
  global $user;
  $guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
  $output = '';
  if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS && user_access('access user profiles') && $uid != $user->uid) {
    $guestbook_user = user_load(array(
      'uid' => $uid,
      'status' => 1,
    ));
    if ($guestbook_user->uid && empty($guestbook_user->guestbook_status)) {
      $namelink = l($guestbook_user->name, "user/{$uid}", array(
        'title' => t('View user profile.'),
      ));
      $output .= '<div class="submitted">' . t("Visit !username's profile", array(
        '!username' => $namelink,
      )) . '</div>';
    }
  }
  return $output;
}

/**
 * Returns if the guestbook specified by $uid exists.
 */
function _guestbook_exists($uid) {
  $title = _guestbook_info($uid, 'title');
  return !empty($title);
}

/**
 * Returns if current user is allowed to perform $action in guestbook $uid.
 */
function _guestbook_access($action, $uid) {
  global $user;
  switch ($action) {
    case 'post':

      // Check whether user has sufficient permissions to post in this guestbook.
      if ($uid == 0 ? user_access('post in site guestbook') : user_access('post in user guestbooks')) {

        // Check whether an authenticated user tries to post in own guestbook,
        // allowing to post to others only.
        if (!($user->uid == $uid && $user->uid > 0)) {
          return 'allowed';
        }
        else {
          return 'own guestbook';
        }
      }
      else {
        if ($user->uid == 0) {
          return 'not logged in';
        }
        else {
          if ($user->uid != $uid) {
            return 'not allowed';
          }
        }
      }
      break;
    case 'moderate':
      return user_access('moderate all guestbooks') || $uid == $user->uid && $user->uid > 0;
  }
}

/**
 * Returns a string representation of a time interval.
 */
function _guestbook_timeinterval($time) {
  if ($time == 0) {
    return t('never');
  }
  else {
    return format_interval(time() - $time, 1);
  }
}

/**
 * Fetches number of new entries for current user.
 */
function _guestbook_newentries() {
  global $user;
  $count = db_result(db_query("SELECT COUNT(created) FROM {guestbook} WHERE recipient = %d AND created > %d", $user->uid, isset($user->guestbook_visited) ? $user->guestbook_visited : time()));
  return $count;
}
function _guestbook_form_filter_tips() {
  $format = variable_get('guestbook_input_format', 1);
  $tips = theme('filter_tips', _filter_tips($format));
  return $tips;
}
function _guestbook_spam($source, $id, $header, $body, $probability, $old, $action) {
  if ($probability > 98) {
    $msgtext = t('Entry is spam: ') . $header . ' ' . $body . ' probability: ' . $probability;
    watchdog('guestbook', $msgtext, WATCHDOG_WARNING);
    drupal_set_message($msgtext, 'error');
    return TRUE;
  }
  return FALSE;
}

/**
 * @ingroup panels_support Panels support.
 * @{
 */

/**
 * Implementation of hook_panels_include_directory().
 */
function guestbook_panels_include_directory($plugintype) {
  if ($plugintype == 'content_types') {
    return 'panels';
  }
}

/**
 * @} End of "ingroup panels_support"
 */

Functions

Namesort descending Description
guestbook_admin_settings Implementation of hook_settings().
guestbook_delete_entry_confirm
guestbook_delete_entry_confirm_page
guestbook_delete_entry_confirm_submit
guestbook_form_comment
guestbook_form_comment_form
guestbook_form_comment_form_submit
guestbook_form_entry Retrieve a guestbook post form.
guestbook_form_entry_form Form builder function for guestbook post form.
guestbook_form_entry_form_edit_submit Submit handler for editing guestbook entries.
guestbook_form_entry_form_submit
guestbook_help Implementation of hook_help().
guestbook_list Output a list of all guestbooks; menu callback.
guestbook_menu Implementation of hook_menu().
guestbook_page Output a guestbook page; menu callback.
guestbook_page_form Display the guestbook form on a separate page; menu callback.
guestbook_panels_include_directory Implementation of hook_panels_include_directory().
guestbook_perm Implementation of hook_perm().
guestbook_user Implementation of hook_user().
theme_guestbook Render a guestbook.
theme_guestbook_entry
theme_guestbook_entry_comment
theme_guestbook_form_entry_form
theme_guestbook_list
_guestbook_access Returns if current user is allowed to perform $action in guestbook $uid.
_guestbook_exists Returns if the guestbook specified by $uid exists.
_guestbook_form_filter_tips
_guestbook_info Returns the title or the intro text of the guestbook specified by $uid.
_guestbook_newentries Fetches number of new entries for current user.
_guestbook_spam
_guestbook_timeinterval Returns a string representation of a time interval.
_guestbook_user_profile_link Return a link to $uid's profile if context allows it.

Constants

Namesort descending Description
GUESTBOOK_PAGER_ABOVE Flags for use in the "guestbook_pager_position" variable.
GUESTBOOK_PAGER_BELOW
GUESTBOOK_SITE_GUESTBOOK Flags for use in the "guestbook_mode" variable.
GUESTBOOK_TEXTAREA_ROWS
GUESTBOOK_USER_GUESTBOOKS