Index: guestbook.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/guestbook/Attic/guestbook.module,v
retrieving revision 1.1.10.8
diff -u -p -r1.1.10.8 guestbook.module
--- guestbook.module	24 Sep 2007 11:12:08 -0000	1.1.10.8
+++ guestbook.module	10 Jan 2008 12:46:46 -0000
@@ -1,866 +1,876 @@
-<?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', 0x0001);
-define('GUESTBOOK_USER_GUESTBOOKS', 0x0002);
-/**
- * Flags for use in the "guestbook_pager_position" variable.
- */
-define('GUESTBOOK_PAGER_ABOVE', 0x0001);
-define('GUESTBOOK_PAGER_BELOW', 0x0002);
-
-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'));
-
-    if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
-      $items[] = array(
-        'path' => 'guestbook', 
-        'title' => t('Guestbooks'),
-        'access' => user_access('access site guestbook') || user_access('access user guestbooks'),
-        'callback' => 'guestbook_list');
-    }
-    else {
-      $items[] = array(
-        'path' => 'guestbook', 
-        'title' => variable_get('guestbook_site_title', t('Site guestbook')),
-        'access' => user_access('access site guestbook'),
-        'callback' => 'guestbook_page');
-    }
-  }
-  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,
-          'access' => $uid == 0 ? user_access('access site guestbook') : user_access('access user guestbooks'),
-          'type' => MENU_CALLBACK,
-          'callback' => 'guestbook_page',
-          'callback arguments' => array($uid));
-      }
-      $items[] = array(
-        'path' => 'guestbook/'. $uid .'/form', 
-        'title' => t('Add guestbook entry'),
-        'access' => $uid == 0 ? user_access('post in site guestbook') : user_access('post in user guestbooks'),
-        'type' => MENU_CALLBACK,
-        'callback' => 'guestbook_page_form',
-        'callback arguments' => array($uid));
-    }
-  }
-
-  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') && $user->guestbook_status == 0) {
-          $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' => $edit['guestbook_status'], 
-            '#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'] : 1,
-          );
-          $form['guestbook']['guestbook_intro'] = array(
-            '#type' => 'textarea',
-            '#title' => t('Intro text'),
-            '#default_value' => $edit['guestbook_intro'], 
-            '#cols' => 70,
-            '#rows' => GUESTBOOK_TEXTAREA_ROWS,
-            '#description' => t('The text that appears on top of your guestbook.'));
-          return $form;
-       }
-    }
-  }
-}
-
-/**
- *  Implementation of hook_perm()
- */
-function guestbook_perm() {
-  return array(
-    'access site guestbook', 
-    'access user guestbooks',
-    'post in site guestbook', 
-    'post in user guestbooks', 
-    'administer all guestbooks');
-}
-
-/**
- *  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() {
-  // Mode
-  $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);
-}
-
-/**
- *  Shows a user or site guestbook
- *  (menu callback)
- */
-function guestbook_page($uid = 0, $op = NULL, $op_id = NULL) {
-  global $user;
-
-  if (!_guestbook_exists($uid)) {
-    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
-  if (_guestbook_access('administer', $uid) && is_numeric($op_id)) {
-    switch ($op) {
-      case 'delete':
-        return guestbook_delete_entry_confirm_page($uid, $op_id);
-      case 'comment':
-        $comment_entry = $op_id;
-        break;
-    }
-  }  
-
-  // Fetch guestbook entries
-  $limit = variable_get('guestbook_entries_per_page', 20);
-  $result = pager_query(
-    "SELECT g.*, u1.name, u1.data, u1.picture, u2.name as commentby 
-    FROM {guestbook} g 
-    LEFT JOIN {users} u1 ON g.author = u1.uid 
-    LEFT JOIN {users} u2 ON g.commentauthor = u2.uid 
-    WHERE g.recipient = %d
-    ORDER BY g.created DESC",
-    $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');
-}
-
-/**
- *  List 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 
-    FROM {users} u 
-    LEFT OUTER JOIN {guestbook} g ON u.uid = g.recipient 
-    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 ($data['guestbook_status'] == 0) {
-        $guestbooks[$guestbook['uid']] = $guestbook;
-      }
-    }
-  }   
-  return theme('guestbook_list', $guestbooks, $header, $limit);
-}
-
-/**
- * Guestbook form functions
- */ 
-
-function guestbook_form_entry($uid, $display = '') {
-  $output =  drupal_get_form('guestbook_form_entry_form', $uid, $display);
-  return $output;
-}
-
-function guestbook_form_entry_form($uid, $display = '') {
-  global $user;
-
-  $form = array();
-  if ($user->uid == 0 ) {
-    // fields for anonymous poster
-    $form['anonname'] = array(
-      '#type' => 'textfield', '#title' => t('Name'), 
-      '#size' => 32, '#maxlength' => 64, '#required' => TRUE);
-    $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);
-    }
-    if (in_array('website', $anonymous_fields)) {
-      $form['anonwebsite'] = array(
-        '#type' => 'textfield', '#title' => t('Homepage'), 
-        '#size' => 32, '#maxlength' => 128);
-    }
-  }
-  $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);
-  $form['send'] = array(
-    '#type' => 'submit', '#value' => t('Send'));
-  $form['uid'] = array(
-    '#type' => 'value', '#value' => $uid);
-  $form['access'] = array(
-    '#type' => 'value', '#value' => _guestbook_access('post', $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 && $guestbook_user->guestbook_status == 0 && $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 (_guestbook_access('post', $uid) == 'allowed') {
-    if ($user->uid == 0) {
-      // anonymous user
-      $entryid = db_next_id('{guestbook}_id');
-      $result = db_query("INSERT INTO {guestbook} (id, anonname, anonemail, anonwebsite, author, recipient, message, created)
-        VALUES(%d, '%s', '%s', '%s', %d, %d, '%s', %d)", $entryid, $edit['anonname'], $edit['anonemail'], $edit['anonwebsite'], 0, $uid, $message, time()); 
-    }
-    else {
-      // registered user
-      $entryid = db_next_id('{guestbook}_id');
-      $result = db_query("INSERT INTO {guestbook} (id, author, recipient, message, created)
-        VALUES(%d, %d, %d, '%s', %d)", $entryid, $user->uid, $uid, $message, time()); 
-    }
-  }
-  return 'guestbook/'. $uid;
-}
-
-function theme_guestbook_form_entry_form($form) {
-  $output = '';
-  $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':
-      $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) {
-  $output = drupal_get_form('guestbook_form_comment_form', $uid, $entry); 
-  return $output; 
-}  
-
-function guestbook_form_comment_form($uid, $entry) {
-  $form = array();
-  $form['comment'] = array(
-    '#type' => 'textfield', 
-    '#default_value' => check_plain($entry['comment']),
-    '#size' => 64);
-  $form['submit'] = array(
-    '#type' => 'submit', 
-    '#value' => t('Comment'));
-  $form['entry_id'] = array('#type' => 'value', '#value' => $entry['id']);
-  $form['uid'] = array('#type' => 'value', '#value' => $uid);
-  return $form; 
-}
-
-function guestbook_form_comment_form_submit($form_id, $edit) {
-  global $user;
-  if (_guestbook_access('administer', $edit['uid'])) {
-    db_query("UPDATE {guestbook} SET comment = '%s', commentauthor = %d WHERE id = %d", $edit['comment'], $user->uid, $edit['entry_id']);
-  }
-  return array('guestbook/'. $edit['uid'], $_GET['page'] ? 'page='. $_GET['page'] : NULL);
-}
-
-function theme_guestbook_form_comment_form($form) {
-  $output = '';
-  $output .= '<div class="container-inline">';
-  $output .= drupal_render($form);
-  $output .= '</div>';
-  return $output;
-}
-
-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 
-    FROM {guestbook} g 
-    LEFT JOIN {users} u1 ON g.author = u1.uid 
-    LEFT JOIN {users} u2 ON g.commentauthor = u2.uid 
-    WHERE g.id = %d", $entry_id));
-      
-  $form = array();
-  $form['entry_id'] = array('#type' => 'value', '#value' => $entry_id);
-  $form['uid'] = array('#type' => 'value', '#value' => $uid);
-  return confirm_form(
-    $form,
-    t('Are you sure you want to delete this guestbook entry?\n'),
-    'guestbook/'. $uid,
-    theme('guestbook_entry', $uid, $entry, NULL, true),
-    t('Delete'), t('Cancel'));
-}
-
-function guestbook_delete_entry_confirm_submit($form_id, $form_values) {
-  if (_guestbook_access('administer', $form_values['uid']) && $form_values['confirm']) {
-    db_query("DELETE FROM {guestbook} WHERE id = %d", $form_values['entry_id']);
-  }
-  return 'guestbook/'. $form_values['uid'];
-}
-
-/**
- *  Guestbook theme functions
- */
-
-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) : '';
-  $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) : '';
-
-  foreach ($entries as $entry) {
-    $output .= theme('guestbook_entry', $uid, $entry, $comment_entry);
-  }
-  
-  // form and pager below entries
-  $output .= $pager_position & GUESTBOOK_PAGER_BELOW ? theme('pager', NULL, $limit, 0) : '';
-  $output .= $form_location == 'below' ? guestbook_form_entry($uid) : '';
-
-  return '<div class="guestbook">'. $output ."</div>\n";
-}
-
-function theme_guestbook_entry($uid, $entry, $comment_entry = NULL, $confirm_delete = false) {
-  global $user;
-  $output = '';
-  $display = (array) variable_get('guestbook_display', array('date', 'email', 'website', 'comments'));
-  
-  $output .= "\n<div class=\"comment guestbook-entry\">\n";
-  if ($comment_entry == $entry['id']) {
-    $output .= '<a name="comment-entry"></a>';
-  }
-
-  // author
-  if ($entry['author'] == 0) {
-     $author = "<b>" . check_plain($entry['anonname']) . "</b>";
-  } else {
-     $author = "<b>" . theme('guestbook_user_picture', $entry['author']) ."</b>";
-  }
-
-  $output .= '<div class="author">' . $author .'</div>';
-  
-  // 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'])) {
-    $output .= '&nbsp;|&nbsp;<a href="http://'. check_url($entry['anonwebsite']) .'">' . t('Website') . '</a>&nbsp;';
-  }
-  $output .= '</div>';
-
-  // message
-  $output .= '<div class="content guestbook-message">'
-           . check_markup($entry['message'], variable_get('guestbook_input_format', 1, FALSE)) .'</div>';
-
-  if ($entry['picture']) {
-    $output .= '<div style="clear:both;"></div>';
-  }
-
-  // comment
-  $output .= theme('guestbook_entry_comment', $uid, $entry, $comment_entry);
-
-  // links
-  if (_guestbook_access('administer', $uid) && !$confirm_delete) {
-    if ($comment_entry != $entry['id']) {
-      $pager = $_GET['page'] ? 'page='. $_GET['page'] : NULL;
-      $output .= '<div class="links">&raquo; ';
-      $output .= l(t('Delete entry'), "guestbook/$uid/delete/{$entry['id']}", array(), $pager) .'&nbsp;|&nbsp;';
-      $output .= l($entry['comment'] == '' ? t('Add comment') : t('Edit comment'), "guestbook/$uid/comment/{$entry['id']}", array(), $pager, 'comment-entry');
-      $output .= '</div>';  
-    }
-  }
-
-  $output .= "\n</div>";
-  return $output;
-}
-
-//
-// Copy of theme_user_picture, with adjustments
-//
-function theme_guestbook_user_picture($uid) {
-  $account = user_load(array('uid' => $uid));
-  $output = $account->name ;
-
-  if (variable_get('user_pictures', 0)) {
-    if ($account->picture && file_exists($account->picture)) {
-      $picture = file_create_url($account->picture);
-    }
-    else if (variable_get('user_picture_default', '')) {
-      $picture = variable_get('user_picture_default', '');
-    }
-
-    if (variable_get('guestbook_mode', -1) == GUESTBOOK_SITE_GUESTBOOK 
-        && user_access('access user profiles')) {
-       $user_link =  "user/$account->uid";
-       $user_text = t('View user profile.');
-    }
-    else if (variable_get('guestbook_user_link_to', 'profile') == 'profile' && user_access('access user profiles')) {
-      $user_link = 'user/'. $account->uid;
-      $user_text = t('View user profile.');
-    }
-    else if (variable_get('guestbook_user_link_to', 'profile') == 'guestbook' && user_access('access user guestbooks')) {
-       $user_link = "guestbook/$account->uid";
-       $user_text = t('View user guestbook.');
-    }
-    else {
-       $user_link = 'guestbook';
-       $user_text = t('View guestbooks.');
-    }
- 
-    $output = l($account->name ? $account->name : variable_get('anonymous', 'Anonymous'), $user_link, array("title" => $user_text)); 
-
-    if (isset($picture)) {
-      $alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', 'Anonymous')));
-      $picture = theme('image', $picture, $alt, $alt, '', false);
-      if (!empty($account->uid) && user_access('access user profiles')) {
-        $picture = l($picture, $user_link, array('title' => $user_text), NULL, NULL, FALSE, TRUE);
-      }
-
-      $output .= "<div class=\"picture\">$picture</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 comment edit form
-    $output .= guestbook_form_comment($uid, $entry);
-  }
-  else if (in_array('comments', $display) && $entry['comment'] != '') {
-    // display comment
-    $commentby = user_access('access user profiles') ? l($entry['commentby'], "user/{$entry['commentauthor']}") : $entry['commentby'];
-    $output .= '<small>'. t('Comment by') .' '. $commentby ."</small><br />";
-    $output .= '<em>'. check_plain($entry['comment']) .'</em>';
-  }
-  return '<div class="guestbook-comment content">'. $output .'</div>'; 
-}
-
-function theme_guestbook_list($guestbooks, $header, $limit = 40) {
-  $output = '';
-  // site guestbook
-  if (isset($guestbooks[0])) {
-    $output .= '<p>'. l(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, 'status' => 1)) : $user;
-      if ($guestbook_user->uid && $guestbook_user->guestbook_status == 0) {
-        $info[$uid]['title'] = t("@username's guestbook", array('@username' => $guestbook_user->name));
-        $info[$uid]['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 && $guestbook_user->guestbook_status == 0) {
-        $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':
-      if (($uid == 0 ? user_access('post in site guestbook') : user_access('post in user guestbooks'))) {
-        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 'administer':
-      return user_access('administer 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, $user->guestbook_visited));
-  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;
-}
+<?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', 0x0001);
+define('GUESTBOOK_USER_GUESTBOOKS', 0x0002);
+/**
+ * Flags for use in the "guestbook_pager_position" variable.
+ */
+define('GUESTBOOK_PAGER_ABOVE', 0x0001);
+define('GUESTBOOK_PAGER_BELOW', 0x0002);
+
+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'));
+
+    if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
+      $items[] = array(
+        'path' => 'guestbook', 
+        'title' => t('Guestbooks'),
+        'access' => user_access('access site guestbook') || user_access('access user guestbooks'),
+        'callback' => 'guestbook_list');
+    }
+    else {
+      $items[] = array(
+        'path' => 'guestbook', 
+        'title' => variable_get('guestbook_site_title', t('Site guestbook')),
+        'access' => user_access('access site guestbook'),
+        'callback' => 'guestbook_page');
+    }
+  }
+  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,
+          'access' => $uid == 0 ? user_access('access site guestbook') : user_access('access user guestbooks'),
+          'type' => MENU_CALLBACK,
+          'callback' => 'guestbook_page',
+          'callback arguments' => array($uid));
+      }
+      $items[] = array(
+        'path' => 'guestbook/'. $uid .'/form', 
+        'title' => t('Add guestbook entry'),
+        'access' => $uid == 0 ? user_access('post in site guestbook') : user_access('post in user guestbooks'),
+        'type' => MENU_CALLBACK,
+        'callback' => 'guestbook_page_form',
+        'callback arguments' => array($uid));
+    }
+  }
+
+  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') && $user->guestbook_status == 0) {
+          $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' => $edit['guestbook_status'], 
+            '#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'] : 1,
+          );
+          $form['guestbook']['guestbook_intro'] = array(
+            '#type' => 'textarea',
+            '#title' => t('Intro text'),
+            '#default_value' => $edit['guestbook_intro'], 
+            '#cols' => 70,
+            '#rows' => GUESTBOOK_TEXTAREA_ROWS,
+            '#description' => t('The text that appears on top of your guestbook.'));
+          return $form;
+       }
+    }
+  }
+}
+
+/**
+ *  Implementation of hook_perm()
+ */
+function guestbook_perm() {
+  return array(
+    'access site guestbook', 
+    'access user guestbooks',
+    'post in site guestbook', 
+    'post in user guestbooks', 
+    'administer all guestbooks');
+}
+
+/**
+ *  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() {
+  // Mode
+  $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);
+}
+
+/**
+ *  Shows a user or site guestbook
+ *  (menu callback)
+ */
+function guestbook_page($uid = 0, $op = NULL, $op_id = NULL) {
+  global $user;
+
+  if (!_guestbook_exists($uid)) {
+    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
+  if (_guestbook_access('administer', $uid) && is_numeric($op_id)) {
+    switch ($op) {
+      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.name, u1.data, u1.picture, u2.name as commentby 
+    FROM {guestbook} g 
+    LEFT JOIN {users} u1 ON g.author = u1.uid 
+    LEFT JOIN {users} u2 ON g.commentauthor = u2.uid 
+    WHERE g.recipient = %d". (isset($sql_where) ? $sql_where : '') ."
+    ORDER BY g.created DESC";
+  if (isset($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');
+}
+
+/**
+ *  List 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 
+    FROM {users} u 
+    LEFT OUTER JOIN {guestbook} g ON u.uid = g.recipient 
+    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 ($data['guestbook_status'] == 0) {
+        $guestbooks[$guestbook['uid']] = $guestbook;
+      }
+    }
+  }   
+  return theme('guestbook_list', $guestbooks, $header, $limit);
+}
+
+/**
+ * Guestbook form functions
+ */ 
+
+function guestbook_form_entry($uid, $display = '') {
+  global $user;
+  
+  // If not displaying own guestbook, display guestbook entry form.
+  if ($uid != $user->uid) {
+    return drupal_get_form('guestbook_form_entry_form', $uid, $display);
+  }
+}
+
+function guestbook_form_entry_form($uid, $display = '') {
+  global $user;
+
+  $form = array();
+  if ($user->uid == 0 ) {
+    // fields for anonymous poster
+    $form['anonname'] = array(
+      '#type' => 'textfield', '#title' => t('Name'), 
+      '#size' => 32, '#maxlength' => 64, '#required' => TRUE);
+    $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);
+    }
+    if (in_array('website', $anonymous_fields)) {
+      $form['anonwebsite'] = array(
+        '#type' => 'textfield', '#title' => t('Homepage'), 
+        '#size' => 32, '#maxlength' => 128);
+    }
+  }
+  $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);
+  $form['send'] = array(
+    '#type' => 'submit', '#value' => t('Send'));
+  $form['uid'] = array(
+    '#type' => 'value', '#value' => $uid);
+  $form['access'] = array(
+    '#type' => 'value', '#value' => _guestbook_access('post', $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 && $guestbook_user->guestbook_status == 0 && $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 (_guestbook_access('post', $uid) == 'allowed') {
+    if ($user->uid == 0) {
+      // anonymous user
+      $entryid = db_next_id('{guestbook}_id');
+      $result = db_query("INSERT INTO {guestbook} (id, anonname, anonemail, anonwebsite, author, recipient, message, created)
+        VALUES(%d, '%s', '%s', '%s', %d, %d, '%s', %d)", $entryid, $edit['anonname'], $edit['anonemail'], $edit['anonwebsite'], 0, $uid, $message, time()); 
+    }
+    else {
+      // registered user
+      $entryid = db_next_id('{guestbook}_id');
+      $result = db_query("INSERT INTO {guestbook} (id, author, recipient, message, created)
+        VALUES(%d, %d, %d, '%s', %d)", $entryid, $user->uid, $uid, $message, time()); 
+    }
+  }
+  return 'guestbook/'. $uid;
+}
+
+function theme_guestbook_form_entry_form($form) {
+  $output = '';
+  $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':
+      $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) {
+  $output = drupal_get_form('guestbook_form_comment_form', $uid, $entry); 
+  return $output; 
+}  
+
+function guestbook_form_comment_form($uid, $entry) {
+  $form = array();
+  $form['comment'] = array(
+    '#type' => 'textfield', 
+    '#default_value' => check_plain($entry['comment']),
+    '#size' => 64);
+  $form['submit'] = array(
+    '#type' => 'submit', 
+    '#value' => t('Comment'));
+  $form['entry_id'] = array('#type' => 'value', '#value' => $entry['id']);
+  $form['uid'] = array('#type' => 'value', '#value' => $uid);
+  return $form; 
+}
+
+function guestbook_form_comment_form_submit($form_id, $edit) {
+  global $user;
+  if (_guestbook_access('administer', $edit['uid'])) {
+    db_query("UPDATE {guestbook} SET comment = '%s', commentauthor = %d WHERE id = %d", $edit['comment'], $user->uid, $edit['entry_id']);
+  }
+  return array('guestbook/'. $edit['uid'], $_GET['page'] ? 'page='. $_GET['page'] : NULL);
+}
+
+function theme_guestbook_form_comment_form($form) {
+  $output = '';
+  $output .= '<div class="container-inline">';
+  $output .= drupal_render($form);
+  $output .= '</div>';
+  return $output;
+}
+
+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 
+    FROM {guestbook} g 
+    LEFT JOIN {users} u1 ON g.author = u1.uid 
+    LEFT JOIN {users} u2 ON g.commentauthor = u2.uid 
+    WHERE g.id = %d", $entry_id));
+      
+  $form = array();
+  $form['entry_id'] = array('#type' => 'value', '#value' => $entry_id);
+  $form['uid'] = array('#type' => 'value', '#value' => $uid);
+  return confirm_form(
+    $form,
+    t('Are you sure you want to delete this guestbook entry?\n'),
+    'guestbook/'. $uid,
+    theme('guestbook_entry', $uid, $entry, NULL, true),
+    t('Delete'), t('Cancel'));
+}
+
+function guestbook_delete_entry_confirm_submit($form_id, $form_values) {
+  if (_guestbook_access('administer', $form_values['uid']) && $form_values['confirm']) {
+    db_query("DELETE FROM {guestbook} WHERE id = %d", $form_values['entry_id']);
+  }
+  return 'guestbook/'. $form_values['uid'];
+}
+
+/**
+ *  Guestbook theme functions
+ */
+
+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) : '';
+  $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) : '';
+
+  foreach ($entries as $entry) {
+    $output .= theme('guestbook_entry', $uid, $entry, $comment_entry);
+  }
+  
+  // form and pager below entries
+  $output .= $pager_position & GUESTBOOK_PAGER_BELOW ? theme('pager', NULL, $limit, 0) : '';
+  $output .= $form_location == 'below' ? guestbook_form_entry($uid) : '';
+
+  return '<div class="guestbook">'. $output ."</div>\n";
+}
+
+function theme_guestbook_entry($uid, $entry, $comment_entry = NULL, $confirm_delete = false) {
+  global $user;
+  $output = '';
+  $display = (array) variable_get('guestbook_display', array('date', 'email', 'website', 'comments'));
+  
+  $output .= "\n<div class=\"comment guestbook-entry\">\n";
+  if ($comment_entry == $entry['id']) {
+    $output .= '<a name="comment-entry"></a>';
+  }
+
+  // author
+  if ($entry['author'] == 0) {
+     $author = "<b>" . check_plain($entry['anonname']) . "</b>";
+  } else {
+     $author = "<b>" . theme('guestbook_user_picture', $entry['author']) ."</b>";
+  }
+
+  $output .= '<div class="author">' . $author .'</div>';
+  
+  // 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'])) {
+    $output .= '&nbsp;|&nbsp;<a href="http://'. check_url($entry['anonwebsite']) .'">' . t('Website') . '</a>&nbsp;';
+  }
+  $output .= '</div>';
+
+  // message
+  $output .= '<div class="content guestbook-message">'
+           . check_markup($entry['message'], variable_get('guestbook_input_format', 1)) .'</div>';
+
+  if ($entry['picture']) {
+    $output .= '<div style="clear:both;"></div>';
+  }
+
+  // comment
+  $output .= theme('guestbook_entry_comment', $uid, $entry, $comment_entry);
+
+  // links
+  if (_guestbook_access('administer', $uid) && !$confirm_delete) {
+    if ($comment_entry != $entry['id']) {
+      $pager = $_GET['page'] ? 'page='. $_GET['page'] : NULL;
+      $output .= '<div class="links">&raquo; ';
+      $output .= l(t('Delete entry'), "guestbook/$uid/delete/{$entry['id']}", array(), $pager) .'&nbsp;|&nbsp;';
+      $output .= l($entry['comment'] == '' ? t('Add comment') : t('Edit comment'), "guestbook/$uid/comment/{$entry['id']}", array(), $pager, 'comment-entry');
+      $output .= '</div>';  
+    }
+  }
+
+  $output .= "\n</div>";
+  return $output;
+}
+
+//
+// Copy of theme_user_picture, with adjustments
+//
+function theme_guestbook_user_picture($uid) {
+  $account = user_load(array('uid' => $uid));
+  $output = $account->name ;
+
+  if (variable_get('user_pictures', 0)) {
+    if ($account->picture && file_exists($account->picture)) {
+      $picture = file_create_url($account->picture);
+    }
+    else if (variable_get('user_picture_default', '')) {
+      $picture = variable_get('user_picture_default', '');
+    }
+
+    if (variable_get('guestbook_mode', -1) == GUESTBOOK_SITE_GUESTBOOK 
+        && user_access('access user profiles')) {
+       $user_link =  "user/$account->uid";
+       $user_text = t('View user profile.');
+    }
+    else if (variable_get('guestbook_user_link_to', 'profile') == 'profile' && user_access('access user profiles')) {
+      $user_link = 'user/'. $account->uid;
+      $user_text = t('View user profile.');
+    }
+    else if (variable_get('guestbook_user_link_to', 'profile') == 'guestbook' && user_access('access user guestbooks')) {
+       $user_link = "guestbook/$account->uid";
+       $user_text = t('View user guestbook.');
+    }
+    else {
+       $user_link = 'guestbook';
+       $user_text = t('View guestbooks.');
+    }
+ 
+    $output = l($account->name ? $account->name : variable_get('anonymous', 'Anonymous'), $user_link, array("title" => $user_text)); 
+
+    if (isset($picture)) {
+      $alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', 'Anonymous')));
+      $picture = theme('image', $picture, $alt, $alt, '', false);
+      if (!empty($account->uid) && user_access('access user profiles')) {
+        $picture = l($picture, $user_link, array('title' => $user_text), NULL, NULL, FALSE, TRUE);
+      }
+
+      $output .= "<div class=\"picture\">$picture</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 comment edit form
+    $output .= guestbook_form_comment($uid, $entry);
+  }
+  else if (in_array('comments', $display) && $entry['comment'] != '') {
+    // display comment
+    $commentby = user_access('access user profiles') ? l($entry['commentby'], "user/{$entry['commentauthor']}") : $entry['commentby'];
+    $output .= '<small>'. t('Comment by') .' '. $commentby ."</small><br />";
+    $output .= '<em>'. check_plain($entry['comment']) .'</em>';
+  }
+  return '<div class="guestbook-comment content">'. $output .'</div>'; 
+}
+
+function theme_guestbook_list($guestbooks, $header, $limit = 40) {
+  $output = '';
+  // site guestbook
+  if (isset($guestbooks[0])) {
+    $output .= '<p>'. l(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, 'status' => 1)) : $user;
+      if ($guestbook_user->uid && $guestbook_user->guestbook_status == 0) {
+        $info[$uid]['title'] = t("@username's guestbook", array('@username' => $guestbook_user->name));
+        $info[$uid]['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 && $guestbook_user->guestbook_status == 0) {
+        $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':
+      if (($uid == 0 ? user_access('post in site guestbook') : user_access('post in user guestbooks'))) {
+        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 'administer':
+      return user_access('administer 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, $user->guestbook_visited));
+  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;
+}
