function preview()
    {
        jimport('joomla.filesystem.file');
        $db =& JFactory::getDBO();
        $params =& JComponentHelper::getParams('com_joomailermailchimpintegration');
        $paramsPrefix = version_compare(JVERSION, '1.6.0', 'ge') ? 'params.' : '';
        $model = $this->getModel('create');
        $error = false;
        $elements = JRequest::getVar('elements', '', 'request', 'string');
        $elements = json_decode($elements);
        $response = array();
        $response['msg'] = '';
        $abs_path = '$1="' . JURI::root() . '$2$3';
        $campaign_name_ent = htmlentities(urldecode($elements->campaignName));
        if (get_magic_quotes_gpc()) {
            $campaign_name_ent = stripslashes($campaign_name_ent);
        }
        $campaign_name_ent = str_ireplace(' ', '_', $campaign_name_ent);
        $subject = urldecode($elements->subject);
        if (get_magic_quotes_gpc()) {
            $subject = stripslashes($subject);
        }
        $intro_text = urldecode($elements->intro);
        $sidebar = urldecode($elements->sidebar);
        if (get_magic_quotes_gpc()) {
            $intro_text = stripslashes($intro_text);
        }
        if (isset($elements->article)) {
            $articles = $elements->article;
        } else {
            $articles = false;
        }
        $k2_installed = $elements->k2_installed;
        if ($k2_installed && isset($elements->k2article)) {
            $articles_k2 = $elements->k2article;
        } else {
            $articles_k2 = false;
        }
        $jomsocial_installed = $elements->jomsocial_installed;
        if ($jomsocial_installed) {
            if (isset($elements->jsProfiles)) {
                $jomsocialProfiles = $elements->jsProfiles;
                $jsFields = $elements->jsProfileFields;
            } else {
                $jomsocialProfiles = false;
            }
            if (isset($elements->jsdisc)) {
                $jsdisc = $elements->jsdisc;
            } else {
                $jsdisc = false;
            }
        }
        $aec_installed = $elements->aec_installed;
        if ($aec_installed) {
            if (isset($elements->aec)) {
                $aec = $elements->aec;
            } else {
                $aec = false;
            }
        }
        $ambra_installed = $elements->ambra_installed;
        if ($ambra_installed) {
            if (isset($elements->ambra)) {
                $ambra = $elements->ambra;
            } else {
                $ambra = false;
            }
        }
        $template_folder = $elements->template;
        // display table of contents?
        $toc_checkbox = $elements->toc;
        $toc_type = $elements->toc_type;
        // display popular articles?
        $popular_checkbox = $elements->popular;
        $popular_ex = $elements->popEx;
        $popular_in = $elements->popIn;
        if ($k2_installed) {
            // include K2 in populars?
            $populark2_checkbox = $elements->populark2;
            $populark2_ex = $elements->popk2Ex;
            $populark2_in = $elements->popk2In;
            // only K2 articles in populars?
            $populark2_only = $elements->populark2_only;
        } else {
            $populark2_checkbox = false;
        }
        // display VM products in sidebar?
        if (isset($elements->vm_sb)) {
            $vm_sb = $elements->vm_sb;
        } else {
            $vm_sb = false;
        }
        if (isset($elements->vm_sb_products)) {
            $vm_sb_products = $elements->vm_sb_products;
        } else {
            $vm_sb_products = false;
        }
        if ($vm_sb_products) {
            $vm_sb_productids = array();
            $vm_sb_productprices = array();
            for ($i = 0; $i < count($vm_sb_products); $i++) {
                $vm_sb_products[$i] = explode(';', $vm_sb_products[$i]);
                $vm_sb_productids[$i] = $vm_sb_products[$i][0];
                $vm_sb_productprices[$i] = $vm_sb_products[$i][1];
                $vm_sb_productcats[$i] = $vm_sb_products[$i][2];
            }
            $vm_sb_order = $elements->vm_sb_order;
            $vm_sb_price = isset($elements->vm_sb_pr) ? $elements->vm_sb_pr : '';
            $vm_sb_cf = isset($elements->vm_sb_cf) ? $elements->vm_sb_cf : '';
            $vm_sb_img = isset($elements->vm_sb_img) ? $elements->vm_sb_img : '';
            $vm_sb_link = isset($elements->vm_sb_link) ? $elements->vm_sb_link : '';
            $vm_short_desc = isset($elements->vm_short_desc) ? $elements->vm_short_desc : '';
            $vm_desc = isset($elements->vm_desc) ? $elements->vm_desc : '';
        }
        $twitter_name = $elements->twitter;
        $facebook_url = $elements->facebook;
        $myspace_name = $elements->myspace;
        //	$facebookShareIt = $_POST['facebookShare'];
        $facebookShareIt = 0;
        // open the template file
        $filename = JPATH_ADMINISTRATOR . DS . "components" . DS . "com_joomailermailchimpintegration" . DS . "templates" . DS . $template_folder . DS . "template.html";
        $template = JFile::read($filename, false, filesize($filename));
        if (!$template) {
            $response['html'] = '<div style="border: 2px solid #ff0000; margin:15px 0 5px;padding:10px 15px 12px;">' . '<img src="' . JURI::root() . 'administrator/components/com_joomailermailchimpintegration/assets/images/warning.png" align="left"/>' . '<div style="padding-left: 45px; line-height: 28px; font-size: 14px;">' . JText::_('JM_TEMPLATE_ERROR') . '</div></div>';
        } else {
            $regex = '!<#repeater#[^>]*>(.*)<#/repeater#>!is';
            preg_match($regex, $template, $repeater);
            $imagepath = '$1="' . JURI::root() . 'administrator' . DS . 'components' . DS . 'com_joomailermailchimpintegration' . DS . 'templates' . DS . $template_folder . DS . '$2$3';
            if (isset($repeater[0])) {
                $repeater[0] = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $imagepath, $repeater[0]);
            } else {
                $response['html'] = '<div style="border: 2px solid #ff0000; margin:15px 0 5px;padding:10px 15px 12px;">' . '<img src="' . JURI::root() . 'administrator/components/com_joomailermailchimpintegration/assets/images/warning.png" align="left"/>' . '<div style="padding-left: 45px; line-height: 28px; font-size: 14px;">' . JTEXT::_('JM_NO_CONTENT_CONTAINER') . '</div></div>';
                $repeater[0] = '';
                $error = true;
            }
            // JomSocial Profiles
            $regex = '!<#jomsocialprofiles#[^>]*>(.*)<#/jomsocialprofiles#>!is';
            preg_match($regex, $template, $jsp);
            if (isset($jsp[0])) {
                $jsp = $jsp[0];
            } else {
                $jsp = '';
                if ($jomsocialProfiles) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('No JomSocial profile container') . '<br />';
                }
            }
            $regex = '!<#jomsocialprofilesrepeater#[^>]*>(.*)<#/jomsocialprofilesrepeater#>!is';
            preg_match($regex, $template, $jspr);
            if (isset($jspr[0])) {
                $jspr = $jspr[0];
            } else {
                $jspr = '';
            }
            $regex = '!<#jsfieldsrepeater#[^>]*>(.*)<#/jsfieldsrepeater#>!is';
            preg_match($regex, $template, $jsfr);
            if (isset($jsfr[0])) {
                $jsfr = $jsfr[0];
            } else {
                $jsfr = '';
            }
            // JomSocial Discussions
            $regex = '!<#jomsocialdiscussions#[^>]*>(.*)<#/jomsocialdiscussions#>!is';
            preg_match($regex, $template, $jsd);
            if (isset($jsd[0])) {
                $jsd = $jsd[0];
            } else {
                $jsd = '';
                if ($jsdisc) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('No JomSocial discussions container') . '<br />';
                }
            }
            $regex = '!<#jomsocialdiscussionsrepeater#[^>]*>(.*)<#/jomsocialdiscussionsrepeater#>!is';
            preg_match($regex, $template, $jsdr);
            if (isset($jsdr[0])) {
                $jsdr = $jsdr[0];
            } else {
                $jsdr = '';
            }
            // AEC plans
            $regex = '!<#aec#[^>]*>(.*)<#/aec#>!is';
            preg_match($regex, $template, $aecph);
            if (isset($aecph[0])) {
                $aecph = $aecph[0];
            } else {
                $aecph = '';
                if ($aec) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('No AEC container') . '<br />';
                }
            }
            $regex = '!<#aecrepeater#[^>]*>(.*)<#/aecrepeater#>!is';
            preg_match($regex, $template, $aecr);
            if (isset($aecr[0])) {
                $aecr = $aecr[0];
            } else {
                $aecr = '';
            }
            // Ambra subscriptions
            $regex = '!<#ambra#[^>]*>(.*)<#/ambra#>!is';
            preg_match($regex, $template, $ambraph);
            if (isset($ambraph[0])) {
                $ambraph = $ambraph[0];
            } else {
                $ambraph = '';
                if ($ambra) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('No Ambra container') . '<br />';
                }
            }
            $regex = '!<#ambrarepeater#[^>]*>(.*)<#/ambrarepeater#>!is';
            preg_match($regex, $template, $ambrar);
            if (isset($ambrar[0])) {
                $ambrar = $ambrar[0];
            } else {
                $ambrar = '';
            }
            // table of contents
            $regex = '!<#tableofcontents#[^>]*>(.*)<#/tableofcontents#>!is';
            preg_match($regex, $template, $tableofcontents);
            if (isset($tableofcontents[0])) {
                $tableofcontents = $tableofcontents[0];
            } else {
                $tableofcontents = '';
                if ($toc_checkbox) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('JM_NO_TOC_CONTAINER') . '<br />';
                }
            }
            $regex = '!<#title_repeater#[^>]*>(.*)<#/title_repeater#>!is';
            preg_match($regex, $template, $title_repeater);
            if (isset($title_repeater[0])) {
                $title_repeater = $title_repeater[0];
            } else {
                $title_repeater = '';
            }
            // popular articles
            $regex = '!<#populararticles#[^>]*>(.*)<#/populararticles#>!is';
            preg_match($regex, $template, $populararticles);
            if (isset($populararticles[0])) {
                $populararticles = $populararticles[0];
            } else {
                $populararticles = '';
                if ($popular_checkbox) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('JM_NO_POPULAR_CONTAINER') . '<br />';
                }
            }
            $regex = '!<#popular_repeater#[^>]*>(.*)<#/popular_repeater#>!is';
            preg_match($regex, $template, $popular_repeater);
            if (isset($popular_repeater[0])) {
                $popular_repeater = $popular_repeater[0];
            } else {
                $popular_repeater = '';
            }
            // VM products
            $regex = '!<#vm_products#[^>]*>(.*)<#/vm_products#>!is';
            preg_match($regex, $template, $vm_products);
            if (isset($vm_products[0])) {
                $vm_products = $vm_products[0];
            } else {
                $vm_products = '';
            }
            $regex = '!<#vm_repeater#[^>]*>(.*)<#/vm_repeater#>!is';
            preg_match($regex, $template, $vm_repeater);
            if (isset($vm_repeater[0])) {
                $vm_repeater = $vm_repeater[0];
            } else {
                $vm_repeater = '';
            }
            // Twitter
            $regex = '!<#twitter#[^>]*>(.*)<#/twitter#>!is';
            preg_match($regex, $template, $twitter);
            if (isset($twitter[0])) {
                $twitter = $twitter[0];
            } else {
                $twitter = '';
                if ($twitter_name) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('JM_NO_TWITTER_CONTAINER') . '<br />';
                }
            }
            // Facebook
            $regex = '!<#facebook#[^>]*>(.*)<#/facebook#>!is';
            preg_match($regex, $template, $facebook);
            if (isset($facebook[0])) {
                $facebook = $facebook[0];
            } else {
                $facebook = '';
                if ($facebook_url) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('JM_NO_FACEBOOK_CONTAINER') . '<br />';
                }
            }
            // MySpace
            $regex = '!<#myspace#[^>]*>(.*)<#/myspace#>!is';
            preg_match($regex, $template, $myspace);
            if (isset($myspace[0])) {
                $myspace = $myspace[0];
            } else {
                $myspace = '';
                if ($myspace_name) {
                    $response['msg'] .= JTEXT::_('Error') . ': ' . JTEXT::_('JM_NO_MYSPACE_CONTAINER') . '<br />';
                }
            }
            // Facebook share
            $regex = '!<#facebook_share#[^>]*>(.*)<#/facebook_share#>!is';
            preg_match($regex, $template, $facebookShare);
            if (isset($facebookShare[0])) {
                $fbs = $facebookShare[0];
            }
            //	 else { $fbs = ''; echo JTEXT::_('Error').': '; echo JTEXT::_('No facebook share container').'<br />'; }
            $fbs = '';
            $content = '';
            $art_titles = array();
            // Joomla core articles
            if (@$articles) {
                foreach ($articles as $id) {
                    $full = $elements->{'article_full_' . $id};
                    $readmore = $elements->{'readmore_' . $id};
                    $query = 'SELECT * FROM #__content WHERE id= ' . $id;
                    $db->setQuery($query);
                    $article = $db->loadObjectList();
                    foreach ($article as $art) {
                        $html_title = $html_content = '';
                        if ($toc_type) {
                            $art_titles[] = '<a href="' . JURI::root() . 'index.php?option=com_content&view=article&id=' . $id . '">' . $art->title . '</a>';
                        } else {
                            $art_titles[] = '<a href="#' . $art->title . $id . '">' . $art->title . '</a>';
                        }
                        if ($readmore) {
                            $html_title = '<a href="' . JURI::root() . 'index.php?option=com_content&view=article&id=' . $id . '">' . $art->title . '</a><a name="' . $art->title . $id . '"></a>';
                        } else {
                            $html_title = $art->title;
                        }
                        $html_content = $art->introtext;
                        if ($full == 1) {
                            $html_content .= ' ' . $art->fulltext;
                        }
                        // Read more link
                        if ($readmore) {
                            $html_content .= '<p><a href="' . JURI::root() . 'index.php?option=com_content&view=article&id=' . $id . '">' . JTEXT::_('JM_READ_MORE') . '</a></p>';
                        }
                        $template = str_ireplace('<#title#>', $html_title, $repeater[0]);
                        $template = str_ireplace('<#content#>', $html_content, $template);
                        $content .= $template;
                    }
                    // end foreach article
                }
            }
            // K2 articles
            if ($k2_installed && $articles_k2) {
                foreach ($articles_k2 as $id) {
                    $full = $elements->{'k2article_full_' . $id};
                    $readmore = $elements->{'k2readmore_' . $id};
                    $query = 'SELECT * FROM #__k2_items WHERE id= ' . $id;
                    $db->setQuery($query);
                    $article = $db->loadObjectList();
                    foreach ($article as $art) {
                        $html_title = $html_content = '';
                        if ($readmore) {
                            $html_title = '<a href="' . JURI::root() . 'index.php?option=com_k2&view=item&id=' . $id . '">' . $art->title . '</a><a name="' . $art->title . $id . '"></a>';
                        } else {
                            $html_title = $art->title . '<a name="' . $art->title . $id . '"></a>';
                        }
                        $art_titles[] = '<a href="#' . $art->title . $id . '">' . $art->title . '</a>';
                        $html_content = $art->introtext;
                        if ($full == 1) {
                            $html_content .= ' ' . $art->fulltext;
                        }
                        // Read more link
                        if ($readmore) {
                            $html_content .= '<p><a href="' . JURI::root() . 'index.php?option=com_k2&view=item&id=' . $id . '">' . JTEXT::_('JM_READ_MORE') . '</a></p>';
                        }
                        $template = str_ireplace('<#title#>', $html_title, $repeater[0]);
                        $template = str_ireplace('<#content#>', $html_content, $template);
                        $content .= $template;
                    }
                }
            }
            // JomSocial Profiles
            $jsProfiles = '';
            if (@$jomsocialProfiles) {
                foreach ($jomsocialProfiles as $jsProfile) {
                    $jsUser = CFactory::getUser($jsProfile);
                    $profileLink = JURI::root() . CRoute::_('index.php?option=com_community&view=profile&userid=' . $jsProfile);
                    $thumb = '<a href="' . $profileLink . '"><img src="' . $jsUser->getThumbAvatar() . '" alt="' . $jsUser->name . '" title="' . $jsUser->name . '" border="0" /></a>';
                    $profiles = str_ireplace('<#jsAvatar#>', $thumb, $jspr);
                    $profiles = str_ireplace('<#jsName#>', '<a href="' . $profileLink . '">' . $jsUser->name . '</a>', $profiles);
                    $fieldValues = $model->getFieldValues($jsProfile, $jsFields);
                    $fields = '';
                    foreach ($fieldValues as $f) {
                        if ($f->value) {
                            if ($f->type == 'date') {
                                $f->value = substr($f->value, 0, -9);
                            }
                            $fieldsTmp = str_ireplace('<#jsFieldTitle#>', $f->name, $jsfr);
                            $fieldsTmp = str_ireplace('<#jsFieldValue#>', $f->value, $fieldsTmp);
                            $fields .= $fieldsTmp;
                        }
                    }
                    $profiles = preg_replace('!<#jsfieldsrepeater#[^>]*>(.*)<#/jsfieldsrepeater#>!is', $fields, $profiles);
                    $profiles = str_ireplace(array('<#jsfieldsrepeater#>', '<#/jsfieldsrepeater#>'), '', $profiles);
                    $jsProfiles .= $profiles;
                }
                $jsProfiles = str_ireplace(array('<#jomsocialprofilesrepeater#>', '<#/jomsocialprofilesrepeater#>'), '', $jsProfiles);
            }
            $jsDiscussions = '';
            if (@$jsdisc) {
                $lang =& JFactory::getLanguage();
                $lang->load('com_community', JPATH_SITE);
                $langString = JText::_('CC ACTIVITIES NEW GROUP DISCUSSION');
                $discussions = $model->getJomsocialDiscussions($jsdisc);
                foreach ($discussions as $d) {
                    $jsUser = CFactory::getUser($d->creator);
                    $profileLink = JURI::root() . CRoute::_('index.php?option=com_community&view=profile&userid=' . $d->creator);
                    $discLink = JURI::root() . CRoute::_('index.php?option=com_community&view=groups&task=viewdiscussion&groupid=' . $d->groupid . '&topicid=' . $d->id);
                    $groupLink = JURI::root() . CRoute::_('index.php?option=com_community&view=groups&task=viewgroup&groupid=' . $d->groupid);
                    $thumb = '<a href="' . $profileLink . '"><img src="' . $jsUser->getThumbAvatar() . '" alt="' . $jsUser->name . '" title="' . $jsUser->name . '" border="0" /></a>';
                    $discTmp = str_ireplace('{actor}', $jsUser->name, $langString);
                    $discTmp = str_ireplace('{topic_url}', $discLink, $discTmp);
                    $discTmp = str_ireplace('{topic}', $d->title, $discTmp);
                    $discTmp = str_ireplace('%1$s', $groupLink, $discTmp);
                    $discTmp = str_ireplace('%2$s', $d->name, $discTmp);
                    $discTmp .= ':<br />' . $d->message;
                    $discTmp = '<table valign="top">
				    <tr>
					<td valign="top">' . $thumb . '</td>
					<td valign="top" style="padding: 0 0 0 10px;">' . $discTmp . '</td>
				    </tr>
				</table>';
                    $discTmp = str_ireplace('<#jsDiscussionContent#>', $discTmp, $jsdr);
                    $jsDiscussions .= $discTmp;
                }
                $jsDiscussions = str_ireplace(array('<#jomsocialdiscussionsrepeater#>', '<#/jomsocialdiscussionsrepeater#>'), '', $jsDiscussions);
            }
            $aecContent = '';
            if (@$aec) {
                $aecConfig = $model->getAECconfig();
                $aecPlans = $model->getAECplans($aec);
                foreach ($aecPlans as $a) {
                    $aecParams = unserialize(base64_decode($a->params));
                    $link = JURI::root() . JRoute::_('index.php?option=com_acctexp&view=subscribe');
                    $aecTmp = '<tr>
				    <td valign="top"><a href="' . $link . '">' . $a->name . '</a></td>
				    <td valign="top" style="padding: 0 0 0 10px;">' . $a->desc . '</td>
				    <td valign="top" style="padding: 0 0 0 10px;">' . $aecParams['full_amount'] . ' ' . $aecConfig['standard_currency'] . '</td>
				</tr>';
                    $aecTmp = str_ireplace('<#aeccontent#>', $aecTmp, $aecr);
                    $aecContent .= $aecTmp;
                }
                $aecContent = str_ireplace(array('<#aecrepeater#>', '<#/aecrepeater#>'), '', $aecContent);
            }
            $ambraContent = '';
            if (@$ambra) {
                require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_ambrasubs' . DS . 'helpers' . DS . '_base.php';
                require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_ambrasubs' . DS . 'helpers' . DS . 'config.php';
                $ambraConfig = AmbrasubsConfig::getInstance();
                $ambraPre = str_ireplace('$', '\\$', $ambraConfig->get('currency_preval', '$'));
                $ambraPost = $ambraConfig->get('currency_postval', '');
                $ambraPlans = $model->getAmbra($ambra);
                foreach ($ambraPlans as $a) {
                    $link = JURI::root() . JRoute::_('index.php?option=com_ambrasubs&controller=subscriptions&task=new&id=' . $a->id);
                    $img = '<img src="' . JURI::root() . $a->img . '" alt="' . $a->title . '" title="' . $a->title . '" border="0" />';
                    $ambraTmp = '<tr>
				    <td valign="top"><a href="' . $link . '">' . $img . '</a></td>
				    <td valign="top"><a href="' . $link . '">' . $a->title . '</a><br />' . $a->description . '</td>
				    <td valign="top" style="padding: 0 0 0 10px;" nowrap="nowrap">' . $ambraPre . '' . $a->value . '' . $ambraPost . '</td>
				</tr>';
                    $ambraTmp = str_ireplace('<#ambracontent#>', $ambraTmp, $ambrar);
                    $ambraContent .= $ambraTmp;
                }
                $ambraContent = str_ireplace(array('<#ambrarepeater#>', '<#/ambrarepeater#>'), '', $ambraContent);
            }
            // remove tiny mce stuff like mce_src="..."
            $content = preg_replace('(mce_style=".*?")', '', $content);
            $content = preg_replace('(mce_src=".*?")', '', $content);
            $content = preg_replace('(mce_href=".*?")', '', $content);
            $content = preg_replace('(mce_bogus=".*?")', '', $content);
            // convert relative to absolute paths
            $content = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $abs_path, $content);
            // create table of contents
            $toc = '';
            foreach ($art_titles as $art_title) {
                $toc .= str_ireplace('<#article_title#>', $art_title, $title_repeater);
            }
            $tableofcontents = preg_replace('!<#title_repeater#[^>]*>(.*)<#/title_repeater#>!is', $toc, $tableofcontents);
            $to_replace = array('<#tableofcontents#>', '<#/tableofcontents#>', '<#title_repeater#>', '<#/title_repeater#>');
            $tableofcontents = str_ireplace($to_replace, '', $tableofcontents);
            $where = '';
            $wEx = array();
            $wIn = array();
            $wCore = array();
            if ($popular_checkbox) {
                if (isset($popular_ex[0])) {
                    foreach ($popular_ex as $p) {
                        $wEx[] = ' c.catid != ' . $p;
                    }
                    $wCore[] = count($wEx) ? ' AND (' . implode(' AND ', $wEx) . ' )' : '';
                }
                if (isset($popular_in[0])) {
                    foreach ($popular_in as $p) {
                        $wIn[] = ' c.catid = ' . $p;
                    }
                    $wCore[] = count($wIn) ? ' AND (' . implode(' OR ', $wIn) . ' )' : '';
                }
                $where = implode('', $wCore);
            }
            $whereK2 = '';
            $wEx = array();
            $wIn = array();
            $wK2 = array();
            if ($populark2_checkbox) {
                if (isset($populark2_ex[0])) {
                    foreach ($populark2_ex as $p) {
                        $wEx[] = ' k.catid != ' . $p;
                    }
                    $wK2[] = count($wEx) ? ' AND (' . implode(' AND ', $wEx) . ' )' : '';
                }
                if (isset($populark2_in[0])) {
                    foreach ($populark2_in as $p) {
                        $wIn[] = ' k.catid = ' . $p;
                    }
                    $wK2[] = count($wIn) ? ' AND (' . implode(' OR ', $wIn) . ' )' : '';
                }
            }
            $whereK2 = implode('', $wK2);
            // create list of popular articles
            if ($popular_checkbox && !$populark2_checkbox) {
                $query = 'SELECT c.id, c.title, c.hits FROM #__content as c
			  WHERE ( c.state = 1 OR c.state = -2 )
			  AND c.hits != 0
			  ' . $where . '
			  ORDER BY c.hits DESC
			  LIMIT 0 , 5';
            } else {
                if ($popular_checkbox && $populark2_checkbox && !$populark2_only) {
                    $query = 'SELECT c.id, c.title, c.hits
			  FROM #__content as c
			  WHERE ( c.state = 1 OR c.state = -2 )
			  AND c.hits != 0
			  ' . $where . '
			  UNION ALL SELECT k.id, k.title, k.hits
			  FROM #__k2_items as k
			  WHERE k.published = 1
			  AND k.hits != 0
			  ' . $whereK2 . '
			  ORDER BY hits DESC
			  LIMIT 0 , 5 ';
                } else {
                    if ($popular_checkbox && $populark2_checkbox && $populark2_only) {
                        $query = 'SELECT k.id, k.title, k.hits
			  FROM #__k2_items as k
			  WHERE k.published = 1
			  AND k.hits != 0
			  ' . $whereK2 . '
			  ORDER BY k.hits DESC
			  LIMIT 0 , 5 ';
                    }
                }
            }
            $popularlist = '';
            if ($popular_checkbox || $populark2_checkbox) {
                $db->setQuery($query);
                $popular = $db->loadObjectList();
                $i = 0;
                if ($popular) {
                    foreach ($popular as $pop) {
                        $i++;
                        $core = false;
                        if ($i > 5) {
                            break;
                        }
                        $query = 'SELECT title FROM #__content WHERE title = "' . $pop->title . '"';
                        $db->setQuery($query);
                        $core = $db->loadResult();
                        if ($core) {
                            $url = '<a href="' . JURI::root() . 'index.php?option=com_content&view=article&id=' . $pop->id . '">' . $pop->title . '</a>';
                        } else {
                            $url = '<a href="' . JURI::root() . 'index.php?option=com_k2&view=item&id=' . $pop->id . '">' . $pop->title . '</a>';
                        }
                        $popularlist .= str_ireplace('<#popular_title#>', $url, $popular_repeater);
                    }
                }
            }
            $popularlist = preg_replace('!<#popular_repeater#[^>]*>(.*)<#/popular_repeater#>!is', $popularlist, $populararticles);
            $to_replace = array('<#populararticles#>', '<#/populararticles#>', '<#popular_repeater#>', '<#/popular_repeater#>');
            $popularlist = str_ireplace($to_replace, '', $popularlist);
            // insert vm products into sidebar
            $vm = '';
            if ($vm_sb) {
                require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'virtuemart.cfg.php';
                $where = '';
                for ($i = 0; $i < count($vm_sb_productids); $i++) {
                    $where .= !$where ? ' AND ' : ' OR ';
                    $where .= ' ( a.product_id = ' . $vm_sb_productids[$i] . ' ' . 'AND b.product_price = ' . $vm_sb_productprices[$i] . ' ' . 'AND c.category_id = ' . $vm_sb_productcats[$i] . ' ) ';
                }
                if ($vm_sb_order == 'price_desc') {
                    $order = ' ORDER BY b.product_price DESC ';
                } else {
                    if ($vm_sb_order == 'price_asc') {
                        $order = ' ORDER BY b.product_price ASC ';
                    } else {
                        if ($vm_sb_order == 'name_desc') {
                            $order = ' ORDER BY a.product_name DESC ';
                        } else {
                            if ($vm_sb_order == 'name_asc') {
                                $order = ' ORDER BY a.product_name ASC ';
                            } else {
                                if ($vm_sb_order == 'cat_desc') {
                                    $order = ' ORDER BY d.category_name DESC ';
                                } else {
                                    if ($vm_sb_order == 'cat_asc') {
                                        $order = ' ORDER BY d.category_name ASC ';
                                    } else {
                                        if ($vm_sb_order == 'random') {
                                            $order = ' ORDER BY RAND() ';
                                        } else {
                                            $order = '';
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                $query = 'SELECT a.product_id,a.product_name,a.product_thumb_image, a.product_s_desc, a.product_desc,
			    b.product_price,b.product_currency,
			    c.category_id,
			    d.category_name
			    FROM #__vm_product as a
			    INNER JOIN #__vm_product_price as b
			    ON a.product_id = b.product_id
			    INNER JOIN #__vm_product_category_xref as c
			    ON a.product_id = c.product_id
			    INNER JOIN #__vm_category as d
			    ON c.category_id = d.category_id
			    WHERE a.product_publish = "Y"
			    ' . $where . '
			    AND a.product_thumb_image != ""
			    ' . $order;
                $db->setQuery($query);
                $products = $db->loadObjectList();
                foreach ($products as $prod) {
                    $product_content = '';
                    if ($vm_sb_link) {
                        $product_content .= '<a href="' . JURI::root() . 'index.php?page=shop.product_details&flypage=' . FLYPAGE . '&product_id=' . $prod->product_id . '&category_id=' . $prod->category_id . '&option=com_virtuemart">';
                    }
                    $product_content .= $prod->product_name . '<br />';
                    if ($vm_sb_price) {
                        if ($vm_sb_cf) {
                            $product_content .= $prod->product_currency . ' ' . number_format($prod->product_price, 2) . '<br />';
                        } else {
                            $product_content .= number_format($prod->product_price, 2) . ' ' . $prod->product_currency . '<br />';
                        }
                    }
                    if ($vm_sb_img) {
                        $product_content .= '<img src="' . JURI::root() . 'components/com_virtuemart/shop_image/product/' . $prod->product_thumb_image . '" border="0" />';
                    }
                    if ($vm_short_desc) {
                        $product_content .= '<p>' . $prod->product_s_desc . '</p>';
                    }
                    if ($vm_desc) {
                        $product_content .= '<p>' . $prod->product_desc . '</p>';
                    }
                    if ($vm_sb_link) {
                        $product_content .= '</a>';
                    }
                    $vm .= str_ireplace('<#vm_content#>', $product_content, $vm_repeater);
                }
            }
            $vm = preg_replace('!<#vm_repeater#[^>]*>(.*)<#/vm_repeater#>!is', $vm, $vm_products);
            $to_replace = array('<#vm_products#>', '<#/vm_products#>', '<#vm_repeater#>', '<#/vm_repeater#>');
            $vm = str_ireplace($to_replace, '', $vm);
            // twitter
            $tw = str_ireplace('<#twitter-name#>', $twitter_name, $twitter);
            $to_replace = array('<#twitter#>', '<#/twitter#>');
            $tw = str_ireplace($to_replace, '', $tw);
            // convert relative to absolute paths
            $tw = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $imagepath, $tw);
            // facebook
            $fb = str_ireplace('<#facebook-url#>', $facebook_url, $facebook);
            $to_replace = array('<#facebook#>', '<#/facebook#>');
            $fb = str_ireplace($to_replace, '', $fb);
            // convert relative to absolute paths
            $fb = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $imagepath, $fb);
            // myspace
            $ms = str_ireplace('<#myspace-name#>', $myspace_name, $myspace);
            $to_replace = array('<#myspace#>', '<#/myspace#>');
            $ms = str_ireplace($to_replace, '', $ms);
            // convert relative to absolute paths
            $ms = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $imagepath, $ms);
            // facebook share
            $to_replace = array('<#facebook_share#>', '<#/facebook_share#>');
            $fbs = str_ireplace($to_replace, '', $fbs);
            // convert relative to absolute paths
            $fbs = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $imagepath, $fbs);
            // modify paths of intro-text
            // remove tiny mce stuff like mce_src="..."
            $intro_text = preg_replace('(mce_style=".*?")', '', $intro_text);
            $intro_text = preg_replace('(mce_src=".*?")', '', $intro_text);
            $intro_text = preg_replace('(mce_href=".*?")', '', $intro_text);
            $intro_text = preg_replace('(mce_bogus=".*?")', '', $intro_text);
            // convert relative to absolute paths
            $intro_text = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $abs_path, $intro_text);
            // end paths intro-text
            // re-open the template file
            $filename = JPATH_ADMINISTRATOR . DS . "components" . DS . "com_joomailermailchimpintegration" . DS . "templates" . DS . $template_folder . DS . "template.html";
            $template = JFile::read($filename, false, filesize($filename));
            // create absolute image paths
            $imagepath = ' src="' . JURI::root() . 'administrator/components/com_joomailermailchimpintegration/templates/' . $template_folder . '/';
            $template = preg_replace('#(src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $imagepath . '$2$3', $template);
            $imagepath = " url('" . JURI::root() . "administrator/components/com_joomailermailchimpintegration/templates/" . $template_folder . "/";
            $template = preg_replace('#(\\s*)url?\\([\'"]?[../]*[\'"]?#i', $imagepath, $template);
            // insert sidebar editor content
            $sidebar = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $abs_path, $sidebar);
            $template = str_ireplace('<#sidebar#>', $sidebar, $template);
            // insert page title and intro-text
            $subject = str_ireplace('$', '\\$', $subject);
            $template = str_ireplace('<#subject#>', $subject, $template);
            $intro_text = str_ireplace('$', '\\$', $intro_text);
            $template = str_ireplace('<#intro_content#>', $intro_text, $template);
            // insert articles
            $content = str_ireplace('$', '\\$', $content);
            $template = preg_replace('!<#repeater#[^>]*>(.*)<#/repeater#>!is', $content, $template);
            // remove placeholders
            $template = str_ireplace('<#repeater#>', '', $template);
            $template = str_ireplace('<#/repeater#>', '', $template);
            // insert JomSocial Profiles
            if ($jsProfiles) {
                $template = preg_replace('!<#jomsocialprofilesrepeater#[^>]*>(.*)<#/jomsocialprofilesrepeater#>!is', $jsProfiles, $template);
                $template = str_ireplace(array('<#jomsocialprofiles#>', '<#/jomsocialprofiles#>'), '', $template);
            } else {
                $template = preg_replace('!<#jomsocialprofiles#[^>]*>(.*)<#/jomsocialprofiles#>!is', '', $template);
            }
            // insert JomSocial Discussions
            if ($jsDiscussions) {
                $template = preg_replace('!<#jomsocialdiscussionsrepeater#[^>]*>(.*)<#/jomsocialdiscussionsrepeater#>!is', $jsDiscussions, $template);
                $template = str_ireplace(array('<#jomsocialdiscussions#>', '<#/jomsocialdiscussions#>'), '', $template);
            } else {
                $template = preg_replace('!<#jomsocialdiscussions#[^>]*>(.*)<#/jomsocialdiscussions#>!is', '', $template);
            }
            // insert AEC plans
            if ($aecContent) {
                $template = preg_replace('!<#aecrepeater#[^>]*>(.*)<#/aecrepeater#>!is', $aecContent, $template);
                $template = str_ireplace(array('<#aec#>', '<#/aec#>'), '', $template);
            } else {
                $template = preg_replace('!<#aec#[^>]*>(.*)<#/aec#>!is', '', $template);
            }
            // insert Ambra subscriptions
            if ($ambraContent) {
                $template = preg_replace('!<#ambrarepeater#[^>]*>(.*)<#/ambrarepeater#>!is', $ambraContent, $template);
                $template = str_ireplace(array('<#ambra#>', '<#/ambra#>'), '', $template);
            } else {
                $template = preg_replace('!<#ambra#[^>]*>(.*)<#/ambra#>!is', '', $template);
            }
            // insert table of contents
            if ($toc_checkbox && ($articles || $articles_k2)) {
                $tableofcontents = str_ireplace('$', '\\$', $tableofcontents);
                $template = preg_replace('!<#tableofcontents#[^>]*>(.*?)<#/tableofcontents#>!is', $tableofcontents, $template);
            } else {
                $template = preg_replace('!<#tableofcontents#[^>]*>(.*?)<#/tableofcontents#>!is', '', $template);
            }
            //insert popular articles
            if ($popular_checkbox) {
                $popularlist = str_ireplace('$', '\\$', $popularlist);
                $template = preg_replace('!<#populararticles#[^>]*>(.*?)<#/populararticles#>!is', $popularlist, $template);
            } else {
                $template = preg_replace('!<#populararticles#[^>]*>(.*?)<#/populararticles#>!is', '', $template);
            }
            //insert vm products
            if ($vm_sb) {
                $vm = str_ireplace('$', '\\$', $vm);
                $template = preg_replace('!<#vm_products#[^>]*>(.*?)<#/vm_products#>!is', $vm, $template);
            } else {
                $template = preg_replace('!<#vm_products#[^>]*>(.*?)<#/vm_products#>!is', '', $template);
            }
            //replace all the escaped dollars
            $template = str_ireplace('\\$', '$', $template);
            //insert twitter link
            $tw = $twitter_name ? $tw : '';
            $template = preg_replace('!<#twitter#[^>]*>(.*?)<#/twitter#>!is', $tw, $template);
            //insert facebook link
            $fb = $facebook_url ? $fb : '';
            $template = preg_replace('!<#facebook#[^>]*>(.*?)<#/facebook#>!is', $fb, $template);
            //insert myspace link
            $ms = $myspace_name ? $ms : '';
            $template = preg_replace('!<#myspace#[^>]*>(.*?)<#/myspace#>!is', $ms, $template);
            //insert facebook share link
            if ($facebookShareIt) {
                $template = preg_replace('!<#facebook_share#[^>]*>(.*?)<#/facebook_share#>!is', $fbs, $template);
                $metaData = '<meta name="title" content="' . $_POST['campaign_name'] . '" />
			    <meta name="description" content="' . $_POST['facebookShareDesc'] . '" />
			    <link rel="image_src" href="A URL to a thumbnail image" / >';
                $metaData = preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\\s|[.]|\\+)[^"]*"))#i', $abs_path, $metaData);
                $template = str_ireplace('</head>', $metaData . '</head>', $template);
            } else {
                $template = preg_replace('!<#facebook_share#[^>]*>(.*?)<#/facebook_share#>!is', '', $template);
            }
            // create google analytics tracking links
            if ($elements->gaEnabled) {
                $ga = 'utm_source=' . $elements->gaSource . '&utm_medium=' . $elements->gaMedium . '&utm_campaign=' . $elements->gaName . '"';
                $excludedURLs = urldecode($elements->gaExcluded);
                $excludedURLs = explode("\n", $excludedURLs);
                for ($i = 0; $i < count($excludedURLs); $i++) {
                    $excludedURLs[$i] = trim($excludedURLs[$i]);
                }
                $excludedURLs[] = '*|UNSUB|*';
                $regex = '#<a(.*?)>(.*?)</a>#i';
                preg_match_all($regex, $template, $templateLinks, PREG_PATTERN_ORDER);
                if (isset($templateLinks[0])) {
                    foreach ($templateLinks[0] as $link) {
                        if (!strstr($link, 'javascript')) {
                            preg_match_all('#((href)="(?!\\.css)[^"]+)"#i', $link, $oldLink, PREG_PATTERN_ORDER);
                            if (isset($oldLink[0][0])) {
                                $glue = strstr($oldLink[0][0], '?') ? $glue = '&' : ($glue = '?');
                                $oldHref = substr($oldLink[0][0], 0, -1);
                                $addGA = true;
                                foreach ($excludedURLs as $ex) {
                                    if (stristr($oldHref, $ex)) {
                                        $addGA = false;
                                    }
                                }
                                if ($addGA) {
                                    $newLink = preg_replace('#((href)="(?!\\.css)[^"]+)"#i', $oldHref . $glue . $ga . '"', $link);
                                    $template = str_ireplace($oldLink[0][0], $oldHref . $glue . $ga . '"', $template);
                                }
                            }
                        }
                    }
                }
            }
            // prevent preview from being cached
            $metaData = '<meta http-Equiv="Cache-Control" Content="no-cache">
			<meta http-Equiv="Pragma" Content="no-cache">
			<meta http-Equiv="Expires" Content="0">
			<script type="text/javascript" src="' . JURI::root() . 'administrator/components/com_joomailermailchimpintegration/assets/js/jquery.min.js"></script>
			<script type="text/javascript">
			var tmplUrl = "' . JURI::root() . 'tmp/";
			jQuery(document).ready(function() {
			    jQuery("a").click( function(){
				link = jQuery(this).attr("href").replace(tmplUrl,"");
				alert( link );
				void(0);
				return false;
			    });
			});
			</script>';
            if (!stristr($template, "<head>")) {
                $template = str_ireplace('<html>', '<html><head>' . $metaData . '</head>', $template);
            } else {
                $template = str_ireplace('</head>', $metaData . '</head>', $template);
            }
            // create output
            if (!$error) {
                $filename = JPATH_SITE . DS . "tmp/" . $campaign_name_ent . ".html";
                if (JFile::exists($filename)) {
                    JFile::delete($filename);
                }
                $handle = JFile::write($filename, $template);
                if (!$handle) {
                    $response['html'] = '<div style="border: 2px solid #ff0000; margin:15px 0 5px;padding:10px 15px 12px;">' . '<img src="' . JURI::root() . 'administrator/components/com_joomailermailchimpintegration/assets/images/warning.png" align="left"/>' . '<div style="padding-left: 45px; line-height: 28px; font-size: 14px;">' . JText::sprintf('PERMISSIONS ERROR GLOBAL', $params->get($paramsPrefix . 'archiveDir', '/administrator/components/com_joomailermailchimpintegration/archive')) . '</div></div>';
                } else {
                    $html_file = JURI::root() . "tmp/" . $campaign_name_ent . ".html";
                    $template = '<iframe src="' . $html_file . '" width="100%" height="800" name="previewIframe" id="previewIframe"></iframe>';
                    $response['html'] = $template;
                }
            }
        }
        // return AJAX response
        echo json_encode($response);
    }
 * 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 General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/
// no direct access
defined('_JEXEC') or die('Restricted Access');
if ($this->jomsocial) {
    require_once JPATH_SITE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'core.php';
}
if ($this->ambra) {
    require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_ambrasubs' . DS . 'helpers' . DS . '_base.php';
    require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_ambrasubs' . DS . 'helpers' . DS . 'config.php';
    $ambraConfig = AmbrasubsConfig::getInstance();
    $ambraPre = $ambraConfig->get('currency_preval', '$');
    $ambraPost = $ambraConfig->get('currency_postval', '');
}
$isWritable = new checkPermissions();
echo $isWritable->check();
$params =& JComponentHelper::getParams('com_joomailermailchimpintegration');
$paramsPrefix = version_compare(JVERSION, '1.6.0', 'ge') ? 'params.' : '';
$MCapi = $params->get($paramsPrefix . 'MCapi');
$MCauth = new MCauth();
// GA data
$gusername = $params->get($paramsPrefix . 'gusername');
$gpw = $params->get($paramsPrefix . 'gpw');
$gprofileid = $params->get($paramsPrefix . 'gprofileid');
if (!$MCapi) {
    echo $MCauth->apiKeyMissing();