예제 #1
0
파일: GSLS.php 프로젝트: sgoendoer/sonic
 /**
  * Pushes an update for a SocialRecord to the GSLS. The SocialRecord will be transformed into a signed JWT, which is then stored in the GSLS
  * 
  * @param $sr The SocialRecord
  * @param $personalPrivateKey The private key to sign the JWT
  * 
  * @throws Exception
  * 
  * @return result json string
  */
 public static function putSocialRecord(SocialRecord $sr, $personalPrivateKey)
 {
     if (!$sr->verify()) {
         throw new \Excetion("Error: Invalid Social Record");
     }
     // create and sign JWT
     $signer = new Sha512();
     $personalPrivateKey = PrivateKey::formatPEM($personalPrivateKey);
     $token = (new Builder())->set('socialRecord', base64_encode($sr->getJSONString()))->sign($signer, $personalPrivateKey)->getToken();
     $ch = curl_init(Configuration::getPrimaryGSLSNode());
     if (Configuration::getCurlVerbose() >= 2) {
         curl_setopt($ch, CURLOPT_VERBOSE, 1);
     }
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, Configuration::getGSLSTimeout());
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-Length: ' . strlen((string) $token)));
     curl_setopt($ch, CURLOPT_POSTFIELDS, (string) $token);
     $result = curl_exec($ch);
     if (curl_errno($ch) != CURLE_OK) {
         $ch = curl_init(Configuration::getSecondaryGSLSNode());
         if (Configuration::getCurlVerbose() >= 2) {
             curl_setopt($ch, CURLOPT_VERBOSE, 1);
         }
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HTTPGET, 1);
         $result = curl_exec($ch);
         if (curl_errno($ch) != CURLE_OK) {
             throw new \Exception('Connection error: ' . curl_error($ch));
         }
     }
     $result = json_decode($result);
     curl_close($ch);
     if ($result->responseCode != 200) {
         throw new \Exception("Error: " . $result->message);
     } else {
         return $result;
     }
 }