Skip to content
This repository was archived by the owner on May 19, 2020. It is now read-only.

Commit e5d7c07

Browse files
committed
Preventive measure if permission is denied for firewall file
1 parent fc2d3c9 commit e5d7c07

File tree

12 files changed

+117
-223
lines changed

12 files changed

+117
-223
lines changed

app/Disk.php

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,39 @@
99
class Disk
1010
{
1111
/**
12-
* Write a file into Disk. Creates if it doesn't exist.
12+
* Check whether a file can be written in the Disk.
1313
*
1414
* @param File $file
15+
* @return bool
1516
*/
16-
public function write(File $file)
17+
public function writable(File $file)
1718
{
18-
if (!is_file($file->path)) {
19-
$this->touch($file->path);
20-
}
21-
22-
$this->put($file->path, $file->content());
19+
return $this->isWritable($file->path);
2320
}
2421

25-
public function checksum(File $file)
22+
/**
23+
* Write a file into Disk. Creates if it doesn't exist.
24+
*
25+
* @param File $file
26+
*/
27+
public function write(File $file)
2628
{
27-
if (is_file($file->path)) {
28-
return md5_file($file->path);
29+
if (! $this->fileExists($file->path)) {
30+
$this->touch($file->path);
2931
}
3032

31-
return md5('');
33+
$this->put($file->path, $file->content);
3234
}
3335

36+
/**
37+
* Check if the content in the database match the content in the Disk file.
38+
*
39+
* @param File $file
40+
* @return bool
41+
*/
3442
public function match(File $file)
3543
{
36-
return $this->checksum($file) == $file->checksum;
44+
return $this->hash($file->path) == $file->checksum;
3745
}
3846

3947
protected function touch($filepath)
@@ -66,11 +74,55 @@ protected function recursiveMakeDirectory($filepath)
6674
{
6775
try {
6876
$directory = dirname($filepath);
69-
if (!is_dir($directory)) {
77+
if (! is_dir($directory)) {
7078
mkdir($directory, 0755, true);
7179
}
7280
} catch (ErrorException $e) {
7381
throw new PermissionDeniedException($filepath);
7482
}
7583
}
84+
85+
/**
86+
* Return true if the file or directory exists.
87+
*
88+
* @param $path
89+
* @return bool
90+
*/
91+
protected function fileExists($path)
92+
{
93+
return file_exists($path);
94+
}
95+
96+
protected function hash($path)
97+
{
98+
if ($this->fileExists($path)) {
99+
return md5_file($path);
100+
}
101+
102+
return md5('');
103+
}
104+
105+
protected function isDirectory($path)
106+
{
107+
return is_dir($path);
108+
}
109+
110+
/**
111+
* Check if a path is writable.
112+
*
113+
* @param string $path
114+
* @return bool
115+
*/
116+
protected function isWritable($path)
117+
{
118+
// If the file or directory exists, we can just check if it's writable.
119+
if ($this->fileExists($path)) {
120+
return is_writable($path);
121+
}
122+
123+
// Since it doesn't exist, let's check if we have permission in the parent directory
124+
$directory = dirname($path);
125+
126+
return $this->isWritable($directory);
127+
}
76128
}

app/Models/File.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @property Collection sections
1212
* @property string path
1313
* @property string checksum
14+
* @property string content
1415
*/
1516
class File extends Model
1617
{
@@ -28,7 +29,7 @@ public function sections()
2829
return $this->hasMany(Section::class, 'file_id');
2930
}
3031

31-
public function content()
32+
public function getContentAttribute()
3233
{
3334
$this->loadMissing('sections');
3435

@@ -40,7 +41,7 @@ public function getChecksumAttribute()
4041
{
4142
// @TODO: Maybe improve performance by adding a checksum column to the file table and keeping it up to date with content change
4243
// instead of recalculating it real time.
43-
return md5($this->content());
44+
return md5($this->content);
4445
}
4546

4647
public function getSynchronizedAttribute()

config/app.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
'name' => env('APP_NAME', 'Thunderwall'),
1717

18-
'version' => '0.0.13',
18+
'version' => '0.0.14',
1919

2020
/*
2121
|--------------------------------------------------------------------------
@@ -229,6 +229,7 @@
229229
'Validator' => Illuminate\Support\Facades\Validator::class,
230230
'View' => Illuminate\Support\Facades\View::class,
231231

232+
'Disk' => Facades\App\Disk::class
232233
],
233234

234235
];

resources/views/app/files/create.blade.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

resources/views/app/files/index.blade.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

resources/views/app/files/retrieve.blade.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

resources/views/app/files/sections/edit.blade.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

resources/views/app/files/sections/index.blade.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

resources/views/app/modules/firewall/edit.blade.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
<div class="row">
66
<div class="col-md-8 col-md-offset-2">
77

8+
@if(! Disk::writable($file))
9+
<div class="alert alert-danger">
10+
<i class="fa fa-warning"></i> {{ __('Permission denied to file :file', ['file' => $file->path]) }}
11+
</div>
12+
@endif
13+
814
<form method="POST" action="/modules/firewall/{{ $file->id }}">
915
{{ method_field('PUT') }}
1016
{{ csrf_field() }}
@@ -27,11 +33,13 @@
2733
@endforeach
2834
</div>
2935

30-
<div class="panel-footer">
31-
<div class="form-group">
32-
<input type="submit" class="btn btn-primary" value="{{ __('Apply') }}">
36+
@if(Disk::writable($file))
37+
<div class="panel-footer">
38+
<div class="form-group">
39+
<input type="submit" class="btn btn-primary" value="{{ __('Apply') }}">
40+
</div>
3341
</div>
34-
</div>
42+
@endif
3543
</div>
3644

3745
</form>

0 commit comments

Comments
 (0)