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(); $foldermatch = 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'])); if ( !in_array($row['path'], $foldermatch) ) $foldermatch[] = $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; if ( !in_array($row['path'], $foldermatch) ) { $foldermatch[] = $row['path']; $item = array(); $item['folder'] = true; $item['fullpath'] = $row['path']; $item['displayname'] = htmlspecialchars(basename($row['path'])); $data['contents'][] = $item; } $item = array(); $item['folder'] = false; $item['fullpath'] = $row['path'] . "/" . $row['filename']; $item['displayname'] = htmlspecialchars(basename($row['filename'])); $item['id'] = $row['id']; $data['contents'][] = $item; } } sendResponse($data); // vim: ts=3 sw=3 et: