Alpha Framework alpha--util
[ class tree: alpha--util ] [ index: alpha--util ] [ all elements ]

Source for file MarkdownFacade.inc

Documentation is available at MarkdownFacade.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/util/AlphaMarkdown.inc';
  4. require_once $config->get('sysRoot').'alpha/view/widgets/Image.inc';
  5.  
  6. /**
  7.  *
  8.  * A facade class for the Markdown library
  9.  * 
  10.  * @package alpha::util
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: MarkdownFacade.inc 1454 2011-12-04 15:14:05Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class MarkdownFacade {
  52.     /**
  53.      * The markdown-format content that we will render
  54.      * 
  55.      * @var string 
  56.      * @since 1.0
  57.      */    
  58.     private $content;
  59.     
  60.     /**
  61.      * The business object that stores the content will be rendered to Markdown
  62.      * 
  63.      * @var AlphaDAO 
  64.      * @since 1.0
  65.      */
  66.     private $BO null;
  67.     
  68.     /**
  69.      * The auto-generated name of the Markdown HTML cache file for the BO
  70.      * 
  71.      * @var string 
  72.      * @since 1.0
  73.      */
  74.     private $filename;
  75.     
  76.     /**
  77.      * The constructor
  78.      * 
  79.      * @param AlphaDAO $BO 
  80.      * @param boolean $useCache 
  81.      * @since 1.0
  82.      */
  83.     public function __construct($BO$useCache true{
  84.         global $config;
  85.         
  86.         $this->BO $BO;
  87.         if($this->BO instanceof ArticleObject && $this->BO->isLoadedFromFile()) {
  88.             $underscoreTimeStamp str_replace(array('-',' ',':')'_'$this->BO->getContentFileDate());
  89.             $this->filename $config->get('sysRoot').'cache/html/'.get_class($this->BO).'_'.$this->BO->get('title').'_'.$underscoreTimeStamp.'.html';
  90.         }else{
  91.             $this->filename $config->get('sysRoot').'cache/html/'.get_class($this->BO).'_'.$this->BO->getID().'_'.$this->BO->getVersion().'.html';
  92.         }
  93.         
  94.         if(!$useCache{
  95.             $this->content $this->markdown($this->BO->get('content'true));
  96.         }else{
  97.             if ($this->checkCache()) {
  98.                 $this->loadCache();
  99.             }else{
  100.                 if($this->BO->get('content'true== ''{
  101.                     // the content may not be loaded from the DB at this stage due to a previous soft-load
  102.                     $this->BO->reload();
  103.                 }
  104.                 
  105.                 $this->content $this->markdown($this->BO->get('content'true));
  106.                 
  107.                 $this->cache();
  108.             }
  109.         }
  110.         
  111.         // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller
  112.         $attachments array();        
  113.         preg_match_all('/href\=\"\$attachURL\/.*\"/'$this->content$attachments);
  114.         
  115.         foreach($attachments[0as $attachmentURL{
  116.             $start strpos($attachmentURL'/');
  117.             $end strrpos($attachmentURL'"');
  118.             $fileName substr($attachmentURL$start+1$end-($start+1));
  119.  
  120.             if(method_exists($this->BO'getAttachmentSecureURL')) {
  121.                 $this->content str_replace($attachmentURL'href="'.$this->BO->getAttachmentSecureURL($fileName).'" rel="nofollow"'$this->content);
  122.             }
  123.         }
  124.         
  125.         // Handle image attachments        
  126.         $attachments array();        
  127.         preg_match_all('/\<img\ src\=\"\$attachURL\/.*\".*\>/'$this->content$attachments);
  128.             
  129.         foreach($attachments[0as $attachmentURL{
  130.             $start strpos($attachmentURL'/');
  131.             $end strrpos($attachmentURL'" alt');
  132.             $fileName substr($attachmentURL$start+1$end-($start+1));
  133.     
  134.             if($config->get('sysCMSImagesWidget')) {
  135.                 // get the details of the source image
  136.                 $path $this->BO->getAttachmentsLocation().'/'.$fileName;
  137.                 $image_details getimagesize($path);
  138.                 $imgType $image_details[2];
  139.                 if($imgType == 1)
  140.                     $type 'gif';
  141.                 elseif($imgType == 2)
  142.                     $type 'jpg';
  143.                 elseif($imgType == 3)
  144.                     $type 'png';
  145.                     
  146.                 $img new Image($path$image_details[0]$image_details[1]$type0.95false(boolean)$config->get('sysCMSImagesWidgetSecure'));
  147.                 
  148.                 $this->content str_replace($attachmentURL$img->renderHTMLLink()$this->content);
  149.             }else{
  150.                 // render a normal image link to the ViewAttachment controller                
  151.                 if(method_exists($this->BO'getAttachmentSecureURL')) {
  152.                     $this->content str_replace($attachmentURL'<img src="'.$this->BO->getAttachmentSecureURL($fileName).'">'$this->content);
  153.                 }
  154.             }
  155.         }
  156.     }
  157.     
  158.     /**
  159.      * Facade method which will invoke our custom markdown class rather than the standard one
  160.      * 
  161.      * @return string 
  162.      * @since 1.0
  163.      */
  164.     public function markdown($text{
  165.          global $config;
  166.          
  167.         // Initialize the parser and return the result of its transform method.
  168.         static $parser;
  169.         
  170.         if (!isset($parser)) {
  171.             $parser_class 'AlphaMarkdown';
  172.             $parser new $parser_class;
  173.         }
  174.         
  175.         /*
  176.          * Replace all instances of $sysURL in the text with the sysURL setting from config
  177.          */
  178.         $text str_replace('$sysURL'$config->get('sysURL')$text);
  179.         
  180.         // transform text using parser.
  181.         return $parser->transform($text);
  182.     }
  183.     
  184.     /**
  185.      * Getter for the content
  186.      * 
  187.      * @return string 
  188.      * @since 1.0
  189.      */
  190.     public function getContent({
  191.         return $this->content;
  192.     }
  193.     
  194.     /**
  195.      * Saves the HTML generated by Markdown to the cache directory
  196.      * 
  197.      * @since 1.0
  198.      */
  199.     private function cache({
  200.         // check to ensure that the article is not transient before caching it
  201.         if ($this->BO->getID(!= '00000000000' || $this->BO->isLoadedFromFile()) {
  202.             $fp=fopen($this->filename,"w");
  203.             if (!$fp{
  204.                 throw new AlphaException('Failed to open the cache file for writing, directory permissions my not be set correctly!');
  205.             }else{
  206.                 flock($fp,2)// locks the file for writting            
  207.                 fwrite($fp,$this->content)
  208.                 flock($fp,3)// unlocks the file
  209.                 fclose($fp)//closes the file
  210.             }
  211.         }
  212.     }
  213.     
  214.     /**
  215.      * Used to check the HTML cache for the BO cache file
  216.      *      
  217.      * @return boolean 
  218.      * @since 1.0
  219.      */
  220.     public function checkCache({
  221.         return file_exists($this->filename);
  222.     }
  223.     
  224.     /**
  225.      * Method to load the content of the cache file to the $content attribute of this object
  226.      * 
  227.      * @since 1.0
  228.      */
  229.     public function loadCache({        
  230.         $fp fopen($this->filename,"r");
  231.         
  232.         if (!$fp{
  233.             throw new AlphaException('Failed to open the cache file for reading, directory permissions my not be set correctly!');
  234.         }else{                    
  235.             $this->content fread($fpfilesize($this->filename));            
  236.             fclose($fp)//closes the file            
  237.         }
  238.     }
  239. }
  240. ?>

Documentation generated on Tue, 13 Dec 2011 20:27:15 +0000 by phpDocumentor 1.4.3