<commit_before><?php
class Vpc_Basic_Text_Row extends Vps_Model_Proxy_Row
{
    private $_classes;
    private $_componentClass;

    protected function _init()
    {
        parent::_init();
        $this->_componentClass = $this->getModel()->getComponentClass();
        $this->_classes = Vpc_Abstract::getChildComponentClasses($this->_componentClass, 'child');
    }

    //für Component und Row
    public function getContentParts($content = null)
    {
        $classes = $this->_classes;

        $usedChildComponentNrs = array();

        $componentId = $this->component_id;
        if (is_null($content)) $content = $this->content;

        $ret = array();
                            //1   2                 3                              4              5
        while (preg_match('#^(.*)(<img[^>]+src=[\n ]*"([^"]*)"[^>]*>|<a[^>]+href=[\n ]*"([^"]*)"[^>]*>)(.*)$#Usi', $content, $m)) {

            if ($m[1] != '') {
                $ret[] = $m[1];
            }

            if (isset($classes['image']) && $m[3] != ''
                && preg_match('#/media/([^/]+)/([^/]+)#', $m[3], $m2)) {
                //"/media/$class/$id/$rule/$type/$checksum/$filename.$extension$random"
                $isInvalid = false;
                $childComponentId = $m2[2];
                if (substr($childComponentId, 0, strlen($componentId)+2)
                            == $componentId.'-i') {
                    $nr = substr($childComponentId, strlen($componentId)+2);
                    if (!in_array('i'.$nr, $usedChildComponentNrs)) {
                        $usedChildComponentNrs[] = 'i'.$nr;
                        $ret[] = array('type'=>'image', 'nr'=>$nr, 'html'=>$m[2]);
                    } else {
                        $isInvalid = true;
                    }
                } else {
                    $isInvalid = true;
                }
                if ($isInvalid) {
                    $ret[] = array('type'=>'invalidImage',
                                    'src'=>$m[3],
                                    'componentClass'=>$m2[1],
                                    'componentId'=>$childComponentId,
                                    'html'=>$m[2]);
                }
            } else if (isset($classes['image']) && $m[3] != '') {
                $ret[] = array('type'=>'invalidImage', 'src'=>$m[3], 'html'=>$m[2]);
            } else if ($m[3] != '') {
                $ret[] = $m[2];
            }

            if ((isset($classes['link']) || isset($classes['download'])) && $m[4] != ''
                && preg_match('#/?([^/]+)$#', $m[4], $m2)) {

                $isInvalid = false;
                $childComponentId = $m2[1];
                if (isset($classes['link'])
                    && substr($childComponentId, 0, strlen($componentId)+2)
                            == $componentId.'-l') {
                    $nr = substr($childComponentId, strlen($componentId)+2);
                    if (!in_array('l'.$nr, $usedChildComponentNrs)) {
                        $usedChildComponentNrs[] = 'l'.$nr;
                        $ret[] = array('type'=>'link', 'nr'=>$nr, 'html'=>$m[2]);
                    } else {
                        $ret[] = array('type'=>'invalidLink',
                                    'href'=>$m[4],
                                    'componentId'=>$m2[1],
                                    'html'=>$m[2]);
                    }
                } else if (isset($classes['download'])
                        && substr($childComponentId, 0, strlen($componentId)+2)
                            == $componentId.'-d') {
                    $nr = substr($childComponentId, strlen($componentId)+2);
                    if (!in_array('d'.$nr, $usedChildComponentNrs)) {
                        $usedChildComponentNrs[] = 'd'.$nr;
                        $ret[] = array('type'=>'download', 'nr'=>$nr, 'html'=>$m[2]);
                    } else {
                        $ret[] = array('type'=>'invalidDownload',
                                    'href'=>$m[4],
                                    'componentId'=>$m2[1],
                                    'html'=>$m[2]);
                    }
                } else if (isset($classes['link']) && preg_match('#-l[0-9]+$#', $m2[1])) {
                    $ret[] = array('type'=>'invalidLink',
                                   'href'=>$m[4],
                                   'componentId'=>$m2[1],
                                   'html'=>$m[2]);
                } else if (isset($classes['download']) && preg_match('#-d[0-9]+$#', $m2[1])) {
                    $ret[] = array('type'=>'invalidDownload',
                                   'href'=>$m[4],
                                   'componentId'=>$m2[1],
                                   'html'=>$m[2]);
                } else {
                    $ret[] = array('type'=>'invalidLink', 'href'=>$m[4], 'html'=>$m[2]);
                }
            } else if (isset($classes['link']) && $m[4] != '') {
                $ret[] = array('type'=>'invalidLink', 'href'=>$m[4], 'html'=>$m[2]);
            } else if ($m[4] != '') {
                $ret[] = $m[2];
            }

            $content = $m[5];
        }
        if(!$m) $ret[] = $content;

        return $ret;
    }

