software_update_list.php 8.32 KB
Newer Older
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
<?php

/*
Copyright (c) 2008, Till Brehm, projektfarm Gmbh
All rights reserved.

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

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * 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.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/

31 32
require_once '../../lib/config.inc.php';
require_once '../../lib/app.inc.php';
33 34 35

//* Check permissions for module
$app->auth->check_module_permissions('admin');
36
$app->auth->check_security_permissions('admin_allow_software_packages');
37 38 39 40 41 42 43 44 45

//* This is only allowed for administrators
if(!$app->auth->is_admin()) die('only allowed for administrators.');

//* Get the latest updates from the repositorys and insert them in the local database
$updates_added = 0;
$repos = $app->db->queryAllRecords("SELECT software_repo_id, repo_url, repo_username, repo_password FROM software_repo WHERE active = 'y'");
if(is_array($repos)) {
	foreach($repos as $repo) {
46

47 48 49 50 51 52 53 54
		/*
		SELECT software_package.package_name, v1, v2, v3, v4
		FROM software_package
		LEFT JOIN software_update ON ( software_package.package_name = software_update.package_name )
		LEFT JOIN software_update_inst ON ( software_update.software_update_id = software_update_inst.software_update_id )
		GROUP BY package_name
		ORDER BY v1 DESC , v2 DESC , v3 DESC , v4 DESC
		*/
55

56
		$client = new SoapClient(null, array('location' => $repo['repo_url'],
57 58
				'uri'      => $repo['repo_url']));

59 60 61
		$packages = $app->db->queryAllRecords("SELECT software_package.package_name, v1, v2, v3, v4 FROM software_package LEFT JOIN software_update ON ( software_package.package_name = software_update.package_name ) GROUP BY package_name ORDER BY v1 DESC , v2 DESC , v3 DESC , v4 DESC");
		if(is_array($packages)) {
			foreach($packages as $p) {
62

63
				$version = $p['v1'].'.'.$p['v2'].'.'.$p['v3'].'.'.$p['v4'];
64 65
				$updates = $client->get_updates($p['package_name'], $version, $repo['repo_username'], $repo['repo_password']);

66 67
				if(is_array($updates)) {
					foreach($updates as $u) {
68 69

						$version_array = explode('.', $u['version']);
70 71 72 73
						$v1 = $app->functions->intval($version_array[0]);
						$v2 = $app->functions->intval($version_array[1]);
						$v3 = $app->functions->intval($version_array[2]);
						$v4 = $app->functions->intval($version_array[3]);
74

75
						$package_name = $u['package_name'];
76
						$software_repo_id = $app->functions->intval($repo['software_repo_id']);
77 78 79 80 81
						$update_url = $u['url'];
						$update_md5 = $u['md5'];
						$update_dependencies = (isset($u['dependencies']))?$u['dependencies']:'';
						$update_title = $u['title'];
						$type = $u['type'];
82

83
						// Check that we do not have this update in the database yet
84 85
						$sql = "SELECT * FROM software_update WHERE package_name = ? and v1 = ? and v2 = ? and v3 = ? and v4 = ?";
						$tmp = $app->db->queryOneRecord($sql, $package_name, $v1, $v2, $v3, $v4);
86 87
						if(!isset($tmp['software_update_id'])) {
							// Insert the update in the datbase
88
							$sql = "INSERT INTO software_update (software_repo_id, package_name, update_url, update_md5, update_dependencies, update_title, v1, v2, v3, v4, type)
89
							VALUES ($software_repo_id, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
90
							//die($sql);
91
							$app->db->query($sql, $package_name, $update_url, $update_md5, $update_dependencies, $update_title, $v1, $v2, $v3, $v4, $type);
92
						}
93

94 95 96 97 98 99 100 101 102 103
					}
				}
			}
		}
	}
}


//* Install packages, if GET Request
if(isset($_GET['action']) && $_GET['action'] == 'install' && $_GET['package'] != '' && $_GET['server_id'] > 0) {
104
	$package_name = $_GET['package'];
105 106
	$server_id = $app->functions->intval($_GET['server_id']);
	$software_update_id = $app->functions->intval($_GET['id']);
107

108 109 110 111 112 113
	$insert_data = array(
		"package_name" => $package_name,
		"server_id" => $server_id,
		"software_update_id" => $software_update_id,
		"status" => 'installing'
	);
114
	$app->db->datalogInsert('software_update_inst', $insert_data, 'software_update_inst_id');
115

116 117 118 119 120 121 122 123
}



