]> git.wh0rd.org - tt-rss.git/blame - plugins/updater/updater.php
add -list-plugins option; about sections to plugins
[tt-rss.git] / plugins / updater / updater.php
CommitLineData
5cedb389
AD
1<?php
2class Updater extends Plugin {
3
4 private $link;
5 private $host;
6
7a866114
AD
7 function _about() {
8 return array(1.0,
9 "Updates tt-rss installation to latest version.",
10 "fox");
11 }
12
5cedb389
AD
13 function __construct($host) {
14 $this->link = $host->get_link();
15 $this->host = $host;
16
17 $host->add_hook($host::HOOK_PREFS_TAB, $this);
73f28fe9
AD
18
19 $host->add_command("update-self",
20 "update tt-rss installation to latest version",
21 $this);
22 }
23
24 function update_self_step($link, $step, $params, $force = false) {
25 // __FILE__ is in plugins/updater so we need to go one level up
26 $work_dir = dirname(dirname(dirname(__FILE__)));
27 $parent_dir = dirname($work_dir);
28
29 if (!chdir($work_dir)) {
30 array_push($log, "Unable to change to work directory: $work_dir");
31 $stop = true; break;
32 }
33
34 $stop = false;
35 $log = array();
36 if (!is_array($params)) $params = array();
37
38 switch ($step) {
39 case 0:
40 array_push($log, "Work directory: $work_dir");
41
42 if (!is_writable($work_dir) && !is_writable("$parent_dir")) {
43 $user = posix_getpwuid(posix_geteuid());
44 $user = $user["name"];
45 array_push($log, "Both tt-rss and parent directories should be writable as current user ($user).");
46 $stop = true; break;
47 }
48
49 if (!file_exists("$work_dir/config.php") || !file_exists("$work_dir/include/sanity_check.php")) {
50 array_push($log, "Work directory $work_dir doesn't look like tt-rss installation.");
51 $stop = true; break;
52 }
53
54 if (!is_writable(sys_get_temp_dir())) {
55 array_push($log, "System temporary directory should be writable as current user.");
56 $stop = true; break;
57 }
58
59 array_push($log, "Checking for tar...");
60
61 $system_rc = 0;
62 system("tar --version >/dev/null", $system_rc);
63
64 if ($system_rc != 0) {
65 array_push($log, "Could not run tar executable (RC=$system_rc).");
66 $stop = true; break;
67 }
68
69 array_push($log, "Checking for latest version...");
70
71 $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"),
72 true);
73
74 if (!is_array($version_info)) {
75 array_push($log, "Unable to fetch version information.");
76 $stop = true; break;
77 }
78
79 $target_version = $version_info["version"];
80 $target_dir = "$parent_dir/tt-rss-$target_version";
81
82 array_push($log, "Target version: $target_version");
83 $params["target_version"] = $target_version;
84
85 if (version_compare(VERSION, $target_version) != -1 && !$force) {
86 array_push($log, "Your Tiny Tiny RSS installation is up to date.");
87 $stop = true; break;
88 }
89
90 if (file_exists($target_dir)) {
91 array_push($log, "Target directory $target_dir already exists.");
92 $stop = true; break;
93 }
94
95 break;
96 case 1:
97 $target_version = $params["target_version"];
98
99 array_push($log, "Downloading checksums...");
100 $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt");
101
102 if (!$md5sum_data) {
103 array_push($log, "Could not download checksums.");
104 $stop = true; break;
105 }
106
107 $md5sum_data = explode("\n", $md5sum_data);
108
109 foreach ($md5sum_data as $line) {
110 $pair = explode(" ", $line);
111
112 if ($pair[1] == "tt-rss-$target_version.tar.gz") {
113 $target_md5sum = $pair[0];
114 break;
115 }
116 }
117
118 if (!$target_md5sum) {
119 array_push($log, "Unable to locate checksum for target version.");
120 $stop = true; break;
121 }
122
123 $params["target_md5sum"] = $target_md5sum;
124
125 break;
126 case 2:
127 $target_version = $params["target_version"];
128 $target_md5sum = $params["target_md5sum"];
129
130 array_push($log, "Downloading distribution tarball...");
131
132 $tarball_url = "http://tt-rss.org/download/tt-rss-$target_version.tar.gz";
133 $data = fetch_file_contents($tarball_url);
134
135 if (!$data) {
136 array_push($log, "Could not download distribution tarball ($tarball_url).");
137 $stop = true; break;
138 }
139
140 array_push($log, "Verifying tarball checksum...");
141
142 $test_md5sum = md5($data);
143
144 if ($test_md5sum != $target_md5sum) {
145 array_push($log, "Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum).");
146 $stop = true; break;
147 }
148
149 $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss');
150 array_push($log, "Saving download to $tmp_file");
151
152 if (!file_put_contents($tmp_file, $data)) {
153 array_push($log, "Unable to save download.");
154 $stop = true; break;
155 }
156
157 $params["tmp_file"] = $tmp_file;
158
159 break;
160 case 3:
161 $tmp_file = $params["tmp_file"];
162 $target_version = $params["target_version"];
163
164 if (!chdir($parent_dir)) {
165 array_push($log, "Unable to change into parent directory.");
166 $stop = true; break;
167 }
168
169 $old_dir = tmpdirname($parent_dir, "tt-rss-old");
170
171 array_push($log, "Renaming tt-rss directory to ".basename($old_dir));
172 if (!rename($work_dir, $old_dir)) {
173 array_push($log, "Unable to rename tt-rss directory.");
174 $stop = true; break;
175 }
176
177 array_push($log, "Extracting tarball...");
178 system("tar zxf $tmp_file", $system_rc);
179
180 if ($system_rc != 0) {
181 array_push($log, "Error while extracting tarball (RC=$system_rc).");
182 $stop = true; break;
183 }
184
185 $target_dir = "$parent_dir/tt-rss-$target_version";
186
187 array_push($log, "Renaming target directory...");
188 if (!rename($target_dir, $work_dir)) {
189 array_push($log, "Unable to rename target directory.");
190 $stop = true; break;
191 }
192
193 if (!chdir($work_dir)) {
194 array_push($log, "Unable to change to work directory: $work_dir");
195 $stop = true; break;
196 }
197
198 array_push($log, "Copying config.php...");
199 if (!copy("$old_dir/config.php", "$work_dir/config.php")) {
200 array_push($log, "Unable to copy config.php to $work_dir.");
201 $stop = true; break;
202 }
203
204 array_push($log, "Cleaning up...");
205 unlink($tmp_file);
206
207 array_push($log, "Fixing permissions...");
208
209 $directories = array(
210 CACHE_DIR,
73f28fe9
AD
211 CACHE_DIR . "/export",
212 CACHE_DIR . "/images",
73f28fe9
AD
213 CACHE_DIR . "/simplepie",
214 ICONS_DIR,
215 LOCK_DIRECTORY);
216
217 foreach ($directories as $dir) {
218 array_push($log, "-> $dir");
219 chmod($dir, 0777);
220 }
221
222 array_push($log, "Upgrade completed.");
223 array_push($log, "Your old tt-rss directory is saved at $old_dir. ".
224 "Please migrate locally modified files (if any) and remove it.");
225 array_push($log, "You might need to re-enter current directory in shell to see new files.");
226
227 $stop = true;
228 break;
229 default:
230 $stop = true;
231 }
232
233 return array("step" => $step, "stop" => $stop, "params" => $params, "log" => $log);
234 }
235
236 function update_self_cli($link, $force = false) {
237 $step = 0;
238 $stop = false;
239 $params = array();
240
241 while (!$stop) {
242 $rc = $this->update_self_step($link, $step, $params, $force);
243
244 $params = $rc['params'];
245 $stop = $rc['stop'];
246
247 foreach ($rc['log'] as $line) {
248 _debug($line);
249 }
250 ++$step;
251 }
252 }
253
254 function update_self($args) {
255 _debug("Warning: self-updating is experimental. Use at your own risk.");
256 _debug("Please backup your tt-rss directory before continuing. Your database will not be modified.");
257 _debug("Type 'yes' to continue.");
258
259 if (read_stdin() != 'yes')
260 exit;
261
262 $this->update_self_cli($link, in_array("-force", $args));
5cedb389
AD
263 }
264
265 function get_prefs_js() {
266 return file_get_contents(dirname(__FILE__) . "/updater.js");
267 }
268
269 function hook_prefs_tab($args) {
270 if ($args != "prefPrefs") return;
271
272 if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) {
273 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Update Tiny Tiny RSS')."\">";
274
275 if ($_SESSION["pref_last_version_check"] + 86400 + rand(-1000, 1000) < time()) {
276 $_SESSION["version_data"] = @check_for_update($this->link);
277 $_SESSION["pref_last_version_check"] = time();
278 }
279
280 if (is_array($_SESSION["version_data"])) {
281 $version = $_SESSION["version_data"]["version"];
282 print_notice(T_sprintf("New version of Tiny Tiny RSS is available (%s).", "<b>$version</b>"));
283
284 print "<p><button dojoType=\"dijit.form.Button\" onclick=\"return updateSelf()\">".
285 __('Update Tiny Tiny RSS')."</button></p>";
286
287 } else {
288 print_notice(__("Your Tiny Tiny RSS installation is up to date."));
289 }
290
291 print "</div>"; #pane
292 }
293
294 function updateSelf() {
295 print "<form style='display : block' name='self_update_form' id='self_update_form'>";
296
297 print "<div class='error'>".__("Do not close this dialog until updating is finished. Backup your tt-rss directory before continuing.")."</div>";
298
299 print "<ul class='selfUpdateList' id='self_update_log'>";
300 print "<li>" . __("Ready to update.") . "</li>";
301 print "</ul>";
302
303 print "<div class='dlgButtons'>";
304 print "<button id=\"self_update_start_btn\" dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('updateSelfDlg').start()\" >".
305 __("Start update")."</button>";
306 print "<button id=\"self_update_stop_btn\" onclick=\"return dijit.byId('updateSelfDlg').close()\" dojoType=\"dijit.form.Button\">".
307 __("Close this window")."</button>";
308 print "</div>";
309 print "</form>";
310 }
311
312 function performUpdate() {
313 $step = (int) $_REQUEST["step"];
314 $params = json_decode($_REQUEST["params"], true);
315 $force = (bool) $_REQUEST["force"];
316
317 if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) {
73f28fe9 318 print json_encode($this->update_self_step($this->link, $step, $params, $force));
5cedb389
AD
319 }
320 }
321
322
323 }
324}
325?>