software_update_plugin.inc.php 6.81 KB
Newer Older
1
2
3
<?php

/*
redray's avatar
redray committed
4
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
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
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.
*/

class software_update_plugin {
	
	var $plugin_name = 'software_update_plugin';
	var $class_name  = 'software_update_plugin';
	
36
37
38
39
40
41
42
43
44
	//* This function is called during ispconfig installation to determine
	//  if a symlink shall be created for this plugin.
	function onInstall() {
		global $conf;
		
		return true;
		
	}
	
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
		
	/*
	 	This function is called when the plugin is loaded
	*/
	
	function onLoad() {
		global $app;
		
		/*
		Register for the events
		*/
		
		//* Mailboxes
		$app->plugins->registerEvent('software_update_inst_insert',$this->plugin_name,'process');
		//$app->plugins->registerEvent('software_update_inst_update',$this->plugin_name,'process');
		//$app->plugins->registerEvent('software_update_inst_delete',$this->plugin_name,'process');
		
		
	}
	
65
66
67
68
69
70
71
	function set_install_status($inst_id, $status) {
        global $app;
        
        $app->db->query("UPDATE software_update_inst SET status = '{$status}' WHERE software_update_inst_id = '{$inst_id}'");
        $app->dbmaster->query("UPDATE software_update_inst SET status = '{$status}' WHERE software_update_inst_id = '{$inst_id}'");
    }
    
72
73
74
	function process($event_name,$data) {
		global $app, $conf;
		
75
76
77
78
79
80
81
		//* Get the info of the package:
        $software_update_id = intval($data["new"]["software_update_id"]);
		$software_update = $app->db->queryOneRecord("SELECT * FROM software_update WHERE software_update_id = '$software_update_id'");
		$software_package = $app->db->queryOneRecord("SELECT * FROM software_package WHERE package_name = '".$app->db->quote($software_update['package_name'])."'");
		
		if($software_package['package_type'] == 'ispconfig' && !$conf['software_updates_enabled'] == true) {
			$app->log('Software Updates not enabled on this server. To enable updates, set $conf["software_updates_enabled"] = true; in config.inc.php',LOGLEVEL_WARN);
82
            $this->set_install_status($data["new"]["software_update_inst_id"], "failed");
83
84
85
			return false;
		}
		
86
87
88
89
90
91
92
93
94
95
		$installuser = '';
		if($software_package['package_type'] == 'ispconfig') {
			$installuser = '';
		} elseif ($software_package['package_type'] == 'app') {
			$installuser = 'ispapps';
		} else {
			$app->log('package_type not supported',LOGLEVEL_WARN);
            $this->set_install_status($data["new"]["software_update_inst_id"], "failed");
			return false;
		}
96
97
98
99
		
		$temp_dir = '/tmp/'.md5 (uniqid (rand()));
		$app->log("The temp dir is $temp_dir",LOGLEVEL_DEBUG);
		mkdir($temp_dir);
100
101
		if($installuser != '') exec('chown '.$installuser.' '.$temp_dir);
		
102
		if(!is_dir($temp_dir)) {
103
			$app->log("Unable to create temp directory.",LOGLEVEL_WARN);
104
            $this->set_install_status($data["new"]["software_update_inst_id"], "failed");
105
106
107
			return false;
		}
		
108
109
110
111
112
113
		$cmd = "cd $temp_dir && wget ".$software_update["update_url"];
		if($installuser == '') {
			exec($cmd);
		} else {
			exec("su -c ".escapeshellarg($cmd)." $installuser");
		}
114
115
116
117
118
119
120
121
122
123
		$app->log("Downloading the update file from: ".$software_update["update_url"],LOGLEVEL_DEBUG);
		
		$url_parts = parse_url($software_update["update_url"]);
		$update_filename = basename($url_parts["path"]);
		$app->log("The update filename is $update_filename",LOGLEVEL_DEBUG);
		
		if(is_file($temp_dir.'/'.$update_filename)) {
			
			//* Checking the md5sum
			if(md5_file($temp_dir.'/'.$update_filename) != $software_update["update_md5"]) {
124
				$app->log("The md5 sum of the downloaded file is incorrect. Update aborted.",LOGLEVEL_WARN);
125
126
				exec("rm -rf $temp_dir");
				$app->log("Deleting the temp directory $temp_dir",LOGLEVEL_DEBUG);
127
                $this->set_install_status($data["new"]["software_update_inst_id"], "failed");
128
				return false;
129
130
			} else {
				$app->log("md5sum of the downloaded file is verified.",LOGLEVEL_DEBUG);
131
132
133
134
			}
			
			
			//* unpacking the update
135
136
137
138
139
140
			$cmd = "cd $temp_dir && unzip $update_filename";
			if($installuser == '') {
				exec($cmd);
			} else {
				exec("su -c ".escapeshellarg($cmd)." $installuser");
			}
141
142
143
144
145
			
			if(is_file($temp_dir.'/setup.sh')) {
				// Execute the setup script
				exec('chmod +x '.$temp_dir.'/setup.sh');
				$app->log("Executing setup.sh file in directory $temp_dir",LOGLEVEL_DEBUG);
146
147
148
149
150
151
				$cmd = 'cd '.$temp_dir.' && ./setup.sh > package_install.log';
				if($installuser == '') {
					exec($cmd);
				} else {
					exec("su -c ".escapeshellarg($cmd)." $installuser");
				}
152
153
154
155
156
157
158
                
                $log_data = @file_get_contents("{$temp_dir}/package_install.log");
                if(preg_match("'.*\[OK\]\s*$'is", $log_data)) {
                    $app->log("Installation successful",LOGLEVEL_DEBUG);
                    $app->log($log_data,LOGLEVEL_DEBUG);
                    $this->set_install_status($data["new"]["software_update_inst_id"], "installed");
                } else {
159
                    $app->log("Installation failed:\n\n" . $log_data,LOGLEVEL_WARN);
160
161
                    $this->set_install_status($data["new"]["software_update_inst_id"], "failed");
                }
162
163
			} else {
				$app->log("setup.sh file not found",LOGLEVEL_ERROR);
164
                $this->set_install_status($data["new"]["software_update_inst_id"], "failed");
165
166
			}
		} else {
167
			$app->log("Download of the update file failed",LOGLEVEL_WARN);
168
            $this->set_install_status($data["new"]["software_update_inst_id"], "failed");
169
170
171
172
173
174
175
176
177
178
		}
		
		exec("rm -rf $temp_dir");
		$app->log("Deleting the temp directory $temp_dir",LOGLEVEL_DEBUG);
	}
	

} // end class

?>