abook_global_file.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * abook_global_file.php
  4. *
  5. * Copyright (c) 1999-2004 The SquirrelMail Project 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. * @version $Id$
  19. * @package squirrelmail
  20. * @subpackage addressbook
  21. */
  22. /**
  23. * Undocumented class - fixme
  24. * @package squirrelmail
  25. */
  26. class abook_global_file extends addressbook_backend {
  27. var $btype = 'local';
  28. var $bname = 'global_file';
  29. var $filehandle = 0;
  30. /* ========================== Private ======================= */
  31. /* Constructor */
  32. function abook_global_file() {
  33. global $address_book_global_filename;
  34. $this->global_filename = $address_book_global_filename;
  35. $this->sname = _("Global address book");
  36. $this->open(true);
  37. }
  38. /* Open the addressbook file and store the file pointer.
  39. * Use $file as the file to open, or the class' own
  40. * filename property. If $param is empty and file is
  41. * open, do nothing. */
  42. function open($new = false) {
  43. $this->error = '';
  44. /* Return true is file is open and $new is unset */
  45. if($this->filehandle && !$new) {
  46. return true;
  47. }
  48. /* Check that new file exists */
  49. if (! file_exists($this->global_filename) ||
  50. ! is_readable($this->global_filename)) {
  51. return $this->set_error($this->global_filename . ': ' .
  52. _("No such file or directory"));
  53. }
  54. /* Close old file, if any */
  55. if ($this->filehandle) {
  56. $this->close();
  57. }
  58. /* Open file, read only. */
  59. $fh = @fopen($this->global_filename, 'r');
  60. $this->writeable = false;
  61. if(! $fh) {
  62. return $this->set_error($this->global_filename . ': ' .
  63. _("Open failed"));
  64. }
  65. $this->filehandle = &$fh;
  66. return true;
  67. }
  68. /* Close the file and forget the filehandle */
  69. function close() {
  70. @fclose($this->filehandle);
  71. $this->filehandle = 0;
  72. $this->global_filename = '';
  73. $this->writable = false;
  74. }
  75. /* ========================== Public ======================== */
  76. /* Search the file */
  77. function search($expr) {
  78. /* To be replaced by advanded search expression parsing */
  79. if(is_array($expr)) {
  80. return;
  81. }
  82. /* Make regexp from glob'ed expression
  83. * May want to quote other special characters like (, ), -, [, ], etc. */
  84. $expr = str_replace('?', '.', $expr);
  85. $expr = str_replace('*', '.*', $expr);
  86. $res = array();
  87. if(!$this->open()) {
  88. return false;
  89. }
  90. @rewind($this->filehandle);
  91. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  92. $line = join(' ', $row);
  93. if (eregi($expr, $line)) {
  94. $res[] = array('nickname' => $row[0],
  95. 'name' => $row[1] . ' ' . $row[2],
  96. 'firstname' => $row[1],
  97. 'lastname' => $row[2],
  98. 'email' => $row[3],
  99. 'label' => $row[4],
  100. 'backend' => $this->bnum,
  101. 'source' => &$this->sname);
  102. }
  103. }
  104. return $res;
  105. }
  106. /* Lookup alias */
  107. function lookup($alias) {
  108. if (empty($alias)) {
  109. return array();
  110. }
  111. $alias = strtolower($alias);
  112. $this->open();
  113. @rewind($this->filehandle);
  114. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  115. if (strtolower($row[0]) == $alias) {
  116. return 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. }
  126. return array();
  127. }
  128. /* List all addresses */
  129. function list_addr() {
  130. $res = array();
  131. $this->open();
  132. @rewind($this->filehandle);
  133. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  134. $res[] = array('nickname' => $row[0],
  135. 'name' => $row[1] . ' ' . $row[2],
  136. 'firstname' => $row[1],
  137. 'lastname' => $row[2],
  138. 'email' => $row[3],
  139. 'label' => $row[4],
  140. 'backend' => $this->bnum,
  141. 'source' => &$this->sname);
  142. }
  143. return $res;
  144. }
  145. /* Add address */
  146. function add($userdata) {
  147. $this->set_error(_("Can not modify global address book"));
  148. return false;
  149. }
  150. /* Delete address */
  151. function remove($alias) {
  152. $this->set_error(_("Can not modify global address book"));
  153. return false;
  154. }
  155. /* Modify address */
  156. function modify($alias, $userdata) {
  157. $this->set_error(_("Can not modify global address book"));
  158. return false;
  159. }
  160. } /* End of class abook_local_file */
  161. ?>