Gravatar mod for sNews 1.7

With this mod you can allow your users to add their email address to their comments and use that email address to fetch their gravatar, if they have one. I've been using it here on my site for quite a while now, as you can see.

So without further ado let's get started. As always BACK-UP your snews.php file and work off a copy.

1. find the function comment and add the following highlighted code;

$url = (strlen($url) > 8 && strpos($url, '?') === false) ? clean(cleanXSS($url)) : null;
$email = trim($_POST['email']);
$email = (strlen($email) > 5 && preg_match( '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' , $email)) ? clean(cleanXSS($email)) : null;

2. still within the function comment find and make the following highlighted changes;

$query = 'INSERT INTO '._PRE.'comments'.'(articleid, name, url, comment, time, approved, email) VALUES'."('$post_article_id', '$name', '$url', '$comment', '$time', '$approved', '$email')";

3. still within the function comment find and add the following highlighted code

'name'=>$name,
'email'=>$email,
'comment'=>$comment,

4. still within the function comment find and add the following highlighted code, make sure you adjust the rating if you like, and the gravatar size, etc;

case ($tag == 'comment'):
$email = retrieve('email', 'comments', 'id', $r['id']);
$name = $r['name'];
$default = _SITE."/images/gravatar.png";
$size = "48";
$rating = "pg";
$gravatar = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($email)."&rating=".$rating."&default=".urlencode($default)."&size=".$size;
echo '<img class="gravatar" src="'.$gravatar.'" width="'.$size.'" height="'.$size.'" alt="'.$name.'" /> ';
echo $r['comment'];
break;

5. still within the function comment find and add the following highlighted code;

echo html_input('text', 'url', 'url', $url, l('url'), 'text', '', '', '', '', '', '', '', '', '')."\r\n";
echo html_input('text', 'email', 'email', '', l('cemail'), 'text', '', '', '', '', '', '', '', '', '');
echo html_input('textarea', 'text', 'text', stripslashes($comment), '* '.l('comment'), '', '', '', '', '', '5', '5', '', '', '')."\r\n";
echo mathCaptcha()."\r\n";  

6. find the function edit_comment and make the following highlighted changes;

$query = mysql_query('SELECT id,articleid,name,url,email,comment,approved FROM '._PRE.'comments'.' WHERE id='.$commentid);

7. still within the function edit_comment find and add the following highlighted code;

echo html_input('text', 'url', 'url', $r['url'], l('url'), '', '', '', '', '', '', '', '', '', '');
echo html_input('text', 'email', 'email', $r['email'], l('email'), '', '', '', '', '', '', '', '', '', '');

8. find the function processing and add the following highlighted code;

$url = cleanXSS($_POST['url']);
$email = cleanXSS($_POST['email']);
$comment = $_POST['editedcomment'];

9. still within the function processing find and add the following highlighted code;

if (isset($_POST['submit_text'])) {
    mysql_query("UPDATE "._PRE.'comments'." SET
    name = '$name',
    url = '$url',
    email = '$email',
    comment = '$comment',
    approved = '$approved'
    WHERE id = $commentid");
} else if (isset($_POST['delete_text'])) {

10. Let's add the email field to the database now.

ALTER TABLE comments ADD email varchar(255) NOT NULL;

11. Now add the gravatar class to your stylesheet.

.gravatar {
  float: right;
  padding: 6px;
}

12. And finally, you need to add the form text to your language file (EN.php in the lang folder most likely)

$l['cemail'] = 'Email (will not be published, for <a rel="nofollow" href="http://www.gravatar.com">gravatar</a> only)';

That should be it, you should now have gravatars available. If you don't have a default gravatar to use, here's the one I use,
default gravatar

As always, if I forgot a step or things didn't turn out quite right, please let me know so I can look into it and fix it if needed.

Share this post:
Share of Facebook Tweet this post Share this post on Google+ Email this post


Comments

RSS Comments Feed


Matt's Avatar

Matt

jesth, I'll shoot ya an email tomorrow and see if I can explain what I did.

jesth's Avatar

jesth

That would be really, cool, atleast then I will know if I ever where close to actually doin the right thing.

pchenk's Avatar

pchenk

Matt,

I followed the tutorial from the beginning to make gravatar, run time does not display anything. What's wrong?? sorry my english not well..

Matt's Avatar

Matt

hi pchenk. If you can use the contact form and send me a link to the finished page with comments, and I'll have a look at the HTML.

John Flower's Avatar

John Flower

I can't seem to find a panel to accept or reject the comments. I followed this mod, and I can place a comment, but when i received the email to tell me there's a comment awaiting moderation, i went to the site, but couldn't find any place to accept or reject. any idea what it might be?

Matt's Avatar

Matt

That's odd John, this mod doesn't change anything within function administration, which is where the edit/preview comments should be.

Did you check the database to see if the comment was actually stored? Maybe something isn't right and preventing the comment from correctly being inserted into the DB?

John Flower's Avatar

John Flower

Ah, yes, perhaps I should check the database. Thanks!

Is there anywhere on the site I can make a donation? Your work is fantastic.

John Flower's Avatar

John Flower

I checked the database. I can't browse the comments. That means there aren't any, right? Not too clued up on PHP and MySQL.

Matt's Avatar

Matt

John,

That means you're probably having a problem actually inserting the comments into the DB.

Have you installed any comment mods?

Don't worry about a donation, this stuff is just for fun :)

John Flower's Avatar

John Flower

Yup, I think I messed up the system with some sort of mod. I'll sort it out.

Thanks!

Gerry MacOstair's Avatar

Gerry MacOstair

Matt,
for the sake of less seasoned coders: in step 2.) there is a little typo, which might cause some people to search for the right portion of code in vain.
._PRE."comments
should read
._PRE.'comments
- it's supposed to be a concatenation rather than the begin (or end) of a string.
The mod itself works like a charm. Cheers, Gerry

Matt's Avatar

Matt

Thanks Gerry,

I just opened a stock snews.php and you are correct.

Mine is so hacked up, I'm not sure why I even changed that, but the entire string is different, hah! I will update the article.

Gerry MacOstair's Avatar

Gerry MacOstair

Matt,
your site has a fine new design and all (I think I said some nice things about this mod already), but you might want to find a way to actually "highlight" the "following highlighted code", as time permits. Not complaining, just saying.
Cheers,
Gerry

Matt's Avatar

Matt

Ha! Whoops, must've left that class out of the new stylesheet. I'll see if I can remedy that now.

Matt's Avatar

Matt

Fixed, thanks for the head's up Gerry!



(optional, not publicly displayed) (optional)

Site News

About Matt

Privacy Policy | About Me
Copyright © 2002 - 2012 Matt Jones
HTML5/CSS3
Back to Top Back to top