Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Added #10 a new configuration variable safe_filename. By default true…
Browse files Browse the repository at this point in the history
…, suppresses any file name errors and converts the file name to a valid name, if set to false, an exception will be thrown upon a invalid name.
  • Loading branch information
timothymarois committed Dec 15, 2017
1 parent 7052246 commit 7ba1d6b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Change Log
==========

### 12/14/2017 - 1.0.13
* Added #10 a new configuration variable `safe_filename`. By default `true`, suppresses any file name errors and converts the file name to a valid name, if set to `false`, an exception will be thrown upon a invalid name. All users who update will notice no errors will appear upon a invalid name. Set `safe_filename` to `false` if you prefer the exception to be thrown.

### 12/11/2017 - 1.0.12
* Added #8 - `select()` method on query class. Now allows you to specify which fields you want your documents to return. *Note: using `select` means your documents will not return document objects but only data arrays.* This will allow you to only include the fields you want to use for your current task. (Excluding the rest and reducing memory usage).
* Added `last()` method on query class to return the last item in the result array (opposite of `first()`)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ $db = new \Filebase\Database([
'cache' => true,
'cache_expires' => 1800,
'pretty' => true,
'safe_filename' => true,
'validate' => [
'name' => [
'valid.type' => 'string',
Expand All @@ -101,6 +102,7 @@ $db = new \Filebase\Database([
|`cache` |bool |false |Stores [query](https://github.com/tmarois/Filebase#8-queries) results into cache for faster loading. |
|`cache_expire` |int |1800 |How long caching will last (in seconds) |
|`pretty` |bool |true |Store the data for human readability? Pretty Print |
|`safe_filename` |bool |true |Automatically converts the file name to a valid name |


## (2) Formatting
Expand Down Expand Up @@ -380,7 +382,7 @@ To run the query use `results()` or if you only want to return the first item us
|`where()` | `mixed` | `array` for simple "equal to" OR `where($field, $operator, $value)` |
|`andWhere()` | `mixed` | see `where()`, uses the logical `AND` |
|`orWhere()` | `mixed` | see `where()`, this uses the logical `OR` |
|`limit()` | `int` | How many documents to return |
|`limit()` | `int` limit, `int` offset | How many documents to return, and offset |
|`orderBy()` | `field` , `sort order` | Order documents by a specific field and order by `ASC` or `DESC` |


Expand Down
12 changes: 12 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ class Config
public $cache_expires = 1800;


/**
* $safe_filename
* (if true) Be sure to automatically change the file name if it does not fit validation
* (if false) File names that are not valid will thrown an error.
*
* default true
*/
public $safe_filename = true;


/**
* $backupLocation
* The location to store backups
Expand Down Expand Up @@ -75,11 +85,13 @@ class Config
*/
public function __construct($config)
{
// let's define all our config variables
foreach ($config as $key => $value)
{
$this->{$key} = $value;
}

// if "backupLocation" is not set, let's set one automatically
if (!isset($config['backupLocation']))
{
$this->backupLocation = $this->dir.'/backups';
Expand Down
8 changes: 4 additions & 4 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Database
* Stores the version of Filebase
* use $db->getVersion()
*/
const VERSION = '1.0.12';
const VERSION = '1.0.13';


//--------------------------------------------------------------------
Expand Down Expand Up @@ -217,7 +217,7 @@ public function save(Document $document, $wdata = '')
{
$id = $document->getId();
$file_extension = $this->config->format::getFileExtension();
$file_location = $this->config->dir.'/'.Filesystem::validateName($id).'.'.$file_extension;
$file_location = $this->config->dir.'/'.Filesystem::validateName($id, $this->config->safe_filename).'.'.$file_extension;
$created = $document->createdAt(false);

if (isset($wdata) && $wdata !== '')
Expand Down Expand Up @@ -274,7 +274,7 @@ public function query()
*/
protected function read($name)
{
return $this->config->format::decode( Filesystem::read( $this->config->dir.'/'.Filesystem::validateName($name).'.'.$this->config->format::getFileExtension() ) );
return $this->config->format::decode( Filesystem::read( $this->config->dir.'/'.Filesystem::validateName($name, $this->config->safe_filename).'.'.$this->config->format::getFileExtension() ) );
}


Expand All @@ -289,7 +289,7 @@ protected function read($name)
*/
public function delete(Document $document)
{
return Filesystem::delete($this->config->dir.'/'.Filesystem::validateName($document->getId()).'.'.$this->config->format::getFileExtension());
return Filesystem::delete($this->config->dir.'/'.Filesystem::validateName($document->getId(), $this->config->safe_filename).'.'.$this->config->format::getFileExtension());
}


Expand Down
16 changes: 14 additions & 2 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,26 @@ public static function delete($path)
* filesystem.
*
* @param string $name The name to validate against
* @param boolean $safe_filename Allows filename to be converted if fails validation
*
* @return bool Returns true if valid. Throws an exception if not.
*/
public static function validateName($name)
public static function validateName($name, $safe_filename)
{
if (!preg_match('/^[0-9A-Za-z\_\-]{1,63}$/', $name))
{
throw new \Exception(sprintf('`%s` is not a valid file name.', $name));
if ($safe_filename === true)
{
// rename the file
$name = preg_replace('/[^0-9A-Za-z\_\-]/','', $name);

// limit the file name size
$name = substr($name,0,63);
}
else
{
throw new \Exception(sprintf('`%s` is not a valid file name.', $name));
}
}

return $name;
Expand Down
22 changes: 21 additions & 1 deletion tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ public function testBadNameException()
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases'
'dir' => __DIR__.'/databases',
'safe_filename' => false
]);

$db->flush(true);
Expand All @@ -520,4 +521,23 @@ public function testBadNameException()
$db->flush(true);
}


public function testBadNameReplacement()
{
$badName = 'ti^@%mo!!~th*y-m_?a(ro%)is.&';
$newName = Filesystem::validateName($badName, true);

$this->assertEquals('timothy-m_arois', $newName);
}


public function testBadNameReplacementLong()
{
$badName = '1234567890123456789012345678901234567890123456789012345678901234';
$newName = Filesystem::validateName($badName, true);

$this->assertEquals(63, (strlen($newName)) );
$this->assertEquals('123456789012345678901234567890123456789012345678901234567890123', $newName);
}

}

0 comments on commit 7ba1d6b

Please sign in to comment.