/** * Create a resource image from a URL. * * @param string $url The URL to create the resource from. * @param \pdyn\cache\CacheInterface $cache A caching object. * @param \pdyn\httpclient\HttpClientInterface $httpclient An HttpClientInterface instance. * @param int $ttl How long the cached information will stay valid, in seconds. * @return HttpResource An HttpResource object, or one of it's decendents. */ public static function instance($url, \pdyn\cache\CacheInterface $cache, \pdyn\httpclient\HttpClientInterface $httpclient, $ttl = 7200) { // Normalize/validate URL. $url = \pdyn\datatype\Url::normalize($url); if (!\pdyn\datatype\Url::validate($url)) { throw new Exception('Invalid URL: ' . $url, Exception::ERR_BAD_REQUEST); } $cache_key = sha1($url); $link_info = $cache->get('link_basic', $cache_key); if (!empty($link_info) && is_array($link_info)) { $link_info = $link_info['data']; } else { $response = $httpclient->get($url); $link_info = ['mime_type' => mb_strtolower($response->mime_type()), 'body' => base64_encode(gzdeflate($response->body(), 9)), 'url' => $url]; $link_info['handler'] = static::detect_resource_type($url, $link_info['mime_type'], $response->body()); $cache->store('link_basic', $cache_key, $link_info, time() + $ttl); } unset($link_info['body']); $resourcetypes = static::getresourcetypes(); $classname = $link_info['handler']; if (in_array($classname, $resourcetypes)) { $resource = new $classname($url, $cache, $httpclient, $ttl, $link_info); return $resource; } else { throw new Exception('rmtRes: did not know how to handle ' . $link_info['handler'] . ' resource type', Exception::ERR_BAD_REQUEST); } }
/** * Verify an incoming request. * * @param \pdyn\httpclient\HttpClientInterface $httpclient An HttpClientInterface instance. * @param Request $request The request object. * @param string $challenge Set the challenge string. If empty, a random one will be chosen. * @return bool Whether the verification was successful. */ public function verify_request(\pdyn\httpclient\HttpClientInterface $httpclient, Request $request, $challenge = '') { if (empty($challenge)) { $challenge = md5(rand() . rand() . rand() . microtime()); } $params = ['hub.mode' => $request->mode, 'hub.topic' => $request->topic, 'hub.challenge' => $challenge, 'hub.lease_seconds' => $request->leaseseconds]; if (!empty($request->token)) { $params['hub.verify_token'] = $request->token; } $response = $httpclient->get($request->callback, $params); return $response->status_type() === 'success' && $response->body() === $challenge ? true : false; }