Skip to content

Commit

Permalink
move verifyLogin + expand Format
Browse files Browse the repository at this point in the history
  • Loading branch information
mikespub committed Jul 19, 2023
1 parent af87878 commit a5389b5
Show file tree
Hide file tree
Showing 20 changed files with 117 additions and 138 deletions.
135 changes: 77 additions & 58 deletions base.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,13 @@ class Config
namespace SebLucas\Cops\Request;

use SebLucas\Cops\Config;
use SebLucas\Template\doT;

function useServerSideRendering()
{
global $config;
return preg_match('/' . $config['cops_server_side_render'] . '/', $_SERVER['HTTP_USER_AGENT']);
}

function serverSideRender($data)
{
// Get the templates
$theme = getCurrentTemplate();
$header = file_get_contents('templates/' . $theme . '/header.html');
$footer = file_get_contents('templates/' . $theme . '/footer.html');
$main = file_get_contents('templates/' . $theme . '/main.html');
$bookdetail = file_get_contents('templates/' . $theme . '/bookdetail.html');
$page = file_get_contents('templates/' . $theme . '/page.html');

// Generate the function for the template
$template = new doT();
$dot = $template->template($page, ['bookdetail' => $bookdetail,
'header' => $header,
'footer' => $footer,
'main' => $main]);
// If there is a syntax error in the function created
// $dot will be equal to FALSE
if (!$dot) {
return false;
}
// Execute the template
if (!empty($data)) {
return $dot($data);
}

return null;
}

