Any PHP Whizzes?

Status
Not open for further replies.

trigatch4

BuildAndEarn
Aug 23, 2006
2,557
79
0
East Coast
www.eurekadiary.com
So I'm switching a forum from SMF to vBulletin and the ONLY issue I need to resolve is 301 redirecting all the OLD links to the corresponding page on the NEW forum.

vBulletin staff wrote a script that is designed to parse the old link structure and relate it to the new link structure. It was written to work with phpBB, IPB and some others but there is no SMF support.

So... I need to customize it which - surprise, surprise - I can't do. Anyone mind helping me on this?

Here is the URL structure for the SMF forum I was using:

Code:
Forums look like this:
http://mysite.com/index.php?board=5.0

Threads look like this:
http://mysite.com/index.php/topic,775.0.html

Posts look like this:
http://mysite.com/index.php/topic,54...2.html#msg3252

So using this information to "parse" the urls, you need to customize the following script (which works in conjunction with a database). This is the first "basic" chunk that needs customizing which I can do... but probably need to know it for the next part:

Code:
// System

#Currently supported : 'phpBB2' 'ubb.threads' 'vb3' 'ipb2'

$old_system 	= 'phpBB2';

// Domain
// Example :: http://www.example.com/phpBB/

$old_folder 	= 'phpBB/'; 								// With trailing slash
$old_ext_type	= '.php'; 									// Including preceding dot
$standard_404 	= 'http://www.example.com/not_found.html'; 	// The usual 404 that this script replaces

// Example :: www.example.com/vBulletin/

$new_domain 	= 'example';
$new_folder		= 'vBulletin/';	// With trailing slash
$ext_type		= '.php'; 		// File extension type that vBulletin is using, i.e. index.php including the preceding dot

And this is example of the code for PHPBB2 which needs to be converted/altered/customized to work with the SMF links:

Code:
#############################################################################################################################
#
# Customise below this line to match your URL's and/or remove the systems you don't need
#
#############################################################################################################################

$old_id 		= 0;
$action_code	= 0;
$action 		= null;
$sql 			= null;
$page			= null;
$postcount		= null;

// Get the file names and types

switch ($old_system)
{
	case 'phpBB2' :
		$old_forum_script 	= "viewforum{$old_ext_type}?f=";
		$old_thread_script 	= "viewtopic{$old_ext_type}?t=";
		$old_post_script 	= "viewtopic{$old_ext_type}?p=";
		$old_user_script 	= "profile{$old_ext_type}?mode=viewprofile&u="; // Append userid
		break;
	case 'ubb.threads' :
		$old_forum_script 	= "postlist{$old_ext_type}?"; 		// postlist.php?Cat=&Board=beadtechniques -- have to try to do it on title
		$old_thread_script 	= "showflat{$old_ext_type}?"; 		// Cat=&Board=beadtechniques&Number=74690&page=0&view=collapsed&sb=5&o=&fpart=1Greg Go for Number=XX
		$old_post_script 	= "showthreaded{$old_ext_type}?";	// ubbthreads/showthreaded.php?Cat=&Board=othertopics&Number=79355&page=0&view=collapsed&sb=5&o=&fpart=1 -- going to thread link, not post, meh
		$old_user_script 	= "showprofile{$old_ext_type}"; 	// ubbthreads/showprofile.php?Cat=&User=SaraSally&Board=isgbannounce&what=ubbthreads&page=0&view=collapsed&sb=5&o -- username
		break;
	case 'vb3' :
		$old_forum_script 	= "forumdisplay{$old_ext_type}?f=";
		$old_thread_script 	= "showthread{$old_ext_type}?p=";
		$old_post_script 	= "showpost{$old_ext_type}?p=";
		$old_user_script 	= "member{$old_ext_type}?u=";
		break;
	case 'ipb2' :	// Single file controller, these are here for refrence, the preg_match finds the actual type during the matching
		$old_forum_script 	= "showforum=";						// index.php?showforum=X
		$old_thread_script 	= "index.php?showtopic=";
		$old_post_script 	= "index.php?";						// index.php?s=&showtopic=209664&view=findpost&p=XXXXXXXX
		$old_user_script	= "index.php?showuser=";			// index.php?showuser=XXXXX
		break;
	default :
		// No valid system entered
		die('No valid system entered');
}

