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