]>
Commit | Line | Data |
---|---|---|
5cedb389 AD |
1 | <?php |
2 | class Updater extends Plugin { | |
3 | ||
5cedb389 AD |
4 | private $host; |
5 | ||
d2a421e3 | 6 | function about() { |
7a866114 AD |
7 | return array(1.0, |
8 | "Updates tt-rss installation to latest version.", | |
de612e7a AD |
9 | "fox", |
10 | true); | |
7a866114 AD |
11 | } |
12 | ||
d2a421e3 | 13 | function init($host) { |
5cedb389 AD |
14 | $this->host = $host; |
15 | ||
16 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
73f28fe9 AD |
17 | |
18 | $host->add_command("update-self", | |
19 | "update tt-rss installation to latest version", | |
20 | $this); | |
21 | } | |
22 | ||
a42c55f0 | 23 | function update_self_step($step, $params, $force = false) { |
73f28fe9 AD |
24 | // __FILE__ is in plugins/updater so we need to go one level up |
25 | $work_dir = dirname(dirname(dirname(__FILE__))); | |
26 | $parent_dir = dirname($work_dir); | |
b509b059 | 27 | // Set PATH to run "which" |
28 | putenv('PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"'); | |
73f28fe9 | 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 | |
cd4d71b4 | 45 | if (!is_writable($work_dir) || !is_writable("$parent_dir")) { |
08da1c6a AD |
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 | |
49dc0280 AD |
62 | // bah, also humbug |
63 | putenv("PATH=" . getenv("PATH") . PATH_SEPARATOR . "/bin" . | |
64 | PATH_SEPARATOR . "/usr/bin"); | |
65 | ||
08da1c6a | 66 | array_push($log, "Checking for tar..."); |
73f28fe9 | 67 | |
08da1c6a AD |
68 | $system_rc = 0; |
69 | system("which tar >/dev/null", $system_rc); | |
73f28fe9 | 70 | |
08da1c6a AD |
71 | if ($system_rc != 0) { |
72 | array_push($log, "Could not run tar executable (RC=$system_rc)."); | |
73 | $stop = true; break; | |
74 | } | |
73f28fe9 | 75 | |
08da1c6a | 76 | array_push($log, "Checking for gunzip..."); |
04486904 | 77 | |
08da1c6a AD |
78 | $system_rc = 0; |
79 | system("which gunzip >/dev/null", $system_rc); | |
04486904 | 80 | |
08da1c6a AD |
81 | if ($system_rc != 0) { |
82 | array_push($log, "Could not run gunzip executable (RC=$system_rc)."); | |
83 | $stop = true; break; | |
84 | } | |
04486904 | 85 | |
08da1c6a | 86 | array_push($log, "Checking for latest version..."); |
73f28fe9 | 87 | |
08da1c6a AD |
88 | $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"), |
89 | true); | |
73f28fe9 | 90 | |
08da1c6a AD |
91 | if (!is_array($version_info)) { |
92 | array_push($log, "Unable to fetch version information."); | |
93 | $stop = true; break; | |
94 | } | |
73f28fe9 | 95 | |
08da1c6a | 96 | $target_version = $version_info["version"]; |
0ac2f52e | 97 | $target_dir = "$parent_dir/Tiny-Tiny-RSS-$target_version"; |
73f28fe9 | 98 | |
08da1c6a AD |
99 | array_push($log, "Target version: $target_version"); |
100 | $params["target_version"] = $target_version; | |
73f28fe9 | 101 | |
08da1c6a AD |
102 | if (version_compare(VERSION, $target_version) != -1 && !$force) { |
103 | array_push($log, "Your Tiny Tiny RSS installation is up to date."); | |
104 | $stop = true; break; | |
105 | } | |
73f28fe9 | 106 | |
08da1c6a AD |
107 | if (file_exists($target_dir)) { |
108 | array_push($log, "Target directory $target_dir already exists."); | |
109 | $stop = true; break; | |
110 | } | |
73f28fe9 | 111 | |
08da1c6a AD |
112 | break; |
113 | case 1: | |
114 | $target_version = $params["target_version"]; | |
73f28fe9 | 115 | |
0ac2f52e | 116 | /* array_push($log, "Downloading checksums..."); |
08da1c6a | 117 | $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt"); |
73f28fe9 | 118 | |
08da1c6a AD |
119 | if (!$md5sum_data) { |
120 | array_push($log, "Could not download checksums."); | |
121 | $stop = true; break; | |
122 | } | |
73f28fe9 | 123 | |
08da1c6a | 124 | $md5sum_data = explode("\n", $md5sum_data); |
73f28fe9 | 125 | |
08da1c6a AD |
126 | foreach ($md5sum_data as $line) { |
127 | $pair = explode(" ", $line); | |
73f28fe9 | 128 | |
08da1c6a AD |
129 | if ($pair[1] == "tt-rss-$target_version.tar.gz") { |
130 | $target_md5sum = $pair[0]; | |
131 | break; | |
132 | } | |
73f28fe9 | 133 | } |
73f28fe9 | 134 | |
08da1c6a AD |
135 | if (!$target_md5sum) { |
136 | array_push($log, "Unable to locate checksum for target version."); | |
137 | $stop = true; break; | |
138 | } | |
73f28fe9 | 139 | |
0ac2f52e AD |
140 | $params["target_md5sum"] = $target_md5sum; */ |
141 | ||
142 | array_push($log, "Proceeding to download..."); | |
73f28fe9 | 143 | |
08da1c6a AD |
144 | break; |
145 | case 2: | |
146 | $target_version = $params["target_version"]; | |
0ac2f52e | 147 | // $target_md5sum = $params["target_md5sum"]; |
73f28fe9 | 148 | |
08da1c6a | 149 | array_push($log, "Downloading distribution tarball..."); |
73f28fe9 | 150 | |
0ac2f52e | 151 | $tarball_url = "https://github.com/gothfox/Tiny-Tiny-RSS/archive/$target_version.tar.gz"; |
08da1c6a | 152 | $data = fetch_file_contents($tarball_url); |
73f28fe9 | 153 | |
08da1c6a AD |
154 | if (!$data) { |
155 | array_push($log, "Could not download distribution tarball ($tarball_url)."); | |
156 | $stop = true; break; | |
157 | } | |
73f28fe9 | 158 | |
0ac2f52e | 159 | /* array_push($log, "Verifying tarball checksum..."); |
73f28fe9 | 160 | |
08da1c6a | 161 | $test_md5sum = md5($data); |
73f28fe9 | 162 | |
08da1c6a AD |
163 | if ($test_md5sum != $target_md5sum) { |
164 | array_push($log, "Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum)."); | |
165 | $stop = true; break; | |
0ac2f52e | 166 | } */ |
73f28fe9 | 167 | |
08da1c6a AD |
168 | $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss'); |
169 | array_push($log, "Saving download to $tmp_file"); | |
73f28fe9 | 170 | |
08da1c6a AD |
171 | if (!file_put_contents($tmp_file, $data)) { |
172 | array_push($log, "Unable to save download."); | |
173 | $stop = true; break; | |
174 | } | |
73f28fe9 | 175 | |
08da1c6a | 176 | $params["tmp_file"] = $tmp_file; |
73f28fe9 | 177 | |
08da1c6a AD |
178 | break; |
179 | case 3: | |
180 | $tmp_file = $params["tmp_file"]; | |
181 | $target_version = $params["target_version"]; | |
73f28fe9 | 182 | |
08da1c6a AD |
183 | if (!chdir($parent_dir)) { |
184 | array_push($log, "Unable to change into parent directory."); | |
185 | $stop = true; break; | |
186 | } | |
73f28fe9 | 187 | |
08da1c6a AD |
188 | array_push($log, "Extracting tarball..."); |
189 | system("tar zxf $tmp_file", $system_rc); | |
73f28fe9 | 190 | |
08da1c6a AD |
191 | if ($system_rc != 0) { |
192 | array_push($log, "Error while extracting tarball (RC=$system_rc)."); | |
193 | $stop = true; break; | |
194 | } | |
73f28fe9 | 195 | |
0ac2f52e AD |
196 | $target_dir = "$parent_dir/Tiny-Tiny-RSS-$target_version"; |
197 | ||
198 | if (!is_dir($target_dir)) { | |
199 | array_push($log, "Target directory ($target_dir) not found."); | |
200 | $stop = true; break; | |
201 | } | |
202 | ||
203 | $old_dir = tmpdirname($parent_dir, "tt-rss-old"); | |
204 | ||
205 | array_push($log, "Renaming tt-rss directory to ".basename($old_dir)); | |
206 | if (!rename($work_dir, $old_dir)) { | |
207 | array_push($log, "Unable to rename tt-rss directory."); | |
208 | $stop = true; break; | |
209 | } | |
73f28fe9 | 210 | |
08da1c6a AD |
211 | array_push($log, "Renaming target directory..."); |
212 | if (!rename($target_dir, $work_dir)) { | |
213 | array_push($log, "Unable to rename target directory."); | |
214 | $stop = true; break; | |
215 | } | |
73f28fe9 | 216 | |
08da1c6a AD |
217 | if (!chdir($work_dir)) { |
218 | array_push($log, "Unable to change to work directory: $work_dir"); | |
219 | $stop = true; break; | |
220 | } | |
73f28fe9 | 221 | |
08da1c6a AD |
222 | array_push($log, "Copying config.php..."); |
223 | if (!copy("$old_dir/config.php", "$work_dir/config.php")) { | |
224 | array_push($log, "Unable to copy config.php to $work_dir."); | |
225 | $stop = true; break; | |
226 | } | |
73f28fe9 | 227 | |
08da1c6a AD |
228 | array_push($log, "Cleaning up..."); |
229 | unlink($tmp_file); | |
73f28fe9 | 230 | |
08da1c6a | 231 | array_push($log, "Fixing permissions..."); |
73f28fe9 | 232 | |
08da1c6a AD |
233 | $directories = array( |
234 | CACHE_DIR, | |
235 | CACHE_DIR . "/export", | |
236 | CACHE_DIR . "/images", | |
897589a5 | 237 | CACHE_DIR . "/js", |
08da1c6a AD |
238 | CACHE_DIR . "/simplepie", |
239 | ICONS_DIR, | |
240 | LOCK_DIRECTORY); | |
73f28fe9 | 241 | |
08da1c6a AD |
242 | foreach ($directories as $dir) { |
243 | array_push($log, "-> $dir"); | |
244 | chmod($dir, 0777); | |
245 | } | |
73f28fe9 | 246 | |
aff02f89 AD |
247 | if (ICONS_DIR == "feed-icons") { |
248 | array_push($log, "Migrating feed icons..."); | |
249 | ||
250 | $icons = glob("$old_dir/feed-icons/*.ico"); | |
251 | $icons_copied = 0; | |
252 | ||
253 | foreach ($icons as $icon) { | |
254 | $icon = basename($icon); | |
255 | ||
256 | if (copy("$old_dir/feed-icons/$icon", "$work_dir/feed-icons/$icon")) { | |
257 | ++$icons_copied; | |
258 | } | |
259 | } | |
260 | ||
261 | array_push($log, "Done; $icons_copied files copied"); | |
262 | ||
263 | } else { | |
264 | array_push($log, "Not migrating feed icons, ICONS_DIR modified."); | |
265 | } | |
266 | ||
08da1c6a AD |
267 | array_push($log, "Upgrade completed."); |
268 | array_push($log, "Your old tt-rss directory is saved at $old_dir. ". | |
269 | "Please migrate locally modified files (if any) and remove it."); | |
270 | array_push($log, "You might need to re-enter current directory in shell to see new files."); | |
73f28fe9 | 271 | |
08da1c6a AD |
272 | $stop = true; |
273 | break; | |
274 | default: | |
275 | $stop = true; | |
276 | } | |
73f28fe9 AD |
277 | } |
278 | ||
279 | return array("step" => $step, "stop" => $stop, "params" => $params, "log" => $log); | |
280 | } | |
281 | ||
a42c55f0 | 282 | function update_self_cli($force = false) { |
73f28fe9 AD |
283 | $step = 0; |
284 | $stop = false; | |
285 | $params = array(); | |
286 | ||
287 | while (!$stop) { | |
a42c55f0 | 288 | $rc = $this->update_self_step($step, $params, $force); |
73f28fe9 AD |
289 | |
290 | $params = $rc['params']; | |
291 | $stop = $rc['stop']; | |
292 | ||
293 | foreach ($rc['log'] as $line) { | |
294 | _debug($line); | |
295 | } | |
296 | ++$step; | |
297 | } | |
298 | } | |
299 | ||
300 | function update_self($args) { | |
0bcf69e1 AD |
301 | _debug("READ THE FOLLOWING BEFORE CONTINUING!"); |
302 | _debug("* It is suggested to backup your tt-rss directory first."); | |
303 | _debug("* Your database will not be modified."); | |
304 | _debug("* Your current tt-rss installation directory will not be modified. It will be renamed and left in the parent directory. You will be able to migrate all your customized files after update finishes."); | |
73f28fe9 AD |
305 | _debug("Type 'yes' to continue."); |
306 | ||
aff02f89 AD |
307 | $input = read_stdin(); |
308 | ||
309 | if ($input != 'yes' && $input != 'force') | |
73f28fe9 AD |
310 | exit; |
311 | ||
a42c55f0 | 312 | $this->update_self_cli($input == 'force'); |
5cedb389 AD |
313 | } |
314 | ||
315 | function get_prefs_js() { | |
316 | return file_get_contents(dirname(__FILE__) . "/updater.js"); | |
317 | } | |
318 | ||
319 | function hook_prefs_tab($args) { | |
320 | if ($args != "prefPrefs") return; | |
321 | ||
322 | if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) { | |
323 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Update Tiny Tiny RSS')."\">"; | |
324 | ||
325 | if ($_SESSION["pref_last_version_check"] + 86400 + rand(-1000, 1000) < time()) { | |
6322ac79 | 326 | $_SESSION["version_data"] = @check_for_update(); |
5cedb389 AD |
327 | $_SESSION["pref_last_version_check"] = time(); |
328 | } | |
329 | ||
330 | if (is_array($_SESSION["version_data"])) { | |
331 | $version = $_SESSION["version_data"]["version"]; | |
2e828a4a | 332 | $version_id = $_SESSION["version_data"]["version_id"]; |
5cedb389 AD |
333 | print_notice(T_sprintf("New version of Tiny Tiny RSS is available (%s).", "<b>$version</b>")); |
334 | ||
2e828a4a AD |
335 | $details = "http://tt-rss.org/redmine/versions/$version_id"; |
336 | ||
337 | print "<p><button onclick=\"window.open('$details')\" dojoType=\"dijit.form.Button\">".__("See the release notes")."</button>"; | |
338 | ||
339 | print " <button dojoType=\"dijit.form.Button\" onclick=\"return updateSelf()\">". | |
5cedb389 AD |
340 | __('Update Tiny Tiny RSS')."</button></p>"; |
341 | ||
342 | } else { | |
343 | print_notice(__("Your Tiny Tiny RSS installation is up to date.")); | |
344 | } | |
345 | ||
346 | print "</div>"; #pane | |
347 | } | |
a439ac8a | 348 | } |
5cedb389 AD |
349 | |
350 | function updateSelf() { | |
0bcf69e1 AD |
351 | print_warning(__("Do not close this dialog until updating is finished.")); |
352 | ||
5cedb389 AD |
353 | print "<form style='display : block' name='self_update_form' id='self_update_form'>"; |
354 | ||
0bcf69e1 AD |
355 | print "<style type='text/css'> |
356 | li.notice { font-style : italic; color : red; } | |
357 | </style>"; | |
5cedb389 AD |
358 | |
359 | print "<ul class='selfUpdateList' id='self_update_log'>"; | |
0bcf69e1 AD |
360 | print "<li class='notice'>" .__("It is suggested to backup your tt-rss directory first.") . "</li>"; |
361 | print "<li class='notice'>" . __("Your database will not be modified.") . "</li>"; | |
362 | print "<li class='notice'>" . __("Your current tt-rss installation directory will not be modified. It will be renamed and left in the parent directory. You will be able to migrate all your customized files after update finishes.") . "</li>"; | |
5cedb389 AD |
363 | print "<li>" . __("Ready to update.") . "</li>"; |
364 | print "</ul>"; | |
365 | ||
366 | print "<div class='dlgButtons'>"; | |
367 | print "<button id=\"self_update_start_btn\" dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('updateSelfDlg').start()\" >". | |
368 | __("Start update")."</button>"; | |
369 | print "<button id=\"self_update_stop_btn\" onclick=\"return dijit.byId('updateSelfDlg').close()\" dojoType=\"dijit.form.Button\">". | |
370 | __("Close this window")."</button>"; | |
371 | print "</div>"; | |
372 | print "</form>"; | |
373 | } | |
374 | ||
375 | function performUpdate() { | |
376 | $step = (int) $_REQUEST["step"]; | |
377 | $params = json_decode($_REQUEST["params"], true); | |
378 | $force = (bool) $_REQUEST["force"]; | |
379 | ||
380 | if (($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) && CHECK_FOR_NEW_VERSION) { | |
a42c55f0 | 381 | print json_encode($this->update_self_step($step, $params, $force)); |
5cedb389 AD |
382 | } |
383 | } | |
384 | ||
106a3de9 AD |
385 | function api_version() { |
386 | return 2; | |
387 | } | |
388 | ||
5cedb389 AD |
389 | } |
390 | ?> |