예제 #1
0
 /**
  * Post update to user Wall
  *
  *
  * @param \Lampcms\Interfaces\FacebookUser $User
  * @param                                  $aData
  *
  * @throws \InvalidArgumentException
  * @throws \Lampcms\FacebookApiException
  * @internal param array $mixed $aData | string can provide just
  *           a string it will be posted to Facebook User's Wall as a message
  *           it can contain some html code - it's up to Facebook to allow
  *           or disallow certain html tags
  *
  * @return mixed if successful post to Facebook API
  *           then it will return the string returned by API
  *           This could be raw string of json data - not json decoded yet
  *           or false in case there were some errors
  *
  */
 public function postUpdate(\Lampcms\Interfaces\FacebookUser $User, $aData)
 {
     if (!is_string($aData) && !is_array($aData)) {
         throw new \InvalidArgumentException('Invalid data type of $aData: ' . \gettype($aData));
     }
     $Curl = new \Lampcms\Curl();
     $aData = \is_array($aData) ? $aData : array('message' => $aData);
     $facebookUid = $User->getFacebookUid();
     $facebookToken = $User->getFacebookToken();
     d('$facebookUid: ' . $facebookUid . ' $facebookToken: ' . $facebookToken);
     if (empty($facebookUid) || empty($facebookToken)) {
         d('User is not connected with Facebook');
         return false;
     }
     $aData['access_token'] = $User->getFacebookToken();
     d('$aData: ' . \json_encode($aData));
     $url = \sprintf(self::WALL_URL, $facebookUid);
     d('cp url: ' . $url);
     try {
         $Curl->getDocument($url, null, null, array('formVars' => $aData))->checkResponse();
         $retCode = $Curl->getHttpResponseCode();
         $body = $Curl->getResponseBody();
         d('retCode: ' . $retCode . ' resp: ' . $body);
         return $body;
     } catch (\Lampcms\HttpTimeoutException $e) {
         d('Request to Facebook server timed out');
         throw new FacebookApiException('Request to Facebook server timed out. Please try again later');
     } catch (\Lampcms\Http401Exception $e) {
         d('Unauthorized to get data from Facebook, most likely user unjoined the site');
         $User->revokeFacebookConnect();
         throw new FacebookApiException('Authorized with Facebook');
     } catch (\Lampcms\HttpResponseCodeException $e) {
         if (function_exists('e')) {
             e('LampcmsError Facebook response exception: ' . $e->getHttpCode() . ' ' . $e->getMessage() . ' body: ' . $Curl->getResponseBody());
         }
         /**
          * The non-200 response code means there is some kind
          * of error, maybe authorization failed or something like that,
          * or maybe Facebook server was acting up,
          * in this case it is better to delete cookies
          * so that we don't go through these steps again.
          * User will just have to re-do the login fir GFC step
          */
         throw new FacebookApiException('Error during authentication with Facebook server');
     } catch (\Exception $e) {
         if (function_exists('e')) {
             e('Unable to post: ' . $e->getMessage() . ' code: ' . $e->getCode());
         }
     }
     d('cp');
     return false;
 }
예제 #2
0
 /**
  * Get user info data from GOOGLE API using access_token
  * Sets  $this->userInfo array
  *
  * @return object $this
  *
  * @throws \Lampcms\AlertException if something goes wrong during CURL call
  * or if unable to json_decode the response
  */
 protected function getUserInfo()
 {
     $url = \sprintf(self::USERINFO_URL, $this->tokenObject->access_token);
     $Curl = new \Lampcms\Curl();
     try {
         d('Calling userinfo url: ' . $url);
         $Response = $Curl->get($url);
         $body = $Response->getBody();
         d('received response from API. HTTP CODE: ' . $Response->getCode() . ' body: ' . $body);
     } catch (\Exception $e) {
         e('Unable to get userinfo from ' . $url . ' Exception: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on ' . $e->getLine());
         throw new \Lampcms\AlertException('Unexpected response received from Google API');
     }
     if (null === ($this->userInfo = \json_decode($body, true))) {
         e('Unable to json_decode response body: ' . $body);
         throw new \Lampcms\AlertException('Unexpected response format received from Google API');
     }
     return $this;
 }