<?php
// $Id: phpbb_redirect.module,v 1.10 2008/04/11 16:35:21 nbz Exp $
/****************************************************
 * phpbb_redirect module: redirects links incoming from old phpBB forum to new Drupal forum.
 *
 ****************************************************/

/****************************************************
 *
 * Implementation of hook_help()
 *
 ****************************************************/
function phpbb_redirect_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/help#phpbb_redirect':
      $output = '<p>'. t('Redirects links to your old phpBB installation to the new Drupal forum.') .'</p>';
      $output .= '<p>'. t('This module requires <a href="@cleanurls">clean urls</a> to be active.', array('@cleanurls' => url('admin/settings/clean-urls'))) .'</p>';
      $output .= '<p>'. t('phpBB_Redirect assumes that the old forum was located in the /forum folder. If that is not true, redirection will not work.') .'</p>';
      $output .= '<p>'. t('This can be worked around by either adding a redirect in the .htacceass file on apache installations or by manually editing the module.') .'</p>';
      $output .= '<p>'. t('If you choose to edit the module, all you need to do is change "forum/viewtopic.php" and "forum/viewtopic.php" in the "phpbb_redirect_menu" function. Just replace the "forum" bit with the real old location. e.g. if the old forums were located in the phpbb folder, change these paths to "phpbb/viewtopic.php" and "phpbb/viewtopic.php".') .'</p>';
	  break;
  }
  return $output;
}


/****************************************************
 *
 * Implementation of hook_menu()
 *
 ****************************************************/
function phpbb_redirect_menu() {
  $items = array();

  $items['forum/viewtopic.php'] = array(
    'title' => 'Topic Redirect',
    'page callback' => 'phpbb_redirect_viewtopic',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK
  );
  $items['forum/viewforum.php'] = array(
    'title' => 'Forum Redirect',
    'page callback' => 'phpbb_redirect_viewforum',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK
  );

  return $items;
}

function phpbb_redirect_viewtopic() {
  if (isset($_GET['t']) && is_numeric($_GET['t'])) {
    $topic_id = $_GET['t'];

    $nid = db_result(db_query('SELECT nid FROM {phpbb2drupal_temp_topic} WHERE topic_id = %d', $topic_id));

    header('HTTP/1.1 301 Moved Permanently');
    drupal_goto("node/$nid");

  } 
  else if (isset($_GET['p'])) {
    $post = explode('#', $_GET['p']);
    if (is_numeric($post[0])) {
      $post_id = $post[0];
    }
    else {
      drupal_goto('/');
    }
    
    $cid = db_result(db_query('SELECT cid FROM {phpbb2drupal_temp_post} WHERE post_id = %d', $post_id));
    $nid = db_result(db_query('SELECT nid FROM {comments} WHERE cid = %d', $cid));
    
    $node = node_load($nid);
    $mode =_comment_get_display_setting('mode', $node);

    $flat = in_array($mode, array(COMMENT_MODE_FLAT_COLLAPSED, COMMENT_MODE_FLAT_EXPANDED));
    if ($flat) {
      $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
      $comment_count = db_result(db_query('SELECT COUNT(c.cid) FROM {comments} c WHERE c.nid = %d AND c.status = %d AND c.cid < %d', $nid, COMMENT_PUBLISHED, $cid));
      $page_number = floor($comment_count / $comments_per_page);
    }
    
    if (empty($page_number)) {
      $pagenum = NULL;
    }
    else {
      $pagenum = 'page='. intval($page_number);
    }

    header('HTTP/1.1 301 Moved Permanently');
    drupal_goto("node/$nid", "$pagenum", "comment-$cid");
  }
}

function phpbb_redirect_viewforum() {
  if (isset($_GET['f']) && is_numeric($_GET['f'])) {
    $forum_id = $_GET['f'];

    $tid = db_result(db_query('SELECT tid FROM {phpbb2drupal_temp_forum} WHERE forum_id = %d', $forum_id));

    header('HTTP/1.1 301 Moved Permanently');
    drupal_goto("forum/$tid");    
  }
}

