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.

1312 lines
47KB

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