• So, if you’re like me and supporting an older site that is still running this plugin (It shouldn’t be, upgrade to something else) but you have to keep running it for the time being while you find the perfect replacement.

    I can help.
    plugin folder/inc/Encoding.php starting at line 155, replace the for loop with the following code

    Explanation: In php 8 we need to replace the curly braces {} used for string offset access with square brackets [] and update the bitwise operators & to use the & operator for binary AND operations

    for ($i = 0; $i < $max; $i++) {
        $c1 = $text[$i];
        if ($c1 >= "\xc0") { // Should be converted to UTF8, if it's not UTF8 already
            $c2 = $i + 1 >= $max ? "\x00" : $text[$i + 1];
            $c3 = $i + 2 >= $max ? "\x00" : $text[$i + 2];
            $c4 = $i + 3 >= $max ? "\x00" : $text[$i + 3];
            if ($c1 >= "\xc0" && $c1 <= "\xdf") { // Looks like 2 bytes UTF8
                if ($c2 >= "\x80" && $c2 <= "\xbf") { // Yeah, almost sure it's UTF8 already
                    $buf .= $c1 . $c2;
                    $i++;
                } else { // Not valid UTF8. Convert it.
                    $cc1 = (chr(ord($c1) >> 6) | "\xc0");
                    $cc2 = ($c1 & "\x3f") | "\x80";
                    $buf .= $cc1 . $cc2;
                }
            } elseif ($c1 >= "\xe0" && $c1 <= "\xef") { // Looks like 3 bytes UTF8
                if ($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf") { // Yeah, almost sure it's UTF8 already
                    $buf .= $c1 . $c2 . $c3;
                    $i = $i + 2;
                } else { // Not valid UTF8. Convert it.
                    $cc1 = (chr(ord($c1) >> 6) | "\xc0");
                    $cc2 = ($c1 & "\x3f") | "\x80";
                    $buf .= $cc1 . $cc2;
                }
            } elseif ($c1 >= "\xf0" && $c1 <= "\xf7") { // Looks like 4 bytes UTF8
                if ($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf" && $c4 >= "\x80" && $c4 <= "\xbf") { // Yeah, almost sure it's UTF8 already
                    $buf .= $c1 . $c2 . $c3;
                    $i = $i + 2;
                } else { // Not valid UTF8. Convert it.
                    $cc1 = (chr(ord($c1) >> 6) | "\xc0");
                    $cc2 = ($c1 & "\x3f") | "\x80";
                    $buf .= $cc1 . $cc2;
                }
            } else { // Doesn't look like UTF8, but should be converted
                $cc1 = (chr(ord($c1) >> 6) | "\xc0");
                $cc2 = ($c1 & "\x3f") | "\x80";
                $buf .= $cc1 . $cc2;
            }
        } elseif (($c1 & "\xc0") == "\x80") { // Needs conversion
            if (isset(self::$win1252ToUtf8[ord($c1)])) { // Found in Windows-1252 special cases
                $buf .= self::$win1252ToUtf8[ord($c1)];
            } else {
                $cc1 = (chr(ord($c1) >> 6) | "\xc0");
                $cc2 = ($c1 & "\x3f") | "\x80";
                $buf .= $cc1 . $cc2;
            }
        } else { // It doesn't need conversion
            $buf .= $c1;
        }
    }

    Next we need to move to the main file itself in the base folder of the plugin
    easy-table.php starting at line 1209 towards the end of the file.

    Explanation: We’ll keep the default value of $enclosure as " and have made $limit a dynamic variable that can be adjusted based on your plugin’s options. If the $option['limit'] value is available and not empty, it will be used as the limit; otherwise, it falls back to the default value of 2000000.

    Replace the entire if statement starting at 1209 with the following

    if (!function_exists('easy_table_str_getcsv')) {
        function easy_table_str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = '\\'){
            /** 
            * Bug fix, custom terminator wont work
            * @since version 1.1.1
            */
            if( ("\r" === $delimiter) OR ("\n" === $delimiter) ) {
            }
            else {
                $input = str_replace("\n",'NLINEBREAK',$input);
                $input = str_replace("\r",'RLINEBREAK',$input);
            }
            $input = str_ireplace( $escape.$delimiter,'_ESCAPED_SEPATATOR_',$input );
            
            $fiveMBs = 5 * 1024 * 1024;
            $limit = 2000000; // Default value for the limit
            
            $option = get_option('easy_table_plugin_option');
            if (!empty($option['limit'])) {
                $limit = (int)$option['limit'];
            }
            
            if (($handle = fopen("php://temp/maxmemory:$fiveMBs", 'r+')) !== FALSE) {
                fputs($handle, $input);
                rewind($handle);
                $line = -1;
                $return = Array();
                
                while (($data = @fgetcsv($handle, $limit, $delimiter)) !== FALSE) {
                    $num = count($data);
                    for ($c = 0; $c < $num; $c++) {
                        $line++;
                        $data[$c] = str_replace('NLINEBREAK',"\n",$data[$c]);
                        $data[$c] = str_replace('RLINEBREAK',"\r",$data[$c]);
                        $data[$c] = str_replace('_ESCAPED_SEPATATOR_',$delimiter,$data[$c]);
                        $return[$line] = $data[$c];
                    }
                }
                
                fclose($handle);
                return $return;
            }
        }
    }

    And That should do it, I hope this helps someone.

    • This topic was modified 1 year, 2 months ago by Jason.
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.