Add option to enable or disable showing usernames in RSS items
[tweeper.git] / tweeper.php
1 <?php
2 /**
3  * @file
4  * Tweeper - a Twitter to RSS web scraper.
5  *
6  * Copyright (C) 2013-2018  Antonio Ospite <ao2@ao2.it>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 require_once 'autoload.php';
23
24 use Tweeper\Tweeper;
25
26 date_default_timezone_set('UTC');
27
28 /**
29  * Check if the script is being run from the command line.
30  */
31 function is_cli() {
32   return (php_sapi_name() === "cli");
33 }
34
35 /**
36  * Show the script usage.
37  */
38 function usage($argv) {
39   if (is_cli()) {
40     $usage = "{$argv[0]} [-e|-u <0|1>|-h|--help] <src_url>\n";
41   }
42   else {
43     $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>&show_usernames=<0|1>");
44   }
45
46   return "usage: $usage";
47 }
48
49 /**
50  * Parse command line options.
51  */
52 function parse_options_cli($argv, $argc) {
53   $options = array(
54     'generate_enclosure' => FALSE,
55     'show_usernames' => TRUE,
56   );
57
58   if ($argc < 2) {
59     return $options;
60   }
61
62   $cli_options = getopt("eu:h", array("help"));
63   foreach ($cli_options as $opt => $val) {
64     switch ($opt) {
65       case 'e':
66         $options['generate_enclosure'] = TRUE;
67         break;
68
69       case 'u':
70         $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
71         if (NULL === $ret) {
72           fwrite(STDERR, "Invalid argument for the -u option.\n");
73           fwrite(STDERR, usage($argv));
74           exit(1);
75         }
76         $options['show_usernames'] = $val;
77         break;
78
79       case 'h':
80       case 'help':
81         echo usage($argv);
82         exit(0);
83
84       default:
85         fwrite(STDERR, usage($argv));
86         exit(1);
87     }
88   }
89
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);
93
94   return $options;
95 }
96
97 /**
98  * Parse options passed from a query string.
99  */
100 function parse_options_query_string() {
101   $options = array(
102     'generate_enclosure' => FALSE,
103     'show_usernames' => TRUE,
104   );
105
106   if (isset($_GET['src_url'])) {
107     $options['src_url'] = $_GET['src_url'];
108   }
109
110   if (isset($_GET['generate_enclosure'])) {
111     $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
112   }
113
114   if (isset($_GET['show_usernames'])) {
115     $options['show_usernames'] = $_GET['show_usernames'] != 0;
116   }
117
118   return $options;
119 }
120
121 if (is_cli()) {
122   $options = parse_options_cli($argv, $argc);
123   $error_stream = fopen('php://stderr', 'w');
124 }
125 else {
126   $options = parse_options_query_string();
127   $error_stream = fopen('php://output', 'w');
128 }
129
130 if (!isset($options['src_url'])) {
131   fwrite($error_stream, usage(is_cli() ? $argv : NULL));
132   exit(1);
133 }
134
135 $tweeper = new Tweeper($options['generate_enclosure'], $options['show_usernames']);
136 $output = $tweeper->tweep($options['src_url']);
137 if (is_null($output)) {
138   exit(1);
139 }
140 echo $output;