File: index.php

package info (click to toggle)
horde3 3.3.8%2Bdebian0-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,220 kB
  • ctags: 28,224
  • sloc: php: 115,191; xml: 4,247; sql: 2,417; sh: 147; makefile: 140
file content (284 lines) | stat: -rw-r--r-- 12,766 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
 * $Horde: horde/admin/setup/index.php,v 1.28.4.19 2009/01/06 15:22:11 jan Exp $
 *
 * Copyright 1999-2009 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author Chuck Hagenbuch <chuck@horde.org>
 */

@define('HORDE_BASE', dirname(__FILE__) . '/../..');
require_once HORDE_BASE . '/lib/base.php';
require_once 'Horde/Template.php';
require_once 'Horde/Form/Renderer.php';

if (!Auth::isAdmin()) {
    Horde::fatal('Forbidden.', __FILE__, __LINE__);
}

/**
 * Returns the CVS version for a given file.
 */
function _getVersion($file)
{
    $data = @file_get_contents($file);
    if (preg_match('/\$.*?conf\.xml,v (.*?) .*\$/', $data, $match)) {
        return $match[1];
    } else {
        return false;
    }
}

/**
 * Does an FTP upload to save the configuration.
 */
function _uploadFTP($params)
{
    global $registry, $notification;

    require_once 'VFS.php';
    $params['hostspec'] = 'localhost';
    $vfs = &VFS::singleton('ftp', $params);
    if (is_a($vfs, 'PEAR_Error')) {
        $notification->push(sprintf(_("Could not connect to server \"%s\" using FTP: %s"), $params['hostspec'], $vfs->getMessage()), 'horde.error');
        return false;
    }

    /* Loop through the config and write to FTP. */
    $no_errors = true;
    foreach ($_SESSION['_config'] as $app => $config) {
        $path = $registry->get('fileroot', $app) . '/config';
        /* Try to back up the current conf.php. */
        if ($vfs->exists($path, 'conf.php')) {
            if (($result = $vfs->rename($path, 'conf.php', $path, '/conf.bak.php')) === true) {
                $notification->push(_("Successfully saved backup configuration."), 'horde.success');
            } elseif (is_a($result, 'PEAR_Error')) {
                $notification->push(sprintf(_("Could not save a backup configuation: %s"), $result->getMessage()), 'horde.error');
            } else {
                $notification->push(_("Could not save a backup configuation."), 'horde.error');
            }
        }

        $write = $vfs->writeData($path, 'conf.php', $config);
        if (is_a($write, 'PEAR_Error')) {
            $no_errors = false;
            $notification->push(sprintf(_("Could not write configuration for \"%s\": %s"), $app, $write->getMessage()), 'horde.error');
        } else {
            $notification->push(sprintf(_("Successfully wrote %s"), Util::realPath($path . '/conf.php')), 'horde.success');
            unset($_SESSION['_config'][$app]);
        }
    }
    $registry->clearCache();
    return $no_errors;
}

/* Check for versions if requested. */
$versions = array();
if (Util::getFormData('check_versions')) {
    require_once 'HTTP/Request.php';
    require_once 'Horde/DOM.php';
    $http = new HTTP_Request('http://www.horde.org/versions.php');
    $result = $http->sendRequest();
    if (is_a($result, 'PEAR_Error')) {
        $notification->push($result, 'horde.error');
    } elseif ($http->getResponseCode() != 200) {
        $notification->push(_("Unexpected response from server, try again later."), 'horde.error');
    } else {
        $dom = Horde_DOM_Document::factory(array('xml' => $http->getResponseBody()));
        $stable = $dom->get_elements_by_tagname('stable');
        if (!count($stable) || !$stable[0]->has_child_nodes()) {
            $notification->push(_("Invalid response from server."), 'horde.error');
        } else {
            for ($app = $stable[0]->first_child();
                 !empty($app);
                 $app = $app->next_sibling()) {
                if (!is_a($app, 'domelement') &&
                    !is_a($app, 'Horde_DOM_Element')) {
                    continue;
                }
                $version = $app->get_elements_by_tagname('version');
                $url = $app->get_elements_by_tagname('url');
                $versions[$app->get_attribute('name')] = array(
                    'version' => $version[0]->get_content(),
                    'url' => $url[0]->get_content());
            }
        }
    }
}

