Source for file MarkdownFacade.inc
Documentation is available at MarkdownFacade.inc
require_once $config->get('sysRoot'). 'alpha/util/AlphaMarkdown.inc';
require_once $config->get('sysRoot'). 'alpha/view/widgets/Image.inc';
* A facade class for the Markdown library
* @author John Collins <dev@alphaframework.org>
* @version $Id: MarkdownFacade.inc 1454 2011-12-04 15:14:05Z johnc $
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* The markdown-format content that we will render
* The business object that stores the content will be rendered to Markdown
* The auto-generated name of the Markdown HTML cache file for the BO
* @param boolean $useCache
if($this->BO instanceof ArticleObject && $this->BO->isLoadedFromFile()) {
$underscoreTimeStamp = str_replace(array('-',' ',':'), '_', $this->BO->getContentFileDate());
$this->filename = $config->get('sysRoot'). 'cache/html/'. get_class($this->BO). '_'. $this->BO->get('title'). '_'. $underscoreTimeStamp. '.html';
$this->filename = $config->get('sysRoot'). 'cache/html/'. get_class($this->BO). '_'. $this->BO->getID(). '_'. $this->BO->getVersion(). '.html';
$this->content = $this->markdown($this->BO->get('content', true));
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->content = $this->markdown($this->BO->get('content', true));
// Replace all instances of $attachURL in link tags to links to the ViewAttachment controller
preg_match_all('/href\=\"\$attachURL\/.*\"/', $this->content, $attachments);
foreach($attachments[0] as $attachmentURL) {
$start = strpos($attachmentURL, '/');
$end = strrpos($attachmentURL, '"');
$fileName = substr($attachmentURL, $start+ 1, $end- ($start+ 1));
$this->content = str_replace($attachmentURL, 'href="'. $this->BO->getAttachmentSecureURL($fileName). '" rel="nofollow"', $this->content);
// Handle image attachments
preg_match_all('/\<img\ src\=\"\$attachURL\/.*\".*\>/', $this->content, $attachments);
foreach($attachments[0] as $attachmentURL) {
$start = strpos($attachmentURL, '/');
$end = strrpos($attachmentURL, '" alt');
$fileName = substr($attachmentURL, $start+ 1, $end- ($start+ 1));
if($config->get('sysCMSImagesWidget')) {
// get the details of the source image
$path = $this->BO->getAttachmentsLocation(). '/'. $fileName;
$imgType = $image_details[2];
$img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (boolean) $config->get('sysCMSImagesWidgetSecure'));
$this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content);
// render a normal image link to the ViewAttachment controller
$this->content = str_replace($attachmentURL, '<img src="'. $this->BO->getAttachmentSecureURL($fileName). '">', $this->content);
* Facade method which will invoke our custom markdown class rather than the standard one
// Initialize the parser and return the result of its transform method.
$parser_class = 'AlphaMarkdown';
$parser = new $parser_class;
* Replace all instances of $sysURL in the text with the sysURL setting from config
$text = str_replace('$sysURL', $config->get('sysURL'), $text);
// transform text using parser.
return $parser->transform($text);
* Saves the HTML generated by Markdown to the cache directory
private function cache() {
// check to ensure that the article is not transient before caching it
if ($this->BO->getID() != '00000000000' || $this->BO->isLoadedFromFile()) {
$fp= fopen($this->filename,"w");
throw new AlphaException('Failed to open the cache file for writing, directory permissions my not be set correctly!');
flock($fp,2); // locks the file for writting
flock($fp,3); // unlocks the file
fclose($fp); //closes the file
* Used to check the HTML cache for the BO cache file
* Method to load the content of the cache file to the $content attribute of this object
$fp = fopen($this->filename,"r");
throw new AlphaException('Failed to open the cache file for reading, directory permissions my not be set correctly!');
fclose($fp); //closes the file
|