abook_global_file.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. ** abook_local_file.php
  4. **
  5. ** Backend for addressbook as a pipe separated file
  6. **
  7. ** An array with the following elements must be passed to
  8. ** the class constructor (elements marked ? are optional):
  9. **
  10. ** NOTE. This class should not be used directly. Use the
  11. ** "AddressBook" class instead.
  12. **
  13. ** Make sure you configure this before using it!
  14. **
  15. ** $Id$
  16. **/
  17. class abook_global_file extends addressbook_backend {
  18. var $btype = 'local';
  19. var $bname = 'global_file';
  20. var $filehandle = 0;
  21. // ========================== Private =======================
  22. // Constructor
  23. function abook_global_file() {
  24. global $address_book_global_filename;
  25. $this->global_filename = $address_book_global_filename;
  26. $this->sname = _("Global address book");
  27. $this->open(true);
  28. }
  29. // Open the addressbook file and store the file pointer.
  30. // Use $file as the file to open, or the class' own
  31. // filename property. If $param is empty and file is
  32. // open, do nothing.
  33. function open($new = false) {
  34. $this->error = '';
  35. // Return true is file is open and $new is unset
  36. if($this->filehandle && !$new)
  37. return true;
  38. // Check that new file exists
  39. if (! file_exists($this->global_filename) ||
  40. ! is_readable($this->global_filename))
  41. return $this->set_error($this->global_filename . ': ' .
  42. _("No such file or directory"));
  43. // Close old file, if any
  44. if ($this->filehandle) $this->close();
  45. // Open file, read only.
  46. $fh = @fopen($this->global_filename, 'r');
  47. $this->writeable = false;
  48. if(! $fh)
  49. return $this->set_error($this->global_filename . ': ' .
  50. _("Open failed"));
  51. $this->filehandle = &$fh;
  52. return true;
  53. }
  54. // Close the file and forget the filehandle
  55. function close() {
  56. @fclose($this->filehandle);
  57. $this->filehandle = 0;
  58. $this->global_filename = '';
  59. $this->writable = false;
  60. }
  61. // ========================== Public ========================
  62. // Search the file
  63. function search($expr) {
  64. // To be replaced by advanded search expression parsing
  65. if(is_array($expr)) return;
  66. // Make regexp from glob'ed expression
  67. // May want to quote other special characters like (, ), -, [, ], etc.
  68. $expr = str_replace('?', '.', $expr);
  69. $expr = str_replace('*', '.*', $expr);
  70. $res = array();
  71. if(!$this->open())
  72. return false;
  73. @rewind($this->filehandle);
  74. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  75. $line = join(' ', $row);
  76. if (eregi($expr, $line)) {
  77. $res[] = array('nickname' => $row[0],
  78. 'name' => $row[1] . ' ' . $row[2],
  79. 'firstname' => $row[1],
  80. 'lastname' => $row[2],
  81. 'email' => $row[3],
  82. 'label' => $row[4],
  83. 'backend' => $this->bnum,
  84. 'source' => &$this->sname);
  85. }
  86. }
  87. return $res;
  88. }
  89. // Lookup alias
  90. function lookup($alias) {
  91. if (empty($alias))
  92. return array();
  93. $alias = strtolower($alias);
  94. $this->open();
  95. @rewind($this->filehandle);
  96. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  97. if (strtolower($row[0]) == $alias) {
  98. return array('nickname' => $row[0],
  99. 'name' => $row[1] . ' ' . $row[2],
  100. 'firstname' => $row[1],
  101. 'lastname' => $row[2],
  102. 'email' => $row[3],
  103. 'label' => $row[4],
  104. 'backend' => $this->bnum,
  105. 'source' => &$this->sname);
  106. }
  107. }
  108. return array();
  109. }
  110. // List all addresses
  111. function list_addr() {
  112. $res = array();
  113. $this->open();
  114. @rewind($this->filehandle);
  115. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  116. $res[] = array('nickname' => $row[0],
  117. 'name' => $row[1] . ' ' . $row[2],
  118. 'firstname' => $row[1],
  119. 'lastname' => $row[2],
  120. 'email' => $row[3],
  121. 'label' => $row[4],
  122. 'backend' => $this->bnum,
  123. 'source' => &$this->sname);
  124. }
  125. return $res;
  126. }
  127. // Add address
  128. function add($userdata) {
  129. $this->set_error(_("Can not modify global address book"));
  130. return false;
  131. }
  132. // Delete address
  133. function remove($alias) {
  134. $this->set_error(_("Can not modify global address book"));
  135. return false;
  136. }
  137. // Modify address
  138. function modify($alias, $userdata) {
  139. $this->set_error(_("Can not modify global address book"));
  140. return false;
  141. }
  142. } // End of class abook_local_file
  143. ?>