ehh, hmm: include -> require
[squirrelmail.git] / functions / abook_global_file.php
CommitLineData
01589f26 1<?php
2
35586184 3/**
4 * abook_local_file.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 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 *
19 * $Id$
20 */
01589f26 21
35586184 22/*****************************************************************/
23/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
24/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
25/*** + Base level indent should begin at left margin, as ***/
26/*** the first line of the class definition below. ***/
27/*** + All identation should consist of four space blocks ***/
28/*** + Tab characters are evil. ***/
29/*** + all comments should use "slash-star ... star-slash" ***/
30/*** style -- no pound characters, no slash-slash style ***/
31/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
32/*** ALWAYS USE { AND } CHARACTERS!!! ***/
33/*** + Please use ' instead of ", when possible. Note " ***/
34/*** should always be used in _( ) function calls. ***/
35/*** Thank you for your help making the SM code more readable. ***/
36/*****************************************************************/
37
38class abook_global_file extends addressbook_backend {
39 var $btype = 'local';
40 var $bname = 'global_file';
01589f26 41
42 var $filehandle = 0;
43
44 // ========================== Private =======================
45
46 // Constructor
47 function abook_global_file() {
48 global $address_book_global_filename;
49 $this->global_filename = $address_book_global_filename;
50
51 $this->sname = _("Global address book");
52
53 $this->open(true);
54 }
55
56 // Open the addressbook file and store the file pointer.
57 // Use $file as the file to open, or the class' own
58 // filename property. If $param is empty and file is
59 // open, do nothing.
60 function open($new = false) {
61 $this->error = '';
62
63 // Return true is file is open and $new is unset
64 if($this->filehandle && !$new)
65 return true;
66
67 // Check that new file exists
68 if (! file_exists($this->global_filename) ||
69 ! is_readable($this->global_filename))
70 return $this->set_error($this->global_filename . ': ' .
71 _("No such file or directory"));
72
73 // Close old file, if any
74 if ($this->filehandle) $this->close();
75
76 // Open file, read only.
77 $fh = @fopen($this->global_filename, 'r');
78 $this->writeable = false;
79 if(! $fh)
80 return $this->set_error($this->global_filename . ': ' .
81 _("Open failed"));
82
83 $this->filehandle = &$fh;
84 return true;
85 }
86
87 // Close the file and forget the filehandle
88 function close() {
89 @fclose($this->filehandle);
90 $this->filehandle = 0;
91 $this->global_filename = '';
92 $this->writable = false;
93 }
94
95 // ========================== Public ========================
96
97 // Search the file
98 function search($expr) {
99
100 // To be replaced by advanded search expression parsing
101 if(is_array($expr)) return;
102
103 // Make regexp from glob'ed expression
104 // May want to quote other special characters like (, ), -, [, ], etc.
105 $expr = str_replace('?', '.', $expr);
106 $expr = str_replace('*', '.*', $expr);
107
108 $res = array();
109 if(!$this->open())
110 return false;
111
112 @rewind($this->filehandle);
113
114 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
115 $line = join(' ', $row);
116 if (eregi($expr, $line)) {
117 $res[] = array('nickname' => $row[0],
118 'name' => $row[1] . ' ' . $row[2],
119 'firstname' => $row[1],
120 'lastname' => $row[2],
121 'email' => $row[3],
122 'label' => $row[4],
123 'backend' => $this->bnum,
124 'source' => &$this->sname);
125 }
126 }
127
128 return $res;
129 }
130
131 // Lookup alias
132 function lookup($alias) {
133 if (empty($alias))
134 return array();
135
136 $alias = strtolower($alias);
137
138 $this->open();
139 @rewind($this->filehandle);
140
141 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
142 if (strtolower($row[0]) == $alias) {
143 return array('nickname' => $row[0],
144 'name' => $row[1] . ' ' . $row[2],
145 'firstname' => $row[1],
146 'lastname' => $row[2],
147 'email' => $row[3],
148 'label' => $row[4],
149 'backend' => $this->bnum,
150 'source' => &$this->sname);
151 }
152 }
153
154 return array();
155 }
156
157 // List all addresses
158 function list_addr() {
159 $res = array();
160 $this->open();
161 @rewind($this->filehandle);
162
163 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
164 $res[] = array('nickname' => $row[0],
165 'name' => $row[1] . ' ' . $row[2],
166 'firstname' => $row[1],
167 'lastname' => $row[2],
168 'email' => $row[3],
169 'label' => $row[4],
170 'backend' => $this->bnum,
171 'source' => &$this->sname);
172 }
173 return $res;
174 }
175
176 // Add address
177 function add($userdata) {
178 $this->set_error(_("Can not modify global address book"));
179 return false;
180 }
181
182 // Delete address
183 function remove($alias) {
184 $this->set_error(_("Can not modify global address book"));
185 return false;
186 }
187
188 // Modify address
189 function modify($alias, $userdata) {
190 $this->set_error(_("Can not modify global address book"));
191 return false;
192 }
193
194 } // End of class abook_local_file
195?>