Войти

Показать полную графическую версию : PHP открытие fictionbook(fb2)


leonid.poydolov@fb
22-06-2015, 11:42
Хочу организовать в локалке чтение книг на fb2 (fictionbook2), но файлы открываются в браузере <?xml version="1.0" и т.д..
Ниже нашел скрипт который открывает файлы это формата, но в строке 157 ($book ->book_load("cuda_kniga.fb2");) надо писать название

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


<?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
нашел решение...
.htaccess

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

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

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

полный код fb2reader.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();
?>




© OSzone.net 2001-2012