PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/fups.php

https://gitlab.com/ezeql/fups
PHP | 147 lines | 101 code | 8 blank | 38 comment | 24 complexity | 2c167186adf370cf457b3b1e6d8f7bae 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 : fups.php.
  25. * Description: The main scraping script, initiated either automatically by
  26. * the web app (run.php) or manually e.g. via the commandline.
  27. * Manual initiation should be as follows:
  28. *
  29. * php path/to/fups.php -i path/to/existing/optionsfile.txt -o path/to/desired/outputfile.html
  30. *
  31. * The optionsfile.txt file should contain a series of lines of
  32. * supported options, each option followed by an equals sign
  33. * and then its value. The first option should be "forum_type",
  34. * which should be set to one of (at time of writing) "phpbb" or
  35. * "xenforo". Further options can be determined by consulting the
  36. * $required_settings and $optional_settings arrays in each of
  37. * classes/CphpBB.php and classes/CXenForo.php.
  38. */
  39. if (php_sapi_name() != 'cli') {
  40. echo 'This script can only be run from the commandline. It appears that it was NOT run from the commandline in this instance, so it is now exiting.';
  41. exit(1);
  42. }
  43. require_once __DIR__.'/common.php';
  44. require_once __DIR__.'/classes/CFUPSBase.php';
  45. # Parse and validate commandline arguments, exiting on error.
  46. if (!isset($argv[1])) {
  47. FUPSBase::exit_err_s('Fatal error: No commandline arguments supplied.'."\n", __FILE__, __METHOD__, __LINE__);
  48. } else {
  49. $chained = false;
  50. $web_initiated = null;
  51. $settings_filename = false;
  52. $output_dirname = false;
  53. $quiet = false;
  54. static $errmsg_mixed_cmdline_args = 'Fatal error: web-initiated (-t) and commandline (-i, -o and -q) arguments specified simultaneously.';
  55. $i = 1;
  56. while ($i < $argc) {
  57. switch ($argv[$i]) {
  58. case '-i':
  59. if ($web_initiated === true) {
  60. FUPSBase::exit_err_s($errmsg_mixed_cmdline_args, __FILE__, __METHOD__, __LINE__);
  61. }
  62. $web_initiated = false;
  63. if ($argc < $i + 1) {
  64. FUPSBase::exit_err_s('Fatal error: no input file specified after "-i" in commandline arguments.', __FILE__, __METHOD__, __LINE__);
  65. } else $settings_filename = $argv[$i + 1];
  66. $i += 2;
  67. break;
  68. case '-o':
  69. if ($web_initiated === true) {
  70. FUPSBase::exit_err_s($errmsg_mixed_cmdline_args, __FILE__, __METHOD__, __LINE__);
  71. }
  72. $web_initiated = false;
  73. if ($argc < $i + 1) {
  74. FUPSBase::exit_err_s('Fatal error: no output directory specified after "-o" in commandline arguments.', __FILE__, __METHOD__, __LINE__);
  75. } else $output_dirname = $argv[$i + 1];
  76. $i += 2;
  77. break;
  78. case '-q':
  79. if ($web_initiated === true) {
  80. FUPSBase::exit_err_s($errmsg_mixed_cmdline_args, __FILE__, __METHOD__, __LINE__);
  81. }
  82. $web_initiated = false;
  83. $quiet = true;
  84. $i++;
  85. break;
  86. case '-t':
  87. if ($web_initiated === false) {
  88. FUPSBase::exit_err_s($errmsg_mixed_cmdline_args, __FILE__, __METHOD__, __LINE__);
  89. }
  90. $web_initiated = true;
  91. if ($argc < $i + 1) {
  92. FUPSBase::exit_err_s('Fatal error: no token specified after "-t" in commandline arguments.', __FILE__, __METHOD__, __LINE__);
  93. } else $token = $argv[$i + 1];
  94. $i += 2;
  95. break;
  96. case '-c':
  97. $chained = true;
  98. $i++;
  99. break;
  100. default:
  101. FUPSBase::exit_err_s('Fatal error: unknown commandline argument specified: "'.$argv[$i].'".', __FILE__, __METHOD__, __LINE__);
  102. break;
  103. }
  104. }
  105. if ($web_initiated) {
  106. $settings_filename = make_settings_filename($token);
  107. } else if ($web_initiated === false) {
  108. if (!$settings_filename || !$output_dirname) {
  109. FUPSBase::exit_err_s('Fatal error: no '.(!$settings_filename ? 'settings' : 'output').' filename specified in commandline arguments.', __FILE__, __METHOD__, __LINE__);
  110. }
  111. } else {
  112. FUPSBase::exit_err_s('Fatal error: $web_initiated is uninitialised after parsing commandline arguments (this error should never occur, and indicates a bug).', __FILE__, __METHOD__, __LINE__);
  113. }
  114. }
  115. $forum_type = FUPSBase::read_forum_type_from_settings_file_s($settings_filename);
  116. $valid_forum_types = FUPSBase::get_valid_forum_types();
  117. if (!isset($valid_forum_types[$forum_type])) {
  118. FUPSBase::exit_err_s('Fatal error: missing or invalid forum_type in settings file "'.$settings_filename.'": "'.$forum_type.'".', __FILE__, __METHOD__, __LINE__);
  119. }
  120. $forum_type_caps = $valid_forum_types[$forum_type];
  121. require_once __DIR__.'/classes/C'.$forum_type_caps.'.php';
  122. if ($chained) {
  123. $token_or_settings_filename = $web_initiated ? $token : $settings_filename;
  124. $FUPS = unserialize(file_get_contents(make_serialize_filename($token_or_settings_filename)));
  125. } else {
  126. if ($web_initiated) {
  127. $params = array('token' => $token);
  128. } else {
  129. $params = array(
  130. 'settings_filename' => $settings_filename,
  131. 'output_dirname' => $output_dirname,
  132. 'quiet' => $quiet
  133. );
  134. }
  135. $class = $forum_type_caps.'FUPS';
  136. $FUPS = new $class($web_initiated, $params);
  137. }
  138. $FUPS->run();
  139. ?>