VCard::setLabel()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 128
nop 8
dl 0
loc 34
rs 8.2111
c 0
b 0
f 0

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 XoopsModules\Wflinks;
4
5
/***************************************************************************
6
 *
7
 * PHP vCard class v2.0
8
 * (c) Kai Blankenhorn
9
 * www.bitfolge.de/en
10
 * [email protected]
11
 *
12
 * This program is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU General Public License
14
 * as published by the Free Software Foundation; either version 2
15
 * of the License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
 ***************************************************************************/
26
27
/***************************************************************************
28
 *
29
 * Modified for WF-Links by McDonald - March 2008
30
 **************************************************************************
31
 * @param $string
32
 * @return mixed
33
 */
34
35
use XoopsModules\Wflinks;
36
37
/**
38
 * @param $string
39
 * @return mixed
40
 */
41
function vcard_encode($string)
42
{
43
    return vcard_escape(vcard_quoted_printable_encode($string));
44
}
45
46
/**
47
 * @param $string
48
 *
49
 * @return mixed
50
 */
51
function vcard_escape($string)
52
{
53
    return \str_replace(';', "\;", $string);
54
}
55
56
/**
57
 * @param $email
58
 *
59
 * @return mixed
60
 */
61
function vcardemailcnvrt($email)
62
{
63
    $search = [
64
        "/\ AT /",
65
        "/\ DOT /",
66
    ];
67
68
    $replace = [
69
        '@',
70
        '.',
71
    ];
72
73
    $text = \preg_replace($search, $replace, $email);
74
75
    return $text;
76
}
77
78
// taken from PHP documentation comments
79
/**
80
 * @param     $input
81
 * @param int $line_max
82
 *
83
 * @return string
84
 */
