<commit_before><?php
/**
 *                    Jojo CMS
 *                ================
 *
 * Copyright 2007-2008 <NAME> <<EMAIL>>
 * Copyright 2007-2008 <NAME> <<EMAIL>>
 * Copyright 2007 <NAME> <<EMAIL>>
 *
 * See the enclosed file license.txt for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME> <<EMAIL>>
 * @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
 * @link    http://www.jojocms.org JojoCMS
 */

class Jojo_Plugin_Jojo_Newsletter extends Jojo_Plugin
{
    /**
     * Get all the bits of the news letter and put them all together
     */
    public static function assembleNewsletter($id, $online=false, $inline=false)
    {
        global $smarty;
        $result = array();

        /* Get the newsletter from the database */
        $newsletter = Jojo::selectRow('SELECT * FROM {newsletter} WHERE id = ?', $id);
        if (!isset($newsletter)) {
            return false;
        }
        $css = Jojo::getOption('css-email', '');
        $tablelists = (boolean)(Jojo::getOption('newslettercss_lists', 'no') == 'yes');
        $newscss = array();
        if ($css) {
            $styles = Jojo::ta2array($css);
            foreach ($styles as $k => $s) {
                $style = explode('{', $s);
                $newscss[$k]['tag'] = $style[0];
                $newscss[$k]['style'] = trim($style[1], '}');
                $smarty->assign(str_replace(array(' ','=','"'), '', $newscss[$k]['tag']) .'css', $newscss[$k]['style']);
            }
        }
        $smarty->assign('htmlintro', true);
        // convert any relative urls to absolute
        $newsletter['intro'] = Jojo::relative2absolute($newsletter['intro'], _SITEURL);
        $newsletter['outro'] = Jojo::relative2absolute($newsletter['outro'], _SITEURL);
        // convert lists into table-based layout with manual bullets and p tags for Outlook if option is enabled
        // add inline styling to tags based on option css settings
        $newsletter['intro'] = $newsletter['intro'] ? Jojo::inlineStyle($newsletter['intro'], $css, $tablelists) : '';
        $newsletter['outro'] = $newsletter['outro'] ? Jojo::inlineStyle($newsletter['outro'], $css, $tablelists) : '';

        // convert text encoding to utf-8 for consistent foreign character display
        $newsletter['intro'] = mb_convert_encoding($newsletter['intro'], 'HTML-ENTITIES', 'UTF-8');
        $newsletter['outro'] = mb_convert_encoding($newsletter['outro'], 'HTML-ENTITIES', 'UTF-8');
        // convert htmlentities to unicode for more consistent display
        $newsletter['intro'] = preg_replace('~^(&([a-zA-Z0-9]);)~',htmlentities('${1}'),$newsletter['intro']); 
        $newsletter['outro'] = preg_replace('~^(&([a-zA-Z0-9]);)~',htmlentities('${1}'),$newsletter['outro']); 

        $smarty->assign('newsletter', $newsletter);
        $smarty->assign('online', $online);
        $smarty->assign('inline', $inline);

        /* Get the templateid */
        $templateid = $newsletter['template'];

        if (Jojo::tableExists('bannerimage') && $newsletter['header_image']) {
            /* Get the image */
            $newsletter_header_image = Jojo::selectRow('SELECT bi_image FROM {bannerimage} WHERE bannerimageid = ?', $newsletter['header_image']);
            $bannerimage = rawurlencode($newsletter_header_image['bi_image']);
            $imagelocation = _SITEURL . '/images/394x213/bannerimages/' . $bannerimage;
            $filename = self::addTemplateImage($templateid, $imagelocation);
            $smarty->assign('bannerimage', $filename);
            $smarty->assign('bannerimagefile', $bannerimage);
        }

        $contentarray = array();
        /* Get results from plugins */
        $contentarray = Jojo::applyFilter('jojo_newslettercontent', $contentarray, $id);
        $smarty->assign('contentarray', $contentarray);
        
       /* Allow plugins to add anything else they need to */
        Jojo::runHook('assembleNewsletter', array('templateid' => $templateid, 'newsletter' => $newsletter));


        /* Add all the images to the database */
        $result['subject']  = $newsletter['name'];
        $result['date']  = $newsletter['date'];
        $result['template'] = $templateid;
        $result['content']  = $smarty->fetch('jojo_newsletter_content.tpl');
        $result['sender'] = ($newsletter['sender']) ? $newsletter['sender'] : Jojo::getOption('phplist_fromaddress');

        /* Get the list id's this is going to */
        $lists = Jojo::selectQuery('SELECT listid FROM {newsletter_list} WHERE newsletterid = ?', $id);
        $result['lists'] = array();
        foreach($lists as $list) {
            $result['lists'][] = $list['listid'];
        }

        return $result;
    }

