/** * 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); } }
/** * This sends a request to the hub. * * @param string $mode The request mode. 'subscribe' or 'unsubscribe' * @param string $url The URL to send the request to - the hub URL. * @param string $topic The topic you are referring to. * @param string $callback The local callback URL. * @param \pdyn\httpclient\HttpClientInterface $httpclient An HttpClientInterface object that will handle the transfer. * @return bool|array True if successful, array of 'status_code', and 'msg', specifying received http code and body text. */ protected function send_request($mode, $url, $topic, $callback, \pdyn\httpclient\HttpClientInterface $httpclient) { if (!\pdyn\datatype\Url::validate($callback)) { throw new Exception('Bad $callback received', Exception::ERR_BAD_REQUEST); } if (!\pdyn\datatype\Url::validate($url)) { throw new Exception('Bad $url received', Exception::ERR_BAD_REQUEST); } $request = $this->storage->get_request($mode, $topic, $url, $callback); $verifytoken = \pdyn\base\Utils::genRandomStr(64); if (empty($request)) { $request = $this->storage->create_request($mode, $topic, $url, $callback, $verifytoken); } $postdata = ['hub.mode' => $mode, 'hub.callback' => $callback, 'hub.topic' => $topic, 'hub.verify' => 'async', 'hub.verify_token' => $verifytoken, 'hub.lease_seconds' => 604800]; $response = $httpclient->post($url, $postdata); if ($response->status_code() === 204 || $response->status_code() === 202) { return true; } else { $this->storage->delete_request($request->id); return ['status_code' => $response->status_code(), 'msg' => $response->body()]; } }
/** * 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; }