Ejemplo n.º 1
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);
             }
         }
     }
 }