From ed07341890f277c4c526fa3fff8dc29df2258849 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 5 Feb 2020 12:11:21 +0100 Subject: Allow typos in mail headers (up to edit distance 1) --- mail.php | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) (limited to 'mail.php') diff --git a/mail.php b/mail.php index 6d2d425..ba11244 100644 --- a/mail.php +++ b/mail.php @@ -156,31 +156,24 @@ function mail_parse_message ($body,$prefill_info=[]) $key=trim ($parts[0]); $val=trim ($parts[1]); - switch ($key){ - case 'Title': - $info['title']=$val; - break; - case 'Location': - $info['location']=$val; - break; - case 'Source': - $info['source']=$val; - break; - case 'Dates': - $parts=explode ('to',$val); - if (count ($parts)==2){ - $info['start_date']=trim ($parts[0]); - $info['end_date']=trim ($parts[1]); - } else { - $info['start_date']=$info['end_date']=$val; - } - break; - case 'Keywords': - $info['keywords']=array_map ('trim',explode (';',$val)); - break; - default: - $warnings[]='Unknown key '.$key; - } + if (levenshtein ($key,'Title') < 2) + $info['title']=$val; + else if (levenshtein ($key,'Location') < 2) + $info['location']=$val; + else if (levenshtein ($key,'Source') < 2) + $info['source']=$val; + else if (levenshtein ($key,'Dates') < 2){ + $parts=explode ('to',$val); + if (count ($parts)==2){ + $info['start_date']=trim ($parts[0]); + $info['end_date']=trim ($parts[1]); + } else { + $info['start_date']=$info['end_date']=$val; + } + } else if (levenshtein ($key,'Keywords') < 2) + $info['keywords']=array_map ('trim',explode (';',$val)); + else + $warnings[]='Unknown key '.$key; } $info['description']=trim (implode ("\n",array_slice ($lines,$i))); -- cgit v1.2.3