Your IP : 216.73.216.40


Current Path : /var/www/html/mmishra/indem/iiita/
Upload File :
Current File : /var/www/html/mmishra/indem/iiita/functions.php

<?php
function dbConnect() {
        $link = mysql_connect("localhost", "root", "mysql");
        mysql_select_db("wwwdb");
        return $link;
}

function sendmail($email, $subject, $body) {
	require("class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "mail.iiita.ac.in"; // SMTP server
	$mail->FromName = "ADA Allahabad";
	$mail->From = "ada@iiita.ac.in";
	$mail->AddAddress($email,$email);
	$mail->AddAddress("ada@iiita.ac.in","ada@iiita.ac.in");
	$mail->Username = "ada";
	$mail->Password = "alld";

	$mail->Subject = $subject;
	$mail->Body = $body;
	$mail->WordWrap = 50;

	if(!$mail->Send()){
   		echo "Message could not be sent\n Mailer Error: " . $mail->ErrorInfo;
	}else{
   		echo "Message sent successfully.";
	}
}

function logAccess($user, $referer, $url) {
        if (!$user) $user = 'guest';
        if (!$referer) $referer = 'direct';
        if ($url == '/') $url = 'index.php';
        $ip = $_SERVER["REMOTE_ADDR"];
        mysql_query("insert into log values(NULL, '$user', '$referer', '$url','$ip',now())");
}

function setup($key) {
        $rs1 = mysql_query("select setup_value from setup where setup_key='$key'");
        if ($rs1) {
                $o = mysql_fetch_object($rs1);
                return $o->setup_value;
        } else {
                return "Ooops! missing";
        }
}

function checkadmin($role) {
	if ($role != "admin") {
        	echo "<div align=center>You must be a Site Administrator to access this page</div>";
        	exit;
	}
}

function checklogin($user) {
	if (!$user) {
        	echo "<div align=center>You must be logged in to access this page</div>";
        	exit;
	}
}

function banner($fname) {
  	generate_file("banner.inc", "<font color='white' size='+1' face='verdana'>
        INDIAN INSTITUTE OF INFORMATION TECHNOLOGY ALLAHABAD
      	</font>");
}

function hitcounter() {
        $file = "counter.inc";
	if (!file_exists($file)) {
        	touch ($file);
		$count=0;
        } else {
		include "$file";
        	$count++;
        }
	$str = '<? $count='.$count.'; ?>';
	$handle = fopen ($file, 'r+');
	fwrite ($handle, $str);
        fclose($handle);
        return "Hits:&nbsp;$count";
}

function hitmeter($url) {
	if (mysql_num_rows(mysql_query("select * from hitmeter where url='$url'"))) {
        	mysql_query("update hitmeter set counter=counter+1 where url='$url'");
	} else {
        	mysql_query("insert into hitmeter(url,counter,dated) values('$url',1,now())");
	}
	if (mysql_num_rows(mysql_query("select * from webpages where id='$url'"))) {
        	mysql_query("update webpages set counter=counter+1 where id='$url'");
	}
}

function gallery() {
	$fp = fopen ("gallery.inc", "r");
	if (!$fp) {
    	        $images = array("logo1.jpg");
	} else {
		$i = 0;
		$images = array();
		while (!feof ($fp)) {
    			$line = fgets ($fp, 1024);
    			//$images = explode(",", $line);
                	if ($line != "\n") $images[$i] = str_replace("\n", "", $line);
			$i++;
		}
	}
	fclose($fp);
	$i--;
	srand((double)microtime()*1000000);
        $no = rand(0, $i);
        $fname = $images[$no];
	echo "<img src='images/$fname' width='234' height='120' border='0'></img>";
}

function search() {
	if (!$conf) $conf = 'search';
	$q = htmlsafe($_REQUEST["q"]);
	
	echo "Search<br><input type='text' name='q' class='searchtext' size='15' value='$q'
	onenter=\"document.location.href='?conf=search&q='+this.value\">
	<script>
	frmStatic.q.focus();
	</script>";
}	


function showdate() {
	echo "<span style='font-family: verdana; font-size: 10px'>
	<font color='orange'>( </font>
	<font color='#000000'>" . date("l, jS F Y") . "</font>
	<font color='#ADFF2F'> )</font>
	</span>";
}

function topmenu() {
	$str = "<a href='index.php'>Home</a>&nbsp;&nbsp;
        <a href='http://btech.iiita.ac.in'>BTech</a> &nbsp;&nbsp;
        <a href='http://mtech.iiita.ac.in'>MTech</a>&nbsp;&nbsp;
        <a href='http://mba.iiita.ac.in'>MBA</a>&nbsp;&nbsp;
        <a href='http://ms.iiita.ac.in'>MS</a>&nbsp;&nbsp;
        <a href='http://profile.iiita.ac.in/list.php?grp=student&ou=Student&mydn=ou*student,dc*iiita,dc*ac,dc*in'>Student</a>&nbsp;&nbsp;
        <a href='http://profile.iiita.ac.in/list.php?grp=alumni&ou=Alumni&mydn=ou*alumni,dc*iiita,dc*ac,dc*in'>Alumni</a>&nbsp;&nbsp;
        <a href='http://placement.iiita.ac.in'>Placement</a>&nbsp;&nbsp;";
	generate_file("topmenus.inc", $str);
}

function botmenu() {
        $str = "<a href='?conf=login&target=$target' title='Please login for control panel'>MyIIITA</a>&nbsp;&nbsp;
	<a href='http://website.iiita.ac.in'>Websites</a>&nbsp;&nbsp;
        <a href='http://profile.iiita.ac.in'>Webprofiles</a>&nbsp;&nbsp;
        <a href='http://mail.iiita.ac.in'>Mailboxes</a>&nbsp;&nbsp;";
	generate_file("botmenus.inc", $str);
}

function usrmenu() {
	$col1 = '#FFEBCD';
	$col2 = 'white';
        $str = "<table width='160' border='0' cellpadding='0' cellspacing='0'>";
	$rs1 = mysql_query("select * from menus where referer=0 order by zindex");
	while ($o = mysql_fetch_object($rs1)) {
		$no++;
		$str .= "<tr><td width='175' height='15'
		onmouseover=\"style.backgroundColor='$col1';show('menu{$o->id}')\"
                onmouseout=\"style.background='url(images/transparent.gif)';hide('menu{$o->id}')\">";
                if ($o->href == 'submenu') $str .= "<a href='/'>";
                else $str .= "<a href='$o->href'>";
		$str .= "$o->title</a><br>";
	
	        $rs2 = mysql_query("select * from menus where referer>0 and referer={$o->id} order by zindex");
	        if (mysql_num_rows($rs2)) {
			$str .= "<table bgcolor='$col1' width='200' class='menu' id='menu{$o->id}'>";
			while ($p = mysql_fetch_object($rs2)) {
                		$str .= "<tr><td height='15' class='menu'
                                onmouseover=\"style.backgroundColor='$col2';\"
                                onmouseout=\"style.backgroundColor='$col1'\">";
				if (strpos($p->href,".")) $str .= "<a href='$p->href'>";
				else $str .= "<a href='?conf=$p->href'>";
				$str .= "$p->title</a></td></tr>";
			}
			$str .= "</table>";
		}
		$str .= "</td></tr>";
	}
	$str .= "</table>";
	generate_file("usrmenus.inc", $str);
	stdmenu();
	topmenu();
	botmenu();
}

function introduction(){
        $rs1 = mysql_query("select title,content from webpages where id='introduction'");
        if ($rs1) {
		$o = mysql_fetch_object($rs1);
        	$str = "<b>$o->title</b><br>$o->content<br>";
	} else {
        	$str = "Welcome...<br><br>Nothing to show here.
		Please visit control panel to create this section";
	}
	generate_file("introduction.inc", $str);
}

function login() {
	if ($user) { 
        	echo "<div align=center>
        	You are already logged in using the ID $user.<br>
        	If you chose the 'Remember me' option on the login page,<br> and no longer wish to get logged in
        	automatically, click on the following link:<br>
        	<a href='?logout=1&mode=forget'>Unremember me</a>
        	</div>";
		exit;
	 } ?>
	<br>
	<br><b>Please login here using your mail ID and password.</b><br>
	<input type='hidden' name='login'> <input type='hidden' name='target' value='<?=$target?>'>
	<table border=0 class='normal'>
        <tr><td align='right'>User ID</td>
	<td><input type='text' name='txtID' size='20' class='input' value='<?=htmlsafe($txtID)?>'></td></tr>
	<tr><td align='right'>Password</td>
	<td><input type='password' name='txtPWD' size='20' class='input' value='<?=htmlsafe($txtPWD)?>'</td>
        </tr><tr><td>&nbsp;</td><td><input type='checkbox' name='chkRem' class='input'>&nbsp;Remember me</td>
        </tr><tr><td>&nbsp;</td><td><input type='submit' name='submit' value='LOGIN' style='width:80px'></td></tr>
	</table> <br><br>
	<script> frmStatic.txtID.focus(); </script>
	<?
	if ($target) {
        	$o = mysql_fetch_object(mysql_query("select * from webpages where id='$target'"));
        	if ($o->access_scope == 'admin') {
                	echo "<br>The page you are trying to access (<b>$o->title</b>) requires authentication.</u><br>";
        	}
	}
}

		
function stdmenu(){
	$str = "<table border=0 cellspacing=2 cellpadding=0>
	<tr><td height='15'><a href='?conf=tendercur'>Tenders</a></td></tr>
	<tr><td height='15'><a href='?conf=rtiquery'>Query & RTI</a></td></tr>
	<tr><td height='15'><a href='?conf=formformats'>Form Formats</a></td></tr>
	<tr><td height='15'><a href='?conf=gallery'>Photo Gallery</a></td></tr>
        <tr><td height='15'><a href='?conf=profiles'>Web Profiles</a></td></tr>
        <tr><td height='15'><a href='?conf=telephone'>Telephones</a></td></tr>
        <tr><td height='15'><a href='?conf=feedback'>Feedback</a></td></tr>
	</table>";
	
	generate_file("stdmenus.inc", $str);
}

function footer($user, $role, $target) {
	$user = strtoupper($user);
	if ($user){
		echo "<a href='?conf=cpanel' title='Goto to control panel'><b>Control Panel</b></a> | "; 
		echo "<a href='?logout=1' title='You are logged in as $user'><b>Sign Out $user</b></a>";
	} else {
		//echo "<a href='?conf=login&target=$target' title='Please login for webadmin'><b>Sign In</b></a>";
	}
	echo "&nbsp;&nbsp;<font face='verdana' size=-2>" . hitcounter() . "<br>Webmaster: " . setup('webmaster') . "</font>";
}

function logView() {
 	mysql_query("insert into log values(NULL, '$user', '$referer', '$page_url','$ip',now())");
}	

function upload_file() {
	$maxsize =9998860800;
	$uploadfile = strtolower(basename($_FILES['userfile']['name']));
	$addto_gallery = $_POST['addto_gallery'];
	$filesize = $_FILES["userfile"]["size"];
	if ($uploadfile && $filesize < $maxsize) {
        	$filename = explode(".", strtolower($uploadfile));
        	$ext = $filename[count($filename)-1];
        	if ($ext == 'jpg' || $ext == 'gif' || $ext == 'tif' || $ext == 'png') {
			$dir = 'images/';
			if ($addto_gallery == 'yes') addto_gallery($uploadfile);
        	} else $dir = 'documents/';
        	if (move_uploaded_file($_FILES['userfile']['tmp_name'], $dir.$uploadfile)) {
                	shell_exec("chmod 755 $dir.$uploadfile");
                	echo "$uploadfile was successfully uploaded.<br><br>";
        	} else {
                	echo "Sorry! there may be some error.<br><br>";
        	}
	} else {
		echo "Either file name is missing or file size exceeds the max. limit of 10 MB<br><br>";
	}
}

function uniq_sid($uid, $pwd) {
  	$epwd = base64_encode($pwd);
  	srand((double)microtime()*100000000);
  	//generate a unique s_id
  	do {
    		$ch = "";
    		for($i=0; $i<20; $i++) $ch = $ch . chr(rand(65, 90));
    		// loop until a uniq string is generated
  	} while (!mysql_query("insert into auth values('$ch','".$_SERVER["REMOTE_ADDR"]."',now())"));
  	return $ch;
}

function authenticate($uid, $pwd) {
	$rs = mysql_query("select * from profiles where login='$uid' and passwd=password('$pwd')");
	if (mysql_num_rows($rs)) {
		$o = mysql_fetch_object($rs);
		if ($o->webmaster == 'Yes') return 'admin';
		else return 'user';
	} else {
		return 'guest';
	}
}

function guzettefeed() {
        //notices
	$rs = mysql_query("SELECT * FROM guzette where subject='notice' and status='online' order by dated desc, id desc");
        if ($rs) {
		$str = "Please take care of dates and go through the attached file. Thanks.";
		while ($o = mysql_fetch_object($rs)) {
        		$i++;
               		$str .= "<table border='0' cellpadding='0' cellspacing='2'>
               		<tr valign='top'><td><img src='images/arrow.gif'></td>
               		<td onmouseover=\"show('announce$i')\" onmouseout=\"hide('announce$i')\">
               		<a href='downloads.php?filename=$o->filename'>$o->title</a><br/>
               		<table class='announce' id='announce$i' border='0' cellpadding='0' cellspacing='5'>
               		<tr valign='top'><td align='center' class='announce'>
               		<font size=1pts>$o->description<br>Dated: $o->dated<br>Effective till: $o->expiry</td></tr></table>
               		</td></tr></table>";
        	}
        	$str .= "<div align='right'>[ <a href='http://www.adobe.com/products/acrobat/readstep2.html'>
		Download Acrobat</a> ][ <a href='?conf=noticemore'>More...</a> ]</div>";
		generate_file("notices.inc", $str);

	}
	//tenders	
        $rs = mysql_query("SELECT * FROM guzette where subject='tender' and status='online' order by dated desc, id desc");
        if ($rs) {
        	$str = "Please take care of dates and go through the attached file, if any. Thanks.<ul>";
		while ($o = mysql_fetch_object($rs)) {
                	$i++;
                	$str .= "<li><a href='downloads.php?filename=$o->filename'>$o->title</a>
			<dir style='margin:0 0 0 0'>";
                	if ($o->description) $str .= " - $o->description<br>";
                	$str .= "<div align='right'><i>Dated $o->dated Downloads $o->counter (Last-date $o->expiry)</i></div>
                	</dir></li>";
        	}
        	$str .= "</ul><div align='right'>
        	<a href='http://www.adobe.com/products/acrobat/readstep2.html'>Download Acrobat</a>
        	&nbsp;|&nbsp;<a href='?conf=tendermore'>More...</a></div>";
        	generate_file("tenders.inc", $str);
	}

        //news
        $rs = mysql_query("SELECT * FROM guzette where subject='news' and status='online' order by dated desc, id desc");
        if ($rs) {
		$str = "";
                while ($o = mysql_fetch_object($rs)) {
                        $i++;
                        $str .= "<a href='downloads.php?filename=$o->filename'>$o->title</a>";
                        if ($o->description) $str .= " - $o->description; ";
                }
                generate_file("news.inc", $str);
        }
}

function xguzettelist($subject) {
        $str = "Past guzettes only.<ul>";
        $rs = mysql_query("SELECT * FROM guzette where subject='$subject' and status='offline' order by dated desc");
        if ($rs) {
		while ($o = mysql_fetch_object($rs)) {
                	$i++;
                	$str .= "<li><a href='downloads.php?filename=$o->filename'>$o->title</a>
			<dir style='margin:0 0 0 0'>";
                	if ($description) $str .= "$o->description<br>";
                	$str .= "<div align='right'><i>During $o->dated - $o->expiry total downloads $o->counter</i></div>
                	</dir></li>";
        	}
        }
	$str .= "</ul>"; 
	return $str;
}

function xnoticefeed() {
	$str = "Past notifications only.<ul>";
        $rs = mysql_query("SELECT * FROM guzette where subject='notice' and status='offline' order by dated desc");
        if ($rs) {
		while ($o = mysql_fetch_object($rs)) {
                	$i++;
                	$str .= "<li><b>$i) <a href='$o->filename'>$o->title</a></b>
                	<dir>$o->description<br>
                	<div align='right'><i>Dated $o->dated expired on $o->expiry</i></div>
                	</dir></li>";
        	}
        	$str .= "</ul>";
		return $str;
	}
}

function tenderfeed() {
	$str = "Please adhere with the dates and go through the file where ever attached. Thanks.<ul>";
        $rs = mysql_query("SELECT * FROM tenders where status='current' order by dated desc, id desc");
        while ($o = mysql_fetch_object($rs)) {
                $i++;
                $str .= "<li><b>$o->subject</b><dir style='margin:0 0 0 0'>";
		if ($description) {
                	echo $o->description."<br>";
                }
		if ($o->filename) $str .= "More details: 
		<a href='downloads.php?filename=$o->filename&tbl=tenders'>$o->filename</a>";
                $str .= "<div align='right'><i>
		Dated $o->dated Downloads $o->counter (Last-date $o->expiry)</i></div>
                </dir></li>";
        }
        $str .= "</ul> <? tenderfeed(); ?>
	<div align='right'>
	<a href='http://www.adobe.com/products/acrobat/readstep2.html'>Download Acrobat</a>
        &nbsp;|&nbsp;<a href='?conf=tendermore'>More...</a></div>";
        generate_file("tender_cur.inc", $str);

	$i = 0;	
        $str = "Past tenders only.<ul>";
        $rs = mysql_query("SELECT * FROM tenders where status='expired' order by dated desc");
        while ($o = mysql_fetch_object($rs)) {
                $i++;
                $str .= "<li><b>$o->subject</b><dir style='margin:0 0 0 0'>";
		if ($description) {
                	echo $o->description."<br>";
                }
		if ($o->filename) $str .= "More details: 
		<a href='downloads.php?filename=$o->filename&tbl=tenders'>$o->filename</a>";
                $str .= "<div align='right'><i>
		During $o->dated - $o->expiry total downloads $o->counter</i></div>
		</dir></li>";
        }
        $str .= "</ul><div align='right'>
	<a href='http://www.adobe.com/products/acrobat/readstep2.html'>Download Acrobat</a>
        &nbsp;|&nbsp;<a href='?conf=tendercur'>More...</a></div>";
        generate_file("tender_exp.inc", $str);
}

function formatfeed() {
        $rs = mysql_query("SELECT * FROM formats where status='current' order by dated desc");
	$str = "Please click on the filename to download desired form format.<ul>";
        while ($o = mysql_fetch_object($rs)) {
                $i++;
                $str .= "<li><b>$o->description</b>
                <dir style='margin:0 0 0 0'>
		<a href='downloads.php?filename=$o->filename&tbl=formats' target='new'>$o->filename</a><br>";
                $str .= "<div align='right' style='margin:0 0 0 0'>
		<i>Dated $o->dated Downloads $o->counter</i></div>
                </dir></li>";
        }
        $str .= "</ul> <? formatfeed(); ?>
        <div align='right'>
        <a href='http://www.adobe.com/products/acrobat/readstep2.html'>Download Acrobat</a>
        </div>";
        generate_file("formats.inc", $str);
}

function newsfeed(){
        $str = '<? xml version=\"1.0\" ?> <rss version=\"2.0\"><channel>';
        $rs = mysql_query("SELECT * FROM news where status='current' order by dated desc, id desc");

        while ($o = mysql_fetch_object($rs)) {
                $str .= "<item>
                <title>$o->subject</title>
                <link>$o->url</link>
                <description>$o->description</description>
                <pubDate>$o->dated</pubDate>
                <category>Achievement</category>
                </item>";
        }
        $str .= "</channel></rss>";
	generate_file("rss.xml", $str);
}


function generate_file($file, $str) {	
        if (!file_exists($file)) {
                touch ($file);
        }
        if (!$handle = fopen ($file, 'w+')) {
		echo "$file file permission error";
	} else if (is_writable($file)) {
        	if (fwrite ($handle, $str) === FALSE) {
			echo "Can't write to $file";
		}
	}
        fclose($handle);
        $out = shell_exec("chmod 757 $file");
}

function addto_gallery($fname){
        $str = shell_exec("cat gallery.inc") . "\n";
        //if not already in gallery
	if (!strpos($str, $fname)) {
		$str .= $fname;
		generate_file("gallery.inc", $str);
	}
}

function getFileExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }

        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;
}

