Skip to content

Commit

Permalink
LinkDB: do not access global variables
Browse files Browse the repository at this point in the history
Relates to shaarli#218

Removes "hidden" access to the following variables:
 - $GLOBALS['config']['datastore']
 - PHPPREFIX
 - PHPSUFFIX

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
  • Loading branch information
virtualtam committed Jun 24, 2015
1 parent 64bc92e commit 45c960a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
29 changes: 19 additions & 10 deletions application/LinkDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
*/
class LinkDB implements Iterator, Countable, ArrayAccess
{
// Links are stored as a PHP serialized string
private $datastore;

// Datastore PHP prefix
protected static $phpPrefix = '<?php /* ';

// Datastore PHP suffix
protected static $phpSuffix = ' */ ?>';

// List of links (associative array)
// - key: link date (e.g. "20110823_124546"),
// - value: associative array (keys: title, description...)
Expand Down Expand Up @@ -55,9 +64,9 @@ class LinkDB implements Iterator, Countable, ArrayAccess
*
* @param $isLoggedIn is the user logged in?
*/
function __construct($isLoggedIn, $hidePublicLinks)
function __construct($datastore, $isLoggedIn, $hidePublicLinks)
{
// FIXME: do not access $GLOBALS, pass the datastore instead
$this->datastore = $datastore;
$this->loggedIn = $isLoggedIn;
$this->hidePublicLinks = $hidePublicLinks;
$this->checkDB();
Expand Down Expand Up @@ -172,7 +181,7 @@ function valid()
*/
private function checkDB()
{
if (file_exists($GLOBALS['config']['DATASTORE'])) {
if (file_exists($this->datastore)) {
return;
}

Expand Down Expand Up @@ -202,8 +211,8 @@ private function checkDB()
// TODO: raise an exception if the file is not write-able
file_put_contents(
// FIXME: do not use $GLOBALS
$GLOBALS['config']['DATASTORE'],
PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
$this->datastore,
self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
);
}

Expand All @@ -225,10 +234,10 @@ private function readdb()
// FIXME: do not use $GLOBALS
$this->links = array();

if (file_exists($GLOBALS['config']['DATASTORE'])) {
if (file_exists($this->datastore)) {
$this->links = unserialize(gzinflate(base64_decode(
substr(file_get_contents($GLOBALS['config']['DATASTORE']),
strlen(PHPPREFIX), -strlen(PHPSUFFIX)))));
substr(file_get_contents($this->datastore),
strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
}

// If user is not logged in, filter private links.
Expand Down Expand Up @@ -266,8 +275,8 @@ public function savedb()
die('You are not authorized to change the database.');
}
file_put_contents(
$GLOBALS['config']['DATASTORE'],
PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
$this->datastore,
self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
);
invalidateCaches();
}
Expand Down
8 changes: 6 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
if (is_file($GLOBALS['config']['DATADIR'].'/options.php')) require($GLOBALS['config']['DATADIR'].'/options.php');

define('shaarli_version','0.0.45beta');
define('PHPPREFIX','<?php /* '); // Prefix to encapsulate data in PHP code.
define('PHPSUFFIX',' */ ?>'); // Suffix to encapsulate data in PHP code.
// http://server.com/x/shaarli --> /shaarli/
define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0)));

Expand Down Expand Up @@ -700,6 +698,7 @@ function showRSS()

