public function index()
 {
     $uri = static::getURI($_SERVER);
     $dist = env('DF_INSTALL', '');
     if (empty($dist) && false !== stripos(env('DB_DATABASE', ''), 'bitnami')) {
         $dist = 'Bitnami';
     }
     $appCount = App::all()->count();
     $adminCount = User::whereIsSysAdmin(1)->count();
     $userCount = User::whereIsSysAdmin(0)->count();
     $serviceCount = Service::all()->count();
     $roleCount = Role::all()->count();
     $status = ["uri" => $uri, "managed" => env('DF_MANAGED', false), "dist" => $dist, "demo" => Environment::isDemoApplication(), "version" => \Config::get('df.version'), "host_os" => PHP_OS, "resources" => ["app" => $appCount, "admin" => $adminCount, "user" => $userCount, "service" => $serviceCount, "role" => $roleCount]];
     return ResponseFactory::sendResponse(ResponseFactory::create($status));
 }
Example #2
0
 /**
  * Handles POST action
  *
  * @return \DreamFactory\Core\Utility\ServiceResponse
  * @throws BadRequestException
  * @throws \Exception
  */
 protected function handlePOST()
 {
     if (!empty($this->resource)) {
         throw new BadRequestException('Create record by identifier not currently supported.');
     }
     $records = ResourcesWrapper::unwrapResources($this->getPayloadData());
     if (empty($records)) {
         throw new BadRequestException('No record(s) detected in request.');
     }
     $this->triggerActionEvent($this->response);
     $model = $this->getModel();
     $result = $model::bulkCreate($records, $this->request->getParameters());
     $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
     $id = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier());
     $result = ResourcesWrapper::cleanResources($result, $asList, $id, ApiOptions::FIELDS_ALL);
     return ResponseFactory::create($result, $this->nativeFormat, ServiceResponseInterface::HTTP_CREATED);
 }
Example #3
0
 /**
  * Handles all service requests
  *
  * @param null|string $version
  * @param string      $service
  * @param null|string $resource
  *
  * @return ServiceResponseInterface|null
  */
 public function handleService($version = null, $service, $resource = null)
 {
     try {
         $service = strtolower($service);
         // fix removal of trailing slashes from resource
         if (!empty($resource)) {
             $uri = \Request::getRequestUri();
             if (false === strpos($uri, '?') && '/' === substr($uri, strlen($uri) - 1, 1) || '/' === substr($uri, strpos($uri, '?') - 1, 1)) {
                 $resource .= '/';
             }
         }
         $response = ServiceHandler::processRequest($version, $service, $resource);
     } catch (\Exception $e) {
         $response = ResponseFactory::create($e);
     }
     if ($response instanceof RedirectResponse) {
         return $response;
     }
     return ResponseFactory::sendResponse($response, null, null, $resource);
 }
