| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | ** abook_local_file.php |
| 5 | ** |
| 6 | ** Backend for addressbook as a pipe separated file |
| 7 | ** |
| 8 | ** An array with the following elements must be passed to |
| 9 | ** the class constructor (elements marked ? are optional): |
| 10 | ** |
| 11 | ** NOTE. This class should not be used directly. Use the |
| 12 | ** "AddressBook" class instead. |
| 13 | ** |
| 14 | ** Make sure you configure this before using it! |
| 15 | ** |
| 16 | ** $Id$ |
| 17 | **/ |
| 18 | |
| 19 | class abook_global_file extends addressbook_backend { |
| 20 | var $btype = 'local'; |
| 21 | var $bname = 'global_file'; |
| 22 | |
| 23 | var $filehandle = 0; |
| 24 | |
| 25 | // ========================== Private ======================= |
| 26 | |
| 27 | // Constructor |
| 28 | function abook_global_file() { |
| 29 | global $address_book_global_filename; |
| 30 | $this->global_filename = $address_book_global_filename; |
| 31 | |
| 32 | $this->sname = _("Global address book"); |
| 33 | |
| 34 | $this->open(true); |
| 35 | } |
| 36 | |
| 37 | // Open the addressbook file and store the file pointer. |
| 38 | // Use $file as the file to open, or the class' own |
| 39 | // filename property. If $param is empty and file is |
| 40 | // open, do nothing. |
| 41 | function open($new = false) { |
| 42 | $this->error = ''; |
| 43 | |
| 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) $this->close(); |
| 56 | |
| 57 | // Open file, read only. |
| 58 | $fh = @fopen($this->global_filename, 'r'); |
| 59 | $this->writeable = false; |
| 60 | if(! $fh) |
| 61 | return $this->set_error($this->global_filename . ': ' . |
| 62 | _("Open failed")); |
| 63 | |
| 64 | $this->filehandle = &$fh; |
| 65 | return true; |
| 66 | } |
| 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 | |
| 76 | // ========================== Public ======================== |
| 77 | |
| 78 | // Search the file |
| 79 | function search($expr) { |
| 80 | |
| 81 | // To be replaced by advanded search expression parsing |
| 82 | if(is_array($expr)) return; |
| 83 | |
| 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 | |
| 89 | $res = array(); |
| 90 | if(!$this->open()) |
| 91 | return false; |
| 92 | |
| 93 | @rewind($this->filehandle); |
| 94 | |
| 95 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
| 96 | $line = join(' ', $row); |
| 97 | if (eregi($expr, $line)) { |
| 98 | $res[] = array('nickname' => $row[0], |
| 99 | 'name' => $row[1] . ' ' . $row[2], |
| 100 | 'firstname' => $row[1], |
| 101 | 'lastname' => $row[2], |
| 102 | 'email' => $row[3], |
| 103 | 'label' => $row[4], |
| 104 | 'backend' => $this->bnum, |
| 105 | 'source' => &$this->sname); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $res; |
| 110 | } |
| 111 | |
| 112 | // Lookup alias |
| 113 | function lookup($alias) { |
| 114 | if (empty($alias)) |
| 115 | return array(); |
| 116 | |
| 117 | $alias = strtolower($alias); |
| 118 | |
| 119 | $this->open(); |
| 120 | @rewind($this->filehandle); |
| 121 | |
| 122 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
| 123 | if (strtolower($row[0]) == $alias) { |
| 124 | return array('nickname' => $row[0], |
| 125 | 'name' => $row[1] . ' ' . $row[2], |
| 126 | 'firstname' => $row[1], |
| 127 | 'lastname' => $row[2], |
| 128 | 'email' => $row[3], |
| 129 | 'label' => $row[4], |
| 130 | 'backend' => $this->bnum, |
| 131 | 'source' => &$this->sname); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return array(); |
| 136 | } |
| 137 | |
| 138 | // List all addresses |
| 139 | function list_addr() { |
| 140 | $res = array(); |
| 141 | $this->open(); |
| 142 | @rewind($this->filehandle); |
| 143 | |
| 144 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
| 145 | $res[] = array('nickname' => $row[0], |
| 146 | 'name' => $row[1] . ' ' . $row[2], |
| 147 | 'firstname' => $row[1], |
| 148 | 'lastname' => $row[2], |
| 149 | 'email' => $row[3], |
| 150 | 'label' => $row[4], |
| 151 | 'backend' => $this->bnum, |
| 152 | 'source' => &$this->sname); |
| 153 | } |
| 154 | return $res; |
| 155 | } |
| 156 | |
| 157 | // Add address |
| 158 | function add($userdata) { |
| 159 | $this->set_error(_("Can not modify global address book")); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | // Delete address |
| 164 | function remove($alias) { |
| 165 | $this->set_error(_("Can not modify global address book")); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | // Modify address |
| 170 | function modify($alias, $userdata) { |
| 171 | $this->set_error(_("Can not modify global address book")); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | } // End of class abook_local_file |
| 176 | ?> |