4 ** abook_local_file.php
6 ** Backend for addressbook as a pipe separated file
8 ** An array with the following elements must be passed to
9 ** the class constructor (elements marked ? are optional):
11 ** NOTE. This class should not be used directly. Use the
12 ** "AddressBook" class instead.
14 ** Make sure you configure this before using it!
19 class abook_global_file
extends addressbook_backend
{
21 var $bname = 'global_file';
25 // ========================== Private =======================
28 function abook_global_file() {
29 global $address_book_global_filename;
30 $this->global_filename
= $address_book_global_filename;
32 $this->sname
= _("Global address book");
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
41 function open($new = false) {
44 // Return true is file is open and $new is unset
45 if($this->filehandle
&& !$new)
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"));
54 // Close old file, if any
55 if ($this->filehandle
) $this->close();
57 // Open file, read only.
58 $fh = @fopen
($this->global_filename
, 'r');
59 $this->writeable
= false;
61 return $this->set_error($this->global_filename
. ': ' .
64 $this->filehandle
= &$fh;
68 // Close the file and forget the filehandle
70 @fclose
($this->filehandle
);
71 $this->filehandle
= 0;
72 $this->global_filename
= '';
73 $this->writable
= false;
76 // ========================== Public ========================
79 function search($expr) {
81 // To be replaced by advanded search expression parsing
82 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);
93 @rewind
($this->filehandle
);
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],
104 'backend' => $this->bnum
,
105 'source' => &$this->sname
);
113 function lookup($alias) {
117 $alias = strtolower($alias);
120 @rewind
($this->filehandle
);
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],
130 'backend' => $this->bnum
,
131 'source' => &$this->sname
);
138 // List all addresses
139 function list_addr() {
142 @rewind
($this->filehandle
);
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],
151 'backend' => $this->bnum
,
152 'source' => &$this->sname
);
158 function add($userdata) {
159 $this->set_error(_("Can not modify global address book"));
164 function remove($alias) {
165 $this->set_error(_("Can not modify global address book"));
170 function modify($alias, $userdata) {
171 $this->set_error(_("Can not modify global address book"));
175 } // End of class abook_local_file