/* Set up some icons. */
$success = Horde::img('alerts/success.png', '', '', $registry->getImageDir('horde'));
$warning = Horde::img('alerts/warning.png', '', '', $registry->getImageDir('horde'));
$error = Horde::img('alerts/error.png', '', '', $registry->getImageDir('horde'));

$conf_url = Horde::applicationUrl('admin/setup/config.php');
$a = $registry->listApps(array('inactive', 'hidden', 'notoolbar', 'active', 'admin'));
$apps = array();
$i = -1;
if (file_exists(HORDE_BASE . '/lib/bundle.php')) {
    include HORDE_BASE . '/lib/bundle.php';
    $apps[0] = array('sort' => '00',
                     'name' => '<strong>' . BUNDLE_FULLNAME . '</strong>',
                     'icon' => Horde::img($registry->get('icon', 'horde'),
                                          BUNDLE_FULLNAME, '', ''),
                     'version' => '<strong>' . BUNDLE_VERSION . '</strong>');
    if (!empty($versions)) {
        if (!isset($versions[BUNDLE_NAME])) {
            $apps[0]['load'] = $warning;
            $apps[0]['vstatus'] = _("No stable version exists yet.");
        } elseif (version_compare($versions[BUNDLE_NAME]['version'], BUNDLE_VERSION, '>')) {
            $apps[0]['load'] = $error;
            $apps[0]['vstatus'] = Horde::link($versions[BUNDLE_NAME]['url'], sprintf(_("Download %s"), BUNDLE_FULLNAME)) . sprintf(_("A newer version (%s) exists."), $versions[BUNDLE_NAME]['version']) . '</a> ';
        } else {
            $apps[0]['load'] = $success;
            $apps[0]['vstatus'] = _("Application is up-to-date.");
        }
    }
    $i++;
}
foreach ($a as $app) {
    /* Skip app if no conf.xml file. */
    $path = $registry->get('fileroot', $app) . '/config';
    if (!file_exists($path . '/conf.xml')) {
        continue;
    }

    $i++;
    $path = $registry->get('fileroot', $app) . '/config';

    $conf_link = Util::addParameter($conf_url, 'app', $app);
    $conf_link = Horde::link($conf_link, sprintf(_("Configure %s"), $app));
    $apps[$i]['sort'] = $registry->get('name', $app) . ' (' . $app . ')';
    $apps[$i]['name'] = $conf_link . $apps[$i]['sort'] . '</a>';
    $apps[$i]['icon'] = Horde::img($registry->get('icon', $app), $registry->get('name', $app), '', '');
    $apps[$i]['version'] = '';
    if (is_readable($registry->get('fileroot', $app) . '/lib/version.php')) {
        require_once $registry->get('fileroot', $app) . '/lib/version.php';
        $version_constant = String::upper($app) . '_VERSION';
        if (defined($version_constant)) {
            $apps[$i]['version'] = constant($version_constant);
            if (!empty($versions)) {
                if (!isset($versions[$app])) {
                    $apps[$i]['load'] = $warning;
                    $apps[$i]['vstatus'] = _("No stable version exists yet.");
                } elseif (version_compare(preg_replace('/H3 \((.*)\)/', '$1', $versions[$app]['version']), preg_replace('/H3 \((.*)\)/', '$1', $apps[$i]['version']), '>')) {
                    $apps[$i]['load'] = $error;
                    $apps[$i]['vstatus'] = Horde::link($versions[$app]['url'], sprintf(_("Download %s"), $app)) . sprintf(_("A newer version (%s) exists."), $versions[$app]['version']) . '</a> ';
                } else {
                    $apps[$i]['load'] = $success;
                    $apps[$i]['vstatus'] = _("Application is up-to-date.");
                }
            }
        }
    }
    if (!file_exists($path . '/conf.php')) {
        /* No conf.php exists. */
        $apps[$i]['conf'] = $conf_link . $error . '</a>';
        $apps[$i]['status'] = _("Missing configuration. You must generate it before using this application.");
    } else {
        /* A conf.php exists, get the xml version. */
        if (($xml_ver = _getVersion($path . '/conf.xml')) === false) {
            $apps[$i]['conf'] = $conf_link . $warning . '</a>';
            $apps[$i]['status'] = _("No version found in original configuration. Regenerate configuration.");
            continue;
        }
        /* Get the generated php version. */
        if (($php_ver = _getVersion($path . '/conf.php')) === false) {
            /* No version found in generated php, suggest regenarating
             * just in case. */
            $apps[$i]['conf'] = $conf_link . $warning . '</a>';
            $apps[$i]['status'] = _("No version found in your configuration. Regenerate configuration.");
            continue;
        }

        if ($xml_ver != $php_ver) {
            /* Versions are not the same, configuration is out of date. */
            $apps[$i]['conf'] = $conf_link . $error . '</a>';
            $apps[$i]['status'] = _("Configuration is out of date.");
            continue;
        } else {
            /* Configuration is ok. */
            $apps[$i]['conf'] = $conf_link . $success . '</a>';
            $apps[$i]['status'] = _("Application is ready.");
        }
    }
}

