Esempio n. 1
0
 /**
  * Handles get requests.
  *
  * @param Alpha\Util\Http\Request $request
  *
  * @return Alpha\Util\Http\Response
  *
  * @since 1.0
  *
  * @throws Alpha\Exception\ResourceNotFoundException
  * @throws Alpha\Exception\ResourceNotAllowedException
  */
 public function doGet($request)
 {
     self::$logger->debug('>>doGet(request=[' . var_export($request, true) . '])');
     $config = ConfigProvider::getInstance();
     $params = $request->getParams();
     try {
         $imgSource = urldecode($params['source']);
         $imgWidth = $params['width'];
         $imgHeight = $params['height'];
         $imgType = $params['type'];
         $imgQuality = (double) $params['quality'];
         $imgScale = new Boolean($params['scale']);
         $imgSecure = new Boolean($params['secure']);
     } catch (\Exception $e) {
         self::$logger->error('Required param missing for ImageController controller[' . $e->getMessage() . ']');
         throw new ResourceNotFoundException('File not found');
     }
     $modified = filemtime($imgSource);
     $responseHeaders = array();
     $responseHeaders['Last-Modified'] = date('D, d M Y H:i:s', $modified) . ' GMT';
     $responseHeaders['Cache-Control'] = 'max-age=1800';
     // exit if not modified
     if ($request->getHeader('If-Modified-Since') != null) {
         if (strtotime($request->getHeader('If-Modified-Since')) == $modified) {
             return new Response(304, '', $responseHeaders);
         }
     }
     // handle secure tokens
     if ($imgSecure->getBooleanValue() && $config->get('cms.images.widget.secure')) {
         $valid = $this->checkSecurityFields();
         // if not valid, just return a blank black image of the same dimensions
         if (!$valid) {
             $im = imagecreatetruecolor($imgWidth, $imgHeight);
             $bgc = imagecolorallocate($im, 0, 0, 0);
             imagefilledrectangle($im, 0, 0, $imgWidth, $imgHeight, $bgc);
             if ($imgSource == 'png' && $config->get('cms.images.perserve.png')) {
                 ob_start();
                 imagepng($im);
                 $body = ob_get_contents();
                 $contentType = 'image/png';
                 ob_end_clean();
             } else {
                 ob_start();
                 imagejpeg($im);
                 $body = ob_get_contents();
                 $contentType = 'image/jpeg';
                 ob_end_clean();
             }
             imagedestroy($im);
             self::$logger->warn('The client [' . $request->getUserAgent() . '] was blocked from accessing the file [' . $imgSource . '] due to bad security tokens being provided');
             $responseHeaders['Content-Type'] = $contentType;
             return new Response(200, $body, $responseHeaders);
         }
     }
     try {
         $image = new Image($imgSource, $imgWidth, $imgHeight, $imgType, $imgQuality, $imgScale->getBooleanValue(), $imgSecure->getBooleanValue());
         ob_start();
         $image->renderImage();
         $body = ob_get_contents();
         ob_end_clean();
     } catch (IllegalArguementException $e) {
         self::$logger->error($e->getMessage());
         throw new ResourceNotFoundException('File not found');
     }
     self::$logger->debug('<<__doGet');
     if ($imgSource == 'png' && $config->get('cms.images.perserve.png')) {
         $responseHeaders['Content-Type'] = 'image/png';
     } else {
         $responseHeaders['Content-Type'] = 'image/jpeg';
     }
     return new Response(200, $body, $responseHeaders);
 }