Source for file Data.php
Documentation is available at Data.php
* Magentron EmailImages Extension
* @package Magentron_EmailImages
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
/** Configuration path for extension enablement.
/** Configuration path for maximum cache lifetime.
/** Configuration path for regular expression used to find image URLs in HTML.
* @see getRegularExpression()
/** Configuration path for index into matches of regular expression used to find image URLs in HTML.
* @see XML_EMAIL_IMAGES_REGEXP,
* getRegularExpressionIndex()
/** Default maximum lifetime used for cache.
/** Default regular expression to extract image URLs from the email HTML body.
* @see getRegularExpression()
const DEFAULT_REGEXP =
'/((<[iI][mM][gG] [^>]*[sS][rR][cC]|[Bb][Aa][Cc][Kk][Gg][Rr][Oo][Uu][Nn][Dd])="|image:url\(\'?)([^\'"\)]*)(["\'\)])/';
/** Default index in the regular expression matches to extract the image URLs from the email HTML body.
* @see getRegularExpressionIndex()
* Is the EmailImages extension enabled to actually attach images?
* @see XML_EMAIL_IMAGES_ENABLE
return (boolean)
Mage::getStoreConfig(self::XML_EMAIL_IMAGES_ENABLE);
* Retrieve the maximum lifetime for caching in seconds.
* @see XML_EMAIL_IMAGES_CACHE_TIME, DEFAULT_CACHE_TIME
$config =
Mage::getStoreConfig(self::XML_EMAIL_IMAGES_CACHE_TIME);
$config =
self::DEFAULT_CACHE_TIME;
return (integer)
$config;
* Retrieve the regular expression to extract the image URLs from the email HTML body.
* @see XML_EMAIL_IMAGES_REGEXP, DEFAULT_REGEXP
$config =
Mage::getStoreConfig(self::XML_EMAIL_IMAGES_REGEXP);
$config =
self::DEFAULT_REGEXP;
* Retrieve the index in the regular expression matches to extract the image URLs from the email HTML body.
* @see XML_EMAIL_IMAGES_REGEXP_INDEX, DEFAULT_REGEXP_INDEX
$config =
Mage::getStoreConfig(self::XML_EMAIL_IMAGES_REGEXP_INDEX);
$config =
self::DEFAULT_REGEXP_INDEX;
return (integer)
$config;
* Attach images to mail object.
* @param string $context [optional] Set to unique identifier for template, so that body needs to be parsed only once per template (NB: case-insensitive).
* @return void Fails silently if unable to attach image, warning message sent to log.
* @see isEnabled(), _getImageUrlsFromMail(), _attachImageUrls()
// check whether the administrator has enabled the module
Mage::log('EmailImages - extension disabled');
// ignore exception, but do log it
Mage::log('EmailImages - ERROR: exception caught: ' .
$e, Zend_Log::ERR);
* Remove cached image and context data from the cache.
* @return Magentron_EmailImages_Helper_Data Provides fluent interface
$cache =
Mage::getSingleton('core/cache');
$cache->flush(self::CACHE_TAG);
* Retrieve image URLs from email content
* @param string $context [optional] Set to unique identifier for template, so that body needs to be parsed only once per template (NB: case-insensitive).
* @return array Array of image URLs.
* _getContextDataFromCache(), _getBodyHtml(), _getImageUrlsFromBodyHtml(), _saveContextDataToCache(),
// check cache for context
$cache =
Mage::getSingleton('core/cache');
$use_cache =
null !==
$context &&
$cache->canUse(self::CACHE_TYPE);
Mage::log(__CLASS__ .
'::' . __FUNCTION__ .
'(): use_cache = ' .
var_export($use_cache, 1));
$context_cache_id =
self::CACHE_TYPE .
'-urls-' .
(is_string($context) ?
$context :
md5(serialize($context)));
// save URLs to cache, if context defined
$isHtml = (boolean)
$bodyHtml;
$urls =
$context_data['is_html'] ?
$context_data['urls'] :
array();
Mage::log('EmailImages - loaded URLs from cache (cache ID: ' .
$context_cache_id .
')');
* Retrieve image URL from email body HTML
* @param string $bodyHtml Email body HTML to use.
* @return array Array of image URLs.
* @see getRegularExpression(), getRegularExpressionIndex()
Mage::log('EmailImages - parsing HTML body');
// find image URLs in email HTML body
$urls =
$matches[$index];
if ( 0 ==
count($urls) ) // no URLs in HTML body?
Mage::log('EmailImages - no images found in email HTML body', Zend_Log::WARN);
else // otherwise no HTML body?
Mage::log('EmailImages - no HTML body for email', Zend_Log::WARN);
* Retrieve cached context data.
* @param string $context_cache_id Cached context data cache ID.
* @return array Array containing keys 'isHtml' to indicate a HTML body, 'urls' for image URLs from that HTML body.
$cache =
Mage::getSingleton('core/cache');
$context_data =
$cache->load($context_cache_id);
Mage::log(__CLASS__ .
'::' . __FUNCTION__ .
'(): context_data=' .
print_r($context_data, 1));
* Retrieve HTML body from mail object.
* @param Zend_Mail $mail Mail object instance to use.
* @return string|false HTML body from the mail object instance, if any;
$bodyHtmlObject =
$mail->getBodyHtml();
$bodyHtml =
$bodyHtmlObject->getContent();
if ( $bodyHtmlObject->encoding ==
Zend_Mime::ENCODING_QUOTEDPRINTABLE )
$bodyHtml =
$bodyHtmlObject;
Mage::log(__CLASS__ .
'::' . __FUNCTION__ .
'(): unsupported bodyHtml = ' .
substr(var_export($bodyHtml, 1), 0, 128) .
'...');
Mage::log(__CLASS__ .
'::' . __FUNCTION__ .
'(): bodyHtml = ' .
($bodyHtml ?
preg_replace('/[\s\t\r\n\k]+/', ' ', substr($bodyHtml, 0, 128)) :
'<empty>'));
* Save context data to cache.
* @param string $context_cache_id Cache ID to use.
* @param boolean $isHtml Set to true if email body is HTML.
* @param array $urls Array of image URLs from HTML body.
$cache =
Mage::getSingleton('core/cache');
$cache->save($context_data, $context_cache_id, array( self::CACHE_TAG ), $cache_time);
Mage::log('EmailImages - saved context URLs to cache');
* Attach image URLs to the email.
* @param array $urls Array of image URLs to attach.
* @see _retrieveImageData(),
foreach ( $urls as $index =>
$url )
$mp =
$mail->createAttachment($data['image']
Mage::log('EmailImages - unable to retrieve image from URL ' .
$url, Zend_Log::WARN);
// remove images that failed to load
// set Content-Type to multipart/related to properly display the images inline, if any
$mail->setType(Zend_Mime::MULTIPART_RELATED);
* Retrieve image data from URL.
* NB: uses file_get_contents().
* @TODO should we try to use curl if available? should that be configurable by admin?
* @param string $url URL to retrieve image data from.
* @return array|false Array with keys 'image' for image binary, 'size' for image size, if successfully retrieved image from URL;
* @see CACHE_TAG, CACHE_TYPE,
// retrieve image from cache or URL
$cache =
Mage::getSingleton('core/cache');
$use_cache =
$cache->canUse(self::CACHE_TYPE);
$cache_id =
self::CACHE_TYPE .
$url;
$data =
$cache->load(self::CACHE_TYPE .
$url);
// retrieve image data from URL
Mage::log('EmailImages - loading image from URL ' .
$url);
// save retrieved image data to cache even if retrieving the image failed, if allowed
$cache->save($serialized_data, $cache_id, array( self::CACHE_TAG ), $cache_time);
Mage::log('EmailImages - saved image to cache');
Mage::log('EmailImages - loaded image from cache');
Documentation generated on Fri, 09 Oct 2015 03:37:09 +0200 by phpDocumentor 1.4.4