";
$dot="";
$in = preg_replace ("/\b([\w.\-]+)@([\w\-]+)\.([\w\-.]+)\b/", "\\1$at\\2$dot\\3", $in);
return $in;
}
function badaddress ($in){
if (!preg_match("/^[\w.-]+@[\w-]+\.[\w.-]+$/",$in)) {return 1;} # basic format and chars
elseif (preg_match("/\.\./",$in)) {return 1;} # double dots
elseif (preg_match("/@\.|\.@/",$in)) {return 1;} # @ adj dot
elseif (preg_match("/^\./",$in)) {return 1;} # dot at begining
elseif (preg_match("/\.$/",$in)) {return 1;} # dot at end
else {return 0;}
}
function charray($a){
foreach ($a as $key=>$val){ $a[$key]=rtrim($val); }
return $a;
}
function makelinks($in){
$in = preg_replace ("/\b(http:\/\/[\w?=%~&\-.\/+]+[\w\/])/", "\\1", $in);
$in = preg_replace ("/([^\/])(www\.[\w?=%~&\-.\/+]+[\w\/])/", "\\1\\2", $in);
return $in;
}
function makepass($c){
srand ((float) microtime() * 10000000); $password="";
$okletters="abcdefghijklmnopqrstuvwxyz0123456789";
while ($c){ $c--; $password.=substr($okletters, intval(rand(0,36)),1 );}
return ($password);
}
function nicedate($wk){
if (preg_match("/(\d\d\d\d)\-(\d\d)\-(\d\d)/", $wk,$m)){
list($bla,$yr,$mo,$dy)=$m; $mo--;
}
else { return($wk);}
$wd=array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct" ,"Nov","Dec");
return ("$dy $wd[$mo] $yr");
}
function goodtext($work){
global $hcount,$ecount;
$hcount= $ecount=0;
for($c=0;$c<256;$c++){
$char=ord(substr($work,$c,1));
if ($char<32 && $char != 10 && $char != 13 && $char != 9 ){$hcount++;}
if ($char>126){$ecount++;}
}
$tcount=strlen($work);
if ($hcount){return (0);}
if ($ecount> (0.05*$tcount)){return(0);}
return (1);
}
function myhtml2txt($wk){
#### Handle
tag
if (preg_match("/<\s*pre\s*>/i",$wk)){
$temp="";
while (preg_match("/^(.+?)<\s*pre\s*>(.+?)<\s*\/\s*pre\s*>(.+)$/si",$wk,$m) ){
$beg=$m[1];$pre=$m[2];$aft=$m[3];
$pre=preg_replace("/ /","{{s}}",$pre);
$pre=preg_replace("\t/","{{t}}",$pre);
$pre=preg_replace("\n/","{{n}}",$pre);
$temp.=$beg.$pre; $wk=$aft;
}
$wk=$temp;
}
#### Ditch chunks
#preg_match("/<\s*title\s*>\s*(.+?)\s*<\s*\/\s*title\s*>/is",$wk,$m);$title=$m[1]; # keep title
$wk=preg_replace("/^.+<\s*\/\s*head\s*>/si","",$wk); # ditch: head
$wk=preg_replace("/<\s*style.*?>.*?<\s*\/\s*style\s*>/si","",$wk); # style defns
$wk=preg_replace("/<\s*script.*?>.+?<\s*\/\s*script\s*>/si","",$wk); # scripts
$wk=preg_replace("/[\t\n ]+/"," ",$wk); # newlines, spaces, tabs
#### Transfer tag information
$wk=preg_replace("/ *< *a +href *= *\"?([^\"> ]+)\"?.*?> */i"," $1 ",$wk); # retain urls
$wk=preg_replace("/ *< *(br|table).*?> */i","\n",$wk); # various \n triggers
$wk=preg_replace("/ *< *\/ *(dd|dt|li|tr).*?> */i","\n",$wk); # "
$wk=preg_replace("/ *< *\/?(dl|ol|ul|hr|p|h\d|blockquote).*?> */i","\n\n",$wk); # "
$wk=preg_replace("/ *< *\/td *> */i","\t",$wk); # crude table coherance
#### Map (some of the) special chars
$a=array('nbsp','quot','lt','gt','copy','deg','sup1','sup2','sup3','frac14','frac12','frac34','pound','cent','yen');
$b=array(" ",'<','>','©','°','¹','²','³','¼','½','¾','£','¢','¥');
for($c=0;$c<14;$c++){ $find=$a[$c];$repl=$b[$c];$wk=preg_replace("/&$find;/",$repl,$wk); }
$wk=preg_replace("/(151);/e",ord(151),$wk); # change format
$wk=preg_replace("/(149);/e",ord(149),$wk); # change format
$wk=preg_replace("/(9);/e"," ",$wk); # change format
#### Clean up
$wk=preg_replace("/<.+?>/s","",$wk); # ditch all tags
$wk=preg_replace("/(\n[\t ]+|[\t ]+\n)/","\n",$wk); # stray whitespace
$wk=preg_replace("/(\n[\t ]+|[\t ]+\n)/","\n",$wk); # stray whitespace
$wk=preg_replace("/\n\n+/","\n\n",$wk); # ditto
$wk=preg_replace("/{{s}}/"," ", $wk);
$wk=preg_replace("/{{t}}/","\t",$wk);
$wk=preg_replace("/{{n}}/","\n",$wk); # pre formating
$wk=preg_replace("/^\s+/","",$wk);
$wk=preg_replace("/\s+$/","",$wk); # whitespace top and end
return($wk);
}
function mysafehtml($wk){
#removes any suspect html
$wk= preg_replace("/<\s*appl[^>]+>.+?<\s*\/appl[^>]+>/si", "", $wk);
$wk= preg_replace("/<\s*embb[^>]+>.+?<\s*\/embb[^>]+>/si", "", $wk);
$wk= preg_replace("/<\s*scri[^>]+>.+?<\s*\/scri[^>]+>/si", "", $wk);
$wk= preg_replace("/<\s*obje[^>]+>.+?<\s*\/obje[^>]+>/si", "", $wk);
$wk= preg_replace("/<\s*meta[^>]+>/si", "", $wk);
$wk= preg_replace("/",$wk); #comments, ssi
$wk= preg_replace("/(<[^>]+) on[\w]+ ?= ?\"[^\" >]+\"([^>]*)>!--.+?>/si","$1 tag=stripped $2",$wk); #event handlers
$wk= preg_replace("/( src\s*=\s*\"?)http[^\" >]+/si","$1"."ext-img.gif",$wk); #external images
$wk= preg_replace ("'<[?%]=? [^>]+?>'si", "" ,$wk); #php and asp
return($wk);
}
function make7bit ($body){
// removes any 8 bit chars suitable for email
$body=preg_replace("/\r\n/","\n",$body);
$body=preg_replace("/[\r\cM]/","",$body);
$body=preg_replace("/[^\x20-\x7E\n\t]/","§",$body);
return($body);
}
function tidyup($wk){
// cleans up stray white space
$wk = preg_replace("/[ \t]+\n/", "\n", $wk);
$wk = preg_replace("/\n[ \t]+\n/", "\n\n", $wk);
$wk = preg_replace("/\n\n+/", "\n\n", $wk);
$wk=trim($wk);
return($wk);
}
function sanitize($wk){
## One off sift of any new form data
## this is a matched pair to htmlize()
// delete any suspect chars ie control chars, nulls, etc
#$wk=preg_replace("/[\x00-\x08\x0B-\x0C\x0E-\x1F]/","",$wk); //ditch ctrl chars
#$wk=preg_replace("/[\x7f]/","",$wk); // ?
//make some judicous char conversions for the sake of sanity
$wk=preg_replace("/\t/"," ",$wk); # tab
$wk=preg_replace("/[\x93\x94]/","\"",$wk); # smart double quotes
$wk=preg_replace("/[\x91\x92]/","'",$wk); # smart single quotes
$wk=preg_replace("/[\x95]/","*",$wk); # bullet
$wk=preg_replace("/[\x85]/","...",$wk); # ellipsis
$wk=preg_replace("/[\x86\x87]/","--",$wk); # m and n dashes
$wk=preg_replace("/[\xA0]/"," ",$wk); # nbsp
// manage quotes and magic quoting
$wk= preg_replace ("/\\\([\'\"\\\])/","\\1",$wk); // ditch backslashes put there by magic quoting
$wk= preg_replace ("/['\"]/","`",$wk); // solve sql single quote hassles simply by using `s
$wk= preg_replace ("/\\\/","/",$wk); // convert any stray backslashes
// once and for all time rid any dos newlines
$wk=preg_replace("/\r\n/","\n", $wk);
$wk=preg_replace("/[\r\cM]/","", $wk);
// Sift any undesirable html
$wk= mysafehtml($wk);
//worst case over length
if (strlen($wk)>100000){bail("Form variable exceeds 100K");}
//tidy
$wk= tidyup($wk);
return ($wk);
}
function htmlize($wk){
## gets plain text ready for placing web page
## adds br tags and converts <>"& etc to avoid problems
// translate key html chars
$wk= htmlspecialchars($wk); //avoids [<>"&] stuffing up the webpage, which dismembers any html tags
// convert plural whitespace
$wk = preg_replace("/ /", " ", $wk);
// convert newlines
$wk= trim ($wk);
$wk = preg_replace ("/\n/", "
", $wk);
return($wk);
}
?>