/** * Requests the $url and tries to parse the response as oembed type. The url * must be pointing to an oembed provider i.e.: * http://flickr.com/services/oembed?url=http://www.flickr.com/photos/neilio/20403964/ * * @param PSX\Url $url * @return PSX\Oembed\TypeAbstract */ public function request(Url $url) { if (!$url->issetParam('url')) { throw new Exception('Required parameter url missing'); } $format = $url->addParam('format', 'json'); $request = new GetRequest($url, array('User-Agent' => __CLASS__ . ' ' . Base::VERSION, 'Accept' => 'application/json')); $response = $this->http->request($request); if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { $source = function (array $data) { $type = isset($data['type']) ? strtolower($data['type']) : null; if (in_array($type, array('link', 'photo', 'rich', 'video'))) { $class = 'PSX\\Oembed\\Type\\' . ucfirst($type); if (class_exists($class)) { return new $class(); } else { throw new Exception('Class "' . $class . '" does not exist'); } } else { throw new InvalidDataException('Invalid type'); } }; return $this->importer->import($source, $response, null, ReaderInterface::JSON); } else { throw new Exception('Invalid response code ' . $response->getStatusCode()); } }
public function testGet() { $url = new Url($this->getEndpoint()); $url->addParam('format', 'jas'); $response = $this->signedRequest('GET', $url); $this->assertEquals(200, $response->getCode()); // check result $result = Json::decode($response->getBody()); $this->assertArrayHasKey('itemsPerPage', $result, $response->getBody()); $this->assertArrayHasKey('startIndex', $result, $response->getBody()); $this->assertArrayHasKey('items', $result, $response->getBody()); $this->assertEquals(true, count($result['items']) > 0); foreach ($result['items'] as $activity) { $this->assertArrayHasKey('actor', $activity); $this->assertArrayHasKey('object', $activity); $this->assertArrayHasKey('verb', $activity); } // try to create activitystream object $reader = new Reader\Json(); $result = $reader->read($response); $collection = new Collection(); $collection->import($result); $this->assertInstanceOf('PSX\\ActivityStream\\Collection', $collection); }
public function testResourceNotFound() { $url = new Url($this->getEndpoint()); $url->addParam('resource', 'foobar'); $request = new GetRequest($url); $response = $this->http->request($request); $this->assertEquals(404, $response->getCode()); }
private function denyAccess($token, $callback) { // insert access $now = new DateTime('NOW', $this->registry['core.default_timezone']); $this->getSql()->replace($this->registry['table.oauth_access'], array('apiId' => $this->apiId, 'userId' => $this->user->getId(), 'allowed' => 0, 'date' => $now->format(DateTime::SQL))); // delete token $con = new Condition(array('token', '=', $token)); $this->getSql()->delete($this->registry['table.oauth_request'], $con); // redirect if callback available if ($callback != 'oob') { $url = new Url($callback); // here we can inform the consumer that the request has been denied $url->addParam('oauth_token', $token); $url->addParam('x_oauth_error', 'request+denied'); header('Location: ' . strval($url)); exit; } else { header('Location: ' . $this->config['psx_url']); exit; } }
protected function discoverWebfingerRfc7033(Url $url, $resource, $rel = null) { try { $url = $url->getScheme() . '://' . $url->getHost() . '/.well-known/webfinger'; $url = new Url($url); $url->addParam('resource', $resource); if ($rel !== null) { $url->addParam('rel', $rel); } return Hostmeta::requestJrd($this->http, $url); } catch (\Exception $e) { $this->lastError = $e->getMessage(); } return null; }
protected function doCheckidSetupRequest($assocHandle) { $url = new Url('http://127.0.0.1/openid'); $url->addParam('openid_ns', 'http://specs.openid.net/auth/2.0'); $url->addParam('openid_mode', 'checkid_setup'); $url->addParam('openid_claimed_id', 'http://k42b3.com'); $url->addParam('openid_identity', 'http://k42b3.com'); $url->addParam('openid_assoc_handle', $assocHandle); $url->addParam('openid_return_to', 'http://127.0.0.1/callback'); $body = new TempStream(fopen('php://memory', 'r+')); $request = new Request($url, 'GET'); $response = new Response(); $response->setBody($body); $controller = $this->loadController($request, $response); return new Url((string) $response->getHeader('Location')); }
public function onCheckidSetup(SetupRequest $request) { // check whether authenticated if (!$this->isAuthenticated()) { $loginUrl = $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . 'login'; $selfUrl = new Url($this->base->getSelf()); $values = array_merge($_GET, $_POST); foreach ($values as $key => $value) { $selfUrl->addParam($key, $value); } //$selfUrl->addParam('openid.mode', 'checkid_setup'); //$selfUrl->addParam('openid.ns', self::NS); header('Location: ' . $loginUrl . '?redirect=' . urlencode(strval($selfUrl))); exit; } // check association $sql = <<<SQL SELECT \t`assoc`.`id`, \t`assoc`.`expires`, \t`assoc`.`date` FROM \t{$this->registry['table.openid_assoc']} `assoc` WHERE \t`assoc`.`assocHandle` = ? SQL; $row = $this->sql->getRow($sql, array($request->getAssocHandle())); if (!empty($row)) { // check expire $now = new DateTime('NOW', $this->registry['core.default_timezone']); $expire = (int) $row['expires']; if (time() > $now->getTimestamp() + $expire) { throw new Exception('Association is expired'); } } else { if (!$request->isImmediate()) { // create association $date = new DateTime('NOW', $this->registry['core.default_timezone']); $assocHandle = ProviderAbstract::generateHandle(); $secret = base64_encode(ProviderAbstract::randomBytes(20)); $this->sql->insert($this->registry['table.openid_assoc'], array('assocHandle' => $assocHandle, 'assocType' => 'HMAC-SHA1', 'sessionType' => 'DH-SHA1', 'secret' => $secret, 'expires' => self::EXPIRE, 'date' => $date->format(DateTime::SQL))); // set assoc handle $request->setAssocHandle($assocHandle); } else { throw new Exception('Invalid association'); } } // count connect requests /* $maxCount = 5; $con = new PSX_Sql_Condition(array('userId', '=', $this->user->getId()), array('status', '=', AmunService_Oauth_Record::TEMPORARY)); $count = $this->sql->count($this->registry['table.oauth_request'], $con); if($count > $maxCount) { $conDelete = new PSX_Sql_Condition(); $result = $this->sql->select($this->registry['table.oauth_request'], array('id', 'expire', 'date'), $con, PSX_Sql::SELECT_ALL); foreach($result as $row) { $now = new DateTime('NOW', $this->registry['core.default_timezone']); $date = new DateTime($row['date'], $this->registry['core.default_timezone']); $date->add(new DateInterval($row['expire'])); if($now > $date) { $conDelete->add('id', '=', $row['id'], 'OR'); } } if($conDelete->hasCondition()) { $this->sql->delete($this->registry['table.oauth_request'], $conDelete); } throw new Exception('You can have max ' . $maxCount . ' temporary account connect requests. Each request expires after 30 hour'); } */ // save request params $_SESSION['amun_openid_request'] = $request; // redirect header('Location: ' . $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . 'login/connect'); exit; }
private function replaceUrl(Token\Text $text) { if (strpos($text->data, 'http://') === false && strpos($text->data, 'https://') === false) { return false; } // if parent element of the text is an link dont replace links $isHref = false; if ($text->parentNode instanceof Token\Element && strtolower($text->parentNode->name) == 'a') { $isHref = true; } $parts = preg_split('/(https?:\\/\\/\\S*)/S', $text->data, -1, PREG_SPLIT_DELIM_CAPTURE); $data = ''; foreach ($parts as $i => $part) { if ($i % 2 == 0) { $data .= $part; } else { try { $url = new Url($part); if ($this->discover) { foreach ($this->oembedHosts as $host => $endpoint) { if (strpos($url->getHost(), $host) !== false) { try { $api = new Url($endpoint); $api->addParam('url', $part); $api->addParam('maxwidth', 240); $api->addParam('maxheight', 180); $type = $this->oembed->request($api); $this->oembedMedia[] = $type; break; } catch (\Exception $e) { // oembed discovery failed } } } } if (!$isHref) { $data .= '<a href="' . $part . '">' . $part . '</a>'; } else { $data .= $part; } } catch (Exception $e) { $data .= $part; } } } $text->data = $data; }