@@ -24,30 +24,35 @@ public function __construct($loc) {
24
24
}
25
25
26
26
public function read ($ type , $ id ) {
27
- $ fileName = $ this ->fileName ($ type , $ id );
27
+ $ fileName = $ this ->fileName ($ type , $ id );
28
28
if (!file_exists ($ fileName )) {
29
29
return false ;
30
30
}
31
- $ data = json_decode (file_get_contents ($ fileName ));
32
- $ mac = phpsecCrypt::pbkdf2 ($ data ->data , $ id , 1000 , 32 );
31
+ $ data = file_get_contents ($ fileName );
32
+ list ($ meta , $ data ) = explode ("\n\n" , $ data );
33
+ $ jsonData = json_decode ($ meta );
34
+
35
+ $ mac = phpsecCrypt::pbkdf2 ($ data , $ id , 1000 , 32 );
33
36
34
- if ($ mac != base64_decode ($ data ->mac )) {
37
+ if ($ mac != base64_decode ($ jsonData ->mac )) {
35
38
phpsec::error ('Message authentication code invalid while reading store ' );
36
39
return false ;
37
40
}
38
- return unserialize (base64_decode ( $ data-> data ) );
41
+ return unserialize ($ data );
39
42
}
40
43
41
44
public function write ($ type , $ id , $ data ) {
42
45
$ fileName = $ this ->fileName ($ type , $ id );
46
+
47
+ $ data = serialize ($ data );
43
48
$ saveData ['id ' ] = base64_encode ($ id );
44
- $ saveData ['data ' ] = base64_encode (serialize ($ data ));
45
- $ saveData ['mac ' ] = base64_encode (phpsecCrypt::pbkdf2 ($ saveData ['data ' ], $ id , 1000 , 32 ));
49
+ $ saveData ['mac ' ] = base64_encode (phpsecCrypt::pbkdf2 ($ data , $ id , 1000 , 32 ));
46
50
47
- $ data = json_encode ($ saveData );
51
+ $ jsonData = json_encode ($ saveData );
48
52
$ fp = fopen ($ fileName , 'w ' );
49
53
if ($ fp !== false ) {
50
54
if (flock ($ fp , LOCK_EX )) {
55
+ fwrite ($ fp , $ jsonData ."\n\n" );
51
56
fwrite ($ fp , $ data );
52
57
flock ($ fp , LOCK_UN );
53
58
fclose ($ fp );
@@ -67,8 +72,11 @@ public function listIds($type) {
67
72
$ ids = array ();
68
73
$ files = glob ($ this ->_dataDir .'/store_ ' .$ type .'_* ' );
69
74
foreach ($ files as $ file ) {
70
- $ data = json_decode (file_get_contents ($ file ));
71
- $ ids [] = base64_decode ($ data ->id );
75
+ $ data = file_get_contents ($ file );
76
+
77
+ list ($ meta , $ data ) = explode ("\n\n" , $ data );
78
+ $ jsonData = json_decode ($ meta );
79
+ $ ids [] = base64_decode ($ jsonData ->id );
72
80
73
81
}
74
82
return $ ids ;
0 commit comments