1
0
Fork 0
mirror of https://github.com/Yetangitu/owncloud-apps.git synced 2025-10-02 14:49:17 +02:00

Initial commit

This commit is contained in:
frankdelange 2014-12-09 22:40:49 +01:00
parent 26dd81b2df
commit 993289d86b
51 changed files with 16863 additions and 0 deletions

View file

@ -0,0 +1,21 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
$l = new \OC_L10N('files_opds');
\OCP\JSON::checkLoggedIn();
\OCP\JSON::callCheck();
Bookshelf::clear();
\OCP\JSON::success(array( "data" => array( "message" => $l->t("Bookshelf cleared"))));

View file

@ -0,0 +1,46 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
\OCP\JSON::callCheck();
\OCP\JSON::checkLoggedIn();
$l = new \OC_L10N('files_opds');
$opdsEnable = isset($_POST['opdsEnable']) ? $_POST['opdsEnable'] : 'false';
$rootPath = isset($_POST['rootPath']) ? $_POST['rootPath'] : null;
$fileTypes = isset($_POST['fileTypes']) ? $_POST['fileTypes'] : '';
if (!is_null($rootPath)){
if (\OC\Files\Filesystem::file_exists($rootPath) === false ){
\OCP\JSON::error(
array(
'data' => array('message'=> $l->t('Directory does not exist!'))
)
);
} else {
Config::set('root_path', $rootPath);
\OCP\JSON::success(
array(
'data' => array('message'=> $l->t('Settings updated successfully.'))
)
);
}
Config::set('enable', $opdsEnable);
Config::set('file_types', $fileTypes);
Config::set('id', Util::genUuid());
exit();
}
exit();

View file

@ -0,0 +1,7 @@
<?php
$l = OC_L10N::get('files');
\OCP\App::registerPersonal('files_opds', 'personal');

View file

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<info>
<id>files_opds</id>
<name>OPDS catalog</name>
<description>Personal OPDS catalog</description>
<licence>AGPL</licence>
<version>0.1</version>
<author>Frank de Lange</author>
<requiremin>7.0</requiremin>
<shipped>true</shipped>
<default_enable/>
</info>

57
files_opds/index.php Normal file
View file

@ -0,0 +1,57 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
\OCP\App::checkAppEnabled('files_opds');
/* Enable login through basic auth, using normal OC username/password
* This is required because opds clients do not support the normal
* OC login process
*/
if (Util::authenticateUser() === false) {
Util::changeHttpStatus(401);
exit;
}
\OCP\User::checkLoggedIn();
/* Refuse access if user disabled opds support */
if (Config::get('enable', 'false') === 'false') {
Util::changeHttpStatus(403);
exit;
}
/* id defaults to 'root' (meaning 'serve root feed') */
$id = isset($_GET['id']) ? $_GET['id'] : 'root';
$dir = \OC\Files\Filesystem::normalizePath(\OC\Files\Filesystem::getPath($id));
$root = Config::get('root_path', '/Library');
/* Only feed files descending from designated root directory */
if (!(Files::isChild($root,$dir))) {
$dir = $root;
}
$dirInfo = \OC\Files\Filesystem::getFileInfo($dir);
/* If requested resource is a file, serve it, otherwise produce opds feed */
switch ($dirInfo->getType()) {
case 'file':
Util::serveFile($dir,$id);
break;
case 'dir':
Util::serveFeed($dir, $id);
break;
default:
Util::logWarn("I don't know how to handle files of type " . $dirInfo->getType());
break;
}

45
files_opds/js/personal.js Normal file
View file