function create_img($image_in,$image_out,$text,$color,$font,$font_size,$angle,$offset_x,$offset_y,$shadow_color) {
        if (!$text) $text = 'mmishra@iiita.ac.in';

        if (!$offset_x) $offset_x = 0; // Horizontal offset in pixels, from the right
        if (!$offset_y) $offset_y = 0; // Vertical offset in pixels, from the bottom

        if ($shadow_color) $drop_shadow = true;
        else $shadow_color = '#909009';

        #################################################################################
        #     END OF SETTINGS
        #################################################################################

        // Load functions for image watermarking
        include("watermark_text.class.php");

        // create class instance
        $img = new Zubrag_watermark($image_in);

        // shadow params
        $img->setShadow($drop_shadow, $shadow_color);

        // font params
        $img->setFont($font, $font_size);

        // Apply watermark
        $img->ApplyWatermark($text, $color, $angle, $offset_x, $offset_y);

        // Save on server
        $img->SaveAsFile($image_out);

        // release resources
        $img->Free();
        return;
}

function check_email($email) {
   	if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) {
	      	return FALSE;
   	}
   	list($Username, $Domain) = split("@",$email);
   	if (getmxrr($Domain, $MXHost)) {
      		return TRUE;
   	} else {
      		if (fsockopen($Domain, 25, $errno, $errstr, 30)) {
         		return TRUE; 
      		} else { 
         		return FALSE; 
      		}
   	}
}

