Ejemplo n.º 1
0
 /**
  * Method to register custom library.
  *
  * @return  void
  */
 public function onAfterInitialise()
 {
     if (defined('REDCORE_LIBRARY_LOADED')) {
         $apiName = JFactory::getApplication()->input->getString('api');
         if ($this->isApiEnabled($apiName)) {
             $input = JFactory::getApplication()->input;
             if (!empty($apiName)) {
                 try {
                     // We will disable all error messaging from PHP from the output
                     error_reporting(0);
                     ini_set('display_errors', 0);
                     JError::setErrorHandling(E_ERROR, 'message');
                     JFactory::getApplication()->clearHeaders();
                     $webserviceClient = $input->get->getString('webserviceClient', '');
                     $optionName = $input->get->getString('option', '');
                     $optionName = strpos($optionName, 'com_') === 0 ? substr($optionName, 4) : $optionName;
                     $viewName = $input->getString('view', '');
                     $version = $input->getString('webserviceVersion', '');
                     $token = $input->getString(RBootstrap::getConfig('oauth2_token_param_name', 'access_token'), '');
                     $apiName = ucfirst($apiName);
                     $method = strtoupper($input->getMethod());
                     $task = RApiHalHelper::getTask();
                     $data = RApi::getPostedData();
                     $dataGet = $input->get->getArray();
                     if (empty($webserviceClient)) {
                         $webserviceClient = JFactory::getApplication()->isAdmin() ? 'administrator' : 'site';
                     }
                     $options = array('api' => $apiName, 'optionName' => $optionName, 'viewName' => $viewName, 'webserviceVersion' => $version, 'webserviceClient' => $webserviceClient, 'method' => $method, 'task' => $task, 'data' => $data, 'dataGet' => $dataGet, 'accessToken' => $token, 'format' => $input->getString('format', $this->params->get('webservices_default_format', 'json')), 'id' => $input->getString('id', ''), 'absoluteHrefs' => $input->get->getBool('absoluteHrefs', true));
                     // Create instance of Api and fill all required options
                     $api = RApi::getInstance($options);
                     // Run the api task
                     $api->execute();
                     // Display output
                     $api->render();
                 } catch (Exception $e) {
                     $code = $e->getCode() > 0 ? $e->getCode() : 500;
                     if (strtolower($apiName) == 'soap') {
                         // We must have status of 200 for SOAP communication even if it is fault
                         $message = RApiSoapHelper::createSoapFaultResponse($e->getMessage());
                         header("Content-Type: soap+xml");
                         header("Content-length: " . strlen($message));
                         header("Status: 200");
                         echo $message;
                     } else {
                         // Set the server response code.
                         header('Status: ' . $code, true, $code);
                         // Check for defined constants
                         if (!defined('JSON_UNESCAPED_SLASHES')) {
                             define('JSON_UNESCAPED_SLASHES', 64);
                         }
                         // An exception has been caught, echo the message and exit.
                         echo json_encode(array('message' => $e->getMessage(), 'code' => $e->getCode(), 'type' => get_class($e)), JSON_UNESCAPED_SLASHES);
                     }
                 }
                 JFactory::getApplication()->close();
             }
         }
     }
 }