@ -0,0 +1,45 @@
$(document).ready(function(){
// clear bookshelf
$('#opds-clear-bookshelf').on("click", function() {
$('#opds-really-clear-bookshelf,#opds-dont-clear-bookshelf').show();
});
$('#opds-dont-clear-bookshelf').on("click", function() {
$('#opds-really-clear-bookshelf,#opds-dont-clear-bookshelf').hide();
});
$('#opds-really-clear-bookshelf').on("click", function() {
$.post(OC.filePath('files_opds','ajax','clear_bookshelf.php'), {},
function(result){
if(result) {
OC.msg.finishedSaving('#opds-personal .clr', result);
$('#opds-book-count').hide();
}
});
$('#opds-really-clear-bookshelf,#opds-dont-clear-bookshelf').hide();
});
// save settings
var opdsSettings = {
save : function() {
var opdsEnable = document.getElementById('opds-enable').checked ? 'true' : 'false';
var data = {
opdsEnable : opdsEnable,
rootPath : $('#opds-root-path').val(),
fileTypes : $('#opds-file-types').val()
};
OC.msg.startSaving('#opds-personal .msg');
$.post(OC.filePath('files_opds', 'ajax', 'personal.php'), data, opdsSettings.afterSave);
},
afterSave : function(data){
OC.msg.finishedSaving('#opds-personal .msg', data);
}
};
$('#opds-root-path,#opds-file-types').blur(opdsSettings.save);
$('#opds-root-path,#opds-file-types').keypress(function( event ) {
if (event.which == 13) {
event.preventDefault();
opdsSettings.save();
}
});
$('#opds-enable').on("change", opdsSettings.save);
});

View file

@ -0,0 +1,65 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
/**
* Bookshelf class for publishing as OPDS
*
* This implements a 'personal bookshelf', listing books
* which have been downloaded from the OPDS feed.
*/
class Bookshelf
{
/**
* @brief add book to personal bookshelf
*
* @param int $id book to add to bookshelf
*/
public static function add($id) {
$bookshelf = json_decode(Config::get('bookshelf', ''), true);
if(!isset($bookshelf[$id])) {
$bookshelf[$id]=time();
}
Config::set('bookshelf', json_encode($bookshelf));
}
/**
* @brief clear personal bookshelf
*/
public static function clear() {
Config::set('bookshelf', '');
}
/**
* @brief return number of books on personal bookshelf
* @return int number of books
*/
public static function count() {
return substr_count(Config::get('bookshelf', ''), ':');
}
/**
* @brief list bookshelf contents
*
* @return array of FileInfo[], sorted by time added
*/
public static function get() {
$files = array();
$bookshelf = json_decode(Config::get('bookshelf', ''), true);
arsort($bookshelf);
while (list($id, $time) = each($bookshelf)) {
array_push($files, \OC\Files\Filesystem::getFileInfo(\OC\Files\Filesystem::normalizePath(\OC\Files\Filesystem::getPath($id))));
}
return $files;
}
}

41
files_opds/lib/config.php Normal file
View file

@ -0,0 +1,41 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
/**
* Config class for publishing as OPDS
*/
class Config
{
/**
* @brief get user config value
*
* @param string $key value to retrieve
* @param string $default default value to use
* @return string retrieved value or default
*/
public static function get($key, $default) {
return \OCP\Config::getUserValue(\OCP\User::getUser(), 'files_opds', $key, $default);
}
/**
* @brief set user config value
*
* @param string $key key for value to change
* @param string $value value to use
* @return bool success
*/
public static function set($key, $value) {
return \OCP\Config::setUserValue(\OCP\User::getUser(), 'files_opds', $key, $value);
}
}

67
files_opds/lib/files.php Normal file
View file

