|
10 | 10 |
|
11 | 11 | use \Psr\Http\Message\ServerRequestInterface as Request;
|
12 | 12 | use \Psr\Http\Message\ResponseInterface as Response;
|
| 13 | +use \ParagonIE\Certainty\RemoteFetch; |
| 14 | +use \GuzzleHttp\Client as GuzzleClient; |
| 15 | +use \GuzzleHttp\Exception\ClientException as GuzzleClientException; |
13 | 16 |
|
14 | 17 | class CompareController extends Controller {
|
15 | 18 | const CACHE_EXPIRE_AFTER = 60 * 60 * 6;
|
16 | 19 |
|
17 | 20 | const GUZZLE_DEFAULT_OPTIONS = [
|
18 |
| - 'verify' => false, // TODO: Make this true once cacert.pem stops being annoying |
19 | 21 | 'http_errors' => true,
|
20 | 22 | ];
|
21 | 23 |
|
22 | 24 | const PARAMETER_MISSING = "<b>Um...</b> Seems like you're missing user %d's ID.";
|
23 | 25 | const COULDNT_FIND_USER = "<b>Whoops!</b> Looks like we couldn't find user %d! Verify that the channel ID is correct, then try again.";
|
24 | 26 | const USER_SUBS_PRIVATE = "<b>Whoops!</b> Looks like user %d's subscriptions settings are set to <b>private</b>! They'll have to <a href=\"https://imgur.com/a/P6Dcm\" class=\"alert-link\">set their subscriptions to <b>public</b></a> before they can be compared here.";
|
25 | 27 |
|
| 28 | + private $guzzle; |
| 29 | + private $guzzleParams; |
| 30 | + |
| 31 | + public function __construct($container) { |
| 32 | + parent::__construct($container); |
| 33 | + |
| 34 | + $fetcher = new RemoteFetch(); |
| 35 | + $latestCACertBundle = $fetcher->getLatestBundle(); |
| 36 | + $this->guzzle = new GuzzleClient(); |
| 37 | + $this->guzzleParams = array_merge(self::GUZZLE_DEFAULT_OPTIONS, [ |
| 38 | + 'verify' => $latestCACertBundle->getFilePath(), |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
26 | 42 | public function compare(Request $request, Response $response) {
|
27 | 43 | $user1 = $request->getParam('user1');
|
28 | 44 | $user2 = $request->getParam('user2');
|
@@ -64,14 +80,14 @@ public function compare(Request $request, Response $response) {
|
64 | 80 |
|
65 | 81 | try {
|
66 | 82 | $user1Subs = $this->getUserSubscriptions($user1);
|
67 |
| - } catch (\GuzzleHttp\Exception\ClientException $e) { |
| 83 | + } catch (GuzzleClientException $e) { |
68 | 84 | $this->container->flash->addMessage('danger', sprintf(self::USER_SUBS_PRIVATE, 1));
|
69 | 85 | return $response->withRedirect($this->container->router->pathFor('home'));
|
70 | 86 | }
|
71 | 87 |
|
72 | 88 | try {
|
73 | 89 | $user2Subs = $this->getUserSubscriptions($user2);
|
74 |
| - } catch (\GuzzleHttp\Exception\ClientException $e) { |
| 90 | + } catch (GuzzleClientException $e) { |
75 | 91 | $this->container->flash->addMessage('danger', sprintf(self::COULDNT_FIND_USER, 2));
|
76 | 92 | return $response->withRedirect($this->container->router->pathFor('home'));
|
77 | 93 | }
|
@@ -103,16 +119,15 @@ private function getChannelNameFromID($channelID) {
|
103 | 119 | if ($this->container->cache->hasItem($cacheName) && $this->container->cache->getItem($cacheName)->isHit()) {
|
104 | 120 | return $this->container->cache->getItem($cacheName)->get();
|
105 | 121 | } else {
|
106 |
| - $client = new \GuzzleHttp\Client(); |
107 |
| - $params = array_merge(self::GUZZLE_DEFAULT_OPTIONS, [ |
| 122 | + $params = array_merge($this->guzzleParams, [ |
108 | 123 | 'query' => [
|
109 | 124 | 'part' => 'snippet',
|
110 | 125 | 'id' => $channelID,
|
111 | 126 | 'key' => getenv('YT_API_KEY'),
|
112 | 127 | ],
|
113 | 128 | ]);
|
114 | 129 |
|
115 |
| - $response = $client->get('https://www.googleapis.com/youtube/v3/channels', $params); |
| 130 | + $response = $this->guzzle->get('https://www.googleapis.com/youtube/v3/channels', $params); |
116 | 131 |
|
117 | 132 | if ($response->getStatusCode() == 200) {
|
118 | 133 | $body = json_decode((string) $response->getBody(), true);
|
@@ -140,22 +155,20 @@ private function getSubscriptionsOnPage($channelID, $page = null) {
|
140 | 155 | if ($this->container->cache->hasItem($cacheName) && $this->container->cache->getItem($cacheName)->isHit()) {
|
141 | 156 | return $this->container->cache->getItem($cacheName)->get();
|
142 | 157 | } else {
|
143 |
| - $client = new \GuzzleHttp\Client(); |
144 |
| - $params = array_merge(self::GUZZLE_DEFAULT_OPTIONS, [ |
| 158 | + $params = array_merge($this->guzzleParams, [ |
145 | 159 | 'query' => [
|
146 | 160 | 'part' => 'snippet',
|
147 | 161 | 'channelId' => $channelID,
|
148 | 162 | 'maxResults' => 50,
|
149 | 163 | 'key' => getenv('YT_API_KEY'),
|
150 | 164 | ],
|
151 |
| - 'verify' => false, |
152 | 165 | ]);
|
153 | 166 |
|
154 | 167 | if ($page) {
|
155 | 168 | $params['query']['pageToken'] = $page;
|
156 | 169 | }
|
157 | 170 |
|
158 |
| - $response = $client->get('https://www.googleapis.com/youtube/v3/subscriptions', $params); |
| 171 | + $response = $this->guzzle->get('https://www.googleapis.com/youtube/v3/subscriptions', $params); |
159 | 172 | if ($response->getStatusCode() == 200) {
|
160 | 173 | $body = json_decode((string) $response->getBody(), true);
|
161 | 174 |
|
|
0 commit comments