aboutsummaryrefslogtreecommitdiff
path: root/stuff/rssfilter.php
blob: 957e6484ad5a162e147176dd39afa21c4f94612a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php

// Here's my embarrassing ass source code. It is public domain, do with it what you will

/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/

if($_GET['downloadsource'] == 'yes') { // just serve the source code as plain text and exit
  header('Content-Type: text/plain');
  echo file_get_contents('rssfilter.php');
  die();
}

$feed = $_GET['feed'];
$filter = $_GET['filter'];
$filtertype = $_GET['type'];
$useregex = false;
if($_GET['regex'] == 'yes' or $_GET['regex'] == 'y') {
  $useregex = true;
}

function rss_testforfilter($item) { // I wrote two different functions for filtering RSS and Atom feeds under the assumption that they would be radically different 
                                    // in some way. The only real difference ended up being how the categories are stored within the category tags. I could probably 
                                    // consolidate them into one function, but these boolean expressions are big and complicated and I'm too scared to touch it now.

  global $useregex; // PHP is silly and makes you re-declare global variables if you want to use them inside functions.
  global $filter;
  foreach($item->category as $category) { // loops through all the <category> tags and returns true if any of them contain the filter string
    if(
      (!$useregex and strpos($category, $filter) !== false) or // Don't use regex and filter string is found
      ($useregex and preg_match($filter, $category))           // DO use regex and filter expression is found
      ) {
      return true;
    }
  }
  if(
    (!$useregex and (strpos($item->description, $filter) !== false or strpos($item->title, $filter) !== false)) or // Don't use regex and filter string is found
    ($useregex and (preg_match($filter, $item->description) or preg_match($filter, $item->title)))                 // DO use regex and filter expression is found
    ) {
    return true;
  }
  return false;
}

function atom_testforfilter($entry) {
  global $useregex;
  global $filter;
  foreach($entry->category as $category) { // loops through all the <category> tags and returns true if any of them contain the filter string
    if(
      (!$useregex and strpos($category['term'], $filter) !== false) or // Don't use regex and filter string is found
      ($useregex and preg_match($filter, $category['term']))           // DO use regex and filter expression is found
      ) {
      return true;
    }
  }
  if(
    (!$useregex and (strpos($entry->description, $filter) !== false or strpos($entry->title, $filter) !== false)) or // Don't use regex and filter string is found
    ($useregex and (preg_match($filter, $entry->description) or preg_match($filter, $entry->title)))                 // DO use regex and filter expression is found
    ) {
    return true;
  }
  return false;
}

if($feed                                                   // A basic check to make sure the URL is formed properly. It doesn't actually check for a valid URL or valid regex. 
  and $filter                                              // If this check fails, the HTML document below this gets served.
  and ($filtertype === "white" or $filtertype === "black")
){
  header('Content-Type: text/xml'); // Not sure if I actually need to do this, but it seems like good practice and it makes Chromium display the XML tree all nice.

  $content = simplexml_load_string(file_get_contents($feed)); // I've encountered a problem where something between the server this command pulls from and this script seems to
                                                              // be caching the result. I'm guessing it's some kind of Nginx configuration error, but I'm not sure. Maybe I
                                                              // should be using a different function to do this? rss-bridge uses the cURL library, which I know because I had
                                                              // to install it myself.

  if($content->getName() == 'rss') { // Here I just assume everything not explicitly an RSS feed is an Atom feed. I am 100% certain there is an edge case I'm missing here.

    echo '<rss version="2.0"><channel>';                        // This is the most embarrassing part of this code by far. I wanted to do this by looping over all the entries
    echo $content->channel->title->asXML();                     // in the XML object and specifically removing the ones that didn't pass the filter *from* the object, but
    echo $content->channel->link->asXML();                      // I couldn't for the life of me get that to work, so I settled for the next best thing: just echoing all the
    echo $content->channel->description->asXML();               // fields that are part of the spec out to a brand new XML document and then adding all the entries that did
    echo $content->channel->language->asXML();                  // pass. I mean, if it works it works, but sorry to anyone that was using some nonstandard feature here.
    echo $content->channel->pubDate->asXML();
    echo $content->channel->lastBuildDate->asXML();
    echo $content->channel->docs->asXML();
    echo "<generator>will's RSS filter script</generator>";
    echo $content->channel->managingEditor->asXML();
    echo $content->channel->webMaster->asXML();

    foreach($content->channel->item as $item) {

      $found = rss_testforfilter($item, $filter);

      if(($found and $filtertype == 'white') or (!$found and $filtertype == 'black')) {
        echo $item->asXML();
      }
      
    }

    echo '</channel></rss>';
    
    die();
  } else {

    echo '<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom">';
    echo $content->title->asXML();
    echo $content->link->asXML();
    echo $content->updated->asXML();
    echo $content->author->asXML();
    echo $content->id->asXML();
    foreach($content->category as $category) {
      echo $category->asXML();
    }
    foreach($content->contributor as $contributor) {
      echo $contributor->asXML();
    }
    echo "<generator>will's Atom filter script</generator>";
    echo $content->icon->asXML();
    echo $content->logo->asXML();
    echo $content->rights->asXML();
    echo $content->subtitle->asXML();

    foreach($content->entry as $entry) {

      $found = atom_testforfilter($entry, $filter);

      if(($found and $filtertype == 'white') or (!$found and $filtertype == 'black')) {
        echo $entry->asXML();
      }
      
    }

    echo '</feed>';
    
    die();
  }
}

