1<?php
2/**
3 * Copyright 2010-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 * @license  http://www.horde.org/licenses/gpl GPL
10 * @package Wicked
11 */
12
13/* Determine the base directories. */
14if (!defined('WICKED_BASE')) {
15    define('WICKED_BASE', realpath(__DIR__ . '/..'));
16}
17
18if (!defined('HORDE_BASE')) {
19    /* If Horde does not live directly under the app directory, the HORDE_BASE
20     * constant should be defined in config/horde.local.php. */
21    if (file_exists(WICKED_BASE . '/config/horde.local.php')) {
22        include WICKED_BASE . '/config/horde.local.php';
23    } else {
24        define('HORDE_BASE', realpath(WICKED_BASE . '/..'));
25    }
26}
27
28/* Load the Horde Framework core (needed to autoload
29 * Horde_Registry_Application::). */
30require_once HORDE_BASE . '/lib/core.php';
31
32/**
33 * Wicked application API.
34 *
35 * This file defines Horde's core API interface. Other core Horde libraries
36 * can interact with Wicked through this API.
37 *
38 * @category Horde
39 * @license  http://www.horde.org/licenses/gpl GPL
40 * @package Wicked
41 */
42class Wicked_Application extends Horde_Registry_Application
43{
44    /**
45     */
46    public $version = 'H5 (2.0.8)';
47
48    protected function _bootstrap()
49    {
50        set_include_path(
51            __DIR__ . DIRECTORY_SEPARATOR . 'Text_Wiki'
52            . PATH_SEPARATOR . get_include_path()
53        );
54        $autoloader = $GLOBALS['injector']->getInstance('Horde_Autoloader');
55        $autoloader->addClassPathMapper(
56            new Horde_Autoloader_ClassPathMapper_Prefix(
57                '/^Text_Wiki/',
58                WICKED_BASE . '/lib/Text_Wiki/Text/Wiki'
59            )
60        );
61        $autoloader->addClassPathMapper(
62            new Horde_Autoloader_ClassPathMapper_Prefix(
63                '/^Text_Wiki$/',
64                WICKED_BASE . '/lib/Text_Wiki/Text/Wiki.php'
65            )
66        );
67        $GLOBALS['injector']->bindFactory('Wicked_Driver', 'Wicked_Factory_Driver', 'create');
68    }
69
70    /**
71     * Global variables defined:
72     * - $wicked:   The Wicked_Driver object.
73     */
74    protected function _init()
75    {
76        $GLOBALS['wicked'] = $GLOBALS['injector']->getInstance('Wicked_Driver');
77    }
78
79    /**
80     */
81    public function menu($menu)
82    {
83        global $conf, $page;
84
85        if (@count($conf['menu']['pages'])) {
86            $pages = array(
87                'Wiki/Home' => _("_Home"),
88                'Wiki/Usage' => _("_Usage"),
89                'RecentChanges' => _("_Recent Changes"),
90                'AllPages' => _("_All Pages"),
91                'MostPopular' => _("Most Popular"),
92                'LeastPopular' => _("Least Popular"),
93            );
94            foreach ($conf['menu']['pages'] as $pagename) {
95                /* Determine who we should say referred us. */
96                $curpage = isset($page) ? $page->pageName() : null;
97                $referrer = Horde_Util::getFormData('referrer', $curpage);
98
99                /* Determine if we should depress the button. We have to do
100                 * this on our own because all the buttons go to the same .php
101                 * file, just with different args. */
102                if (!strstr($_SERVER['PHP_SELF'], 'prefs.php') &&
103                    $curpage === $pagename) {
104                    $cellclass = 'current';
105                } else {
106                    $cellclass = '__noselection';
107                }
108
109                $url = Wicked::url($pagename)->add('referrer', $referrer);
110                $menu->add($url, $pages[$pagename], 'wicked-' . str_replace('/', '', $pagename), null, null, null, $cellclass);
111            }
112        }
113    }
114
115    /**
116     */
117    public function perms()
118    {
119        $perms = array(
120            'pages' => array(
121                'title' => _("Pages")
122            )
123        );
124
125        foreach (array('AllPages', 'LeastPopular', 'MostPopular', 'RecentChanges') as $val) {
126            $perms['pages:' . $val] = array(
127                'title' => $val
128            );
129        }
130
131        try {
132            $pages = $GLOBALS['wicked']->getPages();
133            sort($pages);
134            foreach ($pages as $pagename) {
135                $perms['pages:' .$GLOBALS['wicked']->getPageId($pagename)] = array(
136                    'title' => $pagename
137                );
138            }
139        } catch (Wicked_Exception $e) {}
140
141        return $perms;
142    }
143
144    /* Download data. */
145
146    /**
147     */
148    public function download(Horde_Variables $vars)
149    {
150        global $wicked;
151
152        $pageName = $vars->get('page', 'Wiki/Home');
153        $page = Wicked_Page::getPage($pageName);
154        if (!$page->allows(Wicked::MODE_DISPLAY)) {
155            throw new Horde_Exception_PermissionDenied();
156        }
157
158        $page_id = (($id = $wicked->getPageId($pageName)) === false)
159            ? $pageName
160            : $id;
161
162        $version = $vars->version;
163        if (empty($version)) {
164            try {
165                $attachments = $wicked->getAttachedFiles($page_id);
166                foreach ($attachments as $attachment) {
167                    if ($attachment['attachment_name'] == $vars->file) {
168                        $version = $attachment['attachment_version'];
169                    }
170                }
171            } catch (Wicked_Exception $e) {}
172
173            if (empty($version)) {
174                // If we redirect here, we cause an infinite loop with inline
175                // attachments.
176                header('HTTP/1.1 404 Not Found');
177                exit;
178            }
179        }
180
181        try {
182            $data = $wicked->getAttachmentContents($page_id, $vars->file, $version);
183            $wicked->logAttachmentDownload($page_id, $vars->file);
184        } catch (Wicked_Exception $e) {
185            // If we redirect here, we cause an infinite loop with inline
186            // attachments.
187            header('HTTP/1.1 404 Not Found');
188            echo $e->getMessage();
189            exit;
190        }
191
192        $type = Horde_Mime_Magic::analyzeData($data, isset($conf['mime']['magic_db']) ? $conf['mime']['magic_db'] : null);
193        if ($type === false) {
194            $type = Horde_Mime_Magic::filenameToMime($vars->file, false);
195        }
196
197        return array(
198            'data' => $data,
199            'file' => $vars->file,
200            'type' => $type
201        );
202    }
203
204}
205