Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
    pkg_mgr_install.php
5
    Copyright (C) 2004 Scott Ullrich
6
    All rights reserved.
7

    
8
    Redistribution and use in source and binary forms, with or without
9
    modification, are permitted provided that the following conditions are met:
10

    
11
    1. Redistributions of source code must retain the above copyright notice,
12
       this list of conditions and the following disclaimer.
13

    
14
    2. Redistributions in binary form must reproduce the above copyright
15
       notice, this list of conditions and the following disclaimer in the
16
       documentation and/or other materials provided with the distribution.
17

    
18
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
    POSSIBILITY OF SUCH DAMAGE.
28
*/
29

    
30
require("guiconfig.inc");
31
require("xmlparse_pkg.inc");
32

    
33
/*
34
 *   open logging facility
35
 */
36
$fd_log = fopen("/tmp/pkg_mgr.log", "w");
37
fwrite($fd_log, "Begin of Package Manager installation session.\n");
38

    
39
/*
40
 *   update_output_window: update top textarea dynamically.
41
 */
42
function update_status($status) {
43
            echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
44
}
45

    
46
/*
47
 *   update_output_window: update bottom textarea dynamically.
48
 */
49
function update_output_window($text) {
50
            $log = ereg_replace("\n", "\\n", $text);
51
            echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
52
}
53

    
54
/*
55
 *   get_dir: return an array of $dir
56
 */
57
function get_dir($dir) {
58
            $dir_array = array();
59
            $d = dir($dir);
60
            while (false !== ($entry = $d->read())) {
61
                        array_push($dir_array, $entry);
62
            }
63
            $d->close();
64
            return $dir_array;
65
}
66

    
67
/*
68
 *   exec_command_and_return_text: execute command and return output
69
 */
70
function exec_command_and_return_text($command) {
71
            $counter = 0;
72
            $tmp = "";
73
            $fd = popen($command . " 2>&1 ", "r");
74
            while(!feof($fd)) {
75
                        $tmp .= fread($fd,49);
76
            }
77
            fclose($fd);
78
            return $tmp;
79
}
80

    
81
/*
82
 *   exec_command_and_return_text: execute command and update output window dynamically
83
 */
