From 6828950f9c74d9aaaf34a9aebb784d3737cbeb35 Mon Sep 17 00:00:00 2001 From: Jaap Jansma Date: Thu, 30 Sep 2021 11:39:06 +0200 Subject: [PATCH 1/3] Fix for #38 Fix for #38 --- .../CRM/Civiproxy/Mailer.php | 5 ++ .../CRM/Civiproxy/Mosaico.php | 70 +++++++++++++++++++ proxy/config.dist.php | 1 + proxy/mosaico.php | 68 ++++++++++++------ 4 files changed, 123 insertions(+), 21 deletions(-) create mode 100644 de.systopia.civiproxy/CRM/Civiproxy/Mosaico.php diff --git a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php index 4e8e26ac..ce6e4ac7 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,6 +61,9 @@ 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); + if ($mosaico->isMosaicoInstalled()) { + $value = preg_replace("#({$mosaico->getMosaicoExtensionUrl()}/packages/mosaico/templates/)(\S*)([\"'])#i", $proxy_base . '/mosaico.php?template_url=$2$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); From 59b7544b0b39f5a2b64bf96f6170a621e4db44d2 Mon Sep 17 00:00:00 2001 From: Jaap Jansma Date: Fri, 19 Nov 2021 16:19:48 +0100 Subject: [PATCH 2/3] Fix for #38: added url encoding to mosaico template images, so that google+.png works --- de.systopia.civiproxy/CRM/Civiproxy/Mailer.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php index ce6e4ac7..90284a9b 100644 --- a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php +++ b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php @@ -62,7 +62,9 @@ static function mendURLs(&$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); if ($mosaico->isMosaicoInstalled()) { - $value = preg_replace("#({$mosaico->getMosaicoExtensionUrl()}/packages/mosaico/templates/)(\S*)([\"'])#i", $proxy_base . '/mosaico.php?template_url=$2$3', $value); + $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 From cd780e50e1d0ad2239a655fdee27af22b85b7963 Mon Sep 17 00:00:00 2001 From: Jaap Jansma Date: Fri, 19 Nov 2021 16:31:05 +0100 Subject: [PATCH 3/3] Fix for #38: added url encoding to mosaico template images, so that google+.png works --- de.systopia.civiproxy/CRM/Civiproxy/Mailer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php index 90284a9b..d3b96183 100644 --- a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php +++ b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php @@ -61,6 +61,7 @@ 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); 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];