]> git.wh0rd.org - tt-rss.git/blame - include/update_self.php
add instances plugin
[tt-rss.git] / include / update_self.php
CommitLineData
acf33893 1<?php
808dd053 2 function update_self_step($link, $step, $params, $force = false) {
639ad600
AD
3 // __FILE__ is in include/ so we need to go one level up
4 $work_dir = dirname(dirname(__FILE__));
acf33893
AD
5 $parent_dir = dirname($work_dir);
6
808dd053
AD
7 if (!chdir($work_dir)) {
8 array_push($log, "Unable to change to work directory: $work_dir");
9 $stop = true; break;
acf33893
AD
10 }
11
808dd053
AD
12 $stop = false;
13 $log = array();
14 if (!is_array($params)) $params = array();
639ad600 15
808dd053
AD
16 switch ($step) {
17 case 0:
caeee35f 18 array_push($log, "Work directory: $work_dir");
acf33893 19
808dd053 20 if (!is_writable($work_dir) && !is_writable("$parent_dir")) {
caeee35f
AD
21 $user = posix_getpwuid(posix_geteuid());
22 $user = $user["name"];
23 array_push($log, "Both tt-rss and parent directories should be writable as current user ($user).");
808dd053
AD
24 $stop = true; break;
25 }
acf33893 26
808dd053
AD
27 if (!file_exists("$work_dir/config.php") || !file_exists("$work_dir/include/sanity_check.php")) {
28 array_push($log, "Work directory $work_dir doesn't look like tt-rss installation.");
29 $stop = true; break;
30 }
acf33893 31
808dd053
AD
32 if (!is_writable(sys_get_temp_dir())) {
33 array_push($log, "System temporary directory should be writable as current user.");
34 $stop = true; break;
35 }
acf33893 36
808dd053 37 array_push($log, "Checking for tar...");
acf33893 38
808dd053
AD
39 $system_rc = 0;
40 system("tar --version >/dev/null", $system_rc);
acf33893 41
808dd053
AD
42 if ($system_rc != 0) {
43 array_push($log, "Could not run tar executable (RC=$system_rc).");
44 $stop = true; break;
45 }
acf33893 46
808dd053 47 array_push($log, "Checking for latest version...");
acf33893 48
808dd053
AD
49 $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"),
50 true);
acf33893 51
808dd053
AD
52 if (!is_array($version_info)) {
53 array_push($log, "Unable to fetch version information.");
54 $stop = true; break;
55 }
acf33893 56
808dd053
AD
57 $target_version = $version_info["version"];
58 $target_dir = "$parent_dir/tt-rss-$target_version";
acf33893 59
808dd053
AD
60 array_push($log, "Target version: $target_version");
61 $params["target_version"] = $target_version;
acf33893 62
808dd053
AD
63 if (version_compare(VERSION, $target_version) != -1 && !$force) {
64 array_push($log, "Your Tiny Tiny RSS installation is up to date.");
65 $stop = true; break;
66 }
acf33893 67
808dd053
AD
68 if (file_exists($target_dir)) {
69 array_push($log, "Target directory $target_dir already exists.");
70 $stop = true; break;
71 }
acf33893 72
808dd053
AD
73 break;
74 case 1:
75 $target_version = $params["target_version"];
76
77 array_push($log, "Downloading checksums...");
78 $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt");
acf33893 79
808dd053
AD
80 if (!$md5sum_data) {
81 array_push($log, "Could not download checksums.");
82 $stop = true; break;
83 }
acf33893 84
808dd053 85 $md5sum_data = explode("\n", $md5sum_data);
acf33893 86
808dd053
AD
87 foreach ($md5sum_data as $line) {
88 $pair = explode(" ", $line);
acf33893 89
808dd053
AD
90 if ($pair[1] == "tt-rss-$target_version.tar.gz") {
91 $target_md5sum = $pair[0];
92 break;
93 }
acf33893 94 }
acf33893 95
808dd053
AD
96 if (!$target_md5sum) {
97 array_push($log, "Unable to locate checksum for target version.");
98 $stop = true; break;
99 }
acf33893 100
808dd053 101 $params["target_md5sum"] = $target_md5sum;
acf33893 102
808dd053
AD
103 break;
104 case 2:
105 $target_version = $params["target_version"];
106 $target_md5sum = $params["target_md5sum"];
acf33893 107
808dd053 108 array_push($log, "Downloading distribution tarball...");
acf33893 109
808dd053
AD
110 $tarball_url = "http://tt-rss.org/download/tt-rss-$target_version.tar.gz";
111 $data = fetch_file_contents($tarball_url);
acf33893 112
808dd053
AD
113 if (!$data) {
114 array_push($log, "Could not download distribution tarball ($tarball_url).");
115 $stop = true; break;
116 }
acf33893 117
808dd053 118 array_push($log, "Verifying tarball checksum...");
acf33893 119
808dd053 120 $test_md5sum = md5($data);
acf33893 121
808dd053
AD
122 if ($test_md5sum != $target_md5sum) {
123 array_push($log, "Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum).");
124 $stop = true; break;
125 }
acf33893 126
808dd053
AD
127 $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss');
128 array_push($log, "Saving download to $tmp_file");
acf33893 129
808dd053
AD
130 if (!file_put_contents($tmp_file, $data)) {
131 array_push($log, "Unable to save download.");
132 $stop = true; break;
133 }
acf33893 134
808dd053 135 $params["tmp_file"] = $tmp_file;
acf33893 136
808dd053
AD
137 break;
138 case 3:
139 $tmp_file = $params["tmp_file"];
140 $target_version = $params["target_version"];
141
142 if (!chdir($parent_dir)) {
143 array_push($log, "Unable to change into parent directory.");
144 $stop = true; break;
145 }
acf33893 146
808dd053 147 $old_dir = tmpdirname($parent_dir, "tt-rss-old");
acf33893 148
808dd053
AD
149 array_push($log, "Renaming tt-rss directory to ".basename($old_dir));
150 if (!rename($work_dir, $old_dir)) {
151 array_push($log, "Unable to rename tt-rss directory.");
152 $stop = true; break;
153 }
154
155 array_push($log, "Extracting tarball...");
156 system("tar zxf $tmp_file", $system_rc);
157
158 if ($system_rc != 0) {
159 array_push($log, "Error while extracting tarball (RC=$system_rc).");
160 $stop = true; break;
161 }
162
163 $target_dir = "$parent_dir/tt-rss-$target_version";
164
165 array_push($log, "Renaming target directory...");
166 if (!rename($target_dir, $work_dir)) {
167 array_push($log, "Unable to rename target directory.");
168 $stop = true; break;
169 }
170
171 if (!chdir($work_dir)) {
172 array_push($log, "Unable to change to work directory: $work_dir");
173 $stop = true; break;
174 }
175
176 array_push($log, "Copying config.php...");
177 if (!copy("$old_dir/config.php", "$work_dir/config.php")) {
178 array_push($log, "Unable to copy config.php to $work_dir.");
179 $stop = true; break;
180 }
acf33893 181
808dd053
AD
182 array_push($log, "Cleaning up...");
183 unlink($tmp_file);
acf33893 184
808dd053
AD
185 array_push($log, "Fixing permissions...");
186
187 $directories = array(
188 CACHE_DIR,
189 CACHE_DIR . "/htmlpurifier",
190 CACHE_DIR . "/export",
191 CACHE_DIR . "/images",
192 CACHE_DIR . "/magpie",
193 CACHE_DIR . "/simplepie",
194 ICONS_DIR,
195 LOCK_DIRECTORY);
196
197 foreach ($directories as $dir) {
198 array_push($log, "-> $dir");
199 chmod($dir, 0777);
200 }
201
202 array_push($log, "Upgrade completed.");
203 array_push($log, "Your old tt-rss directory is saved at $old_dir. ".
204 "Please migrate locally modified files (if any) and remove it.");
205 array_push($log, "You might need to re-enter current directory in shell to see new files.");
206
207 $stop = true;
208 break;
209 default:
210 $stop = true;
acf33893
AD
211 }
212
808dd053
AD
213 return array("step" => $step, "stop" => $stop, "params" => $params, "log" => $log);
214 }
215
216
217 function update_self($link, $force = false) {
218 $step = 0;
219 $stop = false;
220 $params = array();
221
222 while (!$stop) {
223 $rc = update_self_step($link, $step, $params, $force);
224
225 $params = $rc['params'];
226 $stop = $rc['stop'];
227
228 foreach ($rc['log'] as $line) {
229 _debug($line);
230 }
231 ++$step;
232 }
acf33893
AD
233 }
234?>