Skip to content

Commit b4eafa9

Browse files
committed
phpsec#38 First implementation of file system storage class.
1 parent 77dff85 commit b4eafa9

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

phpsec/phpsec.store.filesystem.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,58 @@
1111

1212
class phpsecStoreFilesystem extends phpsecStore {
1313

14+
private $_dataDir = null;
15+
public $_hashType = 'sha256';
16+
17+
public function __construct($loc) {
18+
if(!is_writeable($loc)) {
19+
phpsec::error('Storage directory('.$loc.') not writeable');
20+
return false;
21+
}
22+
$this->_dataDir = $loc;
23+
return true;
24+
}
25+
26+
public function read($type, $id) {
27+
$fileName = $this->fileName($type, $id);
28+
if(!file_exists($fileName)) {
29+
return false;
30+
}
31+
$data = json_decode(file_get_contents($fileName));
32+
$mac = phpsecCrypt::pbkdf2($data->data, $id, 1000, 32);
33+
34+
if($mac != base64_decode($data->mac)) {
35+
phpsec::error('Message authentication code invalid while reading store');
36+
return false;
37+
}
38+
return unserialize(base64_decode($data->data));
39+
}
40+
41+
public function write($type, $id, $data) {
42+
$fileName = $this->fileName($type, $id);
43+
$saveData['data'] = base64_encode(serialize($data));
44+
$saveData['mac'] = base64_encode(phpsecCrypt::pbkdf2($saveData['data'], $id, 1000, 32));
45+
46+
$data = json_encode($saveData);
47+
$fp = fopen($fileName, 'w');
48+
if($fp !== false) {
49+
if(flock($fp, LOCK_EX)) {
50+
fwrite($fp, $data);
51+
flock($fp, LOCK_UN);
52+
fclose($fp);
53+
return true;
54+
} else {
55+
self::error('Could not lock logfile while writing to store');
56+
}
57+
}
58+
return false;
59+
}
60+
61+
public function delete($type, $id) {
62+
@unlink(fileName($type, $id));
63+
}
64+
65+
private function fileName($type, $id) {
66+
return $this->_dataDir.'/store_'.$type.'_'.hash($this->_hashType, $id);
67+
}
1468
}

0 commit comments

Comments
 (0)