Example #4
0
 /**
  * Handles GET action
  *
  * @return array
  * @throws NotFoundException
  */
 protected function handleGET()
 {
     if (empty($this->resource)) {
         $service = $this->request->getParameter('service');
         $type = $this->request->getParameter('type');
         $onlyScripted = $this->request->getParameterAsBool('only_scripted');
         if ($onlyScripted) {
             switch ($type) {
                 case 'process':
                     $scripts = EventScript::where('affects_process', 1)->lists('name')->all();
                     break;
                 case 'broadcast':
                     $scripts = EventScript::where('affects_process', 0)->lists('name')->all();
                     break;
                 default:
                     $scripts = EventScript::lists('name')->all();
                     break;
             }
             return ResourcesWrapper::cleanResources(array_values(array_unique($scripts)));
         }
         $results = $this->getEventMap();
         $allEvents = [];
         switch ($type) {
             case 'process':
                 $results = ArrayUtils::get($results, 'process', []);
                 foreach ($results as $serviceKey => $apis) {
                     if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                         unset($results[$serviceKey]);
                     } else {
                         foreach ($apis as $path => $operations) {
                             foreach ($operations['verb'] as $method => $events) {
                                 $allEvents = array_merge($allEvents, $events);
                             }
                         }
                     }
                 }
                 break;
             case 'broadcast':
                 $results = ArrayUtils::get($results, 'broadcast', []);
                 foreach ($results as $serviceKey => $apis) {
                     if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                         unset($results[$serviceKey]);
                     } else {
                         foreach ($apis as $path => $operations) {
                             foreach ($operations['verb'] as $method => $events) {
                                 $allEvents = array_merge($allEvents, $events);
                             }
                         }
                     }
                 }
                 break;
             default:
                 foreach ($results as $type => $services) {
                     foreach ($services as $serviceKey => $apis) {
                         if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                             unset($results[$type][$serviceKey]);
                         } else {
                             foreach ($apis as $path => $operations) {
                                 foreach ($operations['verb'] as $method => $events) {
                                     $allEvents = array_merge($allEvents, $events);
                                 }
                             }
                         }
                     }
                 }
                 break;
         }
         if (!$this->request->getParameterAsBool(ApiOptions::AS_LIST)) {
             return $results;
         }
         return ResourcesWrapper::cleanResources(array_values(array_unique($allEvents)));
     }
     $related = $this->request->getParameter(ApiOptions::RELATED);
     if (!empty($related)) {
         $related = explode(',', $related);
     } else {
         $related = [];
     }
     //	Single script by name
     $fields = [ApiOptions::FIELDS_ALL];
     if (null !== ($value = $this->request->getParameter(ApiOptions::FIELDS))) {
         $fields = explode(',', $value);
     }
     if (null === ($foundModel = EventScript::with($related)->find($this->resource, $fields))) {
         throw new NotFoundException("Script not found.");
     }
     return ResponseFactory::create($foundModel->toArray(), $this->nativeFormat);
 }
Example #5
0
 /**
  * @param string $method
  * @param string $path
  * @param array  $payload
  * @param array  $curlOptions Additional CURL options for external requests
  *
  * @return array
  */
 public static function inlineRequest($method, $path, $payload = null, $curlOptions = [])
 {
     if (null === $payload || 'null' == $payload) {
         $payload = [];
     }
     if (!empty($curlOptions)) {
         $options = [];
         foreach ($curlOptions as $key => $value) {
             if (!is_numeric($key)) {
                 if (defined($key)) {
                     $options[constant($key)] = $value;
                 }
             }
         }
         $curlOptions = $options;
         unset($options);
     }
     try {
         if ('https:/' == ($protocol = substr($path, 0, 7)) || 'http://' == $protocol) {
             $result = static::externalRequest($method, $path, $payload, $curlOptions);
         } else {
             $result = null;
             $params = [];
             if (false !== ($pos = strpos($path, '?'))) {
                 $paramString = substr($path, $pos + 1);
                 if (!empty($paramString)) {
                     $pArray = explode('&', $paramString);
                     foreach ($pArray as $k => $p) {
                         if (!empty($p)) {
                             $tmp = explode('=', $p);
                             $name = ArrayUtils::get($tmp, 0, $k);
                             $value = ArrayUtils::get($tmp, 1);
                             $params[$name] = urldecode($value);
                         }
                     }
                 }
                 $path = substr($path, 0, $pos);
             }
             if (false === ($pos = strpos($path, '/'))) {
                 $serviceName = $path;
                 $resource = null;
             } else {
                 $serviceName = substr($path, 0, $pos);
                 $resource = substr($path, $pos + 1);
                 //	Fix removal of trailing slashes from resource
                 if (!empty($resource)) {
                     if (false === strpos($path, '?') && '/' === substr($path, strlen($path) - 1, 1) || '/' === substr($path, strpos($path, '?') - 1, 1)) {
                         $resource .= '/';
                     }
                 }
             }
             if (empty($serviceName)) {
                 return null;
             }
             $format = DataFormats::PHP_ARRAY;
             if (!is_array($payload)) {
                 $format = DataFormats::TEXT;
             }
             Session::checkServicePermission($method, $serviceName, $resource, ServiceRequestorTypes::SCRIPT);
             $request = new ScriptServiceRequest($method, $params);
             $request->setContent($payload, $format);
             //  Now set the request object and go...
             $service = ServiceHandler::getService($serviceName);
             $result = $service->handleRequest($request, $resource);
         }
     } catch (\Exception $ex) {
         $result = ResponseFactory::create($ex);
         Log::error('Exception: ' . $ex->getMessage(), ['response' => $result]);
     }
     return ResponseFactory::sendScriptResponse($result);
 }
