66
77use PhpList \Core \Domain \Configuration \Model \ConfigOption ;
88use PhpList \Core \Domain \Configuration \Service \Provider \ConfigProvider ;
9+ use Psr \Log \LoggerInterface ;
10+ use Throwable ;
911
1012class ExternalImageService
1113{
1214 private string $ externalCacheDir ;
1315
1416 public function __construct (
1517 private readonly ConfigProvider $ configProvider ,
18+ private readonly LoggerInterface $ logger ,
1619 private readonly string $ tempDir ,
1720 private readonly int $ externalImageMaxAge ,
1821 private readonly int $ externalImageMaxSize ,
@@ -74,26 +77,31 @@ public function cache($filename, $messageId): bool
7477
7578 private function removeOldFilesInCache (): void
7679 {
80+ // phpcs:ignore Generic.PHP.NoSilencedErrors
7781 $ extCacheDirHandle = @opendir ($ this ->externalCacheDir );
7882 if (!$ this ->externalImageMaxAge || !$ extCacheDirHandle ) {
79- return ;
83+ return ;
8084 }
8185
82- while (($ cacheFile = @readdir ($ extCacheDirHandle )) !== false ) {
86+ while (true ) {
87+ // phpcs:ignore Generic.PHP.NoSilencedErrors
88+ $ cacheFile = @readdir ($ extCacheDirHandle );
89+
90+ if ($ cacheFile === false ) {
91+ break ;
92+ }
8393 // todo: make sure that this is what we need
8494 if (!str_starts_with ($ cacheFile , '. ' )) {
85- $ cacheFileMTime = @filemtime ($ this ->externalCacheDir .'/ ' .$ cacheFile );
86-
87- if (
88- is_numeric ($ cacheFileMTime )
89- && ($ cacheFileMTime > 0 )
90- && ((time () - $ cacheFileMTime ) > $ this ->externalImageMaxAge )
91- ) {
92- @unlink ($ this ->externalCacheDir .'/ ' .$ cacheFile );
95+ // phpcs:ignore Generic.PHP.NoSilencedErrors
96+ $ cfmt = @filemtime ($ this ->externalCacheDir . '/ ' . $ cacheFile );
97+
98+ if (is_numeric ($ cfmt ) && ($ cfmt > 0 ) && ((time () - $ cfmt ) > $ this ->externalImageMaxAge )) {
99+ // phpcs:ignore Generic.PHP.NoSilencedErrors
100+ @unlink ($ this ->externalCacheDir . '/ ' . $ cacheFile );
93101 }
94102 }
95103 }
96-
104+ // phpcs:ignore Generic.PHP.NoSilencedErrors
97105 @closedir ($ extCacheDirHandle );
98106 }
99107
@@ -158,8 +166,7 @@ private function downloadUsingFileGetContent(string $filename): string
158166
159167 private function isCacheableUrl ($ filename ): bool
160168 {
161- if (
162- !(str_starts_with ($ filename , 'http ' ))
169+ if (!(str_starts_with ($ filename , 'http ' ))
163170 || str_contains ($ filename , ':// ' . $ this ->configProvider ->getValue (ConfigOption::Website) . '/ ' )
164171 ) {
165172 return false ;
@@ -172,6 +179,7 @@ private function ensureCacheDirectory(): bool
172179 {
173180
174181 if (!file_exists ($ this ->externalCacheDir )) {
182+ // phpcs:ignore Generic.PHP.NoSilencedErrors
175183 @mkdir ($ this ->externalCacheDir );
176184 }
177185
@@ -184,7 +192,7 @@ private function ensureCacheDirectory(): bool
184192
185193 private function isValidCacheFile (string $ cacheFileName ): bool
186194 {
187-
195+ // phpcs:ignore Generic.PHP.NoSilencedErrors
188196 if (file_exists ($ cacheFileName ) && (@filesize ($ cacheFileName ) > 64 )) {
189197 return true ;
190198 }
@@ -194,14 +202,28 @@ private function isValidCacheFile(string $cacheFileName): bool
194202
195203 private function writeCacheFile (string $ cacheFileName , $ content ): void
196204 {
197- $ cacheFileHandle = @fopen ($ cacheFileName , 'wb ' );
198- if ($ cacheFileHandle !== false ) {
199- if (flock ($ cacheFileHandle , LOCK_EX )) {
200- fwrite ($ cacheFileHandle , $ content );
201- fflush ($ cacheFileHandle );
202- flock ($ cacheFileHandle , LOCK_UN );
205+ try {
206+ $ handle = fopen ($ cacheFileName , 'wb ' );
207+
208+ if ($ handle === false ) {
209+ $ this ->logger ->error ('Cannot open cache file ' , [
210+ 'file ' => $ cacheFileName ,
211+ ]);
212+ return ;
213+ }
214+
215+ if (flock ($ handle , LOCK_EX )) {
216+ fwrite ($ handle , $ content );
217+ fflush ($ handle );
218+ flock ($ handle , LOCK_UN );
203219 }
204- fclose ($ cacheFileHandle );
220+
221+ fclose ($ handle );
222+ } catch (Throwable $ e ) {
223+ $ this ->logger ->error ('Cache file write failed ' , [
224+ 'file ' => $ cacheFileName ,
225+ 'error ' => $ e ->getMessage (),
226+ ]);
205227 }
206228 }
207229}
0 commit comments