<?
require_once('rsa.class.php');
/* this is a basic random HTML Generator, take a look at printRandomHTML() for usage
the random page is per seed number so each seed number will give the same random page
For Any suggestions, Bug fixes and other : Serans1 AT gmail.com*/
global $elArr; // internal usage
global $currentSeedNum; //this is the seed number, Random page is per seed number to allow consistence
global $root; //the HTML tree root
global $debug; //assinging 1 will print debug info
define("MAX_BARNCH",7); //this is the Maximum sons that each Tag can have
define("MAX_DEPTH", 7) ;// this is the HTML tree depth
define("INIT_DEPTH", 0) ;// initial depth
define("NO_POSSIBLE_SON", 10000) ;// initial depth
function initGlobals()
{
global $elArr;
global $currentSeedNum;
global $root;
$elArr=initElements();
$root=new DIV();
}
function uchr ($codes) {
if (is_scalar($codes)) $codes= func_get_args();
$str= '';
foreach ($codes as $code) $str.= html_entity_decode('&#'.$code.';',ENT_NOQUOTES,'UTF-8');
return $str;
}
function GenerateUTFRandomContent($length)
{
global $currentSeedNum;
$len = mt_rand(0,$length);
$activatecode='';
mt_srand((double)++$currentSeedNum);
$startCode=mt_rand(0,32000);
while (strlen($activatecode)<$len+1)
{
mt_srand((double)++$currentSeedNum);
$currentChar=mt_rand(0,50);
$activatecode.=uchr($startCode+$currentChar);
}
return htmlentities($activatecode, ENT_QUOTES);
if ($debug==1) echo "<br/> Random Content Generated, length:".count($activatecode)."<br/>";
}
function GenerateRandomContent($length)
{
global $currentSeedNum;
$len = mt_rand(0,$length);
$base='ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789';
$max=strlen($base)-1;
$activatecode='';
//mt_srand((double)++$currentSeedNum);
while (strlen($activatecode)<$len+1) $activatecode.=$base{mt_rand(0,$max)};
return $activatecode;
if ($debug==1) echo "<br/> Random Content Generated, length:".count($activatecode)."<br/>";
}
class HTMLelement
{
var $type;
var $dNeed;
var $father;
var $maxsons;
var $sons;
var $randCon;
var $forceContent;
function HTMLelement($type,$depthNeeded=0,$allowRandContent=0,$maxsons=0)
{
//$this->sons=array();
$this->forceContent="";
$this->father=NULL;
$this->$maxsons=$maxsons;
$this->type=$type;
$this->dNeed=$depthNeeded; //indicates that this element can not be called if insufficient depth.
$this->randCon=$allowRandContent; //indicates that random content will be generate in this TAG
}
function open()
{
echo "<$this->type>";
}
function close()
{
echo "</$this->type>";
}
function printRandContent() //this function will print Random content if Tag supports
{
global $debug;
global $currentSeedNum;
if ($debug==2) echo "Trying to Generate Random Content inside $this->type <br/>";
if ($this->randCon==0) return;
echo GenerateRandomContent(16);
}
}
class DIV extends HTMLelement
{
var $upI; //the array of tags that allowed to be the father of this tag, empty means no limitation
var $downI;//the array of tags that allowed to be the sons of this tag, empty means no limitation
function DIV ()
{
parent::HTMLelement("div",0,1);
$this->upI="";
$this->downI="table,p,a";
}
}
class A extends HTMLelement
{
var $upI;
var $downI;
function A ()
{
parent::HTMLelement("a",0,1);
$this->upI="";
$this->downI="none";
}
}
class TABLE extends HTMLelement
{
var $upI;
var $downI;
function TABLE ()
{
parent::HTMLelement("table",3,0,1);
$this->upI="";
$this->downI="tbody";
}
}
class TBODY extends HTMLelement
{
var $upI;
var $downI;
function TBODY ()
{
parent::HTMLelement("tbody",2);
$this->upI="table";
$this->downI="tr";
$this->forceContent="<tr><td>".GenerateRandomContent(16)."</td></tr>";
}
}
class TR extends HTMLelement
{
var $upI;
var $downI;
function TR ()
{
parent::HTMLelement("tr",1);
$this->upI="tbody";
$this->downI="td";
$this->forceContent="<td>".GenerateRandomContent(16)."</td>";
}
}
class TD extends HTMLelement
{
var $upI;
var $downI;
function TD ()
{
parent::HTMLelement("td",0,1);
$this->upI="tr";
$this->downI="";
}
}
class P extends HTMLelement
{
var $upI;
var $downI;
function P ()
{
parent::HTMLelement("p",0,1);
$this->upI="";
$this->downI="none";
}
}
function initElements()
{
//this needed to be updated if you add new tags
$elArr= array();
$elArr[]=new A();
$elArr[]=new TABLE();
$elArr[]=new TR();
$elArr[]=new TD();
$elArr[]=new P();
$elArr[]=new TBODY();
return $elArr;
}
function giveMatchingSonElement($father,$remainingDepth)
{
if ($father==NULL) $father=new DIV();
if ($remainingDepth==2 && $father->type=="table") return 2;
if ($remainingDepth==1 && $father->type=="tr") return 3;
if ($father->downI=="none") return NO_POSSIBLE_SON;
global $currentSeedNum;
global $elArr;
global $debug;
$matchSon=false;
$matchFat=false;
while (!$matchSon||!$matchFat)
{
mt_srand($currentSeedNum);
$el=mt_rand(0,count($elArr)-1);
if ($debug==1) echo "possible son $el at seed $currentSeedNum <br/>";
$matchSon=false;
if ($father->downI!="")
{
foreach (explode(",",$father->downI) as $posSon)
{
if ($elArr[$el]->type==$posSon)
{
$matchSon=true;
}
if ($debug==8)
{
$tempstr=$matchSon?" true ":" false ";
echo $elArr[$el]->type." check if fits to father $father->type:father's element $posSon => $tempstr </br>";
}
}
}
else $matchSon=true;
$matchFat=false;
if ($elArr[$el]->upI!="")
{
foreach (explode(",",$elArr[$el]->upI) as $posFat)
{
if ($father->type==$posFat)
{
$matchFat=true;
}
if ($debug==8) echo "Array:".$elArr[$el]->upI." $posFat trying to take as a son".$father->type."<br/>";
}
}
else $matchFat=true;
$currentSeedNum++;
if ($elArr[$el]->dNeed>$remainingDepth)
{
$matchFat=false;
$matchSon=false;
}
}
if ($debug==8)
{
global $root;
echo "chosen :father ".$father->type.",son ".$elArr[$el]->type." <br/>";
//print_r($root);
//echo "chosen element $el<br/>";
}
return $el;
}
function generateHTMLtree($currentDepth,$maxDepth,$father)
{
global $debug;
if ($currentDepth==$maxDepth)
{
//unset($father->sons);
if ($debug==2)
{
echo "A leaf: $father->type <br/>";
if (!isset($father->sons)) echo "no sons verified<br/>";
}
return;
}
global $currentSeedNum;
global $elArr;
mt_srand(++$currentSeedNum);
$el=mt_rand(0,MAX_BARNCH); //# of possible sons,
if ($father->maxsons!=0) $el=$father->maxsons;
for ($i=0;$i<$el-1;++$i)
{
$matchingSon=giveMatchingSonElement( $father,$maxDepth-$currentDepth);
if ($debug==6) echo "trying to clone :$matchingSon";
if ($matchingSon==NO_POSSIBLE_SON) return;
$father->sons[$i]= clone $elArr[$matchingSon];
if ($debug==1) echo "generating ".$father->sons[$i]->type." at depth of $currentDepth <br/>";
$father->sons[$i]->father=&$father;
}
if ($debug==2) echo "current depth:$currentDepth target depth:$maxDepth<br/>";
for ($i=0;$i<$el-1;++$i)
{
generateHTMLtree($currentDepth+1,$maxDepth,$father->sons[$i]);
}
}
function printTree($currentItem)
{
global $debug;
$currentItem->open();
if ($debug==4) echo "open $currentItem->type <br/>";
$currentItem->printRandContent();
echo $currentItem->forceContent; //making sure we dont have empty element where needed
if (isset($currentItem->sons)) foreach ($currentItem->sons as $son) printTree($son);
//$currentItem->printRandContent();
if ($debug==4) echo "close $currentItem->type<br/>";
$currentItem->close();
}
function printRandomHTML()
{
global $root;
global $debug;
global $currentSeedNum;
$currentSeedNum=isset($_REQUEST['random'])?(double)$_REQUEST['random']:0; //incase seed number is coming as a parameter
$debug=0;
initGlobals();
generateHTMLtree(INIT_DEPTH,MAX_DEPTH,$root);
printTree($root);
//print_r($root);
}
?>