<commit_before><?php
/**
 *    This file is part of TOXID Module for OXID eShop CE/PE/EE.
 *
 *    TOXID is free software: you can redistribute it and/or modify
 *    it under the terms of the MIT License.
 *
 *
 * @link      http://toxid.org
 * @link      http://marmalade.de
 * @package   core
 * @copyright (C) marmalade.de 2011-2013
 */

/**
 * toxid Class
 *
 */
class toxidCurl extends oxSuperCfg
{
    /**
     * toxidCurl class instance.
     *
     * @var toxidcurl instance
     */
    private static $_instance = null;

    /**
     * array of content snippets
     *
     * @var array
     */
    public static $aToxidSnippets = null;

    /**
     * object
     *
     * @var object
     */
    private $_oSxToxid = null;

    /**
     * string with XML-content
     *
     * @var string
     */
    private $_sPageContent = null;

    /**
     * string with XML-content-title
     *
     * @var string
     */
    public $_sPageTitle = null;
    
    /**
     * string with XML-content-description
     *
     * @var string
     */
    public $_sPageDescription = null;
        
    /**
     * string with XML-content-keywords
     *
     * @var string
     */
    public $_sPageKeywords = null;
    
    /**
     * stores URL from which typo3 content is loaded.
     * @var array
     */
    protected $_aSourceUrlByLang = null;

    /**
     * Stores URI to which typo3 urls will be converted, so it will be loaded in oxid
     * @var array
     */
    protected $_aRewriteStartUrl = null;

    /**
     * array of string with language specific toxidUrlParam
     * @var array
     */
    protected $_aToxidLangUrlParam = null;

    /**
     * stores search url by active language
     * @var array
     */
    protected $_aSearchUrl = null;

    /**
     * stores search results
     * @var array
     */
    protected $_aSearchCache = array();

    /**
     * returns SimpleXMLElement object from Typo3 xml
     * @param bool $blReset
     * @return SimpleXMLElement
     */
    protected function _getXmlObject($blReset = false)
    {
        if ($this->_oSxToxid !== null && !$blReset) {
            return $this->_oSxToxid;
        }
        $this->_oSxToxid = simplexml_load_string($this->_getXmlFromTypo3($blReset));
        return $this->_oSxToxid;

    }

    /**
     * returns snippet text from xml object
     * @param $sSnippet
     * @return SimpleXMLElement
     */
    protected function _getSnippetFromXml($sSnippet)
    {
        $oTypo3Xml = $this->_getXmlObject();
        $aXpathSnippets = $oTypo3Xml->xpath('//'.$sSnippet.'[1]');
        $sText = $aXpathSnippets[0];

        return $sText;

    }

