Forum


Replies: 1   Views: 294
Malformed html with a not valid color returns a php warning
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by LucasSpl  · 13-12-2021 - 11:19

Hello, I found a border color bug, here is my analysis :

My php tail is regularly filled with php warning.

PHP Warning:  Trying to access array offset on value of type null in /var/www/[...]/PhpDocx/classes/DOMPDF_lib.php on line 4400


After some research, i found that the origin of the bug is due to a malformed html

<table class="MsoNormalTable" style="width: 16cm; margin-left: 5.4pt;" width="100%" cellspacing="0" cellpadding="0">
   <tbody>
      <tr style="height: 17pt;">
         <td style="width: 16cm; border: 1pt solid windowtext; padding: 0cm 5.4pt;">
            <p style="margin: 0cm 0cm 0.0001pt 1.7pt; font-size: 12pt; font-family: Arial, sans-serif;"><strong><span style="font-size: 11pt;">N° 2021-07-41</span></strong></p>
         </td>
      </tr>
   </tbody>
</table>

and more precisely le td style border: 1pt solid windowtext;, you noticed windowtext is not a valid css color.

In search of a correction, I found that the warning is launched when calling the protected function _get_border in classes\DOMPDF_lib.php

protected function _get_border($side) {
    $color = $this->__get("border_" . $side . "_color"); // Returns null

    return $this->__get("border_" . $side . "_width") . " " .
      $this->__get("border_" . $side . "_style") . " " . $color["hex"]; // Php warning because $color is null
  }

I thought of

if (!(isset($color) && is_array($color) && isset($color['hex']))) {
    $color = ['hex' => "#000000"];
}

This correction was not very clean, so I looked for another solution.
 

Do you remember windowtext is not a valid color ? So i looked for a parent function that will handle colors, and i found munge_colour in classes\DOMPDF_lib.php
In this function, there is a cascade of if and else if, but no if, si when the color is invalid or unrecognized, the function returns nothing, _get_border therefore has a variable of the wrong type

I propose as a correction to add another else if in the function munge_colour

[...] else if($colour !== "inherit") {
    return $this->getArray("000000");
}


Regards.