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|-m <0|1>|-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>&show_multimedia=<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,
64 $cli_options = getopt("em:u:h", array("help"));
65 foreach ($cli_options as $opt => $val) {
68 $options['generate_enclosure'] = TRUE;
72 $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
74 fwrite(STDERR, "Invalid argument for the -m option.\n");
75 fwrite(STDERR, usage($argv));
78 $options['show_multimedia'] = $val;
82 $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
84 fwrite(STDERR, "Invalid argument for the -u option.\n");
85 fwrite(STDERR, usage($argv));
88 $options['show_usernames'] = $val;
97 fwrite(STDERR, usage($argv));
102 // For now assume that the URL is the lest argument, in the future we could
103 // switch to PHP >= 7.1 and use the $optind argument of getopt().
104 $options['src_url'] = array_pop($argv);
110 * Parse options passed from a query string.
112 function parse_options_query_string() {
114 'generate_enclosure' => FALSE,
115 'show_usernames' => TRUE,
116 'show_multimedia' => TRUE,
119 if (isset($_GET['src_url'])) {
120 $options['src_url'] = $_GET['src_url'];
123 if (isset($_GET['generate_enclosure'])) {
124 $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
127 if (isset($_GET['show_multimedia'])) {
128 $options['show_multimedia'] = $_GET['show_multimedia'] != 0;
131 if (isset($_GET['show_usernames'])) {
132 $options['show_usernames'] = $_GET['show_usernames'] != 0;
139 $options = parse_options_cli($argv, $argc);
140 $error_stream = fopen('php://stderr', 'w');
143 $options = parse_options_query_string();
144 $error_stream = fopen('php://output', 'w');
147 if (!isset($options['src_url'])) {
148 fwrite($error_stream, usage(is_cli() ? $argv : NULL));
152 $tweeper = new Tweeper($options['generate_enclosure'], $options['show_usernames'], $options['show_multimedia']);
153 $output = $tweeper->tweep($options['src_url']);
154 if (is_null($output)) {