+ // Fetch the permission and group ownership of the "avatar" path and apply to all files
+ $dir_perm = fileperms($dirpath) & 0777;
+ $file_perm = fileperms($dirpath) & 0666;
+ $group = filegroup($dirpath);
+
+ // Check directory permissions of all parts of the path
+ foreach (explode('/', dirname($filename)) as $part) {
+ $dirpath .= $part . '/';
+
+ if (!file_exists($dirpath)) {
+ if (!@mkdir($dirpath, $dir_perm) && !file_exists($dirpath)) {
+ Logger::warning('Directory could not be created', ['directory' => $dirpath]);
+ }
+ } elseif ((($old_perm = fileperms($dirpath) & 0777) != $dir_perm) && !chmod($dirpath, $dir_perm)) {
+ Logger::warning('Directory permissions could not be changed', ['directory' => $dirpath, 'old' => $old_perm, 'new' => $dir_perm]);
+ }
+
+ if ((($old_group = filegroup($dirpath)) != $group) && !chgrp($dirpath, $group)) {
+ Logger::warning('Directory group could not be changed', ['directory' => $dirpath, 'old' => $old_group, 'new' => $group]);
+ }
+ }
+
+ if (!file_put_contents($filepath, $image->asString())) {
+ Logger::warning('File could not be created', ['file' => $filepath]);
+ }
+
+ $old_perm = fileperms($filepath) & 0666;
+ $old_group = filegroup($filepath);
+
+ if (($old_perm != $file_perm) && !chmod($filepath, $file_perm)) {
+ Logger::warning('File permissions could not be changed', ['file' => $filepath, 'old' => $old_perm, 'new' => $file_perm]);
+ }
+
+ if (($old_group != $group) && !chgrp($filepath, $group)) {
+ Logger::warning('File group could not be changed', ['file' => $filepath, 'old' => $old_group, 'new' => $group]);