Example #6
0
 /**
  * @return ServiceResponseInterface
  */
 protected function respond()
 {
     if ($this->response instanceof ServiceResponseInterface) {
         return $this->response;
     } elseif ($this->response instanceof RedirectResponse) {
         return $this->response;
     }
     return ResponseFactory::create($this->response, $this->nativeFormat);
 }
Example #7
0
 /**
  * @param \Exception               $e
  * @param \Illuminate\Http\Request $request
  *
  * @return array|mixed|string
  */
 public static function getException($e, $request)
 {
     $response = ResponseFactory::create($e);
     return ResponseFactory::sendResponse($response);
 }
Example #8
0
 /**
  * Handles POST actions.
  *
  * @return \DreamFactory\Core\Utility\ServiceResponse
  * @throws BadRequestException
  * @throws InternalServerErrorException
  * @throws \Exception
  */
 protected function handlePOST()
 {
     if (empty($this->filePath)) {
         // create folders and files
         // possible file handling parameters
         $extract = $this->request->getParameterAsBool('extract', false);
         $clean = $this->request->getParameterAsBool('clean', false);
         $checkExist = $this->request->getParameterAsBool('check_exist', false);
         $fileNameHeader = $this->request->getHeader('X-File-Name');
         $folderNameHeader = $this->request->getHeader('X-Folder-Name');
         $fileUrl = filter_var($this->request->getParameter('url', ''), FILTER_SANITIZE_URL);
         if (!empty($fileNameHeader)) {
             // html5 single posting for file create
             $result = $this->handleFileContent($this->folderPath, $fileNameHeader, $this->request->getContent(), $this->request->getContentType(), $extract, $clean, $checkExist);
         } elseif (!empty($folderNameHeader)) {
             // html5 single posting for folder create
             $fullPathName = $this->folderPath . $folderNameHeader;
             $content = $this->request->getPayloadData();
             $this->driver->createFolder($this->container, $fullPathName, $content);
             $result = ['name' => $folderNameHeader, 'path' => $fullPathName];
         } elseif (!empty($fileUrl)) {
             // upload a file from a url, could be expandable zip
             $tmpName = null;
             try {
                 $tmpName = FileUtilities::importUrlFileToTemp($fileUrl);
                 $result = $this->handleFile($this->folderPath, '', $tmpName, '', $extract, $clean, $checkExist);
                 @unlink($tmpName);
             } catch (\Exception $ex) {
                 if (!empty($tmpName)) {
                     @unlink($tmpName);
                 }
                 throw $ex;
             }
         } elseif (null !== ($uploadedFiles = $this->request->getFile('files'))) {
             // older html multi-part/form-data post, single or multiple files
             $files = FileUtilities::rearrangePostedFiles($uploadedFiles);
             $result = $this->handleFolderContentFromFiles($files, $extract, $clean, $checkExist);
             $result = ResourcesWrapper::cleanResources($result);
         } else {
             // possibly xml or json post either of files or folders to create, copy or move
             if (!empty($data = ResourcesWrapper::unwrapResources($this->getPayloadData()))) {
                 $result = $this->handleFolderContentFromData($data, $extract, $clean, $checkExist);
                 $result = ResourcesWrapper::cleanResources($result);
             } else {
                 // create folder from resource path
                 $this->driver->createFolder($this->container, $this->folderPath);
                 $result = ['name' => basename($this->folderPath), 'path' => $this->folderPath];
             }
         }
     } else {
         // create the file
         // possible file handling parameters
         $extract = $this->request->getParameterAsBool('extract', false);
         $clean = $this->request->getParameterAsBool('clean', false);
         $checkExist = $this->request->getParameterAsBool('check_exist', false);
         $name = basename($this->filePath);
         $path = dirname($this->filePath);
         $files = $this->request->getFile('files');
         if (empty($files)) {
             // direct load from posted data as content
             // or possibly xml or json post of file properties create, copy or move
             $result = $this->handleFileContent($path, $name, $this->request->getContent(), $this->request->getContentType(), $extract, $clean, $checkExist);
         } else {
             // older html multipart/form-data post, should be single file
             $files = FileUtilities::rearrangePostedFiles($files);
             if (1 < count($files)) {
                 throw new BadRequestException("Multiple files uploaded to a single REST resource '{$name}'.");
             }
             $file = ArrayUtils::get($files, 0);
             if (empty($file)) {
                 throw new BadRequestException("No file uploaded to REST resource '{$name}'.");
             }
             $error = $file['error'];
             if (UPLOAD_ERR_OK == $error) {
                 $result = $this->handleFile($path, $name, $file["tmp_name"], $file['type'], $extract, $clean, $checkExist);
             } else {
                 throw new InternalServerErrorException("Failed to upload file {$name}.\n{$error}");
             }
         }
     }
     return ResponseFactory::create($result, $this->nativeFormat, ServiceResponseInterface::HTTP_CREATED);
 }
