abook_global_file.php 4.8 KB

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