-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #148 change permissions when writing a file
- Loading branch information
1 parent
423da90
commit 3f3d666
Showing
10 changed files
with
113 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Wcms; | ||
|
||
class Chmodexception extends Ioexception | ||
{ | ||
public function __construct( | ||
string $message, | ||
int $permissions, | ||
int $code = 0, | ||
?\Throwable $previous = null | ||
) { | ||
$message .= sprintf(' permissions: %o', $permissions); | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Wcms; | ||
|
||
use RuntimeException; | ||
|
||
class Ioexception extends RuntimeException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
<?php | ||
|
||
use Wcms\Chmodexception; | ||
use Wcms\Ioexception; | ||
use Wcms\Mediaopt; | ||
use Wcms\Model; | ||
|
||
use function Clue\StreamFilter\fun; | ||
|
||
|
@@ -469,3 +472,29 @@ function parse_size($size) | |
return round($size); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $filename Path to the file where to write the data. | ||
* @param mixed $data The data to write. Can be either a string, an array or a stream resource. | ||
* @param int $permissions in octal value | ||
* | ||
* @throws Ioexception when file_put_contents fails | ||
* @throws Chmodexception when chmod fails | ||
*/ | ||
function file_put_content_chmod(string $filename, $data, int $permissions): int | ||
{ | ||
$create = !file_exists($filename); | ||
$length = file_put_contents($filename, $data); | ||
if ($length === false) { | ||
throw new Ioexception("Error while writing $filename"); | ||
} | ||
if ($create) { | ||
if ($permissions < 0700 || $permissions > 0777) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
n-peugnet
Collaborator
|
||
throw new Chmodexception("Incorrect value", $permissions); | ||
} | ||
if (!chmod($filename, $permissions)) { | ||
throw new Chmodexception("Error while setting file permissions $filename", $permissions); | ||
} | ||
} | ||
return $length; | ||
} |
Woops we left
0700
here. Maybe you should completely remove thisif
block.