    public function getMaxChildComponentNr($type)
    {
        $select = $this->getModel()->select();
        $select->order('nr', 'DESC');
        $select->limit(1);
        $select->whereEquals('component', $type);
        $row = $this->getChildRows('ChildComponents', $select)->current();
        if (!$row) return 0;
        return $row->nr;
    }

    protected function _beforeDelete()
    {
        $table = new Vpc_Basic_Text_ChildComponentsModel();
        $rows = $table->fetchAll(array('component_id = ?' => $this->component_id));
        foreach ($rows as $row) {
            $t = substr($row->component, 0, 1);
            $admin = Vpc_Admin::getInstance($this->_classes[$row->component]);
            $admin->delete($this->component_id . '-' . $t.$row->nr);
            $row->delete();
        }
    }

    //childComponents löschen die aus dem html-code entfernt wurden
    protected function _beforeSave()
    {
        $classes = $this->_classes;

        $this->content = $this->tidy($this->content);
        $newParts = $this->getContentParts($this->content);
        $newPartStrings = array();
        foreach ($newParts as $part) {
            if (!is_string($part) ) {
                $newPartStrings[] = $part['type'].$part['nr'];
            }
        }

        $rows = $this->getChildRows('ChildComponents');
        $existingParts = array();
        foreach ($rows as $row) {
            $t = substr($row->component, 0, 1);
            if (!in_array($row->component.$row->nr, $newPartStrings)) {
                $admin = Vpc_Admin::getInstance($classes[$row->component]);
                $admin->delete($this->component_id . '-' . $t.$row->nr);
                $row->delete();
            } else {
                if (!$row->saved) {
                    $row->saved = 1;
                    $row->save();
                }
                $existingParts[] = $row->component.$row->nr;
            }
        }

        foreach ($newParts as $part) {
            if (!is_string($part)
                && !in_array($part['type'].$part['nr'], $existingParts)) {
                $row = $this->createChildRow('ChildComponents');
                $row->component = $part['type'];
                $row->nr = $part['nr'];
                $row->saved = 1;
                $row->save();
            }
        }
    }

