Calibre Remote - PHP5 Class


/ Published in: PHP
Save to your folder(s)

forms part of our Calibre Remote Web App. basically an alternate gui for calibre ebooks content-server.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class calibre {
  4.  
  5. public $settings = array(
  6. "server" => array(
  7. // calibre server details
  8. "host" => "minky.mine.nu",
  9. "port" => "8080",
  10. "username" => "",
  11. "password" => "",
  12. ),
  13. // calibre server tokens/values
  14. "start" => 0,
  15. "num" => 20,
  16. "order" => "ascending",
  17. "sort" => "title",
  18. "search" => "",
  19. );
  20.  
  21. // .. curl data
  22. private $curl_meta, $curl_data;
  23.  
  24. // xml, object (simplexml) data
  25. private $xml, $object;
  26.  
  27. // html data
  28. private $paginate;
  29.  
  30. private $authors = array();
  31. private $series = array();
  32.  
  33. public function __construct() {}
  34.  
  35. public function debug() {
  36.  
  37. echo "<pre>";
  38. print_r($this);
  39. echo "</pre>";
  40. die;
  41.  
  42. }
  43.  
  44. private function fetch_query_string() {
  45.  
  46. $data = $this->settings;
  47.  
  48. unset($data["server"]);
  49.  
  50. return "?". http_build_query($data, "&amp;");
  51.  
  52. }
  53.  
  54. private function parse_response() {
  55.  
  56. try {
  57.  
  58. $this->object = new SimpleXMLElement($this->curl_data);
  59.  
  60. $this->xml = $this->object->asXML();
  61.  
  62. } catch(Exception $e) {
  63. throw new CalibreException($e->getMessage());
  64. }
  65.  
  66. }
  67.  
  68. public function set($settings) {
  69.  
  70. if(is_array($settings)) {
  71.  
  72. foreach($settings AS $key => $value) {
  73.  
  74. if(empty($key) || empty($value)) continue;
  75.  
  76. $this->settings[$key] = $value;
  77.  
  78. }
  79. }
  80.  
  81. return $this;
  82.  
  83. }
  84.  
  85. public function fetch_library() {
  86.  
  87. if(empty($this->settings["server"]["host"]) || empty($this->settings["server"]["port"]))
  88. throw new CalibreException('No Server/Port Defined! <a class="button define server" href="#define">Define Server Settings</a>');
  89.  
  90. $qs = $this->fetch_query_string();
  91.  
  92. $curl = curl_init();
  93.  
  94. if(!empty($this->settings["server"]["username"]) && !empty($this->settings["server"]["password"])) {
  95.  
  96. curl_setopt($curl, CURLOPT_USERPWD, "{$this->settings["server"]["username"]}:{$this->settings["server"]["password"]}");
  97. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  98.  
  99. }
  100.  
  101. CURLOPT_RETURNTRANSFER => TRUE,
  102. CURLOPT_HEADER => 0,
  103. CURLOPT_USERAGENT => "",
  104. CURLOPT_URL => "http://{$this->settings["server"]["host"]}/library{$qs}",
  105. CURLOPT_PORT => $this->settings["server"]["port"],
  106. CURLOPT_TIMEOUT => 3,
  107. ));
  108.  
  109. $this->curl_data = curl_exec($curl);
  110. $this->curl_meta = curl_getinfo($curl);
  111.  
  112. curl_close($curl);
  113.  
  114. if($this->curl_data === FALSE)
  115. throw new CalibreException('cURL Data is empty! Is the Server running? <a class="button define server" href="#define">Define Server Settings</a>');
  116.  
  117. try {
  118. $this->parse_response();
  119. } catch(CalibreException $e) {
  120. throw new CalibreException($e->getMessage());
  121. }
  122.  
  123. return $this;
  124.  
  125. }
  126.  
  127. public function as_json() {
  128. return json_encode($this->object);
  129. }
  130.  
  131. public function as_object() {
  132. return $this->object;
  133. }
  134.  
  135. public function as_xml() {
  136. return $this->xml;
  137. }
  138.  
  139. private function page_select($page_count, $current = 1) {
  140.  
  141. $options = array();
  142.  
  143. for($i = 1; $i <= $page_count; $i++) {
  144.  
  145. $selected = $i == $current ? "selected" : "" ;
  146.  
  147. $options[] = '<option value="'. $i .'" '. $selected .'>'. $i .'</option>';
  148.  
  149. }
  150.  
  151. return '<select class="paginate">'. implode("", $options) .'</select>';
  152.  
  153. }
  154.  
  155. public function paginate($total) {
  156.  
  157. $page_count = ceil($total / $this->settings["num"]);
  158.  
  159. $current = ceil($this->settings["start"] / $this->settings["num"]);
  160. $current = empty($current) ? 1 : $current ;
  161.  
  162. $prev = $current == 1 ? 1 : $current - 1 ;
  163. $next = $current == $page_count ? $page_count : $current + 1 ;
  164.  
  165. $links = array();
  166.  
  167. $links[] = '
  168. <section class="paginate">
  169. <ul>
  170. <li class="prev">
  171. <a href="page/'. $prev .'" class="button">&laquo; Prev</a>
  172. </li>
  173. <li class="current">'. $this->page_select($page_count, $current) .' of '. $page_count .'</li>
  174. <li class="next">
  175. <a href="page/'. $next .'" class="button">Next &raquo;</a>
  176. </li>
  177. </ul>
  178. </section>
  179. ';
  180.  
  181. $this->paginate = implode("", $links);
  182.  
  183. }
  184.  
  185. public function parse_html($book) {
  186.  
  187. $data = array();
  188.  
  189. foreach($book->attributes() AS $key => $value) {
  190.  
  191. if($key == "formats") {
  192. $data[$key] = $this->parse_formats((string) $value, $data["title"], $data["id"]);
  193. } elseif($key == "authors") {
  194. $data[$key] = $this->parse_authors((string) $value);
  195. } elseif($key == "series") {
  196. $data[$key] = $this->parse_series((string) $value);
  197. } else {
  198. $data[$key] = (string) $value;
  199. }
  200. }
  201.  
  202. return $data;
  203.  
  204. }
  205.  
  206. private function parse_formats($format, $title, $id) {
  207.  
  208. $formats = stristr($format, ",") ? explode(",", $format) : array($format) ;
  209.  
  210. $output = array();
  211.  
  212. foreach($formats AS $f)
  213. $output[] = '<a href="http://'. $this->settings["server"]["host"] .':'. $this->settings["server"]["port"] .'/get/'. strtolower($f) .'/'. $title .'_'. $id .'.'. strtolower($f) .'" class="file button">'. strtoupper($f) .'</a>';
  214.  
  215. return implode("", $output);
  216.  
  217. }
  218.  
  219. private function parse_authors($authors) {
  220.  
  221. $authors = stristr($authors, ",") ? explode(",", $authors) : array($authors) ;
  222.  
  223. $output = array();
  224.  
  225. foreach($authors AS $author)
  226. $output[] = ucwords(trim($author));
  227.  
  228. $this->authors = array_merge($this->authors, $output);
  229.  
  230. return implode("", $output);
  231.  
  232. }
  233.  
  234. private function parse_series($series) {
  235. return empty($series) ? "Unknown" : ucwords($series) ;
  236. }
  237.  
  238. public function as_html() {
  239.  
  240. $books = array();
  241.  
  242. $attrib = $this->object->attributes();
  243.  
  244. $this->paginate((int) $attrib->total);
  245.  
  246. foreach($this->object->children() AS $key => $value) {
  247.  
  248. $book = $this->parse_html($value);
  249.  
  250. $this->series[] = $book["series"];
  251.  
  252. $books[] = '
  253. <article class="book">
  254.  
  255. <header><h2>'. $book["title"] .'</h2></header>
  256.  
  257. <section class="content">
  258.  
  259. <section class="details float-left">
  260.  
  261. <ul>
  262.  
  263. <li class="author">
  264. <span class="label">Author</span>
  265. <span class="data">'. $book["authors"] .'</span>
  266. </li>
  267. <li class="author">
  268. <span class="label">Series</span>
  269. <span class="data">'. $book["series"] .'</span>
  270. </li>
  271. <li class="formats">
  272. <span class="label">Download</span>
  273. <span class="data">'. $book["formats"] .'</span>
  274. </li>
  275.  
  276. </ul>
  277.  
  278. </section>
  279.  
  280. <section class="image float-right">
  281.  
  282. <img src="http://'. $this->settings["server"]["host"] .':'. $this->settings["server"]["port"] .'/get/cover/'. $book["id"] .'" alt="Cover Image" />
  283.  
  284. </section>
  285.  
  286. </section>
  287.  
  288. </article>
  289. ';
  290. }
  291.  
  292. $paginate = $this->paginate;
  293.  
  294. return $paginate . implode("", $books) . $paginate;
  295.  
  296. }
  297.  
  298. }
  299.  
  300. class CalibreException extends Exception {
  301. public function __construct($message, $code = 0) {
  302. parent::__construct($message, $code);
  303. }
  304. }
  305. //
  306. $calibre = new calibre;
  307. //
  308. $sort = empty($_GET['sort']) ? $calibre->settings["sort"] : $_GET['sort'] ;
  309. $order = empty($_GET['order']) ? $calibre->settings["order"] : $_GET['order'] ;
  310. $search = empty($_GET['search']) ? "" : $_GET['search'] ;
  311. //
  312. $start = empty($_GET['start']) ? 0 : (int) $_GET['start'] ;
  313. $per_page = $calibre->settings["num"];
  314.  
  315. $server = array(
  316. "host" => $_GET['host'],
  317. "port" => $_GET['port'],
  318. "username" => $_GET['username'],
  319. "password" => $_GET['password'],
  320. );
  321. //
  322. $settings = array(
  323. "start" => $start * $per_page,
  324. "num" => $per_page,
  325. "sort" => $sort,
  326. "order" => $order,
  327. "search"=> $search,
  328. "server"=> $server,
  329. );
  330.  
  331. // try getting library as html
  332. try {
  333. echo $calibre->set($settings)->fetch_library()->as_html();
  334. } catch(CalibreException $e) {
  335. echo '<article class="error">'. $e->getMessage() .'</article>';
  336. }
  337.  
  338. die;
  339.  
  340. //$calibre->debug(); // very long page
  341.  
  342. ?>

URL: http://www.pixaweb.co.uk/calibre/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.