Tuesday, May 15, 2012

How to Get Forum Change Alerts by Email

I've a couple of forums(Mybb and PHPBB) but for new forums we are always quite eager to respond/moderate new posts. I searched all options so that in case of new posts I get notified automatically. I even tried http://www.Watchthatpage.com but did not work out.
I was wasting lot of time in visiting my forums for new posts. Ultimately I've written a php script which is executed by crontab every hour on my web hosting. This is a crude custom made script and I'm sure it'll not work with your forum. But you can make similar to this. It's a grate relief for me now! I get an email alert whenever there is new post!

How I'm doing is after reading the html code of page, I trim some content based on regular expression. Then I convert to text using html2text class and again I trim some content based on regular expression.
We need to remove any current times at least.

Download class.html2text.inc from here.
License : As per GNU GPL.

<?php
if(!checkruntime()) //don't run in the night times
{
exit(0); //not a time to run
}
require_once('class.html2text.inc');
define ('MYBB','mybb');
define ('PG', 'pg');
define ('EC', 'emile-coue');
$sites = array(
PG => array("creditcardpaymentgateways.in"),
MYBB => array("indianworkingwoman.org","indiaconsumercomplaints.org"),
EC => array("emile-coue.org"),
);
//Applied in html. This part is RETAINED
$reghtmlRetained = array(
EC => array('{<table.*?</table>}si'),
);
//This is applied in the text. This part is removed
$reg = array(

MYBB => array('/Current time:.*?\b[AP]M\b/i',
'/^.*Latest posts\s+Topic/s',
'/\s+Most views\s+.*/s',
'/[0-9][0-9][:-][0-9][0-9]/',
'/Today|Yesterday/',
),
PG=> array('/It is currently.*?\b[ap]m\b/i',
//'/[?]sid=[0-9a-z]+/i',
'/sid=[0-9a-z]+(#p[0-9]+)?/i',
'/[*] Delete all board cookies .*/s'

),
EC=> array('/It is currently.*?\b[ap]m\b/i',
//'/[?]sid=[0-9a-z]+/i',
'/sid=[0-9a-z]+(#p[0-9]+)?/i',
'/\s+Who Is Online\s+.*/s',
'/Discuss about any auto suggestion methods, their comparison.*/s',

)
);

chdir('/home/premg/www/nov/all/forumcomp');
foreach($sites as $key =>$sitearr)
{
foreach($sitearr as $site)
{
$site1 = "http://forum.$site";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$site1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$data = curl_exec($ch);
curl_close($ch);
$content = $data;

assert(strlen($content)>500);


if(isset($reghtmlRetained[$key]))
{
foreach($reghtmlRetained[$key] as $pat)
{
//print "$content\n";
$matches = array();
$ret = preg_match_all($pat,$content,$matches,PREG_SET_ORDER);
assert($ret > 0);

$content = "";

for($i=0;$i<$ret;++$i)
{
$content .= $matches[$i][0];
}
}
}

$h2t = new html2text($content);
$content = $h2t->get_text();

if(isset($reg[$key]))
{
foreach($reg[$key] as $pat)
{
$content = preg_replace($pat,'',$content);
// print "new pat strlen=".strlen($content)."\n";
}
}

//get the old content
$oldtext = file_get_contents($site);

if($oldtext != $content) //send email
{
send_email('preggup189456@gmail.com', $site);
//now write this new content

//rename the old one to .old
$tofile = "$site.old.txt";
unlink($tofile);
rename("$site", $tofile );

assert(file_put_contents($site, $content));

}

}

}
function send_email($email,$site)
{
print "sending email for $site ...\n";
$subject = "Content $site changed";
$body = "Click <a href=\"http://forum.$site\">here</a> to see.";
mail($email,$subject,$body);
return;
}

function checkruntime()
{
$dt = (gmdate("Hi"));
$hr = (int)($dt/100);
$min = gmdate("Hi") - $hr*100;

$hr += $min/60.0;

//convert to indian time
$hr += 5.5;

if($hr > 23)
{
$hr = $hr - 24;
}

if( ($hr >= 0 and $hr < 7) or (int)$hr == 23 )
{
return false;
}
else
{
return true;
}
}