Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typo3 exporter errors caused by array_merge and htmlspecialchars #165

Open
fishbone1 opened this issue May 5, 2022 · 1 comment
Open
Labels
php PHP related task TYPO3 v11

Comments

@fishbone1
Copy link

fishbone1 commented May 5, 2022

Typo3 version: 11.5.9
Extension version: master 20297bb

I sometimes get a warning and and error when opening the page exporter. It happens after I added a rich text content with text and image. But it doesn't happen always. The warning part:

PHP Warning: array_merge(): Expected parameter 2 to be an array, string given in (...)/myproject/public/typo3/sysext/impexp/Classes/Export.php line 700

This is the typo3 code. I set a debugger breakpoint at line 700:

    protected function flatSoftRefs(array $relations): array
    {
        $list = [];
        foreach ($relations as $field => $relation) {
            if (is_array($relation['softrefs']['keys'] ?? null)) {
                foreach ($relation['softrefs']['keys'] as $spKey => $elements) {
                    foreach ($elements as $subKey => $el) {
                        $lKey = $field . ':' . $spKey . ':' . $subKey;
// Line 700:
                        $list[$lKey] = array_merge(['field' => $field, 'spKey' => $spKey], $el);
                        // Add file_ID key to header - slightly "risky" way of doing this because if the calculation
                        // changes for the same value in $this->records[...] this will not work anymore!
                        if ($el['subst']['relFileName'] ?? false) {
                            $list[$lKey]['file_ID'] = md5(Environment::getPublicPath() . '/' . $el['subst']['relFileName']);
                        }
                    }
                }
            }

array_merge tries to merge with $el which contains:

<p>Before</p>
<p><img alt="" data-alt-override="true" data-htmlarea-file-table="sys_file" data-htmlarea-file-uid="{softref:c64604e5faca1242d8358828cfb4170b}" data-title-override="true" src="/fileadmin/user_upload/myimage.png" style="" title="" width="300" height="73" /></p>

grafik

I can also see this structure in a successful export:

<softrefs type="array">
    <keys type="array">
        <softref_key index="rtehtmlarea_images" type="array">
            <softref_element index="content">&lt;p&gt;Content mit Bild:&lt;/p&gt;
                &lt;p&gt;&lt;img alt=&quot;&quot; data-alt-override=&quot;true&quot; data-htmlarea-file-table=&quot;sys_file&quot; data-htmlarea-file-uid=&quot;{softref:80768071c2028e4faefc5a619f078928}&quot; data-title-override=&quot;true&quot; src=&quot;/fileadmin/user_upload/myimage.png&quot; style=&quot;&quot; title=&quot;&quot; width=&quot;300&quot; height=&quot;73&quot; /&gt;&lt;/p&gt;</softref_element>
            <softref_element index="elements" type="array">
                <numIndex index="1" type="array">
                    <matchString>&lt;img alt=&quot;&quot; data-alt-override=&quot;true&quot; data-htmlarea-file-table=&quot;sys_file&quot; data-htmlarea-file-uid=&quot;91&quot; data-title-override=&quot;true&quot; src=&quot;/fileadmin/user_upload/myimage.png&quot; style=&quot;&quot; title=&quot;&quot; width=&quot;300&quot; height=&quot;73&quot; /&gt;</matchString>
                    <subst type="array">
                        <type>db</type>
                        <recordRef>sys_file:91</recordRef>
                        <tokenID>80768071c2028e4faefc5a619f078928</tokenID>
                        <tokenValue>91</tokenValue>
                    </subst>
                </numIndex>
            </softref_element>
        </softref_key>
    </keys>
</softrefs>

It seems the exporter expects an array for softref_element "content".

After this warning I get an error 500:

Core: Exception handler (WEB: BE): TypeError, code #0, file (...)/myproject/public/typo3/sysext/impexp/Classes/ImportExport.php, line 854: htmlspecialchars() expects parameter 1 to be string, null given

Related code from exporter:

    protected function addSoftRefs(array $softrefs, array &$lines, int $indent): void
    {
        foreach ($softrefs as $softref) {
            $line = [];
            $line['ref'] = 'SOFTREF';
            $line['type'] = 'softref';
            $line['msg'] = '';
            $line['preCode'] = sprintf(
                '%s<span title="%s">%s</span>',
                $this->renderIndent($indent + 2),
                htmlspecialchars($line['ref']),
                $this->iconFactory->getIcon('status-reference-soft', Icon::SIZE_SMALL)->render()
            );
            $line['title'] = sprintf(
                '<em>%s, "%s"</em> : <span title="%s">%s</span>',
                $softref['field'],
                $softref['spKey'],
// Line 854:
                htmlspecialchars($softref['matchString']),
                htmlspecialchars(GeneralUtility::fixed_lgd_cs($softref['matchString'], 60))
            );
            if ($softref['subst']['type'] ?? false) {
                if ($softref['subst']['title'] ?? false) {
                    $line['title'] .= sprintf(
                        '<br/>%s<strong>%s</strong> %s',
                        $this->renderIndent($indent + 4),
                        htmlspecialchars($this->lang->getLL('impexpcore_singlereco_title')),
                        htmlspecialchars(GeneralUtility::fixed_lgd_cs($softref['subst']['title'], 60))
                    );
                }

In this case the content entry is null where an array is expected by the exporter:

grafik

I think this code from t3x-rte_ckeditor_image is related:

    // Classes/Database/RteImagesSoftReferenceIndex.php

    public function findRef_rtehtmlarea_images($content)
    {
        $retVal = false;
        // Start HTML parser and split content by image tag
        $this->htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
        $this->splittedContentTags = $this->htmlParser->splitTags('img', $content);

        $images = $this->findImagesWithDataUid();

        // Assemble result array
        if (!empty($images)) {
            $retVal = [
                'content' => implode('', $this->splittedContentTags),
                'elements' => $images
            ];
        }

        return $retVal;
    }

Maybe this is an exporter issue if string/null is allowed where it expects arrays. Unfortunately I don't know much about how this works...

@magicsunday magicsunday added php PHP related task TYPO3 v11 labels Jan 21, 2023
@jpmschuler
Copy link
Contributor

Confirm: with latest TYPO3 11LTS and PHP 8.1
Steps to reproduce: have a page with a tt_content element with an RTE image. Open export via right click context menu on page, get Exception

rtehtmlarea_images is the only non-core soft reference parser in the system.

Can't see something obvious in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
php PHP related task TYPO3 v11
Projects
None yet
Development

No branches or pull requests

3 participants