PageRenderTime 20ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/run.php

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