Example #9
0
 /**
  * @param \Exception               $e
  * @param \Illuminate\Http\Request $request
  *
  * @return array|mixed|string
  */
 public static function getException($e, $request)
 {
     $response = ResponseFactory::create($e);
     $accepts = explode(',', $request->header('ACCEPT'));
     return ResponseFactory::sendResponse($response, $accepts);
 }
Example #10
0
 /**
  * @return bool|mixed
  * @throws \DreamFactory\Core\Events\Exceptions\ScriptException
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
  */
 protected function processRequest()
 {
     //	Now all actions must be HTTP verbs
     if (!Verbs::contains($this->action)) {
         throw new BadRequestException('The action "' . $this->action . '" is not supported.');
     }
     $logOutput = $this->request->getParameterAsBool('log_output', true);
     $data = ['request' => $this->request->toArray()];
     $data['resource'] = $this->resourcePath;
     $output = null;
     $result = ScriptEngineManager::runScript($this->content, 'script.' . $this->name, $this->engineConfig, $this->scriptConfig, $data, $output);
     if (!empty($output) && $logOutput) {
         \Log::info("Script '{$this->name}' output:" . PHP_EOL . $output . PHP_EOL);
     }
     //  Bail on errors...
     if (is_array($result) && isset($result['script_result'], $result['script_result']['error'])) {
         throw new InternalServerErrorException($result['script_result']['error']);
     }
     if (is_array($result) && isset($result['exception'])) {
         throw new InternalServerErrorException(ArrayUtils::get($result, 'exception', ''));
     }
     //  The script runner should return an array
     if (is_array($result) && isset($result['__tag__'])) {
         $scriptResult = ArrayUtils::get($result, 'script_result', []);
         if (!empty($response = ArrayUtils::get($result, 'response', []))) {
             $content = ArrayUtils::get($response, 'content');
             $status = ArrayUtils::get($response, 'status', ServiceResponseInterface::HTTP_OK);
             $format = ArrayUtils::get($response, 'format', DataFormats::PHP_ARRAY);
             return ResponseFactory::create($content, $format, $status);
         }
         return $scriptResult;
     } else {
         Log::error('  * Script did not return an array: ' . print_r($result, true));
     }
     return $output;
 }
Example #11
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;
 }