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