// If cached was not found (or not usable), then read the database and build the response:
$LINKSDB = new LinkDB(
$GLOBALS['config']['DATASTORE'],
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
Expand Down Expand Up @@ -780,6 +779,7 @@ function showATOM()

// Read links from database (and filter private links if used it not logged in).
$LINKSDB = new LinkDB(
$GLOBALS['config']['DATASTORE'],
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
Expand Down Expand Up @@ -866,6 +866,7 @@ function showDailyRSS()

// Read links from database (and filter private links if used it not logged in).
$LINKSDB = new LinkDB(
$GLOBALS['config']['DATASTORE'],
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
Expand Down Expand Up @@ -937,6 +938,7 @@ function showDailyRSS()
function showDaily()
{
$LINKSDB = new LinkDB(
$GLOBALS['config']['DATASTORE'],
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
Expand Down Expand Up @@ -1006,6 +1008,7 @@ function showDaily()
function renderPage()
{
$LINKSDB = new LinkDB(
$GLOBALS['config']['DATASTORE'],
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
Expand Down Expand Up @@ -1587,6 +1590,7 @@ function importFile()
{
if (!(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'])) { die('Not allowed.'); }
$LINKSDB = new LinkDB(
$GLOBALS['config']['DATASTORE'],
isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'],
$GLOBALS['config']['HIDE_PUBLIC_LINKS']
);
Expand Down
32 changes: 13 additions & 19 deletions tests/LinkDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
require_once 'application/Utils.php';
require_once 'tests/utils/ReferenceLinkDB.php';

define('PHPPREFIX', '<?php /* ');
define('PHPSUFFIX', ' */ ?>');


/**
* Unitary tests for LinkDB
Expand Down Expand Up @@ -38,19 +35,17 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
public static function setUpBeforeClass()
{
self::$refDB = new ReferenceLinkDB();
self::$refDB->write(self::$testDatastore, PHPPREFIX, PHPSUFFIX);
self::$refDB->write(self::$testDatastore);

$GLOBALS['config']['DATASTORE'] = self::$testDatastore;
self::$publicLinkDB = new LinkDB(false, false);
self::$privateLinkDB = new LinkDB(true, false);
self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false);
self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
}

/**
* Resets test data for each test
*/
protected function setUp()
{
$GLOBALS['config']['DATASTORE'] = self::$testDatastore;
if (file_exists(self::$testDatastore)) {
unlink(self::$testDatastore);
}
Expand All @@ -76,7 +71,7 @@ protected static function getMethod($name)
*/
public function testConstructLoggedIn()
{
new LinkDB(true, false);
new LinkDB(self::$testDatastore, true, false);
$this->assertFileExists(self::$testDatastore);
}

Expand All @@ -85,7 +80,7 @@ public function testConstructLoggedIn()
*/
public function testConstructLoggedOut()
{
new LinkDB(false, false);
new LinkDB(self::$testDatastore, false, false);
$this->assertFileExists(self::$testDatastore);
}

Expand All @@ -97,16 +92,15 @@ public function testConstructLoggedOut()
*/
public function testConstructDatastoreNotWriteable()
{
$GLOBALS['config']['DATASTORE'] = 'null/store.db';
new LinkDB(false, false);
new LinkDB('null/store.db', false, false);
}

/**
* The DB doesn't exist, ensure it is created with dummy content
*/
public function testCheckDBNew()
{
$linkDB = new LinkDB(false, false);
$linkDB = new LinkDB(self::$testDatastore, false, false);
unlink(self::$testDatastore);
$this->assertFileNotExists(self::$testDatastore);

Expand All @@ -126,7 +120,7 @@ public function testCheckDBNew()
*/
public function testCheckDBLoad()
{
$linkDB = new LinkDB(false, false);
$linkDB = new LinkDB(self::$testDatastore, false, false);
$this->assertEquals(
self::$dummyDatastoreSHA1,
sha1_file(self::$testDatastore)
Expand All @@ -147,8 +141,8 @@ public function testCheckDBLoad()
*/
public function testReadEmptyDB()
{
file_put_contents(self::$testDatastore, PHPPREFIX.'S7QysKquBQA='.PHPSUFFIX);
$emptyDB = new LinkDB(false, false);
file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
$emptyDB = new LinkDB(self::$testDatastore, false, false);
$this->assertEquals(0, sizeof($emptyDB));
$this->assertEquals(0, count($emptyDB));
}
Expand Down Expand Up @@ -180,7 +174,7 @@ public function testReadPrivateDB()
*/
public function testSaveDB()
{
$testDB = new LinkDB(true, false);
$testDB = new LinkDB(self::$testDatastore, true, false);
$dbSize = sizeof($testDB);

$link = array(
Expand All @@ -198,7 +192,7 @@ function invalidateCaches() {}

$testDB->savedb();

$testDB = new LinkDB(true, false);
$testDB = new LinkDB(self::$testDatastore, true, false);
$this->assertEquals($dbSize + 1, sizeof($testDB));
}

Expand All @@ -222,7 +216,7 @@ public function testCount()
*/
public function testCountHiddenPublic()
{
$linkDB = new LinkDB(false, true);
$linkDB = new LinkDB(self::$testDatastore, false, true);

$this->assertEquals(
0,
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/ReferenceLinkDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ protected function addLink($title, $url, $description, $private, $date, $tags)
/**
* Writes data to the datastore
*/
public function write($filename, $prefix, $suffix)
public function write($filename)
{
file_put_contents(
$filename,
$prefix.base64_encode(gzdeflate(serialize($this->links))).$suffix
'<?php /* '.base64_encode(gzdeflate(serialize($this->links))).' */ ?>'
);
}

Expand Down

0 comments on commit 45c960a

Please sign in to comment.