Completed
Push — master ( 8b665e...8e58d2 )
by Lars
03:05
created

EmailCheck::isValid()   F

Complexity

Conditions 35
Paths 968

Size

Total Lines 166
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 91
CRAP Score 35.893

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 166
ccs 91
cts 100
cp 0.91
rs 2.0714
cc 35
eloc 99
nc 968
nop 5
crap 35.893

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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) 2017, 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 3
      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
      // https://git.ispconfig.org/ispconfig/ispconfig3/blob/master/interface/lib/classes/functions.inc.php#L305
805
      if (
806 105
          defined('IDNA_NONTRANSITIONAL_TO_ASCII')
807 105
          &&
808 105
          defined('INTL_IDNA_VARIANT_UTS46')
809 105
          &&
810 105
          constant('IDNA_NONTRANSITIONAL_TO_ASCII')
811 105
      ) {
812 105
        $useIdnaUts46 = true;
813 105
      } else {
814
        $useIdnaUts46 = false;
815
      }
816
817 105
      if ($useIdnaUts46 === true) {
818 105
        $localTmp = idn_to_ascii($local, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
819 105
      } else {
820
        $localTmp = idn_to_ascii($local);
821
      }
822 105
      if ($localTmp) {
823 98
        $local = $localTmp;
824 98
      }
825 105
      unset($localTmp);
826
827 105
      if ($useIdnaUts46 === true) {
828 105
        $domainTmp = idn_to_ascii($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
829 105
      } else {
830
        $domainTmp = idn_to_ascii($domain);
831
      }
832 105
      if ($domainTmp) {
833 101
        $domain = $domainTmp;
834 101
      }
835 105
      unset($domainTmp);
836
837 105
    } else {
838
839 1
      static $punycode = null;
840 1
      if ($punycode === null) {
841 1
        $punycode = new Punycode();
842 1
      }
843
844
      try {
845 1
        $local = $punycode->encode($local);
846 1
      } catch (LabelOutOfBoundsException $e) {
847
        $local = '';
848
      }
849
850
      try {
851 1
        $domain = $punycode->encode($domain);
852 1
      } catch (LabelOutOfBoundsException $e) {
853
        $domain = '';
854
      }
855
856
    }
857
858 106
    if ($quoteHelperForIdn === true) {
859 17
      $local = '"' . $local . '"';
860 17
    }
861
862 106
    $email = $local . '@' . $domain;
863
864 106
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
865 63
      return false;
866
    }
867
868 44
    if ($useExampleDomainCheck === true && self::isExampleDomain($domain) === true) {
869 2
      return false;
870
    }
871
872 44
    if ($useTypoInDomainCheck === true && self::isTypoInDomain($domain) === true) {
873 1
      return false;
874
    }
875
876 44
    if ($useTemporaryDomainCheck === true && self::isTemporaryDomain($domain) === true) {
877
      return false;
878
    }
879
880 44
    if ($useDnsCheck === true && self::isDnsError($domain) === true) {
881 3
      return false;
882
    }
883
884 41
    return true;
885
  }
886
887
  /**
888
   * Check if the domain is a example domain.
889
   *
890
   * @param string $domain
891
   *
892
   * @return bool
893
   */
894 2
  public static function isExampleDomain($domain)
895
  {
896 2
    if (in_array($domain, self::$domainsExample, true)) {
897 2
      return true;
898
    }
899
900 2
    return false;
901
  }
902
903
  /**
904
   * Check if the domain has a typo.
905
   *
906
   * @param string $domain
907
   *
908
   * @return bool
909
   */
910 3
  public static function isTypoInDomain($domain)
911
  {
912 3
    if (in_array($domain, self::$domainsTypo, true)) {
913 2
      return true;
914
    }
915
916 3
    return false;
917
  }
918
919
  /**
920
   * Check if the domain is a temporary domain.
921
   *
922
   * @param string $domain
923
   *
924
   * @return bool
925
   */
926 3
  public static function isTemporaryDomain($domain)
927
  {
928 3
    if (in_array($domain, self::$domainsTemporary, true)) {
929 1
      return true;
930
    }
931
932 3
    return false;
933
  }
934
935
  /**
936
   * Check if the domain has a typo.
937
   *
938
   * @param string $domain
939
   *
940
   * @return bool|null will return null if we can't use the "checkdnsrr"-function
941
   *
942
   * @throws \Exception
943
   */
944 4
  public static function isDnsError($domain)
945
  {
946 4
    if (function_exists('checkdnsrr')) {
947 4
      return !checkdnsrr($domain . '.', 'MX') || !checkdnsrr($domain, 'A');
948
    }
949
950
    throw new \Exception(' Can\'t call checkdnsrr');
951
  }
952
}
953