1
+ <?php
2
+
3
+ if (!class_exists ("DbaReader " )) require_once ("dbareader.inc " );
4
+
5
+ /** \ingroup Examples
6
+ * @brief This implements a DBA Array
7
+ * @author Marcus Boerger
8
+ * @version 1.0
9
+ */
10
+ class DbaArray extends DbaReader implements ArrayAccess
11
+ {
12
+
13
+ /**
14
+ * Open database $file with $handler in read only mode.
15
+ *
16
+ * @param file Database file to open.
17
+ * @param handler Handler to use for database access.
18
+ */
19
+ function __construct ($ file , $ handler )
20
+ {
21
+ $ this ->db = dba_popen ($ file , "c " , $ handler );
22
+ if (!$ this ->db ) {
23
+ throw new exception ("Databse could not be opened " );
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Close database.
29
+ */
30
+ function __destruct ()
31
+ {
32
+ parent ::__destruct ();
33
+ }
34
+
35
+ /**
36
+ * Read an entry.
37
+ *
38
+ * @param $name key to read from
39
+ * @return value associated with $name
40
+ */
41
+ function offsetGet ($ name )
42
+ {
43
+ $ data = dba_fetch ($ name , $ this ->db );
44
+ if ($ data ) {
45
+ if (ini_get ('magic_quotes_runtime ' )) {
46
+ $ data = stripslashes ($ data );
47
+ }
48
+ //return unserialize($data);
49
+ return $ data ;
50
+ }
51
+ else
52
+ {
53
+ return NULL ;
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Set an entry.
59
+ *
60
+ * @param $name key to write to
61
+ * @param $value value to write
62
+ */
63
+ function offsetSet ($ name , $ value )
64
+ {
65
+ //dba_replace($name, serialize($value), $this->db);
66
+ dba_replace ($ name , $ value , $ this ->db );
67
+ return $ value ;
68
+ }
69
+
70
+ /**
71
+ * @return whether key $name exists.
72
+ */
73
+ function offsetExists ($ name )
74
+ {
75
+ return dba_exists ($ name , $ this ->db );
76
+ }
77
+
78
+ /**
79
+ * Delete a key/value pair.
80
+ *
81
+ * @param $name key to delete.
82
+ */
83
+ function offsetUnset ($ name )
84
+ {
85
+ return dba_delete ($ name , $ this ->db );
86
+ }
87
+ }
88
+
89
+ ?>
0 commit comments