PDA

View Full Version : [ Release v2.0.1 ] External discussion links (join/start a conversation!)


unixman
19 Jun 2001, 19:51
Another quick, but kick-arse hack. We have a home-made content management system (actually, a beta tool see incursio.com for more info - product is called Editio). At any rate, it allows staff or community members to create online articles. Once articles are published, I wanted a way to automatically link them into our forums, so people could start a discussion, or join a discussion already in progress about that particular article. Yahoo! does this with news events as well.

At any rate, perl script. chmod 755, I call it discuss_link.cgi. It should be invoked via SSI. Under Apache:


<!--#include virtual=/cgi-bin/discuss_link.cgi?topic=Downhill+Skiing+Basics&forum=5 -->


In the link above, plug in your forum number (forumid column in VB), and the title of the article. The title will be used as the title of the post. If a post exists in the passed forum with that title, a link to "Join the conversation" will be emitted. Otherwise, you get a link called "Start a conversation". You get the picture.

Our content management system (through its plugin concept) allows us to have stuff like this embedded within each article - you could cobble up something similar in your own system with a little effort.


#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

use DBI;

#Require the cgi library
require '/opt/web/cgi-bin/cgi-lib.pl';

&ReadParse(*input);

# Grab fields
$topic = $input{'topic'};
$forum = $input{'forum'};

# Does the forum/topic combo exist?

# Connect to mySQL

$dbh = DBI->connect( "DBI:mysql:vbulletin", "id", "password");

if ( !defined $dbh ) {
print "<LI>Cannot connect to mySQL server: $DBI::errstr\n";
}

# Grab the forum name

$q = "SELECT title FROM forum WHERE forum = $forum";

$sth = $dbh->prepare($q);
$sth->execute;
@result = $sth->fetchrow_array;
$forum_name = $result[0];
$sth->finish;

$quoted_title = $dbh->quote($topic);
$q = "SELECT threadid FROM thread WHERE forumid = $forum AND title = $quoted_title";
$sth = $dbh->prepare($q);
$sth->execute;
@result = $sth->fetchrow_array;
$threadid = $result[0];
$sth->finish;

# Prep for linking
$topic =~ tr/ /+/;

if( $threadid <= 0 ) {
print "<A HREF=http://www.mysite.com/forums/newthread.php?s=&action=newthread&forumid=$forum&subject=$topic>Start a Conversation!</A>";
exit(1);
}

print ("<A HREF=http://www.mysite.com/forums/showthread.php?s=&threadid=$threadid>Join the Conversation!</A>");

exit;



Cheers.

unixman
19 Jun 2001, 19:58
For those who'd like to see it in action - take a peek:

http://www.wetcanvas.com/Articles/Scott_Burkett/105/

Check the bottom of the page.

Cheers.

Nicholas Brown
20 Jun 2001, 08:50
Good hack, but you should have done it in PHP and not icky pUrl :)

Balbanes
20 Jun 2001, 13:07
Oh wow that really is cool!

I think it would be good for long community news articles too.

unixman
20 Jun 2001, 13:44
So rewrite in PHP. I wrote it in Perl because it was done long before I ever had vbulletin.

v0n
20 Jun 2001, 13:50
unixman - you are the man! Thanks for the hack.

TigerLily
20 Jun 2001, 17:26
Awesome hack! Thank you very much!

Any idea on when Editio will be ready for release? Looks like a cool product.

TigerLily:)

unixman
20 Jun 2001, 18:18
A bit off-topic for this board, but over the summer for sure. Just add yourself to our mailing list and you'll be the first to know. :-)

Cheers.

akiy
20 Jun 2001, 19:33
Originally posted by unixman
So rewrite in PHP.
I went and rewrote the above in PHP. I've attached discussion_link.php below.

All you should need to do is include("discussion_link.php") where you would like the "Start/Join a Conversation!" link to be. You'll need to set your own username, password, database, and forumpath in the script, of course.

I've tried out the script on my site and it looks like it works. Any improvements, bug fixes, and such are always welcome.

ztsky
22 Jun 2001, 01:08
Cool!
But how to use it?

