Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Вебмастеру (http://forum.oszone.net/forumdisplay.php?f=22)
-   -   PHP открытие fictionbook(fb2) (http://forum.oszone.net/showthread.php?t=301503)

leonid.poydolov@fb 22-06-2015 11:42 2521034

PHP открытие fictionbook(fb2)
 
Хочу организовать в локалке чтение книг на fb2 (fictionbook2), но файлы открываются в браузере
HTML код:

<?xml version="1.0"
и т.д..
Ниже нашел скрипт который открывает файлы это формата, но в строке 157 (
PHP код:

$book ->book_load("cuda_kniga.fb2"); 

) надо писать название

Как сделать что бы при обращении к любой книге fb2 открытие(подхват) проходило через этот скрипт?

PHP код:

<?php
/**
 * @author            Diz A Torr
 * @copyright        Copyright (c) 2010, CodeMotion
 * @license            GPL
 * @link            http:/conferdigit.ru
 * @since            Version 1.0
 * @filesource
 */
// ------------------------------------------------------------------------

  
class   fictionbook {
     var  
$book ;
    var 
$table_of_content = array();
    var 
$book_info = array();
    
    function 
book_load ($file){
        
$this->book simplexml_load_file ($file);
        
$this->title_info();
    }
    
    function 
title_info (){
        
$title_info 'title-info';
//        include ('ru-ru.php');
        
foreach ($this->book->description->$title_info->children() as $key=>$children){
            switch (
$key){
                case 
'genre':
                    
$this->book_info['genre'][] = $genre_table_ru[(string) $children]; break;
                case 
'book-title':
                    
$this->book_info['book-title'] = $children; break;
                case 
'author'//Автор(ы) произведения
                    
$last_name 'last-name';
                    
$first_name 'first-name';
                    
$middle_name 'middle-name';
                    
$home_page 'home-page';
                    
$this->book_info['author'][] = array (   'first-name'=>$children->$first_name
                                                            
'middle-name'=>$children->$middle_name
                                                            
'last-name'=>$children->$last_name
                                                            
'home-page'=>$children->$home_page
                                                            
'email'=>$children->email);
                break;
                case 
'annotation':
                    
$this->book_info['annotation'] = $children->asXML(); break;
                case 
'coverpage':
                    
$this->book_info['coverpage'] = $children->image; break;
                case 
'date'// хранит дату создания документа.
                    
$this->book_info['date'] = $children; break;
                case 
'translator'//Переводчик(и)
                    
$last_name 'last-name';
                    
$first_name 'first-name';
                    
$middle_name 'middle-name';
                    
$home_page 'home-page';
                    
$this->book_info['translator'][] = array (   'first-name'=>$children->$first_name
                                                            
'middle-name'=>$children->$middle_name
                                                            
'last-name'=>$children->$last_name
                                                            
'home-page'=>$children->$home_page
                                                            
'email'=>$children->email);
                break;
                case 
'lang'//Язык книги
                    
$this->book_info['lang'] = $children; break;
                case 
'year'// год издания книги.
                    
$this->book_info['year'] = $children; break;                    
                
            }
        }
        
$this->table_of_content $this->table_of_content();
    }
    
    function 
print_image ($image){
        
//print_r($image);
        
$image =  $image->asXML();
        
preg_match("/[\"|\']\#([\S]+\.[\S]+)[\"|\']/i"$image$image_name);
        
$image_name $image_name[1];
           foreach (
$this->book->binary as $binary){
               if (
$binary[id] == $image_name){
                   
$image base64_decode($binary);
                   
$file fopen($image_name'w');
                   
fwrite($file$image);
                   
fclose($file);
                   echo 
'';
               }
        }        
    }
    
    function 
table_of_content($book='false'){ //Надо было как-то отметить, что в начале читать с самого начала , при этом не задавая это явно в методе.
        
if (!empty($this->table_of_content)){return $this->table_of_content;}         //Проверка на наличие
        
if ($book == 'false'){$book $this->book->body;}
        foreach (
$book as $key=>$body){
            if (isset(
$body->title)){
                
$title $this->__clear_string($body->title->asXML());
                if (!empty(
$title)){
                    
//echo $title.' ';
                    
$title_array[] = $title;                    
                }
            }
            
$title_array_temp $this->table_of_content($body->section);
            if (!empty(
$title_array_temp)){$title_array[] = $title_array_temp;}
        }
        return 
$title_array;
    }

    function 
content($book='false',  $i=-1){
        if (
$book == 'false'){$book $this->book->body;}
        ++
$i;
        foreach (
$book->children() as $key=>$body){
            switch (
$key){
                case 
'image'
                    
$this->print_image ($body);
                break;
                case 
'title':
                    if (
$i == 0){$i=1;}
                    
$title $this->__clear_string($body->asXML());
                    echo 
''.$title.'';
                break;
                case 
'section'
                    
$this->content($body$i);
                break;
                case 
'epigraph':
                    echo 
'
'
.$this->__clear_string($body->asXML()).'
'
;
                break;
                default:
                echo 
$this->__clear_string($body->asXML(), false);
            }
        }
    }
    
    function 
print_table_of_content($table false){
        if (!
$table){$table $this->table_of_content;
        }
        echo 
'
'
;
        foreach (
$table as $row){
            if (
is_array($row)){
                
$this->print_table_of_content($row);
            }else{
                echo 
'
'
.$row.'
'
;
            }
        }
        echo 
'
'
;
    }
        
    
private function __clear_string ($string$p true){
        
$string preg_replace('/\|\<\/title\>/im'''$string); //|\|\<\/p\>
        
$string preg_replace('/\s+/m'" "$string);
        
$string preg_replace('/^\s+|\s+$/'''$string);
        if (
$p){$string preg_replace('/\|\<\/p\>/im'''$string);}
        return 
$string;
    }

}
$book  = new  fictionbook ;
$book ->book_load("cuda_kniga.fb2");
//echo '';
$book->print_table_of_content();
$book->content();
?>


leonid.poydolov@fb 22-06-2015 12:15 2521043

нашел решение...
.htaccess
HTML код:

RewriteEngine On
RewriteRule ^(.*).fb2$ fb2reader.php?reader=$1 [L]

Создать наш файл под именем fb2reader.php и заменить строку 157

PHP код:

$book ->book_load($_GET['reader'].".fb2"); 

полный код fb2reader.php

PHP код:

<?php
/**
 * @author            Diz A Torr
 * @copyright        Copyright (c) 2010, CodeMotion
 * @license            GPL
 * @link            http:/conferdigit.ru
 * @since            Version 1.0
 * @filesource
 */
// ------------------------------------------------------------------------

  
class   fictionbook {
     var  
$book ;
    var 
$table_of_content = array();
    var 
$book_info = array();
    
    function 
book_load ($file){
        
$this->book simplexml_load_file ($file);
        
$this->title_info();
    }
    
    function 
title_info (){
        
$title_info 'title-info';
//        include ('ru-ru.php');
        
foreach ($this->book->description->$title_info->children() as $key=>$children){
            switch (
$key){
                case 
'genre':
                    
$this->book_info['genre'][] = $genre_table_ru[(string) $children]; break;
                case 
'book-title':
                    
$this->book_info['book-title'] = $children; break;
                case 
'author'//Автор(ы) произведения
                    
$last_name 'last-name';
                    
$first_name 'first-name';
                    
$middle_name 'middle-name';
                    
$home_page 'home-page';
                    
$this->book_info['author'][] = array (   'first-name'=>$children->$first_name
                                                            
'middle-name'=>$children->$middle_name
                                                            
'last-name'=>$children->$last_name
                                                            
'home-page'=>$children->$home_page
                                                            
'email'=>$children->email);
                break;
                case 
'annotation':
                    
$this->book_info['annotation'] = $children->asXML(); break;
                case 
'coverpage':
                    
$this->book_info['coverpage'] = $children->image; break;
                case 
'date'// хранит дату создания документа.
                    
$this->book_info['date'] = $children; break;
                case 
'translator'//Переводчик(и)
                    
$last_name 'last-name';
                    
$first_name 'first-name';
                    
$middle_name 'middle-name';
                    
$home_page 'home-page';
                    
$this->book_info['translator'][] = array (   'first-name'=>$children->$first_name
                                                            
'middle-name'=>$children->$middle_name
                                                            
'last-name'=>$children->$last_name
                                                            
'home-page'=>$children->$home_page
                                                            
'email'=>$children->email);
                break;
                case 
'lang'//Язык книги
                    
$this->book_info['lang'] = $children; break;
                case 
'year'// год издания книги.
                    
$this->book_info['year'] = $children; break;                    
                
            }
        }
        
$this->table_of_content $this->table_of_content();
    }
    
    function 
print_image ($image){
        
//print_r($image);
        
$image =  $image->asXML();
        
preg_match("/[\"|\']\#([\S]+\.[\S]+)[\"|\']/i"$image$image_name);
        
$image_name $image_name[1];
           foreach (
$this->book->binary as $binary){
               if (
$binary[id] == $image_name){
                   
$image base64_decode($binary);
                   
$file fopen($image_name'w');
                   
fwrite($file$image);
                   
fclose($file);
                   echo 
'';
               }
        }        
    }
    
    function 
table_of_content($book='false'){ //Надо было как-то отметить, что в начале читать с самого начала , при этом не задавая это явно в методе.
        
if (!empty($this->table_of_content)){return $this->table_of_content;}         //Проверка на наличие
        
if ($book == 'false'){$book $this->book->body;}
        foreach (
$book as $key=>$body){
            if (isset(
$body->title)){
                
$title $this->__clear_string($body->title->asXML());
                if (!empty(
$title)){
                    
//echo $title.' ';
                    
$title_array[] = $title;                    
                }
            }
            
$title_array_temp $this->table_of_content($body->section);
            if (!empty(
$title_array_temp)){$title_array[] = $title_array_temp;}
        }
        return 
$title_array;
    }

    function 
content($book='false',  $i=-1){
        if (
$book == 'false'){$book $this->book->body;}
        ++
$i;
        foreach (
$book->children() as $key=>$body){
            switch (
$key){
                case 
'image'
                    
$this->print_image ($body);
                break;
                case 
'title':
                    if (
$i == 0){$i=1;}
                    
$title $this->__clear_string($body->asXML());
                    echo 
''.$title.'';
                break;
                case 
'section'
                    
$this->content($body$i);
                break;
                case 
'epigraph':
                    echo 
'
'
.$this->__clear_string($body->asXML()).'
'
;
                break;
                default:
                echo 
$this->__clear_string($body->asXML(), false);
            }
        }
    }
    
    function 
print_table_of_content($table false){
        if (!
$table){$table $this->table_of_content;
        }
        echo 
'
'
;
        foreach (
$table as $row){
            if (
is_array($row)){
                
$this->print_table_of_content($row);
            }else{
                echo 
'
'
.$row.'
'
;
            }
        }
        echo 
'
'
;
    }
        
    
private function __clear_string ($string$p true){
        
$string preg_replace('/\|\<\/title\>/im'''$string); //|\|\<\/p\>
        
$string preg_replace('/\s+/m'" "$string);
        
$string preg_replace('/^\s+|\s+$/'''$string);
        if (
$p){$string preg_replace('/\|\<\/p\>/im'''$string);}
        return 
$string;
    }

}
$book  = new  fictionbook ;
$book ->book_load($_GET['reader'].".fb2");
//echo '';
$book->print_table_of_content();
$book->content();
?>



Время: 23:12.

Время: 23:12.
© OSzone.net 2001-