public function getContent()
 {
     if ($this->output != '') {
         return $this->objOutput->getObjectRender()->overrideContent($this->output);
     } elseif ($this->imgToDisplay) {
         if (empty($this->imgExtension)) {
             $imginfo = getimagesize($this->imgToDisplay);
             $this->imgExtension = image_type_to_extension($imginfo[2], false);
         }
         $imageResource = false;
         $types = array('jpg' => array('function' => 'imagecreatefromjpeg', 'Content-Type' => 'image/jpeg'), 'jpeg' => array('function' => 'imagecreatefromjpeg', 'Content-Type' => 'image/jpeg'), 'png' => array('function' => 'imagecreatefrompng', 'Content-Type' => 'image/png'), 'gif' => array('function' => 'imagecreatefromgif', 'Content-Type' => 'image/gif'));
         if (array_key_exists($this->imgExtension, $types)) {
             $imageResource = @$types[$this->imgExtension]['function']($this->imgToDisplay);
         }
         if (!$imageResource) {
             throw new WebserviceException(sprintf('Unable to load the image "%s"', str_replace(_PS_ROOT_DIR_, '[SHOP_ROOT_DIR]', $this->imgToDisplay)), array(47, 500));
         } else {
             if (array_key_exists($this->imgExtension, $types)) {
                 $this->objOutput->setHeaderParams('Content-Type', $types[$this->imgExtension]['Content-Type']);
             }
             return file_get_contents($this->imgToDisplay);
         }
     }
 }
Beispiel #2
0
 /**
  * Thanks to the (WebserviceOutputBuilder) WebserviceKey::objOutput
  * Method build the output depend on the WebserviceRequest::outputFormat
  * and set HTTP header parameters.
  *
  * @return array with displaying informations (used in the dispatcher).
  */
 protected function returnOutput()
 {
     $return = array();
     // write headers
     $this->objOutput->setHeaderParams('Access-Time', time())->setHeaderParams('X-Powered-By', 'PrestaShop Webservice')->setHeaderParams('PSWS-Version', _PS_VERSION_)->setHeaderParams('Execution-Time', round(microtime(true) - $this->_startTime, 3));
     $return['type'] = strtolower($this->outputFormat);
     // write this header only now (avoid hackers happiness...)
     if ($this->_authenticated) {
         $this->objOutput->setHeaderParams('PSWS-Version', _PS_VERSION_);
     }
     // If Specific Management is asked
     if ($this->objectSpecificManagement instanceof WebserviceSpecificManagementInterface) {
         try {
             $return['content'] = $this->objectSpecificManagement->getContent();
         } catch (WebserviceException $e) {
             if ($e->getType() == WebserviceException::DID_YOU_MEAN) {
                 $this->setErrorDidYouMean($e->getStatus(), $e->getMessage(), $e->getWrongValue(), $e->getAvailableValues(), $e->getCode());
             } elseif ($e->getType() == WebserviceException::SIMPLE) {
                 $this->setError($e->getStatus(), $e->getMessage(), $e->getCode());
             }
         }
     }
     // for use a general output
     if (!$this->hasErrors() && $this->objectSpecificManagement == null) {
         if (empty($this->objects)) {
             try {
                 $return['content'] = $this->objOutput->getResourcesList($this->keyPermissions);
             } catch (WebserviceException $e) {
                 if ($e->getType() == WebserviceException::DID_YOU_MEAN) {
                     $this->setErrorDidYouMean($e->getStatus(), $e->getMessage(), $e->getWrongValue(), $e->getAvailableValues(), $e->getCode());
                 } elseif ($e->getType() == WebserviceException::SIMPLE) {
                     $this->setError($e->getStatus(), $e->getMessage(), $e->getCode());
                 }
             }
         } else {
             try {
                 if (isset($this->urlSegment[1]) && !empty($this->urlSegment[1])) {
                     $type_of_view = WebserviceOutputBuilder::VIEW_DETAILS;
                 } else {
                     $type_of_view = WebserviceOutputBuilder::VIEW_LIST;
                 }
                 if (in_array($this->method, array('PUT', 'POST'))) {
                     $type_of_view = WebserviceOutputBuilder::VIEW_DETAILS;
                     $this->fieldsToDisplay = 'full';
                 }
                 $return['content'] = $this->objOutput->getContent($this->objects, $this->schemaToDisplay, $this->fieldsToDisplay, $this->depth, $type_of_view);
             } catch (WebserviceException $e) {
                 if ($e->getType() == WebserviceException::DID_YOU_MEAN) {
                     $this->setErrorDidYouMean($e->getStatus(), $e->getMessage(), $e->getWrongValue(), $e->getAvailableValues(), $e->getCode());
                 } elseif ($e->getType() == WebserviceException::SIMPLE) {
                     $this->setError($e->getStatus(), $e->getMessage(), $e->getCode());
                 }
             } catch (Exception $e) {
                 $this->setError(500, $e->getMessage(), $e->getCode());
             }
         }
     }
     // if the output is not enable, delete the content
     // the type content too
     if (!$this->_outputEnabled) {
         if (isset($return['type'])) {
             unset($return['type']);
         }
         if (isset($return['content'])) {
             unset($return['content']);
         }
     } elseif (isset($return['content'])) {
         $this->objOutput->setHeaderParams('Content-Sha1', sha1($return['content']));
     }
     // if errors happends when creating returned xml,
     // the usual xml content is replaced by the nice error handler content
     if ($this->hasErrors()) {
         $this->_outputEnabled = true;
         $return['content'] = $this->objOutput->getErrors($this->errors);
     }
     if (!isset($return['content']) || strlen($return['content']) <= 0) {
         $this->objOutput->setHeaderParams('Content-Type', '');
     }
     $return['headers'] = $this->objOutput->buildHeader();
     restore_error_handler();
     return $return;
 }