Skip to content

Commit

Permalink
Merge pull request #54 from jaapjansma/fix_for_38
Browse files Browse the repository at this point in the history
Fix for #38
  • Loading branch information
bjendres authored Nov 19, 2021
2 parents 4ed9d58 + cd780e5 commit b6176c3
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 22 deletions.
9 changes: 8 additions & 1 deletion de.systopia.civiproxy/CRM/Civiproxy/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
70 changes: 70 additions & 0 deletions de.systopia.civiproxy/CRM/Civiproxy/Mosaico.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright (C) 2021 Jaap Jansma (jaap.jansma@civicoop.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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;
}

}
1 change: 1 addition & 0 deletions proxy/config.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
68 changes: 47 additions & 21 deletions proxy/mosaico.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b6176c3

Please sign in to comment.