stroppytart
28 Jun 2001, 09:07
Ok, I imstalled both the .cgi and .php versions on a test page, but neither worked. Here's the gist of the page:


<HTML>
<HEAD>
<BASEFONT face="Verdana, Arial, helvetica" size="2">
<content="4; URL=http://www.lilymud.com/forum/">
<META name="Author" CONTENT="Lilymud">
<META name="Description" content="">
<META name="Keywords" content="">
<TITLE>Test</TITLE>
<STYLE TYPE="text/css">
<!--
BODY {
SCROLLBAR-BASE-COLOR: 000080;
SCROLLBAR-ARROW-COLOR: ffffff;
}
SELECT {
FONT-FAMILY: Verdana,Arial,Helvetica;
FONT-SIZE: 9px;
COLOR: black;
BACKGROUND-COLOR: #CFCFCF
}
TEXTAREA, .bginput {
FONT-SIZE: 10px;
FONT-FAMILY: Verdana,Arial,Helvetica;
COLOR: black;
BACKGROUND-COLOR: #CFCFCF
}
A:link, A:visited, A:active {
COLOR: FFFFFF;
TEXT-DECORATION: underline;
}
A:hover {
COLOR: ffffcc;
TEXT-DECORATION: none;
}
-->
</STYLE>
</script>
</head>

<body bgcolor="#666696">

<p>&nbsp;</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p align="center"><font face="Arial">Text/article here.</font></p>

<p align="center"><font face="Arial"></font></p>
<!--#include virtual=/discussion_link.php?topic=Topic+Title+Here&forum=13 -->


</blockquote>
</blockquote>
</blockquote>
</blockquote>
</body>
</html>


What am I doing wrong? I'm not extremely familiar with the #include option.

For reference, here's my discussion_link.php page (password/username starred out for security reasons..duh):


<?
############################################################################
# Per-Site Variable Settings
############################################################################

$username = "*******";
$password = "*******";
$database = "lilymud";
$forumpath = "http://www.lilymud.com/forum"; # no trailing /

############################################################################

$link = db_connect() or exit();
$query = "SELECT title FROM forum WHERE forumid = $forum";

$result = mysql_query("$query") or exit();

if (mysql_num_rows($result)) {
while ($row = mysql_fetch_array($result)) {
$forum_name = $row["title"];
}
}

$quoted_title = addslashes($topic);
$query = "SELECT threadid FROM thread WHERE forumid = $forum AND title = \"$quoted_title\"";
$result = mysql_query("$query") or exit();
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_array($result)) {
$threadid = $row["threadid"];
}
}
$topic = str_replace(" ", "+", $topic);

if ($threadid <= 0) {
print "<A HREF=\"$forumpath/newthread.php?s=&action=newthread&forumid=$forum&subject=$topic\">Start a Conversation!</A>";
} else {
print ("<A HREF=\"$forumpath/showthread.php?s=&threadid=$threadid\">Join the Conversation!</A>");
}

function db_connect ()
{
global $username, $password, $database;
$link = @mysql_pconnect("localhost", $username, $password);
if ($link && mysql_select_db($database)) return($link);
return(FALSE);
}

?>

stroppytart
30 Jun 2001, 15:57
Can anyone help..?

unixman
30 Jun 2001, 20:35
The #include will only work if your Apache web server supports server-side includes (SSI). With regard to the PHP version that akiy posted - you just have to include it in using PHPs include syntax:


<? include "/path/to/your/script/whatever.php"; ?>


Cheers.

Juan
02 Jul 2001, 00:46
This looks like a nice hack!

I am testing it with the php version. I have named it discussion.php and uploaded it to the forums directory.

What is exactly what I need to include below to make sure it starts or joins in a conversation?

<? include "/path/to/my/forums/discussion.php"; ?>

The way it is, it just displays a blank page.

stroppytart
02 Jul 2001, 08:51
OK, that worked, but now I recieve this error where the link should be.

Warning: open_basedir restriction in effect. File is in wrong directory in /home/sites/site207/web/browse.php on line 306

