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