5 * Tweeper - a Twitter to RSS web scraper.
7 * Copyright (C) 2013-2020 Antonio Ospite <ao2@ao2.it>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 require_once 'autoload.php';
27 date_default_timezone_set('UTC');
30 * Check if the script is being run from the command line.
33 return (php_sapi_name() === "cli");
37 * Show the script usage.
39 function usage($argv) {
41 $usage = "{$argv[0]} [-e|-m <0|1>|-u <0|1>|-v <0|1>|-h|--help] <src_url>\n";
44 $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>&show_usernames=<0|1>&show_multimedia=<0|1>&verbose_output=<0|1>");
47 return "usage: $usage";
51 * Parse command line options.
53 function parse_options_cli($argv, $argc) {
55 'generate_enclosure' => FALSE,
56 'show_usernames' => TRUE,
57 'show_multimedia' => TRUE,
58 'verbose_output' => TRUE,
65 $cli_options = getopt("em:u:v:h", array("help"));
66 foreach ($cli_options as $opt => $val) {
69 $options['generate_enclosure'] = TRUE;
73 $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
75 fwrite(STDERR, "Invalid argument for the -m option.\n");
76 fwrite(STDERR, usage($argv));
79 $options['show_multimedia'] = $val;
83 $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
85 fwrite(STDERR, "Invalid argument for the -u option.\n");
86 fwrite(STDERR, usage($argv));
89 $options['show_usernames'] = $val;
93 $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
95 fwrite(STDERR, "Invalid argument for the -v option.\n");
96 fwrite(STDERR, usage($argv));
99 $options['verbose_output'] = $val;
108 fwrite(STDERR, usage($argv));
113 // For now assume that the URL is the lest argument, in the future we could
114 // switch to PHP >= 7.1 and use the $optind argument of getopt().
115 $options['src_url'] = array_pop($argv);
121 * Parse options passed from a query string.
123 function parse_options_query_string() {
125 'generate_enclosure' => FALSE,
126 'show_usernames' => TRUE,
127 'show_multimedia' => TRUE,
128 'verbose_output' => TRUE,
131 if (isset($_GET['src_url'])) {
132 $options['src_url'] = $_GET['src_url'];
135 if (isset($_GET['generate_enclosure'])) {
136 $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
139 if (isset($_GET['show_multimedia'])) {
140 $options['show_multimedia'] = $_GET['show_multimedia'] != 0;
143 if (isset($_GET['show_usernames'])) {
144 $options['show_usernames'] = $_GET['show_usernames'] != 0;
147 if (isset($_GET['verbose_output'])) {
148 $options['verbose_output'] = $_GET['verbose_output'] != 0;
155 $options = parse_options_cli($argv, $argc);
156 $error_stream = fopen('php://stderr', 'w');
159 $options = parse_options_query_string();
160 $error_stream = fopen('php://output', 'w');
163 if (!isset($options['src_url'])) {
164 fwrite($error_stream, usage(is_cli() ? $argv : NULL));
168 $tweeper = new Tweeper($options['generate_enclosure'], $options['show_usernames'], $options['show_multimedia'], $options['verbose_output']);
169 $output = $tweeper->tweep($options['src_url']);
170 if (is_null($output)) {