Warning: Failed opening '/discussion_link.php' for inclusion (include_path='.:..:/usr/local/lib/php') in /home/sites/site207/web/browse.php on line 306



The usr/local/lib/php would be necessary? I wasn't aware php required a bin.. but I'm a novice, so eh.. is there any quick fix to this?

stroppytart
04 Jul 2001, 14:46

chilliboy
04 Jul 2001, 17:20
I would guess your include path is not the full path - if your on a shared server this is likely to be the case - you may need to add sometheing like www/public_html/ infront of your path. You cant set a .htaccess to stop you having to do this repetitively (which I have done) - but don't ask me how off my head. - do a search.

stroppytart
04 Jul 2001, 20:11
this is the full, absolute location.

<? include "/home/sites/site207/web/discussion_link.php"; ?>

I insert that text, on .php pages, .cgi pages, .html pages.. no luck. its blank on all of them. Not so much as an error anymore.

chilliboy
05 Jul 2001, 09:09
Have you tried it without the starting / ie <? include ("home/sites/site207/web/discussion_link.php"); ?> ??

I doubt this would be the prob though as you would get 'file not found error' - unless you have turned off error reporting.

PS. I haven't tried this hack yet, so I may be missing something.

stroppytart
05 Jul 2001, 21:34
Warning: open_basedir restriction in effect. File is in wrong directory in /home/sites/site207/web/get.php on line 144

Warning: Failed opening 'home/sites/site207/web/discussion_link.php' for inclusion (include_path='.:..:/usr/local/lib/php') in /home/sites/site207/web/get.php on line 144


Got this again. Do you think the first "open_basedir" is a setting, perhaps a permissions setting that needs to be configured? I did a google search for this error text, and it appears to be VERY common.

chilliboy
12 Jul 2001, 14:22
Sorry its lost me - maybe its a host thing - contact them.

Deacon Frost
24 Jul 2001, 17:34
I've a Question to the cgi-version (php works fine *g*)... When my DB is not stored on localhost, what have I to add?

unixman
24 Jul 2001, 18:24
Originally posted by Deacon Frost
I've a Question to the cgi-version (php works fine *g*)... When my DB is not stored on localhost, what have I to add?

Change:


# Connect to mySQL

$dbh = DBI->connect( "DBI:mysql:vbulletin", "id", "password");


to:


# Connect to mySQL

$dbh = DBI->connect( "DBI:mysql:vbulletin:xyz.mydomain.com", "id", "password");


Cheers.

Juan
25 Jul 2001, 01:30
Can someone please explain this hack better, I cannot get it to work. Instructions are not that clear to me.

unixman
25 Jul 2001, 03:37
This isn't a "hack", but a simple add-on script. What problems are you having specifically? "I can't get it to work" doesn't tell us a whole lot.

Juan
25 Jul 2001, 05:16
Thank you for your reply.

I don't undertand/know what code I need to add in the comment/join converstation to make it work.

tamarian
27 Jul 2001, 19:29
This is a great script.

I just added it (the PHP version) and it works.

Just one problem when the title of the page contains '#' it halts.

I tried htmlspecial chars, and stripslashes but they didn't change the behaviour. Any tricks to make it work with '#"?

tamarian
29 Jul 2001, 20:00
Another observation. If the page title ($topic) had an '&' it will trim it to just before the '&'

My guess is that the php parser expects a variable following &.

Example:

$topic = "Sweet & Sour" will create a thread titled "Sweet"

htnlspecialchars won't help, it will replace & with &amp; !! :)

Any ideas?

unixman
30 Jul 2001, 18:01
With the PHP version of this script, you'll need to use urlencode() to encode the title before passing it along. There are similar solutions for Perl scripts out there as well.

tamarian
30 Jul 2001, 21:41
Yes, this fixed both of my problems, the & and #.

Thanks!

jarvis
30 Jul 2001, 21:51
Ok, being a PHP-idiot, where exactly would the urlencode() go?

thanks...

tamarian
30 Jul 2001, 22:04
Jarvis,

Find:

if ($threadid <= 0) {


And add this before it:


$topic = urlencode($topic);