<commit_before>#!/usr/local/bin/php
<?php
/*
    pkg_mgr_install.php
    part of pfSense (http://www.pfSense.com)
    Copyright (C) 2004 <NAME>
    All rights reserved.

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

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

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

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

require("guiconfig.inc");
require("xmlparse_pkg.inc");

/* /usr/local/pkg/ is where xml package files are stored. */
if(!file_exists("/usr/local/pkg/")) mwexec("mkdir -p /usr/local/pkg/");
/* /usr/local/pkg/pf is where custom php hook packages live to alter the rules when needed */
if(!file_exists("/usr/local/pkg/pf")) mwexec("mkdir -p /usr/local/pkg/pf");
/* /usr/local/www/ext is where package links live for the left hand pane */
if(!file_exists("/usr/local/www/ext/")) mwexec("mkdir -p /usr/local/www/ext");

$pb_percent = 1;

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

$packages_to_install = Array();

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

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php
include("fbegin.inc");
?>
<p class="pgtitle">System: Package Manager: Install Package</p>
<form action="pkg_mgr_install.php" method="post">

<?php if ($savemsg) print_info_box($savemsg); ?>
<?php

if($_GET['showlog'] <> "") {
            echo "<table>";
            echo "<tr><td>";
            echo "<pre>";
            // reopen and read log in
            $fd = fopen("{$g['tmp_path']}/pkg_mgr.log", "r");
            $tmp = "";
            while(!feof($fd)) {
                        $tmp .= fread($fd,49);
            }
            fclose($fd);
            echo $tmp;
            echo "</pre>";
            echo "</td></tr>";
            echo "</table>";
            exit;
}

/*
 *   open logging facility
 */
$fd_log = fopen("{$g['tmp_path']}/pkg_mgr.log", "w");
if(!$fd_log) log_error("Warning, could not open {$g['tmp_path']}/pkg_mgr.log for writing");
fwrite($fd_log, "Begin of Package Manager installation session.\n");

fetch_latest_pkg_config();

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

if(!$pkg_config['packages'])
    print_info_box_np("Could not find any packages in pkg_config.xml");

?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
  <ul id="tabnav">
    <li class="tabinact"><a href="pkg_mgr.php">Available Packages</a></li>
    <li class="tabact">Installed Packages</a></li>
  </ul>
  </td></tr>
  <tr>
    <td class="tabcont">
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
               <tr>
                 <td>
                     <!-- progress bar -->
                     <center>
                     <table id="progholder" name="progholder" height='20' border='1' bordercolor='black' width='420' bordercolordark='#000000' bordercolorlight='#000000' style='border-collapse: collapse' colspacing='2' cellpadding='2' cellspacing='2'><tr><td><img border='0' src='progress_bar.gif' width='280' height='23' name='progressbar' id='progressbar'></td></tr></table>
                     <br>
	             <!-- status box -->
                     <textarea cols="60" rows="1" name="status" id="status" wrap="hard">One moment please... This will take a while!</textarea>
                     <!-- command output box -->
	             <textarea cols="60" rows="25" name="output" id="output" wrap="hard"></textarea>
                     </center>
                 </td>
               </tr>
        </table>
    </td>
  </tr>
</table>
</form>
<?php include("fend.inc"); ?>
</body>
</html>

<?php



if($_GET['mode'] == "reinstallall") {
    /*
     *  Loop through installed packages and if name matches
     *  push the package id onto array to reinstall
     */
    $counter = 0;
    foreach($pkg_config['packages']['package'] as $available_package) {
        foreach($config['installedpackages']['package'] as $package) {
            if($package['name'] == $available_package['name']) {
                array_push($packages_to_install, $counter);
                update_status("Adding package " . $package['name']);
                fwrite($fd_log, "Adding (" . $counter . ") " . $package['name'] . " to package installation array.\n" . $status);
            }
        }
        $counter++;
    }
} else {
    /*
     * Push the desired package id onto the install packages array
     */
    fwrite($fd_log, "Single package installation started.\n");
    array_push($packages_to_install, $_GET['id']);
}

/*
 *  Loop through packages_to_install, installing needed packages
 */
