예제 #1
0
 public function __construct($message = null, $code = 0, \Exception $previous = null)
 {
     parent::__construct($message, $code, $previous);
     if (Configuration::getVerbose() >= 1) {
         Sonic::getLogger()->addError($message);
     }
 }
예제 #2
0
 public function __construct($expectedGID = NULL)
 {
     $this->headers = array();
     $this->headers[SONIC_HEADER__RANDOM] = Random::getRandom();
     $this->headers[SONIC_HEADER__DATE] = XSDDateTime::getXSDDatetime();
     $this->headers[SONIC_HEADER__TARGET_API] = SONIC_SDK__API_VERSION;
     $this->headers[SONIC_HEADER__PLATFORM_GID] = Sonic::getPlatformGlobalID();
     $this->headers[SONIC_HEADER__SOURCE_GID] = Sonic::getContextGlobalID();
 }
예제 #3
0
 public function dispatch()
 {
     if (!$this->initialized) {
         throw new \Exception('ResponseBuilder not initialized. Call init() first');
     }
     $this->response->setResponseBody($this->sonicResponse->getJSONString());
     //echo "verifying response with key from GID ".Sonic::getContextGlobalID();
     $this->response->signResponse(Sonic::getContextAccountKeyPair()->getPrivateKey());
     $this->response->send();
 }
예제 #4
0
 /**
  * Retrieves a SocialRecord for a given GID from the GSLS or local cache
  * throws Exception
  * 
  * @param $globalID String The globalID to resolve
  * @param $skipCache Boolean Determin whether caching should be skipped or not
  *
  * @return SocialRecord if social record exists. Otherwise, an exception is thrown
  */
 public static function retrieveSocialRecord($globalID, $skipCache = false)
 {
     if (Sonic::socialRecordCachingEnabled() === true && $skipCache === false) {
         $sr = Sonic::getSocialRecordCaching()->getSocialRecordFromCache($globalID);
         if ($sr !== false) {
             $sr->verify();
             return $sr;
         }
     }
     return GSLS::getSocialRecord($globalID);
 }
예제 #5
0
파일: Random.php 프로젝트: sgoendoer/sonic
 /**
  * creates a random character sequence of length $length. The created value is checked against already used values for this account to guarantee uniquenes. The newly created value is then registered via the UniqueIDManager of the SDK.
  * 
  * @param $length integer. Defaults to 8
  * @return string
  */
 public static function getUniqueRandom($length = 16)
 {
     $id = self::getRandom($length);
     if (Sonic::getUniqueIDManager() != NULL) {
         while (Sonic::getUniqueIDManager()->isIDRegistered($id) === true) {
             $id = self::getRandom($length);
         }
         Sonic::getUniqueIDManager()->registerID($id);
     }
     return $id;
 }
예제 #6
0
파일: UOID.php 프로젝트: sgoendoer/sonic
 /**
  * Creates a new UOID for the current Sonic context
  * 
  * @param $gid String The global id part of the UOID
  * @param $id String The local id part of the UOID
  * 
  * @return A UOID
  */
 public static function createUOID($gid = NULL, $id = NULL)
 {
     if ($gid == NULL) {
         $gid = Sonic::getContextGlobalID();
     }
     if ($id == NULL) {
         $id = Random::getUniqueRandom();
     }
     $uoid = $gid . UOID::SEPARATOR . $id;
     return $uoid;
 }
예제 #7
0
 /**
  * constructor for building a Sonic compliant HTTP response.
  */
 public function __construct()
 {
     $this->headers = array();
     $this->headers[SONIC_HEADER__RANDOM] = Random::getRandom();
     $this->headers[SONIC_HEADER__DATE] = XSDDateTime::getXSDDatetime();
     $this->headers[SONIC_HEADER__TARGET_API] = SONIC_SDK__API_VERSION;
     $this->headers[SONIC_HEADER__PLATFORM_GID] = Sonic::getPlatformGlobalID();
     $this->headers[SONIC_HEADER__SOURCE_GID] = Sonic::getContextGlobalID();
     $this->statusCode = 200;
     $this->statusMessage = 'OK';
 }