function getQueryString()
{
if (isset($_SERVER['QUERY_STRING'])) {
Expand Down Expand Up @@ -116,34 +86,6 @@ function setURLParam($name, $value)
$urlParams[$name] = $value;
}

function addURLParameter($urlParams, $paramName, $paramValue)
{
if (empty($urlParams)) {
$urlParams = '';
}
$start = '';
if (preg_match('#^\?(.*)#', $urlParams, $matches)) {
$start = '?';
$urlParams = $matches[1];
}
$params = [];
parse_str($urlParams, $params);
if (empty($paramValue) && strval($paramValue) !== "0") {
unset($params[$paramName]);
} else {
$params[$paramName] = $paramValue;
}
return $start . http_build_query($params);
}

function addDbParameter($urlParams)
{
if (!is_null(getURLParam('db'))) {
$urlParams = addURLParameter($urlParams, 'db', getURLParam('db'));
}
return $urlParams;
}

function getCurrentOption($option)
{
global $config;
Expand Down Expand Up @@ -186,10 +128,29 @@ function getUrlWithVersion($url)
return $url . '?v=' . Config::VERSION;
}

function verifyLogin()
{
global $config;
if (isset($config['cops_basic_authentication']) &&
is_array($config['cops_basic_authentication'])) {
if (!isset($_SERVER['PHP_AUTH_USER']) ||
(isset($_SERVER['PHP_AUTH_USER']) &&
($_SERVER['PHP_AUTH_USER'] != $config['cops_basic_authentication']['username'] ||
$_SERVER['PHP_AUTH_PW'] != $config['cops_basic_authentication']['password']))) {
return false;
}
}
return true;
}

namespace SebLucas\Cops\Output;

use SebLucas\Template\doT;
use DOMDocument;

use function SebLucas\Cops\Request\getURLParam;
use function SebLucas\Cops\Request\getCurrentTemplate;

class Format
{
/**
Expand All @@ -212,6 +173,64 @@ public static function str_format($format)
return $format;
}

public static function addURLParam($urlParams, $paramName, $paramValue)
{
if (empty($urlParams)) {
$urlParams = '';
}
$start = '';
if (preg_match('#^\?(.*)#', $urlParams, $matches)) {
$start = '?';
$urlParams = $matches[1];
}
$params = [];
parse_str($urlParams, $params);
if (empty($paramValue) && strval($paramValue) !== "0") {
unset($params[$paramName]);
} else {
$params[$paramName] = $paramValue;
}
return $start . http_build_query($params);
}

public static function addDatabaseParam($urlParams)
{
$database = getURLParam('db');
if (!is_null($database)) {
$urlParams = self::addURLParam($urlParams, 'db', $database);
}
return $urlParams;
}

public static function serverSideRender($data)
{
// Get the templates
$theme = getCurrentTemplate();
$header = file_get_contents('templates/' . $theme . '/header.html');
$footer = file_get_contents('templates/' . $theme . '/footer.html');
$main = file_get_contents('templates/' . $theme . '/main.html');
$bookdetail = file_get_contents('templates/' . $theme . '/bookdetail.html');
$page = file_get_contents('templates/' . $theme . '/page.html');

// Generate the function for the template
$template = new doT();
$dot = $template->template($page, ['bookdetail' => $bookdetail,
'header' => $header,
'footer' => $footer,
'main' => $main]);
// If there is a syntax error in the function created
// $dot will be equal to FALSE
if (!$dot) {
return false;
}
// Execute the template
if (!empty($data)) {
return $dot($data);
}

return null;
}

public static function xml2xhtml($xml)
{
return preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', function ($m) {
Expand Down
1 change: 0 additions & 1 deletion checkconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use function SebLucas\Cops\Request\useServerSideRendering;

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/base.php';
/** @var array $config */

$err = getURLParam('err', -1);
Expand Down
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
if (file_exists(dirname(__FILE__) . '/' . $user_config_file) && (php_sapi_name() !== 'cli')) {
require dirname(__FILE__) . '/' . $user_config_file;
}
require_once 'verifyLogin.php';
require_once dirname(__FILE__) . '/base.php';
if (!verifyLogin()) {
header('WWW-Authenticate: Basic realm="COPS Authentication"');
header('HTTP/1.0 401 Unauthorized');
Expand Down
1 change: 0 additions & 1 deletion epubfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use function SebLucas\Cops\Request\notFound;

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/base.php';

if (php_sapi_name() === 'cli') {
return;
Expand Down
1 change: 0 additions & 1 deletion epubreader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use function SebLucas\Cops\Request\notFound;

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/base.php';
/** @var array $config */

$idData = (int) getURLParam('data', null);
Expand Down
1 change: 0 additions & 1 deletion feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use function SebLucas\Cops\Request\getURLParam;

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/base.php';
/** @var array $config */

header('Content-Type:application/xml');
Expand Down
1 change: 0 additions & 1 deletion fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use function SebLucas\Cops\Request\notFound;

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/base.php';
/** @var array $config */

global $config;
Expand Down
1 change: 0 additions & 1 deletion getJSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use SebLucas\Cops\Output\JSONRenderer;

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/base.php';
/** @var array $config */

header('Content-Type:application/json;charset=utf-8');
Expand Down
7 changes: 2 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
use SebLucas\Cops\Pages\Page;
use SebLucas\Template\doT;

use function SebLucas\Cops\Request\addURLParameter;
use function SebLucas\Cops\Request\getCurrentCss;
use function SebLucas\Cops\Request\getCurrentTemplate;
use function SebLucas\Cops\Request\getQueryString;
use function SebLucas\Cops\Request\getURLParam;
use function SebLucas\Cops\Request\serverSideRender;
use function SebLucas\Cops\Request\useServerSideRendering;

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/base.php';
/** @var array $config */

// If we detect that an OPDS reader try to connect try to redirect to feed.php
Expand Down Expand Up @@ -60,7 +57,7 @@
'server_side_rendering' => useServerSideRendering(),
'current_css' => getCurrentCss(),
'favico' => $config['cops_icon'],
'getjson_url' => Config::ENDPOINT["json"] . '?' . addURLParameter(getQueryString(), 'complete', 1)];
'getjson_url' => JSONRenderer::getCurrentUrl(getQueryString())];
if (preg_match('/Kindle/', $_SERVER['HTTP_USER_AGENT'])) {
$data['customHeader'] = '<style media="screen" type="text/css"> html { font-size: 75%; -webkit-text-size-adjust: 75%; -ms-text-size-adjust: 75%; }</style>';
}
Expand All @@ -75,7 +72,7 @@
// Get the data
$data = JSONRenderer::getJson(true);

echo serverSideRender($data);
echo Format::serverSideRender($data);
}
?>
</body>
Expand Down
5 changes: 1 addition & 4 deletions lib/Calibre/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use SebLucas\EPubMeta\EPub;
use Exception;

use function SebLucas\Cops\Request\addURLParameter;
use function SebLucas\Cops\Request\getCurrentOption;
use function SebLucas\Cops\Request\getURLParam;

Expand Down Expand Up @@ -230,9 +229,7 @@ public function getUri()
public function getDetailUrl()
{
$urlParam = $this->getUri();
if (!is_null(getURLParam('db'))) {
$urlParam = addURLParameter($urlParam, 'db', getURLParam('db'));
}
$urlParam = Format::addDatabaseParam($urlParam);
return self::$endpoint . $urlParam;
}

Expand Down
16 changes: 7 additions & 9 deletions lib/Calibre/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

use SebLucas\Cops\Config;
use SebLucas\Cops\Model\Link;
use SebLucas\Cops\Output\Format;

use function SebLucas\Cops\Request\addURLParameter;
use function SebLucas\Cops\Request\getURLParam;

class Data
Expand Down Expand Up @@ -193,7 +193,7 @@ public static function handleThumbnailLink($urlParam, $height)
}
}
if ($config['cops_thumbnail_handling'] != "1") {
$urlParam = addURLParameter($urlParam, "height", $height);
$urlParam = Format::addURLParam($urlParam, "height", $height);
}

