Example #1
0
File: Mock.php Project: seytar/psx
 public function request(RequestInterface $request, Options $options)
 {
     $url = $request->getUri();
     foreach ($this->resources as $resource) {
         $resourceUrl = new Url($resource['url']);
         if ($resource['method'] == $request->getMethod() && $resourceUrl->getHost() == $url->getHost() && $resourceUrl->getPath() == $url->getPath() && $resourceUrl->getQuery() == $url->getQuery()) {
             $response = $resource['handler']($request);
             return ResponseParser::convert($response);
         }
     }
     throw new Exception('Resource not available ' . $request->getMethod() . ' ' . $url);
 }
Example #2
0
 public function hasProject($url)
 {
     $url = new Url($url);
     if ($url->getHost() != 'github.com') {
         return false;
     }
     $parts = explode('/', trim($url->getPath(), '/'));
     if (count($parts) != 2) {
         return false;
     }
     $request = new GetRequest($url);
     $response = $this->http->request($request);
     return $response->getCode() == 200;
 }
Example #3
0
File: Oauth.php Project: seytar/psx
 /**
  * Normalize the url like defined in
  *
  * @see http://tools.ietf.org/html/rfc5849#section-3.4.1.2
  * @param \PSX\Url $url
  * @return false|string
  */
 public static function getNormalizedUrl(Url $url)
 {
     $scheme = $url->getScheme();
     $host = $url->getHost();
     $port = $url->getPort();
     $path = $url->getPath();
     // no port for 80 (http) and 443 (https)
     if (($port == 80 || empty($port)) && strcasecmp($scheme, 'http') == 0 || ($port == 443 || empty($port)) && strcasecmp($scheme, 'https') == 0) {
         $normalizedUrl = $scheme . '://' . $host . $path;
     } else {
         if (!empty($port)) {
             $normalizedUrl = $scheme . '://' . $host . ':' . $port . $path;
         } else {
             throw new Exception('No port specified');
         }
     }
     return strtolower($normalizedUrl);
 }
Example #4
0
File: Html.php Project: visapi/amun
 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;
 }
Example #5
0
 public function create(RecordInterface $record)
 {
     if ($record->hasFields('groupId', 'status', 'identity', 'name', 'pw')) {
         // check whether identity exists
         $con = new Condition();
         $con->add('identity', '=', $record->identity);
         if ($this->table->count($con) > 0) {
             throw new Exception('Identity already exists');
         }
         // check whether name and hostid exists
         $con = new Condition();
         $con->add('hostId', '=', !empty($record->hostId) ? $record->hostId : 0);
         $con->add('name', '=', $record->name);
         if ($this->table->count($con) > 0) {
             throw new Exception('Identity already exists');
         }
         // default values
         if (!isset($record->countryId)) {
             $record->setCountryId(1);
         }
         if (!isset($record->timezone)) {
             $record->setTimezone('UTC');
         }
         $date = new DateTime('NOW', $this->registry['core.default_timezone']);
         $record->token = Security::generateToken();
         $record->ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
         $record->lastSeen = $date->format(DateTime::SQL);
         $record->updated = $date->format(DateTime::SQL);
         $record->date = $date->format(DateTime::SQL);
         // set host id if we have an remote host discover the profile url
         if (empty($record->hostId)) {
             $record->hostId = 0;
             $record->profileUrl = $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . 'profile/' . $record->name;
         } else {
             $record->status = Record::REMOTE;
             $record->profileUrl = $this->discoverProfileUrl($record->hostId, $record->name);
         }
         // set global id
         if (!isset($record->globalId)) {
             $profileUrl = new Url($record->profileUrl);
             $record->globalId = $this->base->getUUID('user:account:' . $profileUrl->getHost() . ':' . $record->name . ':' . uniqid());
         }
         // set thumbnail if email available and thumbnail not set
         if (!isset($record->thumbnailUrl)) {
             $default = $this->config['psx_url'] . '/img/avatar/no_image.png';
             if (!empty($record->email)) {
                 $record->thumbnailUrl = 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($record->email))) . '?d=' . urlencode($default) . '&s=48';
             } else {
                 $record->thumbnailUrl = $default;
             }
         }
         $this->table->insert($record->getData());
         $record->id = $this->sql->getLastInsertId();
         // insert relation to self
         $this->sql->insert($this->registry['table.user_friend'], array('status' => Friend\Record::NORMAL, 'userId' => $record->id, 'friendId' => $record->id, 'date' => $date->format(DateTime::SQL)));
         $this->notify(RecordAbstract::INSERT, $record);
         return $record;
     } else {
         throw new Exception('Missing field in record');
     }
 }