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.

1400 lines
50KB

  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", "1.0.0RC4");
  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. $theme = getCurrentTemplate ();
  18. $header = file_get_contents('templates/' . $theme . '/header.html');
  19. $footer = file_get_contents('templates/' . $theme . '/footer.html');
  20. $main = file_get_contents('templates/' . $theme . '/main.html');
  21. $bookdetail = file_get_contents('templates/' . $theme . '/bookdetail.html');
  22. $page = file_get_contents('templates/' . $theme . '/page.html');
  23. // Generate the function for the template
  24. $template = new doT ();
  25. $dot = $template->template ($page, array ("bookdetail" => $bookdetail,
  26. "header" => $header,
  27. "footer" => $footer,
  28. "main" => $main));
  29. // If there is a syntax error in the function created
  30. // $dot will be equal to FALSE
  31. if (!$dot) {
  32. return FALSE;
  33. }
  34. // Execute the template
  35. if (!empty ($data)) {
  36. return $dot ($data);
  37. }
  38. return NULL;
  39. }
  40. function getQueryString () {
  41. if ( isset($_SERVER['QUERY_STRING']) ) {
  42. return $_SERVER['QUERY_STRING'];
  43. }
  44. return "";
  45. }
  46. function notFound () {
  47. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  48. header("Status: 404 Not Found");
  49. $_SERVER['REDIRECT_STATUS'] = 404;
  50. }
  51. function getURLParam ($name, $default = NULL) {
  52. if (!empty ($_GET) && isset($_GET[$name]) && $_GET[$name] != "") {
  53. return $_GET[$name];
  54. }
  55. return $default;
  56. }
  57. function getCurrentOption ($option) {
  58. global $config;
  59. if (isset($_COOKIE[$option])) {
  60. if (isset($config ["cops_" . $option]) && is_array ($config ["cops_" . $option])) {
  61. return explode (",", $_COOKIE[$option]);
  62. } else {
  63. return $_COOKIE[$option];
  64. }
  65. }
  66. if ($option == "style") {
  67. return "default";
  68. }
  69. if (isset($config ["cops_" . $option])) {
  70. return $config ["cops_" . $option];
  71. }
  72. return "";
  73. }
  74. function getCurrentCss () {
  75. return "templates/" . getCurrentTemplate () . "/styles/style-" . getCurrentOption ("style") . ".css";
  76. }
  77. function getCurrentTemplate () {
  78. return getCurrentOption ("template");
  79. }
  80. function getUrlWithVersion ($url) {
  81. return $url . "?v=" . VERSION;
  82. }
  83. function xml2xhtml($xml) {
  84. return preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', create_function('$m', '
  85. $xhtml_tags = array("br", "hr", "input", "frame", "img", "area", "link", "col", "base", "basefont", "param");
  86. return in_array($m[1], $xhtml_tags) ? "<$m[1]$m[2] />" : "<$m[1]$m[2]></$m[1]>";
  87. '), $xml);
  88. }
  89. function display_xml_error($error)
  90. {
  91. $return = "";
  92. $return .= str_repeat('-', $error->column) . "^\n";
  93. switch ($error->level) {
  94. case LIBXML_ERR_WARNING:
  95. $return .= "Warning $error->code: ";
  96. break;
  97. case LIBXML_ERR_ERROR:
  98. $return .= "Error $error->code: ";
  99. break;
  100. case LIBXML_ERR_FATAL:
  101. $return .= "Fatal Error $error->code: ";
  102. break;
  103. }
  104. $return .= trim($error->message) .
  105. "\n Line: $error->line" .
  106. "\n Column: $error->column";
  107. if ($error->file) {
  108. $return .= "\n File: $error->file";
  109. }
  110. return "$return\n\n--------------------------------------------\n\n";
  111. }
  112. function are_libxml_errors_ok ()
  113. {
  114. $errors = libxml_get_errors();
  115. foreach ($errors as $error) {
  116. if ($error->code == 801) return false;
  117. }
  118. return true;
  119. }
  120. function html2xhtml ($html) {
  121. $doc = new DOMDocument();
  122. libxml_use_internal_errors(true);
  123. $doc->loadHTML('<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' .
  124. $html . '</body></html>'); // Load the HTML
  125. $output = $doc->saveXML($doc->documentElement); // Transform to an Ansi xml stream
  126. $output = xml2xhtml($output);
  127. if (preg_match ('#<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></meta></head><body>(.*)</body></html>#ms', $output, $matches)) {
  128. $output = $matches [1]; // Remove <html><body>
  129. }
  130. /*
  131. // In case of error with summary, use it to debug
  132. $errors = libxml_get_errors();
  133. foreach ($errors as $error) {
  134. $output .= display_xml_error($error);
  135. }
  136. */
  137. if (!are_libxml_errors_ok ()) $output = "HTML code not valid.";
  138. libxml_use_internal_errors(false);
  139. return $output;
  140. }
  141. /**
  142. * This method is a direct copy-paste from
  143. * http://tmont.com/blargh/2010/1/string-format-in-php
  144. */
  145. function str_format($format) {
  146. $args = func_get_args();
  147. $format = array_shift($args);
  148. preg_match_all('/(?=\{)\{(\d+)\}(?!\})/', $format, $matches, PREG_OFFSET_CAPTURE);
  149. $offset = 0;
  150. foreach ($matches[1] as $data) {
  151. $i = $data[0];
  152. $format = substr_replace($format, @$args[$i], $offset + $data[1] - 1, 2 + strlen($i));
  153. $offset += strlen(@$args[$i]) - 2 - strlen($i);
  154. }
  155. return $format;
  156. }
  157. /**
  158. * Get all accepted languages from the browser and put them in a sorted array
  159. * languages id are normalized : fr-fr -> fr_FR
  160. * @return array of languages
  161. */
  162. function getAcceptLanguages() {
  163. $langs = array();
  164. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  165. // break up string into pieces (languages and q factors)
  166. $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  167. if (preg_match('/^(\w{2})-\w{2}$/', $accept, $matches)) {
  168. // Special fix for IE11 which send fr-FR and nothing else
  169. $accept = $accept . "," . $matches[1] . ";q=0.8";
  170. }
  171. preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $accept, $lang_parse);
  172. if (count($lang_parse[1])) {
  173. $langs = array();
  174. foreach ($lang_parse[1] as $lang) {
  175. // Format the language code (not standard among browsers)
  176. if (strlen($lang) == 5) {
  177. $lang = str_replace("-", "_", $lang);
  178. $splitted = preg_split("/_/", $lang);
  179. $lang = $splitted[0] . "_" . strtoupper($splitted[1]);
  180. }
  181. array_push($langs, $lang);
  182. }
  183. // create a list like "en" => 0.8
  184. $langs = array_combine($langs, $lang_parse[4]);
  185. // set default to 1 for any without q factor
  186. foreach ($langs as $lang => $val) {
  187. if ($val === '') $langs[$lang] = 1;
  188. }
  189. // sort list based on value
  190. arsort($langs, SORT_NUMERIC);
  191. }
  192. }
  193. return $langs;
  194. }
  195. /**
  196. * Find the best translation file possible based on the accepted languages
  197. * @return array of language and language file
  198. */
  199. function getLangAndTranslationFile() {
  200. global $config;
  201. $langs = array();
  202. $lang = "en";
  203. if (!empty($config['cops_language'])) {
  204. $lang = $config['cops_language'];
  205. }
  206. elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  207. $langs = getAcceptLanguages();
  208. }
  209. //echo var_dump($langs);
  210. $lang_file = NULL;
  211. foreach ($langs as $language => $val) {
  212. $temp_file = dirname(__FILE__). '/lang/Localization_' . $language . '.json';
  213. if (file_exists($temp_file)) {
  214. $lang = $language;
  215. $lang_file = $temp_file;
  216. break;
  217. }
  218. }
  219. if (empty ($lang_file)) {
  220. $lang_file = dirname(__FILE__). '/lang/Localization_' . $lang . '.json';
  221. }
  222. return array($lang, $lang_file);
  223. }
  224. /**
  225. * This method is based on this page
  226. * http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/
  227. */
  228. function localize($phrase, $count=-1, $reset=false) {
  229. global $config;
  230. if ($count == 0)
  231. $phrase .= ".none";
  232. if ($count == 1)
  233. $phrase .= ".one";
  234. if ($count > 1)
  235. $phrase .= ".many";
  236. /* Static keyword is used to ensure the file is loaded only once */
  237. static $translations = NULL;
  238. if ($reset) {
  239. $translations = NULL;
  240. }
  241. /* If no instance of $translations has occured load the language file */
  242. if (is_null($translations)) {
  243. $lang_file_en = NULL;
  244. list ($lang, $lang_file) = getLangAndTranslationFile();
  245. if ($lang != "en") {
  246. $lang_file_en = dirname(__FILE__). '/lang/' . 'Localization_en.json';
  247. }
  248. $lang_file_content = file_get_contents($lang_file);
  249. /* Load the language file as a JSON object and transform it into an associative array */
  250. $translations = json_decode($lang_file_content, true);
  251. /* Clean the array of all unfinished translations */
  252. foreach (array_keys ($translations) as $key) {
  253. if (preg_match ("/^##TODO##/", $key)) {
  254. unset ($translations [$key]);
  255. }
  256. }
  257. if ($lang_file_en)
  258. {
  259. $lang_file_content = file_get_contents($lang_file_en);
  260. $translations_en = json_decode($lang_file_content, true);
  261. $translations = array_merge ($translations_en, $translations);
  262. }
  263. }
  264. if (array_key_exists ($phrase, $translations)) {
  265. return $translations[$phrase];
  266. }
  267. return $phrase;
  268. }
  269. function addURLParameter($urlParams, $paramName, $paramValue) {
  270. if (empty ($urlParams)) {
  271. $urlParams = "";
  272. }
  273. $start = "";
  274. if (preg_match ("#^\?(.*)#", $urlParams, $matches)) {
  275. $start = "?";
  276. $urlParams = $matches[1];
  277. }
  278. $params = array();
  279. parse_str($urlParams, $params);
  280. if (empty ($paramValue) && $paramValue != 0) {
  281. unset ($params[$paramName]);
  282. } else {
  283. $params[$paramName] = $paramValue;
  284. }
  285. return $start . http_build_query($params);
  286. }
  287. function useNormAndUp () {
  288. global $config;
  289. return $config ['cops_normalized_search'] == "1";
  290. // return extension_loaded('mbstring') &&
  291. // extension_loaded('intl') &&
  292. // class_exists("Normalizer", $autoload = false) &&
  293. // $config ['cops_normalized_search'] == "1";
  294. }
  295. function normalizeUtf8String( $s)
  296. {
  297. $original_string = $s;
  298. // maps German (umlauts) and other European characters onto two characters before just removing diacritics
  299. $s = preg_replace( '@\x{00c4}@u' , "AE", $s ); // umlaut Ä => AE
  300. $s = preg_replace( '@\x{00d6}@u' , "OE", $s ); // umlaut Ö => OE
  301. $s = preg_replace( '@\x{00dc}@u' , "UE", $s ); // umlaut Ü => UE
  302. $s = preg_replace( '@\x{00e4}@u' , "ae", $s ); // umlaut ä => ae
  303. $s = preg_replace( '@\x{00f6}@u' , "oe", $s ); // umlaut ö => oe
  304. $s = preg_replace( '@\x{00fc}@u' , "ue", $s ); // umlaut ü => ue
  305. $s = preg_replace( '@\x{00f1}@u' , "ny", $s ); // ñ => ny
  306. $s = preg_replace( '@\x{00ff}@u' , "yu", $s ); // ÿ => yu
  307. // maps special characters (characters with diacritics) on their base-character followed by the diacritical mark
  308. // exmaple: Ú => U´, á => a`
  309. $s = Normalizer::normalize( $s, Normalizer::FORM_D );
  310. $s = preg_replace( '@\pM@u' , "", $s ); // removes diacritics
  311. $s = preg_replace( '@\x{00df}@u' , "ss", $s ); // maps German ß onto ss
  312. $s = preg_replace( '@\x{00c6}@u' , "AE", $s ); // Æ => AE
  313. $s = preg_replace( '@\x{00e6}@u' , "ae", $s ); // æ => ae
  314. $s = preg_replace( '@\x{0132}@u' , "IJ", $s ); // ? => IJ
  315. $s = preg_replace( '@\x{0133}@u' , "ij", $s ); // ? => ij
  316. $s = preg_replace( '@\x{0152}@u' , "OE", $s ); // Π=> OE
  317. $s = preg_replace( '@\x{0153}@u' , "oe", $s ); // œ => oe
  318. $s = preg_replace( '@\x{00d0}@u' , "D", $s ); // Ð => D
  319. $s = preg_replace( '@\x{0110}@u' , "D", $s ); // Ð => D
  320. $s = preg_replace( '@\x{00f0}@u' , "d", $s ); // ð => d
  321. $s = preg_replace( '@\x{0111}@u' , "d", $s ); // d => d
  322. $s = preg_replace( '@\x{0126}@u' , "H", $s ); // H => H
  323. $s = preg_replace( '@\x{0127}@u' , "h", $s ); // h => h
  324. $s = preg_replace( '@\x{0131}@u' , "i", $s ); // i => i
  325. $s = preg_replace( '@\x{0138}@u' , "k", $s ); // ? => k
  326. $s = preg_replace( '@\x{013f}@u' , "L", $s ); // ? => L
  327. $s = preg_replace( '@\x{0141}@u' , "L", $s ); // L => L
  328. $s = preg_replace( '@\x{0140}@u' , "l", $s ); // ? => l
  329. $s = preg_replace( '@\x{0142}@u' , "l", $s ); // l => l
  330. $s = preg_replace( '@\x{014a}@u' , "N", $s ); // ? => N
  331. $s = preg_replace( '@\x{0149}@u' , "n", $s ); // ? => n
  332. $s = preg_replace( '@\x{014b}@u' , "n", $s ); // ? => n
  333. $s = preg_replace( '@\x{00d8}@u' , "O", $s ); // Ø => O
  334. $s = preg_replace( '@\x{00f8}@u' , "o", $s ); // ø => o
  335. $s = preg_replace( '@\x{017f}@u' , "s", $s ); // ? => s
  336. $s = preg_replace( '@\x{00de}@u' , "T", $s ); // Þ => T
  337. $s = preg_replace( '@\x{0166}@u' , "T", $s ); // T => T
  338. $s = preg_replace( '@\x{00fe}@u' , "t", $s ); // þ => t
  339. $s = preg_replace( '@\x{0167}@u' , "t", $s ); // t => t
  340. // remove all non-ASCii characters
  341. $s = preg_replace( '@[^\0-\x80]@u' , "", $s );
  342. // possible errors in UTF8-regular-expressions
  343. if (empty($s))
  344. return $original_string;
  345. else
  346. return $s;
  347. }
  348. function transliterate ($a) {
  349. include_once 'transliteration.php';
  350. return _transliteration_process($a);
  351. }
  352. function normAndUp ($a) {
  353. //return mb_strtoupper (normalizeUtf8String($a), 'UTF-8');
  354. return mb_strtoupper (transliterate($a), 'UTF-8');
  355. }
  356. class Link
  357. {
  358. const OPDS_THUMBNAIL_TYPE = "http://opds-spec.org/image/thumbnail";
  359. const OPDS_IMAGE_TYPE = "http://opds-spec.org/image";
  360. const OPDS_ACQUISITION_TYPE = "http://opds-spec.org/acquisition";
  361. const OPDS_NAVIGATION_TYPE = "application/atom+xml;profile=opds-catalog;kind=navigation";
  362. const OPDS_PAGING_TYPE = "application/atom+xml;profile=opds-catalog;kind=acquisition";
  363. public $href;
  364. public $type;
  365. public $rel;
  366. public $title;
  367. public $facetGroup;
  368. public $activeFacet;
  369. public function __construct($phref, $ptype, $prel = NULL, $ptitle = NULL, $pfacetGroup = NULL, $pactiveFacet = FALSE) {
  370. $this->href = $phref;
  371. $this->type = $ptype;
  372. $this->rel = $prel;
  373. $this->title = $ptitle;
  374. $this->facetGroup = $pfacetGroup;
  375. $this->activeFacet = $pactiveFacet;
  376. }
  377. public function hrefXhtml () {
  378. return $this->href;
  379. }
  380. }
  381. class LinkNavigation extends Link
  382. {
  383. public function __construct($phref, $prel = NULL, $ptitle = NULL) {
  384. parent::__construct ($phref, Link::OPDS_NAVIGATION_TYPE, $prel, $ptitle);
  385. if (!is_null (GetUrlParam (DB))) $this->href = addURLParameter ($this->href, DB, GetUrlParam (DB));
  386. if (!preg_match ("#^\?(.*)#", $this->href) && !empty ($this->href)) $this->href = "?" . $this->href;
  387. if (preg_match ("/(bookdetail|getJSON).php/", $_SERVER["SCRIPT_NAME"])) {
  388. $this->href = "index.php" . $this->href;
  389. } else {
  390. $this->href = $_SERVER["SCRIPT_NAME"] . $this->href;
  391. }
  392. }
  393. }
  394. class LinkFacet extends Link
  395. {
  396. public function __construct($phref, $ptitle = NULL, $pfacetGroup = NULL, $pactiveFacet = FALSE) {
  397. parent::__construct ($phref, Link::OPDS_PAGING_TYPE, "http://opds-spec.org/facet", $ptitle, $pfacetGroup, $pactiveFacet);
  398. if (!is_null (GetUrlParam (DB))) $this->href = addURLParameter ($this->href, DB, GetUrlParam (DB));
  399. $this->href = $_SERVER["SCRIPT_NAME"] . $this->href;
  400. }
  401. }
  402. class Entry
  403. {
  404. public $title;
  405. public $id;
  406. public $content;
  407. public $numberOfElement;
  408. public $contentType;
  409. public $linkArray;
  410. public $localUpdated;
  411. public $className;
  412. private static $updated = NULL;
  413. public static $icons = array(
  414. Author::ALL_AUTHORS_ID => 'images/author.png',
  415. Serie::ALL_SERIES_ID => 'images/serie.png',
  416. Book::ALL_RECENT_BOOKS_ID => 'images/recent.png',
  417. Tag::ALL_TAGS_ID => 'images/tag.png',
  418. Language::ALL_LANGUAGES_ID => 'images/language.png',
  419. CustomColumn::ALL_CUSTOMS_ID => 'images/tag.png',
  420. "cops:books$" => 'images/allbook.png',
  421. "cops:books:letter" => 'images/allbook.png',
  422. Publisher::ALL_PUBLISHERS_ID => 'images/publisher.png'
  423. );
  424. public function getUpdatedTime () {
  425. if (!is_null ($this->localUpdated)) {
  426. return date (DATE_ATOM, $this->localUpdated);
  427. }
  428. if (is_null (self::$updated)) {
  429. self::$updated = time();
  430. }
  431. return date (DATE_ATOM, self::$updated);
  432. }
  433. public function getNavLink () {
  434. foreach ($this->linkArray as $link) {
  435. if ($link->type != Link::OPDS_NAVIGATION_TYPE) { continue; }
  436. return $link->hrefXhtml ();
  437. }
  438. return "#";
  439. }
  440. public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pclass = "", $pcount = 0) {
  441. global $config;
  442. $this->title = $ptitle;
  443. $this->id = $pid;
  444. $this->content = $pcontent;
  445. $this->contentType = $pcontentType;
  446. $this->linkArray = $plinkArray;
  447. $this->className = $pclass;
  448. $this->numberOfElement = $pcount;
  449. if ($config['cops_show_icons'] == 1)
  450. {
  451. foreach (self::$icons as $reg => $image)
  452. {
  453. if (preg_match ("/" . $reg . "/", $pid)) {
  454. array_push ($this->linkArray, new Link (getUrlWithVersion ($image), "image/png", Link::OPDS_THUMBNAIL_TYPE));
  455. break;
  456. }
  457. }
  458. }
  459. if (!is_null (GetUrlParam (DB))) $this->id = str_replace ("cops:", "cops:" . GetUrlParam (DB) . ":", $this->id);
  460. }
  461. }
  462. class EntryBook extends Entry
  463. {
  464. public $book;
  465. public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pbook) {
  466. parent::__construct ($ptitle, $pid, $pcontent, $pcontentType, $plinkArray);
  467. $this->book = $pbook;
  468. $this->localUpdated = $pbook->timestamp;
  469. }
  470. public function getCoverThumbnail () {
  471. foreach ($this->linkArray as $link) {
  472. if ($link->rel == Link::OPDS_THUMBNAIL_TYPE)
  473. return $link->hrefXhtml ();
  474. }
  475. return null;
  476. }
  477. public function getCover () {
  478. foreach ($this->linkArray as $link) {
  479. if ($link->rel == Link::OPDS_IMAGE_TYPE)
  480. return $link->hrefXhtml ();
  481. }
  482. return null;
  483. }
  484. }
  485. class Page
  486. {
  487. public $title;
  488. public $subtitle = "";
  489. public $authorName = "";
  490. public $authorUri = "";
  491. public $authorEmail = "";
  492. public $idPage;
  493. public $idGet;
  494. public $query;
  495. public $favicon;
  496. public $n;
  497. public $book;
  498. public $totalNumber = -1;
  499. public $entryArray = array();
  500. public static function getPage ($pageId, $id, $query, $n)
  501. {
  502. switch ($pageId) {
  503. case Base::PAGE_ALL_AUTHORS :
  504. return new PageAllAuthors ($id, $query, $n);
  505. case Base::PAGE_AUTHORS_FIRST_LETTER :
  506. return new PageAllAuthorsLetter ($id, $query, $n);
  507. case Base::PAGE_AUTHOR_DETAIL :
  508. return new PageAuthorDetail ($id, $query, $n);
  509. case Base::PAGE_ALL_TAGS :
  510. return new PageAllTags ($id, $query, $n);
  511. case Base::PAGE_TAG_DETAIL :
  512. return new PageTagDetail ($id, $query, $n);
  513. case Base::PAGE_ALL_LANGUAGES :
  514. return new PageAllLanguages ($id, $query, $n);
  515. case Base::PAGE_LANGUAGE_DETAIL :
  516. return new PageLanguageDetail ($id, $query, $n);
  517. case Base::PAGE_ALL_CUSTOMS :
  518. return new PageAllCustoms ($id, $query, $n);
  519. case Base::PAGE_CUSTOM_DETAIL :
  520. return new PageCustomDetail ($id, $query, $n);
  521. case Base::PAGE_ALL_RATINGS :
  522. return new PageAllRating ($id, $query, $n);
  523. case Base::PAGE_RATING_DETAIL :
  524. return new PageRatingDetail ($id, $query, $n);
  525. case Base::PAGE_ALL_SERIES :
  526. return new PageAllSeries ($id, $query, $n);
  527. case Base::PAGE_ALL_BOOKS :
  528. return new PageAllBooks ($id, $query, $n);
  529. case Base::PAGE_ALL_BOOKS_LETTER:
  530. return new PageAllBooksLetter ($id, $query, $n);
  531. case Base::PAGE_ALL_RECENT_BOOKS :
  532. return new PageRecentBooks ($id, $query, $n);
  533. case Base::PAGE_SERIE_DETAIL :
  534. return new PageSerieDetail ($id, $query, $n);
  535. case Base::PAGE_OPENSEARCH_QUERY :
  536. return new PageQueryResult ($id, $query, $n);
  537. case Base::PAGE_BOOK_DETAIL :
  538. return new PageBookDetail ($id, $query, $n);
  539. case Base::PAGE_ALL_PUBLISHERS:
  540. return new PageAllPublishers ($id, $query, $n);
  541. case Base::PAGE_PUBLISHER_DETAIL :
  542. return new PagePublisherDetail ($id, $query, $n);
  543. case Base::PAGE_ABOUT :
  544. return new PageAbout ($id, $query, $n);
  545. case Base::PAGE_CUSTOMIZE :
  546. return new PageCustomize ($id, $query, $n);
  547. default:
  548. $page = new Page ($id, $query, $n);
  549. $page->idPage = "cops:catalog";
  550. return $page;
  551. }
  552. }
  553. public function __construct($pid, $pquery, $pn) {
  554. global $config;
  555. $this->idGet = $pid;
  556. $this->query = $pquery;
  557. $this->n = $pn;
  558. $this->favicon = $config['cops_icon'];
  559. $this->authorName = empty($config['cops_author_name']) ? utf8_encode('S�bastien Lucas') : $config['cops_author_name'];
  560. $this->authorUri = empty($config['cops_author_uri']) ? 'http://blog.slucas.fr' : $config['cops_author_uri'];
  561. $this->authorEmail = empty($config['cops_author_email']) ? 'sebastien@slucas.fr' : $config['cops_author_email'];
  562. }
  563. public function InitializeContent ()
  564. {
  565. global $config;
  566. $this->title = $config['cops_title_default'];
  567. $this->subtitle = $config['cops_subtitle_default'];
  568. if (Base::noDatabaseSelected ()) {
  569. $i = 0;
  570. foreach (Base::getDbNameList () as $key) {
  571. $nBooks = Book::getBookCount ($i);
  572. array_push ($this->entryArray, new Entry ($key, "cops:{$i}:catalog",
  573. str_format (localize ("bookword", $nBooks), $nBooks), "text",
  574. array ( new LinkNavigation ("?" . DB . "={$i}")), "", $nBooks));
  575. $i++;
  576. Base::clearDb ();
  577. }
  578. } else {
  579. if (!in_array (PageQueryResult::SCOPE_AUTHOR, getCurrentOption ('ignored_categories'))) {
  580. array_push ($this->entryArray, Author::getCount());
  581. }
  582. if (!in_array (PageQueryResult::SCOPE_SERIES, getCurrentOption ('ignored_categories'))) {
  583. $series = Serie::getCount();
  584. if (!is_null ($series)) array_push ($this->entryArray, $series);
  585. }
  586. if (!in_array (PageQueryResult::SCOPE_PUBLISHER, getCurrentOption ('ignored_categories'))) {
  587. $publisher = Publisher::getCount();
  588. if (!is_null ($publisher)) array_push ($this->entryArray, $publisher);
  589. }
  590. if (!in_array (PageQueryResult::SCOPE_TAG, getCurrentOption ('ignored_categories'))) {
  591. $tags = Tag::getCount();
  592. if (!is_null ($tags)) array_push ($this->entryArray, $tags);
  593. }
  594. if (!in_array (PageQueryResult::SCOPE_RATING, getCurrentOption ('ignored_categories'))) {
  595. $rating = Rating::getCount();
  596. if (!is_null ($rating)) array_push ($this->entryArray, $rating);
  597. }
  598. if (!in_array ("language", getCurrentOption ('ignored_categories'))) {
  599. $languages = Language::getCount();
  600. if (!is_null ($languages)) array_push ($this->entryArray, $languages);
  601. }
  602. foreach ($config['cops_calibre_custom_column'] as $lookup) {
  603. $customId = CustomColumn::getCustomId ($lookup);
  604. if (!is_null ($customId)) {
  605. array_push ($this->entryArray, CustomColumn::getCount($customId));
  606. }
  607. }
  608. $this->entryArray = array_merge ($this->entryArray, Book::getCount());
  609. if (Base::isMultipleDatabaseEnabled ()) $this->title = Base::getDbName ();
  610. }
  611. }
  612. public function isPaginated ()
  613. {
  614. return (getCurrentOption ("max_item_per_page") != -1 &&
  615. $this->totalNumber != -1 &&
  616. $this->totalNumber > getCurrentOption ("max_item_per_page"));
  617. }
  618. public function getNextLink ()
  619. {
  620. $currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . getQueryString ());
  621. if (($this->n) * getCurrentOption ("max_item_per_page") < $this->totalNumber) {
  622. return new LinkNavigation ($currentUrl . "&n=" . ($this->n + 1), "next", localize ("paging.next.alternate"));
  623. }
  624. return NULL;
  625. }
  626. public function getPrevLink ()
  627. {
  628. $currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . getQueryString ());
  629. if ($this->n > 1) {
  630. return new LinkNavigation ($currentUrl . "&n=" . ($this->n - 1), "previous", localize ("paging.previous.alternate"));
  631. }
  632. return NULL;
  633. }
  634. public function getMaxPage ()
  635. {
  636. return ceil ($this->totalNumber / getCurrentOption ("max_item_per_page"));
  637. }
  638. public function containsBook ()
  639. {
  640. if (count ($this->entryArray) == 0) return false;
  641. if (get_class ($this->entryArray [0]) == "EntryBook") return true;
  642. return false;
  643. }
  644. }
  645. class PageAllAuthors extends Page
  646. {
  647. public function InitializeContent ()
  648. {
  649. $this->title = localize("authors.title");
  650. if (getCurrentOption ("author_split_first_letter") == 1) {
  651. $this->entryArray = Author::getAllAuthorsByFirstLetter();
  652. }
  653. else {
  654. $this->entryArray = Author::getAllAuthors();
  655. }
  656. $this->idPage = Author::ALL_AUTHORS_ID;
  657. }
  658. }
  659. class PageAllAuthorsLetter extends Page
  660. {
  661. public function InitializeContent ()
  662. {
  663. $this->idPage = Author::getEntryIdByLetter ($this->idGet);
  664. $this->entryArray = Author::getAuthorsByStartingLetter ($this->idGet);
  665. $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("authorword", count ($this->entryArray)), count ($this->entryArray)), $this->idGet);
  666. }
  667. }
  668. class PageAuthorDetail extends Page
  669. {
  670. public function InitializeContent ()
  671. {
  672. $author = Author::getAuthorById ($this->idGet);
  673. $this->idPage = $author->getEntryId ();
  674. $this->title = $author->name;
  675. list ($this->entryArray, $this->totalNumber) = Book::getBooksByAuthor ($this->idGet, $this->n);
  676. }
  677. }
  678. class PageAllPublishers extends Page
  679. {
  680. public function InitializeContent ()
  681. {
  682. $this->title = localize("publishers.title");
  683. $this->entryArray = Publisher::getAllPublishers();
  684. $this->idPage = Publisher::ALL_PUBLISHERS_ID;
  685. }
  686. }
  687. class PagePublisherDetail extends Page
  688. {
  689. public function InitializeContent ()
  690. {
  691. $publisher = Publisher::getPublisherById ($this->idGet);
  692. $this->title = $publisher->name;
  693. list ($this->entryArray, $this->totalNumber) = Book::getBooksByPublisher ($this->idGet, $this->n);
  694. $this->idPage = $publisher->getEntryId ();
  695. }
  696. }
  697. class PageAllTags extends Page
  698. {
  699. public function InitializeContent ()
  700. {
  701. $this->title = localize("tags.title");
  702. $this->entryArray = Tag::getAllTags();
  703. $this->idPage = Tag::ALL_TAGS_ID;
  704. }
  705. }
  706. class PageAllLanguages extends Page
  707. {
  708. public function InitializeContent ()
  709. {
  710. $this->title = localize("languages.title");
  711. $this->entryArray = Language::getAllLanguages();
  712. $this->idPage = Language::ALL_LANGUAGES_ID;
  713. }
  714. }
  715. class PageCustomDetail extends Page
  716. {
  717. public function InitializeContent ()
  718. {
  719. $customId = getURLParam ("custom", NULL);
  720. $custom = CustomColumn::getCustomById ($customId, $this->idGet);
  721. $this->idPage = $custom->getEntryId ();
  722. $this->title = $custom->name;
  723. list ($this->entryArray, $this->totalNumber) = Book::getBooksByCustom ($customId, $this->idGet, $this->n);
  724. }
  725. }
  726. class PageAllCustoms extends Page
  727. {
  728. public function InitializeContent ()
  729. {
  730. $customId = getURLParam ("custom", NULL);
  731. $this->title = CustomColumn::getAllTitle ($customId);
  732. $this->entryArray = CustomColumn::getAllCustoms($customId);
  733. $this->idPage = CustomColumn::getAllCustomsId ($customId);
  734. }
  735. }
  736. class PageTagDetail extends Page
  737. {
  738. public function InitializeContent ()
  739. {
  740. $tag = Tag::getTagById ($this->idGet);
  741. $this->idPage = $tag->getEntryId ();
  742. $this->title = $tag->name;
  743. list ($this->entryArray, $this->totalNumber) = Book::getBooksByTag ($this->idGet, $this->n);
  744. }
  745. }
  746. class PageLanguageDetail extends Page
  747. {
  748. public function InitializeContent ()
  749. {
  750. $language = Language::getLanguageById ($this->idGet);
  751. $this->idPage = $language->getEntryId ();
  752. $this->title = $language->lang_code;
  753. list ($this->entryArray, $this->totalNumber) = Book::getBooksByLanguage ($this->idGet, $this->n);
  754. }
  755. }
  756. class PageAllSeries extends Page
  757. {
  758. public function InitializeContent ()
  759. {
  760. $this->title = localize("series.title");
  761. $this->entryArray = Serie::getAllSeries();
  762. $this->idPage = Serie::ALL_SERIES_ID;
  763. }
  764. }
  765. class PageSerieDetail extends Page
  766. {
  767. public function InitializeContent ()
  768. {
  769. $serie = Serie::getSerieById ($this->idGet);
  770. $this->title = $serie->name;
  771. list ($this->entryArray, $this->totalNumber) = Book::getBooksBySeries ($this->idGet, $this->n);
  772. $this->idPage = $serie->getEntryId ();
  773. }
  774. }
  775. class PageAllRating extends Page
  776. {
  777. public function InitializeContent ()
  778. {
  779. $this->title = localize("ratings.title");
  780. $this->entryArray = Rating::getAllRatings();
  781. $this->idPage = Rating::ALL_RATING_ID;
  782. }
  783. }
  784. class PageRatingDetail extends Page
  785. {
  786. public function InitializeContent ()
  787. {
  788. $rating = Rating::getRatingById ($this->idGet);
  789. $this->idPage = $rating->getEntryId ();
  790. $this->title =str_format (localize ("ratingword", $rating->name/2), $rating->name/2);
  791. list ($this->entryArray, $this->totalNumber) = Book::getBooksByRating ($this->idGet, $this->n);
  792. }
  793. }
  794. class PageAllBooks extends Page
  795. {
  796. public function InitializeContent ()
  797. {
  798. $this->title = localize ("allbooks.title");
  799. if (getCurrentOption ("titles_split_first_letter") == 1) {
  800. $this->entryArray = Book::getAllBooks();
  801. }
  802. else {
  803. list ($this->entryArray, $this->totalNumber) = Book::getBooks ($this->n);
  804. }
  805. $this->idPage = Book::ALL_BOOKS_ID;
  806. }
  807. }
  808. class PageAllBooksLetter extends Page
  809. {
  810. public function InitializeContent ()
  811. {
  812. list ($this->entryArray, $this->totalNumber) = Book::getBooksByStartingLetter ($this->idGet, $this->n);
  813. $this->idPage = Book::getEntryIdByLetter ($this->idGet);
  814. $count = $this->totalNumber;
  815. if ($count == -1)
  816. $count = count ($this->entryArray);
  817. $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("bookword", $count), $count), $this->idGet);
  818. }
  819. }
  820. class PageRecentBooks extends Page
  821. {
  822. public function InitializeContent ()
  823. {
  824. $this->title = localize ("recent.title");
  825. $this->entryArray = Book::getAllRecentBooks ();
  826. $this->idPage = Book::ALL_RECENT_BOOKS_ID;
  827. }
  828. }
  829. class PageQueryResult extends Page
  830. {
  831. const SCOPE_TAG = "tag";
  832. const SCOPE_RATING = "rating";
  833. const SCOPE_SERIES = "series";
  834. const SCOPE_AUTHOR = "author";
  835. const SCOPE_BOOK = "book";
  836. const SCOPE_PUBLISHER = "publisher";
  837. private function useTypeahead () {
  838. return !is_null (getURLParam ("search"));
  839. }
  840. private function searchByScope ($scope, $limit = FALSE) {
  841. $n = $this->n;
  842. $numberPerPage = NULL;
  843. $queryNormedAndUp = $this->query;
  844. if (useNormAndUp ()) {
  845. $queryNormedAndUp = normAndUp ($this->query);
  846. }
  847. if ($limit) {
  848. $n = 1;
  849. $numberPerPage = 5;
  850. }
  851. switch ($scope) {
  852. case self::SCOPE_BOOK :
  853. $array = Book::getBooksByStartingLetter ('%' . $queryNormedAndUp, $n, NULL, $numberPerPage);
  854. break;
  855. case self::SCOPE_AUTHOR :
  856. $array = Author::getAuthorsForSearch ('%' . $queryNormedAndUp);
  857. break;
  858. case self::SCOPE_SERIES :
  859. $array = Serie::getAllSeriesByQuery ($queryNormedAndUp);
  860. break;
  861. case self::SCOPE_TAG :
  862. $array = Tag::getAllTagsByQuery ($queryNormedAndUp, $n, NULL, $numberPerPage);
  863. break;
  864. case self::SCOPE_PUBLISHER :
  865. $array = Publisher::getAllPublishersByQuery ($queryNormedAndUp);
  866. break;
  867. default:
  868. $array = Book::getBooksByQuery (
  869. array ("all" => "%" . $queryNormedAndUp . "%"), $n);
  870. }
  871. return $array;
  872. }
  873. public function doSearchByCategory () {
  874. $database = GetUrlParam (DB);
  875. $out = array ();
  876. $pagequery = Base::PAGE_OPENSEARCH_QUERY;
  877. $dbArray = array ("");
  878. $d = $database;
  879. $query = $this->query;
  880. // Special case when no databases were chosen, we search on all databases
  881. if (Base::noDatabaseSelected ()) {
  882. $dbArray = Base::getDbNameList ();
  883. $d = 0;
  884. }
  885. foreach ($dbArray as $key) {
  886. if (Base::noDatabaseSelected ()) {
  887. array_push ($this->entryArray, new Entry ($key, DB . ":query:{$d}",
  888. " ", "text",
  889. array ( new LinkNavigation ("?" . DB . "={$d}")), "tt-header"));
  890. Base::getDb ($d);
  891. }
  892. foreach (array (PageQueryResult::SCOPE_BOOK,
  893. PageQueryResult::SCOPE_AUTHOR,
  894. PageQueryResult::SCOPE_SERIES,
  895. PageQueryResult::SCOPE_TAG,
  896. PageQueryResult::SCOPE_PUBLISHER) as $key) {
  897. if (in_array($key, getCurrentOption ('ignored_categories'))) {
  898. continue;
  899. }
  900. $array = $this->searchByScope ($key, TRUE);
  901. $i = 0;
  902. if (count ($array) == 2 && is_array ($array [0])) {
  903. $total = $array [1];
  904. $array = $array [0];
  905. } else {
  906. $total = count($array);
  907. }
  908. if ($total > 0) {
  909. // Comment to help the perl i18n script
  910. // str_format (localize("bookword", count($array))
  911. // str_format (localize("authorword", count($array))
  912. // str_format (localize("seriesword", count($array))
  913. // str_format (localize("tagword", count($array))
  914. // str_format (localize("publisherword", count($array))
  915. array_push ($this->entryArray, new Entry (str_format (localize ("search.result.{$key}"), $this->query), DB . ":query:{$d}:{$key}",
  916. str_format (localize("{$key}word", $total), $total), "text",
  917. array ( new LinkNavigation ("?page={$pagequery}&query={$query}&db={$d}&scope={$key}")),
  918. Base::noDatabaseSelected () ? "" : "tt-header", $total));
  919. }
  920. if (!Base::noDatabaseSelected () && $this->useTypeahead ()) {
  921. foreach ($array as $entry) {
  922. array_push ($this->entryArray, $entry);
  923. $i++;
  924. if ($i > 4) { break; };
  925. }
  926. }
  927. }
  928. $d++;
  929. if (Base::noDatabaseSelected ()) {
  930. Base::clearDb ();
  931. }
  932. }
  933. return $out;
  934. }
  935. public function InitializeContent ()
  936. {
  937. $scope = getURLParam ("scope");
  938. if (empty ($scope)) {
  939. $this->title = str_format (localize ("search.result"), $this->query);
  940. } else {
  941. // Comment to help the perl i18n script
  942. // str_format (localize ("search.result.author"), $this->query)
  943. // str_format (localize ("search.result.tag"), $this->query)
  944. // str_format (localize ("search.result.series"), $this->query)
  945. // str_format (localize ("search.result.book"), $this->query)
  946. // str_format (localize ("search.result.publisher"), $this->query)
  947. $this->title = str_format (localize ("search.result.{$scope}"), $this->query);
  948. }
  949. $crit = "%" . $this->query . "%";
  950. // Special case when we are doing a search and no database is selected
  951. if (Base::noDatabaseSelected () && !$this->useTypeahead ()) {
  952. $i = 0;
  953. foreach (Base::getDbNameList () as $key) {
  954. Base::clearDb ();
  955. list ($array, $totalNumber) = Book::getBooksByQuery (array ("all" => $crit), 1, $i, 1);
  956. array_push ($this->entryArray, new Entry ($key, DB . ":query:{$i}",
  957. str_format (localize ("bookword", $totalNumber), $totalNumber), "text",
  958. array ( new LinkNavigation ("?" . DB . "={$i}&page=9&query=" . $this->query)), "", $totalNumber));
  959. $i++;
  960. }
  961. return;
  962. }
  963. if (empty ($scope)) {
  964. $this->doSearchByCategory ();
  965. return;
  966. }
  967. $array = $this->searchByScope ($scope);
  968. if (count ($array) == 2 && is_array ($array [0])) {
  969. list ($this->entryArray, $this->totalNumber) = $array;
  970. } else {
  971. $this->entryArray = $array;
  972. }
  973. }
  974. }
  975. class PageBookDetail extends Page
  976. {
  977. public function InitializeContent ()
  978. {
  979. $this->book = Book::getBookById ($this->idGet);
  980. $this->title = $this->book->title;
  981. }
  982. }
  983. class PageAbout extends Page
  984. {
  985. public function InitializeContent ()
  986. {
  987. $this->title = localize ("about.title");
  988. }
  989. }
  990. class PageCustomize extends Page
  991. {
  992. private function isChecked ($key, $testedValue = 1) {
  993. $value = getCurrentOption ($key);
  994. if (is_array ($value)) {
  995. if (in_array ($testedValue, $value)) {
  996. return "checked='checked'";
  997. }
  998. } else {
  999. if ($value == $testedValue) {
  1000. return "checked='checked'";
  1001. }
  1002. }
  1003. return "";
  1004. }
  1005. private function isSelected ($key, $value) {
  1006. if (getCurrentOption ($key) == $value) {
  1007. return "selected='selected'";
  1008. }
  1009. return "";
  1010. }
  1011. private function getStyleList () {
  1012. $result = array ();
  1013. foreach (glob ("templates/" . getCurrentTemplate () . "/styles/style-*.css") as $filename) {
  1014. if (preg_match ('/styles\/style-(.*?)\.css/', $filename, $m)) {
  1015. array_push ($result, $m [1]);
  1016. }
  1017. }
  1018. return $result;
  1019. }
  1020. public function InitializeContent ()
  1021. {
  1022. $this->title = localize ("customize.title");
  1023. $this->entryArray = array ();
  1024. $ignoredBaseArray = array (PageQueryResult::SCOPE_AUTHOR,
  1025. PageQueryResult::SCOPE_TAG,
  1026. PageQueryResult::SCOPE_SERIES,
  1027. PageQueryResult::SCOPE_PUBLISHER,
  1028. PageQueryResult::SCOPE_RATING,
  1029. "language");
  1030. $content = "";
  1031. array_push ($this->entryArray, new Entry ("Template", "",
  1032. "<span style='cursor: pointer;' onclick='$.cookie(\"template\", \"bootstrap\", { expires: 365 });window.location=$(\".headleft\").attr(\"href\");'>Click to switch to Bootstrap</span>", "text",
  1033. array ()));
  1034. if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
  1035. $content .= '<select id="style" onchange="updateCookie (this);">';
  1036. foreach ($this-> getStyleList () as $filename) {
  1037. $content .= "<option value='{$filename}' " . $this->isSelected ("style", $filename) . ">{$filename}</option>";
  1038. }
  1039. $content .= '</select>';
  1040. } else {
  1041. foreach ($this-> getStyleList () as $filename) {
  1042. $content .= "<input type='radio' onchange='updateCookieFromCheckbox (this);' id='style-{$filename}' name='style' value='{$filename}' " . $this->isChecked ("style", $filename) . " /><label for='style-{$filename}'> {$filename} </label>";
  1043. }
  1044. }
  1045. array_push ($this->entryArray, new Entry (localize ("customize.style"), "",
  1046. $content, "text",
  1047. array ()));
  1048. if (!useServerSideRendering ()) {
  1049. $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="use_fancyapps" ' . $this->isChecked ("use_fancyapps") . ' />';
  1050. array_push ($this->entryArray, new Entry (localize ("customize.fancybox"), "",
  1051. $content, "text",
  1052. array ()));
  1053. }
  1054. $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]+$" />';
  1055. array_push ($this->entryArray, new Entry (localize ("customize.paging"), "",
  1056. $content, "text",
  1057. array ()));
  1058. $content = '<input type="text" onchange="updateCookie (this);" id="email" value="' . getCurrentOption ("email") . '" />';
  1059. array_push ($this->entryArray, new Entry (localize ("customize.email"), "",
  1060. $content, "text",
  1061. array ()));
  1062. $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="html_tag_filter" ' . $this->isChecked ("html_tag_filter") . ' />';
  1063. array_push ($this->entryArray, new Entry (localize ("customize.filter"), "",
  1064. $content, "text",
  1065. array ()));
  1066. $content = "";
  1067. foreach ($ignoredBaseArray as $key) {
  1068. $keyPlural = preg_replace ('/(ss)$/', 's', $key . "s");
  1069. $content .= '<input type="checkbox" name="ignored_categories[]" onchange="updateCookieFromCheckboxGroup (this);" id="ignored_categories_' . $key . '" ' . $this->isChecked ("ignored_categories", $key) . ' > ' . localize ("{$keyPlural}.title") . '</input> ';
  1070. }
  1071. array_push ($this->entryArray, new Entry (localize ("customize.ignored"), "",
  1072. $content, "text",
  1073. array ()));
  1074. }
  1075. }
  1076. abstract class Base
  1077. {
  1078. const PAGE_INDEX = "index";
  1079. const PAGE_ALL_AUTHORS = "1";
  1080. const PAGE_AUTHORS_FIRST_LETTER = "2";
  1081. const PAGE_AUTHOR_DETAIL = "3";
  1082. const PAGE_ALL_BOOKS = "4";
  1083. const PAGE_ALL_BOOKS_LETTER = "5";
  1084. const PAGE_ALL_SERIES = "6";
  1085. const PAGE_SERIE_DETAIL = "7";
  1086. const PAGE_OPENSEARCH = "8";
  1087. const PAGE_OPENSEARCH_QUERY = "9";
  1088. const PAGE_ALL_RECENT_BOOKS = "10";
  1089. const PAGE_ALL_TAGS = "11";
  1090. const PAGE_TAG_DETAIL = "12";
  1091. const PAGE_BOOK_DETAIL = "13";
  1092. const PAGE_ALL_CUSTOMS = "14";
  1093. const PAGE_CUSTOM_DETAIL = "15";
  1094. const PAGE_ABOUT = "16";
  1095. const PAGE_ALL_LANGUAGES = "17";
  1096. const PAGE_LANGUAGE_DETAIL = "18";
  1097. const PAGE_CUSTOMIZE = "19";
  1098. const PAGE_ALL_PUBLISHERS = "20";
  1099. const PAGE_PUBLISHER_DETAIL = "21";
  1100. const PAGE_ALL_RATINGS = "22";
  1101. const PAGE_RATING_DETAIL = "23";
  1102. const COMPATIBILITY_XML_ALDIKO = "aldiko";
  1103. private static $db = NULL;
  1104. public static function isMultipleDatabaseEnabled () {
  1105. global $config;
  1106. return is_array ($config['calibre_directory']);
  1107. }
  1108. public static function useAbsolutePath () {
  1109. global $config;
  1110. $path = self::getDbDirectory();
  1111. return preg_match ('/^\//', $path) || // Linux /
  1112. preg_match ('/^\w\:/', $path); // Windows X:
  1113. }
  1114. public static function noDatabaseSelected () {
  1115. return self::isMultipleDatabaseEnabled () && is_null (GetUrlParam (DB));
  1116. }
  1117. public static function getDbList () {
  1118. global $config;
  1119. if (self::isMultipleDatabaseEnabled ()) {
  1120. return $config['calibre_directory'];
  1121. } else {
  1122. return array ("" => $config['calibre_directory']);
  1123. }
  1124. }
  1125. public static function getDbNameList () {
  1126. global $config;
  1127. if (self::isMultipleDatabaseEnabled ()) {
  1128. return array_keys ($config['calibre_directory']);
  1129. } else {
  1130. return array ("");
  1131. }
  1132. }
  1133. public static function getDbName ($database = NULL) {
  1134. global $config;
  1135. if (self::isMultipleDatabaseEnabled ()) {
  1136. if (is_null ($database)) $database = GetUrlParam (DB, 0);
  1137. $array = array_keys ($config['calibre_directory']);
  1138. return $array[$database];
  1139. }
  1140. return "";
  1141. }
  1142. public static function getDbDirectory ($database = NULL) {
  1143. global $config;
  1144. if (self::isMultipleDatabaseEnabled ()) {
  1145. if (is_null ($database)) $database = GetUrlParam (DB, 0);
  1146. $array = array_values ($config['calibre_directory']);
  1147. return $array[$database];
  1148. }
  1149. return $config['calibre_directory'];
  1150. }
  1151. public static function getDbFileName ($database = NULL) {
  1152. return self::getDbDirectory ($database) .'metadata.db';
  1153. }
  1154. private static function error () {
  1155. if (php_sapi_name() != "cli") {
  1156. header("location: checkconfig.php?err=1");
  1157. }
  1158. throw new Exception('Database not found.');
  1159. }
  1160. public static function getDb ($database = NULL) {
  1161. if (is_null (self::$db)) {
  1162. try {
  1163. if (is_readable (self::getDbFileName ($database))) {
  1164. self::$db = new PDO('sqlite:'. self::getDbFileName ($database));
  1165. if (useNormAndUp ()) {
  1166. self::$db->sqliteCreateFunction ('normAndUp', 'normAndUp', 1);
  1167. }
  1168. } else {
  1169. self::error ();
  1170. }
  1171. } catch (Exception $e) {
  1172. self::error ();
  1173. }
  1174. }
  1175. return self::$db;
  1176. }
  1177. public static function checkDatabaseAvailability () {
  1178. if (self::noDatabaseSelected ()) {
  1179. for ($i = 0; $i < count (self::getDbList ()); $i++) {
  1180. self::getDb ($i);
  1181. self::clearDb ();
  1182. }
  1183. } else {
  1184. self::getDb ();
  1185. }
  1186. return true;
  1187. }
  1188. public static function clearDb () {
  1189. self::$db = NULL;
  1190. }
  1191. public static function executeQuerySingle ($query, $database = NULL) {
  1192. return self::getDb ($database)->query($query)->fetchColumn();
  1193. }
  1194. public static function getCountGeneric($table, $id, $pageId, $numberOfString = NULL) {
  1195. if (!$numberOfString) {
  1196. $numberOfString = $table . ".alphabetical";
  1197. }
  1198. $count = self::executeQuerySingle ('select count(*) from ' . $table);
  1199. if ($count == 0) return NULL;
  1200. $entry = new Entry (localize($table . ".title"), $id,
  1201. str_format (localize($numberOfString, $count), $count), "text",
  1202. array ( new LinkNavigation ("?page=".$pageId)), "", $count);
  1203. return $entry;
  1204. }
  1205. public static function getEntryArrayWithBookNumber ($query, $columns, $params, $category) {
  1206. list (, $result) = self::executeQuery ($query, $columns, "", $params, -1);
  1207. $entryArray = array();
  1208. while ($post = $result->fetchObject ())
  1209. {
  1210. $instance = new $category ($post);
  1211. if (property_exists($post, "sort")) {
  1212. $title = $post->sort;
  1213. } else {
  1214. $title = $post->name;
  1215. }
  1216. array_push ($entryArray, new Entry ($title, $instance->getEntryId (),
  1217. str_format (localize("bookword", $post->count), $post->count), "text",
  1218. array ( new LinkNavigation ($instance->getUri ())), "", $post->count));
  1219. }
  1220. return $entryArray;
  1221. }
  1222. public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL, $numberPerPage = NULL) {
  1223. $totalResult = -1;
  1224. if (useNormAndUp ()) {
  1225. $query = preg_replace("/upper/", "normAndUp", $query);
  1226. $columns = preg_replace("/upper/", "normAndUp", $columns);
  1227. }
  1228. if (is_null ($numberPerPage)) {
  1229. $numberPerPage = getCurrentOption ("max_item_per_page");
  1230. }
  1231. if ($numberPerPage != -1 && $n != -1)
  1232. {
  1233. // First check total number of results
  1234. $result = self::getDb ($database)->prepare (str_format ($query, "count(*)", $filter));
  1235. $result->execute ($params);
  1236. $totalResult = $result->fetchColumn ();
  1237. // Next modify the query and params
  1238. $query .= " limit ?, ?";
  1239. array_push ($params, ($n - 1) * $numberPerPage, $numberPerPage);
  1240. }
  1241. $result = self::getDb ($database)->prepare(str_format ($query, $columns, $filter));
  1242. $result->execute ($params);
  1243. return array ($totalResult, $result);
  1244. }
  1245. }