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