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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1158 lines
40KB

  1. <?php
  2. /**
  3. * COPS (Calibre OPDS PHP Server) class file
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Sébastien Lucas <sebastien@slucas.fr>
  7. */
  8. define ("VERSION", "0.9.1beta");
  9. define ("DB", "db");
  10. date_default_timezone_set($config['default_timezone']);
  11. function useServerSideRendering () {
  12. global $config;
  13. return preg_match("/" . $config['cops_server_side_render'] . "/", $_SERVER['HTTP_USER_AGENT']);
  14. }
  15. function serverSideRender ($data) {
  16. // Get the templates
  17. $header = file_get_contents('templates/default/header.html');
  18. $footer = file_get_contents('templates/default/footer.html');
  19. $main = file_get_contents('templates/default/main.html');
  20. $bookdetail = file_get_contents('templates/default/bookdetail.html');
  21. $page = file_get_contents('templates/default/page.html');
  22. // Generate the function for the template
  23. $template = new doT ();
  24. $dot = $template->template ($page, array ("bookdetail" => $bookdetail,
  25. "header" => $header,
  26. "footer" => $footer,
  27. "main" => $main));
  28. // Execute the template
  29. if (!empty ($data)) {
  30. return $dot ($data);
  31. }
  32. return NULL;
  33. }
  34. function getQueryString () {
  35. if ( isset($_SERVER['QUERY_STRING']) ) {
  36. return $_SERVER['QUERY_STRING'];
  37. }
  38. return "";
  39. }
  40. function getURLParam ($name, $default = NULL) {
  41. if (!empty ($_GET) && isset($_GET[$name]) && $_GET[$name] != "") {
  42. return $_GET[$name];
  43. }
  44. return $default;
  45. }
  46. function getCurrentOption ($option) {
  47. global $config;
  48. if (isset($_COOKIE[$option])) {
  49. if (isset($config ["cops_" . $option]) && is_array ($config ["cops_" . $option])) {
  50. return explode (",", $_COOKIE[$option]);
  51. } else {
  52. return $_COOKIE[$option];
  53. }
  54. }
  55. if ($option == "style") {
  56. return "default";
  57. }
  58. if (isset($config ["cops_" . $option])) {
  59. return $config ["cops_" . $option];
  60. }
  61. return "";
  62. }
  63. function getCurrentCss () {
  64. return "styles/style-" . getCurrentOption ("style") . ".css";
  65. }
  66. function getUrlWithVersion ($url) {
  67. return $url . "?v=" . VERSION;
  68. }
  69. function xml2xhtml($xml) {
  70. return preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', create_function('$m', '
  71. $xhtml_tags = array("br", "hr", "input", "frame", "img", "area", "link", "col", "base", "basefont", "param");
  72. return in_array($m[1], $xhtml_tags) ? "<$m[1]$m[2] />" : "<$m[1]$m[2]></$m[1]>";
  73. '), $xml);
  74. }
  75. function display_xml_error($error)
  76. {
  77. $return .= str_repeat('-', $error->column) . "^\n";
  78. switch ($error->level) {
  79. case LIBXML_ERR_WARNING:
  80. $return .= "Warning $error->code: ";
  81. break;
  82. case LIBXML_ERR_ERROR:
  83. $return .= "Error $error->code: ";
  84. break;
  85. case LIBXML_ERR_FATAL:
  86. $return .= "Fatal Error $error->code: ";
  87. break;
  88. }
  89. $return .= trim($error->message) .
  90. "\n Line: $error->line" .
  91. "\n Column: $error->column";
  92. if ($error->file) {
  93. $return .= "\n File: $error->file";
  94. }
  95. return "$return\n\n--------------------------------------------\n\n";
  96. }
  97. function are_libxml_errors_ok ()
  98. {
  99. $errors = libxml_get_errors();
  100. foreach ($errors as $error) {
  101. if ($error->code == 801) return false;
  102. }
  103. return true;
  104. }
  105. function html2xhtml ($html) {
  106. $doc = new DOMDocument();
  107. libxml_use_internal_errors(true);
  108. $doc->loadHTML('<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' .
  109. $html . '</body></html>'); // Load the HTML
  110. $output = $doc->saveXML($doc->documentElement); // Transform to an Ansi xml stream
  111. $output = xml2xhtml($output);
  112. if (preg_match ('#<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></meta></head><body>(.*)</body></html>#ms', $output, $matches)) {
  113. $output = $matches [1]; // Remove <html><body>
  114. }
  115. /*
  116. // In case of error with summary, use it to debug
  117. $errors = libxml_get_errors();
  118. foreach ($errors as $error) {
  119. $output .= display_xml_error($error);
  120. }
  121. */
  122. if (!are_libxml_errors_ok ()) $output = "HTML code not valid.";
  123. libxml_use_internal_errors(false);
  124. return $output;
  125. }
  126. /**
  127. * This method is a direct copy-paste from
  128. * http://tmont.com/blargh/2010/1/string-format-in-php
  129. */
  130. function str_format($format) {
  131. $args = func_get_args();
  132. $format = array_shift($args);
  133. preg_match_all('/(?=\{)\{(\d+)\}(?!\})/', $format, $matches, PREG_OFFSET_CAPTURE);
  134. $offset = 0;
  135. foreach ($matches[1] as $data) {
  136. $i = $data[0];
  137. $format = substr_replace($format, @$args[$i], $offset + $data[1] - 1, 2 + strlen($i));
  138. $offset += strlen(@$args[$i]) - 2 - strlen($i);
  139. }
  140. return $format;
  141. }
  142. /**
  143. * This method is based on this page
  144. * http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/
  145. */
  146. function localize($phrase, $count=-1, $reset=false) {
  147. if ($count == 0)
  148. $phrase .= ".none";
  149. if ($count == 1)
  150. $phrase .= ".one";
  151. if ($count > 1)
  152. $phrase .= ".many";
  153. /* Static keyword is used to ensure the file is loaded only once */
  154. static $translations = NULL;
  155. if ($reset) {
  156. $translations = NULL;
  157. }
  158. /* If no instance of $translations has occured load the language file */
  159. if (is_null($translations)) {
  160. $lang = "en";
  161. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  162. {
  163. $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  164. }
  165. $lang_file_en = NULL;
  166. $lang_file = dirname(__FILE__). '/lang/Localization_' . $lang . '.json';
  167. if (!file_exists($lang_file)) {
  168. $lang_file = dirname(__FILE__). '/lang/' . 'Localization_en.json';
  169. }
  170. elseif ($lang != "en") {
  171. $lang_file_en = dirname(__FILE__). '/lang/' . 'Localization_en.json';
  172. }
  173. $lang_file_content = file_get_contents($lang_file);
  174. /* Load the language file as a JSON object and transform it into an associative array */
  175. $translations = json_decode($lang_file_content, true);
  176. /* Clean the array of all unfinished translations */
  177. foreach (array_keys ($translations) as $key) {
  178. if (preg_match ("/^##TODO##/", $key)) {
  179. unset ($translations [$key]);
  180. }
  181. }
  182. if ($lang_file_en)
  183. {
  184. $lang_file_content = file_get_contents($lang_file_en);
  185. $translations_en = json_decode($lang_file_content, true);
  186. $translations = array_merge ($translations_en, $translations);
  187. }
  188. }
  189. if (array_key_exists ($phrase, $translations)) {
  190. return $translations[$phrase];
  191. }
  192. return $phrase;
  193. }
  194. function addURLParameter($urlParams, $paramName, $paramValue) {
  195. if (empty ($urlParams)) {
  196. $urlParams = "";
  197. }
  198. $start = "";
  199. if (preg_match ("#^\?(.*)#", $urlParams, $matches)) {
  200. $start = "?";
  201. $urlParams = $matches[1];
  202. }
  203. $params = array();
  204. parse_str($urlParams, $params);
  205. if (empty ($paramValue) && $paramValue != 0) {
  206. unset ($params[$paramName]);
  207. } else {
  208. $params[$paramName] = $paramValue;
  209. }
  210. return $start . http_build_query($params);
  211. }
  212. class Link
  213. {
  214. const OPDS_THUMBNAIL_TYPE = "http://opds-spec.org/image/thumbnail";
  215. const OPDS_IMAGE_TYPE = "http://opds-spec.org/image";
  216. const OPDS_ACQUISITION_TYPE = "http://opds-spec.org/acquisition";
  217. const OPDS_NAVIGATION_TYPE = "application/atom+xml;profile=opds-catalog;kind=navigation";
  218. const OPDS_PAGING_TYPE = "application/atom+xml;profile=opds-catalog;kind=acquisition";
  219. public $href;
  220. public $type;
  221. public $rel;
  222. public $title;
  223. public $facetGroup;
  224. public $activeFacet;
  225. public function __construct($phref, $ptype, $prel = NULL, $ptitle = NULL, $pfacetGroup = NULL, $pactiveFacet = FALSE) {
  226. $this->href = $phref;
  227. $this->type = $ptype;
  228. $this->rel = $prel;
  229. $this->title = $ptitle;
  230. $this->facetGroup = $pfacetGroup;
  231. $this->activeFacet = $pactiveFacet;
  232. }
  233. public function hrefXhtml () {
  234. return $this->href;
  235. }
  236. }
  237. class LinkNavigation extends Link
  238. {
  239. public function __construct($phref, $prel = NULL, $ptitle = NULL) {
  240. parent::__construct ($phref, Link::OPDS_NAVIGATION_TYPE, $prel, $ptitle);
  241. if (!is_null (GetUrlParam (DB))) $this->href = addURLParameter ($this->href, DB, GetUrlParam (DB));
  242. if (!preg_match ("#^\?(.*)#", $this->href) && !empty ($this->href)) $this->href = "?" . $this->href;
  243. if (preg_match ("/(bookdetail|getJSON).php/", $_SERVER["SCRIPT_NAME"])) {
  244. $this->href = "index.php" . $this->href;
  245. } else {
  246. $this->href = $_SERVER["SCRIPT_NAME"] . $this->href;
  247. }
  248. }
  249. }
  250. class LinkFacet extends Link
  251. {
  252. public function __construct($phref, $ptitle = NULL, $pfacetGroup = NULL, $pactiveFacet = FALSE) {
  253. parent::__construct ($phref, Link::OPDS_PAGING_TYPE, "http://opds-spec.org/facet", $ptitle, $pfacetGroup, $pactiveFacet);
  254. if (!is_null (GetUrlParam (DB))) $this->href = addURLParameter ($this->href, DB, GetUrlParam (DB));
  255. $this->href = $_SERVER["SCRIPT_NAME"] . $this->href;
  256. }
  257. }
  258. class Entry
  259. {
  260. public $title;
  261. public $id;
  262. public $content;
  263. public $contentType;
  264. public $linkArray;
  265. public $localUpdated;
  266. public $className;
  267. private static $updated = NULL;
  268. public static $icons = array(
  269. Author::ALL_AUTHORS_ID => 'images/author.png',
  270. Serie::ALL_SERIES_ID => 'images/serie.png',
  271. Book::ALL_RECENT_BOOKS_ID => 'images/recent.png',
  272. Tag::ALL_TAGS_ID => 'images/tag.png',
  273. Language::ALL_LANGUAGES_ID => 'images/language.png',
  274. CustomColumn::ALL_CUSTOMS_ID => 'images/tag.png',
  275. "cops:books$" => 'images/allbook.png',
  276. "cops:books:letter" => 'images/allbook.png',
  277. Publisher::ALL_PUBLISHERS_ID => 'images/publisher.png'
  278. );
  279. public function getUpdatedTime () {
  280. if (!is_null ($this->localUpdated)) {
  281. return date (DATE_ATOM, $this->localUpdated);
  282. }
  283. if (is_null (self::$updated)) {
  284. self::$updated = time();
  285. }
  286. return date (DATE_ATOM, self::$updated);
  287. }
  288. public function getNavLink () {
  289. foreach ($this->linkArray as $link) {
  290. if ($link->type != Link::OPDS_NAVIGATION_TYPE) { continue; }
  291. return $link->hrefXhtml ();
  292. }
  293. return "#";
  294. }
  295. public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pclass = "") {
  296. global $config;
  297. $this->title = $ptitle;
  298. $this->id = $pid;
  299. $this->content = $pcontent;
  300. $this->contentType = $pcontentType;
  301. $this->linkArray = $plinkArray;
  302. $this->className = $pclass;
  303. if ($config['cops_show_icons'] == 1)
  304. {
  305. foreach (self::$icons as $reg => $image)
  306. {
  307. if (preg_match ("/" . $reg . "/", $pid)) {
  308. array_push ($this->linkArray, new Link (getUrlWithVersion ($image), "image/png", Link::OPDS_THUMBNAIL_TYPE));
  309. break;
  310. }
  311. }
  312. }
  313. if (!is_null (GetUrlParam (DB))) $this->id = str_replace ("cops:", "cops:" . GetUrlParam (DB) . ":", $this->id);
  314. }
  315. }
  316. class EntryBook extends Entry
  317. {
  318. public $book;
  319. public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pbook) {
  320. parent::__construct ($ptitle, $pid, $pcontent, $pcontentType, $plinkArray);
  321. $this->book = $pbook;
  322. $this->localUpdated = $pbook->timestamp;
  323. }
  324. public function getCoverThumbnail () {
  325. foreach ($this->linkArray as $link) {
  326. if ($link->rel == Link::OPDS_THUMBNAIL_TYPE)
  327. return $link->hrefXhtml ();
  328. }
  329. return null;
  330. }
  331. public function getCover () {
  332. foreach ($this->linkArray as $link) {
  333. if ($link->rel == Link::OPDS_IMAGE_TYPE)
  334. return $link->hrefXhtml ();
  335. }
  336. return null;
  337. }
  338. }
  339. class Page
  340. {
  341. public $title;
  342. public $subtitle = "";
  343. public $authorName = "";
  344. public $authorUri = "";
  345. public $authorEmail = "";
  346. public $idPage;
  347. public $idGet;
  348. public $query;
  349. public $favicon;
  350. public $n;
  351. public $book;
  352. public $totalNumber = -1;
  353. public $entryArray = array();
  354. public static function getPage ($pageId, $id, $query, $n)
  355. {
  356. switch ($pageId) {
  357. case Base::PAGE_ALL_AUTHORS :
  358. return new PageAllAuthors ($id, $query, $n);
  359. case Base::PAGE_AUTHORS_FIRST_LETTER :
  360. return new PageAllAuthorsLetter ($id, $query, $n);
  361. case Base::PAGE_AUTHOR_DETAIL :
  362. return new PageAuthorDetail ($id, $query, $n);
  363. case Base::PAGE_ALL_TAGS :
  364. return new PageAllTags ($id, $query, $n);
  365. case Base::PAGE_TAG_DETAIL :
  366. return new PageTagDetail ($id, $query, $n);
  367. case Base::PAGE_ALL_LANGUAGES :
  368. return new PageAllLanguages ($id, $query, $n);
  369. case Base::PAGE_LANGUAGE_DETAIL :
  370. return new PageLanguageDetail ($id, $query, $n);
  371. case Base::PAGE_ALL_CUSTOMS :
  372. return new PageAllCustoms ($id, $query, $n);
  373. case Base::PAGE_CUSTOM_DETAIL :
  374. return new PageCustomDetail ($id, $query, $n);
  375. case Base::PAGE_ALL_SERIES :
  376. return new PageAllSeries ($id, $query, $n);
  377. case Base::PAGE_ALL_BOOKS :
  378. return new PageAllBooks ($id, $query, $n);
  379. case Base::PAGE_ALL_BOOKS_LETTER:
  380. return new PageAllBooksLetter ($id, $query, $n);
  381. case Base::PAGE_ALL_RECENT_BOOKS :
  382. return new PageRecentBooks ($id, $query, $n);
  383. case Base::PAGE_SERIE_DETAIL :
  384. return new PageSerieDetail ($id, $query, $n);
  385. case Base::PAGE_OPENSEARCH_QUERY :
  386. return new PageQueryResult ($id, $query, $n);
  387. case Base::PAGE_BOOK_DETAIL :
  388. return new PageBookDetail ($id, $query, $n);
  389. case Base::PAGE_ALL_PUBLISHERS:
  390. return new PageAllPublishers ($id, $query, $n);
  391. case Base::PAGE_PUBLISHER_DETAIL :
  392. return new PagePublisherDetail ($id, $query, $n);
  393. case Base::PAGE_ABOUT :
  394. return new PageAbout ($id, $query, $n);
  395. case Base::PAGE_CUSTOMIZE :
  396. return new PageCustomize ($id, $query, $n);
  397. default:
  398. $page = new Page ($id, $query, $n);
  399. $page->idPage = "cops:catalog";
  400. return $page;
  401. }
  402. }
  403. public function __construct($pid, $pquery, $pn) {
  404. global $config;
  405. $this->idGet = $pid;
  406. $this->query = $pquery;
  407. $this->n = $pn;
  408. $this->favicon = $config['cops_icon'];
  409. $this->authorName = empty($config['cops_author_name']) ? utf8_encode('Sébastien Lucas') : $config['cops_author_name'];
  410. $this->authorUri = empty($config['cops_author_uri']) ? 'http://blog.slucas.fr' : $config['cops_author_uri'];
  411. $this->authorEmail = empty($config['cops_author_email']) ? 'sebastien@slucas.fr' : $config['cops_author_email'];
  412. }
  413. public function InitializeContent ()
  414. {
  415. global $config;
  416. $this->title = $config['cops_title_default'];
  417. $this->subtitle = $config['cops_subtitle_default'];
  418. if (Base::noDatabaseSelected ()) {
  419. $i = 0;
  420. foreach (Base::getDbNameList () as $key) {
  421. $nBooks = Book::getBookCount ($i);
  422. array_push ($this->entryArray, new Entry ($key, "cops:{$i}:catalog",
  423. str_format (localize ("bookword", $nBooks), $nBooks), "text",
  424. array ( new LinkNavigation ("?" . DB . "={$i}"))));
  425. $i++;
  426. Base::clearDb ();
  427. }
  428. } else {
  429. if (!in_array (PageQueryResult::SCOPE_AUTHOR, getCurrentOption ('ignored_categories'))) {
  430. array_push ($this->entryArray, Author::getCount());
  431. }
  432. if (!in_array (PageQueryResult::SCOPE_SERIES, getCurrentOption ('ignored_categories'))) {
  433. $series = Serie::getCount();
  434. if (!is_null ($series)) array_push ($this->entryArray, $series);
  435. }
  436. if (!in_array (PageQueryResult::SCOPE_PUBLISHER, getCurrentOption ('ignored_categories'))) {
  437. $publisher = Publisher::getCount();
  438. if (!is_null ($publisher)) array_push ($this->entryArray, $publisher);
  439. }
  440. if (!in_array (PageQueryResult::SCOPE_TAG, getCurrentOption ('ignored_categories'))) {
  441. $tags = Tag::getCount();
  442. if (!is_null ($tags)) array_push ($this->entryArray, $tags);
  443. }
  444. if (!in_array ("language", getCurrentOption ('ignored_categories'))) {
  445. $languages = Language::getCount();
  446. if (!is_null ($languages)) array_push ($this->entryArray, $languages);
  447. }
  448. foreach ($config['cops_calibre_custom_column'] as $lookup) {
  449. $customId = CustomColumn::getCustomId ($lookup);
  450. if (!is_null ($customId)) {
  451. array_push ($this->entryArray, CustomColumn::getCount($customId));
  452. }
  453. }
  454. $this->entryArray = array_merge ($this->entryArray, Book::getCount());
  455. if (Base::isMultipleDatabaseEnabled ()) $this->title = Base::getDbName ();
  456. }
  457. }
  458. public function isPaginated ()
  459. {
  460. return (getCurrentOption ("max_item_per_page") != -1 &&
  461. $this->totalNumber != -1 &&
  462. $this->totalNumber > getCurrentOption ("max_item_per_page"));
  463. }
  464. public function getNextLink ()
  465. {
  466. $currentUrl = getQueryString ();
  467. $currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . getQueryString ());
  468. if (($this->n) * getCurrentOption ("max_item_per_page") < $this->totalNumber) {
  469. return new LinkNavigation ($currentUrl . "&n=" . ($this->n + 1), "next", localize ("paging.next.alternate"));
  470. }
  471. return NULL;
  472. }
  473. public function getPrevLink ()
  474. {
  475. $currentUrl = getQueryString ();
  476. $currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . getQueryString ());
  477. if ($this->n > 1) {
  478. return new LinkNavigation ($currentUrl . "&n=" . ($this->n - 1), "previous", localize ("paging.previous.alternate"));
  479. }
  480. return NULL;
  481. }
  482. public function getMaxPage ()
  483. {
  484. return ceil ($this->totalNumber / getCurrentOption ("max_item_per_page"));
  485. }
  486. public function containsBook ()
  487. {
  488. if (count ($this->entryArray) == 0) return false;
  489. if (get_class ($this->entryArray [0]) == "EntryBook") return true;
  490. return false;
  491. }
  492. public function getContentArrayTypeahead () {
  493. $out = array ();
  494. foreach ($this->entryArray as $entry) {
  495. if ($entry instanceof EntryBook) {
  496. array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->book->getDetailUrl ()));
  497. } else {
  498. if (empty ($entry->className) xor Base::noDatabaseSelected ()) {
  499. array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->getNavLink ()));
  500. } else {
  501. array_push ($out, array ("class" => $entry->className, "title" => $entry->content, "navlink" => $entry->getNavLink ()));
  502. }
  503. }
  504. }
  505. return $out;
  506. }
  507. }
  508. class PageAllAuthors extends Page
  509. {
  510. public function InitializeContent ()
  511. {
  512. $this->title = localize("authors.title");
  513. if (getCurrentOption ("author_split_first_letter") == 1) {
  514. $this->entryArray = Author::getAllAuthorsByFirstLetter();
  515. }
  516. else {
  517. $this->entryArray = Author::getAllAuthors();
  518. }
  519. $this->idPage = Author::ALL_AUTHORS_ID;
  520. }
  521. }
  522. class PageAllAuthorsLetter extends Page
  523. {
  524. public function InitializeContent ()
  525. {
  526. $this->idPage = Author::getEntryIdByLetter ($this->idGet);
  527. $this->entryArray = Author::getAuthorsByStartingLetter ($this->idGet);
  528. $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("authorword", count ($this->entryArray)), count ($this->entryArray)), $this->idGet);
  529. }
  530. }
  531. class PageAuthorDetail extends Page
  532. {
  533. public function InitializeContent ()
  534. {
  535. $author = Author::getAuthorById ($this->idGet);
  536. $this->idPage = $author->getEntryId ();
  537. $this->title = $author->name;
  538. list ($this->entryArray, $this->totalNumber) = Book::getBooksByAuthor ($this->idGet, $this->n);
  539. }
  540. }
  541. class PageAllPublishers extends Page
  542. {
  543. public function InitializeContent ()
  544. {
  545. $this->title = localize("publishers.title");
  546. $this->entryArray = Publisher::getAllPublishers();
  547. $this->idPage = Publisher::ALL_PUBLISHERS_ID;
  548. }
  549. }
  550. class PagePublisherDetail extends Page
  551. {
  552. public function InitializeContent ()
  553. {
  554. $publisher = Publisher::getPublisherById ($this->idGet);
  555. $this->title = $publisher->name;
  556. list ($this->entryArray, $this->totalNumber) = Book::getBooksByPublisher ($this->idGet, $this->n);
  557. $this->idPage = $publisher->getEntryId ();
  558. }
  559. }
  560. class PageAllTags extends Page
  561. {
  562. public function InitializeContent ()
  563. {
  564. $this->title = localize("tags.title");
  565. $this->entryArray = Tag::getAllTags();
  566. $this->idPage = Tag::ALL_TAGS_ID;
  567. }
  568. }
  569. class PageAllLanguages extends Page
  570. {
  571. public function InitializeContent ()
  572. {
  573. $this->title = localize("languages.title");
  574. $this->entryArray = Language::getAllLanguages();
  575. $this->idPage = Language::ALL_LANGUAGES_ID;
  576. }
  577. }
  578. class PageCustomDetail extends Page
  579. {
  580. public function InitializeContent ()
  581. {
  582. $customId = getURLParam ("custom", NULL);
  583. $custom = CustomColumn::getCustomById ($customId, $this->idGet);
  584. $this->idPage = $custom->getEntryId ();
  585. $this->title = $custom->name;
  586. list ($this->entryArray, $this->totalNumber) = Book::getBooksByCustom ($customId, $this->idGet, $this->n);
  587. }
  588. }
  589. class PageAllCustoms extends Page
  590. {
  591. public function InitializeContent ()
  592. {
  593. $customId = getURLParam ("custom", NULL);
  594. $this->title = CustomColumn::getAllTitle ($customId);
  595. $this->entryArray = CustomColumn::getAllCustoms($customId);
  596. $this->idPage = CustomColumn::getAllCustomsId ($customId);
  597. }
  598. }
  599. class PageTagDetail extends Page
  600. {
  601. public function InitializeContent ()
  602. {
  603. $tag = Tag::getTagById ($this->idGet);
  604. $this->idPage = $tag->getEntryId ();
  605. $this->title = $tag->name;
  606. list ($this->entryArray, $this->totalNumber) = Book::getBooksByTag ($this->idGet, $this->n);
  607. }
  608. }
  609. class PageLanguageDetail extends Page
  610. {
  611. public function InitializeContent ()
  612. {
  613. $language = Language::getLanguageById ($this->idGet);
  614. $this->idPage = $language->getEntryId ();
  615. $this->title = $language->lang_code;
  616. list ($this->entryArray, $this->totalNumber) = Book::getBooksByLanguage ($this->idGet, $this->n);
  617. }
  618. }
  619. class PageAllSeries extends Page
  620. {
  621. public function InitializeContent ()
  622. {
  623. $this->title = localize("series.title");
  624. $this->entryArray = Serie::getAllSeries();
  625. $this->idPage = Serie::ALL_SERIES_ID;
  626. }
  627. }
  628. class PageSerieDetail extends Page
  629. {
  630. public function InitializeContent ()
  631. {
  632. $serie = Serie::getSerieById ($this->idGet);
  633. $this->title = $serie->name;
  634. list ($this->entryArray, $this->totalNumber) = Book::getBooksBySeries ($this->idGet, $this->n);
  635. $this->idPage = $serie->getEntryId ();
  636. }
  637. }
  638. class PageAllBooks extends Page
  639. {
  640. public function InitializeContent ()
  641. {
  642. $this->title = localize ("allbooks.title");
  643. if (getCurrentOption ("titles_split_first_letter") == 1) {
  644. $this->entryArray = Book::getAllBooks();
  645. }
  646. else {
  647. list ($this->entryArray, $this->totalNumber) = Book::getBooks ($this->n);
  648. }
  649. $this->idPage = Book::ALL_BOOKS_ID;
  650. }
  651. }
  652. class PageAllBooksLetter extends Page
  653. {
  654. public function InitializeContent ()
  655. {
  656. list ($this->entryArray, $this->totalNumber) = Book::getBooksByStartingLetter ($this->idGet, $this->n);
  657. $this->idPage = Book::getEntryIdByLetter ($this->idGet);
  658. $count = $this->totalNumber;
  659. if ($count == -1)
  660. $count = count ($this->entryArray);
  661. $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("bookword", $count), $count), $this->idGet);
  662. }
  663. }
  664. class PageRecentBooks extends Page
  665. {
  666. public function InitializeContent ()
  667. {
  668. $this->title = localize ("recent.title");
  669. $this->entryArray = Book::getAllRecentBooks ();
  670. $this->idPage = Book::ALL_RECENT_BOOKS_ID;
  671. }
  672. }
  673. class PageQueryResult extends Page
  674. {
  675. const SCOPE_TAG = "tag";
  676. const SCOPE_SERIES = "series";
  677. const SCOPE_AUTHOR = "author";
  678. const SCOPE_BOOK = "book";
  679. const SCOPE_PUBLISHER = "publisher";
  680. private function useTypeahead () {
  681. return !is_null (getURLParam ("search"));
  682. }
  683. private function searchByScope ($scope, $limit = FALSE) {
  684. $n = $this->n;
  685. $numberPerPage = NULL;
  686. if ($limit) {
  687. $n = 1;
  688. $numberPerPage = 5;
  689. }
  690. switch ($scope) {
  691. case self::SCOPE_BOOK :
  692. $array = Book::getBooksByStartingLetter ('%' . $this->query, $n, NULL, $numberPerPage);
  693. break;
  694. case self::SCOPE_AUTHOR :
  695. $array = Author::getAuthorsByStartingLetter ('%' . $this->query);
  696. break;
  697. case self::SCOPE_SERIES :
  698. $array = Serie::getAllSeriesByQuery ($this->query);
  699. break;
  700. case self::SCOPE_TAG :
  701. $array = Tag::getAllTagsByQuery ($this->query, $n, NULL, $numberPerPage);
  702. break;
  703. case self::SCOPE_PUBLISHER :
  704. $array = Publisher::getAllPublishersByQuery ($this->query);
  705. break;
  706. default:
  707. $array = Book::getBooksByQuery (
  708. array ("all" => "%" . $this->query . "%"), $n);
  709. }
  710. return $array;
  711. }
  712. public function doSearchByCategory () {
  713. $database = GetUrlParam (DB);
  714. $out = array ();
  715. $pagequery = Base::PAGE_OPENSEARCH_QUERY;
  716. $dbArray = array ("");
  717. $d = $database;
  718. $query = $this->query;
  719. // Special case when no databases were chosen, we search on all databases
  720. if (Base::noDatabaseSelected ()) {
  721. $dbArray = Base::getDbNameList ();
  722. $d = 0;
  723. }
  724. foreach ($dbArray as $key) {
  725. if (Base::noDatabaseSelected ()) {
  726. array_push ($this->entryArray, new Entry ($key, DB . ":query:{$d}",
  727. " ", "text",
  728. array ( new LinkNavigation ("?" . DB . "={$d}")), "tt-header"));
  729. Base::getDb ($d);
  730. }
  731. foreach (array (PageQueryResult::SCOPE_BOOK,
  732. PageQueryResult::SCOPE_AUTHOR,
  733. PageQueryResult::SCOPE_SERIES,
  734. PageQueryResult::SCOPE_TAG,
  735. PageQueryResult::SCOPE_PUBLISHER) as $key) {
  736. if (in_array($key, getCurrentOption ('ignored_categories'))) {
  737. continue;
  738. }
  739. $array = $this->searchByScope ($key, TRUE);
  740. $i = 0;
  741. if (count ($array) == 2 && is_array ($array [0])) {
  742. $total = $array [1];
  743. $array = $array [0];
  744. } else {
  745. $total = count($array);
  746. }
  747. if ($total > 0) {
  748. // Comment to help the perl i18n script
  749. // str_format (localize("bookword", count($array))
  750. // str_format (localize("authorword", count($array))
  751. // str_format (localize("seriesword", count($array))
  752. // str_format (localize("tagword", count($array))
  753. // str_format (localize("publisherword", count($array))
  754. array_push ($this->entryArray, new Entry (str_format (localize ("search.result.{$key}"), $this->query), DB . ":query:{$d}:{$key}",
  755. str_format (localize("{$key}word", $total), $total), "text",
  756. array ( new LinkNavigation ("?page={$pagequery}&query={$query}&db={$d}&scope={$key}")),
  757. Base::noDatabaseSelected () ? "" : "tt-header"));
  758. }
  759. if (!Base::noDatabaseSelected () && $this->useTypeahead ()) {
  760. foreach ($array as $entry) {
  761. array_push ($this->entryArray, $entry);
  762. $i++;
  763. if ($i > 4) { break; };
  764. }
  765. }
  766. }
  767. $d++;
  768. if (Base::noDatabaseSelected ()) {
  769. Base::clearDb ();
  770. }
  771. }
  772. return $out;
  773. }
  774. public function InitializeContent ()
  775. {
  776. $scope = getURLParam ("scope");
  777. if (empty ($scope)) {
  778. $this->title = str_format (localize ("search.result"), $this->query);
  779. } else {
  780. // Comment to help the perl i18n script
  781. // str_format (localize ("search.result.author"), $this->query)
  782. // str_format (localize ("search.result.tag"), $this->query)
  783. // str_format (localize ("search.result.series"), $this->query)
  784. // str_format (localize ("search.result.book"), $this->query)
  785. // str_format (localize ("search.result.publisher"), $this->query)
  786. $this->title = str_format (localize ("search.result.{$scope}"), $this->query);
  787. }
  788. $crit = "%" . $this->query . "%";
  789. // Special case when we are doing a search and no database is selected
  790. if (Base::noDatabaseSelected () && !$this->useTypeahead ()) {
  791. $i = 0;
  792. foreach (Base::getDbNameList () as $key) {
  793. Base::clearDb ();
  794. list ($array, $totalNumber) = Book::getBooksByQuery (array ("all" => $crit), 1, $i, 1);
  795. array_push ($this->entryArray, new Entry ($key, DB . ":query:{$i}",
  796. str_format (localize ("bookword", $totalNumber), $totalNumber), "text",
  797. array ( new LinkNavigation ("?" . DB . "={$i}&page=9&query=" . $this->query))));
  798. $i++;
  799. }
  800. return;
  801. }
  802. if (empty ($scope)) {
  803. $this->doSearchByCategory ();
  804. return;
  805. }
  806. $array = $this->searchByScope ($scope);
  807. if (count ($array) == 2 && is_array ($array [0])) {
  808. list ($this->entryArray, $this->totalNumber) = $array;
  809. } else {
  810. $this->entryArray = $array;
  811. }
  812. }
  813. }
  814. class PageBookDetail extends Page
  815. {
  816. public function InitializeContent ()
  817. {
  818. $this->book = Book::getBookById ($this->idGet);
  819. $this->title = $this->book->title;
  820. }
  821. }
  822. class PageAbout extends Page
  823. {
  824. public function InitializeContent ()
  825. {
  826. $this->title = localize ("about.title");
  827. }
  828. }
  829. class PageCustomize extends Page
  830. {
  831. private function isChecked ($key, $testedValue = 1) {
  832. $value = getCurrentOption ($key);
  833. if (is_array ($value)) {
  834. if (in_array ($testedValue, $value)) {
  835. return "checked='checked'";
  836. }
  837. } else {
  838. if ($value == $testedValue) {
  839. return "checked='checked'";
  840. }
  841. }
  842. return "";
  843. }
  844. private function isSelected ($key, $value) {
  845. if (getCurrentOption ($key) == $value) {
  846. return "selected='selected'";
  847. }
  848. return "";
  849. }
  850. private function getStyleList () {
  851. $result = array ();
  852. foreach (glob ("styles/style-*.css") as $filename) {
  853. if (preg_match ('/styles\/style-(.*?)\.css/', $filename, $m)) {
  854. array_push ($result, $m [1]);
  855. }
  856. }
  857. return $result;
  858. }
  859. public function InitializeContent ()
  860. {
  861. $this->title = localize ("customize.title");
  862. $this->entryArray = array ();
  863. $ignoredBaseArray = array (PageQueryResult::SCOPE_AUTHOR,
  864. PageQueryResult::SCOPE_TAG,
  865. PageQueryResult::SCOPE_SERIES,
  866. PageQueryResult::SCOPE_PUBLISHER,
  867. "language");
  868. $content = "";
  869. if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
  870. $content .= '<select id="style" onchange="updateCookie (this);">';
  871. foreach ($this-> getStyleList () as $filename) {
  872. $content .= "<option value='{$filename}' " . $this->isSelected ("style", $filename) . ">{$filename}</option>";
  873. }
  874. $content .= '</select>';
  875. } else {
  876. foreach ($this-> getStyleList () as $filename) {
  877. $content .= "<input type='radio' onchange='updateCookieFromCheckbox (this);' id='style-{$filename}' name='style' value='{$filename}' " . $this->isChecked ("style", $filename) . " /><label for='style-{$filename}'> {$filename} </label>";
  878. }
  879. }
  880. array_push ($this->entryArray, new Entry (localize ("customize.style"), "",
  881. $content, "text",
  882. array ()));
  883. if (!useServerSideRendering ()) {
  884. $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="use_fancyapps" ' . $this->isChecked ("use_fancyapps") . ' />';
  885. array_push ($this->entryArray, new Entry (localize ("customize.fancybox"), "",
  886. $content, "text",
  887. array ()));
  888. }
  889. $content = '<input type="number" onchange="updateCookie (this);" id="max_item_per_page" value="' . getCurrentOption ("max_item_per_page") . '" min="-1" max="1200" pattern="^[-+]?[0-9]+$" />';
  890. array_push ($this->entryArray, new Entry (localize ("customize.paging"), "",
  891. $content, "text",
  892. array ()));
  893. $content = '<input type="text" onchange="updateCookie (this);" id="email" value="' . getCurrentOption ("email") . '" />';
  894. array_push ($this->entryArray, new Entry (localize ("customize.email"), "",
  895. $content, "text",
  896. array ()));
  897. $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="html_tag_filter" ' . $this->isChecked ("html_tag_filter") . ' />';
  898. array_push ($this->entryArray, new Entry (localize ("customize.filter"), "",
  899. $content, "text",
  900. array ()));
  901. $content = "";
  902. foreach ($ignoredBaseArray as $key) {
  903. $keyPlural = preg_replace ('/(ss)$/', 's', $key . "s");
  904. $content .= '<input type="checkbox" name="ignored_categories[]" onchange="updateCookieFromCheckboxGroup (this);" id="ignored_categories_' . $key . '" ' . $this->isChecked ("ignored_categories", $key) . ' > ' . localize ("{$keyPlural}.title") . '</input> ';
  905. }
  906. array_push ($this->entryArray, new Entry (localize ("customize.ignored"), "",
  907. $content, "text",
  908. array ()));
  909. }
  910. }
  911. abstract class Base
  912. {
  913. const PAGE_INDEX = "index";
  914. const PAGE_ALL_AUTHORS = "1";
  915. const PAGE_AUTHORS_FIRST_LETTER = "2";
  916. const PAGE_AUTHOR_DETAIL = "3";
  917. const PAGE_ALL_BOOKS = "4";
  918. const PAGE_ALL_BOOKS_LETTER = "5";
  919. const PAGE_ALL_SERIES = "6";
  920. const PAGE_SERIE_DETAIL = "7";
  921. const PAGE_OPENSEARCH = "8";
  922. const PAGE_OPENSEARCH_QUERY = "9";
  923. const PAGE_ALL_RECENT_BOOKS = "10";
  924. const PAGE_ALL_TAGS = "11";
  925. const PAGE_TAG_DETAIL = "12";
  926. const PAGE_BOOK_DETAIL = "13";
  927. const PAGE_ALL_CUSTOMS = "14";
  928. const PAGE_CUSTOM_DETAIL = "15";
  929. const PAGE_ABOUT = "16";
  930. const PAGE_ALL_LANGUAGES = "17";
  931. const PAGE_LANGUAGE_DETAIL = "18";
  932. const PAGE_CUSTOMIZE = "19";
  933. const PAGE_ALL_PUBLISHERS = "20";
  934. const PAGE_PUBLISHER_DETAIL = "21";
  935. const COMPATIBILITY_XML_ALDIKO = "aldiko";
  936. private static $db = NULL;
  937. public static function isMultipleDatabaseEnabled () {
  938. global $config;
  939. return is_array ($config['calibre_directory']);
  940. }
  941. public static function noDatabaseSelected () {
  942. return self::isMultipleDatabaseEnabled () && is_null (GetUrlParam (DB));
  943. }
  944. public static function getDbList () {
  945. global $config;
  946. if (self::isMultipleDatabaseEnabled ()) {
  947. return $config['calibre_directory'];
  948. } else {
  949. return array ("" => $config['calibre_directory']);
  950. }
  951. }
  952. public static function getDbNameList () {
  953. global $config;
  954. if (self::isMultipleDatabaseEnabled ()) {
  955. return array_keys ($config['calibre_directory']);
  956. } else {
  957. return array ("");
  958. }
  959. }
  960. public static function getDbName ($database = NULL) {
  961. global $config;
  962. if (self::isMultipleDatabaseEnabled ()) {
  963. if (is_null ($database)) $database = GetUrlParam (DB, 0);
  964. $array = array_keys ($config['calibre_directory']);
  965. return $array[$database];
  966. }
  967. return "";
  968. }
  969. public static function getDbDirectory ($database = NULL) {
  970. global $config;
  971. if (self::isMultipleDatabaseEnabled ()) {
  972. if (is_null ($database)) $database = GetUrlParam (DB, 0);
  973. $array = array_values ($config['calibre_directory']);
  974. return $array[$database];
  975. }
  976. return $config['calibre_directory'];
  977. }
  978. public static function getDbFileName ($database = NULL) {
  979. return self::getDbDirectory ($database) .'metadata.db';
  980. }
  981. private static function error () {
  982. if (php_sapi_name() != "cli") {
  983. header("location: checkconfig.php?err=1");
  984. }
  985. throw new Exception('Database not found.');
  986. }
  987. public static function getDb ($database = NULL) {
  988. if (is_null (self::$db)) {
  989. try {
  990. if (is_readable (self::getDbFileName ($database))) {
  991. self::$db = new PDO('sqlite:'. self::getDbFileName ($database));
  992. } else {
  993. self::error ();
  994. }
  995. } catch (Exception $e) {
  996. self::error ();
  997. }
  998. }
  999. return self::$db;
  1000. }
  1001. public static function checkDatabaseAvailability () {
  1002. if (self::noDatabaseSelected ()) {
  1003. for ($i = 0; $i < count (self::getDbList ()); $i++) {
  1004. self::getDb ($i);
  1005. self::clearDb ();
  1006. }
  1007. } else {
  1008. self::getDb ();
  1009. }
  1010. return true;
  1011. }
  1012. public static function clearDb () {
  1013. self::$db = NULL;
  1014. }
  1015. public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL, $numberPerPage = NULL) {
  1016. $totalResult = -1;
  1017. if (is_null ($numberPerPage)) {
  1018. $numberPerPage = getCurrentOption ("max_item_per_page");
  1019. }
  1020. if ($numberPerPage != -1 && $n != -1)
  1021. {
  1022. // First check total number of results
  1023. $result = self::getDb ($database)->prepare (str_format ($query, "count(*)", $filter));
  1024. $result->execute ($params);
  1025. $totalResult = $result->fetchColumn ();
  1026. // Next modify the query and params
  1027. $query .= " limit ?, ?";
  1028. array_push ($params, ($n - 1) * $numberPerPage, $numberPerPage);
  1029. }
  1030. $result = self::getDb ($database)->prepare(str_format ($query, $columns, $filter));
  1031. $result->execute ($params);
  1032. return array ($totalResult, $result);
  1033. }
  1034. }