Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 77 additions & 59 deletions system/Helpers/filesystem_helper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* CodeIgniter
*
Expand Down Expand Up @@ -35,7 +36,6 @@
* @since Version 1.0.0
* @filesource
*/

/**
* CodeIgniter Directory Helpers
*
Expand Down Expand Up @@ -66,8 +66,10 @@
*/
function directory_map(string $source_dir, int $directory_depth = 0, bool $hidden = false): array
{
if ($fp = @opendir($source_dir))
try
{
$fp = opendir($source_dir);

$filedata = [];
$new_depth = $directory_depth - 1;
$source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -95,8 +97,10 @@ function directory_map(string $source_dir, int $directory_depth = 0, bool $hidde
closedir($fp);
return $filedata;
}

return [];
catch (\Exception $fe)
{
return [];
}
}

}
Expand All @@ -120,25 +124,29 @@ function directory_map(string $source_dir, int $directory_depth = 0, bool $hidde
*/
function write_file(string $path, string $data, string $mode = 'wb'): bool
{
if ( ! $fp = @fopen($path, $mode))
try
{
return false;
}
$fp = fopen($path, $mode);

flock($fp, LOCK_EX);
flock($fp, LOCK_EX);

for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
{
if (($result = fwrite($fp, substr($data, $written))) === false)
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
{
break;
if (($result = fwrite($fp, substr($data, $written))) === false)
{
break;
}
}
}

flock($fp, LOCK_UN);
fclose($fp);
flock($fp, LOCK_UN);
fclose($fp);

return is_int($result);
return is_int($result);
}
catch (\Exception $fe)
{
return false;
}
}

}
Expand Down Expand Up @@ -168,29 +176,33 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
// Trim the trailing slash
$path = rtrim($path, '/\\');

if ( ! $current_dir = @opendir($path))
try
{
return false;
}
$current_dir = opendir($path);

while (false !== ($filename = @readdir($current_dir)))
{
if ($filename !== '.' && $filename !== '..')
while (false !== ($filename = @readdir($current_dir)))
{
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
{
delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1);
}
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
if ($filename !== '.' && $filename !== '..')
{
@unlink($path . DIRECTORY_SEPARATOR . $filename);
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
{
delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1);
}
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
{
@unlink($path . DIRECTORY_SEPARATOR . $filename);
}
}
}
}

closedir($current_dir);
closedir($current_dir);

return ($delDir === true && $_level > 0) ? @rmdir($path) : true;
return ($delDir === true && $_level > 0) ? @rmdir($path) : true;
}
catch (\Exception $fe)
{
return false;
}
}

}
Expand All @@ -216,8 +228,9 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
{
static $filedata = [];

if ($fp = @opendir($source_dir))
try
{
$fp = opendir($source_dir);
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
Expand All @@ -240,8 +253,10 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
closedir($fp);
return $filedata;
}

return [];
catch (\Exception $fe)
{
return [];
}
}

}
Expand Down Expand Up @@ -270,34 +285,38 @@ function get_dir_file_info(string $source_dir, bool $top_level_only = true, bool
static $filedata = [];
$relative_path = $source_dir;

if ($fp = @opendir($source_dir))
try
{
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
$filedata = [];
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}

// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
while (false !== ($file = readdir($fp)))
{
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
$fp = @opendir($source_dir); {
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
$filedata = [];
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
elseif ($file[0] !== '.')

// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
while (false !== ($file = readdir($fp)))
{
$filedata[$file] = get_file_info($source_dir . $file);
$filedata[$file]['relative_path'] = $relative_path;
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
{
get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
}
elseif ($file[0] !== '.')
{
$filedata[$file] = get_file_info($source_dir . $file);
$filedata[$file]['relative_path'] = $relative_path;
}
}
}

closedir($fp);
return $filedata;
closedir($fp);
return $filedata;
}
}
catch (\Exception $fe)
{
return [];
}

