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