]> git.wh0rd.org - tt-rss.git/blob - include/update_self.php
cb3482ab05ecfa1976211629cb142abf8b1d7000
[tt-rss.git] / include / update_self.php
1 <?php
2 function update_self_step($link, $step, $params, $force = false) {
3 // __FILE__ is in include/ so we need to go one level up
4 $work_dir = dirname(dirname(__FILE__));
5 $parent_dir = dirname($work_dir);
6
7 if (!chdir($work_dir)) {
8 array_push($log, "Unable to change to work directory: $work_dir");
9 $stop = true; break;
10 }
11
12 $stop = false;
13 $log = array();
14 if (!is_array($params)) $params = array();
15
16 switch ($step) {
17 case 0:
18 array_push($log, "Work directory: $work_dir");
19
20 if (!is_writable($work_dir) && !is_writable("$parent_dir")) {
21 $user = posix_getpwuid(posix_geteuid());
22 $user = $user["name"];
23 array_push($log, "Both tt-rss and parent directories should be writable as current user ($user).");
24 $stop = true; break;
25 }
26
27 if (!file_exists("$work_dir/config.php") || !file_exists("$work_dir/include/sanity_check.php")) {
28 array_push($log, "Work directory $work_dir doesn't look like tt-rss installation.");
29 $stop = true; break;
30 }
31
32 if (!is_writable(sys_get_temp_dir())) {
33 array_push($log, "System temporary directory should be writable as current user.");
34 $stop = true; break;
35 }
36
37 array_push($log, "Checking for tar...");
38
39 $system_rc = 0;
40 system("tar --version >/dev/null", $system_rc);
41
42 if ($system_rc != 0) {
43 array_push($log, "Could not run tar executable (RC=$system_rc).");
44 $stop = true; break;
45 }
46
47 array_push($log, "Checking for latest version...");
48
49 $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"),
50 true);
51
52 if (!is_array($version_info)) {
53 array_push($log, "Unable to fetch version information.");
54 $stop = true; break;
55 }
56
57 $target_version = $version_info["version"];
58 $target_dir = "$parent_dir/tt-rss-$target_version";
59
60 array_push($log, "Target version: $target_version");
61 $params["target_version"] = $target_version;
62
63 if (version_compare(VERSION, $target_version) != -1 && !$force) {
64 array_push($log, "Your Tiny Tiny RSS installation is up to date.");
65 $stop = true; break;
66 }
67
68 if (file_exists($target_dir)) {
69 array_push($log, "Target directory $target_dir already exists.");
70 $stop = true; break;
71 }
72
73 break;
74 case 1:
75 $target_version = $params["target_version"];
76
77 array_push($log, "Downloading checksums...");
78 $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt");
79
80 if (!$md5sum_data) {
81 array_push($log, "Could not download checksums.");
82 $stop = true; break;
83 }
84
85 $md5sum_data = explode("\n", $md5sum_data);
86
87 foreach ($md5sum_data as $line) {
88 $pair = explode(" ", $line);
89
90 if ($pair[1] == "tt-rss-$target_version.tar.gz") {
91 $target_md5sum = $pair[0];
92 break;
93 }
94 }
95
96 if (!$target_md5sum) {
97 array_push($log, "Unable to locate checksum for target version.");
98 $stop = true; break;
99 }
100
101 $params["target_md5sum"] = $target_md5sum;
102
103 break;
104 case 2:
105 $target_version = $params["target_version"];
106 $target_md5sum = $params["target_md5sum"];
107
108 array_push($log, "Downloading distribution tarball...");
109
110 $tarball_url = "http://tt-rss.org/download/tt-rss-$target_version.tar.gz";
111 $data = fetch_file_contents($tarball_url);
112
113 if (!$data) {
114 array_push($log, "Could not download distribution tarball ($tarball_url).");
115 $stop = true; break;
116 }
117
118 array_push($log, "Verifying tarball checksum...");
119
120 $test_md5sum = md5($data);
121
122 if ($test_md5sum != $target_md5sum) {
123 array_push($log, "Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum).");
124 $stop = true; break;
125 }
126
127 $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss');
128 array_push($log, "Saving download to $tmp_file");
129
130 if (!file_put_contents($tmp_file, $data)) {
131 array_push($log, "Unable to save download.");
132 $stop = true; break;
133 }
134
135 $params["tmp_file"] = $tmp_file;
136
137 break;
138 case 3:
139 $tmp_file = $params["tmp_file"];
140 $target_version = $params["target_version"];
141
142 if (!chdir($parent_dir)) {
143 array_push($log, "Unable to change into parent directory.");
144 $stop = true; break;
145 }
146
147 $old_dir = tmpdirname($parent_dir, "tt-rss-old");
148
149 array_push($log, "Renaming tt-rss directory to ".basename($old_dir));
150 if (!rename($work_dir, $old_dir)) {
151 array_push($log, "Unable to rename tt-rss directory.");
152 $stop = true; break;
153 }
154
155 array_push($log, "Extracting tarball...");
156 system("tar zxf $tmp_file", $system_rc);
157
158 if ($system_rc != 0) {
159 array_push($log, "Error while extracting tarball (RC=$system_rc).");
160 $stop = true; break;
161 }
162
163 $target_dir = "$parent_dir/tt-rss-$target_version";
164
165 array_push($log, "Renaming target directory...");
166 if (!rename($target_dir, $work_dir)) {
167 array_push($log, "Unable to rename target directory.");
168 $stop = true; break;
169 }
170
171 if (!chdir($work_dir)) {
172 array_push($log, "Unable to change to work directory: $work_dir");
173 $stop = true; break;
174 }
175
176 array_push($log, "Copying config.php...");
177 if (!copy("$old_dir/config.php", "$work_dir/config.php")) {
178 array_push($log, "Unable to copy config.php to $work_dir.");
179 $stop = true; break;
180 }
181
182 array_push($log, "Cleaning up...");
183 unlink($tmp_file);
184
185 array_push($log, "Fixing permissions...");
186
187 $directories = array(
188 CACHE_DIR,
189 CACHE_DIR . "/htmlpurifier",
190 CACHE_DIR . "/export",
191 CACHE_DIR . "/images",
192 CACHE_DIR . "/magpie",
193 CACHE_DIR . "/simplepie",
194 ICONS_DIR,
195 LOCK_DIRECTORY);
196
197 foreach ($directories as $dir) {
198 array_push($log, "-> $dir");
199 chmod($dir, 0777);
200 }
201
202 array_push($log, "Upgrade completed.");
203 array_push($log, "Your old tt-rss directory is saved at $old_dir. ".
204 "Please migrate locally modified files (if any) and remove it.");
205 array_push($log, "You might need to re-enter current directory in shell to see new files.");
206
207 $stop = true;
208 break;
209 default:
210 $stop = true;
211 }
212
213 return array("step" => $step, "stop" => $stop, "params" => $params, "log" => $log);
214 }
215
216
217 function update_self($link, $force = false) {
218 $step = 0;
219 $stop = false;
220 $params = array();
221
222 while (!$stop) {
223 $rc = update_self_step($link, $step, $params, $force);
224
225 $params = $rc['params'];
226 $stop = $rc['stop'];
227
228 foreach ($rc['log'] as $line) {
229 _debug($line);
230 }
231 ++$step;
232 }
233 }
234 ?>