forked from lairdshaw/fups
/
run.php
222 lines (205 loc) · 8.33 KB
/
run.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
* FUPS: Forum user-post scraper. An extensible PHP framework for scraping and
* outputting the posts of a specified user from a specified forum/board
* running supported forum software. Can be run as either a web app or a
* commandline script.
*
* Copyright (C) 2013-2014 Laird Shaw.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* File : run.php.
* Description: Starts a background fups.php process based on the options
* entered by the user on the previous page (enter-options.php)
* and displays a status page to the user, auto-updating via
* AJAX if supported, otherwise refreshing the entire page
* at an interval configured by FUPS_META_REDIRECT_DELAY in
* settings.php.
* Part of the web app functionality of FUPS.
*/
require_once __DIR__.'/common.php';
$err = false;
$file_errs = '';
if (!isset($_GET['token'])) {
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
exec('ps -e | grep php | wc -l', $output, $res);
$num_php_processes = $output[0];
if ($num_php_processes > FUPS_MAX_PHP_PROCESSES) {
$err = 'Apologies, my web hosting account is currently running short of allowable tasks; please wait a little and then try again. Feel free to <a href="'.FUPS_CONTACT_URL.'">contact me</a> if this situation persists over an extended period. Thank you!';
}
}
if (!$err) {
$num_settings_set = 0;
foreach (array_keys($_POST) as $setting) {
if ($_POST[$setting]) $num_settings_set++;
}
if ($num_settings_set == 0) {
$err = 'You do not seem to have entered any values on the settings form. Please click "Back" and try again.';
} else {
$fp = false;
for ($i = 0; $i < FUPS_MAX_TOKEN_ATTEMPTS; $i++) {
$token = md5(rand(0, 1000000000));
$settings_filename = make_settings_filename($token);
$fp = @fopen($settings_filename, "x");
if ($fp !== false) break;
}
if ($fp === false) {
$err = 'Apologies, the server encountered a technical error: it was unable to generate a unique token to be associated with your request after '.FUPS_MAX_TOKEN_ATTEMPTS.' attempts. You might like to try again or <a href="'.FUPS_CONTACT_URL.'">contact me</a> about this error.';
} else {
foreach (array_keys($_POST) as $setting) {
$value = $_POST[$setting];
fwrite($fp, $setting.'='.$value.PHP_EOL);
}
fclose($fp);
$status_filename = make_status_filename($token);
$errs_filename = make_errs_filename($token);
$serialize_filename = make_serialize_filename($token);
if (file_put_contents($status_filename , 'Starting up.') === false) {
if ($file_errs) $file_errs .= ' ';
$file_errs .= 'Error: unable to write to the status file.';
}
if (file_put_contents($errs_filename , '') === false) {
if ($file_errs) $file_errs .= ' ';
$file_errs .= 'Error: unable to write to the error file.';
}
if (file_put_contents($serialize_filename, '') === false) {
if ($file_errs) $file_errs .= ' ';
$file_errs .= 'Error: unable to write to the serialization file.';
}
$cmd = make_php_exec_cmd(array('token' => $token));
if (!try_run_bg_proc($cmd)) {
$err = 'Apologies, the server encountered a technical error: it was unable to initiate the background process to perform the task of scraping, sorting and finally presenting your posts. The command used was:<br />'.PHP_EOL.'<br />'.PHP_EOL.$cmd.'<br />'.PHP_EOL.'<br />'.PHP_EOL.'You might like to try again or <a href="'.FUPS_CONTACT_URL.'">contact me</a> about this error.';
}
}
}
}
} else {
$token = $_GET['token'];
if (validate_token($token, $err)) {
$status_filename = make_status_filename ($token);
$errs_filename = make_errs_filename ($token);
$errs_admin_filename = make_errs_admin_filename($token);
}
}
if (!$err) {
$ts = @filemtime($status_filename);
if ($ts === false) {
$err = 'The status file for your FUPS process with token "'.$token.'" does not exist - possibly because you have already deleted it.';
}
$status = @file_get_contents($status_filename);
$errs = @file_get_contents($errs_filename );
$errs_admin = @file_get_contents($errs_admin_filename);
}
$head_extra = '';
if (!$err) {
global $fups_url_run, $fups_url_homepage;
get_failed_done_cancelled($status, $done, $cancelled, $failed);
if (!isset($_GET['ajax']) && ((!isset($_GET['last_status']) || $status != $_GET['last_status']) && !$done && !$failed && !$err)) {
$head_extra = '<meta http-equiv="refresh" content="'.FUPS_META_REDIRECT_DELAY.'; URL='.$fups_url_run.'?token='.htmlspecialchars(urlencode($token)).'&last_status='.htmlspecialchars(urlencode($status)).'" />';
}
}
$page = substr(__FILE__, strlen(FUPS_INC_ROOT));
fups_output_page_start($page, 'FUPS progress', 'Monitor the progress of the scraping script.', $head_extra);
?>
<ul class="fups_listmin">
<li><a href="<?php echo $fups_url_homepage; ?>"><< Back to the FUPS homepage</a></li>
</ul>
<h2>FUPS progress</h2>
<?php
if ($file_errs) {
?>
<div class="fups_error"><?php echo 'Apologies, however the following non-fatal error(s) occurred. These may affect updating and/or your final output: '.$file_errs; ?></div>
<?php
}
if ($err) {
?>
<div class="fups_error"><?php echo $err; ?></div>
<?php
} else {
?>
<script type="text/javascript">
//<![CDATA[
function toggle_ext_errs() {
var elem = document.getElementById('id_ext_err');
if (elem) {
elem.style.display = (elem.style.display == 'none' ? 'block' : 'none');
}
}
//]]>
</script>
<?php
if (isset($_GET['ajax']) && !$done && !$cancelled && !$failed) {
global $fups_url_ajax_get_status;
?>
<div id="ajax.fill">
<?php
output_update_html($token, $status, $done, $cancelled, $failed, $err, $errs, $errs_admin, true);
?>
</div>
<script type="text/javascript">
//<![CDATA[
var xhr;
var filesize = 0;
var ts = <?php echo $ts; ?>;
if (!xhr) try {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
} catch (e) {
xhr = null;
}
if (xhr) {
function fups_xhr_state_change_function() {
try {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var p = xhr.responseText.indexOf("\n");
filesize = xhr.responseText.substr(0, p);
var p2 = xhr.responseText.indexOf("\n", p + 1);
ts = xhr.responseText.substr(p + 1, p2);
document.getElementById('ajax.fill').innerHTML = xhr.responseText.substr(p2 + 1);
var len = xhr.responseText.length;
var FUPS_FAILED_STR = '<?php echo FUPS_FAILED_STR; ?>';
var FUPS_DONE_STR = '<?php echo FUPS_DONE_STR; ?>';
var FUPS_CANCELLED_STR = '<?php echo FUPS_CANCELLED_STR; ?>';
var failed = (xhr.responseText.indexOf(FUPS_FAILED_STR) != -1);
var done = (xhr.responseText.indexOf(FUPS_DONE_STR) != -1);
var cancelled = (xhr.responseText.indexOf(FUPS_CANCELLED_STR) != -1);
if (!failed && !done && !cancelled) {
xhr.open('GET', base_url + '&filesize=' + filesize + '&ts=' + ts, true);
xhr.onreadystatechange = fups_xhr_state_change_function;
xhr.send(null);
}
}
}
} catch (e) { alert('Exception: ' + e); }
}
try {
var base_url = '<?php echo $fups_url_ajax_get_status; ?>?token=<?php echo $token; ?>';
xhr.open('GET', base_url + '&filesize=' + filesize + '&ts=' + ts, true);
xhr.onreadystatechange = fups_xhr_state_change_function;
xhr.send(null);
} catch (e) { alert('Exception(2): ' + e); }
}
//]]>
</script>
<?php
} else output_update_html($token, $status, $done, $cancelled, $failed, $err, $errs, $errs_admin);
}
fups_output_page_end($page);
?>