<?php
/*$arrayDiv[0]='body';
$arrayDiv[1]='#container';
$arrayDiv[2]='#banner';
$arrayDiv[3]='#content';
$arrayDiv[4]='#left';
$arrayDiv[5]='#footer';
$datos[0]['div']='body';

$datos[0]['background-color']="#FEFEFE";

$datos[0]['color']='#F0F0F0';
$datos[0]['font-family']='Verdana';
$datos[0]['font-size']='11px';
$datos[1]['div']='#container';
$datos[1]['background-color']='#F4F4F4';
$datos[1]['color']='#FAFAFA';
$datos[1]['extra'][0]='background-image:lalal';
$datos[1]['extra'][1]='style:lalal';
$da[0]['div']='body';

$da[0]['background-color']="waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

*/


class FILE{
var $CSS;
var $num;
		function coincidencias($lineaX,$array,$nums){
			$x=0;
			$devolver=-1;
				while($array[$x]['div']!=''){
					if($num[$x]!=1){
	//						echo $sep[0].' '.$array[$x]['div'].'</br>';
						if($lineaX==$array[$x]['div']){
							$devolver=$x;

						}	
					}
					$x++;
				}
			return $devolver;
		}
		function makeCSS($filename,$datos){
			$indice=1;
			$file=fopen($filename,"ra");
					$numline=-1;
					$n=0;
					$m=0;
					while(!feof($file)){
						$numline++;
						$ln=fgets($file,1024);
						$x=substr($ln,0,strlen($ln)-2);
							$propiedades=explode(' ',$x);
							$lon=strlen($propiedades[0]);							
							$comprobacion=substr($propiedades[0],$lon-1,$lon);
							if($comprobacion==':'){
								$param=substr($propiedades[0],0,$lon-1);
							}else{
								$param=$propiedades[0];
							}	
//								echo $param.'</br>';
	

						if($si==1){							
							if($datos[$nDato][$param]!=''){

								$linea[$numline]=$param.': '.$datos[$nDato][$param].';';
							}else if($param=='}'){
									$n=0;
									while($datos[$nDato]['extra'][$n]!=''){
										$linea[$numline]=$datos[$nDato]['extra'][$n];
										$numline++;
										$n++;
									}
									$linea[$numline]='}';
									$si=0;
							}else{
								$linea[$numline]=$x;
							}

						}else if($this->coincidencias($param,$datos,$usado)!=-1){
							$nDato=$this->coincidencias($param,$datos,$usado);		
							$nums[$nDato]=1;										
								$si=1;
							$linea[$numline]=$x;
						}else{
							$linea[$numline]=$x;
						}
						//echo $linea[$numline];
					}
			fclose($file);
				$this->CSS=$linea;
				$this->num=$numline;
			}


			function createCSS($filename,$datos){
					$this->makeCSS($filename,$datos);
					$ahora=time();
					$random=rand(1000,9999);
					$url=date('YmdHis',$ahora);
					$newurl='samples/'.$url.$random;
					$filename=$newurl."/estilo.css";
					mkdir($newurl,0777);
					$file=fopen($filename,"wt");
					
					$pline=0;
					while($pline!=$this->num){		
						$write=$this->CSS[$pline]."\n";
						fwrite($file,$write);
						$pline++;	
					}						
					fclose($file);
					return $newurl;
			}

			function editCSS($filename,$datos){
					$this->makeCSS($filename,$datos);
					$file=fopen($filename,"wt");					
					$pline=0;
					while($pline!=$this->num){		
						$write=$this->CSS[$pline]."\n";
						fwrite($file,$write);
						$pline++;	
					}						
					fclose($file);
			}
			
			function buildHTML($filename,$result){
			
				$file=fopen($filename,"ra");
				$numline=-1;
				$total=-1;
				while(!feof($file)){
							$numline++;
							$total++;
							$ln=fgets($file,1024);
							$x=substr($ln,0,strlen($ln)-2);

							$linea[$numline]=$x;				
				}
				fclose($file);	
									
				$file=fopen($result,"wt");
					
				$lineapoyo[0]='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
				$lineapoyo[1]='<html xmlns="http://www.w3.org/1999/xhtml">';
				$lineapoyo[2]='<head>';
				$lineapoyo[3]='<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';
				$lineapoyo[4]='<title>Template</title>';
				$lineapoyo[5]='<link href="estilo.css" rel="stylesheet" type="text/css" />';
				$lineapoyo[6]='</head>';
				$lineapoyo[7]='<body>';
				$lineapoyo[8]='</body>';
				$lineapoyo[9]='</html>';					
					
				$pline=0;
				for($i=0;$i<8;$i++){
					$write=$lineapoyo[$i]."\n";
					fwrite($file,$write);
				}
				while($pline!=$total){		
						$write=$linea[$pline]."\n";
						fwrite($file,$write);
						$pline++;	
				}	
				for($i=8;$i<10;$i++){
					$write=$lineapoyo[$i]."\n";
					fwrite($file,$write);
				}										
				fclose($file);					
				
			}
			
}


?>