abook_global_file.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * abook_local_file.php
  4. *
  5. * Copyright (c) 1999-2003 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. * $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. }
  42. /* Check that new file exists */
  43. if (! file_exists($this->global_filename) ||
  44. ! is_readable($this->global_filename)) {
  45. return $this->set_error($this->global_filename . ': ' .
  46. _("No such file or directory"));
  47. }
  48. /* Close old file, if any */
  49. if ($this->filehandle) {
  50. $this->close();
  51. }
  52. /* Open file, read only. */
  53. $fh = @fopen($this->global_filename, 'r');
  54. $this->writeable = false;
  55. if(! $fh) {
  56. return $this->set_error($this->global_filename . ': ' .
  57. _("Open failed"));
  58. }
  59. $this->filehandle = &$fh;
  60. return true;
  61. }
  62. /* Close the file and forget the filehandle */
  63. function close() {
  64. @fclose($this->filehandle);
  65. $this->filehandle = 0;
  66. $this->global_filename = '';
  67. $this->writable = false;
  68. }
  69. /* ========================== Public ======================== */
  70. /* Search the file */
  71. function search($expr) {
  72. /* To be replaced by advanded search expression parsing */
  73. if(is_array($expr)) {
  74. return;
  75. }
  76. /* Make regexp from glob'ed expression
  77. * May want to quote other special characters like (, ), -, [, ], etc. */
  78. $expr = str_replace('?', '.', $expr);
  79. $expr = str_replace('*', '.*', $expr);
  80. $res = array();
  81. if(!$this->open()) {
  82. return false;
  83. }
  84. @rewind($this->filehandle);
  85. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  86. $line = join(' ', $row);
  87. if (eregi($expr, $line)) {
  88. $res[] = array('nickname' => $row[0],
  89. 'name' => $row[1] . ' ' . $row[2],
  90. 'firstname' => $row[1],
  91. 'lastname' => $row[2],
  92. 'email' => $row[3],
  93. 'label' => $row[4],
  94. 'backend' => $this->bnum,
  95. 'source' => &$this->sname);
  96. }
  97. }
  98. return $res;
  99. }
  100. /* Lookup alias */
  101. function lookup($alias) {
  102. if (empty($alias)) {
  103. return array();
  104. }
  105. $alias = strtolower($alias);
  106. $this->open();
  107. @rewind($this->filehandle);
  108. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  109. if (strtolower($row[0]) == $alias) {
  110. return array('nickname' => $row[0],
  111. 'name' => $row[1] . ' ' . $row[2],
  112. 'firstname' => $row[1],
  113. 'lastname' => $row[2],
  114. 'email' => $row[3],
  115. 'label' => $row[4],
  116. 'backend' => $this->bnum,
  117. 'source' => &$this->sname);
  118. }
  119. }
  120. return array();
  121. }
  122. /* List all addresses */
  123. function list_addr() {
  124. $res = array();
  125. $this->open();
  126. @rewind($this->filehandle);
  127. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  128. $res[] = array('nickname' => $row[0],
  129. 'name' => $row[1] . ' ' . $row[2],
  130. 'firstname' => $row[1],
  131. 'lastname' => $row[2],
  132. 'email' => $row[3],
  133. 'label' => $row[4],
  134. 'backend' => $this->bnum,
  135. 'source' => &$this->sname);
  136. }
  137. return $res;
  138. }
  139. /* Add address */
  140. function add($userdata) {
  141. $this->set_error(_("Can not modify global address book"));
  142. return false;
  143. }
  144. /* Delete address */
  145. function remove($alias) {
  146. $this->set_error(_("Can not modify global address book"));
  147. return false;
  148. }
  149. /* Modify address */
  150. function modify($alias, $userdata) {
  151. $this->set_error(_("Can not modify global address book"));
  152. return false;
  153. }
  154. } /* End of class abook_local_file */
  155. ?>