1<?php
2/**
3 * Generates upgrade scripts for Horde's configuration.
4 *
5 * Currently allows the generation of PHP upgrade scripts for conf.php
6 * files either as download or saved to the server's temporary
7 * directory.
8 *
9 * Copyright 1999-2017 Horde LLC (http://www.horde.org/)
10 *
11 * See the enclosed file COPYING for license information (LGPL-2). If you
12 * did not receive this file, see http://www.horde.org/licenses/lgpl.
13 *
14 * @author   Chuck Hagenbuch <chuck@horde.org>
15 * @category Horde
16 * @license  http://www.horde.org/licenses/lgpl LGPL-2
17 * @package  Horde
18 */
19
20require_once __DIR__ . '/../../lib/Application.php';
21Horde_Registry::appInit('horde', array(
22    'permission' => array('horde:administration:configuration')
23));
24
25$filename = 'horde_configuration_upgrade.php';
26$vars = $injector->getInstance('Horde_Variables');
27
28/* Check if this is only a request to clean up. */
29if ($vars->clean == 'tmp') {
30    $tmp_dir = Horde::getTempDir();
31    $path = Horde_Util::realPath($tmp_dir . '/' . $filename);
32    if (@unlink($tmp_dir . '/' . $filename)) {
33        $notification->push(sprintf(_("Deleted configuration upgrade script \"%s\"."), $path), 'horde.success');
34    } else {
35        $notification->push(sprintf(_("Could not delete configuration upgrade script \"%s\"."), Horde_Util::realPath($path)), 'horde.error');
36    }
37    $registry->rebuild();
38    Horde::url('admin/config/index.php', true)->redirect();
39}
40
41$data = '';
42if ($vars->setup == 'conf' && $vars->type == 'php') {
43    /* Save PHP code into a string for creating the script to be run at the
44     * command prompt. */
45    $data = '#!/usr/bin/env php' . "\n";
46    $data .= '<?php' . "\n";
47    foreach ($session->get('horde', 'config/') as $app => $php) {
48        $path = $registry->get('fileroot', $app) . '/config';
49        $data .= '$conf = \'' . $path . '/conf.php\';' . "\n";
50        /* Add code to save backup. */
51        $data .= 'if (file_exists($conf)) {' . "\n";
52        $data .= '    if (is_link($conf)) {' . "\n";
53        $data .= '        $conf = readlink($conf);' . "\n";
54        $data .= '    }' . "\n";
55        $data .= '    if (@copy($conf, \'' . $path . '/conf.bak.php\')) {' . "\n";
56        $data .= '        echo \'Successfully saved backup configuration.\' . "\n";' . "\n";
57        $data .= '    } else {' . "\n";
58        $data .= '        echo \'Could NOT save a backup configuation.\' . "\n";' . "\n";
59        $data .= '    }' . "\n";
60        $data .= '}' . "\n";
61
62        $data .= 'if (file_put_contents($conf, \'';
63        $data .= str_replace(array('\\', '\''), array('\\\\', '\\\''), $php);
64        $data .= '\')) {' . "\n";
65        $data .= '    echo \'' . sprintf('Saved %s configuration.', $app) . '\' . "\n";' . "\n";
66        $data .= '} else {' . "\n";
67        $data .= '    echo \'' . sprintf('Could NOT save %s configuration.', $app) . '\' . "\n";' . "\n";
68        $data .= '    exit;' . "\n";
69        $data .= '}' . "\n\n";
70    }
71}
72
73if ($vars->save != 'tmp') {
74    /* Output script to browser for download. */
75    $browser->downloadHeaders($filename, 'text/plain', false, strlen($data));
76    echo $data;
77    exit;
78}
79
80$tmp_dir = Horde::getTempDir();
81/* Add self-destruct code. */
82$data .= 'echo \'Self-destructing...\' . "\n";' . "\n";
83$data .= 'if (@unlink(__FILE__)) {' . "\n";
84$data .= '    echo \'Upgrade script deleted.\' . "\n";' . "\n";
85$data .= '} else {' . "\n";
86$data .= '    echo \'WARNING!!! REMOVE SCRIPT MANUALLY FROM ' . $tmp_dir . '\' . "\n";' . "\n";
87$data .= '}' . "\n";
88/* The script should be saved to server's temporary directory. */
89$path = Horde_Util::realPath($tmp_dir . '/' . $filename);
90if (file_put_contents($tmp_dir . '/' . $filename, $data)) {
91    chmod($tmp_dir . '/' . $filename, 0777);
92    $notification->push(sprintf(_("Saved configuration upgrade script to: \"%s\"."), $path), 'horde.success', array('sticky'));
93} else {
94    $notification->push(sprintf(_("Could not save configuration upgrade script to: \"%s\"."), $path), 'horde.error');
95}
96
97Horde::url('admin/config/index.php', true)->redirect();
98