@@ -17,6 +17,17 @@ class FileProfilerStorage {
1717 // Folder where profiler data are stored.
1818 private string $ folder ;
1919
20+ /** @psalm-suppress UndefinedClass */
21+ public const allowedClasses = [
22+ \OCA \Profiler \DataCollector \EventLoggerDataProvider::class,
23+ \OCA \Profiler \DataCollector \HttpDataCollector::class,
24+ \OCA \Profiler \DataCollector \MemoryDataCollector::class,
25+ \OCA \User_LDAP \DataCollector \LdapDataCollector::class,
26+ \OC \Memcache \ProfilerWrapperCache::class,
27+ \OC \Profiler \RoutingDataCollector::class,
28+ \OC \DB \DbDataCollector::class,
29+ ];
30+
2031 /**
2132 * Constructs the file storage using a "dsn-like" path.
2233 *
@@ -97,11 +108,21 @@ public function read(string $token): ?IProfile {
97108 return null ;
98109 }
99110
100- if (\function_exists ('gzcompress ' )) {
101- $ file = 'compress.zlib:// ' . $ file ;
111+ $ h = fopen ($ file , 'r ' );
112+ flock ($ h , \LOCK_SH );
113+ $ data = stream_get_contents ($ h );
114+ flock ($ h , \LOCK_UN );
115+ fclose ($ h );
116+
117+ if (\function_exists ('gzdecode ' )) {
118+ $ data = @gzdecode ($ data ) ?: $ data ;
119+ }
120+
121+ if (!$ data = unserialize ($ data , ['allowed_classes ' => self ::allowedClasses])) {
122+ return null ;
102123 }
103124
104- return $ this ->createProfileFromData ($ token , unserialize ( file_get_contents ( $ file )) );
125+ return $ this ->createProfileFromData ($ token , $ data );
105126 }
106127
107128 /**
@@ -139,14 +160,13 @@ public function write(IProfile $profile): bool {
139160 'status_code ' => $ profile ->getStatusCode (),
140161 ];
141162
142- $ context = stream_context_create ( );
163+ $ data = serialize ( $ data );
143164
144- if (\function_exists ('gzcompress ' )) {
145- $ file = 'compress.zlib:// ' . $ file ;
146- stream_context_set_option ($ context , 'zlib ' , 'level ' , 3 );
165+ if (\function_exists ('gzencode ' )) {
166+ $ data = gzencode ($ data , 3 );
147167 }
148168
149- if (file_put_contents ($ file , serialize ( $ data), 0 , $ context ) === false ) {
169+ if (file_put_contents ($ file , $ data, \ LOCK_EX ) === false ) {
150170 return false ;
151171 }
152172
@@ -266,11 +286,21 @@ protected function createProfileFromData(string $token, array $data, ?IProfile $
266286 continue ;
267287 }
268288
269- if (\function_exists ('gzcompress ' )) {
270- $ file = 'compress.zlib:// ' . $ file ;
289+ $ h = fopen ($ file , 'r ' );
290+ flock ($ h , \LOCK_SH );
291+ $ data = stream_get_contents ($ h );
292+ flock ($ h , \LOCK_UN );
293+ fclose ($ h );
294+
295+ if (\function_exists ('gzdecode ' )) {
296+ $ data = @gzdecode ($ data ) ?: $ data ;
297+ }
298+
299+ if (!$ data = unserialize ($ data , ['allowed_classes ' => self ::allowedClasses])) {
300+ continue ;
271301 }
272302
273- $ profile ->addChild ($ this ->createProfileFromData ($ token , unserialize ( file_get_contents ( $ file )) , $ profile ));
303+ $ profile ->addChild ($ this ->createProfileFromData ($ token , $ data , $ profile ));
274304 }
275305
276306 return $ profile ;
0 commit comments