// Show the list in the interface
// Loading the template
$app->uses('tpl');
$app->tpl->newTemplate("form.tpl.htm");
124
$app->tpl->setInclude('content_tpl', 'templates/software_update_list.htm');
125 126 127 128 129 130 131 132 133 134 135 136 137

/*
SELECT software_package.package_name, software_package.package_title, software_update.update_title, v1, v2, v3, v4, software_update_inst.status
		FROM software_package
		LEFT JOIN software_update ON ( software_package.package_name = software_update.package_name )
		LEFT JOIN software_update_inst ON ( software_update.software_update_id = software_update_inst.software_update_id )
GROUP BY software_update.software_update_id
		ORDER BY v1 DESC , v2 DESC , v3 DESC , v4 DESC
*/



if(isset($_POST["server_id"]) && $_POST["server_id"] > 0) {
138
	$server_id = $app->functions->intval($_POST["server_id"]);
139 140 141 142 143 144 145 146 147 148 149 150 151
} else {
	$server_id = 1;
}

$servers = $app->db->queryAllRecords('SELECT server_id, server_name FROM server ORDER BY server_name');
foreach($servers as $key => $server) {
	if($server['server_id'] == $server_id) {
		$servers[$key]['selected'] = 'selected';
	} else {
		$servers[$key]['selected'] = '';
	}
}

152
$app->tpl->setLoop('servers', $servers);
153 154 155 156 157 158 159 160 161 162 163 164 165 166

$sql = "SELECT v1, v2, v3, v4, software_update.update_title, software_update.software_update_id, software_update.package_name, v1, v2, v3, v4, software_update_inst.status
		FROM software_update LEFT JOIN software_update_inst ON ( software_update.software_update_id = software_update_inst.software_update_id )
		WHERE server_id = $server_id
		GROUP BY software_update.package_name
		ORDER BY software_update.package_name ASC, v1 DESC , v2 DESC , v3 DESC , v4 DESC";

$installed_packages = $app->db->queryAllRecords($sql);


$records_out = array();

if(is_array($installed_packages)) {
	foreach($installed_packages as $ip) {
167

168
		// Get version number of the latest installed version
169 170
		$sql = "SELECT v1, v2, v3, v4 FROM software_update, software_update_inst WHERE software_update.software_update_id = software_update_inst.software_update_id AND server_id = ? ORDER BY v1 DESC , v2 DESC , v3 DESC , v4 DESC LIMIT 0,1";
		$lu = $app->db->queryOneRecord($sql, $server_id);
171

172
		// Get all installable updates
173 174
		$sql = "SELECT * FROM software_update WHERE v1 >= ? AND v2 >= ? AND v3 >= ? AND v4 >= ? AND package_name = ? ORDER BY v1 DESC , v2 DESC , v3 DESC , v4 DESC";
		$updates = $app->db->queryAllRecords($sql, $lu['v1'], $lu['v2'], $lu['v3'], $lu['v4'], $ip['package_name']);
175
		//die($sql);
176

177 178 179
		if(is_array($updates)) {
			// Delete the last record as it is already installed
			unset($updates[count($updates)-1]);
180

181 182
			foreach($updates as $key => $u) {
				$version = $u['v1'].'.'.$u['v2'].'.'.$u['v3'].'.'.$u['v4'];
Marius Cramer's avatar
Marius Cramer committed
183
				$installed_txt = "<a href=\"#\" data-load-content=\"admin/software_update_list.php?action=install&package=".$u["package_name"]."&id=".$u["software_update_id"]."&server_id=".$server_id."\">Install Update</a><br />";
184
				$records_out[] = array('version' => $version, 'update_title' => $u["update_title"], 'installed' => $installed_txt);
185

186 187 188 189 190 191 192
			}
		}
	}
}



193
$app->tpl->setLoop('records', $records_out);
194

195
$language = (isset($_SESSION['s']['language']))?$_SESSION['s']['language']:$conf['language'];
Till Brehm's avatar
Till Brehm committed
196
include_once 'lib/lang/'.$app->functions->check_language($language).'_software_update_list.lng';
197 198 199 200 201 202 203
$app->tpl->setVar($wb);


$app->tpl_defaults();
$app->tpl->pparse();


204
?>