PHP < 5.6 compatibility and StyleCI recommendations

This commit is contained in:
El RIDO 2017-10-08 11:31:41 +02:00
parent 4f06feef81
commit 9f26894b2e
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
6 changed files with 24 additions and 25 deletions

View File

@ -27,7 +27,7 @@ class Configuration
* *
* @const string * @const string
*/ */
const PROTECTION_LINE = ';<?php http_response_code(403); /*' . PHP_EOL; const PROTECTION_LINE = ';<?php http_response_code(403); /*';
/** /**
* parsed configuration * parsed configuration
@ -116,7 +116,7 @@ class Configuration
// don't overwrite already converted file // don't overwrite already converted file
if (!is_file($configFile)) { if (!is_file($configFile)) {
$iniHandle = fopen($configIni, 'r', false, $context); $iniHandle = fopen($configIni, 'r', false, $context);
file_put_contents($configFile, self::PROTECTION_LINE); file_put_contents($configFile, self::PROTECTION_LINE . PHP_EOL);
file_put_contents($configFile, $iniHandle, FILE_APPEND); file_put_contents($configFile, $iniHandle, FILE_APPEND);
fclose($iniHandle); fclose($iniHandle);
} }
@ -128,7 +128,7 @@ class Configuration
if (is_readable($configIniSample)) { if (is_readable($configIniSample)) {
if (!is_readable($configSample)) { if (!is_readable($configSample)) {
$iniSampleHandle = fopen($configIniSample, 'r', false, $context); $iniSampleHandle = fopen($configIniSample, 'r', false, $context);
file_put_contents($configSample, self::PROTECTION_LINE); file_put_contents($configSample, self::PROTECTION_LINE . PHP_EOL);
file_put_contents($configSample, $iniSampleHandle, FILE_APPEND); file_put_contents($configSample, $iniSampleHandle, FILE_APPEND);
fclose($iniSampleHandle); fclose($iniSampleHandle);
} }

View File

@ -79,7 +79,7 @@ class Filesystem extends AbstractData
if (!$this->exists($pasteid)) { if (!$this->exists($pasteid)) {
return false; return false;
} }
$paste = self::_decodeFile(self::_dataid2path($pasteid) . $pasteid . '.php'); $paste = DataStore::get(self::_dataid2path($pasteid) . $pasteid . '.php');
if (property_exists($paste->meta, 'attachment')) { if (property_exists($paste->meta, 'attachment')) {
$paste->attachment = $paste->meta->attachment; $paste->attachment = $paste->meta->attachment;
unset($paste->meta->attachment); unset($paste->meta->attachment);
@ -210,7 +210,7 @@ class Filesystem extends AbstractData
// - commentid is the comment identifier itself. // - commentid is the comment identifier itself.
// - parentid is the comment this comment replies to (It can be pasteid) // - parentid is the comment this comment replies to (It can be pasteid)
if (is_file($discdir . $filename)) { if (is_file($discdir . $filename)) {
$comment = self::_decodeFile($discdir . $filename); $comment = DataStore::get($discdir . $filename);
$items = explode('.', $filename); $items = explode('.', $filename);
// Add some meta information not contained in file. // Add some meta information not contained in file.
$comment->id = $items[1]; $comment->id = $items[1];
@ -285,7 +285,7 @@ class Filesystem extends AbstractData
} }
$thirdLevel = array_filter( $thirdLevel = array_filter(
array_map( array_map(
function($filename) { function ($filename) {
return strlen($filename) >= 20 ? return strlen($filename) >= 20 ?
substr($filename, 0, -4) : substr($filename, 0, -4) :
$filename; $filename;
@ -385,17 +385,4 @@ class Filesystem extends AbstractData
{ {
return (bool) preg_match('/^[a-f0-9]{2}$/', $element); return (bool) preg_match('/^[a-f0-9]{2}$/', $element);
} }
/**
* Decodes a paste or comment file.
*
* @access private
* @static
* @param string $file
* @return array
*/
private static function _decodeFile($file)
{
return json_decode(substr(file_get_contents($file), strlen(DataStore::PROTECTION_LINE . PHP_EOL)));
}
} }

View File

@ -51,4 +51,17 @@ class DataStore extends AbstractPersistence
return false; return false;
} }
} }
/**
* get the data
*
* @access public
* @static
* @param string $filename
* @return array $data
*/
public static function get($filename)
{
return json_decode(substr(file_get_contents($filename), strlen(self::PROTECTION_LINE . PHP_EOL)));
}
} }

View File

@ -134,15 +134,15 @@ class FilesystemTest extends PHPUnit_Framework_TestCase
public function testOldFilesGetConverted() public function testOldFilesGetConverted()
{ {
// generate 10 (default purge batch size) pastes in the old format // generate 10 (default purge batch size) pastes in the old format
$paste = Helper::getPaste(); $paste = Helper::getPaste();
$comment = Helper::getComment(); $comment = Helper::getComment();
$commentid = Helper::getCommentId(); $commentid = Helper::getCommentId();
$ids = array(); $ids = array();
for ($i = 0, $max = 10; $i < $max; ++$i) { for ($i = 0, $max = 10; $i < $max; ++$i) {
// PHPs mt_rand only supports 32 bit or up 0x7fffffff on 64 bit systems to be precise :-/ // PHPs mt_rand only supports 32 bit or up 0x7fffffff on 64 bit systems to be precise :-/
$dataid = str_pad(dechex(mt_rand(0, mt_getrandmax())), 8, '0', STR_PAD_LEFT) . $dataid = str_pad(dechex(mt_rand(0, mt_getrandmax())), 8, '0', STR_PAD_LEFT) .
str_pad(dechex(mt_rand(0, mt_getrandmax())), 8, '0', STR_PAD_LEFT); str_pad(dechex(mt_rand(0, mt_getrandmax())), 8, '0', STR_PAD_LEFT);
$storagedir = $this->_path . DIRECTORY_SEPARATOR . substr($dataid, 0, 2) . $storagedir = $this->_path . DIRECTORY_SEPARATOR . substr($dataid, 0, 2) .
DIRECTORY_SEPARATOR . substr($dataid, 2, 2) . DIRECTORY_SEPARATOR; DIRECTORY_SEPARATOR . substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
$ids[$dataid] = $storagedir; $ids[$dataid] = $storagedir;

View File

@ -111,7 +111,7 @@ class PrivateBinTest extends PHPUnit_Framework_TestCase
*/ */
public function testViewUrlShortener() public function testViewUrlShortener()
{ {
$shortener = 'https://shortener.example.com/api?link='; $shortener = 'https://shortener.example.com/api?link=';
$options = parse_ini_file(CONF, true); $options = parse_ini_file(CONF, true);
$options['main']['urlshortener'] = $shortener; $options['main']['urlshortener'] = $shortener;
Helper::createIniFile(CONF, $options); Helper::createIniFile(CONF, $options);

View File

@ -1,7 +1,6 @@
<?php <?php
use PrivateBin\Data\Database; use PrivateBin\Data\Database;
use PrivateBin\Persistence\ServerSalt;
require_once 'PrivateBinTest.php'; require_once 'PrivateBinTest.php';