1<?php
2/**
3 * Help display script.
4 *
5 * Copyright 1999-2017 Horde LLC (http://www.horde.org/)
6 *
7 * See the enclosed file COPYING for license information (LGPL-2). If you
8 * did not receive this file, see http://www.horde.org/licenses/lgpl.
9 *
10 * @category Horde
11 * @license  http://www.horde.org/licenses/lgpl LGPL-2
12 * @package  Horde
13 */
14
15require_once __DIR__ . '/../../lib/Application.php';
16Horde_Registry::appInit('horde', array('authentication' => 'none'));
17
18$vars = $injector->getInstance('Horde_Variables');
19
20$rtl = $registry->nlsconfig->curr_rtl;
21$show = isset($vars->show)
22    ? Horde_String::lower($vars->show)
23    : 'index';
24$module = isset($vars->module)
25    ? Horde_String::lower(preg_replace('/\W/', '', $vars->module))
26    : 'horde';
27$topic = $vars->get('topic', 'overview');
28
29$base_url = $registry->getServiceLink('help', $module);
30
31$sidebar_url = Horde::url($base_url->copy()->add(array(
32    'show' => 'sidebar',
33    'topic' => $topic
34)));
35
36if ($show == 'index') {
37    $main_url = Horde::url($base_url->copy()->add(array(
38        'show' => 'entry',
39        'topic' => $topic
40    )));
41    $menu_url = Horde::url($base_url->copy()->add('show', 'menu'));
42
43    require HORDE_TEMPLATES . '/help/index.inc';
44    exit;
45}
46
47$help_app = $registry->get('name', ($module == 'admin') ? 'horde' : $module);
48$fileroot = ($module == 'admin')
49    ? $registry->get('fileroot') . '/admin'
50    : $registry->get('fileroot', $module);
51$fileroots = array(
52    $fileroot . '/locale/' . $language . '/',
53    $fileroot . '/locale/' . substr($language, 0, 2) . '/',
54    $fileroot . '/locale/en/'
55);
56foreach ($fileroots as $val) {
57    $fname = $val . 'help.xml';
58    if (@is_file($fname)) {
59        break;
60    }
61}
62
63$views = array();
64switch ($registry->getView()) {
65case $registry::VIEW_BASIC:
66    $views[] = 'basic';
67    break;
68
69case $registry::VIEW_DYNAMIC:
70    $views[] = 'dynamic';
71    break;
72}
73
74$help = new Horde_Help(Horde_Help::SOURCE_FILE, $fname, $views);
75
76$page_output->sidebar = $page_output->topbar = false;
77$page_output->header(array(
78    'body_class' => 'help help_' . urlencode($show),
79    'title' => _("Help")
80));
81
82switch ($show) {
83case 'menu':
84    $version = Horde_String::ucfirst($module) . ' ' . $registry->getVersion($module);
85    require HORDE_TEMPLATES . '/help/menu.inc';
86    break;
87
88case 'sidebar':
89    /* Generate Tabs */
90    if (!isset($vars->side_show)) {
91        $vars->side_show = 'index';
92    }
93    $tabs = new Horde_Core_Ui_Tabs('side_show', $vars);
94    $tabs->addTab(_("Help _Topics"), $sidebar_url, 'index');
95    $tabs->addTab(_("Sea_rch"), $sidebar_url, 'search');
96
97    /* Set up the tree. */
98    $tree = $injector->getInstance('Horde_Core_Factory_Tree')->create('horde_help', 'Javascript');
99    $tree->setOption(array('target' => 'help_main'));
100
101    $contents = '';
102    switch ($vars->get('side_show', 'index')) {
103    case 'index':
104        $topics = $help->topics();
105        $added_nodes = array();
106        $node_params_master = array(
107            'icon' => '',
108            'target' => 'help_main'
109        );
110
111        foreach ($topics as $id => $title) {
112            if (!$title) {
113                continue;
114            }
115
116            /* If the title doesn't begin with :: then replace all
117             * double colons with single colons. */
118            if (substr($title, 0, 2) != '::') {
119                $title = str_replace('::', ': ', $title);
120            }
121
122            /* Split title in multiple levels */
123            $levels = preg_split('/:\s/', $title);
124
125            $idx = '';
126            $lcount = count($levels) - 1;
127            $node_params = $node_params_master;
128            $parent = null;
129
130            foreach ($levels as $key => $name) {
131                $idx .= '|' . $name;
132                if (empty($added_nodes[$idx])) {
133                    if ($key == $lcount) {
134                        $node_params['url'] = $base_url->copy()->setRaw(true)->add(array(
135                            'show' => 'entry',
136                            'topic' => $id
137                        ));
138                        $added_nodes[$idx] = true;
139                    }
140                    $tree->addNode(array(
141                        'id' => $idx,
142                        'parent' => $parent,
143                        'label' => $name,
144                        'expanded' => false,
145                        'params' => $node_params
146                    ));
147                }
148                $parent .= '|' . $name;
149            }
150        }
151        break;
152
153    case 'search':
154        /* Create Form */
155        $searchForm = new Horde_Form($vars, null, 'search');
156        $searchForm->setButtons(_("Search"));
157
158        $searchForm->addHidden('sidebar', 'show', 'text', false);
159        $searchForm->addHidden('', 'module', 'text', false);
160        $searchForm->addHidden('', 'side_show', 'text', false);
161        $searchForm->addVariable(_("Keyword"), 'keyword', 'text', false, false, null, array(null, 20));
162
163        $renderer = new Horde_Form_Renderer();
164        $renderer->setAttrColumnWidth('50%');
165
166        Horde::startBuffer();
167        $searchForm->renderActive($renderer, $vars, $sidebar_url->copy()->setRaw(true), 'post');
168        $contents = Horde::endBuffer() . '<br />';
169
170        $keywords = $vars->get('keyword');
171        if (!empty($keywords)) {
172            $results = $help->search($keywords);
173            foreach ($results as $id => $title) {
174                if (empty($title)) {
175                    continue;
176                }
177                $contents .= Horde::link($base_url->copy()->add(array('show' => 'entry', 'topic' => $id)), null, null, 'help_main') .
178                    htmlspecialchars($title) . "</a><br />\n";
179            }
180        }
181        break;
182    }
183
184    require HORDE_TEMPLATES . '/help/sidebar.inc';
185    break;
186
187case 'entry':
188    if ($topic) {
189        echo $help->lookup($topic);
190    }
191    break;
192}
193
194$page_output->footer();
195