Plugin Directory

Changeset 1361997


Ignore:
Timestamp:
03/01/2016 06:31:15 PM (11 years ago)
Author:
msimpson
Message:

Better parsing of comma-delimited option values when they have regex with commas and escape characters in them

Location:
contact-form-7-to-database-extension/trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • contact-form-7-to-database-extension/trunk/CF7DBPlugin.php

    r1334626 r1361997  
    11491149     */
    11501150    public function getNoSaveFields() {
    1151         return preg_split('/,|;/', $this->getOption('NoSaveFields'), -1, PREG_SPLIT_NO_EMPTY);
     1151        return $this->parseOption($this->getOption('NoSaveFields'));
    11521152    }
    11531153
    … …  
    11561156     */
    11571157    public function getNoSaveForms() {
    1158         return preg_split('/,|;/', $this->getOption('NoSaveForms'), -1, PREG_SPLIT_NO_EMPTY);
     1158        return $this->parseOption($this->getOption('NoSaveForms'));
     1159    }
     1160
     1161    /**
     1162     * Parse option string that is a comma-delimited set of stings (some of which may be regex's with commas in them)
     1163     * @param $option string
     1164     * @return array
     1165     */
     1166    public function parseOption($option) {
     1167//        return preg_split('/,|;/', $option, -1, PREG_SPLIT_NO_EMPTY);
     1168        $values = array();
     1169        if ($option) {
     1170            $regex = false;
     1171            $esc = false;
     1172            $value = '';
     1173            $len = strlen($option);
     1174            for ($i = 0; $i < $len; $i++) {
     1175
     1176                if ($regex && !$esc && $option[$i] == '\\') {
     1177                    $esc = true;
     1178                    $value .= $option[$i];
     1179                    continue;
     1180                }
     1181
     1182                if (!$value && $option[$i] == '/') {
     1183                    $regex = true;
     1184                    $value .= $option[$i];
     1185                    continue;
     1186                }
     1187
     1188                if (!$regex) {
     1189                    if ($option[$i] == ',') {
     1190                        if ($value) {
     1191                            $values[] = $value;
     1192                        }
     1193                        $value = '';
     1194                    } else {
     1195                        $value .= $option[$i];
     1196                    }
     1197                } else {
     1198                    if ($option[$i] == '/' && !$esc) {
     1199                        $regex = false;
     1200                    }
     1201                    $value .= $option[$i];
     1202                }
     1203                $esc = false;
     1204            }
     1205            if ($value) {
     1206                $values[] = $value;
     1207            }
     1208        }
     1209        return $values;
    11591210    }
    11601211
    … …  
    11631214     */
    11641215    public function getSaveCookies() {
    1165         return preg_split('/,|;/', $this->getOption('SaveCookieNames'), -1, PREG_SPLIT_NO_EMPTY);
     1216        return $this->parseOption($this->getOption('SaveCookieNames'));
    11661217    }
    11671218
Note: See TracChangeset for help on using the changeset viewer.