Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 105 lines (96 sloc) 2.87 kb
482fd059 » adamfranco
2011-10-14 Initial outline.
1 <?php
2
554f74c6 » adamfranco
2011-10-19 Now giving a 400 Error response for no/bad URL values.
3 try {
4 if (empty($_GET['url']))
5 throw new InvalidArgumentException('No url provided.');
6 if (!parse_url($_GET['url']))
7 throw new InvalidArgumentException('Invalid url provided');
8 } catch (InvalidArgumentException $e) {
9 header('HTTP/1.1 400 Bad Request');
10 print "400 Bad Request\n".$e->getMessage();
11 exit;
12 }
482fd059 » adamfranco
2011-10-14 Initial outline.
13
14 $url = $_GET['url'];
b37fb4c1 » adamfranco
2011-10-17 Fleshed out fetching.
15 $id = md5($url);
482fd059 » adamfranco
2011-10-14 Initial outline.
16
17 require_once('config.php');
18 require_once('lib.php');
5f6c4378 » adamfranco
2011-10-17 Added security check for allowed clients.
19
b37fb4c1 » adamfranco
2011-10-17 Fleshed out fetching.
20 global $db;
482fd059 » adamfranco
2011-10-14 Initial outline.
21 $db = new PDO(DB_DSN, DB_USER, DB_PASS);
22
5f6c4378 » adamfranco
2011-10-17 Added security check for allowed clients.
23 // Verify that the client is allowed.
eefb2487 » adamfranco
2011-10-17 Can now support limiting access based on a chain of proxies using X-F…
24 $allowed = FALSE;
25 if (!empty($allowedClients) && in_array($_SERVER['REMOTE_ADDR'], $allowedClients))
26 $allowed = TRUE;
2ae08e33 » adamfranco
2011-10-17 Fixed PHP notice when there is no proxy chain.
27 if (!empty($allowedProxyChains) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
eefb2487 » adamfranco
2011-10-17 Can now support limiting access based on a chain of proxies using X-F…
28 foreach ($allowedProxyChains as $regex) {
29 if (preg_match($regex, $_SERVER['HTTP_X_FORWARDED_FOR'])) {
30 $allowed = TRUE;
31 break;
32 }
33 }
34 }
35 if (!$allowed) {
5f6c4378 » adamfranco
2011-10-17 Added security check for allowed clients.
36 header('HTTP/1.1 403 Forbidden');
37 header('Content-Type: text/plain');
38 print "403 Forbidden\n";
39 $message = $_SERVER['REMOTE_ADDR']." is not in the allowed-client list.";
1e7d0063 » adamfranco
2011-10-18 Improved the error message when proxy-servers are involved.
40 if (!empty($allowedProxyChains) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']))
41 $message .= "\n".$_SERVER['HTTP_X_FORWARDED_FOR'].' is not in the allowed-proxy-chains list.';
5f6c4378 » adamfranco
2011-10-17 Added security check for allowed clients.
42 log_event('access_denied', $message, $id, $url);
43 print $message;
44 exit;
45 }
46
47 // Look up our data
b37fb4c1 » adamfranco
2011-10-17 Fleshed out fetching.
48 $stmt = $db->prepare('SELECT * FROM feeds WHERE id = ?');
49 $stmt->execute(array($id));
50 $row = $stmt->fetchObject();
7576bcef » adamfranco
2011-10-17 Ensuring that all SELECT cursors are closed so that additional querie…
51 $stmt->closeCursor();
482fd059 » adamfranco
2011-10-14 Initial outline.
52 if (empty($row)) {
16a21253 » adamfranco
2011-10-17 Improved error handling. Now can provide a reason the fetch failed.
53 try {
54 fetch_url($id, $url);
337262d5 » adamfranco
2011-10-19 Now storing status codes and messages to pass those through to clients.
55 } catch (FetchProxyException $e) {
56 header('HTTP/1.1 '.$e->getCode().' '.$e->getStatusMessage());
57 header('Content-Type: text/plain');
58 print $e->getMessage();
59 exit;
16a21253 » adamfranco
2011-10-17 Improved error handling. Now can provide a reason the fetch failed.
60 } catch (Exception $e) {
61 header('HTTP/1.1 500 Internal Server Error');
62 header('Content-Type: text/plain');
63 print $e->getMessage();
64 exit;
65 }
b37fb4c1 » adamfranco
2011-10-17 Fleshed out fetching.
66 $stmt = $db->prepare('SELECT * FROM feeds WHERE id = ?');
67 $stmt->execute(array($id));
68 $row = $stmt->fetchObject();
7576bcef » adamfranco
2011-10-17 Ensuring that all SELECT cursors are closed so that additional querie…
69 $stmt->closeCursor();
482fd059 » adamfranco
2011-10-14 Initial outline.
70 }
6bdcfe75 » adamfranco
2011-10-17 Now handling the case where the first fetch fails.
71
72 if ($row) {
73 $stmt = $db->prepare('UPDATE feeds SET last_access = NOW(), num_access = :num_access WHERE id = :id');
74 $stmt->execute(array(
75 ':id' => $id,
76 ':num_access' => $row->num_access + 1,
77 ));
78 }
482fd059 » adamfranco
2011-10-14 Initial outline.
79
b37fb4c1 » adamfranco
2011-10-17 Fleshed out fetching.
80 // If headers and data are null, then return an error
6bdcfe75 » adamfranco
2011-10-17 Now handling the case where the first fetch fails.
81 if (!$row || is_null($row->headers) && is_null($row->data)) {
337262d5 » adamfranco
2011-10-19 Now storing status codes and messages to pass those through to clients.
82 if (!empty($row->status_code))
83 $code = $row->status_code;
84 else
85 $code = 500;
86 if (!empty($row->status_msg))
87 $status = $row->status_msg;
88 else
89 $status = 'Internal Server Error';
90
91 header('HTTP/1.1 '.$code.' '.$status);
16a21253 » adamfranco
2011-10-17 Improved error handling. Now can provide a reason the fetch failed.
92 header('Content-Type: text/plain');
337262d5 » adamfranco
2011-10-19 Now storing status codes and messages to pass those through to clients.
93 print $code.' '.$status."\n";
16a21253 » adamfranco
2011-10-17 Improved error handling. Now can provide a reason the fetch failed.
94 print 'Errors have occurred while trying to fetch the feed.';
95 exit;
b37fb4c1 » adamfranco
2011-10-17 Fleshed out fetching.
96 }
97 // Otherwise, return our content.
98 else {
99 foreach (explode("\n", $row->headers) as $header) {
100 // To-do: Filter out some cache-control and expires header here if needed.
101 header($header);
102 }
103 print $row->data;
482fd059 » adamfranco
2011-10-14 Initial outline.
104 }
105 exit;
Something went wrong with that request. Please try again.