abook_global_file.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /*****************************************************************/
  21. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  22. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  23. /*** + Base level indent should begin at left margin, as ***/
  24. /*** the first line of the class definition below. ***/
  25. /*** + All identation should consist of four space blocks ***/
  26. /*** + Tab characters are evil. ***/
  27. /*** + all comments should use "slash-star ... star-slash" ***/
  28. /*** style -- no pound characters, no slash-slash style ***/
  29. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  30. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  31. /*** + Please use ' instead of ", when possible. Note " ***/
  32. /*** should always be used in _( ) function calls. ***/
  33. /*** Thank you for your help making the SM code more readable. ***/
  34. /*****************************************************************/
  35. class abook_global_file extends addressbook_backend {
  36. var $btype = 'local';
  37. var $bname = 'global_file';
  38. var $filehandle = 0;
  39. // ========================== Private =======================
  40. // Constructor
  41. function abook_global_file() {
  42. global $address_book_global_filename;
  43. $this->global_filename = $address_book_global_filename;
  44. $this->sname = _("Global address book");
  45. $this->open(true);
  46. }
  47. // Open the addressbook file and store the file pointer.
  48. // Use $file as the file to open, or the class' own
  49. // filename property. If $param is empty and file is
  50. // open, do nothing.
  51. function open($new = false) {
  52. $this->error = '';
  53. // Return true is file is open and $new is unset
  54. if($this->filehandle && !$new)
  55. return true;
  56. // Check that new file exists
  57. if (! file_exists($this->global_filename) ||
  58. ! is_readable($this->global_filename))
  59. return $this->set_error($this->global_filename . ': ' .
  60. _("No such file or directory"));
  61. // Close old file, if any
  62. if ($this->filehandle) $this->close();
  63. // Open file, read only.
  64. $fh = @fopen($this->global_filename, 'r');
  65. $this->writeable = false;
  66. if(! $fh)
  67. return $this->set_error($this->global_filename . ': ' .
  68. _("Open failed"));
  69. $this->filehandle = &$fh;
  70. return true;
  71. }
  72. // Close the file and forget the filehandle
  73. function close() {
  74. @fclose($this->filehandle);
  75. $this->filehandle = 0;
  76. $this->global_filename = '';
  77. $this->writable = false;
  78. }
  79. // ========================== Public ========================
  80. // Search the file
  81. function search($expr) {
  82. // To be replaced by advanded search expression parsing
  83. if(is_array($expr)) return;
  84. // Make regexp from glob'ed expression
  85. // May want to quote other special characters like (, ), -, [, ], etc.
  86. $expr = str_replace('?', '.', $expr);
  87. $expr = str_replace('*', '.*', $expr);
  88. $res = array();
  89. if(!$this->open())
  90. return false;
  91. @rewind($this->filehandle);
  92. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  93. $line = join(' ', $row);
  94. if (eregi($expr, $line)) {
  95. $res[] = array('nickname' => $row[0],
  96. 'name' => $row[1] . ' ' . $row[2],
  97. 'firstname' => $row[1],
  98. 'lastname' => $row[2],
  99. 'email' => $row[3],
  100. 'label' => $row[4],
  101. 'backend' => $this->bnum,
  102. 'source' => &$this->sname);
  103. }
  104. }
  105. return $res;
  106. }
  107. // Lookup alias
  108. function lookup($alias) {
  109. if (empty($alias))
  110. return array();
  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. ?>