/**
  * Parse the newsletter
  * @author      Cloudrexx AG
  * @author      Stefan Heinemann <*****@*****.**>
  * @param       string $userType Which type the user has (newsletter or access)
  */
 function ParseNewsletter($subject, $content_text, $TemplateSource, $format, $TargetEmail, $userData, $NewsletterID, $testDelivery = false)
 {
     global $objDatabase, $_ARRAYLANG, $_CONFIG;
     $NewsletterBody = '';
     $codeResult = $objDatabase->Execute('SELECT `code` FROM `' . DBPREFIX . 'module_newsletter_tmp_sending` WHERE `newsletter` = ' . $NewsletterID . ' AND `email` = "' . $userData['email'] . '"');
     $code = $codeResult->fields['code'];
     // TODO: replace with new methode $this->GetBrowserViewURL()
     $browserViewUrl = ASCMS_PROTOCOL . '://' . $_CONFIG['domainUrl'] . ASCMS_PATH_OFFSET . '/' . \FWLanguage::getLanguageCodeById(FRONTEND_LANG_ID) . '/index.php?section=Newsletter&cmd=displayInBrowser&standalone=true&code=' . $code . '&email=' . $userData['email'] . '&id=' . $NewsletterID;
     if ($format == 'text') {
         $NewsletterBody = $_ARRAYLANG['TXT_NEWSLETTER_BROWSER_VIEW'] . "\n" . $browserViewUrl;
         return $NewsletterBody;
     }
     $country = empty($userData['country_id']) ? '' : htmlentities(\FWUser::getFWUserObject()->objUser->objAttribute->getById('country_' . $userData['country_id'])->getName(), ENT_QUOTES, CONTREXX_CHARSET);
     switch ($userData['sex']) {
         case 'm':
             $sex = $_ARRAYLANG['TXT_NEWSLETTER_MALE'];
             break;
         case 'f':
             $sex = $_ARRAYLANG['TXT_NEWSLETTER_FEMALE'];
             break;
         default:
             $sex = '';
             break;
     }
     switch ($userData['type']) {
         case self::USER_TYPE_ACCESS:
         case self::USER_TYPE_CORE:
             $realUser = true;
             break;
         case self::USER_TYPE_NEWSLETTER:
         default:
             $realUser = false;
             break;
     }
     // lets prepare all links for tracker before we replace placeholders
     // TODO: migrate tracker to new URL-format
     $content_text = self::prepareNewsletterLinksForSend($NewsletterID, $content_text, $userData['id'], $realUser);
     $search = array('[[email]]', '[[sex]]', '[[salutation]]', '[[title]]', '[[firstname]]', '[[lastname]]', '[[position]]', '[[company]]', '[[industry_sector]]', '[[address]]', '[[city]]', '[[zip]]', '[[country]]', '[[phone_office]]', '[[phone_private]]', '[[phone_mobile]]', '[[fax]]', '[[birthday]]', '[[website]]');
     $replace = array($userData['email'], $sex, $userData['salutation'], $userData['title'], $userData['firstname'], $userData['lastname'], $userData['position'], $userData['company'], $userData['industry_sector'], $userData['address'], $userData['city'], $userData['zip'], $country, $userData['phone_office'], $userData['phone_private'], $userData['phone_mobile'], $userData['fax'], $userData['birthday'], $userData['website']);
     if ($testDelivery) {
         $replace = $search;
     }
     // do the replacement
     $content_text = str_replace($search, $replace, $content_text);
     $TemplateSource = str_replace($search, $replace, $TemplateSource);
     $search = array('[[display_in_browser_url]]', '[[profile_setup]]', '[[unsubscribe]]', '[[date]]', '[[subject]]');
     $replace = array($browserViewUrl, $this->GetProfileURL($userData['code'], $TargetEmail, $userData['type']), $this->GetUnsubscribeURL($userData['code'], $TargetEmail, $userData['type']), date(ASCMS_DATE_FORMAT_DATE), $subject);
     // Replace the links in the content
     $content_text = str_replace($search, $replace, $content_text);
     // replace the links in the template
     $TemplateSource = str_replace($search, $replace, $TemplateSource);
     // i believe this replaces image paths...
     $allImg = array();
     preg_match_all('/src="([^"]*)"/', $content_text, $allImg, PREG_PATTERN_ORDER);
     $size = sizeof($allImg[1]);
     $i = 0;
     while ($i < $size) {
         $URLforReplace = $allImg[1][$i];
         $replaceUrl = new \Cx\Core\Routing\Url($URLforReplace, true);
         if ($replaceUrl->isInternal()) {
             $ReplaceWith = $replaceUrl->toString();
         } else {
             $ReplaceWith = $URLforReplace;
         }
         $content_text = str_replace('"' . $URLforReplace . '"', '"' . $ReplaceWith . '"', $content_text);
         $i++;
     }
     // Set HTML height and width attributes for img-tags
     $allImgsWithHeightOrWidth = array();
     preg_match_all('/<img[^>]*style=(["\'])[^\\1]*(?:width|height):\\s*[^;\\1]+;?\\s*[^\\1]*\\1[^>]*>/', $content_text, $allImgsWithHeightOrWidth);
     foreach ($allImgsWithHeightOrWidth as $img) {
         $htmlHeight = $this->getAttributeOfTag($img, 'img', 'height');
         $htmlWidth = $this->getAttributeOfTag($img, 'img', 'width');
         // no need to proceed if attributes are already set
         if (!empty($htmlHeight) && !empty($htmlWidth)) {
             continue;
         }
         $cssHeight = $this->getCssAttributeOfTag($img, 'img', 'height');
         $cssWidth = $this->getCssAttributeOfTag($img, 'img', 'width');
         // no need to proceed if we have no values to set
         if (empty($cssHeight) && empty($cssWidth)) {
             continue;
         }
         $imgOrig = $img;
         // set height and width attributes (if not yet set)
         if (empty($htmlHeight) && !empty($cssHeight)) {
             $img = $this->setAttributeOfTag($img, 'img', 'height', $cssHeight);
         }
         if (empty($htmlWidth) && !empty($cssWidth)) {
             $img = $this->setAttributeOfTag($img, 'img', 'width', $cssWidth);
         }
         $content_text = str_replace($imgOrig, $img, $content_text);
     }
     $NewsletterBody = str_replace("[[content]]", $content_text, $TemplateSource);
     return $NewsletterBody;
 }