You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
1.7 KiB

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