/**
  * @param string $url
  * @param array  $data
  * @param array  $curlOptions
  *
  * @return bool|mixed|\stdClass
  */
 protected function postData($url, $data, $curlOptions = [])
 {
     if (false === $this->checkPayload($data)) {
         return false;
     }
     try {
         if (false === ($_result = Curl::post($url, $data, $curlOptions))) {
             \Log::error('[dfe.license] Network communication error.');
             return false;
         }
         //  Yay?
         if (Response::HTTP_OK == Curl::getLastHttpCode()) {
             return $_result;
         }
         \Log::error('[dfe.license] POST failed with status "' . Curl::getLastHttpCode() . '". Response: ' . print_r($_result, true));
     } catch (\Exception $_ex) {
         \Log::error('[dfe.license] Exception while POSTing: ' . $_ex->getMessage());
     }
     return false;
 }
Example #2
0
 /**
  * @param User  $user
  * @param array $payload
  *
  * @return bool
  */
 public static function registerUser($user, array $payload = [])
 {
     $source = 'Product Install DreamFactory';
     if (env('DF_MANAGED', false)) {
         $serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
         if (false === strpos($serverName, '.enterprise.dreamfactory.com')) {
             return true;
             // bail, not tracking
         }
         $source = 'Website Free Hosted';
     }
     $partner = env('DF_INSTALL', '');
     if (empty($partner) && false !== stripos(env('DB_DATABASE', ''), 'bitnami')) {
         $partner = 'Bitnami';
     }
     $payload = array_merge(['email' => $user->email, 'name' => $user->name, 'firstname' => $user->first_name, 'lastname' => $user->last_name, 'phone' => $user->phone, 'lead_event' => $source, 'lead_source' => $source, 'partner' => $partner, 'product' => 'DreamFactory', 'version' => config('df.version', 'unknown'), 'host_os' => PHP_OS], $payload);
     $payload = json_encode($payload);
     $options = [CURLOPT_HTTPHEADER => ['Content-Type: application/json']];
     if (false !== ($_response = Curl::post(static::ENDPOINT, $payload, $options))) {
         return true;
     }
     return false;
 }
 /**
  * @param string $uri
  * @param array  $payload
  * @param array  $curlOptions
  * @param string $method
  *
  * @return array|bool|\stdClass
  * @throws \DreamFactory\Managed\Exceptions\ManagedInstanceException
  */
 protected function callConsole($uri, $payload = [], $curlOptions = [], $method = Request::METHOD_POST)
 {
     try {
         //  Strip leading double-slash
         '//' == substr($uri, 0, 2) && ($uri = substr($uri, 2));
         //  Allow full URIs or manufacture one...
         'http' != substr($uri, 0, 4) && ($uri = $this->config['console-api-url'] . ltrim($uri, '/ '));
         if (false === ($_result = Curl::request($method, $uri, $this->signPayload($payload), $curlOptions))) {
             throw new \RuntimeException('Failed to contact DFE console');
         }
         if (!$_result instanceof \stdClass) {
             if (is_string($_result) && (false === json_decode($_result) || JSON_ERROR_NONE !== json_last_error())) {
                 throw new \RuntimeException('Invalid response received from DFE console');
             }
         }
         return $_result;
     } catch (\Exception $_ex) {
         throw new ManagedInstanceException('DFE Console API Error: ' . $_ex->getMessage(), $_ex->getCode(), $_ex);
     }
 }
