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