@ -0,0 +1,67 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
/**
* Files class, extendes \OCA\Files, tailored for OPDS
*/
class Files extends \OCA\Files\Helper
{
/**
* Formats the file info to be returned as OPDS to the client.
*
* @param \OCP\Files\FileInfo $i
* @return array formatted file info
*/
public static function formatFileInfo($i) {
$entry = array();
$entry['id'] = $i['fileid'];
$entry['mtime'] = $i['mtime'] * 1000;
$entry['icon'] = self::determineIcon($i);
$entry['name'] = $i->getName();
$entry['mimetype'] = $i['mimetype'];
$entry['size'] = $i['size'];
$entry['type'] = $i['type'];
return $entry;
}
/**
* Format file info for OPDS feed
* @param \OCP\Files\FileInfo[] $fileInfos file infos
*/
public static function formatFileInfos($fileInfos) {
$files = array();
/* if set, add only files with given extensions */
$fileTypes = array_filter(explode(',', strtolower(Config::get('file_types', ''))));
foreach ($fileInfos as $i) {
if((!empty($fileTypes)) && (!in_array(strtolower(substr(strrchr($i->getName(), "."), 1)), $fileTypes))) {
continue;
}
$files[] = self::formatFileInfo($i);
}
return $files;
}
/*
* @brief check if $child is a subdirectory of $parent
*
* @param string $parent a directory
* @param string $child a directory
* @return bool true if $child is a subdirectory of $parent
*/
public static function isChild($parent, $child) {
return strpos($child, $parent . '/') === 0;
}
}

151
files_opds/lib/util.php Normal file
View file

@ -0,0 +1,151 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
/**
* Utility class for OPDS
*/
class Util
{
/**
* @brief Authenticate user by HTTP Basic Authentication
* with user name and password
*/
public static function authenticateUser() {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
$defaults = new \OC_Defaults();
$realm = $defaults->getName();
header ("HTTP/1.0 401 Unauthorized");
header ('WWW-Authenticate: Basic realm="' . $realm. '"');
exit();
}
$userName = $_SERVER['PHP_AUTH_USER'];
// Check the password in the ownCloud database
return self::checkPassword($userName, $_SERVER['PHP_AUTH_PW']);
}
/**
* @brief Checks the password of a user.
* @param string $userName ownCloud user name whose password will be checked.
* @param string $password ownCloud password.
* @return bool True if the password is correct, false otherwise.
*
*/
private static function checkPassword($userName, $password) {
// Check password normally
if (\OCP\User::checkPassword($userName, $password) != false) {
return true;
}
return false;
}
/**
* @brief Change HTTP response code.
*
* @param integer $statusCode The new HTTP status code.
*/
public static function changeHttpStatus($statusCode) {
$message = '';
switch ($statusCode) {
case 400: $message = 'Bad Request'; break;
case 401: $message = 'Unauthorized'; break;
case 403: $message = 'Forbidden'; break;
case 404: $message = 'Not Found'; break;
case 500: $message = 'Internal Server Error'; break;
case 503: $message = 'Service Unavailable'; break;
}
// Set status code and status message in HTTP header
header('HTTP/1.0 ' . $statusCode . ' ' . $message);
}
/**
* @brief offer single file for download
*
* @param string $path full path to file
* @param int $id file id
*/
public static function serveFile($path, $id) {
\OCP\User::checkLoggedIn();
\OC::$session->close();
Bookshelf::add($id);
$dirName = dirname($path);
$fileName = basename($path);
\OC_Files::get($dirName, array($fileName), $_SERVER['REQUEST_METHOD'] == 'HEAD');
}
/**
* @brief serve opds feed for given directory
*
* @param string $dir full path to directory
* @param int $id requested id
*/
public static function serveFeed($dir, $id) {
if (isset($_SERVER['HTTP_ACCEPT']) && stristr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
header('Content-Type: application/atom+xml');
} else {
header('Content-Type: text/xml; charset=UTF-8');
}
$sortAttribute = 'name';
$sortDirection = false;
$defaults = new \OC_Defaults();
$tmpl = new \OCP\Template('files_opds', 'feed');
$tmpl->assign('files', Files::formatFileInfos(Files::getFiles($dir, $sortAttribute, $sortDirection)));
$tmpl->assign('bookshelf', Files::formatFileInfos(Bookshelf::get()));
$tmpl->assign('bookshelf-count', Bookshelf::count());
$tmpl->assign('feed_id', self::getFeedId());
$tmpl->assign('id', $id);
$tmpl->assign('dir', $dir);
$tmpl->assign('user', \OCP\User::getDisplayName());
$tmpl->assign('ocname', $defaults->getName());
$tmpl->printPage();
}
/**
* @brief generate v3 UUID based on display name and site url
*
* @return string uuid
*/
public static function genUuid() {
$defaults = new \OC_Defaults();
$hash = md5(\OCP\User::getDisplayName() . $defaults->getBaseUrl());
$hash = substr($hash, 0, 8 ) .'-'.
substr($hash, 8, 4) .'-3'.
substr($hash, 13, 3) .'-9'.
substr($hash, 17, 3) .'-'.
substr($hash, 20);
return $hash;
}
/**
* @brief get feed id
*
* @return string feed id
*/
public static function getFeedId() {
return Config::get('id', '');
}
/**
* @brief log warning
* @param string message to write to log
*/
public static function logWarn($msg) {
\OCP\Util::writeLog('files_opds', $msg, \OCP\Util::WARN);
}
}

