LinkDB: add 'hidePublicLinks' parameter to the constructor

Fixes #236
Relates to #237

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
VirtualTam 2015-06-23 22:34:07 +02:00 committed by nodiscc
parent ae63027010
commit 9f15ca9ee7
3 changed files with 59 additions and 20 deletions

View file

@ -45,6 +45,9 @@ class LinkDB implements Iterator, Countable, ArrayAccess
// Is the user logged in? (used to filter private links) // Is the user logged in? (used to filter private links)
private $loggedIn; private $loggedIn;
// Hide public links
private $hidePublicLinks;
/** /**
* Creates a new LinkDB * Creates a new LinkDB
* *
@ -52,10 +55,11 @@ class LinkDB implements Iterator, Countable, ArrayAccess
* *
* @param $isLoggedIn is the user logged in? * @param $isLoggedIn is the user logged in?
*/ */
function __construct($isLoggedIn) function __construct($isLoggedIn, $hidePublicLinks)
{ {
// FIXME: do not access $GLOBALS, pass the datastore instead // FIXME: do not access $GLOBALS, pass the datastore instead
$this->loggedIn = $isLoggedIn; $this->loggedIn = $isLoggedIn;
$this->hidePublicLinks = $hidePublicLinks;
$this->checkDB(); $this->checkDB();
$this->readdb(); $this->readdb();
} }
@ -210,7 +214,7 @@ private function readdb()
{ {
// Public links are hidden and user not logged in => nothing to show // Public links are hidden and user not logged in => nothing to show
if ($GLOBALS['config']['HIDE_PUBLIC_LINKS'] && !isLoggedIn()) { if ($this->hidePublicLinks && !$this->loggedIn) {
$this->links = array(); $this->links = array();
return; return;
} }

View file

@ -702,7 +702,11 @@ function showRSS()
$cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; }
// If cached was not found (or not usable), then read the database and build the response: // If cached was not found (or not usable), then read the database and build the response:
$LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if user it not logged in). $LINKSDB = new LinkDB(
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
// Read links from database (and filter private links if user it not logged in).
// Optionally filter the results: // Optionally filter the results:
$linksToDisplay=array(); $linksToDisplay=array();
@ -777,8 +781,10 @@ function showATOM()
$cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; }
// If cached was not found (or not usable), then read the database and build the response: // If cached was not found (or not usable), then read the database and build the response:
$LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). $LINKSDB = new LinkDB(
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
// Optionally filter the results: // Optionally filter the results:
$linksToDisplay=array(); $linksToDisplay=array();
@ -859,7 +865,11 @@ function showDailyRSS()
$cache = new pageCache(pageUrl(),startsWith($query,'do=dailyrss') && !isLoggedIn()); $cache = new pageCache(pageUrl(),startsWith($query,'do=dailyrss') && !isLoggedIn());
$cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; }
// If cached was not found (or not usable), then read the database and build the response: // If cached was not found (or not usable), then read the database and build the response:
$LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in).
$LINKSDB = new LinkDB(
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
/* Some Shaarlies may have very few links, so we need to look /* Some Shaarlies may have very few links, so we need to look
back in time (rsort()) until we have enough days ($nb_of_days). back in time (rsort()) until we have enough days ($nb_of_days).
@ -927,8 +937,10 @@ function showDailyRSS()
// "Daily" page. // "Daily" page.
function showDaily() function showDaily()
{ {
$LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). $LINKSDB = new LinkDB(
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
$day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD.
if (isset($_GET['day'])) $day=$_GET['day']; if (isset($_GET['day'])) $day=$_GET['day'];
@ -993,7 +1005,10 @@ function showDaily()
// Render HTML page (according to URL parameters and user rights) // Render HTML page (according to URL parameters and user rights)
function renderPage() function renderPage()
{ {
$LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). $LINKSDB = new LinkDB(
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
// -------- Display login form. // -------- Display login form.
if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=login')) if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=login'))
@ -1571,7 +1586,10 @@ function renderPage()
function importFile() function importFile()
{ {
if (!(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'])) { die('Not allowed.'); } if (!(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'])) { die('Not allowed.'); }
$LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). $LINKSDB = new LinkDB(
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
$filename=$_FILES['filetoupload']['name']; $filename=$_FILES['filetoupload']['name'];
$filesize=$_FILES['filetoupload']['size']; $filesize=$_FILES['filetoupload']['size'];
$data=file_get_contents($_FILES['filetoupload']['tmp_name']); $data=file_get_contents($_FILES['filetoupload']['tmp_name']);

View file

@ -41,8 +41,8 @@ public static function setUpBeforeClass()
self::$refDB->write(self::$testDatastore, PHPPREFIX, PHPSUFFIX); self::$refDB->write(self::$testDatastore, PHPPREFIX, PHPSUFFIX);
$GLOBALS['config']['DATASTORE'] = self::$testDatastore; $GLOBALS['config']['DATASTORE'] = self::$testDatastore;
self::$publicLinkDB = new LinkDB(false); self::$publicLinkDB = new LinkDB(false, false);
self::$privateLinkDB = new LinkDB(true); self::$privateLinkDB = new LinkDB(true, false);
} }
/** /**
@ -76,7 +76,7 @@ protected static function getMethod($name)
*/ */
public function testConstructLoggedIn() public function testConstructLoggedIn()
{ {
new LinkDB(true); new LinkDB(true, false);
$this->assertFileExists(self::$testDatastore); $this->assertFileExists(self::$testDatastore);
} }
@ -85,7 +85,7 @@ public function testConstructLoggedIn()
*/ */
public function testConstructLoggedOut() public function testConstructLoggedOut()
{ {
new LinkDB(false); new LinkDB(false, false);
$this->assertFileExists(self::$testDatastore); $this->assertFileExists(self::$testDatastore);
} }
@ -98,7 +98,7 @@ public function testConstructLoggedOut()
public function testConstructDatastoreNotWriteable() public function testConstructDatastoreNotWriteable()
{ {
$GLOBALS['config']['DATASTORE'] = 'null/store.db'; $GLOBALS['config']['DATASTORE'] = 'null/store.db';
new LinkDB(false); new LinkDB(false, false);
} }
/** /**
@ -106,7 +106,7 @@ public function testConstructDatastoreNotWriteable()
*/ */
public function testCheckDBNew() public function testCheckDBNew()
{ {
$linkDB = new LinkDB(false); $linkDB = new LinkDB(false, false);
unlink(self::$testDatastore); unlink(self::$testDatastore);
$this->assertFileNotExists(self::$testDatastore); $this->assertFileNotExists(self::$testDatastore);
@ -126,7 +126,7 @@ public function testCheckDBNew()
*/ */
public function testCheckDBLoad() public function testCheckDBLoad()
{ {
$linkDB = new LinkDB(false); $linkDB = new LinkDB(false, false);
$this->assertEquals( $this->assertEquals(
self::$dummyDatastoreSHA1, self::$dummyDatastoreSHA1,
sha1_file(self::$testDatastore) sha1_file(self::$testDatastore)
@ -148,7 +148,7 @@ public function testCheckDBLoad()
public function testReadEmptyDB() public function testReadEmptyDB()
{ {
file_put_contents(self::$testDatastore, PHPPREFIX.'S7QysKquBQA='.PHPSUFFIX); file_put_contents(self::$testDatastore, PHPPREFIX.'S7QysKquBQA='.PHPSUFFIX);
$emptyDB = new LinkDB(false); $emptyDB = new LinkDB(false, false);
$this->assertEquals(0, sizeof($emptyDB)); $this->assertEquals(0, sizeof($emptyDB));
$this->assertEquals(0, count($emptyDB)); $this->assertEquals(0, count($emptyDB));
} }
@ -180,7 +180,7 @@ public function testReadPrivateDB()
*/ */
public function testSaveDB() public function testSaveDB()
{ {
$testDB = new LinkDB(true); $testDB = new LinkDB(true, false);
$dbSize = sizeof($testDB); $dbSize = sizeof($testDB);
$link = array( $link = array(
@ -198,7 +198,7 @@ function invalidateCaches() {}
$testDB->savedb(); $testDB->savedb();
$testDB = new LinkDB(true); $testDB = new LinkDB(true, false);
$this->assertEquals($dbSize + 1, sizeof($testDB)); $this->assertEquals($dbSize + 1, sizeof($testDB));
} }
@ -217,6 +217,23 @@ public function testCount()
); );
} }
/**
* Count existing links - public links hidden
*/
public function testCountHiddenPublic()
{
$linkDB = new LinkDB(false, true);
$this->assertEquals(
0,
$linkDB->count()
);
$this->assertEquals(
0,
$linkDB->count()
);
}
/** /**
* List the days for which links have been posted * List the days for which links have been posted
*/ */