4 * Tweeper - a Twitter to RSS web scraper.
6 * Copyright (C) 2013-2018 Antonio Ospite <ao2@ao2.it>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 require_once 'autoload.php';
26 date_default_timezone_set('UTC');
29 * Check if the script is being run from the command line.
32 return (php_sapi_name() === "cli");
36 * Show the script usage.
38 function usage($argv) {
40 $usage = "{$argv[0]} [-e|-u <0|1>|-h|--help] <src_url>\n";
43 $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>&show_usernames=<0|1>");
46 return "usage: $usage";
50 * Parse command line options.
52 function parse_options_cli($argv, $argc) {
54 'generate_enclosure' => FALSE,
55 'show_usernames' => TRUE,
62 $cli_options = getopt("eu:h", array("help"));
63 foreach ($cli_options as $opt => $val) {
66 $options['generate_enclosure'] = TRUE;
70 $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
72 fwrite(STDERR, "Invalid argument for the -u option.\n");
73 fwrite(STDERR, usage($argv));
76 $options['show_usernames'] = $val;
85 fwrite(STDERR, usage($argv));
90 // For now assume that the URL is the lest argument, in the future we could
91 // switch to PHP >= 7.1 and use the $optind argument of getopt().
92 $options['src_url'] = array_pop($argv);
98 * Parse options passed from a query string.
100 function parse_options_query_string() {
102 'generate_enclosure' => FALSE,
103 'show_usernames' => TRUE,
106 if (isset($_GET['src_url'])) {
107 $options['src_url'] = $_GET['src_url'];
110 if (isset($_GET['generate_enclosure'])) {
111 $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
114 if (isset($_GET['show_usernames'])) {
115 $options['show_usernames'] = $_GET['show_usernames'] != 0;
122 $options = parse_options_cli($argv, $argc);
123 $error_stream = fopen('php://stderr', 'w');
126 $options = parse_options_query_string();
127 $error_stream = fopen('php://output', 'w');
130 if (!isset($options['src_url'])) {
131 fwrite($error_stream, usage(is_cli() ? $argv : NULL));
135 $tweeper = new Tweeper($options['generate_enclosure'], $options['show_usernames']);
136 $output = $tweeper->tweep($options['src_url']);
137 if (is_null($output)) {