1<?php
2/**
3 * Mnemo application API.
4 *
5 * This file defines Horde's core API interface. Other core Horde libraries
6 * can interact with Mnemo through this API.
7 *
8 * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
9 *
10 * See the enclosed file LICENSE for license information (ASL). If you
11 * did not receive this file, see http://www.horde.org/licenses/apache.
12 *
13 * @package Mnemo
14 */
15
16/* Determine the base directories. */
17if (!defined('MNEMO_BASE')) {
18    define('MNEMO_BASE', realpath(__DIR__ . '/..'));
19}
20
21if (!defined('HORDE_BASE')) {
22    /* If Horde does not live directly under the app directory, the HORDE_BASE
23     * constant should be defined in config/horde.local.php. */
24    if (file_exists(MNEMO_BASE . '/config/horde.local.php')) {
25        include MNEMO_BASE . '/config/horde.local.php';
26    } else {
27        define('HORDE_BASE', realpath(MNEMO_BASE . '/..'));
28    }
29}
30
31/* Load the Horde Framework core (needed to autoload
32 * Horde_Registry_Application::). */
33require_once HORDE_BASE . '/lib/core.php';
34
35class Mnemo_Application extends Horde_Registry_Application
36{
37    /**
38     */
39    public $features = array(
40        'activesync' => true,
41        'modseq' => true,
42    );
43
44    /**
45     */
46    public $version = 'H5 (4.2.14)';
47
48    /**
49     * Global variables defined:
50     *   $mnemo_shares - TODO
51     */
52    protected function _init()
53    {
54        /* For now, autoloading the Content_* classes depend on there being a
55         * registry entry for the 'content' application that contains at least
56         * the fileroot entry. */
57        $GLOBALS['injector']->getInstance('Horde_Autoloader')
58            ->addClassPathMapper(
59                new Horde_Autoloader_ClassPathMapper_Prefix('/^Content_/', $GLOBALS['registry']->get('fileroot', 'content') . '/lib/'));
60
61        if (!class_exists('Content_Tagger')) {
62            throw new Horde_Exception(_("The Content_Tagger class could not be found. Make sure the Content application is installed."));
63        }
64
65        Mnemo::initialize();
66    }
67
68    /**
69     */
70    public function perms()
71    {
72        return array(
73            'max_notes' => array(
74                'title' => _("Maximum Number of Notes"),
75                'type' => 'int'
76            )
77        );
78    }
79
80    /**
81     */
82    public function menu($menu)
83    {
84        global $conf, $injector;
85
86        $menu->add(Horde::url('list.php'), _("_List Notes"), 'mnemo-list', null, null, null, basename($_SERVER['PHP_SELF']) == 'index.php' ? 'current' : null);
87
88        /* Search. */
89        $menu->add(Horde::url('search.php'), _("_Search"), 'mnemo-search');
90
91        /* Import/Export */
92        if ($conf['menu']['import_export']) {
93            $menu->add(Horde::url('data.php'), _("_Import/Export"), 'horde-data');
94        }
95    }
96
97    /**
98     * Add additional items to the sidebar.
99     *
100     * @param Horde_View_Sidebar $sidebar  The sidebar object.
101     */
102    public function sidebar($sidebar)
103    {
104        $perms = $GLOBALS['injector']->getInstance('Horde_Core_Perms');
105        if (Mnemo::getDefaultNotepad(Horde_Perms::EDIT) &&
106            ($perms->hasAppPermission('max_notes') === true ||
107             $perms->hasAppPermission('max_notes') > Mnemo::countMemos())) {
108            $sidebar->addNewButton(
109                _("_New Note"),
110                Horde::url('memo.php')->add('actionID', 'add_memo'));
111        }
112
113        $url = Horde::url('');
114        $edit = Horde::url('notepads/edit.php');
115        $user = $GLOBALS['registry']->getAuth();
116
117        $sidebar->containers['my'] = array(
118            'header' => array(
119                'id' => 'mnemo-toggle-my',
120                'label' => _("My Notepads"),
121                'collapsed' => false,
122            ),
123        );
124        if (!$GLOBALS['prefs']->isLocked('default_notepad')) {
125            $sidebar->containers['my']['header']['add'] = array(
126                'url' => Horde::url('notepads/create.php'),
127                'label' => _("Create a new Notepad"),
128            );
129        }
130        $sidebar->containers['shared'] = array(
131            'header' => array(
132                'id' => 'mnemo-toggle-shared',
133                'label' => _("Shared Notepads"),
134                'collapsed' => true,
135            ),
136        );
137        foreach (Mnemo::listNotepads() as $name => $notepad) {
138            $url->add(array(
139                'display_notepad' => $name,
140                'actionID' => in_array($name, $GLOBALS['display_notepads'])
141                    ? 'remove_displaylist'
142                    : 'add_displaylist'
143            ));
144            $row = array(
145                'selected' => in_array($name, $GLOBALS['display_notepads']),
146                'url' => $url,
147                'label' => Mnemo::getLabel($notepad),
148                'color' => $notepad->get('color') ?: '#dddddd',
149                'edit' => $edit->add('n', $notepad->getName()),
150                'type' => 'checkbox',
151            );
152            if ($notepad->get('owner') == $user) {
153                $sidebar->addRow($row, 'my');
154            } else {
155                $sidebar->addRow($row, 'shared');
156            }
157        }
158    }
159
160    /**
161     */
162    public function hasPermission($permission, $allowed, $opts = array())
163    {
164        if (is_array($allowed)) {
165            switch ($permission) {
166            case 'max_notes':
167                $allowed = max($allowed);
168                break;
169            }
170        }
171        return $allowed;
172    }
173
174    /* Topbar method. */
175
176    /**
177     */
178    public function topbarCreate(Horde_Tree_Renderer_Base $tree, $parent = null,
179                                 array $params = array())
180    {
181        global $registry;
182
183        $add = Horde::url('memo.php', true)->add('actionID', 'add_memo');
184
185        $tree->addNode(array(
186            'id' => $parent . '__new',
187            'parent' => $parent,
188            'label' => _("New Note"),
189            'expanded' => false,
190            'params' => array(
191                'icon' => Horde_Themes::img('add.png'),
192                'url' => $add
193            )
194        ));
195
196        $user = $registry->getAuth();
197        foreach (Mnemo::listNotepads(false, Horde_Perms::SHOW) as $name => $notepad) {
198            if (!$notepad->hasPermission($user, Horde_Perms::EDIT)) {
199                continue;
200            }
201            $tree->addNode(array(
202                'id' => $parent . $name . '__new',
203                'parent' => $parent . '__new',
204                'label' => sprintf(_("in %s"), Mnemo::getLabel($notepad)),
205                'expanded' => false,
206                'params' => array(
207                    'icon' => Horde_Themes::img('add.png'),
208                    'url' => $add->copy()->add('memolist', $name)
209                )
210            ));
211        }
212
213        $tree->addNode(array(
214            'id' => $parent . '__search',
215            'parent' => $parent,
216            'label' => _("Search"),
217            'expanded' => false,
218            'params' => array(
219                'icon' => Horde_Themes::img('search.png'),
220                'url' => Horde::url('search.php')
221            )
222        ));
223    }
224
225    /**
226     */
227    public function removeUserData($user)
228    {
229        $error = false;
230        $notepads = $GLOBALS['mnemo_shares']->listShares(
231            $user, array('attributes' => $user));
232        foreach ($notepads as $notepad => $share) {
233            $driver = $GLOBALS['injector']
234                ->getInstance('Mnemo_Factory_Driver')
235                ->create($notepad);
236            try {
237                $driver->deleteAll();
238            } catch (Mnemo_Exception $e) {
239                Horde::log($e, 'NOTICE');
240                $error = true;
241            }
242
243            try {
244                $GLOBALS['mnemo_shares']->removeShare($share);
245            } catch (Horde_Share_Exception $e) {
246                Horde::log($e, 'NOTICE');
247                $error = true;
248            }
249        }
250
251        // Get a list of all shares this user has perms to and remove the perms.
252        try {
253            $shares = $GLOBALS['mnemo_shares']->listShares($user);
254            foreach ($shares as $share) {
255                $share->removeUser($user);
256            }
257        } catch (Horde_Share_Exception $e) {
258            Horde::log($e, 'NOTICE');
259            $error = true;
260        }
261
262        if ($error) {
263            throw new Mnemo_Exception(sprintf(_("There was an error removing notes for %s. Details have been logged."), $user));
264        }
265    }
266
267    /* Download data. */
268
269    /**
270     * @throws Mnemo_Exception
271     */
272    public function download(Horde_Variables $vars)
273    {
274        global $injector, $registry;
275
276        switch ($vars->actionID) {
277        case 'export':
278            /* Get the full, sorted memo list. */
279            $notes = Mnemo::listMemos();
280
281            switch ($vars->exportID) {
282            case Horde_Data::EXPORT_CSV:
283                $data = array();
284                foreach ($notes as $note) {
285                    unset(
286                        $note['desc'],
287                        $note['memo_id'],
288                        $note['memolist_id'],
289                        $nore['uid']
290                    );
291                    $note['tags'] = implode(',', $note['tags']);
292                    if ($note['body'] instanceof Mnemo_Exception) {
293                        $note['body'] = $note['body']->getMessage();
294                    }
295                    $data[] = $note;
296                }
297
298                $injector->getInstance('Horde_Core_Factory_Data')
299                    ->create('Csv',
300                             array('cleanup' => array($this, 'cleanupData')))
301                    ->exportFile(_("notes.csv"), $data, true);
302                exit;
303            }
304        }
305    }
306
307    /**
308     */
309    public function cleanupData()
310    {
311        $GLOBALS['import_step'] = 1;
312        return Horde_Data::IMPORT_FILE;
313    }
314
315}
316