Beispiel #1
0
 /**
  * Testing the convertImageURLToPath method.
  *
  * @since 1.0
  */
 public function testConvertImageURLToPath()
 {
     $config = ConfigProvider::getInstance();
     $this->assertEquals('images/testimage.png', Image::convertImageURLToPath($config->get('app.url') . '/images/testimage.png'), 'testing the convertImageURLToPath method');
 }
Beispiel #2
0
 /**
  * Testing that the constructor will call setFilename internally to get up a filename  to store the generated image automatically.
  *
  * @since 1.0
  */
 public function testConstructorSetFilename()
 {
     $config = ConfigProvider::getInstance();
     $this->assertEquals($config->get('app.file.store.dir') . 'cache/images/accept_16x16.png', $this->img->getFilename(), 'testing that the constructor will call setFilename internally to get up a filename  to store the generated image automatically');
 }
Beispiel #3
0
 /**
  * Overrides the TCPDF::Image method to decrypt encrypted $file paths from the Image widget, then pass
  * them to the normal TCPDF::Image along with all of the other (unmodified) parameters.
  *
  * @param string $file    Name of the file containing the image.
  * @param float  $x       Abscissa of the upper-left corner.
  * @param float  $y       Ordinate of the upper-left corner.
  * @param float  $w       Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  * @param float  $h       Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  * @param string $type    Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
  * @param mixed  $link    URL or identifier returned by AddLink().
  * @param string $align   Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  * @param bool   $resize  If true resize (reduce) the image to fit $w and $h (requires GD library).
  * @param int    $dpi     dot-per-inch resolution used on resize
  * @param string $palign  Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  * @param bool   $ismask  true if this image is a mask, false otherwise
  * @param mixed  $imgmask image object returned by this function or false
  * @param mixed  $border  Indicates if borders must be drawn around the image. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
  *
  * @since 1.0
  */
 public function Image($file, $x = '', $y = '', $w = 0, $h = 0, $type = '', $link = '', $align = '', $resize = false, $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border = 0)
 {
     if (self::$logger == null) {
         self::$logger = new Logger('TCPDF');
     }
     $config = ConfigProvider::getInstance();
     self::$logger->debug('Processing image file URL [' . $file . ']');
     try {
         if (mb_strpos($file, '/tk/') !== false) {
             $start = mb_strpos($file, '/tk/') + 3;
             $end = mb_strlen($file);
             $tk = mb_substr($file, $start + 1, $end - ($start + 1));
             $decoded = FrontController::getDecodeQueryParams($tk);
             parent::Image($decoded['source'], $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
         } else {
             // it has no query string, so threat as a regular image URL
             if (Validator::isURL($file)) {
                 parent::Image($config->get('app.root') . '/' . Image::convertImageURLToPath($file), $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
             } else {
                 parent::Image($file, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
             }
         }
     } catch (\Exception $e) {
         self::$logger->error('Error processing image file URL [' . $file . '], error [' . $e->getMessage() . ']');
         throw $e;
     }
 }
Beispiel #4
0
 /**
  * The constructor.
  *
  * @param Alpha\Model\ActiveRecord $BO
  * @param bool                     $useCache
  *
  * @since 1.0
  */
 public function __construct($BO, $useCache = true)
 {
     $config = ConfigProvider::getInstance();
     $this->BO = $BO;
     if ($this->BO instanceof \Alpha\Model\Article && $this->BO->isLoadedFromFile()) {
         $underscoreTimeStamp = str_replace(array('-', ' ', ':'), '_', $this->BO->getContentFileDate());
         $this->filename = $config->get('app.file.store.dir') . 'cache/html/' . get_class($this->BO) . '_' . $this->BO->get('title') . '_' . $underscoreTimeStamp . '.html';
     } else {
         $this->filename = $config->get('app.file.store.dir') . 'cache/html/' . get_class($this->BO) . '_' . $this->BO->getID() . '_' . $this->BO->getVersion() . '.html';
     }
     if (!$useCache) {
         $this->content = $this->markdown($this->BO->get('content', true));
     } else {
         if ($this->checkCache()) {
             $this->loadCache();
         } else {
             if ($this->BO->get('content', true) == '') {
                 // the content may not be loaded from the DB at this stage due to a previous soft-load
                 $this->BO->reload();
             }
             $this->content = $this->markdown($this->BO->get('content', true));
             $this->cache();
         }
     }
     // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller
     $attachments = array();
     preg_match_all('/href\\=\\"\\$attachURL\\/.*\\"/', $this->content, $attachments);
     foreach ($attachments[0] as $attachmentURL) {
         $start = mb_strpos($attachmentURL, '/');
         $end = mb_strrpos($attachmentURL, '"');
         $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1));
         if (method_exists($this->BO, 'getAttachmentSecureURL')) {
             $this->content = str_replace($attachmentURL, 'href="' . $this->BO->getAttachmentSecureURL($fileName) . '" rel="nofollow"', $this->content);
         }
     }
     // Handle image attachments
     $attachments = array();
     preg_match_all('/\\<img\\ src\\=\\"\\$attachURL\\/.*\\.[a-zA-Z]{3}\\"[^<]*/', $this->content, $attachments);
     foreach ($attachments[0] as $attachmentURL) {
         preg_match('/\\/.*\\.[a-zA-Z]{3}/', $attachmentURL, $matches);
         $fileName = $matches[0];
         if ($config->get('cms.images.widget')) {
             // get the details of the source image
             $path = $this->BO->getAttachmentsLocation() . $fileName;
             $image_details = getimagesize($path);
             $imgType = $image_details[2];
             if ($imgType == 1) {
                 $type = 'gif';
             } elseif ($imgType == 2) {
                 $type = 'jpg';
             } elseif ($imgType == 3) {
                 $type = 'png';
             }
             $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (bool) $config->get('cms.images.widget.secure'));
             $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content);
         } else {
             // render a normal image link to the ViewAttachment controller
             if (method_exists($this->BO, 'getAttachmentSecureURL')) {
                 $this->content = str_replace($attachmentURL, '<img src="' . $this->BO->getAttachmentSecureURL($fileName) . '">', $this->content);
             }
         }
     }
 }