24
files_opds/personal.php Normal file
View file

@ -0,0 +1,24 @@
<?php
/**
* ownCloud - Files_Opds app
*
* Copyright (c) 2014 Frank de Lange
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Files_Opds;
\OCP\Util::addScript('files_opds', 'personal');
$tmpl = new \OCP\Template('files_opds', 'personal');
$opdsEnable = Config::get('enable', false);
$tmpl->assign('opdsEnable-checked', ($opdsEnable === 'true') ? 'checked="checked"' : '');
$tmpl->assign('opdsEnable-value', ($opdsEnable === 'true') ? '1' : '0');
$tmpl->assign('rootPath', Config::get('root_path', '/Library'));
$tmpl->assign('fileTypes', Config::get('file_types', ''));
$tmpl->assign('bookshelf-count', Bookshelf::count());
return $tmpl->fetchPage();

View file

@ -0,0 +1,124 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:dc="http://purl.org/dc/terms/"
xmlns:opds="http://opds-spec.org/2010/catalog">
<id>id:<?php p($_['feed_id']); ?></id>
<title><?php p($l->t("%s's library", array($_['user']))); ?></title>
<subtitle><?php p($l->t("%s OPDS Catalog", array($_['ocname']))); ?></subtitle>
<updated><?php p(date("Y-m-d\TH:i:sP", $_['feed_updated'])); ?></updated>
<author>
<name><?php p($_['user']); ?></name>
</author>
<link rel="start"
href="?id=root"
type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="self"
href="?id=<?php p($_['id']); ?>"
type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<?php if ($_['id'] == 'root'): ?>
<entry>
<title><?php p($l->t("Browse catalog")); ?></title>
<content type="text"><?php p($l->t("Browse the catalog in alphabetical order")); ?></content>
<link type="application/atom+xml;profile=opds-catalog;kind=navigation"
href="?id=directory"/>
<id>id:by_directory</id>
</entry>
<entry>
<title><?php p($l->t("%s's bookshelf", array($_['user']))); ?></title>
<content type="text"><?php p($l->t("This bookshelf contains %s books", array($_['bookshelf-count']))); ?></content>
<link type="application/atom+xml;profile=opds-catalog;kind=navigation"
href="?id=bookshelf"/>
<id>id:by_bookshelf</id>
</entry>
<?php elseif ($_['id'] == 'bookshelf'): ?>
<link rel="http://opds-spec.org/facet"
href="?id=bookshelf"
title="Bookshelf"
opds:activeFacet="true" />
<?php foreach ($_['bookshelf'] as $file): ?>
<entry>
<title><?php p($file['name']); ?></title>
<updated><?php p(date("Y-m-d\TH:i:sP", $file['mtime'])); ?></updated>
<id>id:<?php p($file['id']); ?></id>
<link type="<?php p($file['mimetype']); ?>"
rel="alternate"
href="?id=<?php p($file['id']); ?>"/>
<link type="<?php p($file['mimetype']); ?>"
rel="http://opds-spec.org/acquisition/open-access"
href="?id=<?php p($file['id']); ?>"/>
<link href="<?php p($file['icon']); ?>"
rel="http://opds-spec.org/image"
type="image/jpeg" />
<link href="<?php p($file['icon']); ?>"
rel="x-stanza-cover-image"
type="image/jpeg" />
<link href="<?php p($file['icon']); ?>"
rel="http://opds-spec.org/thumbnail"
type="image/jpeg" />
<link href="<?php p($file['icon']); ?>"
rel="x-stanza-cover-image-thumbnail"
type="image/jpeg" />
<content type="text"></content>
</entry>
<?php endforeach; ?>
<?php else: ?>
<?php foreach ($_['files'] as $file): ?>
<?php if ($file['type'] == 'dir'): ?>
<entry>
<title><?php p($file['name']); ?></title>
<updated><?php p(date("Y-m-d\TH:i:sP", $file['mtime'])); ?></updated>
<id>id:<?php p($file['id']); ?></id>
<link type="application/atom+xml"
rel="alternate"
href="?id=<?php p($file['id']); ?>"/>
<link type="application/atom+xml;profile=opds-catalog;kind=navigation"
rel="subsection"
href="?id=<?php p($file['id']); ?>"/>
<content type="text"></content>
</entry>
<?php else: ?>
<entry>
<title><?php p($file['name']); ?></title>
<updated><?php p(date("Y-m-d\TH:i:sP", $file['mtime'])); ?></updated>
<id>id:<?php p($file['id']); ?></id>
<link type="<?php p($file['mimetype']); ?>"
rel="alternate"
href="?id=<?php p($file['id']); ?>"/>
<link type="<?php p($file['mimetype']); ?>"
rel="http://opds-spec.org/acquisition/open-access"
href="?id=<?php p($file['id']); ?>"/>
<link href="<?php p($file['icon']); ?>"
rel="http://opds-spec.org/image"
type="image/jpeg" />
<link href="<?php p($file['icon']); ?>"
rel="x-stanza-cover-image"
type="image/jpeg" />
<link href="<?php p($file['icon']); ?>"
rel="http://opds-spec.org/thumbnail"
type="image/jpeg" />
<link href="<?php p($file['icon']); ?>"
rel="x-stanza-cover-image-thumbnail"
type="image/jpeg" />
<content type="text/html"></content>
</entry>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</feed>

View file

@ -0,0 +1,39 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
?>
<div class="section" id="opds-personal">
<h2><?php p($l->t('OPDS')); ?></h2>
<div>
<input id="opds-enable" name="opds-enable" value="<?php p($_['opdsEnable-value']) ?>" <?php p($_['opdsEnable-checked']) ?> type="checkbox">
<label for="opds-enable"><?php p($l->t('enable OPDS catalog')) ?></label>
</div>
<div>
<label for="opds-root-path"><?php p($l->t('Root directory:')) ?></label>
<input type="text" id="opds-root-path" title="<?php p($l->t("Enter root directory for OPDS catalog.")); ?>" value="<?php p($_['rootPath']) ?>" /><span class="msg"></span>
</div>
<div>
<label for="opds-file-types"><?php p($l->t('Supported extensions:')) ?></label>
<input type="text" id="opds-file-types" title="<?php p($l->t("Enter list of comma-separated extensions (eg. pdf,epub,doc,txt). Leave blank to publish all file types.")); ?>" value="<?php p($_['fileTypes']) ?>" />
</div>
<div>
<input type="button" id="opds-clear-bookshelf" value="<?php p($l -> t('Clear Bookshelf')); ?>" />
<input type="button" id="opds-really-clear-bookshelf" title="<?php p($l->t("Clear list of downloaded books from bookshelf. This only clears the list, it does not delete any books.")); ?>" value="<?php p($l -> t('Yes, I really want to clear my personal bookshelf')); ?>" hidden />
<input type="button" id="opds-dont-clear-bookshelf" value="<?php p($l -> t('No, I do not want to clear my bookshelf')); ?>" hidden />
<span class="clr"></span>
<div>
<span id="opds-book-count"><?php p($l->t('There are %s books on your personal bookshelf', array($_['bookshelf-count']))) ?></span>&nbsp;
</div>
</div>
</div>