1<?php
2/**
3 * Copyright 1999-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (GPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/gpl.
7 *
8 * @category  Horde
9 * @copyright 1999-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/gpl GPL
11 * @package   IMP
12 */
13
14/**
15 * IMP base class.
16 *
17 * @author    Chuck Hagenbuch <chuck@horde.org>
18 * @author    Jon Parise <jon@horde.org>
19 * @author    Michael Slusarz <slusarz@horde.org>
20 * @category  Horde
21 * @copyright 1999-2017 Horde LLC
22 * @license   http://www.horde.org/licenses/gpl GPL
23 * @package   IMP
24 */
25class IMP
26{
27    /* Encrypt constants. */
28    const ENCRYPT_NONE = 'encrypt_none';
29
30    /* IMP Mailbox view constants. */
31    const MAILBOX_START_FIRSTUNSEEN = 1;
32    const MAILBOX_START_LASTUNSEEN = 2;
33    const MAILBOX_START_FIRSTPAGE = 3;
34    const MAILBOX_START_LASTPAGE = 4;
35
36    /* Folder list actions. */
37    const NOTEPAD_EDIT = "notepad\0";
38    const TASKLIST_EDIT = "tasklist\0";
39
40    /* Initial page constants. */
41    const INITIAL_FOLDERS = "initial\0folders";
42
43    /* Sorting constants. */
44    const IMAP_SORT_DATE = 100;
45
46    /**
47     * Filters a string, if requested.
48     *
49     * @param string $text  The text to filter.
50     *
51     * @return string  The filtered text (if requested).
52     */
53    static public function filterText($text)
54    {
55        global $injector, $prefs;
56
57        if ($prefs->getValue('filtering') && strlen($text)) {
58            try {
59                return $injector->getInstance('Horde_Core_Factory_TextFilter')->filter(
60                    $text,
61                    'words',
62                    $injector->getInstance('Horde_Core_Hooks')->callHook('msg_filter', 'imp')
63                );
64            } catch (Horde_Exception_HookNotSet $e) {}
65        }
66
67        return $text;
68    }
69
70    /**
71     *
72     * @param integer $size  The byte size of data.
73     *
74     * @return string  A formatted size string.
75     */
76    static public function sizeFormat($size)
77    {
78        return ($size >= 1048576)
79            ? sprintf(_("%s MB"), self::numberFormat($size / 1048576, 1))
80            : sprintf(_("%s KB"), self::numberFormat($size / 1024, 0));
81    }
82
83    /**
84     * Workaround broken number_format() prior to PHP 5.4.0.
85     *
86     * @param integer $number    Number to format.
87     * @param integer $decimals  Number of decimals to display.
88     *
89     * @return string  See number_format().
90     */
91    static public function numberFormat($number, $decimals)
92    {
93        $localeinfo = Horde_Nls::getLocaleInfo();
94
95        return str_replace(
96            array('X', 'Y'),
97            array($localeinfo['decimal_point'], $localeinfo['thousands_sep']),
98            number_format($decimals ? $number : ceil($number), $decimals, 'X', 'Y')
99        );
100    }
101
102    /**
103     * Wrapper around Horde_Mail_Rfc822#parseAddressList(). Ensures all
104     * addresses have a default mail domain appended.
105     *
106     * @param mixed $in    The address string or an address list object.
107     * @param array $opts  Options to override the default.
108     *
109     * @return array  See Horde_Mail_Rfc822#parseAddressList().
110     *
111     * @throws Horde_Mail_Exception
112     */
113    static public function parseAddressList($in, array $opts = array())
114    {
115        $md = $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create()->config->maildomain;
116
117        if ($in instanceof Horde_Mail_Rfc822_List) {
118            $res = clone $in;
119            foreach ($res->raw_addresses as $val) {
120                if (is_null($val->host)) {
121                    $val->host = $md;
122                 }
123            }
124        } else {
125            $rfc822 = $GLOBALS['injector']->getInstance('Horde_Mail_Rfc822');
126            $res = $rfc822->parseAddressList($in, array_merge(array(
127                'default_domain' => $md,
128                'validate' => false
129            ), $opts));
130        }
131
132        $res->setIteratorFilter(Horde_Mail_Rfc822_List::HIDE_GROUPS);
133
134        return $res;
135    }
136
137    /**
138     * Returns the initial page for IMP.
139     *
140     * @return object  Object with the following properties:
141     * <pre>
142     *   - mbox (IMP_Mailbox)
143     *   - url (Horde_Url)
144     * </pre>
145     */
146    static public function getInitialPage()
147    {
148        global $injector, $prefs, $registry;
149
150        $init_url = $prefs->getValue('initial_page');
151        if (!$init_url ||
152            !$injector->getInstance('IMP_Factory_Imap')->create()->access(IMP_Imap::ACCESS_FOLDERS)) {
153            $init_url = 'INBOX';
154        }
155
156        if ($init_url == IMP::INITIAL_FOLDERS) {
157            $mbox = null;
158        } else {
159            $mbox = IMP_Mailbox::get($init_url);
160            if (!$mbox->exists) {
161                $mbox = IMP_Mailbox::get('INBOX');
162            }
163        }
164
165        $result = new stdClass;
166        $result->mbox = $mbox;
167
168        switch ($registry->getView()) {
169        case Horde_Registry::VIEW_BASIC:
170            $result->url = is_null($mbox)
171                ? IMP_Basic_Folders::url()
172                : $mbox->url('mailbox');
173            break;
174
175        case Horde_Registry::VIEW_DYNAMIC:
176            $result->url = IMP_Dynamic_Mailbox::url(array(
177                'mailbox' => is_null($mbox) ? 'INBOX' : $mbox
178            ));
179            break;
180
181        case Horde_Registry::VIEW_MINIMAL:
182            $result->url = is_null($mbox)
183                ? IMP_Minimal_Folders::url()
184                : IMP_Minimal_Mailbox::url(array('mailbox' => $mbox));
185            break;
186
187        case Horde_Registry::VIEW_SMARTMOBILE:
188            $result->url = is_null($mbox)
189                ? Horde::url('smartmobile.php', true)
190                : $mbox->url('mailbox');
191            break;
192        }
193
194        return $result;
195    }
196
197}
198