Viewing file: fb2.classtxapuza.php (8.03 KB) -rw-rw-r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
class fb2
{
public $file;
public $titulo;
public $nombre;
public $apellido;
public $programa;
public $document_info;
public $document_info_array=array();
public $autor_documento;
public $portada;
public $xmlizar;
var $handle_open;
var $xml;
var $array_file=array();
public $debug=FALSE;
function __construct($fichero)
{
$this->file=$fichero;
$this->array_file=file($this->file);
if (count($this->array_file)==1){
echo "<b>AVISO: fichero sin saltos de l�nea...corrigiendo</b>";
$variable=$this->array_file[0];
echo $variable;
// sustotuimos los "<" por "chr(10).<";
$variable=str_replace("<",chr(10)."<",$variable);
$this->array_file=split(chr(10),$variable);
unset($variable);
}
/*
$handle=fopen ($this->file,"r");
if ($handle) {
while (!feof($handle)) {
$this->array_file[] = fgets($handle, 4096);
}
fclose($handle);
}
*/
// $this->xmlizar= new xmlizar($this->xml);
}
function get_titulo()
{
$inicio_titulo=array_find("<book-title>",$this->array_file);
$fin_titulo=array_find("</book-title>",$this->array_file);
$cadena="";
for ($f=$inicio_titulo;$f<=$fin_titulo;$f++){
$cadena.=$this->array_file[$f];
if ($this->debug){
echo "gettitulo-_$f_>{$this->array_file[$f]}<br>";
}
}
$this->titulo=hasta($cadena,"<book-title>","</book-title>");
}
function get_autor()
{
$inicio_nombre=array_find("<first-name>",$this->array_file);
$fin_nombre=array_find("</first-name>",$this->array_file);
$cadena="";
for ($f=$inicio_nombre;$f<=$fin_nombre;$f++){
@ $cadena.=$this->array_file[$f];
if ($this->debug){
echo "getautor/nombre-_$f_>{$this->array_file[$f]}<br>";
}
}
$this->nombre=hasta($cadena,"<first-name>","</first-name>");
$inicio_apellido=array_find("<last-name>",$this->array_file);
$fin_apellido=array_find("</last-name>",$this->array_file);
$cadena="";
for ($f=$inicio_apellido;$f<=$fin_apellido;$f++){
@$cadena.=$this->array_file[$f];
if ($this->debug){
echo "getautor/apellido-_$f_>{$this->array_file[$f]}<br>";
}
}
$this->apellido=hasta($cadena,"<last-name>","</last-name>");
}
function get_programa()
{
$inicio_programa=array_find("<program-used>",$this->array_file);
$fin_programa=array_find("</program-used>",$this->array_file);
$cadena="";
for ($f=$inicio_programa;$f<=$fin_programa;$f++){
if ($this->debug){
echo "programa-_$f_>{$this->array_file[$f]}<br>";
}
@$cadena.=$this->array_file[$f];
}
$this->programa=hasta($cadena,"<program-used>","</program-used>");
}
function get_document_info()
{
$inicio_document=array_find("<document-info>",$this->array_file);
$fin_document=array_find("</document-info>",$this->array_file);
$cadena="";
$this->document_info_array=array();
for ($f=$inicio_document;$f<=$fin_document;$f++){
if ($this->debug){
echo "getdocumentinfo-_$f_>{$this->array_file[$f]}<br>";
}
@$cadena.=$this->array_file[$f];
$this->document_info_array[$f]=$this->array_file[$f];
}
$this->document_info=hasta($cadena,"<document-info>","</document-info>");
}
function set_titulo($titulo)
{
$this->titulo=$titulo;
$inicio_titulo=array_find("<book-title>",$this->array_file);
$fin_titulo=array_find("</book-title>",$this->array_file);
// ya tenemos el inicio y el fin. unseteamos los valores desde inicio hasta fin ambos inclusive.
for ($f=$inicio_titulo;$f<=$fin_titulo;$f++){
if ($this->debug){
echo "set_titulo..BORRANDO-_$f_>{$this->array_file[$f]}<br>";
}
unset ($this->array_file[$f]);
}
//e insertamos de nuevo, ya corregido
$this->array_file[$inicio_titulo]="<book-title>".utf8_encode($this->titulo)."</book-title>".chr(13).chr(10);
}
function set_autor_documento($autor)
{
$this->autor_documento=$autor;
$inicio_documento=array_find("<author>",$this->document_info_array);
$fin_documento=array_find("</author>",$this->document_info_array);
// ya tenemos el inicio y el fin. unseteamos los valores desde inicio hasta fin ambos inclusive.
for ($f=$inicio_documento;$f<=$fin_documento;$f++){
if ($this->debug){
echo "setautor_documento..borrando-_$f_>{$this->array_file[$f]}<br>";
}
unset ($this->array_file[$f]);
}
//e insertamos de nuevo, ya corregido
$this->array_file[$inicio_documento]="<author>".$this->autor_documento."</author>".chr(13).chr(10);
}
function set_autor($nombre,$apellido)
{
// nombre
$this->nombre=$nombre;
$inicio_nombre=array_find("<first-name>",$this->array_file);
$fin_nombre=array_find("</first-name>",$this->array_file);
// ya tenemos el inicio y el fin. unseteamos los valores desde inicio hasta fin ambos inclusive.
for ($f=$inicio_nombre;$f<=$fin_nombre;$f++){
if ($this->debug){
echo "setautorNOMBRE...BORRANDO-_$f_>{$this->array_file[$f]}<br>";
}
unset ($this->array_file[$f]);
}
//e insertamos de nuevo, ya corregido
$this->array_file[$inicio_nombre]="<first-name>".utf8_encode($this->nombre)."</first-name>".chr(13).chr(10);
// apellido
$this->apellido=$apellido;
$inicio_apellido=array_find("<last-name>",$this->array_file);
$fin_apellido=array_find("</last-name>",$this->array_file);
// ya tenemos el inicio y el fin. unseteamos los valores desde inicio hasta fin ambos inclusive.
// sino hay apellido, lo ponemos a continuacion del nombre, y borramos la etiqueta <last-name /> si la hubiera.
if(!is_numeric($inicio_apellido)){
echo "<br><b>Nota: sin etiqueta de apellido... se inserta.<br></b>";
if ($etiqueta=array_find("<last-name />",$this->array_file)){
if ($this->debug){
echo "setautorAPELLIDO1....BORRANDO-_$f_>{$this->array_file[$f]}<br>";
}
unset ($this->array_file[$etiqueta]);
}
$this->array_file[$inicio_nombre+1]="<last-name>".utf8_encode($this->apellido)."</last-name>".chr(13).chr(10);
}
for ($f=$inicio_apellido;$f<=$fin_apellido;$f++){
if ($this->debug){
echo "setautorAPELLIDO2....BORRANDO-_$f_>{$this->array_file[$f]}<br>";
}
unset ($this->array_file[$f]);
}
//e insertamos de nuevo, ya corregido
$this->array_file[$inicio_apellido]="<last-name>".utf8_encode($this->apellido)."</last-name>".chr(13).chr(10);
// si hay middle-name, nos lo cargamos
$inicio_middle=array_find("</middle-name>",$this->array_file);
$fin_middle=array_find("</middle-name>",$this->array_file);
for ($f=$inicio_middle;$f<=$fin_middle;$f++){
if ($this->debug){
echo "setautorMIDDLE....BORRANDO-_$f_>{$this->array_file[$f]}<br>";
}
unset ($this->array_file[$f]);
}
}
function graba($nombre)
{
if ($handle=fopen($nombre,"w+")){;
for ($f=0;$f<=count($this->array_file)+2;$f++){
@ fwrite($handle,$this->array_file[$f]);
}
fclose($handle);
}else{
echo "<br><b>error reescribiendo el fichero</b><br>";
}
}
}
function hasta($ccadena,$vdesde,$vhasta)
{
if (!$ccadena=strstr($ccadena,$vdesde)){
return(null);
}
## ahora, contamos hasta desde $desde
## hasta el caracter $vhasta
$ccadena=substr($ccadena,strlen($vdesde));
$c="";
$a=0;
$b="";
while(!strstr($c,$vhasta)) {
$b=substr($ccadena,$a,1);
$c=$c.$b;
$a++;
}
return(substr($c,0,(strlen($c)-(strlen($vhasta)))));
}
function array_find($needle, $array)
{
foreach ($array as $key=>$item)
{
if (strpos($item, $needle) !== FALSE)
{
return $key;
break;
}
}
}
?>
|