seblucas / cops

Calibre OPDS (and HTML) PHP Server : web-based light alternative to Calibre content server / Calibre2OPDS to serve ebooks (epub, mobi, pdf, ...)
http://blog.slucas.fr/en/oss/calibre-opds-php-server
GNU General Public License v2.0
1.43k stars 229 forks source link

Epubreader throws error if some of the titles in TOC has new line #365

Closed thorindwarf closed 6 years ago

thorindwarf commented 7 years ago

Hi, In epubreader.php there is a line which is constructing a JS array with TOC titles and their sources. When some of the titles in the book has new lines e.g with 1984:

<navPoint id="num_4" playOrder="4">
      <navLabel>
        <text>1984
</text>
      </navLabel>

the following is thrown in console and book is not loaded.

10:16:50.053 epubreader.php?data=3387&db=:18 Uncaught SyntaxError: Invalid or unexpected token
10:16:50.113 cops-monocle.js?v=1.1.1:18 Uncaught ReferenceError: bookData is not defined
    at cops-monocle.js?v=1.1.1:18
(anonymous) @ cops-monocle.js?v=1.1.1:18

I fixed it in my installation with a simple preg_replace('~[\r\n]+~', '', $content['title']) but I'm not sure if this is the correct place for the fix.

Thanks! Regards, Nikolay

seblucas commented 7 years ago

Sorry for the delay and thanks for the issue. Can you please tell me where you put it ?

Directly in second line of private function getNavPointDetail($node) ?

thorindwarf commented 7 years ago

I put it in epubreader.php on the line <?php echo 'return [' . implode(', ', array_map(function($content) { return "{title: '" . addslashes(preg_replace('~[\r\n]+~', '', $content['title'])) . "', src: '". $content['src'] . "'}"; }, $book->contents())) . '];'; ?> but the place you are asking for is better I think. I simply couldn't find it to make the change in the function :) Not very familiar with OO php :)

seblucas commented 7 years ago

Can you confirm that putting it in EPub.php at the place I mentionned (and replacing $content['title'] by $title) works ?

If you can't can you provide an epub to test ? you can send me an email if the epub is not public domain.

thorindwarf commented 7 years ago

I just tested it. It's working :)

private function getNavPointDetail($node)
{
    $title = $this->toc_xpath->query('x:navLabel/x:text', $node)->item(0)->nodeValue;
    $src = $this->toc_xpath->query('x:content', $node)->item(0)->attr('src');
    $src = $this->encodeComponentName ($src);
    return array('title' => preg_replace('~[\r\n]+~', '', $title), 'src' => $src);
}
seblucas commented 7 years ago

I've not forgotten about this modification, I'll work on COPS this weekend and integrate the fix properly