Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
992797 1 #!/usr/bin/php
MC 2 <?php
3
4 /*
5 Copyright (c) 2013, Marius Cramer, pixcept KG
6 ISPConfig Copyright (c) 2009-2013, Till Brehm, projektfarm Gmbh
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without modification,
10 are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice,
13       this list of conditions and the following disclaimer.
14     * Redistributions in binary form must reproduce the above copyright notice,
15       this list of conditions and the following disclaimer in the documentation
16       and/or other materials provided with the distribution.
17     * Neither the name of ISPConfig nor the names of its contributors
18       may be used to endorse or promote products derived from this software without
19       specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 function sread() {
34     $input = fgets(STDIN);
35     return rtrim($input);
36 }
37
38 function swrite($text = '') {
39     echo $text;
40 }
41
42 function swriteln($text = '') {
43     echo $text."\n";
44 }
45
46 function simple_query($query, $answers, $default)
47 {        
48         $finished = false;
49         do {
50             if(is_array($answers)) {
51                 $answers_str = implode(',', $answers);
52                 swrite($query.' ('.$answers_str.') ['.$default.']: ');
53             } else {
54                 swrite($query.': ');
55             }
56             $input = sread();
57             
58             //* Stop the installation
59             if($input == 'quit') {
60                 swriteln("Patch terminated by user.\n");
61                 die();
62             }
63             
64             //* Select the default
65             if($input == '') {
66                 $answer = $default;
67                 $finished = true;
68             }
69             
70             //* Set answer id valid
71             if(!is_array($answers) || in_array($input, $answers)) {
72                 $answer = $input;
73                 $finished = true;
74             }
75             
76         } while ($finished == false);
77         swriteln();
78         return $answer;
79 }
80
81 function is_installed($appname) {
82     exec('which '.escapeshellcmd($appname).' 2> /dev/null',$out,$returncode);
83     if(isset($out[0]) && stristr($out[0],$appname) && $returncode == 0) {
84         return true;
85     } else {
86         return false;
87     }
88 }
89
90 echo "\n\n".str_repeat('-',80)."\n";
91 echo " _____ ___________   _____              __ _       
92 |_   _/  ___| ___ \ /  __ \            / _(_)      
93   | | \ `--.| |_/ / | /  \/ ___  _ __ | |_ _  __ _ 
94   | |  `--. \  __/  | |    / _ \| '_ \|  _| |/ _` |
95  _| |_/\__/ / |     | \__/\ (_) | | | | | | | (_| |
96  \___/\____/\_|      \____/\___/|_| |_|_| |_|\__, |
97                                               __/ |
98                                              |___/ ";
99 echo "\n".str_repeat('-',80)."\n";
100 echo "\n\n>> Patch tool  \n\n";
101 echo "Please enter the patch id that you want to be applied to your ISPConfig installation.\nPlease be aware that we take NO responsibility that this will work for you.\nOnly use patches if you know what you are doing.\n\n";
102
103 if(!is_installed('patch')) {
104     swriteln("The program 'patch' is missing on your server. Please install it and try again.");
842bf5 105     exit;
992797 106 }
MC 107
108 $patch_id = simple_query('Enter patch id', false, '');
109 if($patch_id == '') {
110     swriteln("Patch terminated by user.\n");
111     die();
112 } elseif(preg_match('/^[a-zA-Z0-9_\-]+$/', $patch_id) == false) {
113     swriteln("Invalid patch id.\n");
114     die();
115 }
116
117 $patch_data = @file_get_contents('http://ispconfig.org/downloads/patches/' . $patch_id . '.diff');
118 if(!$patch_data) {
119     swriteln("Patch with id " . $patch_id . " does not exist.\n");
120     die();
121 }
122
123 $patch_text = @file_get_contents('http://ispconfig.org/downloads/patches/' . $patch_id . '.txt');
124 if($patch_text) {
125     $ok = simple_query("Patch description:\n".str_repeat("-", 80)."\n".$patch_text."\n".str_repeat("-", 80)."\nDo you really want to apply this patch now?", array('y','n'), 'y');
126     if($ok != 'y') {
127         swriteln("Patch terminated by user.\n");
128         die();
129     }
130 }
131
132 $temp_file = tempnam(sys_get_temp_dir(), 'ispc');
133
134 file_put_contents($temp_file, $patch_data);
135 chdir('/usr/local/ispconfig');
136
137 passthru('patch -p0 < ' . escapeshellarg($temp_file));
138
139 unlink($temp_file);
140
141 exit;
142
143 ?>