aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-06-23 22:34:07 +0200
committernodiscc <nodiscc@gmail.com>2015-06-24 00:26:59 +0200
commit9f15ca9ee76bc36832f12a952005fdaf52e2559d (patch)
treeb29e109d2eccad7b13a6dd5d6f59571f4023686f
parentae630270109bf78b5f561c887d35b7b099d30271 (diff)
downloadShaarli-9f15ca9ee76bc36832f12a952005fdaf52e2559d.tar.gz
Shaarli-9f15ca9ee76bc36832f12a952005fdaf52e2559d.tar.zst
Shaarli-9f15ca9ee76bc36832f12a952005fdaf52e2559d.zip
LinkDB: add 'hidePublicLinks' parameter to the constructor
Fixes #236 Relates to #237 Signed-off-by: VirtualTam <virtualtam@flibidi.net>
-rw-r--r--application/LinkDB.php8
-rw-r--r--index.php34
-rw-r--r--tests/LinkDBTest.php37
3 files changed, 59 insertions, 20 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index 137f42e5..47dbcaf3 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -45,6 +45,9 @@ class LinkDB implements Iterator, Countable, ArrayAccess
45 // Is the user logged in? (used to filter private links) 45 // Is the user logged in? (used to filter private links)
46 private $loggedIn; 46 private $loggedIn;
47 47
48 // Hide public links
49 private $hidePublicLinks;
50
48 /** 51 /**
49 * Creates a new LinkDB 52 * Creates a new LinkDB
50 * 53 *
@@ -52,10 +55,11 @@ class LinkDB implements Iterator, Countable, ArrayAccess
52 * 55 *
53 * @param $isLoggedIn is the user logged in? 56 * @param $isLoggedIn is the user logged in?
54 */ 57 */
55 function __construct($isLoggedIn) 58 function __construct($isLoggedIn, $hidePublicLinks)
56 { 59 {
57 // FIXME: do not access $GLOBALS, pass the datastore instead 60 // FIXME: do not access $GLOBALS, pass the datastore instead
58 $this->loggedIn = $isLoggedIn; 61 $this->loggedIn = $isLoggedIn;
62 $this->hidePublicLinks = $hidePublicLinks;
59 $this->checkDB(); 63 $this->checkDB();
60 $this->readdb(); 64 $this->readdb();
61 } 65 }
@@ -210,7 +214,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
210 { 214 {
211 215
212 // Public links are hidden and user not logged in => nothing to show 216 // Public links are hidden and user not logged in => nothing to show
213 if ($GLOBALS['config']['HIDE_PUBLIC_LINKS'] && !isLoggedIn()) { 217 if ($this->hidePublicLinks && !$this->loggedIn) {
214 $this->links = array(); 218 $this->links = array();
215 return; 219 return;
216 } 220 }
diff --git a/index.php b/index.php
index db2b7521..f116a87b 100644
--- a/index.php
+++ b/index.php
@@ -702,7 +702,11 @@ function showRSS()
702 $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } 702 $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; }
703 703
704 // If cached was not found (or not usable), then read the database and build the response: 704 // If cached was not found (or not usable), then read the database and build the response:
705 $LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if user it not logged in). 705 $LINKSDB = new LinkDB(
706 isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
707 $GLOBALS['config']['HIDE_PUBLIC_LINKS']
708 );
709 // Read links from database (and filter private links if user it not logged in).
706 710
707 // Optionally filter the results: 711 // Optionally filter the results:
708 $linksToDisplay=array(); 712 $linksToDisplay=array();
@@ -777,8 +781,10 @@ function showATOM()
777 $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } 781 $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; }
778 // If cached was not found (or not usable), then read the database and build the response: 782 // If cached was not found (or not usable), then read the database and build the response:
779 783
780 $LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). 784 $LINKSDB = new LinkDB(
781 785 isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
786 $GLOBALS['config']['HIDE_PUBLIC_LINKS']
787 );
782 788
783 // Optionally filter the results: 789 // Optionally filter the results:
784 $linksToDisplay=array(); 790 $linksToDisplay=array();
@@ -859,7 +865,11 @@ function showDailyRSS()
859 $cache = new pageCache(pageUrl(),startsWith($query,'do=dailyrss') && !isLoggedIn()); 865 $cache = new pageCache(pageUrl(),startsWith($query,'do=dailyrss') && !isLoggedIn());
860 $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } 866 $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; }
861 // If cached was not found (or not usable), then read the database and build the response: 867 // If cached was not found (or not usable), then read the database and build the response:
862 $LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). 868
869 $LINKSDB = new LinkDB(
870 isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
871 $GLOBALS['config']['HIDE_PUBLIC_LINKS']
872 );
863 873
864 /* Some Shaarlies may have very few links, so we need to look 874 /* Some Shaarlies may have very few links, so we need to look
865 back in time (rsort()) until we have enough days ($nb_of_days). 875 back in time (rsort()) until we have enough days ($nb_of_days).
@@ -927,8 +937,10 @@ function showDailyRSS()
927// "Daily" page. 937// "Daily" page.
928function showDaily() 938function showDaily()
929{ 939{
930 $LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). 940 $LINKSDB = new LinkDB(
931 941 isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
942 $GLOBALS['config']['HIDE_PUBLIC_LINKS']
943 );
932 944
933 $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. 945 $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD.
934 if (isset($_GET['day'])) $day=$_GET['day']; 946 if (isset($_GET['day'])) $day=$_GET['day'];
@@ -993,7 +1005,10 @@ function showDaily()
993// Render HTML page (according to URL parameters and user rights) 1005// Render HTML page (according to URL parameters and user rights)
994function renderPage() 1006function renderPage()
995{ 1007{
996 $LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). 1008 $LINKSDB = new LinkDB(
1009 isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
1010 $GLOBALS['config']['HIDE_PUBLIC_LINKS']
1011 );
997 1012
998 // -------- Display login form. 1013 // -------- Display login form.
999 if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=login')) 1014 if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=login'))
@@ -1571,7 +1586,10 @@ HTML;
1571function importFile() 1586function importFile()
1572{ 1587{
1573 if (!(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'])) { die('Not allowed.'); } 1588 if (!(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'])) { die('Not allowed.'); }
1574 $LINKSDB = new LinkDB(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). 1589 $LINKSDB = new LinkDB(
1590 isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
1591 $GLOBALS['config']['HIDE_PUBLIC_LINKS']
1592 );
1575 $filename=$_FILES['filetoupload']['name']; 1593 $filename=$_FILES['filetoupload']['name'];
1576 $filesize=$_FILES['filetoupload']['size']; 1594 $filesize=$_FILES['filetoupload']['size'];
1577 $data=file_get_contents($_FILES['filetoupload']['tmp_name']); 1595 $data=file_get_contents($_FILES['filetoupload']['tmp_name']);
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php
index bbe4e026..f67d4d9b 100644
--- a/tests/LinkDBTest.php
+++ b/tests/LinkDBTest.php
@@ -41,8 +41,8 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
41 self::$refDB->write(self::$testDatastore, PHPPREFIX, PHPSUFFIX); 41 self::$refDB->write(self::$testDatastore, PHPPREFIX, PHPSUFFIX);
42 42
43 $GLOBALS['config']['DATASTORE'] = self::$testDatastore; 43 $GLOBALS['config']['DATASTORE'] = self::$testDatastore;
44 self::$publicLinkDB = new LinkDB(false); 44 self::$publicLinkDB = new LinkDB(false, false);
45 self::$privateLinkDB = new LinkDB(true); 45 self::$privateLinkDB = new LinkDB(true, false);
46 } 46 }
47 47
48 /** 48 /**
@@ -76,7 +76,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
76 */ 76 */
77 public function testConstructLoggedIn() 77 public function testConstructLoggedIn()
78 { 78 {
79 new LinkDB(true); 79 new LinkDB(true, false);
80 $this->assertFileExists(self::$testDatastore); 80 $this->assertFileExists(self::$testDatastore);
81 } 81 }
82 82
@@ -85,7 +85,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
85 */ 85 */
86 public function testConstructLoggedOut() 86 public function testConstructLoggedOut()
87 { 87 {
88 new LinkDB(false); 88 new LinkDB(false, false);
89 $this->assertFileExists(self::$testDatastore); 89 $this->assertFileExists(self::$testDatastore);
90 } 90 }
91 91
@@ -98,7 +98,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
98 public function testConstructDatastoreNotWriteable() 98 public function testConstructDatastoreNotWriteable()
99 { 99 {
100 $GLOBALS['config']['DATASTORE'] = 'null/store.db'; 100 $GLOBALS['config']['DATASTORE'] = 'null/store.db';
101 new LinkDB(false); 101 new LinkDB(false, false);
102 } 102 }
103 103
104 /** 104 /**
@@ -106,7 +106,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
106 */ 106 */
107 public function testCheckDBNew() 107 public function testCheckDBNew()
108 { 108 {
109 $linkDB = new LinkDB(false); 109 $linkDB = new LinkDB(false, false);
110 unlink(self::$testDatastore); 110 unlink(self::$testDatastore);
111 $this->assertFileNotExists(self::$testDatastore); 111 $this->assertFileNotExists(self::$testDatastore);
112 112
@@ -126,7 +126,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
126 */ 126 */
127 public function testCheckDBLoad() 127 public function testCheckDBLoad()
128 { 128 {
129 $linkDB = new LinkDB(false); 129 $linkDB = new LinkDB(false, false);
130 $this->assertEquals( 130 $this->assertEquals(
131 self::$dummyDatastoreSHA1, 131 self::$dummyDatastoreSHA1,
132 sha1_file(self::$testDatastore) 132 sha1_file(self::$testDatastore)
@@ -148,7 +148,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
148 public function testReadEmptyDB() 148 public function testReadEmptyDB()
149 { 149 {
150 file_put_contents(self::$testDatastore, PHPPREFIX.'S7QysKquBQA='.PHPSUFFIX); 150 file_put_contents(self::$testDatastore, PHPPREFIX.'S7QysKquBQA='.PHPSUFFIX);
151 $emptyDB = new LinkDB(false); 151 $emptyDB = new LinkDB(false, false);
152 $this->assertEquals(0, sizeof($emptyDB)); 152 $this->assertEquals(0, sizeof($emptyDB));
153 $this->assertEquals(0, count($emptyDB)); 153 $this->assertEquals(0, count($emptyDB));
154 } 154 }
@@ -180,7 +180,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
180 */ 180 */
181 public function testSaveDB() 181 public function testSaveDB()
182 { 182 {
183 $testDB = new LinkDB(true); 183 $testDB = new LinkDB(true, false);
184 $dbSize = sizeof($testDB); 184 $dbSize = sizeof($testDB);
185 185
186 $link = array( 186 $link = array(
@@ -198,7 +198,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
198 198
199 $testDB->savedb(); 199 $testDB->savedb();
200 200
201 $testDB = new LinkDB(true); 201 $testDB = new LinkDB(true, false);
202 $this->assertEquals($dbSize + 1, sizeof($testDB)); 202 $this->assertEquals($dbSize + 1, sizeof($testDB));
203 } 203 }
204 204
@@ -218,6 +218,23 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
218 } 218 }
219 219
220 /** 220 /**
221 * Count existing links - public links hidden
222 */
223 public function testCountHiddenPublic()
224 {
225 $linkDB = new LinkDB(false, true);
226
227 $this->assertEquals(
228 0,
229 $linkDB->count()
230 );
231 $this->assertEquals(
232 0,
233 $linkDB->count()
234 );
235 }
236
237 /**
221 * List the days for which links have been posted 238 * List the days for which links have been posted
222 */ 239 */
223 public function testDays() 240 public function testDays()