84
function execute_command_return_output($command) {
85
    global $fd_log;
86
    $fd = popen($command . " 2>&1 ", "r");
87
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
88
    $counter = 0;
89
    $counter2 = 0;
90
    while(!feof($fd)) {
91
	$tmp = fread($fd, 50);
92
        fwrite($fd_log, $tmp);
93
	$tmp1 = ereg_replace("\n","\\n", $tmp);
94
	$text = ereg_replace("\"","'", $tmp1);
95
	if($lasttext == "..") {
96
	    $text = "";
97
	    $lasttext = "";
98
	    $counter=$counter-2;
99
	} else {
100
	    $lasttext .= $text;
101
	}
102
	if($counter > 51) {
103
	    $counter = 0;
104
	    $extrabreak = "\\n";
105
	} else {
106
	    $extrabreak = "";
107
	    $counter++;
108
	}
109
	if($counter2 > 600) {
110
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
111
	    $counter2 = 0;
112
	} else
113
	    $counter2++;
114
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
115
    }
116
    fclose($fd);
117
}
118

    
119
$a_out = &$pkg_config['packages'];
120

    
121
?>
122
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
123
<html>
124
<head>
125
<title><?=gentitle("System: Package Manager: Install Package");?></title>
126
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
127
<link href="gui.css" rel="stylesheet" type="text/css">
128
</head>
129

    
130
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
131
<?php include("fbegin.inc"); ?>
132
<p class="pgtitle">System: Package Manager: Install Package</p>
133
<form action="firewall_nat_out_load_balancing.php" method="post">
134
<?php if ($savemsg) print_info_box($savemsg); ?>
135
<?php if (file_exists($d_natconfdirty_path)): ?><p>
136
<?php print_info_box_np("The Package Manager configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
137
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
138
<?php endif; ?>
139
<?php
140
if(!file_exists("/tmp/pkg_config.xml")) {
141
            mwexec("cd {$g['tmp_path']} && /usr/bin/fetch \"http://www.pfsense.com/packages/pkg_config.xml\" >/dev/null 2>&1 ");
142
            if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
143
                        print_info_box_np("Could not download pkg_config.xml from pfSense.com.  Check your DNS settings.");
144
                        die;
145
            }
146
}
147

    
148
$pkg_config = parse_xml_config("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
149

    
150
$id = $_GET['id'];
151

    
152
if(!$pkg_config['packages']) {
153
            print_info_box_np("Could not find any packages in pkg_config.xml");
154
}
155
?>
156
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
157
  <ul id="tabnav">
158
    <li class="tabinact"><a href="pkg_mgr.php">Available Packages</a></li>
159
    <li class="tabact">Installed Packages</a></li>
160
  </ul>
161
  </td></tr>
162
  <tr>
163
    <td class="tabcont">
164
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
165
               <tr>
166
                 <td>
167
	             <textarea cols="100%" rows="1" name="status" id="status" wrap="hard">One moment please... This will take a while!</textarea>
168
	             <textarea cols="100%" rows="25" name="output" id="output" wrap="hard"></textarea>
169
                 </td>
170
               </tr>
171
        </table>
172
    </td>
173
  </tr>
174
</table>
175
</form>
176
<?php include("fend.inc"); ?>
177
</body>
178
</html>
179

    
180
<?
181

    
182
/* install the package */
183

    
184
$a_out = &$pkg_config['packages']['package'];
185
$pkgent = array();
186
$pkgent['name'] = $pkg_config['packages']['package'][$id]['name'];
187
$pkgent['descr'] = $pkg_config['packages']['package'][$id]['descr'];
188
$pkgent['category'] = $pkg_config['packages']['package'][$id]['category'];
189
$pkgent['depends_on_package'] = $a_out[$id]['depends_on_package'];
190
$pkgent['depends_on_package_base'] = $a_out[$id]['depends_on_package_base'];
191
$pkgent['pfsense_package'] = $a_out[$id]['pfsense_package'];
192
$pkgent['pfsense_package_base'] = $a_out[$id]['pfsense_package_base'];
193
$pkgent['configurationfile'] = $a_out[$id]['configurationfile'];
194
$a_out = &$config['packages']['package'];
195

    
196
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
197
if($status <> "") {
198
            // package is already installed!?
199
            print_info_box_np("NOTICE! " . $pkgent['name'] . " is already installed!  Installation will be registered.");
200
}
201

    
202
update_status("Downloading and installing " . $pkgent['name'] . " - " . $pkgent['pfsense_package'] . " and its dependencies ... This could take a moment ...");
203
fwrite($fd_log, "Downloading and installing " . $pkgent['name'] . " ... \n");
204

    
205
$text = exec_command_and_return_text("cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base'] . "/" . $pkgent['pfsense_package']);
206
update_output_window($text);
207
fwrite($fd_log, "Executing: cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base'] . "/" . $pkgent['pfsense_package'] . "\n" . $text);
208

    
209
if ($pkgent['pfsense_package_base']) {
210
            update_status("Downloading and installing " . $pkgent['name'] . " - " . $pkgent['depends_on_package_base'] . " and its dependencies ... This could take a moment ...");
211
            $text = exec_command_and_return_text("cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base'] . "/" . $pkgent['depends_on_package']);
212
            update_output_window($text);
213
            fwrite($fd_log, "cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base'] . "/" . $pkgent['depends_on_package'] . "\n" . $text);;
214
}
215

    
216
$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
217

    
218
$config['installedpackages']['package'][] = $pkgent;
219

    
220
if (isset($id) && $a_out[$id])
221
        $a_out[$id] = $pkgent;
222
else
223
        $a_out[] = $pkgent;
224

    
225
write_config();
226

    
227
$name = $pkgent['name'];
228

    
229
// parse the config file for this package and install neededtext items.
230
if(file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
231
            $config = parse_xml_config("/usr/local/pkg/" . $pkgent['name'] . ".xml", "packagegui");
232
            foreach ($config['modify_system']['item'] as $ms) {
233
                        if($ms['textneeded']) {
234
                                  fwrite($fd_log, "Adding needed text items:\n");
235
                                  $filecontents = exec_command_and_return_text("cat " . $ms['modifyfilename']);
236
                                  $text = ereg_replace($ms['textneeded'], "", $filecontents);
237
                                  $text .= $ms['textneeded'];
238
                                  fwrite($fd_log, $ms['textneeded'] . "\n");
239
                                  $fd = fopen($ms['modifyfilename'], "w");
240
                                  fwrite($fd, $text . "\n");
241
                                  fclose($fd);
242
                        }
243
            }
244
            // install menu item into the ext folder
245
            mwexec("mkdir /usr/local/www/ext/System >/dev/null 2>&1");
246
            mwexec("mkdir /usr/local/www/ext/Interfaces >/dev/null 2>&1");
247
            mwexec("mkdir /usr/local/www/ext/Firewall >/dev/null 2>&1");
248
            mwexec("mkdir /usr/local/www/ext/VPN >/dev/null 2>&1");
249
            mwexec("mkdir /usr/local/www/ext/Status >/dev/null 2>&1");
250
            fwrite($fd_log, "Adding menu option to " . $config['menu']['section'] . "/" . $config['name'] . ":\n");
251
            $fd = fopen("/usr/local/www/ext/" . $config['menu']['section'] . "/" . $config['name'] , "w");
252
            fwrite($fd, "/usr/local/www/pkg.php?xml=" . $config['name'] . "\n");
253
            fclose($fd);
254
} else {
255
            update_output_window("WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
256
            fwrite($fd_log, "WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
257
}
258
fwrite($fd_log, "End of Package Manager installation session.\n");
259

    
260
// return dependency list to output later.
261
$command = "TODELETE=`ls /var/db/pkg | grep " . $name . "` && /usr/sbin/pkg_info -r \$TODELETE | grep Dependency: | cut -d\" \" -f2";
262
$dependencies = exec_command_and_return_text($command);
263
fwrite($fd_log, "Installed " . $name . " and the following depdencies:\n" . $dependencies);
264

    
265
$status = exec_command_and_return_text("ls /var/db/pkg | grep " . $pkgent['name']);
266
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
267
if($status <> "") {
268
            update_status("Package installation completed.");
269
            fwrite($fd_log, "Package installation completed.\n");
270
} else {
271
            update_status("Package WAS NOT installed properly.");
272
            fwrite($fd_log, "Package WAS NOT installed properly.\n");
273
}
274

    
275
// close log
276
fclose($fd_log);
277

    
278
// reopen and read log in
279
$fd_log = fopen("/tmp/pkg_mgr.log", "r");
280
$tmp = "";
281
while(!feof($fd_log)) {
282
            $tmp .= fread($fd_log,49);
283
}
284
fclose($fd_log);
285
$log = ereg_replace("\n", "\\n", $tmp);
286
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
287

    
288
?>
289

    
290

    
291

    
292

    
293

    
294

    
295

    
296

    
297

    
(51-51/93)