?>
<!DOCTYPE html>
<html lang="en">
  <head>
    
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>RSS Filter</title>
    <style type="text/css">
      html, body {
        margin: 0;
        padding: 0;
        height: 100%;
      }
      body {
        margin: auto;
        width: fit-content;
        max-width: 32rem;
        background-color: #222222;
        font-family: monospace;
        color: white;
        display: flex;
        align-items: center;
        font-size: 1rem;
        flex-direction: column;
        justify-content: center;
        text-shadow: 1px 1px 3px black;
      }
      td:first-child {
        padding-right: 1em;
        font-weight: bold;
      }
      input[type="text"] {
        width: 100%;
      }
      code {
        font-size: initial;
        background-color: #101010;
        padding: 1em;
        border-radius: 1em;
        display: block;
        text-align: center;
        margin: auto;
        box-shadow: 2px 2px 4px black;
      }
      code span {
        color: #ccc;
      }
      table {
        font-size: 0.9rem;
      }
      a {
        color: #df1955;
      }
      a:hover {
        color: #00ffd5;
      }
      @media only screen and (orientation: portrait) {
        td {
          display: block;
          padding-top: 0.5rem;
        }
        body {
          max-width: 20rem;
        }
      }
    </style>
  </head>
  <body>
    <h1 style="margin: 0;">RSS Filter</h1>    
    <hr style="width: 30%; margin: 1.25rem;">
    <table>
      <noscript>
        <tr>
          <td colspan="2">You need to have Javascript enabled for this generator to work, sorry.</td>
        </tr>
      </noscript>
      <tr>
        <td>Feed URL</td>
        <td><input type="text" placeholder="https://example.com/rss" id="feed" value=""></input></td>
      </tr>
      <tr>
        <td>Filter String</td>
        <td><input type="text" placeholder="foobar" id="filter" value=""></input></td>
      </tr>
      <tr>
        <td>Filter Type</td>
        <td>
          <input type="radio" id="whitelist" name="filtertype" value="white" checked>
          <label for="whitelist">Whitelist</label>
          <input type="radio" id="blacklist" name="filtertype" value="black">
          <label for="blacklist">Blacklist</label>
        </td>
      </tr>
      <tr>
        <td><label for="regex">Use Regex</label></td>
        <td><input type="checkbox" id="regex"></td>
      </tr>
    </table>
    <p>
      <code><a id="generatedlink" href=""></a></code>
    </p>
    <p style="position: absolute; bottom: 1em; margin: 0; text-align: center;">
      Made by <a href="https://bugwo.men/">an isopod</a><br>
      <a href="?downloadsource=yes">Download source code</a>
    </p>
    <script>

      let feedURL = "https://example.com/rss";
      let filter = "foobar";
      let type = "white";
      let regex = "";

      function updateLink() {
        let link = `${window.location.href}?feed=${feedURL}&filter=${filter}&type=${type}${regex}`;
        document.getElementById('generatedlink').href = link;
        document.getElementById('generatedlink').innerHTML = link.replace(/&/g, "&amp;");
      }

      updateLink();

      document.getElementById('feed').addEventListener('input', function(evt) {
        feedURL = encodeURI(this.value);
        updateLink();
      });
      document.getElementById('filter').addEventListener('input', function(evt) {
        filter = encodeURI(this.value);
        updateLink();
      });
      document.getElementById('whitelist').addEventListener('input', function(evt) {
        type = "white";
        updateLink();
      });
      document.getElementById('blacklist').addEventListener('input', function(evt) {
        type = "black";
        updateLink();
      });
      document.getElementById('regex').addEventListener('change', function(evt) {
        if(this.checked) {
          regex = "&regex=yes";
          document.getElementById('filter').placeholder = "/foobar/";
        } else {
          regex = "";
          document.getElementById('filter').placeholder = "foobar";
        }
        updateLink();
      });
    </script>
  </body>
</html>