return $urlParam;
Expand All @@ -203,24 +203,22 @@ public static function getLink($book, $type, $mime, $rel, $filename, $idData, $t
{
global $config;

$urlParam = addURLParameter("", "data", $idData);
$urlParam = Format::addURLParam("", "data", $idData);
if ($view) {
$urlParam = addURLParameter($urlParam, "view", 1);
$urlParam = Format::addURLParam($urlParam, "view", 1);
}

if (Base::useAbsolutePath() ||
$rel == Link::OPDS_THUMBNAIL_TYPE ||
($type == "epub" && $config['cops_update_epub-metadata'])) {
if ($type != "jpg") {
$urlParam = addURLParameter($urlParam, "type", $type);
$urlParam = Format::addURLParam($urlParam, "type", $type);
}
if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
$urlParam = self::handleThumbnailLink($urlParam, $height);
}
$urlParam = addURLParameter($urlParam, "id", $book->id);
if (!is_null(getURLParam('db'))) {
$urlParam = addURLParameter($urlParam, 'db', getURLParam('db'));
}
$urlParam = Format::addURLParam($urlParam, "id", $book->id);
$urlParam = Format::addDatabaseParam($urlParam);
if ($config['cops_thumbnail_handling'] != "1" &&
!empty($config['cops_thumbnail_handling']) &&
$rel == Link::OPDS_THUMBNAIL_TYPE) {
Expand Down
7 changes: 2 additions & 5 deletions lib/Model/LinkFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@

namespace SebLucas\Cops\Model;

use function SebLucas\Cops\Request\addURLParameter;
use function SebLucas\Cops\Request\getURLParam;
use SebLucas\Cops\Output\Format;

class LinkFacet extends Link
{
public function __construct($phref, $ptitle = null, $pfacetGroup = null, $pactiveFacet = false)
{
parent::__construct($phref, Link::OPDS_PAGING_TYPE, "http://opds-spec.org/facet", $ptitle, $pfacetGroup, $pactiveFacet);
if (!is_null(getURLParam('db'))) {
$this->href = addURLParameter($this->href, 'db', getURLParam('db'));
}
$this->href = Format::addDatabaseParam($this->href);
$this->href = parent::getScriptName() . $this->href;
}
}
7 changes: 2 additions & 5 deletions lib/Model/LinkNavigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@

namespace SebLucas\Cops\Model;

use function SebLucas\Cops\Request\addURLParameter;
use function SebLucas\Cops\Request\getURLParam;
use SebLucas\Cops\Output\Format;

class LinkNavigation extends Link
{
public function __construct($phref, $prel = null, $ptitle = null)
{
parent::__construct($phref, Link::OPDS_NAVIGATION_TYPE, $prel, $ptitle);
if (!is_null(getURLParam('db'))) {
$this->href = addURLParameter($this->href, 'db', getURLParam('db'));
}
$this->href = Format::addDatabaseParam($this->href);
if (!preg_match("#^\?(.*)#", $this->href) && !empty($this->href)) {
$this->href = "?" . $this->href;
}
Expand Down
Loading

0 comments on commit a5389b5

Please sign in to comment.