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