foreach ($packages_to_install as $id) {

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

    update_progress_bar($pb_percent);
    $pb_percent += 10;

    /*
     * install the package
     */

    // Ensure directories are in place for pkg_add.
    mwexec("mkdir {$g['www_path']}/ext/Services >/dev/null 2>&1");
    mwexec("mkdir {$g['www_path']}/ext/System >/dev/null 2>&1");
    mwexec("mkdir {$g['www_path']}/ext/Interfaces >/dev/null 2>&1");
    mwexec("mkdir {$g['www_path']}/ext/Firewall >/dev/null 2>&1");
    mwexec("mkdir {$g['www_path']}/ext/VPN >/dev/null 2>&1");
    mwexec("mkdir {$g['www_path']}/ext/Status >/dev/null 2>&1");
    mwexec("mkdir {$g['www_path']}/ext/Diagnostics >/dev/null 2>&1");
    mwexec("mkdir /usr/local/pkg >/dev/null 2>&1");

    update_progress_bar($pb_percent);
    $pb_percent += 10;

    $a_out = &$pkg_config['packages']['package'];

    if($pkg_config['packages']['package'][$id]['verifyinstalledpkg'] <> "")
        $package_to_verify = $pkg_config['packages']['package'][$id]['verifyinstalledpkg'];
    else
        $package_to_verify = $pkg_config['packages']['package'][$id]['name'];

    $pkgent = array();<commit_msg>use new make_dirs() and safe_mkdir() functions<commit_after>#!/usr/local/bin/php
<?php
/*
    pkg_mgr_install.php
    part of pfSense (http://www.pfSense.com)
    Copyright (C) 2004 <NAME>
    All rights reserved.

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

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

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

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

require("guiconfig.inc");
require("xmlparse_pkg.inc");

/* /usr/local/pkg/ is where xml package files are stored. */
make_dirs("/usr/local/pkg");
/* /usr/local/pkg/pf is where custom php hook packages live to alter the rules when needed */
make_dirs("/usr/local/pkg/pf");
/* /usr/local/www/ext is where package links live for the left hand pane */
make_dirs("/usr/local/www/ext");

$pb_percent = 1;

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

$packages_to_install = Array();

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

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php
include("fbegin.inc");
?>
<p class="pgtitle">System: Package Manager: Install Package</p>
<form action="pkg_mgr_install.php" method="post">

<?php if ($savemsg) print_info_box($savemsg); ?>
<?php

if($_GET['showlog'] <> "") {
            echo "<table>";
            echo "<tr><td>";
            echo "<pre>";
            // reopen and read log in
            $fd = fopen("{$g['tmp_path']}/pkg_mgr.log", "r");
            $tmp = "";
            while(!feof($fd)) {
                        $tmp .= fread($fd,49);
            }
            fclose($fd);
            echo $tmp;
            echo "</pre>";
            echo "</td></tr>";
            echo "</table>";
            exit;
}

/*
 *   open logging facility
 */
$fd_log = fopen("{$g['tmp_path']}/pkg_mgr.log", "w");
if(!$fd_log) log_error("Warning, could not open {$g['tmp_path']}/pkg_mgr.log for writing");
fwrite($fd_log, "Begin of Package Manager installation session.\n");

fetch_latest_pkg_config();

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

if(!$pkg_config['packages'])
    print_info_box_np("Could not find any packages in pkg_config.xml");

?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
  <ul id="tabnav">
    <li class="tabinact"><a href="pkg_mgr.php">Available Packages</a></li>
    <li class="tabact">Installed Packages</a></li>
  </ul>
  </td></tr>
  <tr>
    <td class="tabcont">
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
               <tr>
                 <td>
                     <!-- progress bar -->
                     <center>
                     <table id="progholder" name="progholder" height='20' border='1' bordercolor='black' width='420' bordercolordark='#000000' bordercolorlight='#000000' style='border-collapse: collapse' colspacing='2' cellpadding='2' cellspacing='2'><tr><td><img border='0' src='progress_bar.gif' width='280' height='23' name='progressbar' id='progressbar'></td></tr></table>
                     <br>
	             <!-- status box -->
                     <textarea cols="60" rows="1" name="status" id="status" wrap="hard">One moment please... This will take a while!</textarea>
                     <!-- command output box -->
	             <textarea cols="60" rows="25" name="output" id="output" wrap="hard"></textarea>
                     </center>
                 </td>
               </tr>
        </table>
    </td>
  </tr>
</table>
</form>
<?php include("fend.inc"); ?>
</body>
</html>

<?php



if($_GET['mode'] == "reinstallall") {
    /*
     *  Loop through installed packages and if name matches
     *  push the package id onto array to reinstall
     */
    $counter = 0;
    foreach($pkg_config['packages']['package'] as $available_package) {
        foreach($config['installedpackages']['package'] as $package) {
            if($package['name'] == $available_package['name']) {
                array_push($packages_to_install, $counter);
                update_status("Adding package " . $package['name']);
                fwrite($fd_log, "Adding (" . $counter . ") " . $package['name'] . " to package installation array.\n" . $status);
            }
        }
        $counter++;
    }
} else {
    /*
     * Push the desired package id onto the install packages array
     */
    fwrite($fd_log, "Single package installation started.\n");
    array_push($packages_to_install, $_GET['id']);
}

/*
 *  Loop through packages_to_install, installing needed packages
 */
foreach ($packages_to_install as $id) {

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

    update_progress_bar($pb_percent);
    $pb_percent += 10;

    /*
     * install the package
     */

    // Ensure directories are in place for pkg_add.
    safe_mkdir("{$g['www_path']}/ext/Services", 0755);
    safe_mkdir("{$g['www_path']}/ext/System", 0755);
    safe_mkdir("{$g['www_path']}/ext/Interfaces", 0755);
    safe_mkdir("{$g['www_path']}/ext/Firewall", 0755);
    safe_mkdir("{$g['www_path']}/ext/VPN", 0755);
    safe_mkdir("{$g['www_path']}/ext/Status", 0755);
    safe_mkdir("{$g['www_path']}/ext/Diagnostics", 0755);
    safe_mkdir("/usr/local/pkg", 0755);

    update_progress_bar($pb_percent);
    $pb_percent += 10;

    $a_out = &$pkg_config['packages']['package'];

    if($pkg_config['packages']['package'][$id]['verifyinstalledpkg'] <> "")
        $package_to_verify = $pkg_config['packages']['package'][$id]['verifyinstalledpkg'];
    else
        $package_to_verify = $pkg_config['packages']['package'][$id]['name'];

    $pkgent = array();
{'fraction_non_alphanumeric': 0.165379113018598, 'fraction_numerical': 0.015021459227467811, 'mean_word_length': 3.1120588235294115, 'pattern_counts': {'":': 0, '<': 153, '<?xml version=': 0, '>': 167, 'https://': 0, 'lorem ipsum': 0, 'www.': 21, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1988472', 'n_tokens_mistral': 4540, 'n_tokens_neox': 4212, 'n_words': 1115}