diff --git a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php index 59feac18..d3b96183 100644 --- a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php +++ b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php @@ -47,6 +47,8 @@ static function mendURLs(&$value) { $enabled = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_enabled'); if (!$enabled) return; + $mosaico = CRM_Civiproxy_Mosaico::singleton(); + // get the URLs $config = CRM_Core_Config::singleton(); $system_base = $config->userFrameworkBaseURL; @@ -59,7 +61,12 @@ static function mendURLs(&$value) { $value = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/open.php#i", $proxy_base.'/open.php', $value); $value = preg_replace("#{$system_base}sites/default/files/civicrm/persist/#i", $proxy_base.'/file.php?id=', $value); $value = preg_replace("#{$system_base}civicrm/mosaico/img\?src=#i", $proxy_base.'/mosaico.php?id=', $value); - $value = preg_replace("#{$system_base}civicrm/mosaico/img/\?src=#i", $proxy_base.'/mosaico.php?id=', $value); + $value = preg_replace("#{$system_base}civicrm/mosaico/img/\?src=#i", $proxy_base.'/mosaico.php?id=', $value); + if ($mosaico->isMosaicoInstalled()) { + $value = preg_replace_callback("#({$mosaico->getMosaicoExtensionUrl()}/packages/mosaico/templates/)(\S*)([\"'])#i", function($matches) use ($proxy_base) { + return $proxy_base . '/mosaico.php?template_url=' . urlencode($matches[2]) . $matches[3]; + }, $value); + } // Mailing related functions $value = preg_replace("#{$system_base}civicrm/mailing/view#i", $proxy_base.'/mailing/mail.php', $value); diff --git a/de.systopia.civiproxy/CRM/Civiproxy/Mosaico.php b/de.systopia.civiproxy/CRM/Civiproxy/Mosaico.php new file mode 100644 index 00000000..ea44dc6e --- /dev/null +++ b/de.systopia.civiproxy/CRM/Civiproxy/Mosaico.php @@ -0,0 +1,70 @@ +. + */ + +class CRM_Civiproxy_Mosaico { + + /** + * @var CRM_Civiproxy_Mosaico + */ + private static $singleton; + + /** + * @var String + */ + private $mosiacoExtenionUrl; + + /** + * @var bool + */ + private $isMosaicoInstalled = false; + + private function __construct() { + try { + $mosaicoExt = civicrm_api3('Extension', 'getsingle', ['full_name' => "uk.co.vedaconsulting.mosaico"]); + $this->isMosaicoInstalled = true; + $this->mosiacoExtenionUrl = CRM_Mosaico_ExtensionUtil::url(); + } catch (\Exception $ex) { + // Do nothing + } + } + + /** + * @return CRM_Civiproxy_Mosaico + */ + public static function singleton() { + if (!self::$singleton) { + self::$singleton = new CRM_Civiproxy_Mosaico(); + } + return self::$singleton; + } + + /** + * @return bool + */ + public function isMosaicoInstalled() { + return $this->isMosaicoInstalled; + } + + /** + * @return string + */ + public function getMosaicoExtensionUrl() { + return $this->mosiacoExtenionUrl; + } + +} \ No newline at end of file diff --git a/proxy/config.dist.php b/proxy/config.dist.php index 404c3972..7ddeef42 100644 --- a/proxy/config.dist.php +++ b/proxy/config.dist.php @@ -43,6 +43,7 @@ $target_rest = $target_civicrm . '/sites/all/modules/civicrm/extern/rest.php'; $target_file = $target_civicrm . '/sites/default/files/civicrm/persist/'; $target_mosaico = NULL; // (disabled by default): $target_civicrm . '/civicrm/mosaico/img?src='; +$target_mosaico_template_url = NULL; // (disabled by default): $target_civicrm . '/wp-content/uploads/civicrm/ext/uk.co.vedaconsulting.mosaico/packages/mosaico/templates/'; $target_mail_view = $target_civicrm . '/civicrm/mailing/view'; $target_url = $target_civicrm . '/civicrm/mailing/url'; $target_open = $target_civicrm . '/civicrm/mailing/open'; diff --git a/proxy/mosaico.php b/proxy/mosaico.php index 1b579dbe..936df041 100644 --- a/proxy/mosaico.php +++ b/proxy/mosaico.php @@ -17,30 +17,61 @@ civiproxy_security_check('file'); // basic restraints -$valid_parameters = array( 'id' => 'string' ); +$valid_parameters = array( 'id' => 'string', 'template_url' => 'string' ); $parameters = civiproxy_get_parameters($valid_parameters); -// check if id specified -if (empty($parameters['id'])) civiproxy_http_error("Resource not found"); - -// check restrictions -if (!empty($file_cache_exclude)) { - foreach ($file_cache_exclude as $pattern) { - if (preg_match($pattern, $parameters['id'])) { +if (!empty($parameters['id'])) { + // check restrictions + if (!empty($file_cache_exclude)) { + foreach ($file_cache_exclude as $pattern) { + if (preg_match($pattern, $parameters['id'])) { + civiproxy_http_error("Invalid Resource", 403); + } + } + } + if (!empty($file_cache_include)) { + $accept_id = FALSE; + foreach ($file_cache_include as $pattern) { + if (preg_match($pattern, $parameters['id'])) { + $accept_id = TRUE; + } + } + if (!$accept_id) { civiproxy_http_error("Invalid Resource", 403); } } -} -if (!empty($file_cache_include)) { - $accept_id = FALSE; - foreach ($file_cache_include as $pattern) { - if (preg_match($pattern, $parameters['id'])) { - $accept_id = TRUE; + + // look up the required resource + $header_key = 'header&' . $parameters['id']; + $data_key = 'data&' . $parameters['id']; + $url = $target_mosaico . $parameters['id']; +} elseif (!empty($parameters['template_url'])) { + // check restrictions + if (!empty($file_cache_exclude)) { + foreach ($file_cache_exclude as $pattern) { + if (preg_match($pattern, $parameters['template_url'])) { + civiproxy_http_error("Invalid Resource", 403); + } } } - if (!$accept_id) { - civiproxy_http_error("Invalid Resource", 403); + if (!empty($file_cache_include)) { + $accept_id = FALSE; + foreach ($file_cache_include as $pattern) { + if (preg_match($pattern, $parameters['template_url'])) { + $accept_id = TRUE; + } + } + if (!$accept_id) { + civiproxy_http_error("Invalid Resource", 403); + } } + + // look up the required resource + $header_key = 'header&' . $parameters['template_url']; + $data_key = 'data&' . $parameters['template_url']; + $url = $target_mosaico_template_url . $parameters['template_url']; +} else { + civiproxy_http_error("Resource not found"); } // load PEAR file cache @@ -49,9 +80,6 @@ require_once('Cache/Lite.php'); $file_cache = new Cache_Lite($file_cache_options); -// look up the required resource -$header_key = 'header&' . $parameters['id']; -$data_key = 'data&' . $parameters['id']; $header = $file_cache->get($header_key); $data = $file_cache->get($data_key); @@ -68,8 +96,6 @@ } // if we get here, we have a cache miss => load -$url = $target_mosaico . $parameters['id']; - $curlSession = curl_init(); curl_setopt($curlSession, CURLOPT_URL, $url); curl_setopt($curlSession, CURLOPT_HEADER, 1);