function myhref($link,$text,$desc,$target) {
	$text = htmlsafe($text);
	$desc = htmlsafe($desc);
	$href = "&nbsp;[&nbsp;<a href='$link' onMouseOut=\"ClearFlyOver();\" onMouseOver=\"FlyOver('$desc');\"";
	if ($target) $href .= " target='$target'";
	$href .= ">$text</a>&nbsp;]&nbsp;";
	echo $href;
}

function tooltips() {
	$str = "<STYLE>
    	.Help {
            position: absolute; posTop: 0; posLeft: 0;
            border-width:1; border-style: solid;
            background-color: yellow; color: #000000;
            width:240; font-size:9pt
          }
	</STYLE>
	<DIV ID=FOArea CLASS='Help' STYLE='display: none'>
	</DIV>
	<SCRIPT LANGUAGE='JavaScript'>
	<!--
    	var HelpX, HelpY, HelpText;
    	var ToShow, ToClear;

    	function DoFlyOver() {
      		if( ToClear != -1 ) window.clearTimeout( ToClear );
      		FOArea.innerText = HelpText;
      		FOArea.style.posLeft = HelpX + 20;
      		FOArea.style.posTop = HelpY + 20;
      		FOArea.style.display = '';
      		ToClear = setTimeout('ClearFlyOver()', 8000, 'JAVASCRIPT');
    	}

    	function ClearFlyOver() {
      		FOArea.style.display = 'none';
    	}

    	function FlyOver(Text) {
      		HelpText = Text;
     		HelpX = window.event.clientX;
      		HelpY = window.event.clientY;
      		ToShow = setTimeout('DoFlyOver()', 500, 'JAVASCRIPT');
    	}
	// -->
	</SCRIPT>";
	generate_file("tooltips.inc",$str);
}

