File: abook_global_file.php

package info (click to toggle)
squirrelmail 1%3A1.2.6-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,300 kB
  • ctags: 4,949
  • sloc: php: 21,297; perl: 2,538; sh: 350; ansic: 122; makefile: 69
file content (188 lines) | stat: -rw-r--r-- 5,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php

/**
 * abook_local_file.php
 *
 * Copyright (c) 1999-2002 The Squirrelmail Project Team
 * Licensed under the GNU GPL. For full terms see the file COPYING.
 *
 * Backend for addressbook as a pipe separated file
 *
 * An array with the following elements must be passed to
 * the class constructor (elements marked ? are optional):
 *
 * NOTE. This class should not be used directly. Use the
 *       "AddressBook" class instead.
 *
 * Make sure you configure this before using it!
 *
 * $Id: abook_global_file.php,v 1.6 2002/01/28 20:41:01 kink Exp $
 */

class abook_global_file extends addressbook_backend {
    var $btype = 'local';
    var $bname = 'global_file';

    var $filehandle = 0;

    /* ========================== Private ======================= */

    /* Constructor */
    function abook_global_file() {
        global $address_book_global_filename;
        $this->global_filename = $address_book_global_filename;
      
        $this->sname = _("Global address book");
 
        $this->open(true);
    }

    /* Open the addressbook file and store the file pointer.
     * Use $file as the file to open, or the class' own 
     * filename property. If $param is empty and file is  
     * open, do nothing. */
    function open($new = false) {
        $this->error = '';

        /* Return true is file is open and $new is unset */
        if($this->filehandle && !$new) {
            return true;
        }
 
        /* Check that new file exists */
        if (! file_exists($this->global_filename) || 
            ! is_readable($this->global_filename)) {
            return $this->set_error($this->global_filename . ': ' .
                _("No such file or directory"));
        }
 
        /* Close old file, if any */
        if ($this->filehandle) {
            $this->close();
        }
        
        /* Open file, read only. */
        $fh = @fopen($this->global_filename, 'r');
        $this->writeable  = false;
        if(! $fh) {
            return $this->set_error($this->global_filename . ': ' . 
                _("Open failed"));
        }
 
        $this->filehandle = &$fh;
        return true;
    }

    /* Close the file and forget the filehandle */
    function close() {
        @fclose($this->filehandle);
        $this->filehandle = 0;
        $this->global_filename   = '';
        $this->writable   = false;
    }

    /* ========================== Public ======================== */
    
    /* Search the file */
    function search($expr) {

        /* To be replaced by advanded search expression parsing */
        if(is_array($expr)) {
            return;
        }
 
        /* Make regexp from glob'ed expression
         * May want to quote other special characters like (, ), -, [, ], etc. */
        $expr = str_replace('?', '.', $expr);
        $expr = str_replace('*', '.*', $expr);
 
        $res = array();
        if(!$this->open()) {
            return false;
        }
 
        @rewind($this->filehandle);
        
        while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
            $line = join(' ', $row);
            if (eregi($expr, $line)) {
                $res[] = array('nickname'  => $row[0],
                               'name'      => $row[1] . ' ' . $row[2],
                               'firstname' => $row[1],
                               'lastname'  => $row[2],
                               'email'     => $row[3],
                               'label'     => $row[4],
                               'backend'   => $this->bnum,
                               'source'    => &$this->sname);
            }
        }
        
        return $res;
    }
    
    /* Lookup alias */
    function lookup($alias) {
        if (empty($alias)) {
            return array();
        }
 
        $alias = strtolower($alias);
        
        $this->open();
        @rewind($this->filehandle);
        
        while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
            if (strtolower($row[0]) == $alias) {
                return array('nickname'  => $row[0],
                             'name'      => $row[1] . ' ' . $row[2],
                             'firstname' => $row[1],
                             'lastname'  => $row[2],
                             'email'     => $row[3],
                             'label'     => $row[4],
                             'backend'   => $this->bnum,
                             'source'    => &$this->sname);
            }
        }
      
        return array();
    }

    /* List all addresses */
    function list_addr() {
        $res = array();
        $this->open();
        @rewind($this->filehandle);
        
        while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
            $res[] = array('nickname'  => $row[0],
                           'name'      => $row[1] . ' ' . $row[2],
                           'firstname' => $row[1],
                           'lastname'  => $row[2],
                           'email'     => $row[3],
                           'label'     => $row[4],
                           'backend'   => $this->bnum,
                           'source'    => &$this->sname);
        }
        return $res;
    }

    /* Add address */
    function add($userdata) {
        $this->set_error(_("Can not modify global address book"));
        return false;
    }

    /* Delete address */
    function remove($alias) {
        $this->set_error(_("Can not modify global address book"));
        return false;
    }

    /* Modify address */
    function modify($alias, $userdata) {
        $this->set_error(_("Can not modify global address book"));
        return false;
    }
     
} /* End of class abook_local_file */
?>