97 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
require '../header.php';
 | 
						|
require '../variables.php';
 | 
						|
require '../functions.php';
 | 
						|
 | 
						|
require_login();
 | 
						|
 | 
						|
$data = array();
 | 
						|
$data['bookdir'] = $_SESSION['bookdir'];
 | 
						|
$data['bookdirname'] = basename($_SESSION['bookdir']);
 | 
						|
$data['kindlemail'] = "";
 | 
						|
$cankindle = false;
 | 
						|
 | 
						|
$searching = false;
 | 
						|
$searchfor = "";
 | 
						|
if ( isset($_REQUEST['search']) && ($_REQUEST['search'] != "") ) {
 | 
						|
   $searching = true;
 | 
						|
   $searchfor = strtolower(str_replace(["\"", "'", "/", "\\"], "", $_REQUEST['search']));
 | 
						|
   $searchfor = "%{$searchfor}%";
 | 
						|
}
 | 
						|
 | 
						|
$query = "SELECT email FROM amazon WHERE username=:username";
 | 
						|
$sth = $globaldbh->prepare($query);
 | 
						|
$sth->bindValue(":username", $_SESSION['username'], PDO::PARAM_STR);
 | 
						|
$sth->execute();
 | 
						|
if ( $row = $sth->fetch() ) {
 | 
						|
   $data['kindlemail'] = $row['email'];
 | 
						|
   $cankindle = true;
 | 
						|
}
 | 
						|
 | 
						|
$data['contents'] = array();
 | 
						|
 | 
						|
if ( !$searching ) {
 | 
						|
   $contents = glob(BOOKDIR . $_SESSION['bookdir'] . "/*");
 | 
						|
   if ( $_SESSION['bookdir'] == "/" ) {
 | 
						|
      $query = "SELECT DISTINCT(path), '' AS filename FROM books WHERE path <> '/' ORDER BY path";
 | 
						|
      $sth = $globaldbh->prepare($query);
 | 
						|
   } else {
 | 
						|
      $query = "SELECT id, path, filename FROM books WHERE path=:path ORDER BY path, filename";
 | 
						|
      $sth = $globaldbh->prepare($query);
 | 
						|
      $sth->bindValue(":path", $_SESSION['bookdir'], PDO::PARAM_STR);
 | 
						|
   }
 | 
						|
   $sth->execute();
 | 
						|
   while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
 | 
						|
      $item = array();
 | 
						|
      if ( $row['filename'] == "" ) {
 | 
						|
         $item['folder'] = true;
 | 
						|
         $item['fullpath'] = $row['path'];
 | 
						|
         $item['displayname'] = htmlspecialchars(basename($row['path']));
 | 
						|
      } else {
 | 
						|
         $item['folder'] = false;
 | 
						|
         $item['fullpath'] = $row['path'] . "/" . $row['filename'];
 | 
						|
         $item['displayname'] = htmlspecialchars(basename($row['filename']));
 | 
						|
         $item['id'] = $row['id'];
 | 
						|
         if ( $cankindle && (substr($row['filename'], -4) == "epub") ) {
 | 
						|
            $item['cankindle'] = true;
 | 
						|
         } else {
 | 
						|
            $item['cankindle'] = false;
 | 
						|
         }
 | 
						|
      }
 | 
						|
      $data['contents'][] = $item;
 | 
						|
   }
 | 
						|
} else {
 | 
						|
   $query = "SELECT DISTINCT(path) FROM books WHERE LOWER(path) LIKE :searchfor ORDER BY path";
 | 
						|
   $sth = $globaldbh->prepare($query);
 | 
						|
   $sth->bindValue(":searchfor", $searchfor, PDO::PARAM_STR);
 | 
						|
   $sth->execute();
 | 
						|
   while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
 | 
						|
      if ( $row['path'] == "/" ) continue;
 | 
						|
      $item = array();
 | 
						|
      $item['folder'] = true;
 | 
						|
      $item['fullpath'] = $row['path'];
 | 
						|
      $item['displayname'] = htmlspecialchars(basename($row['path']));
 | 
						|
      $data['contents'][] = $item;
 | 
						|
   }
 | 
						|
   $query = "SELECT id, path, filename FROM books WHERE LOWER(filename) LIKE :searchfor ORDER BY path, filename";
 | 
						|
   $sth = $globaldbh->prepare($query);
 | 
						|
   $sth->bindValue(":searchfor", $searchfor, PDO::PARAM_STR);
 | 
						|
   $sth->execute();
 | 
						|
   while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
 | 
						|
      if ( $row['path'] == "/" ) continue;
 | 
						|
      $item = array();
 | 
						|
      $item['folder'] = false;
 | 
						|
      $item['fullpath'] = $row['path'] . "/" . $row['filename'];
 | 
						|
      $item['displayname'] = htmlspecialchars(basename($row['filename']));
 | 
						|
      $item['id'] = $row['id'];
 | 
						|
      $data['contents'][] = $item;
 | 
						|
   }
 | 
						|
}
 | 
						|
 | 
						|
header('Content-Type: application/json');
 | 
						|
echo json_encode($data);
 | 
						|
exit();
 | 
						|
 | 
						|
// vim: ts=3 sw=3 et:
 |