From b6dcfbcb167163309de909e87361b7657465ed76 Mon Sep 17 00:00:00 2001 From: systopia Date: Fri, 17 Nov 2017 13:59:28 +0100 Subject: [PATCH] moved mosaico to separate script (#7) --- .../CRM/Civiproxy/Mailer.php | 8 +- proxy/config.php | 2 +- proxy/file.php | 28 ++-- proxy/mosaico.php | 120 ++++++++++++++++++ 4 files changed, 134 insertions(+), 24 deletions(-) create mode 100644 proxy/mosaico.php diff --git a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php index 15ed411..bdff057 100644 --- a/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php +++ b/de.systopia.civiproxy/CRM/Civiproxy/Mailer.php @@ -53,10 +53,10 @@ class CRM_Civiproxy_Mailer { $proxy_base = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_url'); // General external functions - $value = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/url.php#i", $proxy_base.'/url.php', $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?mosaico=0&id=', $value); - $value = preg_replace("#{$system_base}civicrm/mosaico/img\?src=#i", $proxy_base.'/file.php?mosaico=1&id=', $value); + $value = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/url.php#i", $proxy_base.'/url.php', $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); // Mailing related functions $value = preg_replace("#{$system_base}civicrm/mailing/view#i", $proxy_base.'/mailing/mail.php', $value); diff --git a/proxy/config.php b/proxy/config.php index f18235a..97ad7b5 100644 --- a/proxy/config.php +++ b/proxy/config.php @@ -29,8 +29,8 @@ $target_rest = $target_civicrm . '/sites/all/modules/civicrm/extern/rest.ph $target_url = $target_civicrm . '/sites/all/modules/civicrm/extern/url.php'; $target_open = $target_civicrm . '/sites/all/modules/civicrm/extern/open.php'; $target_file = $target_civicrm . '/sites/default/files/civicrm/persist/'; +$target_mosaico = $target_civicrm . '/civicrm/mosaico/img?src='; $target_mail_view = $target_civicrm . '/civicrm/mailing/view'; -$target_mosaico_file = $target_civicrm . '/civicrm/mosaico/img?src='; // Set api-key for mail subscribe/unsubscribe user // Set to NULL/FALSE to disable the feature diff --git a/proxy/file.php b/proxy/file.php index fa52084..36684dd 100644 --- a/proxy/file.php +++ b/proxy/file.php @@ -6,20 +6,20 @@ | Author: B. Endres (endres -at- systopia.de) | | http://www.systopia.de/ | +---------------------------------------------------------*/ + require_once "config.php"; require_once "proxy.php"; -// basic restraints -$valid_parameters = array( 'id' => 'string', 'mosaico' => 'int' ); -$parameters = civiproxy_get_parameters($valid_parameters); // see if file caching is enabled -if ((!$target_file && !$parameters['mosaico']) || (!$target_mosaico_file && $parameters['mosaico'])) { - civiproxy_http_error("Feature disabled", 405); -} +if (!$target_file) civiproxy_http_error("Feature disabled", 405); // basic check civiproxy_security_check('file'); +// basic restraints +$valid_parameters = array( 'id' => 'string' ); +$parameters = civiproxy_get_parameters($valid_parameters); + // check if id specified if (empty($parameters['id'])) civiproxy_http_error("Resource not found"); @@ -66,13 +66,9 @@ if ($header && $data) { print $data; exit(); } + // if we get here, we have a cache miss => load -if ($parameters['mosaico'] == 1) { - $url = $target_mosaico_file . $parameters['id']; -} -else { - $url = $target_file . $parameters['id']; -} +$url = $target_file . $parameters['id']; // error_log("CACHE MISS. LOADING $url"); $curlSession = curl_init(); @@ -96,6 +92,7 @@ if (curl_error($curlSession)) { error_log(curl_error($curlSession)); civiproxy_http_error(curl_error($curlSession), curl_errno($curlSession)); } + // process the results $content = explode("\r\n\r\n", $response, 2); $header = $content[0]; @@ -104,13 +101,6 @@ $body = $content[1]; // extract headers $header_lines = explode(chr(10), $header); -// Remove chunked encoding header -foreach ($header_lines as $k => $header_line) { - if(strpos($header_line,'Transfer-Encoding: chunked') !== FALSE) { - unset($header_lines[$k]); - } -} - // store the information in the cache $file_cache->save(json_encode($header_lines), $header_key); $file_cache->save($body, $data_key); diff --git a/proxy/mosaico.php b/proxy/mosaico.php new file mode 100644 index 0000000..91ad5fe --- /dev/null +++ b/proxy/mosaico.php @@ -0,0 +1,120 @@ + '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'])) { + 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); + } +} + +// load PEAR file cache +ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . 'libs'); +if (!file_exists($file_cache_options['cacheDir'])) mkdir($file_cache_options['cacheDir']); +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); + +if ($header && $data) { + // error_log("CACHE HIT"); + $header_lines = json_decode($header); + foreach ($header_lines as $header_line) { + header($header_line); + } + + print $data; + exit(); +} + +// 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); +curl_setopt($curlSession, CURLOPT_RETURNTRANSFER,1); +curl_setopt($curlSession, CURLOPT_TIMEOUT, 30); +curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 1); +if (!empty($target_interface)) { + curl_setopt($curlSession, CURLOPT_INTERFACE, $target_interface); +} +if (file_exists(dirname(__FILE__).'/target.pem')) { + curl_setopt($curlSession, CURLOPT_CAINFO, dirname(__FILE__).'/target.pem'); +} + +//Send the request and store the result in an array +$response = curl_exec($curlSession); + +// Check that a connection was made +if (curl_error($curlSession)) { + error_log(curl_error($curlSession)); + civiproxy_http_error(curl_error($curlSession), curl_errno($curlSession)); +} + +// process the results +$content = explode("\r\n\r\n", $response, 2); +$header = $content[0]; +$body = $content[1]; + +// extract headers +$header_lines = explode(chr(10), $header); + +// Remove chunked encoding header +foreach ($header_lines as $k => $header_line) { + if(strpos($header_line,'Transfer-Encoding: chunked') !== FALSE) { + unset($header_lines[$k]); + } +} + +// store the information in the cache +$file_cache->save(json_encode($header_lines), $header_key); +$file_cache->save($body, $data_key); + +// and reply +foreach ($header_lines as $header_line) { + header($header_line); +} + +print $body; +curl_close ($curlSession);