85
function vcard_quoted_printable_encode($input, $line_max = 76)
86
{
87
    $hex       = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
88
    $lines     = \preg_split("/(?:\r\n|\r|\n)/", $input);
89
    $eol       = "\r\n";
90
    $linebreak = '=0D=0A';
91
    $escape    = '=';
92
    $output    = '';
93
94
    for ($j = 0, $jMax = \count($lines); $j < $jMax; ++$j) {
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
    for ($j = 0, $jMax = \count(/** @scrutinizer ignore-type */ $lines); $j < $jMax; ++$j) {
Loading history...
95
        $line    = $lines[$j];
96
        $linlen  = mb_strlen($line);
97
        $newline = '';
98
        for ($i = 0; $i < $linlen; ++$i) {
99
            $c   = mb_substr($line, $i, 1);
100
            $dec = \ord($c);
101
            if ((32 == $dec) && ($i == ($linlen - 1))) { // convert space at eol only
102
                $c = '=20';
103
            } elseif ((61 == $dec) || ($dec < 32)
104
                      || ($dec > 126)) { // always vcard_encode "\t", which is *not* required
105
                $h2 = \floor($dec / 16);
106
                $h1 = \floor($dec % 16);
107
                $c  = $escape . $hex[(string)$h2] . $hex[(string)$h1];
108
            }
109
            if ((mb_strlen($newline) + mb_strlen($c)) >= $line_max) { // CRLF is not counted
110
                $output  .= $newline . $escape . $eol; // soft line break; " =\r\n" is okay
111
                $newline = '    ';
112
            }
113
            $newline .= $c;
114
        } // end of for
115
        $output .= $newline;
116
        if ($j < \count($lines) - 1) {
117
            $output .= $linebreak;
118
        }
119
    }
120
121
    return \trim($output);
122
}
123
124
/**
125
 * Class VCard
126
 */
127
class VCard
128
{
129
    public $properties;
130
    public $filename;
131
132
    /**
133
     * @param        $number
134
     * @param string $type
135
     */
136
    public function setPhoneNumber($number, $type = '')
137
    {
138
        // type may be PREF | WORK | HOME | VOICE | FAX | MSG | CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO or any senseful combination, e.g. "PREF;WORK;VOICE"
139
        $key = 'TEL';
140
        if ('' !== $type) {
141
            $key .= ';' . $type;
142
        }
143
        $key                    .= ';ENCODING=QUOTED-PRINTABLE';
144
        $this->properties[$key] = vcard_quoted_printable_encode($number);
145
    }
146
147
    // UNTESTED !!!
148
149
    /**
150
     * @param $type
151
     * @param $photo
152
     */
153
    public function setPhoto($type, $photo)
154
    { // $type = "GIF" | "JPEG"
155
        $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_vcard_encode($photo);
0 ignored issues
show
Bug introduced by
The function base64_vcard_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
        $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = /** @scrutinizer ignore-call */ base64_vcard_encode($photo);
Loading history...
156
    }
157
158
    /**
159
     * @param $name
160
     */
161
    public function setFormattedName($name)
162
    {
163
        $this->properties['FN'] = vcard_quoted_printable_encode($name);
164
    }
165
166
    /**
167
     * @param string $family
168
     * @param string $first
169
     * @param string $additional
170
     * @param string $prefix
171
     * @param string $suffix
172
     */
173
    public function setName($family = '', $first = '', $additional = '', $prefix = '', $suffix = '')
174
    {
175
        $this->properties['N'] = "$family;$first;$additional;$prefix;$suffix";
176
        //  $this -> filename = "$first%20$family.vcf";
177
        if ('' === $this->properties['FN']) {
178
            $this->setFormattedName(\trim("$prefix $first $additional $family $suffix"));
179
        }
180
    }
181
182
    /**
183
     * @param $date
184
     */
185
    public function setBirthday($date)
186
    { // $date format is YYYY-MM-DD
187
        $this->properties['BDAY'] = $date;
188
    }
189
190
    /**
191
     * @param string $postoffice
192
     * @param string $extended
193
     * @param string $street
194
     * @param string $city
195
     * @param string $region
196
     * @param string $zip
197
     * @param string $country
198
     * @param string $type
199
     */
200
    public function setAddress(
201
        $postoffice = '',
202
        $extended = '',
203
        $street = '',
204
        $city = '',
205
        $region = '',
206
        $zip = '',
207
        $country = '',
208
        $type = ''
209
    ) {
210
        // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
211
        $key = 'ADR';
212
        if ('' !== $type) {
213
            $key .= ";$type";
214
        }
215
        $key                    .= ';ENCODING=QUOTED-PRINTABLE';
216
        $this->properties[$key] = vcard_encode($postoffice) . ';' . vcard_encode($extended) . ';' . vcard_encode($street) . ';' . vcard_encode($city) . ';' . vcard_encode($region) . ';' . vcard_encode($zip) . ';' . vcard_encode($country);
217
218
        if ('' === $this->properties["LABEL;$type;ENCODING=QUOTED-PRINTABLE"]) {
219
            //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
220
        }
221
    }
222
223
    /**
224
     * @param string $postoffice
225
     * @param string $extended
226
     * @param string $street
227
     * @param string $city
228
     * @param string $region
229
     * @param string $zip
230
     * @param string $country
231
     * @param string $type
232
     */
233
    public function setLabel(
234
        $postoffice = '',
235
        $extended = '',
236
        $street = '',
237
        $city = '',
238
        $region = '',
239
        $zip = '',
240
        $country = '',
241
        $type = 'HOME;POSTAL'
242
    ) {
243
        $label = '';
244
        if ('' !== $postoffice) {
245
            $label .= "$postoffice\r\n";
246
        }
247
        if ('' !== $extended) {
248
            $label .= "$extended\r\n";
249
        }
250
        if ('' !== $street) {
251
            $label .= "$street\r\n";
252
        }
253
        if ('' !== $zip) {
254
            $label .= "$zip ";
255
        }
256
        if ('' !== $city) {
257
            $label .= "$city\r\n";
258
        }
259
        if ('' !== $region) {
260
            $label .= "$region\r\n";
261
        }
262
        if ('' !== $country) {
263
            $country .= "$country\r\n";
264
        }
265
266
        $this->properties["LABEL;$type;ENCODING=QUOTED-PRINTABLE"] = vcard_quoted_printable_encode($label);
267
    }
268
269
    /**
270
     * @param $address
271
     */
272
    public function setEmail($address)
273
    {
274
        $this->properties['EMAIL;INTERNET'] = $address;
275
    }
276
277
    /**
278
     * @param $note
279
     */
280
    public function setNote($note)
281
    {
282
        $this->properties['NOTE;ENCODING=QUOTED-PRINTABLE'] = vcard_quoted_printable_encode($note);
283
    }
284
285
    /**
286
     * @param        $url
287
     * @param string $type
288
     */
289
    public function setURL($url, $type = '')
290
    {
291
        // $type may be WORK | HOME
292
        $key = 'URL';
293
        if ('' !== $type) {
294
            $key .= ";$type";
295
        }
296
        $this->properties[$key] = $url;
297
    }
298
299
    /**
300
     * @param $comp
301
     */
302
    public function setCOMP($comp)
303
    {
304
        $this->filename          = "$comp.vcf";
305
        $this->properties['ORG'] = $comp;
306
    }
307
308
    /**
309
     * @return string
310
     */
311
    public function getVCard()
312
    {
313
        $text = "BEGIN:VCARD\r\n";
314
        $text .= "VERSION:2.1\r\n";
315
        foreach ($this->properties as $key => $value) {
316
            $text .= "$key:$value\r\n";
317
        }
318
        $text .= 'REV:' . \date('Y-m-d') . 'T' . \date('H:i:s') . "Z\r\n";
319
        $text .= "MAILER:PHP vCard class by Kai Blankenhorn\r\n";
320
        $text .= "END:VCARD\r\n";
321
322
        return $text;
323
    }
324
325
    public function getFileName()
326
    {
327
        return $this->filename;
328
    }
329
330
    // NOT TESTED -- McDONALD
331
332
    /**
333
     * @param $charset
334
     */
335
    public function setADR($charset)
336
    {
337
        $this->properties['ADR;CHARSET'] = $charset;
338
    }
339
}
340
341
require_once __DIR__ . '/header.php';
342
343
$lid = \Xmf\Request::getInt('lid', 0);
344
$lid = (int)$lid;
345
346
global $xoopsDB;
347
$result    = $xoopsDB->query('SELECT title, street1, street2, town, zip, state, country, url, tel, fax, voip, mobile, email, vat FROM ' . $xoopsDB->prefix('wflinks_links') . ' WHERE lid=' . $lid);
348
$vcard_arr = $xoopsDB->fetchArray($result);
349
350
$title   = $vcard_arr['title'];
351
$street1 = $vcard_arr['street1'];
352
$street2 = $vcard_arr['street2'];
353
$town    = $vcard_arr['town'];
354
$zip     = $vcard_arr['zip'];
355
$state   = $vcard_arr['state'];
356
$country = Wflinks\Utility::getCountryName($vcard_arr['country']);
357
$tel     = $vcard_arr['tel'];
358
$mobile  = $vcard_arr['mobile'];
359
$fax     = $vcard_arr['fax'];
360
$voip    = $vcard_arr['voip'];
361
$url     = $vcard_arr['url'];
362
$email   = $vcard_arr['email'];
363
$vat     = $vcard_arr['vat'];
364
$charset = _CHARSET;
365
366
$v = new VCard();
367
368
// Set Xoops Character set
369
$v->setADR($charset);
370
371
// Phone number(s)
372
$v->setPhoneNumber($tel, 'WORK;VOICE;PREF');
373
$v->setPhoneNumber($mobile, 'WORK;CELL');
374
$v->setPhoneNumber($fax, 'WORK;FAX');
375
$v->setPhoneNumber($voip, 'WORK;PCS');
376
377
// Name
378
$v->setName('', '', '', '');
379
380
// Birthday
381
// $v -> setBirthday("1960-07-31");
382
383
// Address
384
$street = $street1;
385
if ($street2) {
386
    $street = $street1 . ', ' . $street2;
387
}
388
$v->setAddress('', '', $street, $town, $state, $zip, $country, 'WORK');
389
390
// Email
391
$emailaddr = vcardemailcnvrt($email);
392
$v->setEmail($emailaddr);
393
394
// Note
395
// $v -> setNote("You can take some notes here.\r\nMultiple lines are supported via \\r\\n.");
396
if ($voip) {
397
    $v->setNote('VoIP: ' . $voip);
398
}
399
if ($vat) {
400
    $v->setNote(_MD_WFL_VAT . $vat);
401
}
402
403
// Website
404
$v->setURL($url, 'WORK');
405
406
// Company name
407
$v->setCOMP($title);
408
409
$output   = $v->getVCard();
410
$filename = $v->getFileName();
411
412
\header("Content-Disposition: attachment; filename=$filename");
413
\header('Content-Length: ' . mb_strlen($output));
414
\header('Connection: close');
415
\header("Content-Type: text/x-vCard; name=$filename");
416
417
echo $output;
418