function goodie($title) {
	echo "<table border=0 cellspacing=0 class=black><tr>\n";
	echo "<td><!-- img src='images/goodie2.gif' --></td>\n";
	echo "<td style='font-size:16px;'>$title</td>\n";
	echo "<td><!-- img src='images/goodie.gif' --></td>\n";
	echo "</tr></table>\n";
}

function htmlsafe($str) {
	return trim(htmlspecialchars(stripslashes($str), ENT_QUOTES));
}

function unhtmlentities ($string){
    	$trans_tbl = get_html_translation_table (HTML_ENTITIES);
    	$trans_tbl = array_flip ($trans_tbl);
    	return strtr ($string, $trans_tbl);
}

function hilite($body, $text){ 
	/* Can we make it work also for &amp; kind of stuffs.. */
	/* The &amp; etc is also taken care of now. */

	$body = unhtmlentities($body);

	$new_text = preg_replace("/\040/","+",$text);
	$new_text = explode("+",$new_text);
	foreach($new_text as $txt){
		$txt = trim($txt);
		if(empty($txt)) continue;
		$alpha_text = preg_replace("/\W/","-",$text);
		$strings = explode("-",$alpha_text);
		foreach($strings as $str){
			if(!empty($str))
			$texts[] = $str;
		}
	}
	//$patterns[$i] = '/'.preg_quote($text[$i], '/').'/i';
	$orig_body = $body;
	$new_body = "";
	foreach($texts as $text){
		$text = trim($text);
		if(strlen($text)<=2) continue;
		if($new_body!=""){ 
			$body=unhtmlentities($new_body);
			$new_body="";
		}
		while ($body!="") { 
	      		if (preg_match('{^([^<]*)?(</?[^>]+?>)?(.*)$}s',$body,$matches)) { 
			/* this while loop is to preserve case of every match. */
			$new_str = $matches[1];
			while($new_str!=""){
				$match_pos = strpos(strtolower($new_str),strtolower($text));
	      			if($match_pos===false){
					$new_body.=htmlentities($new_str);
					$new_str="";
				} else {
					if($match_pos!=0)
						$new_body.=htmlentities(substr($new_str,0,$match_pos));
						$match = substr($new_str,$match_pos,strlen($text));
						$replacement = "<span style=\"background: black; color: white;\">$match</span>";  
						$new_body.= $replacement;
						$new_str = substr($new_str,$match_pos+strlen($text));
					}
				}
	        		$new_body.= $matches[2];
	        		$body = $matches[3];
	    		} 
		}
	}
	if (!$new_body) return $orig_body;
	else return $new_body;
}
?>