예제 #1
0
 public function testCanCreateStreamsUsingCustomFactory()
 {
     $stream = $this->getMockBuilder('Guzzle\\Stream\\StreamRequestFactoryInterface')->setMethods(array('fromRequest'))->getMockForAbstractClass();
     $resource = new Stream(fopen('php://temp', 'r+'));
     $stream->expects($this->once())->method('fromRequest')->will($this->returnValue($resource));
     $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest"));
     $result = StaticClient::get($this->getServer()->getUrl(), array('stream' => $stream));
     $this->assertSame($resource, $result);
 }
 public function getUserInformation()
 {
     $url = $this->config['api_url'] . '/profile';
     $response = Guzzle::get($url, array('headers' => array('Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->access_token), 'exceptions' => false));
     if ($response->isSuccessful()) {
         $userInfo = $response->json();
         if (is_array($userInfo)) {
             return $userInfo;
         } else {
             Yii::app()->user->setFlash('danger', "There was a problem with the response from OAuth server, maybe try again in a moment? (" . __LINE__ . ")");
             return false;
         }
     } else {
         Yii::app()->user->setFlash('danger', "There was a problem with the response from OAuth server, maybe try again in a moment? (" . __LINE__ . "). More Info: " . $response->getBody());
         return false;
     }
 }
예제 #3
0
 public static function execute()
 {
     // TODO: create configuration file for organization/client_id/client_secret
     $organization = 'doubleleft';
     $data = '# -------------------------------------------------------------' . PHP_EOL;
     $data .= '# WARNING: PROTECT PUBLIC ACCESS OF THIS FILE ON YOUR WEBSERVER' . PHP_EOL;
     $data .= '# -------------------------------------------------------------' . PHP_EOL;
     $data .= '# Lists of allowed openssh keys to consume API via commandline ' . PHP_EOL;
     $data .= '# ' . PHP_EOL;
     \Guzzle\Http\StaticClient::mount();
     $public_keys = array();
     foreach (self::get("/orgs/{$organization}/members") as $member) {
         foreach (self::getUserKeys($member['login']) as $key) {
             $data .= $key['key'] . PHP_EOL;
         }
     }
     file_put_contents(__DIR__ . '/../../' . self::DESTINATION_FILE, $data);
     echo "Github public keys downloaded successfully at '" . self::DESTINATION_FILE . "'." . PHP_EOL;
 }
예제 #4
0
 /**
  * Download the file to a local path.
  *
  * @param  string  $pathToFile,
  * @param  string  $name
  * @param  headers $array
  * @return void
  */
 public function download($pathToFile, $name = null, array $headers = array())
 {
     $response = StaticClient::get($this->offsetGet('_downloadURL'), array('headers' => $headers, 'timeout' => $this->timeout, 'save_to' => $pathToFile));
     file_put_contents($pathToFile, $response->getBody()->getStream());
 }
use Symfony\Component\Yaml\Yaml;
use OCLC\Auth\WSKey;
use OCLC\Auth\AuthCode;
use OCLC\Auth\AccessToken;
use OCLC\User;
// The global configuration object.
// If you have not created a config.yaml file with your authentication parameters in it,
// copy app/config/sampleConfig.yaml to app/config/config.yaml and set the parameters.
global $config;
$config = Yaml::parse('app/config/config.yaml');
// Set the Guzzle options.
// Guzzle makes it easy to work with restful web services.
// http://guzzle.readthedocs.org/en/latest/
$guzzleOptions = array('config' => array('curl' => array(CURLOPT_SSLVERSION => 3)), 'allow_redirects' => array('strict' => true), 'timeout' => 60);
if (!class_exists('Guzzle')) {
    \Guzzle\Http\StaticClient::mount();
}
// We use sessions to persist our authentication between calls to the WMS Availability service.
session_start();
// Construct a new WSkey object using the key, secret and an options array that contains the services
// you want to access and your redirect_uri
$options = array('services' => array('WMS_Availability', 'refresh_token'));
$wskey = new WSKey($config['wskey'], $config['secret'], $options);
// if you don't have an Access Token already
if (empty($_SESSION['accessToken'])) {
    $accessToken = $wskey->getAccessTokenWithClientCredentials($config['institution'], $config['institution']);
    $_SESSION['accessToken'] = $accessToken;
} else {
    // Otherwise, get the existing access token from the session
    $accessToken = $_SESSION['accessToken'];
}
 public function apiInvalidToken()
 {
     if (!Yii::app()->user->isGuest && isset(Yii::app()->user->access_token)) {
         $url = Yii::app()->params['oauth']['api_url'] . '/profile';
         $response = Guzzle::get($url, array('headers' => array('Accept' => 'application/json', 'Authorization' => 'Bearer invalidtoken'), 'exceptions' => false));
         if ($response->isSuccessful()) {
             return $response->json();
         } else {
             return json_decode($response->getBody(true), true);
         }
     }
 }
예제 #7
0
 /**
  * Adds support for Twitter user photo resource paths.
  *
  * @param string $path
  * @return string|null
  */
 public function getResourcePath($path)
 {
     // Are they requesting a Twitter user image?
     if (strncmp($path, 'twitteruserimages/', 18) === 0) {
         $parts = array_merge(array_filter(explode('/', $path)));
         if (count($parts) != 3) {
             return;
         }
         $userId = $parts[1];
         $size = $parts[2];
         $imageSizes = array('mini' => 24, 'normal' => 48, 'bigger' => 73);
         if (is_numeric($size) && ($sizeKey = array_search($size, $imageSizes)) !== false) {
             $size = $sizeKey;
         }
         $baseUserImagePath = craft()->path->getRuntimePath() . 'twitter/userimages/' . $userId . '/';
         $sizedFolderPath = $baseUserImagePath . $size . '/';
         // Have we already downloaded this user's image at this size?
         $contents = IOHelper::getFolderContents($sizedFolderPath, false);
         if ($contents) {
             return $contents[0];
         } else {
             // Do we have the original image?
             if (!is_numeric($size)) {
                 if ($size == 'original' || array_key_exists($size, $imageSizes)) {
                     $sizeName = $size;
                 } else {
                     return;
                 }
             } else {
                 $sizeName = 'original';
                 foreach ($imageSizes as $sizeKey => $sizeSize) {
                     if ($size <= $sizeSize) {
                         $sizeName = $sizeKey;
                         break;
                     }
                 }
             }
             $originalFolderPath = $baseUserImagePath . $sizeName . '/';
             $contents = IOHelper::getFolderContents($originalFolderPath, false);
             if ($contents) {
                 $originalPath = $contents[0];
             } else {
                 // OK, let's fetch it then
                 $user = craft()->twitter->getUserById($userId);
                 if (!$user || empty($user['profile_image_url'])) {
                     return;
                 }
                 $url = $user['profile_image_url'];
                 if ($sizeName != 'normal') {
                     if ($sizeName == 'original') {
                         $url = str_replace('_normal', '', $url);
                     } else {
                         $url = str_replace('_normal', '_' . $sizeName, $url);
                     }
                 }
                 IOHelper::ensureFolderExists($originalFolderPath);
                 $fileName = pathinfo($url, PATHINFO_BASENAME);
                 $originalPath = $originalFolderPath . $fileName;
                 $response = \Guzzle\Http\StaticClient::get($url, array('save_to' => $originalPath));
                 if (!$response->isSuccessful()) {
                     return;
                 }
             }
             // If they were actually requesting "mini", "normal", "bigger", or "original", we're done
             if (!is_numeric($size)) {
                 return $originalPath;
             }
             // Resize it to the requested size
             $fileName = pathinfo($originalPath, PATHINFO_BASENAME);
             $sizedPath = $sizedFolderPath . $fileName;
             IOHelper::ensureFolderExists($sizedFolderPath);
             craft()->images->loadImage($originalPath)->scaleAndCrop($size, $size)->saveAs($sizedPath);
             return $sizedPath;
         }
     }
 }