    /**
     * returns the called snippet
     * @param string $snippet
     * @param bool $blMultiLang
     * @return string
     */
    public function getCmsSnippet($snippet=null, $blMultiLang = false, $customPage = null)
    {
        if($snippet == null) {
            return '<strong style="color:red;">TOXID: Please add part, you want to display!</strong>';
        }

        if ($customPage != '') {
            $this->_aCustomPage = $customPage;
        }

        $sText = $this->_getSnippetFromXml($snippet);
        $sText = $this->_rewriteUrls($sText, null, $blMultiLang);

        $sPageTitle = $this->_rewriteUrls($this->_getSnippetFromXml('//metadata//title', null, $blMultiLang));

        $sPageDescription = $this->_rewriteUrls($this->_getSnippetFromXml('//metadata//description', null, $blMultiLang));

        $sPageKeywords = $this->_rewriteUrls($this->_getSnippetFromXml('//metadata//keywords', null, $blMultiLang));

        $oConf   = $this->getConfig();
        $sShopId = $oConf->getActiveShop()->getId();
        $sLangId = oxLang::getInstance()->getBaseLanguage();
        $sText   = oxUtilsView::getInstance()->parseThroughSmarty(
            $sText,
            $snippet.'_'.$sShopId.'_'.$sLangId,
            null,
            true
        );

        $this->_sPageTitle = oxUtilsView::getInstance()->parseThroughSmarty(
            $sPageTitle,
            '//metadata//title_'.$sShopId.'_'.$sLangId,
            null,
            true
        );
        
        $this->_sPageDescription = oxUtilsView::getInstance()->parseThroughSmarty(
            $sPageDescription,
            '//metadata//description_'.$sShopId.'_'.$sLangId,
            null,
            true
        );

        $this->_sPageKeywords = oxUtilsView::getInstance()->parseThroughSmarty(
            $sPageKeywords,
            '//metadata//keywords_'.$sShopId.'_'.$sLangId,
            null,
            true
        );
        
        $this->_aCustomPage = null;

        /* if actual site is ssl-site, replace all image-sources with ssl-urls */
        if ($oConf->isSsl())
        {
            
            $aSslUrl = $oConf->getShopConfVar('aToxidCurlSourceSsl', $sShopId);
            $sSslUrl = $aSslUrl[$sLangId];
            $oldSrc = $this->_getToxidLangSource($sLangId);
            
            $sText = $this->replaceNonSslUrls( $sText, $sSslUrl, $oldSrc );
        }

        $sShopCharset = oxRegistry::getLang()->translateString('charset');
        if($oConf->getConfigParam('iUtfMode') === 0)
        {
            $sText = utf8_decode($sText);
            return $sText;
        } else {
            return $sText;
        }
    }