Example #4
0
 /**
  * @param string $lookup
  * @param string $value
  * @param bool   $use_private
  *
  * @returns bool
  */
 public static function getLookupValue($lookup, &$value, $use_private = false)
 {
     if (empty($lookup)) {
         return false;
     }
     $_parts = explode('.', $lookup);
     if (count($_parts) > 1) {
         $_section = array_shift($_parts);
         $_lookup = implode('.', $_parts);
         if (!empty($_section)) {
             switch ($_section) {
                 case 'session':
                     switch ($_lookup) {
                         case 'id':
                         case 'token':
                             $value = static::getSessionToken();
                             return true;
                             //                            case 'ticket':
                             //                                $value = static::_generateTicket();
                             //
                             //                                return true;
                     }
                     break;
                 case 'user':
                 case 'role':
                     // get fields here
                     if (!empty($_lookup)) {
                         $info = static::get($_section);
                         if (isset($info, $info[$_lookup])) {
                             $value = $info[$_lookup];
                             return true;
                         }
                     }
                     break;
                 case 'app':
                     switch ($_lookup) {
                         case 'id':
                             $value = static::get('app.id');
                             return true;
                         case 'api_key':
                             $value = static::getApiKey();
                             return true;
                     }
                     break;
                 case 'df':
                     switch ($_lookup) {
                         case 'host_url':
                             $value = Curl::currentUrl(false, false);
                             return true;
                         case 'name':
                             $value = \Config::get('df.instance_name', gethostname());
                             return true;
                         case 'version':
                             $value = \Config::get('df.version');
                             return true;
                         case 'api_version':
                             $value = \Config::get('df.api_version');
                             return true;
                         case 'confirm_invite_url':
                             $value = url(\Config::get('df.confirm_invite_url'));
                             return true;
                         case 'confirm_register_url':
                             $value = url(\Config::get('df.confirm_register_url'));
                             return true;
                         case 'confirm_reset_url':
                             $value = url(\Config::get('df.confirm_reset_url'));
                             return true;
                     }
                     break;
             }
         }
     }
     $control = $use_private ? 'lookup_secret' : 'lookup';
     $lookups = static::get($control);
     if (isset($lookups, $lookups[$lookup])) {
         $value = $lookups[$lookup];
         return true;
     }
     return false;
 }
 /**
  * @param string      $subGuid
  * @param string|null $partnerEmail Partner registrant email address
  *
  * @return bool|\Illuminate\Http\RedirectResponse
  */
 protected function locateContactBySubmissionGuid($subGuid, $partnerEmail)
 {
     if ('false' !== $subGuid && empty($partnerEmail)) {
         $_url = 'https://api.hubapi.com/contacts/v1/lists/recently_updated/contacts/recent/?hapikey=' . config('marketing.hubspot.api-key') . '&count=50';
         if (false === ($_response = Curl::get($_url))) {
             \Log::debug('[auth.landing-page] recent contact pull failed.');
             return false;
         }
         if (empty($_response) || !$_response instanceof \stdClass || !isset($_response->contacts) || empty($_response->contacts)) {
             //  Methinks thine guid is bogus
             \Log::debug('[auth.landing-page] recent contacts empty or invalid.');
             \Log::debug('[auth.landing-page] * response: ' . print_r($_response, true));
             return false;
         }
         //  Mine for gold...
         $_email = null;
         /**
          * GHA 2015-06-16
          * This has to be the most ridiculous way to get a contact's email address that I've ever seen.
          */
         foreach ($_response->contacts as $_contact) {
             if (isset($_contact->{'form-submissions'})) {
                 foreach ($_contact->{'form-submissions'} as $_sub) {
                     if (isset($_sub->{'conversion-id'}) && $subGuid == $_sub->{'conversion-id'}) {
                         if (isset($_contact->{'identity-profiles'})) {
                             foreach ($_contact->{'identity-profiles'} as $_profile) {
                                 if (isset($_profile->identities)) {
                                     foreach ($_profile->identities as $_identity) {
                                         if (isset($_identity->type) && 'EMAIL' == $_identity->type && isset($_identity->value)) {
                                             $_email = $_identity->value;
                                             break 4;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         //  Didn't find this person out of the last 50? how could he just have been redirected??
         if (empty($_email)) {
             \Log::debug('[auth.landing-page] subGuid "' . $subGuid . '" not found in recents');
             return false;
         }
         \Log::debug('[auth.landing-page] subGuid "' . $subGuid . '" attached with email "' . $_email . '"');
     } else {
         //  Make sure it came from our domain...
         if (null === ($_referrer = \Request::server('HTTP_REFERER')) || false === stripos($_referrer, 'verizon.dreamfactory.com')) {
             \Log::debug('[auth.landing-page] bad referrer "' . $_referrer . '" in auto-login request.');
             return false;
         }
         $_email = $partnerEmail;
         \Log::debug('[auth.landing-page] using partner supplied email "' . $_email . '"');
     }
     //  Lookup email address
     try {
         $_user = User::byEmail($_email)->firstOrFail();
         \Log::debug('[auth.landing-page] subGuid "' . $subGuid . '"/"' . $_email . '" user id#' . $_user->id);
     } catch (ModelNotFoundException $_ex) {
         \Log::debug('[auth.landing-page] subGuid "' . $subGuid . '"/"' . $_email . '" no related user.');
         return false;
     }
     //  Ok, now we have a user, we need to log his ass in...
     /** @noinspection PhpParamsInspection */
     \Auth::login($_user);
     \Log::info('[auth.landing-page] auto-login user "' . $_email . '"');
     \Redirect::to('/auth/login');
     return true;
 }
Example #6
0
 /**
  * @param string $method
  * @param string $url
  * @param mixed  $payload
  * @param array  $curlOptions
  *
  * @return \DreamFactory\Core\Utility\ServiceResponse
  * @throws \DreamFactory\Core\Exceptions\NotImplementedException
  * @throws \DreamFactory\Core\Exceptions\RestException
  */
 protected static function externalRequest($method, $url, $payload = [], $curlOptions = [])
 {
     $result = Curl::request($method, $url, $payload, $curlOptions);
     $contentType = Curl::getInfo('content_type');
     $format = DataFormats::fromMimeType($contentType);
     $status = Curl::getLastHttpCode();
     if ($status >= 300) {
         if (!is_string($result)) {
             $result = json_encode($result);
         }
         throw new RestException($status, $result, $status);
     }
     return ResponseFactory::create($result, $format, $status, $contentType);
 }
 /**
  * Makes a shout out to an instance's private back-end. Should be called bootyCall()  ;)
  *
  * @param string $uri     The REST uri (i.e. "/[rest|api][/v[1|2]]/db", "/rest/system/users", etc.) to retrieve
  *                        from the instance
  * @param array  $payload Any payload to send with request
  * @param array  $options Any options to pass to transport layer
  * @param string $method  The HTTP method. Defaults to "POST"
  *
  * @return array|bool|\stdClass
  */
 public function call($uri, $payload = [], $options = [], $method = Request::METHOD_POST)
 {
     $options[CURLOPT_HTTPHEADER] = array_merge(array_get($options, CURLOPT_HTTPHEADER, []), [EnterpriseDefaults::CONSOLE_X_HEADER . ': ' . $this->token]);
     try {
         $_response = Curl::request($method, Uri::segment([$this->resourceUri, $uri], false), $payload, $options);
     } catch (\Exception $_ex) {
         return false;
     }
     return $_response;
 }
 /**
  * Makes a shout out to an instance's private back-end. Should be called bootyCall()  ;)
  *
  * @param string $uri     The REST uri (i.e. "/[rest|api][/v[1|2]]/db", "/rest/system/users", etc.) to retrieve
  *                        from the instance
  * @param array  $payload Any payload to send with request
  * @param array  $options Any options to pass to transport layer
  * @param string $method  The HTTP method. Defaults to "POST"
  *
  * @return array|bool|\stdClass
  */
 public function call($uri, $payload = [], $options = [], $method = Request::METHOD_POST)
 {
     $options[CURLOPT_HTTPHEADER] = array_merge(array_get($options, CURLOPT_HTTPHEADER, []), $this->headers ?: []);
     if (!empty($payload) && !is_scalar($payload)) {
         $payload = Json::encode($payload);
         $options[CURLOPT_HTTPHEADER] = array_merge(array_get($options, CURLOPT_HTTPHEADER, []), ['Content-Type: application/json']);
     }
     try {
         $_response = Curl::request($method, $this->resourceUri . ltrim($uri, ' /'), $payload, $options);
     } catch (\Exception $_ex) {
         $this->error('[dfe.instance-api-client] ' . $method . ' failure: ' . $_ex->getMessage());
         return false;
     }
     return $_response;
 }
 /**
  * @param string $method
  * @param string $url
  * @param mixed  $payload
  * @param array  $curlOptions
  *
  * @return \stdClass|string
  */
 protected static function externalRequest($method, $url, $payload = [], $curlOptions = [])
 {
     try {
         $result = Curl::request($method, $url, $payload, $curlOptions);
         $result = ResponseFactory::create($result);
     } catch (\Exception $ex) {
         $result = ResponseFactory::create($ex);
         Log::error('Exception: ' . $ex->getMessage(), ['response' => $result]);
     }
     return ResponseFactory::sendScriptResponse($result);
 }
Example #10
0
 /**
  * @throws \DreamFactory\Core\Exceptions\RestException
  * @return bool
  */
 protected function processRequest()
 {
     $data = $this->request->getContent();
     $resource = !empty($this->resourcePath) ? ltrim($this->resourcePath, '/') : null;
     if ($resource) {
         $this->url = rtrim($this->baseUrl, '/') . '/' . $resource;
     } else {
         $this->url = $this->baseUrl;
     }
     if (!empty($this->query)) {
         $splicer = false === strpos($this->baseUrl, '?') ? '?' : '&';
         $this->url .= $splicer . $this->query;
     }
     $cacheKey = '';
     if ($this->cacheEnabled) {
         switch ($this->action) {
             case Verbs::GET:
                 // build cache_key
                 $cacheKey = $this->action . ':' . $this->name;
                 if ($resource) {
                     $cacheKey .= ':' . $resource;
                 }
                 if (!empty($this->cacheQuery)) {
                     $cacheKey .= ':' . $this->cacheQuery;
                 }
                 $cacheKey = hash('sha256', $cacheKey);
                 if (null !== ($result = $this->getFromCache($cacheKey))) {
                     return $result;
                 }
                 break;
         }
     }
     Log::debug('Outbound HTTP request: ' . $this->action . ': ' . $this->url);
     Curl::setDecodeToArray(true);
     $result = Curl::request($this->action, $this->url, $data, $this->curlOptions);
     if (false === $result) {
         $error = Curl::getError();
         throw new RestException(ArrayUtils::get($error, 'code', 500), ArrayUtils::get($error, 'message'));
     }
     $status = Curl::getLastHttpCode();
     if ($status >= 300) {
         if (!is_string($result)) {
             $result = json_encode($result);
         }
         throw new RestException($status, $result, $status);
     }
     $contentType = Curl::getInfo('content_type');
     $format = DataFormats::fromMimeType($contentType);
     $response = ResponseFactory::create($result, $format, $status, $contentType);
     if ($this->cacheEnabled) {
         switch ($this->action) {
             case Verbs::GET:
                 $this->addToCache($cacheKey, $result);
                 break;
         }
     }
     return $response;
 }