Completed
Push — master ( 991681...655e8b )
by Lars
02:36
created

EmailCheck   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 920
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 42
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 920
ccs 96
cts 104
cp 0.9231
rs 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
F isValid() 0 150 33
A isExampleDomain() 0 8 2
A isTypoInDomain() 0 8 2
A isTemporaryDomain() 0 8 2
A isDnsError() 0 8 3

How to fix   Complexity   

Complex Class

Complex classes like EmailCheck often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EmailCheck, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace voku\helper;
4
5
use TrueBV\Exception\LabelOutOfBoundsException;
6
use TrueBV\Punycode;
7
8
/**
9
 * E-Mail Check Class
10
 *
11
 * -> use "EmailCheck::isValid()" to validate a email-address
12
 *
13
 * @author      Lars Moelleken
14
 * @copyright   Copyright (c) 2015, Lars Moelleken (http://moelleken.org/)
15
 * @license     http://opensource.org/licenses/MIT	MIT License
16
 */
17
class EmailCheck
18
{
19
  /**
20
   * @var array
21
   */
22
  public static $domainsExample = array(
23
      'test.de',
24
      'test.com',
25
      'test.net',
26
      'test.org',
27
      'example.de',
28
      'example.com',
29
      'example.net',
30
      'example.org',
31
  );
32
33
  /**
34
   * @var array
35
   *
36
   * here you can find more domains:
37
   * - https://github.com/vboctor/disposable_email_checker
38
   * - https://gist.github.com/michenriksen/8710649
39
   */
40
  public static $domainsTemporary = array(
41
      '0-mail.com',
42
      '0815.ru',
43
      '0815.ru0clickemail.com',
44
      '0clickemail.com',
45
      '0wnd.net',
46
      '0wnd.org',
47
      '10minutemail.com',
48
      '10minutemail.co.za',
49
      '20minutemail.com',
50
      '2prong.com',
51
      '30minutemail.com',
52
      '33mail.com',
53
      '3d-painting.com',
54
      '4warding.com',
55
      '4warding.net',
56
      '4warding.org',
57
      '60minutemail.com',
58
      '675hosting.com',
59
      '675hosting.net',
60
      '675hosting.org',
61
      '6paq.com',
62
      '6url.com',
63
      '75hosting.com',
64
      '75hosting.net',
65
      '75hosting.org',
66
      '7tags.com',
67
      '9ox.net',
68
      'a-bc.net',
69
      'afrobacon.com',
70
      'ag.us.to',
71
      'ajaxapp.net',
72
      'amilegit.com',
73
      'amiri.net',
74
      'amiriindustries.com',
75
      'anonbox.net',
76
      'anonymbox.com',
77
      'antichef.com',
78
      'antichef.net',
79
      'antispam.de',
80
      'armyspy.com',
81
      'atvclub.msk.ru',
82
      'azmeil.tk',
83
      'baxomale.ht.cx',
84
      'beefmilk.com',
85
      'binkmail.com',
86
      'bio-muesli.net',
87
      'bobmail.info',
88
      'bodhi.lawlita.com',
89
      'bofthew.com',
90
      'bootybay.de',
91
      'boun.cr',
92
      'bouncr.com',
93
      'brefmail.com',
94
      'broadbandninja.com',
95
      'bsnow.net',
96
      'bu.mintemail.com',
97
      'bugmenot.com',
98
      'bumpymail.com',
99
      'casualdx.com',
100
      'centermail.com',
101
      'centermail.net',
102
      'chogmail.com',
103
      'choicemail1.com',
104
      'consumerriot.com',
105
      'cool.fr.nf',
106
      'correo.blogos.net',
107
      'cosmorph.com',
108
      'courriel.fr.nf',
109
      'courrieltemporaire.com',
110
      'crazymailing.com',
111
      'cubiclink.com',
112
      'curryworld.de',
113
      'cust.in',
114
      'cuvox.de',
115
      'dacoolest.com',
116
      'dandikmail.com',
117
      'dayrep.com',
118
      'deadaddress.com',
119
      'deadspam.com',
120
      'despam.it',
121
      'despammed.com',
122
      'devnullmail.com',
123
      'dfgh.net',
124
      'digitalsanctuary.com',
125
      'dingbone.com',
126
      'discardmail.com',
127
      'discardmail.de',
128
      'disposableaddress.com',
129
      'disposableemailaddresses.com',
130
      'disposeamail.com',
131
      'disposemail.com',
132
      'dispostable.com',
133
      'dm.w3internet.co.uk',
134
      'dm.w3internet.co.ukexample.com',
135
      'dodgeit.com',
136
      'dodgit.com',
137
      'dodgit.org',
138
      'doiea.com',
139
      'donemail.ru',
140
      'dontreg.com',
141
      'dontsendmespam.de',
142
      'drdrb.com',
143
      'drdrb.net',
144
      'dump-email.info',
145
      'dumpandjunk.com',
146
      'dumpmail.de',
147
      'dumpyemail.com',
148
      'e-mail.com',
149
      'e4ward.com',
150
      'easytrashmail.com',
151
      'einrot.com',
152
      'email.net',
153
      'email60.com',
154
      'emaildienst.de',
155
      'emailias.com',
156
      'emailigo.de',
157
      'emailinfive.com',
158
      'emailmiser.com',
159
      'emailsensei.com',
160
      'emailtemporario.com.br',
161
      'emailto.de',
162
      'emailwarden.com',
163
      'emailx.at.hm',
164
      'emailxfer.com',
165
      'emeil.in',
166
      'emeil.ir',
167
      'emz.net',
168
      'enterto.com',
169
      'ephemail.net',
170
      'etranquil.com',
171
      'etranquil.net',
172
      'etranquil.org',
173
      'evopo.com',
174
      'explodemail.com',
175
      'fakeinbox.com',
176
      'fakeinformation.com',
177
      'fansworldwide.de',
178
      'fastacura.com',
179
      'fastchevy.com',
180
      'fastchrysler.com',
181
      'fastkawasaki.com',
182
      'fastmazda.com',
183
      'fastmitsubishi.com',
184
      'fastnissan.com',
185
      'fastsubaru.com',
186
      'fastsuzuki.com',
187
      'fasttoyota.com',
188
      'fastyamaha.com',
189
      'filzmail.com',
190
      'fixmail.tk',
191
      'fizmail.com',
192
      'fleckens.hu',
193
      'fr33mail.info',
194
      'frapmail.com',
195
      'front14.org',
196
      'fudgerub.com',
197
      'fux0ringduh.com',
198
      'garliclife.com',
199
      'gelitik.in',
200
      'get1mail.com',
201
      'get2mail.fr',
202
      'getairmail.com',
203
      'getonemail.com',
204
      'getonemail.net',
205
      'ghosttexter.de',
206
      'girlsundertheinfluence.com',
207
      'gishpuppy.com',
208
      'goemailgo.com',
209
      'gowikibooks.com',
210
      'gowikicampus.com',
211
      'gowikicars.com',
212
      'gowikifilms.com',
213
      'gowikigames.com',
214
      'gowikimusic.com',
215
      'gowikinetwork.com',
216
      'gowikitravel.com',
217
      'gowikitv.com',
218
      'great-host.in',
219
      'greensloth.com',
220
      'grr.la',
221
      'gsrv.co.uk',
222
      'guerillamail.biz',
223
      'guerillamail.com',
224
      'guerillamail.net',
225
      'guerillamail.org',
226
      'guerrillamail.biz',
227
      'guerrillamail.com',
228
      'guerrillamail.de',
229
      'guerrillamail.net',
230
      'guerrillamail.org',
231
      'guerrillamailblock.com',
232
      'gustr.com',
233
      'h.mintemail.com',
234
      'h8s.org',
235
      'haltospam.com',
236
      'hatespam.org',
237
      'hidemail.de',
238
      'hidzz.com',
239
      'hochsitze.com',
240
      'hotpop.com',
241
      'hulapla.de',
242
      'ieatspam.eu',
243
      'ieatspam.info',
244
      'ieh-mail.de',
245
      'ihateyoualot.info',
246
      'iheartspam.org',
247
      'imails.info',
248
      'inbax.tk',
249
      'inbox.si',
250
      'inbox2.info',
251
      'inboxalias.com',
252
      'inboxclean.com',
253
      'inboxclean.org',
254
      'incognitomail.com',
255
      'incognitomail.net',
256
      'incognitomail.org',
257
      'insorg-mail.info',
258
      'ipoo.org',
259
      'irish2me.com',
260
      'iwi.net',
261
      'jetable.com',
262
      'jetable.fr.nf',
263
      'jetable.net',
264
      'jetable.org',
265
      'jnxjn.com',
266
      'jourrapide.com',
267
      'junk1e.com',
268
      'kasmail.com',
269
      'kaspop.com',
270
      'keepmymail.com',
271
      'killmail.com',
272
      'killmail.net',
273
      'kir.ch.tc',
274
      'klassmaster.com',
275
      'klassmaster.net',
276
      'klzlk.com',
277
      'koszmail.pl',
278
      'kulturbetrieb.info',
279
      'kurzepost.de',
280
      'letthemeatspam.com',
281
      'lhsdv.com',
282
      'lifebyfood.com',
283
      'link2mail.net',
284
      'litedrop.com',
285
      'lol.ovpn.to',
286
      'lookugly.com',
287
      'lopl.co.cc',
288
      'lortemail.dk',
289
      'lr78.com',
290
      'm4ilweb.info',
291
      'ma1l.bij.pl',
292
      'maboard.com',
293
      'mail-temporaire.fr',
294
      'mail.by',
295
      'mail.mezimages.net',
296
      'mail2rss.org',
297
      'mail333.com',
298
      'mail4trash.com',
299
      'mailbidon.com',
300
      'mailblocks.com',
301
      'mailcatch.com',
302
      'maildrop.cc',
303
      'maileater.com',
304
      'mailed.in',
305
      'mailexpire.com',
306
      'mailfa.tk',
307
      'mailfreeonline.com',
308
      'mailimate.com',
309
      'mailin8r.com',
310
      'mailinater.com',
311
      'mailinator.com',
312
      'mailinator.net',
313
      'mailinator2.com',
314
      'mailincubator.com',
315
      'mailismagic.com',
316
      'mailme.ir',
317
      'mailme.lv',
318
      'mailmetrash.com',
319
      'mailmoat.com',
320
      'mailnator.com',
321
      'mailnesia.com',
322
      'mailnull.com',
323
      'mailshell.com',
324
      'mailsiphon.com',
325
      'mailslite.com',
326
      'mailtemp.info',
327
      'mailtothis.com',
328
      'mailzilla.com',
329
      'mailzilla.org',
330
      'mbx.cc',
331
      'mega.zik.dj',
332
      'meinspamschutz.de',
333
      'meltmail.com',
334
      'messagebeamer.de',
335
      'mierdamail.com',
336
      'mintemail.com',
337
      'mjukglass.nu',
338
      'mobi.web.id',
339
      'moburl.com',
340
      'moncourrier.fr.nf',
341
      'monemail.fr.nf',
342
      'monmail.fr.nf',
343
      'monumentmail.com',
344
      'msa.minsmail.com',
345
      'mt2009.com',
346
      'mt2014.com',
347
      'mx0.wwwnew.eu',
348
      'mycleaninbox.net',
349
      'mypartyclip.de',
350
      'myphantomemail.com',
351
      'myspaceinc.com',
352
      'myspaceinc.net',
353
      'myspaceinc.org',
354
      'myspacepimpedup.com',
355
      'myspamless.com',
356
      'mytempemail.com',
357
      'mytrashmail.com',
358
      'naver.com',
359
      'neomailbox.com',
360
      'nepwk.com',
361
      'nervmich.net',
362
      'nervtmich.net',
363
      'netmails.com',
364
      'netmails.net',
365
      'netzidiot.de',
366
      'neverbox.com',
367
      'no-spam.ws',
368
      'nobulk.com',
369
      'noclickemail.com',
370
      'nogmailspam.info',
371
      'nomail.xl.cx',
372
      'nomail2me.com',
373
      'nomorespamemails.com',
374
      'nospam.ze.tc',
375
      'nospam4.us',
376
      'nospamfor.us',
377
      'nospamthanks.info',
378
      'notmailinator.com',
379
      'nowmymail.com',
380
      'nullbox.info',
381
      'nurfuerspam.de',
382
      'nus.edu.sg',
383
      'nwldx.com',
384
      'objectmail.com',
385
      'obobbo.com',
386
      'odaymail.com',
387
      'oneoffemail.com',
388
      'onewaymail.com',
389
      'onlatedotcom.info',
390
      'online.ms',
391
      'oopi.org',
392
      'opayq.com',
393
      'ordinaryamerican.net',
394
      'otherinbox.com',
395
      'ourklips.com',
396
      'outlawspam.com',
397
      'ovpn.to',
398
      'owlpic.com',
399
      'pancakemail.com',
400
      'pimpedupmyspace.com',
401
      'pjjkp.com',
402
      'plexolan.de',
403
      'politikerclub.de',
404
      'poofy.org',
405
      'pookmail.com',
406
      'privacy.net',
407
      'privymail.de',
408
      'proxymail.eu',
409
      'prtnx.com',
410
      'punkass.com',
411
      'putthisinyourspamdatabase.com',
412
      'qq.com',
413
      'quickinbox.com',
414
      'rcpt.at',
415
      'reallymymail.com',
416
      'recode.me',
417
      'recursor.net',
418
      'regbypass.com',
419
      'regbypass.comsafe-mail.net',
420
      'rejectmail.com',
421
      'rhyta.com',
422
      'rklips.com',
423
      'rmqkr.net',
424
      'rppkn.com',
425
      'rtrtr.com',
426
      's0ny.net',
427
      'safe-mail.net',
428
      'safersignup.de',
429
      'safetymail.info',
430
      'safetypost.de',
431
      'sandelf.de',
432
      'saynotospams.com',
433
      'selfdestructingmail.com',
434
      'selfdestructingmail.org',
435
      'sendspamhere.com',
436
      'sharklasers.com',
437
      'shieldedmail.com',
438
      'shiftmail.com',
439
      'shitmail.me',
440
      'shortmail.net',
441
      'sibmail.com',
442
      'sify.com',
443
      'skeefmail.com',
444
      'slaskpost.se',
445
      'slopsbox.com',
446
      'slushmail.com',
447
      'smaakt.naar.gravel',
448
      'smapfree24.com',
449
      'smapfree24.de',
450
      'smapfree24.eu',
451
      'smapfree24.info',
452
      'smapfree24.org',
453
      'smashmail.de',
454
      'smellfear.com',
455
      'snakemail.com',
456
      'sneakemail.com',
457
      'sofimail.com',
458
      'sofort-mail.de',
459
      'sogetthis.com',
460
      'soodonims.com',
461
      'spam.la',
462
      'spam.su',
463
      'spam4.me',
464
      'spamavert.com',
465
      'spambob.com',
466
      'spambob.net',
467
      'spambob.org',
468
      'spambog.com',
469
      'spambog.de',
470
      'spambog.net',
471
      'spambog.ru',
472
      'spambox.info',
473
      'spambox.irishspringrealty.com',
474
      'spambox.org',
475
      'spambox.us',
476
      'spamcannon.com',
477
      'spamcannon.net',
478
      'spamcero.com',
479
      'spamcon.org',
480
      'spamcorptastic.com',
481
      'spamcowboy.com',
482
      'spamcowboy.net',
483
      'spamcowboy.org',
484
      'spamday.com',
485
      'spamex.com',
486
      'spamfree.eu',
487
      'spamfree24.com',
488
      'spamfree24.de',
489
      'spamfree24.eu',
490
      'spamfree24.info',
491
      'spamfree24.net',
492
      'spamfree24.org',
493
      'spamgoes.in',
494
      'spamgourmet.com',
495
      'spamgourmet.net',
496
      'spamgourmet.org',
497
      'spamherelots.com',
498
      'spamhereplease.com',
499
      'spamhole.com',
500
      'spamify.com',
501
      'spaminator.de',
502
      'spamkill.info',
503
      'spaml.com',
504
      'spaml.de',
505
      'spammotel.com',
506
      'spamobox.com',
507
      'spamoff.de',
508
      'spamslicer.com',
509
      'spamspot.com',
510
      'spamthis.co.uk',
511
      'spamthisplease.com',
512
      'spamtrail.com',
513
      'speed.1s.fr',
514
      'squizzy.de',
515
      'supergreatmail.com',
516
      'supermailer.jp',
517
      'superrito.com',
518
      'suremail.info',
519
      'tagyourself.com',
520
      'teewars.org',
521
      'teleworm.com',
522
      'teleworm.us',
523
      'tempalias.com',
524
      'tempe-mail.com',
525
      'tempemail.biz',
526
      'tempemail.com',
527
      'tempemail.net',
528
      'tempinbox.co.uk',
529
      'tempinbox.com',
530
      'temp-mail.ru',
531
      'tempmail.it',
532
      'tempmail2.com',
533
      'tempomail.fr',
534
      'temporarily.de',
535
      'temporarioemail.com.br',
536
      'temporaryemail.net',
537
      'temporaryforwarding.com',
538
      'temporaryinbox.com',
539
      'tempymail.com',
540
      'thanksnospam.info',
541
      'thankyou2010.com',
542
      'thisisnotmyrealemail.com',
543
      'throwawayemailaddress.com',
544
      'throam.com',
545
      'tilien.com',
546
      'tmail.com',
547
      'tmailinator.com',
548
      'toiea.com',
549
      'tradermail.info',
550
      'trash-amil.com',
551
      'trash-mail.at',
552
      'trash-mail.com',
553
      'trash-mail.de',
554
      'trash2009.com',
555
      'trashemail.de',
556
      'trashmail.at',
557
      'trashmail.com',
558
      'trashmail.de',
559
      'trashmail.me',
560
      'trashmail.net',
561
      'trashmail.org',
562
      'trashmail.ws',
563
      'trashmailer.com',
564
      'trashymail.com',
565
      'trashymail.net',
566
      'trbvm.com',
567
      'trillianpro.com',
568
      'turual.com',
569
      'twinmail.de',
570
      'tyldd.com',
571
      'uggsrock.com',
572
      'upliftnow.com',
573
      'uplipht.com',
574
      'venompen.com',
575
      'veryrealemail.com',
576
      'vidchart.com',
577
      'viditag.com',
578
      'viewcastmedia.com',
579
      'viewcastmedia.net',
580
      'viewcastmedia.org',
581
      'webm4il.info',
582
      'wegwerfadresse.de',
583
      'wegwerfemail.de',
584
      'wegwerfmail.de',
585
      'wegwerfmail.net',
586
      'wegwerfmail.org',
587
      'wetrainbayarea.com',
588
      'wetrainbayarea.org',
589
      'wh4f.org',
590
      'whatiaas.com',
591
      'whatpaas.com',
592
      'whyspam.me',
593
      'willselfdestruct.com',
594
      'winemaven.info',
595
      'wronghead.com',
596
      'wuzup.net',
597
      'wuzupmail.net',
598
      'www.e4ward.com',
599
      'www.gishpuppy.com',
600
      'www.mailinator.com',
601
      'wwwnew.eu',
602
      'xagloo.co',
603
      'xagloo.com',
604
      'xemaps.com',
605
      'xents.com',
606
      'xmail.com',
607
      'xmaily.com',
608
      'xoxy.net',
609
      'yep.it',
610
      'yogamaven.com',
611
      'yopmail.com',
612
      'yopmail.fr',
613
      'yopmail.net',
614
      'ypmail.webarnak.fr.eu.org',
615
      'yuurok.com',
616
      'zehnminutenmail.de',
617
      'zippymail.info',
618
      'zoaxe.com',
619
      'zoemail.com',
620
      'zoemail.org',
621
  );
622
623
  /**
624
   * @var array
625
   */
626
  public static $domainsTypo = array(
627
      '',
628
      '-online.de',
629
      '-tonline.de',
630
      'acor.de',
631
      'aecor.de',
632
      'ahoo.de',
633
      'al.com',
634
      'ao.com',
635
      'aol.cm',
636
      'aol.con',
637
      'aol.ocm',
638
      'aol.om',
639
      'aoll.com',
640
      'apl.com',
641
      'arco.de',
642
      'arocr.de',
643
      'aror.de',
644
      'feenet.de',
645
      'freeenet.de',
646
      'freeent.de',
647
      'freeet.de',
648
      'freemet.de',
649
      'freeneet.de',
650
      'freent.de',
651
      'frenet.de',
652
      'gm.de',
653
      'gm.net',
654
      'gm.xde',
655
      'gmc.de',
656
      'gmx.ded',
657
      'gmx.dw',
658
      'gmxx.de',
659
      'gmy.de',
660
      'gx.de',
661
      'hmx.de',
662
      'homail.de',
663
      'hotmai.de',
664
      'hotmal.de',
665
      'mx.de',
666
      'mx.net',
667
      'otmail.com',
668
      'r-online.de',
669
      'rcor.de',
670
      'reenet.de',
671
      't--online.de',
672
      't-0nline.de',
673
      't-nline.de',
674
      't-oline.de',
675
      't-omline.de',
676
      't-onine.de',
677
      't-onlien.de',
678
      't-onliine.de',
679
      't-onlin.de',
680
      't-onlinde.de',
681
      't-onlinr.de',
682
      't-onlione.de',
683
      't-onlline.de',
684
      't-onlne.de',
685
      't-onlone.de',
686
      't.-online.de',
687
      't.online.de',
688
      'tonline.de',
689
      'wb.de',
690
      'we.de',
691
      'web.ded',
692
      'web.dw',
693
      'wed.de',
694
      'weeb.de',
695
      'wen.de',
696
      'wweb.de',
697
      'yaho.de',
698
      'yahooo.de',
699
      'yaoo.de',
700
      'yhoo.de',
701
      'yahhoo.de',
702
  );
703
704
  /**
705
   * @var bool
706
   */
707
  protected static $useDnsCheck = true;
708
709
  /**
710
   * Check if the email is valid.
711
   *
712
   * @param string $email
713
   * @param bool   $useExampleDomainCheck
714
   * @param bool   $useTypoInDomainCheck
715
   * @param bool   $useTemporaryDomainCheck
716
   * @param bool   $useDnsCheck (do not use, if you don't need it)
717
   *
718
   * @return bool
719
   */
720 151
  public static function isValid($email, $useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false)
721
  {
722
    // must be a string
723 151
    if (!is_string($email)) {
724
      return false;
725
    }
726
727
    // make sure string length is limited to avoid DOS attacks
728 151
    $emailStringLength = strlen($email);
729
    if (
730
        $emailStringLength >= 320
731 151
        ||
732
        $emailStringLength <= 2 // i@y //
733 151
    ) {
734 4
      return false;
735
    }
736 149
    unset($emailStringLength);
737
738 149
    $email = str_replace(
739
        array(
740 149
            '.', // non-Latin chars are also allowed | https://tools.ietf.org/html/rfc6530
741 149
            '@', // non-Latin chars are also allowed | https://tools.ietf.org/html/rfc6530
742 149
        ),
743
        array(
744 149
            '.',
745 149
            '@',
746 149
        ),
747
        $email
748 149
    );
749
750
    if (
751 149
        (strpos($email, '@') === false) // "at" is needed
752
        ||
753 147
        (strpos($email, '.') === false && strpos($email, ':') === false) // "dot" or "colon" is needed
754 149
    ) {
755 24
      return false;
756
    }
757
758 128
    if (!preg_match('/^(?<local>.*<?)(?:.*)@(?<domain>.*)(?:>?)$/', $email, $parts)) {
759 9
      return false;
760
    }
761
762 119
    $local = $parts['local'];
763 119
    $domain = $parts['domain'];
764
765 119
    if (!$local) {
766 2
      return false;
767
    }
768
769 118
    if (!$domain) {
770
      return false;
771
    }
772
773
    // Escaped spaces are allowed in the "local"-part.
774 118
    $local = str_replace('\\ ', '', $local);
775
776
    // Spaces in quotes e.g. "firstname lastname"@foo.bar are also allowed in the "local"-part.
777 118
    $quoteHelperForIdn = false;
778 118
    if (preg_match('/^"(?<inner>[^"]*)"$/mU', $local, $parts)) {
779 17
      $quoteHelperForIdn = true;
780 17
      $local = trim(
781 17
          str_replace(
782 17
              $parts['inner'],
783 17
              str_replace(' ', '', $parts['inner']),
784
              $local
785 17
          ),
786
          '"'
787 17
      );
788 17
    }
789
790
    if (
791 118
        strpos($local, ' ') !== false // no spaces allowed, anymore
792 118
        ||
793 108
        strpos($local, '".') !== false // no quote + dot allowed
794 118
    ) {
795 13
      return false;
796
    }
797
798
    if (
799 106
        function_exists('idn_to_ascii')
800 106
        &&
801 106
        UTF8::max_chr_width($email) <= 3 // check for unicode chars with more then 3 bytes
802 106
    ) {
803
804 105
      $localTmp = idn_to_ascii($local);
805 105
      if ($localTmp) {
806 98
        $local = $localTmp;
807 98
      }
808 105
      unset($localTmp);
809
810
      // https://git.ispconfig.org/ispconfig/ispconfig3/blob/master/interface/lib/classes/functions.inc.php#L305
811 105
      if(defined('IDNA_NONTRANSITIONAL_TO_ASCII') && defined('INTL_IDNA_VARIANT_UTS46') && constant('IDNA_NONTRANSITIONAL_TO_ASCII')) {
812 105
        $domainTmp = idn_to_ascii($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
813 105
      } else {
814
        $domainTmp = idn_to_ascii($domain);
815
      }
816 105
      if ($domainTmp) {
817 101
        $domain = $domainTmp;
818 101
      }
819 105
      unset($domainTmp);
820
821 105
    } else {
822
823 1
      static $punycode = null;
824 1
      if ($punycode === null) {
825 1
        $punycode = new Punycode();
826 1
      }
827
828
      try {
829 1
        $local = $punycode->encode($local);
830 1
      } catch (LabelOutOfBoundsException $e) {
831
        $local = '';
832
      }
833
834
      try {
835 1
        $domain = $punycode->encode($domain);
836 1
      } catch (LabelOutOfBoundsException $e) {
837
        $domain = '';
838
      }
839
840
    }
841
842 106
    if ($quoteHelperForIdn === true) {
843 17
      $local = '"' . $local . '"';
844 17
    }
845
846 106
    $email = $local . '@' . $domain;
847
848 106
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
849 63
      return false;
850
    }
851
852 44
    if ($useExampleDomainCheck === true && self::isExampleDomain($domain) === true) {
853 2
      return false;
854
    }
855
856 44
    if ($useTypoInDomainCheck === true && self::isTypoInDomain($domain) === true) {
857 1
      return false;
858
    }
859
860 44
    if ($useTemporaryDomainCheck === true && self::isTemporaryDomain($domain) === true) {
861
      return false;
862
    }
863
864 44
    if ($useDnsCheck === true && self::isDnsError($domain) === true) {
865 3
      return false;
866
    }
867
868 41
    return true;
869
  }
870
871
  /**
872
   * Check if the domain is a example domain.
873
   *
874
   * @param string $domain
875
   *
876
   * @return bool
877
   */
878 2
  public static function isExampleDomain($domain)
879
  {
880 2
    if (in_array($domain, self::$domainsExample, true)) {
881 2
      return true;
882
    }
883
884 2
    return false;
885
  }
886
887
  /**
888
   * Check if the domain has a typo.
889
   *
890
   * @param string $domain
891
   *
892
   * @return bool
893
   */
894 3
  public static function isTypoInDomain($domain)
895
  {
896 3
    if (in_array($domain, self::$domainsTypo, true)) {
897 2
      return true;
898
    }
899
900 3
    return false;
901
  }
902
903
  /**
904
   * Check if the domain is a temporary domain.
905
   *
906
   * @param string $domain
907
   *
908
   * @return bool
909
   */
910 3
  public static function isTemporaryDomain($domain)
911
  {
912 3
    if (in_array($domain, self::$domainsTemporary, true)) {
913 1
      return true;
914
    }
915
916 3
    return false;
917
  }
918
919
  /**
920
   * Check if the domain has a typo.
921
   *
922
   * @param string $domain
923
   *
924
   * @return bool|null will return null if we can't use the "checkdnsrr"-function
925
   *
926
   * @throws \Exception
927
   */
928 4
  public static function isDnsError($domain)
929
  {
930 4
    if (function_exists('checkdnsrr')) {
931 4
      return !checkdnsrr($domain . '.', 'MX') || !checkdnsrr($domain, 'A');
932
    }
933
934
    throw new \Exception(' Can\'t call checkdnsrr');
935
  }
936
}
937