return [];
}

}
Expand All @@ -318,9 +337,9 @@ function get_dir_file_info(string $source_dir, bool $top_level_only = true, bool
* @param string $file Path to file
* @param mixed $returned_values Array or comma separated string of information returned
*
* @return array
* @return array|null
*/
function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date']): array
function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date'])
{
if ( ! file_exists($file))
{
Expand All @@ -334,8 +353,7 @@ function get_file_info(string $file, $returned_values = ['name', 'server_path',

foreach ($returned_values as $key)
{
switch ($key)
{
switch ($key) {
case 'name':
$fileinfo['name'] = basename($file);
break;
Expand Down
16 changes: 16 additions & 0 deletions tests/system/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ public function testArrayDotWildcardWithMultipleChoices()
$this->assertEquals(23, dot_array_search('foo.*.baz', $data));
}

public function testArrayDotNestedNotFound()
{
$data = [
'foo' => [
'buzz' => [
'fizz' => 11
],
'bar' => [
'baz' => 23
]
]
];

$this->assertNull(dot_array_search('foo.*.notthere', $data));
}

public function testArrayDotIgnoresLastWildcard()
{
$data = [
Expand Down
48 changes: 26 additions & 22 deletions tests/system/Helpers/CookieHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use CodeIgniter\Config\Services;
use Tests\Support\HTTP\MockResponse;

final class cookieHelperTest extends \CIUnitTestCase
final class CookieHelperTest extends \CIUnitTestCase
{

private $name;
Expand All @@ -22,6 +22,8 @@ public function setUp()

Services::injectMock('response', new MockResponse(new App()));
$this->response = service('response');
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent());
Services::injectMock('request', $this->request);

helper('cookie');
}
Expand All @@ -30,14 +32,11 @@ public function setUp()

public function testSetCookie()
{
$this->response->setCookie($this->name, $this->value, $this->expire);

//TODO: Find a way for set_cookie() to use the MockResponse object.
//set_cookie($this->name, $this->value, $this->expire);
set_cookie($this->name, $this->value, $this->expire);

$this->assertTrue($this->response->hasCookie($this->name));

$this->response->deleteCookie($this->name);
delete_cookie($this->name);
}

//--------------------------------------------------------------------
Expand All @@ -49,17 +48,16 @@ public function testSetCookieByArrayParameters()
'value' => $this->value,
'expire' => $this->expire
];
//set_cookie($cookieAttr);
$this->response->setCookie($cookieAttr);

set_cookie($cookieAttr);

$this->assertTrue($this->response->hasCookie($this->name, $this->value));

$this->response->deleteCookie($this->name);
delete_cookie($this->name);
}

//--------------------------------------------------------------------

public function testGetCookie()
public function testSetCookieSecured()
{
$pre = 'Hello, I try to';
$pst = 'your site';
Expand All @@ -68,29 +66,35 @@ public function testGetCookie()
$unsecured = 'unsecured';
$secured = 'secured';

//set_cookie($unsecured, $unsec, $this->expire);
//set_cookie($secured, $sec, $this->expire);
$this->response->setCookie($unsecured, $unsec, $this->expire);
$this->response->setCookie($secured, $sec, $this->expire);
set_cookie($unsecured, $unsec, $this->expire);
set_cookie($secured, $sec, $this->expire);

$this->assertTrue($this->response->hasCookie($unsecured, $unsec));
$this->assertTrue($this->response->hasCookie($secured, $sec));

$this->response->deleteCookie($unsecured);
$this->response->deleteCookie($secured);
delete_cookie($unsecured);
delete_cookie($secured);
}

//--------------------------------------------------------------------

public function testDeleteCookie()
{
//set_cookie($this->name, $this->value, $this->expire);
$this->response->setCookie($this->name, $this->value, $this->expire);
set_cookie($this->name, $this->value, $this->expire);
//$this->response->setCookie($this->name, $this->value, $this->expire);

$this->response->deleteCookie($this->name);
delete_cookie($this->name);

//$this->assertEquals(get_cookie($this->name), '');
$this->assertTrue($this->response->hasCookie($this->name));
$this->assertEmpty($this->response->getCookie($this->name));
}

//--------------------------------------------------------------------

public function testGetCookie()
{
$_COOKIE['TEST'] = 5;

$this->assertEquals(5, get_cookie('TEST'));
}

}
Loading