// It's for the old forum
if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}") === 0)
{
	switch ($old_system)
	{
		case 'phpBB2' :
			// It's a forum link
			if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}{$old_forum_script}") === 0)
			{
				$action = 'forum';
				$old_id = intval(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?f=')+3));
				$sql = "SELECT forumid FROM {$tableprefix}forum WHERE importforumid={$old_id}";
			}

			// It's a thread link
			if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}{$old_thread_script}") === 0)
			{
				$action = 'thread';
				if( preg_match("/&/", $_SERVER['REQUEST_URI']) )
				{
					$old_id = intval(substr(substr($_SERVER['REQUEST_URI'], 2), 0,  strpos(substr($_SERVER['REQUEST_URI'], 2), '&')));
					$sql = "SELECT threadid FROM {$tableprefix}thread WHERE importthreadid={$old_id}";
				}
				else
				{
					$old_id = intval(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?t=')+3));
					$sql = "SELECT threadid FROM {$tableprefix}thread WHERE importthreadid={$old_id}";
				}
			}

			// It's a post link
			if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}{$old_post_script}") === 0)
			{
				$action = 'post';
				if( preg_match("/&/", $_SERVER['REQUEST_URI']) )
				{
					$old_id = intval(substr(substr($_SERVER['REQUEST_URI'], 2), 0,  strpos(substr($_SERVER['REQUEST_URI'], 2), '&')));
					$sql = "SELECT postid FROM {$tableprefix}post WHERE importpostid={$old_id}";
				}
				else
				{
					$old_id = intval(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?p=')+3));
					$sql = "SELECT postid FROM {$tableprefix}post WHERE importpostid={$old_id}";
				}
			}

			// It's a user link
			if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}{$old_user_script}") === 0)
			{
				$action = 'user';
				// Cuts 12 out of this : profile.php?mode=viewprofile&u=12&sid=f646e2a0948e0244ba82cef12c3b93d8
				$old_id = intval(substr(substr($_SERVER['REQUEST_URI'], 19), 0,  strpos(substr($_SERVER['REQUEST_URI'], 19), '&')));
				$sql = "SELECT userid FROM {$tableprefix}user WHERE importuserid={$old_id}";
			}

I know I'm "reaching" with this but all people on VB.org/com are tarded and won't answer me and I'm kind of in need so I figured it can't hurt to ask. For a PHP whiz I imagine it wouldn't even take that long.

In the famous words of someone else on this forum who I forget so I cannot name:

Discuss
 


I won't be able to work on it till tomorrow, but if it isn't solved by then I'll give it a whirl.
 
This sets the variables we use below to determine if we need to parse the old urls.

Code:
case 'smf'        
        $old_forum_script    = "index.php?board=";                    //http://mysite.com/index.php?board=5.0
        $old_thread_script   = "index.php/topic,";                    //http://mysite.com/index.php/topic,775.0.html
        $old_post_script     = ".msg";                                //http://mysite.com/index.php/topic,603.msg6020.html#new
        $old_user_script     = "index.php?action=profile,u=";         //http://mysite.com/index.php?action=profile;u=41
        break;
This is the actual parser.

Forum Link - Tell it to grab the querystring value for 'board'

Code:
            // It's a forum link
            if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}{$old_forum_script}") === 0)
            {
                $action = 'forum';
                $old_id = intval($_GET['board']);
                $sql = "SELECT forumid FROM {$tableprefix}forum WHERE importforumid={$old_id}";
            }


Thread Link
- Tell it to grab the int value for thread id

Code:
// It's a thread link
            if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}{$old_thread_script}") === 0)
            {
                $action = 'thread';
                // Cuts 775 out of this : /index.php/topic,775.0.html
                $old_id = intval(substr(substr($_SERVER['REQUEST_URI'], 17), 0,  strpos(substr($_SERVER['REQUEST_URI'], 17), '.')));
                $sql = "SELECT threadid FROM {$tableprefix}thread WHERE importthreadid={$old_id}";
            }
Post Link - Tell it to grab the int value for post

Code:
 // It's a post link
            if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}{$old_post_script}") === 0)
            {
                
                $action = 'post';
                // Cuts 6020 out of this /index.php/topic,603.msg6020.html#new
                $old_id = (substr(substr($_SERVER['REQUEST_URI'], intval(strpos(substr($_SERVER['REQUEST_URI'], 0), '.msg')) + 4), 0,  strpos(substr($_SERVER['REQUEST_URI'], intval(strpos(substr($_SERVER['REQUEST_URI'], 0), '.msg')) + 4), '.')));

                $sql = "SELECT postid FROM {$tableprefix}post WHERE importpostid={$old_id}";

            }
User Link - Tell it to grab the int value for users profile

Code:
// It's a user link
            if (strpos($_SERVER['REQUEST_URI'], "/{$old_folder}{$old_user_script}") === 0)
            {
                $action = 'user';
                // Cuts 41 out of this : index.php?action=profile;u=41
                $old_id = intval(substr($_SERVER['REQUEST_URI'], 28, 4));
                $sql = "SELECT userid FROM {$tableprefix}user WHERE importuserid={$old_id}";
            }

I would strongly suggest you test all these out prior to using it and post any problems. Also it only checks for the parent post links and not anchors such as #5444
 
AWESOME iconic. THANK YOU.

unfortunately its kind of hard to "test" this since if I put it up its going to start redirecting traffic immediately which I don't want to do until after the transition is made? Any recommendations?

Anyone have thoughts after browsing ikonic's code? Should I just make the transition and test it out?
 
I've tested all the code myself and barring a copy and paste issue you should be fine.

However, there is no substitute for your own testing.

1. create a page called test1.php (same length as index.php)
2. Add the following code into test1.php
Code:
<?
// Cuts 775 out of this : /index.php/topic,775.0.html
$old_id = intval(substr(substr($_SERVER['REQUEST_URI'], 17), 0,  strpos(substr($_SERVER['REQUEST_URI'], 17), '.')));

echo($old_id);
?>

3. Throw some topic urls at it and ensure it returns the correct topic id each time.
4. Repeat for each $action (forum, post, user) ensuring you update the script that assigns the value to $old_id and test with different urls

If everything returns correctly then you should be fine to implement. If not post any issues here so we can investigate.
 
Okay so this APPEARS to work for Board ID and Thread ID... it returns the number it should at the top and then below it loads the index though?

I'm going to PM you the URL I'm testing it on. Check your PMs.
 
Status
Not open for further replies.