    private function replaceNonSslUrl( $sText, $sSslUrl, $oldSrc )
    {
        if (!empty($sSslUrl))
        {

            $newSrc = $sSslUrl;

            if ($oldSrc != "" && $newSrc != "")
            {
                $sText= str_replace('src="'.$oldSrc, 'src="'.$newSrc, $sText);<commit_msg>Some helpful comments<commit_after><?php
/**
 *    This file is part of TOXID Module for OXID eShop CE/PE/EE.
 *
 *    TOXID is free software: you can redistribute it and/or modify
 *    it under the terms of the MIT License.
 *
 *
 * @link      http://toxid.org
 * @link      http://marmalade.de
 * @package   core
 * @copyright (C) marmalade.de 2011-2013
 */

/**
 * toxid Class
 *
 */
class toxidCurl
{
    /**
     * array of content snippets
     *
     * @var array
     */
    public static $aToxidSnippets = null;

    /**
     * object
     *
     * @var object
     */
    private $_oSxToxid = null;

    /**
     * string with XML-content
     *
     * @var string
     */
    private $_sPageContent = null;

    /**
     * string with XML-content-title
     *
     * @var string
     */
    public $_sPageTitle = null;
    
    /**
     * string with XML-content-description
     *
     * @var string
     */
    public $_sPageDescription = null;
        
    /**
     * string with XML-content-keywords
     *
     * @var string
     */
    public $_sPageKeywords = null;
    
    /**
     * stores URL from which typo3 content is loaded.
     * @var array
     */
    protected $_aSourceUrlByLang = null;

    /**
     * Stores URI to which typo3 urls will be converted, so it will be loaded in oxid
     * @var array
     */
    protected $_aRewriteStartUrl = null;

    /**
     * array of string with language specific toxidUrlParam
     * @var array
     */
    protected $_aToxidLangUrlParam = null;

    /**
     * stores search url by active language
     * @var array
     */
    protected $_aSearchUrl = null;

    /**
     * stores search results
     * @var array
     */
    protected $_aSearchCache = array();

    /**
     * returns SimpleXMLElement object from Typo3 xml
     * @param bool $blReset
     * @return SimpleXMLElement
     */
    protected function _getXmlObject($blReset = false)
    {
        if ($this->_oSxToxid !== null && !$blReset) {
            return $this->_oSxToxid;
        }
        $this->_oSxToxid = simplexml_load_string($this->_getXmlFromTypo3($blReset));
        return $this->_oSxToxid;

    }

    /**
     * returns snippet text from xml object
     * @param $sSnippet
     * @return SimpleXMLElement
     */
    protected function _getSnippetFromXml($sSnippet)
    {
        $oTypo3Xml = $this->_getXmlObject();
        $aXpathSnippets = $oTypo3Xml->xpath('//'.$sSnippet.'[1]');
        $sText = $aXpathSnippets[0];

        return $sText;

    }

    /**
     * returns the called snippet
     * @param string $snippet
     * @param bool $blMultiLang
     * @return string
     */
    public function getCmsSnippet($snippet=null, $blMultiLang = false, $customPage = null)
    {
        if($snippet == null) {
            return '<strong style="color:red;">TOXID: Please add part, you want to display!</strong>';
        }

        if ($customPage != '') {
            $this->_aCustomPage = $customPage;
        }

        $sText = $this->_getSnippetFromXml($snippet);
        $sText = $this->_rewriteUrls($sText, null, $blMultiLang);

        $sPageTitle = $this->_rewriteUrls($this->_getSnippetFromXml('//metadata//title', null, $blMultiLang));

        $sPageDescription = $this->_rewriteUrls($this->_getSnippetFromXml('//metadata//description', null, $blMultiLang));

        $sPageKeywords = $this->_rewriteUrls($this->_getSnippetFromXml('//metadata//keywords', null, $blMultiLang));

        $oConf   = $this->getConfig();
        $sShopId = $oConf->getActiveShop()->getId();
        $sLangId = oxLang::getInstance()->getBaseLanguage();
        $sText   = oxUtilsView::getInstance()->parseThroughSmarty(
            $sText,
            $snippet.'_'.$sShopId.'_'.$sLangId,
            null,
            true
        );

        $this->_sPageTitle = oxUtilsView::getInstance()->parseThroughSmarty(
            $sPageTitle,
            '//metadata//title_'.$sShopId.'_'.$sLangId,
            null,
            true
        );
        
        $this->_sPageDescription = oxUtilsView::getInstance()->parseThroughSmarty(
            $sPageDescription,
            '//metadata//description_'.$sShopId.'_'.$sLangId,
            null,
            true
        );

        $this->_sPageKeywords = oxUtilsView::getInstance()->parseThroughSmarty(
            $sPageKeywords,
            '//metadata//keywords_'.$sShopId.'_'.$sLangId,
            null,
            true
        );
        
        $this->_aCustomPage = null;

        /* if actual site is ssl-site, replace all image-sources with ssl-urls */
        if ($oConf->isSsl())
        {
            
            $aSslUrl = $oConf->getShopConfVar('aToxidCurlSourceSsl', $sShopId);
            $sSslUrl = $aSslUrl[$sLangId];
            $oldSrc = $this->_getToxidLangSource($sLangId);
            
            $sText = $this->replaceNonSslUrls( $sText, $sSslUrl, $oldSrc );
        }

        $sShopCharset = oxRegistry::getLang()->translateString('charset');
        if($oConf->getConfigParam('iUtfMode') === 0)
        {
            $sText = utf8_decode($sText);
            return $sText;
        } else {
            return $sText;
        }
    }
    
    /**
     * Replaces Links to SSL-Links if configured.
     * 
     * @param string $sText
     * @param string $sSslUrl
     * @param string $oldSrc
     * @return string
     */
    private function replaceNonSslUrl( $sText, $sSslUrl, $oldSrc )
    {
        if (!empty($sSslUrl))
        {

            $newSrc = $sSslUrl;

            if ($oldSrc != "" && $newSrc != "")
            {
                $sText= str_replace('src="'.$oldSrc, 'src="'.$newSrc, $sText);
{'fraction_non_alphanumeric': 0.13554645781416874, 'fraction_numerical': 0.003203987184051264, 'mean_word_length': 1.9308815858111632, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1502234', 'n_tokens_mistral': 3595, 'n_tokens_neox': 3464, 'n_words': 710}