5 * Tweeper - a Twitter to RSS web scraper.
7 * Copyright (C) 2013-2018 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|-u <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>");
47 return "usage: $usage";
51 * Parse command line options.
53 function parse_options_cli($argv, $argc) {
55 'generate_enclosure' => FALSE,
56 'show_usernames' => TRUE,
63 $cli_options = getopt("eu:h", array("help"));
64 foreach ($cli_options as $opt => $val) {
67 $options['generate_enclosure'] = TRUE;
71 $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
73 fwrite(STDERR, "Invalid argument for the -u option.\n");
74 fwrite(STDERR, usage($argv));
77 $options['show_usernames'] = $val;
86 fwrite(STDERR, usage($argv));
91 // For now assume that the URL is the lest argument, in the future we could
92 // switch to PHP >= 7.1 and use the $optind argument of getopt().
93 $options['src_url'] = array_pop($argv);
99 * Parse options passed from a query string.
101 function parse_options_query_string() {
103 'generate_enclosure' => FALSE,
104 'show_usernames' => TRUE,
107 if (isset($_GET['src_url'])) {
108 $options['src_url'] = $_GET['src_url'];
111 if (isset($_GET['generate_enclosure'])) {
112 $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
115 if (isset($_GET['show_usernames'])) {
116 $options['show_usernames'] = $_GET['show_usernames'] != 0;
123 $options = parse_options_cli($argv, $argc);
124 $error_stream = fopen('php://stderr', 'w');
127 $options = parse_options_query_string();
128 $error_stream = fopen('php://output', 'w');
131 if (!isset($options['src_url'])) {
132 fwrite($error_stream, usage(is_cli() ? $argv : NULL));
136 $tweeper = new Tweeper($options['generate_enclosure'], $options['show_usernames']);
137 $output = $tweeper->tweep($options['src_url']);
138 if (is_null($output)) {