/* Sort the apps by name. */
require_once 'Horde/Array.php';
Horde_Array::arraySort($apps, 'sort');

/* Set up any actions that may be offered. */
$actions = array();
$ftpform = '';
if (!empty($_SESSION['_config'])) {
    Horde::addScriptFile('popup.js', 'horde', true);
    $url = Horde::applicationUrl('admin/setup/diff.php');
    $action = _("Show differences between currently saved and the newly generated configuration.");
    $actions[] = array('icon' => Horde::img('search.png', '', 'align="middle"', $registry->getImageDir('horde')),
                       'link' => Horde::link('#', '', '', '', 'popup(\'' . $url . '\',640, 480); return false;') . $action . '</a>');

    $url = Horde::applicationUrl('admin/setup/scripts.php');
    /* Action to download the configuration upgrade PHP script. */
    $url = Util::addParameter($url, array('setup' => 'conf', 'type' => 'php'));
    $action = _("Download generated configuration as PHP script.");
    $actions[] = array('icon' => Horde::img('download.png', '', 'align="middle"', $registry->getImageDir('horde')),
                       'link' => Horde::link($url) . $action . '</a>');
    /* Action to save the configuration upgrade PHP script. */
    $url = Util::addParameter($url, 'save', 'tmp');
    $action = _("Save generated configuration as a PHP script to your server's temporary directory.");
    $actions[] = array('icon' => Horde::img('save.png', '', 'align="middle"', $registry->getImageDir('horde')),
                       'link' => Horde::link($url) . $action . '</a>');

    /* Set up the form for FTP upload of scripts. */
    require_once 'Horde/Form.php';
    require_once 'Horde/Variables.php';

    $vars = Variables::getDefaultVariables();
    $ftpform = new Horde_Form($vars);
    $ftpform->setButtons(_("Upload"), true);
    $ftpform->addVariable(_("Username"), 'username', 'text', true, false, null, array('', 20));
    $ftpform->addVariable(_("Password"), 'password', 'password', false);

    if ($ftpform->validate($vars)) {
        $ftpform->getInfo($vars, $info);
        $upload = _uploadFTP($info);
        if ($upload) {
            $notification->push(_("Uploaded all application setup files to the server."), 'horde.success');
            $url = Horde::applicationUrl('admin/setup/index.php', true);
            header('Location: ' . $url);
            exit;
        }
    }
    /* Render the form. */
    $ftpform = Util::bufferOutput(array($ftpform, 'renderActive'), new Horde_Form_Renderer(), $vars, 'index.php', 'post');
}

if (file_exists(Horde::getTempDir() . '/horde_setup_upgrade.php')) {
    /* Action to remove the configuration upgrade PHP script. */
    $url = Horde::applicationUrl('admin/setup/scripts.php');
    $url = Util::addParameter($url, 'clean', 'tmp');
    $action = _("Remove saved script from server's temporary directory.");
    $actions[] = array('icon' => Horde::img('delete.png', '', 'align="middle"', $registry->getImageDir('horde')),
                       'link' => Horde::link($url) . $action . '</a>');
}

/* Set up the template. */
$template = new Horde_Template();
$template->setOption('gettext', true);
$template->set('versions', !empty($versions), true);
$template->set('version_action', Horde::applicationUrl('admin/setup/index.php'));
$template->set('version_input', Util::formInput());
$template->set('apps', $apps);
$template->set('actions', $actions, true);
$template->set('ftpform', $ftpform, true);

$title = sprintf(_("%s Setup"), $registry->get('name', 'horde'));
Horde::addScriptFile('stripe.js', 'horde', true);
require HORDE_TEMPLATES . '/common-header.inc';
require HORDE_TEMPLATES . '/admin/menu.inc';
echo $template->fetch(HORDE_TEMPLATES . '/admin/setup/index.html');
require HORDE_TEMPLATES . '/common-footer.inc';