    public function tidy($html)
    {
        $config = array(
                    'indent'         => true,
                    'output-xhtml'   => true,
                    'clean'          => false,
                    'wrap'           => '86',
                    'doctype'        => 'omit',
                    'drop-proprietary-attributes' => true,
                    'drop-font-tags' => true,
                    'word-2000'      => true,
                    'show-body-only' => true,
                    'bare'           => true,
                    'enclose-block-text'=>true,
                    'enclose-text'   => true,
                    'join-styles'    => false,
                    'join-classes'   => false,
                    'logical-emphasis' => true,
                    'lower-literals' => true,
                    'literal-attributes' => false,
                    'indent-spaces' => 2,
                    'quote-nbsp'     => true,
                    'output-bom'     => false,
                    'char-encoding'  =>'utf8',
                    'newline'        =>'LF',
                    'uppercase-tags' =>'false'
                    );
        $enableTidy = Vpc_Abstract::getSetting($this->_componentClass, 'enableTidy');
        $enableFontSize = Vpc_Abstract::getSetting($this->_componentClass, 'enableFontSize');
        if ($enableFontSize){
            $config['drop-font-tags'] = false;
        }
        if (class_exists('tidy') && $enableTidy) {
            $tidy = new tidy;
            $html = str_replace('&nbsp;', '#nbsp#', $html); //einstellungen oben funktionieren nicht richtig
            $tidy->parseString($html, $config, 'utf8');
            $tidy->cleanRepair();
            $html = $tidy->value;
            $parser = new Vpc_Basic_Text_Parser($this);
            $parser->setEnableColor(Vpc_Abstract::getSetting($this->_componentClass, 'enableColors'));
            $parser->setEnableTagsWhitelist(Vpc_Abstract::getSetting($this->_componentClass, 'enableTagsWhitelist'));
            $parser->setEnableStyles(Vpc_Abstract::getSetting($this->_componentClass, 'enableStyles'));
            $html = $parser->parse($html);
            $tidy->parseString($html, $config, 'utf8');
            $tidy->cleanRepair();
            $html = $tidy->value;
            $html = str_replace('#nbsp#', '&nbsp;', $html);
        }

        $classes = $this->_classes;

        $newContent = '';

        foreach ($this->getContentParts($html) as $part) {
            if (is_string($part)) {
                $newContent .= $part;
            } else if ($part['type'] == 'invalidImage') {
                if (isset($part['componentId'])
                    && class_exists($part['componentClass'])
                    && (strtolower($part['componentClass']) == 'vpc_basic_image_component'
                        || is_subclass_of($part['componentClass'], 'Vpc_Basic_Image_Component'))) {

                    $srcRow = Vpc_Abstract::createModel($part['componentClass'])
                                    ->getRow($part['componentId']);
                    if ($srcRow->imageExists()) {
                        $destRow = Vpc_Abstract::createModel($classes['image'])
                                                ->createRow($srcRow->toArray());
                        $childComponentRow = $this->addChildComponentRow('image', $destRow);
                        $destRow->save();
                        $imageComponent = Vps_Component_Data_Root::getInstance()
                            ->getComponentByDbId($this->component_id.'-i'.$childComponentRow->nr)
                            ->getComponent();
                        $dimension = $imageComponent->getImageDimensions();
                        $newContent .= "<img src=\"".$imageComponent->getImageUrl()."\" ".
                                    "width=\"$dimension[width]\" ".
                                    "height=\"$dimension[height]\" />";
                        continue;
                    }
                }
                $client = new Zend_Http_Client();
                try {
                    $client->setUri($part['src']);
                } catch (Zend_Uri_Exception $e) {
                    //wann relative url mit http_host davor probieren
                    if (isset($_SERVER['HTTP_HOST'])) {
                        $client->setUri('http://'.$_SERVER['HTTP_HOST'].'/'.$part['src']);
                    }
                }
                try {
                    $response = $client->request();
                } catch (Exception $e) {
                    continue;
                }
                if (!$response->isSuccessful()) continue;

                $contentType = $response->getHeader('Content-type');
                if ($contentType == 'image/jpg' || $contentType == 'image/jpeg') {
                    $extension = 'jpg';
                } else if ($contentType == 'image/gif') {
                    $extension = 'gif';
                } else if ($contentType == 'image/png') {
                    $extension = 'png';
                } else {
                    continue;
                }

                $destFileRow = Vpc_Abstract::createModel($classes['image'])
                            ->getReferencedModel('Image')
                            ->createRow();

                $path = explode('?', $part['src']);
                if (preg_match('#([^/]*)\\.[a-z]+$#U', $path[0], $m)) {
                    $srcFileName = Zend_Filter::get($m[1], 'Alnum', array(ENT_QUOTES));
                }
                if (!isset($srcFileName) || !$srcFileName) {
                    $srcFileName = 'download';
                }

                $destFileRow->writeFile($response->getBody(), $srcFileName, $extension, $contentType);

                $destRow = Vpc_Abstract::createModel($classes['image'])->createRow();
                $destRow->vps_upload_id = $destFileRow->id;
                $size = getimagesize($destFileRow->getFileSource());
                $destRow->width = $size[0];
                $destRow->height = $size[1];
                $destRow->filename = $srcFileName;
                $destRow->scale = '';
                $childComponentRow = $this->addChildComponentRow('image', $destRow);
                $destRow->save();
                $imageComponent = Vps_Component_Data_Root::getInstance()
                    ->getComponentByDbId($this->component_id.'-i'.$childComponentRow->nr)
                    ->getComponent();
                $dimension = $imageComponent->getImageDimensions();
                $newContent .= "<img src=\"".$imageComponent->getImageUrl()."\" ".
                            "width=\"$dimension[width]\" ".
                            "height=\"$dimension[height]\" />";

            } else if ($part['type'] == 'invalidLink') {

                $model = Vpc_Abstract::createModel($classes['link']);
                if (isset($part['componentId'])) {
                    try {
                        $srcRow = $model->getRow($part['componentId']);
                    } catch (Vpc_Exception $e) {
                        $srcRow = false;
                    }
                    $linkClasses = Vpc_Abstract::getChildComponentClasses($classes['link'], 'link');
                    if ($srcRow && class_exists($linkClasses[$srcRow->component])) {
                        $linkModel = Vpc_Abstract::createModel($linkClasses[$srcRow->component]);
                        $srcLinkRow = $linkModel->getRow($part['componentId'].'-link');
                        if ($srcLinkRow) {
                            $destRow = $model->createRow();
                            $destRow->component = $srcRow->component;
                            $this->addChildComponentRow('link', $destRow);
                            $destRow->save();
                            $destLinkRow = $linkModel->createRow($srcLinkRow->toArray());
                            $destLinkRow->component_id = $destRow->component_id.'-link';
                            $destLinkRow->save();
                            $newContent .= "<a href=\"{$destRow->component_id}\">";
                            continue;
                        }
                    }
                }
                $destRow = $model->createRow();
                $linkClasses = Vpc_Abstract::getChildComponentClasses($classes['link'], 'link');

                $destRow->component = null;
                if (preg_match('#^mailto:#', $part['href'], $m)) {
                    if (isset($linkClasses['mail']) && $linkClasses['mail']) {
                        $destRow->component = 'mail';
                    }
                } else {
                    if (isset($linkClasses['intern']) && $linkClasses['intern']) {
                        $internLinkPage = Vps_Component_Data_Root::getInstance()
                            ->getPageByPath($part['href']);
                        if ($internLinkPage) {
                            $destRow->component = 'intern';
                        }
                    }
                    if (!$destRow->component && isset($linkClasses['extern']) && $linkClasses['extern']) {
                        $destRow->component = 'extern';
                    }
                }

                if (!$destRow->component) continue; //kein solcher-link möglich
                $this->addChildComponentRow('link', $destRow);
                $destRow->save();
                $destClasses =  Vpc_Abstract::getChildComponentClasses($classes['link'], 'link');

                $row = Vpc_Abstract::createModel($destClasses[$destRow->component])->createRow();
                if ($destRow->component == 'extern') {
                    $row->target = $part['href'];
                } else if ($destRow->component == 'intern') {
                    $row->target = $internLinkPage->dbId;
                } else {
                    preg_match('#^mailto:(.*)\\??(.*)#', $part['href'], $m);
                    $row->mail = $m[1];
                    $m = parse_str($m[2]);
                    $row->subject = isset($m['subject']) ? $m['subject'] : '';
                    $row->text = isset($m['body']) ? $m['body'] : '';
                }
                $row->component_id = $destRow->component_id.'-link';
                $row->save();
                $newContent .= "<a href=\"{$destRow->component_id}\">";

            } else if ($part['type'] == 'invalidDownload') {

                $srcRow = Vpc_Abstract::createModel($classes['download'])
                                ->getRow($part['componentId']);
                if ($srcRow->fileExists()) {
                    $destRow = Vpc_Abstract::createModel($classes['download'])
                                                ->createRow($srcRow->toArray());
                    $this->addChildComponentRow('download', $destRow);
                    $destRow->save();
                    $newContent .= "<a href=\"{$destRow->component_id}\">";<commit_msg>bugfix: es kann vorkommen dass dieser eintrag bereits da ist<commit_after><?php
class Vpc_Basic_Text_Row extends Vps_Model_Proxy_Row
{
    private $_classes;
    private $_componentClass;

    protected function _init()
    {
        parent::_init();
        $this->_componentClass = $this->getModel()->getComponentClass();
        $this->_classes = Vpc_Abstract::getChildComponentClasses($this->_componentClass, 'child');
    }

    //für Component und Row
    public function getContentParts($content = null)
    {
        $classes = $this->_classes;

        $usedChildComponentNrs = array();

        $componentId = $this->component_id;
        if (is_null($content)) $content = $this->content;

        $ret = array();
                            //1   2                 3                              4              5
        while (preg_match('#^(.*)(<img[^>]+src=[\n ]*"([^"]*)"[^>]*>|<a[^>]+href=[\n ]*"([^"]*)"[^>]*>)(.*)$#Usi', $content, $m)) {

            if ($m[1] != '') {
                $ret[] = $m[1];
            }

            if (isset($classes['image']) && $m[3] != ''
                && preg_match('#/media/([^/]+)/([^/]+)#', $m[3], $m2)) {
                //"/media/$class/$id/$rule/$type/$checksum/$filename.$extension$random"
                $isInvalid = false;
                $childComponentId = $m2[2];
                if (substr($childComponentId, 0, strlen($componentId)+2)
                            == $componentId.'-i') {
                    $nr = substr($childComponentId, strlen($componentId)+2);
                    if (!in_array('i'.$nr, $usedChildComponentNrs)) {
                        $usedChildComponentNrs[] = 'i'.$nr;
                        $ret[] = array('type'=>'image', 'nr'=>$nr, 'html'=>$m[2]);
                    } else {
                        $isInvalid = true;
                    }
                } else {
                    $isInvalid = true;
                }
                if ($isInvalid) {
                    $ret[] = array('type'=>'invalidImage',
                                    'src'=>$m[3],
                                    'componentClass'=>$m2[1],
                                    'componentId'=>$childComponentId,
                                    'html'=>$m[2]);
                }
            } else if (isset($classes['image']) && $m[3] != '') {
                $ret[] = array('type'=>'invalidImage', 'src'=>$m[3], 'html'=>$m[2]);
            } else if ($m[3] != '') {
                $ret[] = $m[2];
            }

            if ((isset($classes['link']) || isset($classes['download'])) && $m[4] != ''
                && preg_match('#/?([^/]+)$#', $m[4], $m2)) {

                $isInvalid = false;
                $childComponentId = $m2[1];
                if (isset($classes['link'])
                    && substr($childComponentId, 0, strlen($componentId)+2)
                            == $componentId.'-l') {
                    $nr = substr($childComponentId, strlen($componentId)+2);
                    if (!in_array('l'.$nr, $usedChildComponentNrs)) {
                        $usedChildComponentNrs[] = 'l'.$nr;
                        $ret[] = array('type'=>'link', 'nr'=>$nr, 'html'=>$m[2]);
                    } else {
                        $ret[] = array('type'=>'invalidLink',
                                    'href'=>$m[4],
                                    'componentId'=>$m2[1],
                                    'html'=>$m[2]);
                    }
                } else if (isset($classes['download'])
                        && substr($childComponentId, 0, strlen($componentId)+2)
                            == $componentId.'-d') {
                    $nr = substr($childComponentId, strlen($componentId)+2);
                    if (!in_array('d'.$nr, $usedChildComponentNrs)) {
                        $usedChildComponentNrs[] = 'd'.$nr;
                        $ret[] = array('type'=>'download', 'nr'=>$nr, 'html'=>$m[2]);
                    } else {
                        $ret[] = array('type'=>'invalidDownload',
                                    'href'=>$m[4],
                                    'componentId'=>$m2[1],
                                    'html'=>$m[2]);
                    }
                } else if (isset($classes['link']) && preg_match('#-l[0-9]+$#', $m2[1])) {
                    $ret[] = array('type'=>'invalidLink',
                                   'href'=>$m[4],
                                   'componentId'=>$m2[1],
                                   'html'=>$m[2]);
                } else if (isset($classes['download']) && preg_match('#-d[0-9]+$#', $m2[1])) {
                    $ret[] = array('type'=>'invalidDownload',
                                   'href'=>$m[4],
                                   'componentId'=>$m2[1],
                                   'html'=>$m[2]);
                } else {
                    $ret[] = array('type'=>'invalidLink', 'href'=>$m[4], 'html'=>$m[2]);
                }
            } else if (isset($classes['link']) && $m[4] != '') {
                $ret[] = array('type'=>'invalidLink', 'href'=>$m[4], 'html'=>$m[2]);
            } else if ($m[4] != '') {
                $ret[] = $m[2];
            }

            $content = $m[5];
        }
        if(!$m) $ret[] = $content;

        return $ret;
    }

    public function getMaxChildComponentNr($type)
    {
        $select = $this->getModel()->select();
        $select->order('nr', 'DESC');
        $select->limit(1);
        $select->whereEquals('component', $type);
        $row = $this->getChildRows('ChildComponents', $select)->current();
        if (!$row) return 0;
        return $row->nr;
    }

    protected function _beforeDelete()
    {
        $table = new Vpc_Basic_Text_ChildComponentsModel();
        $rows = $table->fetchAll(array('component_id = ?' => $this->component_id));
        foreach ($rows as $row) {
            $t = substr($row->component, 0, 1);
            $admin = Vpc_Admin::getInstance($this->_classes[$row->component]);
            $admin->delete($this->component_id . '-' . $t.$row->nr);
            $row->delete();
        }
    }

    //childComponents löschen die aus dem html-code entfernt wurden
    protected function _beforeSave()
    {
        $classes = $this->_classes;

        $this->content = $this->tidy($this->content);
        $newParts = $this->getContentParts($this->content);
        $newPartStrings = array();
        foreach ($newParts as $part) {
            if (!is_string($part) ) {
                $newPartStrings[] = $part['type'].$part['nr'];
            }
        }

        $rows = $this->getChildRows('ChildComponents');
        $existingParts = array();
        foreach ($rows as $row) {
            $t = substr($row->component, 0, 1);
            if (!in_array($row->component.$row->nr, $newPartStrings)) {
                $admin = Vpc_Admin::getInstance($classes[$row->component]);
                $admin->delete($this->component_id . '-' . $t.$row->nr);
                $row->delete();
            } else {
                if (!$row->saved) {
                    $row->saved = 1;
                    $row->save();
                }
                $existingParts[] = $row->component.$row->nr;
            }
        }

        foreach ($newParts as $part) {
            if (!is_string($part)
                && !in_array($part['type'].$part['nr'], $existingParts)) {
                $row = $this->createChildRow('ChildComponents');
                $row->component = $part['type'];
                $row->nr = $part['nr'];
                $row->saved = 1;
                $row->save();
            }
        }
    }

    public function tidy($html)
    {
        $config = array(
                    'indent'         => true,
                    'output-xhtml'   => true,
                    'clean'          => false,
                    'wrap'           => '86',
                    'doctype'        => 'omit',
                    'drop-proprietary-attributes' => true,
                    'drop-font-tags' => true,
                    'word-2000'      => true,
                    'show-body-only' => true,
                    'bare'           => true,
                    'enclose-block-text'=>true,
                    'enclose-text'   => true,
                    'join-styles'    => false,
                    'join-classes'   => false,
                    'logical-emphasis' => true,
                    'lower-literals' => true,
                    'literal-attributes' => false,
                    'indent-spaces' => 2,
                    'quote-nbsp'     => true,
                    'output-bom'     => false,
                    'char-encoding'  =>'utf8',
                    'newline'        =>'LF',
                    'uppercase-tags' =>'false'
                    );
        $enableTidy = Vpc_Abstract::getSetting($this->_componentClass, 'enableTidy');
        $enableFontSize = Vpc_Abstract::getSetting($this->_componentClass, 'enableFontSize');
        if ($enableFontSize){
            $config['drop-font-tags'] = false;
        }
        if (class_exists('tidy') && $enableTidy) {
            $tidy = new tidy;
            $html = str_replace('&nbsp;', '#nbsp#', $html); //einstellungen oben funktionieren nicht richtig
            $tidy->parseString($html, $config, 'utf8');
            $tidy->cleanRepair();
            $html = $tidy->value;
            $parser = new Vpc_Basic_Text_Parser($this);
            $parser->setEnableColor(Vpc_Abstract::getSetting($this->_componentClass, 'enableColors'));
            $parser->setEnableTagsWhitelist(Vpc_Abstract::getSetting($this->_componentClass, 'enableTagsWhitelist'));
            $parser->setEnableStyles(Vpc_Abstract::getSetting($this->_componentClass, 'enableStyles'));
            $html = $parser->parse($html);
            $tidy->parseString($html, $config, 'utf8');
            $tidy->cleanRepair();
            $html = $tidy->value;
            $html = str_replace('#nbsp#', '&nbsp;', $html);
        }

        $classes = $this->_classes;

        $newContent = '';

        foreach ($this->getContentParts($html) as $part) {
            if (is_string($part)) {
                $newContent .= $part;
            } else if ($part['type'] == 'invalidImage') {
                if (isset($part['componentId'])
                    && class_exists($part['componentClass'])
                    && (strtolower($part['componentClass']) == 'vpc_basic_image_component'
                        || is_subclass_of($part['componentClass'], 'Vpc_Basic_Image_Component'))) {

                    $srcRow = Vpc_Abstract::createModel($part['componentClass'])
                                    ->getRow($part['componentId']);
                    if ($srcRow->imageExists()) {
                        $destRow = Vpc_Abstract::createModel($classes['image'])
                                                ->createRow($srcRow->toArray());
                        $childComponentRow = $this->addChildComponentRow('image', $destRow);
                        $destRow->save();
                        $imageComponent = Vps_Component_Data_Root::getInstance()
                            ->getComponentByDbId($this->component_id.'-i'.$childComponentRow->nr)
                            ->getComponent();
                        $dimension = $imageComponent->getImageDimensions();
                        $newContent .= "<img src=\"".$imageComponent->getImageUrl()."\" ".
                                    "width=\"$dimension[width]\" ".
                                    "height=\"$dimension[height]\" />";
                        continue;
                    }
                }
                $client = new Zend_Http_Client();
                try {
                    $client->setUri($part['src']);
                } catch (Zend_Uri_Exception $e) {
                    //wann relative url mit http_host davor probieren
                    if (isset($_SERVER['HTTP_HOST'])) {
                        $client->setUri('http://'.$_SERVER['HTTP_HOST'].'/'.$part['src']);
                    }
                }
                try {
                    $response = $client->request();
                } catch (Exception $e) {
                    continue;
                }
                if (!$response->isSuccessful()) continue;

                $contentType = $response->getHeader('Content-type');
                if ($contentType == 'image/jpg' || $contentType == 'image/jpeg') {
                    $extension = 'jpg';
                } else if ($contentType == 'image/gif') {
                    $extension = 'gif';
                } else if ($contentType == 'image/png') {
                    $extension = 'png';
                } else {
                    continue;
                }

                $destFileRow = Vpc_Abstract::createModel($classes['image'])
                            ->getReferencedModel('Image')
                            ->createRow();

                $path = explode('?', $part['src']);
                if (preg_match('#([^/]*)\\.[a-z]+$#U', $path[0], $m)) {
                    $srcFileName = Zend_Filter::get($m[1], 'Alnum', array(ENT_QUOTES));
                }
                if (!isset($srcFileName) || !$srcFileName) {
                    $srcFileName = 'download';
                }

                $destFileRow->writeFile($response->getBody(), $srcFileName, $extension, $contentType);

                $destRow = Vpc_Abstract::createModel($classes['image'])->createRow();
                $destRow->vps_upload_id = $destFileRow->id;
                $size = getimagesize($destFileRow->getFileSource());
                $destRow->width = $size[0];
                $destRow->height = $size[1];
                $destRow->filename = $srcFileName;
                $destRow->scale = '';
                $childComponentRow = $this->addChildComponentRow('image', $destRow);
                $destRow->save();
                $imageComponent = Vps_Component_Data_Root::getInstance()
                    ->getComponentByDbId($this->component_id.'-i'.$childComponentRow->nr)
                    ->getComponent();
                $dimension = $imageComponent->getImageDimensions();
                $newContent .= "<img src=\"".$imageComponent->getImageUrl()."\" ".
                            "width=\"$dimension[width]\" ".
                            "height=\"$dimension[height]\" />";

            } else if ($part['type'] == 'invalidLink') {

                $model = Vpc_Abstract::createModel($classes['link']);
                if (isset($part['componentId'])) {
                    try {
                        $srcRow = $model->getRow($part['componentId']);
                    } catch (Vpc_Exception $e) {
                        $srcRow = false;
                    }
                    $linkClasses = Vpc_Abstract::getChildComponentClasses($classes['link'], 'link');
                    if ($srcRow && class_exists($linkClasses[$srcRow->component])) {
                        $linkModel = Vpc_Abstract::createModel($linkClasses[$srcRow->component]);
                        $srcLinkRow = $linkModel->getRow($part['componentId'].'-link');
                        if ($srcLinkRow) {
                            $destRow = $model->createRow();
                            $destRow->component = $srcRow->component;
                            $this->addChildComponentRow('link', $destRow);
                            $destRow->save();
                            $destLinkRow = $linkModel->createRow($srcLinkRow->toArray());
                            $destLinkRow->component_id = $destRow->component_id.'-link';
                            $destLinkRow->save();
                            $newContent .= "<a href=\"{$destRow->component_id}\">";
                            continue;
                        }
                    }
                }
                $destRow = $model->createRow();
                $linkClasses = Vpc_Abstract::getChildComponentClasses($classes['link'], 'link');

                $destRow->component = null;
                if (preg_match('#^mailto:#', $part['href'], $m)) {
                    if (isset($linkClasses['mail']) && $linkClasses['mail']) {
                        $destRow->component = 'mail';
                    }
                } else {
                    if (isset($linkClasses['intern']) && $linkClasses['intern']) {
                        $internLinkPage = Vps_Component_Data_Root::getInstance()
                            ->getPageByPath($part['href']);
                        if ($internLinkPage) {
                            $destRow->component = 'intern';
                        }
                    }
                    if (!$destRow->component && isset($linkClasses['extern']) && $linkClasses['extern']) {
                        $destRow->component = 'extern';
                    }
                }

                if (!$destRow->component) continue; //kein solcher-link möglich
                $this->addChildComponentRow('link', $destRow);
                $destRow->save();
                $destClasses =  Vpc_Abstract::getChildComponentClasses($classes['link'], 'link');

                $row = Vpc_Abstract::createModel($destClasses[$destRow->component])
                            ->getRow($destRow->component_id.'-link');
                if (!$row) $row = Vpc_Abstract::createModel($destClasses[$destRow->component])
                                            ->createRow();
                $row->component_id = $destRow->component_id.'-link';
                if ($destRow->component == 'extern') {
                    $row->target = $part['href'];
                } else if ($destRow->component == 'intern') {
                    $row->target = $internLinkPage->dbId;
                } else {
                    preg_match('#^mailto:(.*)\\??(.*)#', $part['href'], $m);
                    $row->mail = $m[1];
                    $m = parse_str($m[2]);
                    $row->subject = isset($m['subject']) ? $m['subject'] : '';
                    $row->text = isset($m['body']) ? $m['body'] : '';
                }
                $row->save();
                $newContent .= "<a href=\"{$destRow->component_id}\">";

            } else if ($part['type'] == 'invalidDownload') {

                $srcRow = Vpc_Abstract::createModel($classes['download'])
                                ->getRow($part['componentId']);
                if ($srcRow->fileExists()) {
                    $destRow = Vpc_Abstract::createModel($classes['download'])
                                                ->createRow($srcRow->toArray());
                    $this->addChildComponentRow('download', $destRow);
                    $destRow->save();
                    $newContent .= "<a href=\"{$destRow->component_id}\">";
{'fraction_non_alphanumeric': 0.1698778760588974, 'fraction_numerical': 0.005023917051922717, 'mean_word_length': 1.3663842165170101, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 454, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 78, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3789631', 'n_tokens_mistral': 10087, 'n_tokens_neox': 9513, 'n_words': 1448}