    function _getContent()
    {
        global $smarty;
        $content = array();
<commit_msg>Newsletter: Fix bug with csstoinline doctype insertion by only calling it once, on the final html. Do encoding once likewise.<commit_after><?php
/**
 *                    Jojo CMS
 *                ================
 *
 * Copyright 2007-2008 <NAME> <<EMAIL>>
 * Copyright 2007-2008 <NAME> <<EMAIL>>
 * Copyright 2007 <NAME> <<EMAIL>>
 *
 * See the enclosed file license.txt for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME> <<EMAIL>>
 * @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
 * @link    http://www.jojocms.org JojoCMS
 */

class Jojo_Plugin_Jojo_Newsletter extends Jojo_Plugin
{
    /**
     * Get all the bits of the news letter and put them all together
     */
    public static function assembleNewsletter($id, $online=false, $inline=false)
    {
        global $smarty;
        $result = array();

        /* Get the newsletter from the database */
        $newsletter = Jojo::selectRow('SELECT * FROM {newsletter} WHERE id = ?', $id);
        if (!isset($newsletter)) {
            return false;
        }
        $css = Jojo::getOption('css-email', '');
        $tablelists = (boolean)(Jojo::getOption('newslettercss_lists', 'no') == 'yes');
        $newscss = array();
        if ($css) {
            $styles = Jojo::ta2array($css);
            foreach ($styles as $k => $s) {
                $style = explode('{', $s);
                $newscss[$k]['tag'] = $style[0];
                $newscss[$k]['style'] = trim($style[1], '}');
                $smarty->assign(str_replace(array(' ','=','"'), '', $newscss[$k]['tag']) .'css', $newscss[$k]['style']);
            }
        }
        $smarty->assign('htmlintro', true);
        // convert any relative urls to absolute
        $newsletter['intro'] = Jojo::relative2absolute($newsletter['intro'], _SITEURL);
        $newsletter['outro'] = Jojo::relative2absolute($newsletter['outro'], _SITEURL);
        // convert lists into table-based layout with manual bullets and p tags for Outlook if option is enabled
        // add inline styling to tags based on option css settings

        // convert text encoding to utf-8 for consistent foreign character display
        $newsletter['intro'] = mb_convert_encoding($newsletter['intro'], 'HTML-ENTITIES', 'UTF-8');
        $newsletter['outro'] = mb_convert_encoding($newsletter['outro'], 'HTML-ENTITIES', 'UTF-8');
        // convert htmlentities to unicode for more consistent display
        $newsletter['intro'] = preg_replace('~^(&([a-zA-Z0-9]);)~',htmlentities('${1}'),$newsletter['intro']); 
        $newsletter['outro'] = preg_replace('~^(&([a-zA-Z0-9]);)~',htmlentities('${1}'),$newsletter['outro']); 

        $smarty->assign('newsletter', $newsletter);
        $smarty->assign('online', $online);
        $smarty->assign('inline', $inline);

        /* Get the templateid */
        $templateid = $newsletter['template'];

        if (Jojo::tableExists('bannerimage') && $newsletter['header_image']) {
            /* Get the image */
            $newsletter_header_image = Jojo::selectRow('SELECT bi_image FROM {bannerimage} WHERE bannerimageid = ?', $newsletter['header_image']);
            $bannerimage = rawurlencode($newsletter_header_image['bi_image']);
            $imagelocation = _SITEURL . '/images/394x213/bannerimages/' . $bannerimage;
            $filename = self::addTemplateImage($templateid, $imagelocation);
            $smarty->assign('bannerimage', $filename);
            $smarty->assign('bannerimagefile', $bannerimage);
        }

        $contentarray = array();
        /* Get results from plugins */
        $contentarray = Jojo::applyFilter('jojo_newslettercontent', $contentarray, $id);
        $smarty->assign('contentarray', $contentarray);
        
       /* Allow plugins to add anything else they need to */
        Jojo::runHook('assembleNewsletter', array('templateid' => $templateid, 'newsletter' => $newsletter));


        /* Add all the images to the database */
        $result['subject']  = $newsletter['name'];
        $result['date']  = $newsletter['date'];
        $result['template'] = $templateid;
        $content = $smarty->fetch('jojo_newsletter_content.tpl');
        $result['content']  =  $css ? Jojo::inlineStyle($content, $css, $tablelists) : $content;
        $result['content'] = mb_convert_encoding($result['content'], 'HTML-ENTITIES', 'UTF-8');
        $result['sender'] = ($newsletter['sender']) ? $newsletter['sender'] : Jojo::getOption('phplist_fromaddress');

        /* Get the list id's this is going to */
        $lists = Jojo::selectQuery('SELECT listid FROM {newsletter_list} WHERE newsletterid = ?', $id);
        $result['lists'] = array();
        foreach($lists as $list) {
            $result['lists'][] = $list['listid'];
        }

        return $result;
    }

    function _getContent()
    {
        global $smarty;
        $content = array();

{'fraction_non_alphanumeric': 0.17335214674460794, 'fraction_numerical': 0.008163676678089095, 'mean_word_length': 2.94866693195384, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '190559', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2881, 'n_words': 651}