예제 #8
0
 public function __construct()
 {
     $this->platformSR = SocialRecordManager::importSocialRecord($this->platformSRjson);
     $this->platformSocialRecord = $this->platformSR['socialRecord'];
     $this->platformAccountKeyPair = $this->platformSR['accountKeyPair'];
     $this->platformPersonalKeyPair = $this->platformSR['personalKeyPair'];
     $this->aliceSR = SocialRecordManager::importSocialRecord($this->aliceSRjson);
     $this->aliceSocialRecord = $this->aliceSR['socialRecord'];
     $this->aliceAccountKeyPair = $this->aliceSR['accountKeyPair'];
     $this->alicePersonalKeyPair = $this->aliceSR['personalKeyPair'];
     $this->sonic = Sonic::initInstance(new EntityAuthData($this->platformSocialRecord, $this->platformAccountKeyPair, $this->platformPersonalKeyPair));
     Sonic::setUserAuthData(new EntityAuthData($this->aliceSocialRecord, $this->aliceAccountKeyPair));
     Sonic::setContext(Sonic::CONTEXT_USER);
 }
예제 #9
0
 public function testSonic()
 {
     $platformSR = SocialRecordManager::importSocialRecord($this->platformSRjson);
     $platformSocialRecord = $platformSR['socialRecord'];
     $platformAccountKeyPair = $platformSR['accountKeyPair'];
     $platformPersonalKeyPair = $platformSR['personalKeyPair'];
     $aliceSR = SocialRecordManager::importSocialRecord($this->aliceSRjson);
     $aliceSocialRecord = $aliceSR['socialRecord'];
     $aliceAccountKeyPair = $aliceSR['accountKeyPair'];
     $alicePersonalKeyPair = $aliceSR['personalKeyPair'];
     $sonic = Sonic::initInstance(new EntityAuthData($platformSocialRecord, $platformAccountKeyPair, $platformPersonalKeyPair));
     $this->assertEquals($platformSocialRecord->getGlobalID(), Sonic::getContextGlobalID());
     Sonic::setUserAuthData(new EntityAuthData($aliceSocialRecord, $aliceAccountKeyPair));
     Sonic::setContext(Sonic::CONTEXT_USER);
     $this->assertEquals($aliceSocialRecord->getGlobalID(), Sonic::getContextGlobalID());
 }
예제 #10
0
 public static function performPOSTLikeRequest($targetedGID, $likedContentID)
 {
     // create an instance of PersonRequestBuilder
     $likeRequest = new LikeRequestBuilder($targetedGID);
     // create the LIKE object for the content to be liked
     $likeObject = (new LikeObjectBuilder())->objectID(UOID::createUOID())->targetID($likedContentID)->author(Sonic::getUserGlobalID())->build();
     // perform the request
     $response = $likeRequest->createGETLike($likeObject)->dispatch();
     // to access contents of the response, use
     // $response->getPayload(); <-- the actual object data
     // $response->getResponseBody(); <-- the complete response body
     if ($response->getResponseStatusCode() != 200) {
         // in case the request returned something else thatn a 200
         throw new \Exception('Request failed with status code ' . $response->getResponseStatusCode());
     } else {
         // request was performed successfully
         return true;
     }
 }
예제 #11
0
<?php

namespace sgoendoer\Sonic\examples;

