File: class_mail-methods-dovecot.inc

package info (click to toggle)
fusiondirectory 1.0.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 28,984 kB
  • sloc: php: 74,645; xml: 3,645; perl: 1,555; pascal: 705; sh: 135; sql: 45; makefile: 19
file content (225 lines) | stat: -rw-r--r-- 7,913 bytes parent folder | download | duplicates (2)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2013  FusionDirectory

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

class mailMethodDovecot extends mailMethod
{
  /* Allow modification of account_ids for existing mail accounts */
  protected $modifyableMail   = FALSE;

  /* Allow modification of the mail server attribute existing mail accounts */
  protected $modifyableServer = FALSE;

  /* internal */
  protected $imap_handle  = NULL;
  protected $quota_loaded = FALSE;

  public function is_connected()
  {
    return (parent::is_connected() && $this->imap_handle);
  }

  public function connect()
  {
    mailMethod::connect();

    if ($this->config->get_cfg_value("mailMethodDisabled", "FALSE") == "TRUE") {
      return FALSE;
    }

    if (!count($this->config->data['SERVERS']['IMAP'])) {
      $this->error = _("There are no IMAP compatible mail servers defined!");
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
          "<b>IMAP: No mail servers configured, check systems->server->service->imap.</b>", "");
      return FALSE;
    } elseif (!isset($this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer])) {
      $this->error = _("Mail server for this account is invalid!");
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
          "<b>IMAP: The selected mail server '".$this->parent->gosaMailServer."' is invalid.</b>", "");
      return FALSE;
    } else {
      $cfg = $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
    }

    /* For some reason, hiding errors with @ does not wor here... */
    if (!isset($cfg['connect'])) {
      $cfg['connect']   = "";
    }
    if (!isset($cfg['admin'])) {
      $cfg['admin']     = "";
    }
    if (!isset($cfg['password'])) {
      $cfg['password']  = "";
    }

    $this->build_account_id();
    $cfg['admin'] = $this->account_id.'*'.$cfg['admin'];

    /* Setting connect timeout to 10 seconds,
        else the FusionDirectory UI may freeze for 60 seconds.
       (PHP default is 'default_socket_timeout = 60') */
    $timeout = $this->config->get_cfg_value("imapTimeout", 10);
    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $timeout,
          "<b>IMAP: Setting imap connect timeout to</b> (seconds)");
    imap_timeout(1, $timeout);

    $this->imap_handle = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);

    /* Mailbox reachable? */
    if ($this->imap_handle === FALSE) {
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>Failed</b> :".imap_last_error(),
        "<b>IMAP:</b> ".$cfg['admin']."@".$cfg['connect']);
      if ($cfg['mkdir']) {
        if ($this->argonautCreateFolder()) {
          $this->imap_handle = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
        } else {
          return FALSE;
        }
      }
      if ($this->imap_handle === FALSE) {
        $this->error = imap_last_error();

        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>Failed</b> :".imap_last_error(),
          "<b>IMAP:</b> ".$cfg['admin']."@".$cfg['connect']);

        $this->connected = FALSE;
        return FALSE;
      }
    }
    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>successful</b>",
        "<b>IMAP:</b> ".$cfg['admin']."@".$cfg['connect']);
    $this->connected = TRUE;

    return TRUE;
  }

  protected function loadQuota()
  {
    if (!$this->quotaEnabled()) {
      return TRUE;
    }
    if ($this->config->get_cfg_value("mailMethodDisabled", "FALSE") == "TRUE") {
      return FALSE;
    }
    if (!$this->is_connected()) {
      trigger_error('Method not connected, catch error.');
      return FALSE;
    }

    $this->reset_error();

    /* Load quota settings */
    $quota_value = @imap_get_quotaroot($this->imap_handle, 'INBOX'); /* FIXME: is this always INBOX? */

    if (is_array($quota_value)) {
      if (count($quota_value)) {
        $this->quotaUsage   = $quota_value['STORAGE']['usage'] / 1024;
        if (($quota_value['STORAGE']['limit'] == 2147483647) || ($quota_value['STORAGE']['limit'] <= 0)) {
          $this->quotaValue = '';
        } else {
          $this->quotaValue = $quota_value['STORAGE']['limit'] / 1024;
        }
        $this->quota_loaded = TRUE;

        /* Write debug output */
        if ($this->quotaValue == "") {
          $quota = "(".$this->quotaUsage." / unlimited)";
        } else {
          $quota = "(".$this->quotaUsage." / ".$this->quotaValue.")";
        }
        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $quota,
            "<b>IMAP: Successfully received account quota</b>");
      } else {
        $this->quotaUsage = '';
        $this->quotaValue = '';
        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, '',
            "<b>IMAP: Received empty account quota information</b>");
      }
    } else {
      $this->error = imap_last_error();
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, imap_last_error(),
          "<b>IMAP: Failed to receive account quota</b>");
    }
  }

  /* Get quota usage in MB */
  public function getQuotaUsage()
  {
    mailMethod::getQuotaUsage();
    if (!$this->quota_loaded) {
      $this->loadQuota();
    }
    return $this->quotaUsage;
  }

  /*
   * Create the folder for the user INBOX through Argonaut
   */
  private function argonautCreateFolder()
  {
    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>",
      "<b>Attempting to create folder through Argonaut</b> on server :".$this->parent->gosaMailServer);

    $cfg = $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];

    $o_queue = new supportDaemon();
    if (!$o_queue->is_error()) {
      $o_queue->append_call('Dovecot.create_mailbox',
                            array($cfg['mac']),
                            array('args' => array($this->account_id, $this->parent->uidNumber, $this->parent->gidNumber)));
    }
    /* If we got an error while connecting or sending the call */
    if ($o_queue->is_error()) {
      $this->error = sprintf(_('Error while trying to contact Dovecot server through Argonaut: %s'), $o_queue->get_error());
      return FALSE;
    }

    return TRUE;
  }

  /*! \brief  Checks whether this account is removeable or not.
   */
  public function accountRemoveable (&$reason = "")
  {
    return FALSE;
  }

  static public function get_server_list($config)
  {
    $serverList = array();

    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);
    $ldap->search ('(objectClass=fdDovecotServer)',
                  array('cn', 'fdDovecotConnect', 'fdDovecotAdmin', 'fdDovecotPassword', 'fdDovecotArgonautMkdir', 'macAddress'));
    while ($attrs = $ldap->fetch()) {
      $serverList[$attrs['cn'][0]] = array(
        'server_dn' => $attrs['dn'],
        'connect'   => $attrs['fdDovecotConnect'][0],
        'admin'     => $attrs['fdDovecotAdmin'][0],
        'password'  => $attrs['fdDovecotPassword'][0],
        'mkdir'     => (isset($attrs['fdDovecotArgonautMkdir'])?(strtoupper($attrs['fdDovecotArgonautMkdir'][0]) == 'TRUE'):TRUE),
        'mac'       => $attrs['macAddress'][0],
      );
    }

    return $serverList;
  }
}
?>