Completed
Pull Request — master (#58)
by
unknown
02:22
created

VCard::addAddress()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 22
rs 9.2
cc 2
eloc 15
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace JeroenDesloovere\VCard;
4
5
/*
6
 * This file is part of the VCard PHP Class from Jeroen Desloovere.
7
 *
8
 * For the full copyright and license information, please view the license
9
 * file that was distributed with this source code.
10
 */
11
12
use Behat\Transliterator\Transliterator;
13
14
/**
15
 * VCard PHP Class to generate .vcard files and save them to a file or output as a download.
16
 *
17
 * @author Jeroen Desloovere <[email protected]>
18
 */
19
class VCard
20
{
21
    /**
22
     * definedElements
23
     *
24
     * @var array
25
     */
26
    private $definedElements;
27
28
    /**
29
     * Filename
30
     *
31
     * @var string
32
     */
33
    private $filename;
34
35
    /**
36
     * Multiple properties for element allowed
37
     *
38
     * @var array
39
     */
40
    private $multiplePropertiesForElementAllowed = array(
41
        'email',
42
        'address',
43
        'phoneNumber',
44
        'url'
45
    );
46
47
    /**
48
     * Properties
49
     *
50
     * @var array
51
     */
52
    private $properties;
53
54
    /**
55
     * Default Charset
56
     *
57
     * @var string
58
     */
59
    public $charset = 'utf-8';
60
61
    /**
62
     * Add address
63
     *
64
     * @param  string [optional] $name
65
     * @param  string [optional] $extended
66
     * @param  string [optional] $street
67
     * @param  string [optional] $city
68
     * @param  string [optional] $region
69
     * @param  string [optional] $zip
70
     * @param  string [optional] $country
71
     * @param  string [optional] $type
72
     *                                     $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK
73
     *                                     or any combination of these: e.g. "WORK;PARCEL;POSTAL"
74
     * @return $this
75
     */
76
    public function addAddress(
77
        $name = '',
78
        $extended = '',
79
        $street = '',
80
        $city = '',
81
        $region = '',
82
        $zip = '',
83
        $country = '',
84
        $type = 'WORK;POSTAL'
85
    ) {
86
        // init value
87
        $value = $name . ';' . $extended . ';' . $street . ';' . $city . ';' . $region . ';' . $zip . ';' . $country;
88
89
        // set property
90
        $this->setProperty(
91
            'address',
92
            'ADR' . (($type != '') ? ';' . $type : '') . $this->getCharsetString(),
93
            $value
94
        );
95
96
        return $this;
97
    }
98
99
    /**
100
     * Add birthday
101
     *
102
     * @param  string $date Format is YYYY-MM-DD
103
     * @return $this
104
     */
105
    public function addBirthday($date)
106
    {
107
        $this->setProperty(
108
            'birthday',
109
            'BDAY',
110
            $date
111
        );
112
113
        return $this;
114
    }
115
116
    /**
117
     * Add company
118
     *
119
     * @param  string $company
120
     * @return $this
121
     */
122
    public function addCompany($company)
123
    {
124
        $this->setProperty(
125
            'company',
126
            'ORG' . $this->getCharsetString(),
127
            $company
128
        );
129
130
        // if filename is empty, add to filename
131
        if ($this->getFilename() === null) {
132
            $this->setFilename($company);
133
        }
134
135
        return $this;
136
    }
137
138
    /**
139
     * Add email
140
     *
141
     * @param  string            $address The e-mail address
142
     * @param  string [optional] $type    The type of the email address
143
     *                                    $type may be  PREF | WORK | HOME
144
     *                                    or any combination of these: e.g. "PREF;WORK"
145
     * @return $this
146
     */
147
    public function addEmail($address, $type = '')
148
    {
149
        $this->setProperty(
150
            'email',
151
            'EMAIL;INTERNET' . (($type != '') ? ';' . $type : ''),
152
            $address
153
        );
154
155
        return $this;
156
    }
157
158
    /**
159
     * Add jobtitle
160
     *
161
     * @param  string $jobtitle The jobtitle for the person.
162
     * @return $this
163
     */
164
    public function addJobtitle($jobtitle)
165
    {
166
        $this->setProperty(
167
            'jobtitle',
168
            'TITLE' . $this->getCharsetString(),
169
            $jobtitle
170
        );
171
172
        return $this;
173
    }
174
175
    /**
176
     * Add a photo or logo (depending on property name)
177
     *
178
     * @param  string              $property LOGO|PHOTO
179
     * @param  string              $url      image url or filename
180
     * @param  bool                $include  Do we include the image in our vcard or not?
181
     * @throws VCardMediaException if file is empty or not an image file
182
     */
183
    private function addMedia($property, $url, $include = true, $element)
184
    {
185
        if ($include) {
186
            $value = file_get_contents($url);
187
188
            if (!$value) {
189
                throw new VCardMediaException('Nothing returned from URL.');
190
            }
191
192
            $value = base64_encode($value);
193
194
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
195
            $mimetype = finfo_file($finfo, 'data://application/octet-stream;base64,' . $value);
196
            finfo_close($finfo);
197
198
            if (preg_match('/^image\//', $mimetype) !== 1) {
199
                throw new VCardMediaException('Returned data aren\'t an image.');
200
            }
201
202
            $type = strtoupper(str_replace('image/', '', $mimetype));
203
204
            $property .= ";ENCODING=b;TYPE=" . $type;
205
        } else {
206
            if (filter_var($url, FILTER_VALIDATE_URL) !== FALSE) {
207
                $propertySuffix = ';VALUE=URL';
208
209
                $headers = get_headers($url);
210
211
                $imageTypeMatched = false;
212
                $fileType = null;
213
214
                foreach ($headers as $header) {
215
                    if (preg_match('/Content-Type:\simage\/([a-z]+)/i', $header, $m)) {
216
                        $fileType = $m[1];
217
                        $imageTypeMatched = true;
218
                    }
219
                }
220
221
                if (!$imageTypeMatched) {
222
                    throw new VCardMediaException('Returned data isn\'t an image.');
223
                }
224
225
                $propertySuffix .= ';TYPE=' . strtoupper($fileType);
226
227
                $property = $property . $propertySuffix;
228
                $value = $url;
229
            } else {
230
                $value = $url;
231
            }
232
        }
233
234
        $this->setProperty(
235
            $element,
236
            $property,
237
            $value
238
        );
239
    }
240
241
    /**
242
     * Add name
243
     *
244
     * @param  string [optional] $lastName
245
     * @param  string [optional] $firstName
246
     * @param  string [optional] $additional
247
     * @param  string [optional] $prefix
248
     * @param  string [optional] $suffix
249
     * @return $this
250
     */
251
    public function addName(
252
        $lastName = '',
253
        $firstName = '',
254
        $additional = '',
255
        $prefix = '',
256
        $suffix = ''
257
    ) {
258
        // define values with non-empty values
259
        $values = array_filter(array(
260
            $prefix,
261
            $firstName,
262
            $additional,
263
            $lastName,
264
            $suffix,
265
        ));
266
267
        // define filename
268
        $this->setFilename($values);
269
270
        // set property
271
        $property = $lastName . ';' . $firstName . ';' . $additional . ';' . $prefix . ';' . $suffix;
272
        $this->setProperty(
273
            'name',
274
            'N' . $this->getCharsetString(),
275
            $property
276
        );
277
278
        // is property FN set?
279
        if (!$this->hasProperty('FN')) {
280
            // set property
281
            $this->setProperty(
282
                'fullname',
283
                'FN' . $this->getCharsetString(),
284
                trim(implode(' ', $values))
285
            );
286
        }
287
288
        return $this;
289
    }
290
291
    /**
292
     * Add note
293
     *
294
     * @param  string $note
295
     * @return $this
296
     */
297
    public function addNote($note)
298
    {
299
        $this->setProperty(
300
            'note',
301
            'NOTE' . $this->getCharsetString(),
302
            $note
303
        );
304
305
        return $this;
306
    }
307
308
    /**
309
     * Add phone number
310
     *
311
     * @param  string            $number
312
     * @param  string [optional] $type
313
     *                                   Type may be PREF | WORK | HOME | VOICE | FAX | MSG |
314
     *                                   CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO
315
     *                                   or any senseful combination, e.g. "PREF;WORK;VOICE"
316
     * @return $this
317
     */
318
    public function addPhoneNumber($number, $type = '')
319
    {
320
        $this->setProperty(
321
            'phoneNumber',
322
            'TEL' . (($type != '') ? ';' . $type : ''),
323
            $number
324
        );
325
326
        return $this;
327
    }
328
329
    /**
330
     * Add Logo
331
     *
332
     * @param  string $url     image url or filename
333
     * @param  bool   $include Include the image in our vcard?
334
     * @return $this
335
     */
336
    public function addLogo($url, $include = true)
337
    {
338
        $this->addMedia(
339
            'LOGO',
340
            $url,
341
            $include,
342
            'logo'
343
        );
344
345
        return $this;
346
    }
347
348
    /**
349
     * Add Photo
350
     *
351
     * @param  string $url     image url or filename
352
     * @param  bool   $include Include the image in our vcard?
353
     * @return $this
354
     */
355
    public function addPhoto($url, $include = true)
356
    {
357
        $this->addMedia(
358
            'PHOTO',
359
            $url,
360
            $include,
361
            'photo'
362
        );
363
364
        return $this;
365
    }
366
367
    /**
368
     * Add URL
369
     *
370
     * @param  string            $url
371
     * @param  string [optional] $type Type may be WORK | HOME
372
     * @return $this
373
     */
374
    public function addURL($url, $type = '')
375
    {
376
        $this->setProperty(
377
            'url',
378
            'URL' . (($type != '') ? ';' . $type : ''),
379
            $url
380
        );
381
382
        return $this;
383
    }
384
385
    /**
386
     * Build VCard (.vcf)
387
     *
388
     * @return string
389
     */
390
    public function buildVCard()
391
    {
392
        // init string
393
        $string = "BEGIN:VCARD\r\n";
394
        $string .= "VERSION:3.0\r\n";
395
        $string .= "REV:" . date("Y-m-d") . "T" . date("H:i:s") . "Z\r\n";
396
397
        // loop all properties
398
        $properties = $this->getProperties();
399
        foreach ($properties as $property) {
400
            // add to string
401
            $string .= $this->fold($property['key'] . ':' . $property['value'] . "\r\n");
402
        }
403
404
        // add to string
405
        $string .= "END:VCARD\r\n";
406
407
        // return
408
        return $string;
409
    }
410
411
    /**
412
     * Build VCalender (.ics) - Safari (< iOS 8) can not open .vcf files, so we have build a workaround.
413
     *
414
     * @return string
415
     */
416
    public function buildVCalendar()
417
    {
418
        // init dates
419
        $dtstart = date("Ymd") . "T" . date("Hi") . "00";
420
        $dtend = date("Ymd") . "T" . date("Hi") . "01";
421
422
        // init string
423
        $string = "BEGIN:VCALENDAR\n";
424
        $string .= "VERSION:2.0\n";
425
        $string .= "BEGIN:VEVENT\n";
426
        $string .= "DTSTART;TZID=Europe/London:" . $dtstart . "\n";
427
        $string .= "DTEND;TZID=Europe/London:" . $dtend . "\n";
428
        $string .= "SUMMARY:Click attached contact below to save to your contacts\n";
429
        $string .= "DTSTAMP:" . $dtstart . "Z\n";
430
        $string .= "ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;\n";
431
        $string .= " X-APPLE-FILENAME=" . $this->getFilename() . "." . $this->getFileExtension() . ":\n";
432
433
        // base64 encode it so that it can be used as an attachemnt to the "dummy" calendar appointment
434
        $b64vcard = base64_encode($this->buildVCard());
435
436
        // chunk the single long line of b64 text in accordance with RFC2045
437
        // (and the exact line length determined from the original .ics file exported from Apple calendar
438
        $b64mline = chunk_split($b64vcard, 74, "\n");
439
440
        // need to indent all the lines by 1 space for the iphone (yes really?!!)
441
        $b64final = preg_replace('/(.+)/', ' $1', $b64mline);
442
        $string .= $b64final;
443
444
        // output the correctly formatted encoded text
445
        $string .= "END:VEVENT\n";
446
        $string .= "END:VCALENDAR\n";
447
448
        // return
449
        return $string;
450
    }
451
452
    /**
453
     * Returns the browser user agent string.
454
     *
455
     * @return string
456
     */
457
    protected function getUserAgent()
0 ignored issues
show
Coding Style introduced by
getUserAgent uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
458
    {
459
        if (array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
460
            $browser = strtolower($_SERVER['HTTP_USER_AGENT']);
461
        } else {
462
            $browser = 'unknown';
463
        }
464
465
        return $browser;
466
    }
467
468
    /**
469
     * Decode
470
     *
471
     * @param  string $value The value to decode
472
     * @return string decoded
473
     */
474
    private function decode($value)
475
    {
476
        // convert cyrlic, greek or other caracters to ASCII characters
477
        return Transliterator::transliterate($value);
478
    }
479
480
    /**
481
     * Download a vcard or vcal file to the browser.
482
     */
483
    public function download()
484
    {
485
        // define output
486
        $output = $this->getOutput();
487
488
        foreach ($this->getHeaders(false) as $header) {
489
            header($header);
490
        }
491
492
        // echo the output and it will be a download
493
        echo $output;
494
    }
495
496
    /**
497
     * Fold a line according to RFC2425 section 5.8.1.
498
     *
499
     * @link http://tools.ietf.org/html/rfc2425#section-5.8.1
500
     * @param  string $text
501
     * @return mixed
502
     */
503
    protected function fold($text)
504
    {
505
        if (strlen($text) <= 75) {
506
            return $text;
507
        }
508
509
        // split, wrap and trim trailing separator
510
        return substr(chunk_split($text, 73, "\r\n "), 0, -3);
511
    }
512
513
    /**
514
     * Get output as string
515
     * @deprecated in the future
516
     *
517
     * @return string
518
     */
519
    public function get()
520
    {
521
        return $this->getOutput();
522
    }
523
524
    /**
525
     * Get charset
526
     *
527
     * @return string
528
     */
529
    public function getCharset()
530
    {
531
        return $this->charset;
532
    }
533
534
    /**
535
     * Get charset string
536
     *
537
     * @return string
538
     */
539
    public function getCharsetString()
540
    {
541
        $charsetString = '';
542
        if ($this->charset == 'utf-8') {
543
            $charsetString = ';CHARSET=' . $this->charset;
544
        }
545
        return $charsetString;
546
    }
547
548
    /**
549
     * Get content type
550
     *
551
     * @return string
552
     */
553
    public function getContentType()
554
    {
555
        return ($this->isIOS7()) ?
556
            'text/x-vcalendar' : 'text/x-vcard';
557
    }
558
559
    /**
560
     * Get filename
561
     *
562
     * @return string
563
     */
564
    public function getFilename()
565
    {
566
        return $this->filename;
567
    }
568
569
    /**
570
     * Get file extension
571
     *
572
     * @return string
573
     */
574
    public function getFileExtension()
575
    {
576
        return ($this->isIOS7()) ?
577
            'ics' : 'vcf';
578
    }
579
580
    /**
581
     * Get headers
582
     *
583
     * @param  bool  $asAssociative
584
     * @return array
585
     */
586
    public function getHeaders($asAssociative)
587
    {
588
        $contentType        = $this->getContentType() . '; charset=' . $this->getCharset();
589
        $contentDisposition = 'attachment; filename=' . $this->getFilename() . '.' . $this->getFileExtension();
590
        $contentLength      = strlen($this->getOutput());
591
        $connection         = 'close';
592
593
        if ((bool) $asAssociative) {
594
            return array(
595
                'Content-type'        => $contentType,
596
                'Content-Disposition' => $contentDisposition,
597
                'Content-Length'      => $contentLength,
598
                'Connection'          => $connection,
599
            );
600
        }
601
602
        return array(
603
            'Content-type: ' . $contentType,
604
            'Content-Disposition: ' . $contentDisposition,
605
            'Content-Length: ' . $contentLength,
606
            'Connection: ' . $connection,
607
        );
608
    }
609
610
    /**
611
     * Get output as string
612
     * iOS devices (and safari < iOS 8 in particular) can not read .vcf (= vcard) files.
613
     * So I build a workaround to build a .ics (= vcalender) file.
614
     *
615
     * @return string
616
     */
617
    public function getOutput()
618
    {
619
        $output = ($this->isIOS7()) ?
620
            $this->buildVCalendar() : $this->buildVCard();
621
622
        return $output;
623
    }
624
625
    /**
626
     * Get properties
627
     *
628
     * @return array
629
     */
630
    public function getProperties()
631
    {
632
        return $this->properties;
633
    }
634
635
    /**
636
     * Has property
637
     *
638
     * @param  string $key
639
     * @return bool
640
     */
641
    public function hasProperty($key)
642
    {
643
        $properties = $this->getProperties();
644
645
        foreach ($properties as $property) {
646
            if ($property['key'] === $key && $property['value'] !== '') {
647
                return true;
648
            }
649
        }
650
651
        return false;
652
    }
653
654
    /**
655
     * Is iOS - Check if the user is using an iOS-device
656
     *
657
     * @return bool
658
     */
659
    public function isIOS()
660
    {
661
        // get user agent
662
        $browser = $this->getUserAgent();
663
664
        return (strpos($browser, 'iphone') || strpos($browser, 'ipod') || strpos($browser, 'ipad'));
665
    }
666
667
    /**
668
     * Is iOS less than 7 (should cal wrapper be returned)
669
     *
670
     * @return bool
671
     */
672
    public function isIOS7()
673
    {
674
        return ($this->isIOS() && $this->shouldAttachmentBeCal());
675
    }
676
677
    /**
678
     * Save to a file
679
     *
680
     * @return void
681
     */
682
    public function save()
683
    {
684
        $file = $this->getFilename() . '.' . $this->getFileExtension();
685
686
        file_put_contents(
687
            $file,
688
            $this->getOutput()
689
        );
690
    }
691
692
    /**
693
     * Set charset
694
     *
695
     * @param  mixed  $charset
696
     * @return void
697
     */
698
    public function setCharset($charset)
699
    {
700
        $this->charset = $charset;
701
    }
702
703
    /**
704
     * Set filename
705
     *
706
     * @param  mixed  $value
707
     * @param  bool   $overwrite [optional] Default overwrite is true
708
     * @param  string $separator [optional] Default separator is an underscore '_'
709
     * @return void
710
     */
711
    public function setFilename($value, $overwrite = true, $separator = '_')
712
    {
713
        // recast to string if $value is array
714
        if (is_array($value)) {
715
            $value = implode($separator, $value);
716
        }
717
718
        // trim unneeded values
719
        $value = trim($value, $separator);
720
721
        // remove all spaces
722
        $value = preg_replace('/\s+/', $separator, $value);
723
724
        // if value is empty, stop here
725
        if (empty($value)) {
726
            return;
727
        }
728
729
        // decode value + lowercase the string
730
        $value = strtolower($this->decode($value));
731
732
        // urlize this part
733
        $value = Transliterator::urlize($value);
734
735
        // overwrite filename or add to filename using a prefix in between
736
        $this->filename = ($overwrite) ?
737
            $value : $this->filename . $separator . $value;
738
    }
739
740
    /**
741
     * Set property
742
     *
743
     * @param  string $element The element name you want to set, f.e.: name, email, phoneNumber, ...
744
     * @param  string $key
745
     * @param  string $value
746
     * @return void
747
     */
748
    private function setProperty($element, $key, $value)
749
    {
750
        if (!in_array($element, $this->multiplePropertiesForElementAllowed)
751
            && isset($this->definedElements[$element])
752
        ) {
753
            throw new Exception('You can only set "' . $element . '" once.');
754
        }
755
756
        // we define that we set this element
757
        $this->definedElements[$element] = true;
758
759
        // adding property
760
        $this->properties[] = array(
761
            'key' => $key,
762
            'value' => $value
763
        );
764
    }
765
766
    /**
767
     * Checks if we should return vcard in cal wrapper
768
     *
769
     * @return bool
770
     */
771
    protected function shouldAttachmentBeCal()
772
    {
773
        $browser = $this->getUserAgent();
774
775
        $matches = array();
776
        preg_match('/os (\d+)_(\d+)\s+/', $browser, $matches);
777
        $version = isset($matches[1]) ? ((int) $matches[1]) : 999;
778
779
        return ($version < 8);
780
    }
781
}
782