File: scripts.php

package info (click to toggle)
horde3 3.0.4-4sarge7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 15,980 kB
  • ctags: 16,295
  • sloc: php: 68,726; xml: 2,382; sql: 498; makefile: 74; sh: 63; pascal: 6
file content (101 lines) | stat: -rw-r--r-- 4,352 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
<?php
/**
 * Generates upgrade scripts for Horde's setup. Currently allows the generation
 * of PHP upgrade scripts for conf.php files either as download or saved to the
 * server's temporary directory.
 *
 * $Horde: horde/admin/setup/scripts.php,v 1.9.10.1 2005/01/03 12:25:29 jan Exp $
 *
 * Copyright 1999-2005 Charles J. Hagenbuch <chuck@horde.org>
 * Copyright 1999-2005 Jon Parise <jon@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.
 */

define('HORDE_BASE', dirname(__FILE__) . '/../..');
require_once HORDE_BASE . '/lib/base.php';

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

/* Get form data. */
$setup = Util::getFormData('setup');
$type = Util::getFormData('type');
$save = Util::getFormData('save');
$clean = Util::getFormData('clean');

$filename = 'horde_setup_upgrade.php';

/* Check if this is only a request to clean up. */
if ($clean == 'tmp') {
    $tmp_dir = Horde::getTempDir();
    if (unlink($tmp_dir . '/' . $filename)) {
        $notification->push(sprintf(_("Deleted setup upgrade script '%s'."), $tmp_dir . '/' . $filename), 'horde.success');
    } else {
        $notification->push(sprintf(_("Could not delete setup upgrade script '%s'."), $tmp_dir . '/' . $filename), 'horde.error');
    }
    $url = Horde::applicationUrl('admin/setup/index.php', true);
    header('Location: ' . $url);
    exit;
}

$data = '';
if ($setup == 'conf' && $type == 'php') {
    /* A bit ugly here, save PHP code into a string for creating the script
     * to be run at the command prompt. */
    $data = '#!/usr/local/bin/php' . "\n";
    $data .= '<?php' . "\n";
    foreach ($_SESSION['_config'] as $app => $php) {
        $path = $registry->get('fileroot', $app) . '/config';
        /* Add code to save backup. */
        $data .= 'if (file_exists(\'' . $path . '/conf.php\')) {' . "\n";
        $data .= '    if (@copy(\'' . $path . '/conf.php\', \'' . $path . '/conf.php.bak\')) {' . "\n";
        $data .= '        echo \'' . _("Successfully saved backup configuration.") . '\' . "\n";' . "\n";
        $data .= '    } else {' . "\n";
        $data .= '        echo \'' . _("Could not save a backup configuation.") . '\' . "\n";' . "\n";
        $data .= '    }' . "\n";
        $data .= '}' . "\n";

        $data .= 'if ($fp = @fopen(\'' . $path . '/conf.php\', \'w\')) {' . "\n";
        $data .= '    fwrite($fp, \'';
        $data .= String::convertCharset(str_replace(array('\\', '\''), 
                                                    array('\\\\', '\\\''),
                                                    $php),
                                        NLS::getCharset(), 'iso-8859-1');
        $data .= '\');' . "\n";
        $data .= '    fclose($fp);' . "\n";
        $data .= '    echo \'' . sprintf(_("Saved %s configuration."), $app) . '\' . "\n";' . "\n";
        $data .= '} else {' . "\n";
        $data .= '    echo \'' . sprintf(_("Could not save %s configuration."), $app) . '\' . "\n";' . "\n";
        $data .= '}' . "\n\n";
    }
}

if ($save == 'tmp') {
    $tmp_dir = Horde::getTempDir();
    /* Add self-destruct code. */
    $data .= 'echo \'Self-destructing...\' . "\n";' . "\n";
    $data .= 'if (unlink(__FILE__)) {' . "\n";
    $data .= '    echo \'' . _("Upgrade script deleted.") . '\' . "\n";' . "\n";
    $data .= '} else {' . "\n";
    $data .= '    echo \'' . sprintf(_("WARNING!!! REMOVE SCRIPT MANUALLY FROM %s."), $tmp_dir) . '\' . "\n";' . "\n";
    $data .= '}' . "\n";
    /* The script should be saved to server's temporary directory. */
    if ($fp = @fopen($tmp_dir . '/' . $filename, 'w')) {;
        fwrite($fp, $data);
        fclose($fp);
        chmod($tmp_dir . '/' . $filename, 0777);
        $notification->push(sprintf(_("Saved setup upgrade script to: '%s'."), $tmp_dir . '/' . $filename), 'horde.success');
    } else {
        $notification->push(sprintf(_("Could not save setup upgrade script to: '%s'."), $tmp_dir . '/' . $filename), 'horde.error');
    }
    $url = Horde::applicationUrl('admin/setup/index.php', true);
    header('Location: ' . $url);
    exit;
} else {
    /* Output script to browser for download. */
    $browser->downloadHeaders($filename, 'text/plain', false, strlen($data));
    echo $data;
}