require_once __DIR__ . '/../vendor/autoload.php';
use sgoendoer\Sonic\Sonic;
use sgoendoer\Sonic\Identity\EntityAuthData;
use sgoendoer\Sonic\Identity\SocialRecordManager;
try {
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // importing SocialRecord objects to work with
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // load SocialRecords from files to instatiaze the Sonic framework
    $srp = SocialRecordManager::importSocialRecord(file_get_contents(__DIR__ . '/data/SRPlatform.json'));
    $platformSocialRecord = $srp['socialRecord'];
    $platformAccountKeyPair = $srp['accountKeyPair'];
    $platformPersonalKeyPair = $srp['personalKeyPair'];
    $sra = SocialRecordManager::importSocialRecord(file_get_contents(__DIR__ . '/data/SRAlice.json'));
    $userSocialRecord = $sra['socialRecord'];
    $userAccountKeyPair = $sra['accountKeyPair'];
    $userPersonalKeyPair = $sra['personalKeyPair'];
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // initializing Sonic SDK
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // instantiaze the Sonic framework with the platform's SocialRecord
    $sonic = Sonic::initInstance(new EntityAuthData($platformSocialRecord, $platformAccountKeyPair, $platformPersonalKeyPair));
    Sonic::setUserAuthData(new EntityAuthData($userSocialRecord, $userAccountKeyPair));
    Sonic::setContext(Sonic::CONTEXT_USER);
} catch (\Exception $e) {
    die($e->getMessage() . "\n\n" . $e->getTraceAsString());
}
예제 #12
0
 public function dispatch()
 {
     $this->request->signRequest(Sonic::getContextAccountKeyPair()->getPrivateKey());
     $this->response = new IncomingResponse($this->request->send(), $this->targetGID);
     return $this->response;
 }
예제 #13
0
     $response->dispatch();
     die;
 }
 // check access permissions for the addressed interface
 if (!Sonic::getAccessControlManager()->hasInterfaceAccessPriviledges($request->getHeaderSourceGID(), $resource, $request->getMethod())) {
     throw new AccessControlException();
 }
 switch (strtolower($resource)) {
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     // resource PERSON
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     case 'person':
         if ($request->getMethod() == 'GET') {
             $personObject = PersonObjectBuilder::buildFromJSON(file_get_contents(__DIR__ . '/data/AlicePerson.json'));
             // check access permissions for the content object
             if (!Sonic::getAccessControlManager()->hasContentAccessPriviledges($request->getHeaderSourceGID(), $personObject->getObjectID())) {
                 throw new AccessControlException();
             }
             $response = new ResponseBuilder(200);
             $response->init()->setBody($personObject->getJSON());
             $response->dispatch();
         } else {
             throw new MethodNotAllowedException();
         }
         break;
     case 'like':
         if ($request->getMethod() == 'POST') {
             // building LIKE object from request body data
             $likeObject = LikeObjectBuilder::buildFromJSON($request->getBody());
             // here, we would store the received object in the database
             $response = new ResponseBuilder(200);
예제 #14
0
 public function testSearch()
 {
     $searchQuery = (new SearchQueryObjectBuilder())->initiatingGID(Sonic::getContextGlobalID())->query((new ESQueryBuilder())->type('profile')->match('displayName', 'alice')->build())->datetime()->build();
     $this->assertTrue($searchQuery->validate());
     $this->assertEquals($searchQuery, SearchQueryObjectBuilder::buildFromJSON($searchQuery->getJSONString()));
     $profile = (new ProfileObjectBuilder())->globalID(Sonic::getContextGlobalID())->displayName($this->aliceSocialRecord->getDisplayName())->param('x', 'y')->build();
     $searchResult = (new SearchResultObjectBuilder())->targetID($searchQuery->getObjectID())->resultOwnerGID($profile->getGlobalID())->resultObjectID($profile->getObjectID())->resultIndex($searchQuery->getQuery()->getIndex())->resultType($searchQuery->getQuery()->getType())->displayName($profile->getDisplayName())->datetime()->build();
     $this->assertTrue($searchResult->validate());
     $this->assertEquals($searchResult, SearchResultObjectBuilder::buildFromJSON($searchResult->getJSONString()));
     $searchResultCollection = (new SearchResultCollectionObjectBuilder())->objectID(UOID::createUOID($this->platformSocialRecord->getGlobalID()))->targetID($searchQuery->getObjectID())->platformGID($this->platformSocialRecord->getGlobalID())->datetime()->result($searchResult)->build();
     $this->assertTrue($searchResultCollection->validate());
     $this->assertEquals($searchResultCollection, SearchResultCollectionObjectBuilder::buildFromJSON($searchResultCollection->getJSONString()));
 }