Beispiel #5
0
 /**
  * The constructor.
  *
  * @param Alpha\Model\ActiveRecord $BO the business object that stores the content will be rendered to Markdown
  *
  * @since 1.0
  */
 public function __construct($BO)
 {
     self::$logger = new Logger('TCPDFFacade');
     self::$logger->debug('>>__construct()');
     $config = ConfigProvider::getInstance();
     $this->BO = $BO;
     $reflect = new \ReflectionClass($this->BO);
     $classname = $reflect->getShortName();
     $this->PDFFilename = $config->get('app.file.store.dir') . 'cache/pdf/' . $classname . '_' . $this->BO->getID() . '_' . $this->BO->getVersion() . '.pdf';
     $PDFDownloadName = str_replace(' ', '_', $this->BO->get('title') . '.pdf');
     $this->HTMLFilename = $config->get('app.file.store.dir') . 'cache/html/' . $classname . '_' . $this->BO->getID() . '_' . $this->BO->getVersion() . '.html';
     // first check the PDF cache
     if ($this->checkPDFCache()) {
         return;
     }
     if (method_exists($this->BO, 'getAttachmentsURL')) {
         $attachURL = $this->BO->getAttachmentsURL();
     } else {
         $attachURL = '';
     }
     if ($this->checkHTMLCache()) {
         $this->loadHTMLCache();
     } else {
         $this->content = $this->markdown($this->BO->get('content', true), $attachURL);
         $this->HTMLCache();
     }
     // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller
     $attachments = array();
     preg_match_all('/href\\=\\"\\$attachURL\\/.*\\"/', $this->content, $attachments);
     foreach ($attachments[0] as $attachmentURL) {
         $start = mb_strpos($attachmentURL, '/');
         $end = mb_strrpos($attachmentURL, '"');
         $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1));
         if (method_exists($this->BO, 'getAttachmentSecureURL')) {
             $this->content = str_replace($attachmentURL, 'href=' . $this->BO->getAttachmentSecureURL($fileName), $this->content);
         }
     }
     // Handle image attachments
     $attachments = array();
     preg_match_all('/\\<img\\ src\\=\\"\\$attachURL\\/.*\\".*\\>/', $this->content, $attachments);
     foreach ($attachments[0] as $attachmentURL) {
         $start = mb_strpos($attachmentURL, '/');
         $end = mb_strrpos($attachmentURL, '" alt');
         $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1));
         if ($config->get('cms.images.widget')) {
             // get the details of the source image
             $path = $this->BO->getAttachmentsLocation() . '/' . $fileName;
             $image_details = getimagesize($path);
             $imgType = $image_details[2];
             if ($imgType == 1) {
                 $type = 'gif';
             } elseif ($imgType == 2) {
                 $type = 'jpg';
             } elseif ($imgType == 3) {
                 $type = 'png';
             }
             $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (bool) $config->get('cms.images.widget.secure'));
             $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content);
         } else {
             // render a normal image link to the ViewAttachment controller
             if (method_exists($this->BO, 'getAttachmentSecureURL')) {
                 $this->content = str_replace($attachmentURL, '<img src="' . $this->BO->getAttachmentSecureURL($fileName) . '">', $this->content);
             }
         }
     }
     $this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
     $this->pdf->SetCreator(PDF_CREATOR);
     $this->pdf->SetAuthor($this->BO->get('author'));
     $this->pdf->SetTitle($this->BO->get('title'));
     $this->pdf->SetSubject($this->BO->get('description'));
     //set margins
     $this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
     $this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
     $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
     //set auto page breaks
     $this->pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
     //set image scale factor
     $this->pdf->setImageScale(2.5);
     // add a page
     $this->pdf->AddPage();
     // add the title
     $title = '<h1>' . $this->BO->get('title') . '</h1>';
     // add some custom footer info about the article
     $footer = '<br><p>Article URL: <a href="' . $this->BO->get('URL') . '">' . $this->BO->get('URL') . '</a><br>Title: ' . $this->BO->get('title') . '<br>Author: ' . $this->BO->get('author') . '</p>';
     // write the title
     self::$logger->debug('Writing the title [' . $title . '] to the PDF');
     $this->pdf->writeHTML(utf8_encode($title), true, false, true, false, '');
     // output the HTML content
     self::$logger->debug('Writing the content [' . $this->content . '] to the PDF');
     $this->pdf->writeHTML(utf8_encode($this->content), true, false, true, false, '');
     // write the article footer
     $this->pdf->writeHTML(utf8_encode($footer), true, false, true, false, '');
     self::$logger->debug('Writing the footer [' . $footer . '] to the PDF');
     // save this PDF to the cache
     $this->pdf->Output($this->PDFFilename, 'F');
     self::$logger->debug('<<__construct()');
 }
Beispiel #6
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);
 }