Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for php 8.1.2 / Ubuntu 22.04 #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 89 additions & 59 deletions vcard_convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,50 @@
class vCard
{
var $version;
var $displayname;
var $surname;
var $firstname;
var $middlename;
var $nickname;
var $title;
var $birthday;
var $organization;
var $department;
var $jobtitle;
var $home = array();
var $work = array();
var $countrycode;
var $relatedname;
var $email;
var $email2;
var $email3;
var $pager;
var $mobile;
var $displayname = '';
var $surname = '';
var $firstname = '';
var $middlename = '';
var $nickname = '';
var $title = '';
var $birthday = '';
var $gender = '';
var $organization = '';
var $department = '';
var $jobtitle = '';
var $home = array(
'phone' => '',
'url' => '',
'fax' => '',
'addr1' => '',
'addr2' => '',
'city' => '',
'state' => '',
'zipcode' => '',
'country' => '',
);
var $work = array(
'phone' => '',
'url' => '',
'fax' => '',
'addr1' => '',
'addr2' => '',
'city' => '',
'state' => '',
'zipcode' => '',
'country' => '',
);
var $countrycode = '';
var $relatedname = '';
var $email = '';
var $email2 = '';
var $email3 = '';
var $pager = '';
var $mobile = '';
var $im = array();
var $notes;
var $categories;
var $uid;
var $notes = '';
var $categories = '';
var $uid = '';
var $photo;
}

Expand Down Expand Up @@ -176,7 +197,7 @@ function normalize()
// extract birthday and anniversary
foreach (array('BDAY' => 'birthday', 'ANNIVERSARY' => 'anniversary', 'X-ANNIVERSARY' => 'anniversary') as $vcf => $propname)
{
if (is_array($card[$vcf]))
if (array_key_exists($vcf, $card) && is_array($card[$vcf]))
{
$temp = preg_replace('/[\-\.\/]/', '', $card[$vcf][0]['value'][0][0]);
$vcard->$propname = array(
Expand All @@ -186,77 +207,77 @@ function normalize()
}
}

if (is_array($card['GENDER']))
if (array_key_exists('GENDER', $card) && is_array($card['GENDER']))
$vcard->gender = $card['GENDER'][0]['value'][0][0];
else if (is_array($card['X-GENDER']))
else if (array_key_exists('X-GENDER', $card) && is_array($card['X-GENDER']))
$vcard->gender = $card['X-GENDER'][0]['value'][0][0];

if (!empty($vcard->gender))
$vcard->gender = strtoupper($vcard->gender[0]);

// extract job_title
if (is_array($card['TITLE']))
if (array_key_exists('TITLE', $card) && is_array($card['TITLE']))
$vcard->jobtitle = $card['TITLE'][0]['value'][0][0];

// extract UID
if (is_array($card['UID']))
if (array_key_exists('UID', $card) && is_array($card['UID']))
$vcard->uid = $card['UID'][0]['value'][0][0];

// extract org and dep
if (is_array($card['ORG']) && ($temp = $card['ORG'][0]['value']))
if (array_key_exists('ORG', $card) && is_array($card['ORG']) && ($temp = $card['ORG'][0]['value']))
{
$vcard->organization = trim($temp[0][0]);
$vcard->department = trim($temp[1][0]);
}

// extract urls
if (is_array($card['URL']))
if (array_key_exists('URL', $card) && is_array($card['URL']))
$this->parse_url($card['URL'], $vcard);

// extract addresses
if (is_array($card['ADR']))
if (array_key_exists('ADR', $card) && is_array($card['ADR']))
$this->parse_adr($card['ADR'], $vcard);

// extract phones
if (is_array($card['TEL']))
if (array_key_exists('TEL', $card) && is_array($card['TEL']))
$this->parse_tel($card['TEL'], $vcard);

// read Apple Address Book proprietary fields
for ($n = 1; $n <= 9; $n++)
{
$prefix = 'ITEM'.$n;
if (is_array($card["$prefix.TEL"])) {
if (array_key_exists("$prefix.TEL", $card) && is_array($card["$prefix.TEL"])) {
$this->parse_tel($card["$prefix.TEL"], $vcard);
}
if (is_array($card["$prefix.URL"])) {
if (array_key_exists("$prefix.URL", $card) && is_array($card["$prefix.URL"])) {
$this->parse_url($card["$prefix.URL"], $vcard);
}
if (is_array($card["$prefix.ADR"])) {
if (array_key_exists("$prefix.ADR", $card) && is_array($card["$prefix.ADR"])) {
$this->parse_adr($card["$prefix.ADR"], $vcard);
}
if (is_array($card["$prefix.X-ABADR"])) {
if (array_key_exists("$prefix.X-ABADR", $card) && is_array($card["$prefix.X-ABADR"])) {
$this->parse_cc($card["$prefix.X-ABADR"], $vcard);
}
if (is_array($card["$prefix.X-ABDATE"])) {
if (array_key_exists("$prefix.X-ABDATE", $card) && is_array($card["$prefix.X-ABDATE"])) {
$this->parse_abdate($card["$prefix.X-ABDATE"], $vcard, $card["$prefix.X-ABLABEL"][0]);
}
if (is_array($card["$prefix.X-ABRELATEDNAMES"])) {
if (array_key_exists("$prefix.X-ABRELATEDNAMES", $card) && is_array($card["$prefix.X-ABRELATEDNAMES"])) {
$this->parse_rn($card["$prefix.X-ABRELATEDNAMES"], $vcard /*, $card["$prefix.X-ABLABEL"][0]*/);
}
}

// extract e-mail addresses
$a_email = array();
$n = 0;
if (is_array($card['EMAIL'])) {
if (array_key_exists('EMAIL', $card) && is_array($card['EMAIL'])) {
while (isset($card['EMAIL'][$n])) {
$a_email[] = $card['EMAIL'][$n]['value'][0][0];
$n++;
}
}
if ($n < 2) { //as only 3 e-mail address will be exported we don't need to search for more
for ($n = 1; $n <= 9; $n++) {
if (is_array($card["ITEM$n.EMAIL"]))
if (array_key_exists("ITEM$n.EMAIL", $card) && is_array($card["ITEM$n.EMAIL"]))
{
$a_email[] = $card["ITEM$n.EMAIL"][0]['value'][0][0];
if (isset($card["ITEM$n.EMAIL"][1]))
Expand All @@ -273,16 +294,16 @@ function normalize()
$vcard->email3 = $a_email[2];

// find IM entries
if (is_array($card['X-AIM']))
if (array_key_exists('X-AIM', $card) && is_array($card['X-AIM']))
$vcard->im['aim'] = $card['X-AIM'][0]['value'][0][0];
if (is_array($card['X-IQC']))
if (array_key_exists('X-ICQ', $card) && is_array($card['X-ICQ']))
$vcard->im['icq'] = $card['X-ICQ'][0]['value'][0][0];
if (is_array($card['X-MSN']))
if (array_key_exists('X-MSN', $card) && is_array($card['X-MSN']))
$vcard->im['msn'] = $card['X-MSN'][0]['value'][0][0];
if (is_array($card['X-JABBER']))
if (array_key_exists('X-Jabber', $card) && is_array($card['X-JABBER']))
$vcard->im['jabber'] = $card['X-JABBER'][0]['value'][0][0];

if (is_array($card['PHOTO'][0]))
if (array_key_exists('PHOTO', $card) && is_array($card['PHOTO'][0]))
$vcard->photo = array('data' => $card['PHOTO'][0]['value'][0][0], 'encoding' => $card['PHOTO'][0]['param']['ENCODING'][0], 'type' => $card['PHOTO'][0]['param']['TYPE'][0]);

$vcard->categories = join(',', (array)$card['CATEGORIES'][0]['value'][0]);
Expand Down Expand Up @@ -358,46 +379,50 @@ function parse_adr(&$node, &$vcard)
}

// values not splitted by Contact_Vcard_Parse if key is like item1.ADR
if (strstr($home[0][0], ';'))
if (isset($home[0][0]) && strstr($home[0][0], ';'))
{
$temp = explode(';', $home[0][0]);
$vcard->home += array(
$vcard->home = array_merge($vcard->home, array(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add operator + is not equal to array_merge(): "for keys that exist in both arrays, the elements from the left-hand array will be used" whereas with array_merge, the second argument wins.

What's the problem with the add operator in PHP 8 (haven't tested it)?

'addr1' => $temp[2],
'city' => $temp[3],
'state' => $temp[4],
'zipcode' => $temp[5],
'country' => $temp[6]);
'country' => $temp[6]
));
}
else if (sizeof($home)>6)
else if (sizeof($home) > 6)
{
$vcard->home += array(
$vcard->home = array_merge($vcard->home, array(
'addr1' => $home[2][0],
'city' => $home[3][0],
'state' => $home[4][0],
'zipcode' => $home[5][0],
'country' => $home[6][0]);
'country' => $home[6][0]
));
}

// values not splitted by Contact_Vcard_Parse if key is like item1.ADR
if (strstr($work[0][0], ';'))
if (isset($work[0][0]) && strstr($work[0][0], ';'))
{
$temp = explode(';', $work[0][0]);
$vcard->work += array(
$vcard->work = array_merge($vcard->work, array(
'office' => $temp[1],
'addr1' => $temp[2],
'city' => $temp[3],
'state' => $temp[4],
'zipcode' => $temp[5],
'country' => $temp[6]);
'country' => $temp[6]
));
}
else if (sizeof($work)>6)
{
$vcard->work += array(
$vcard->work = array_merge($vcard->work, array(
'addr1' => $work[2][0],
'city' => $work[3][0],
'state' => $work[4][0],
'zipcode' => $work[5][0],
'country' => $work[6][0]);
'country' => $work[6][0]
));
}
}

Expand All @@ -410,7 +435,10 @@ function parse_tel(&$node, &$vcard)
{
foreach($node as $tel)
{
if (in_array_nc("PAGER", $tel['param']['TYPE']))
if (!isset($tel['param']['TYPE'])) {
$vcard->home['phone'] = $tel['value'][0][0];
}
else if (in_array_nc("PAGER", $tel['param']['TYPE']))
$vcard->pager = $tel['value'][0][0];
else if (in_array_nc("CELL", $tel['param']['TYPE']))
$vcard->mobile = $tel['value'][0][0];
Expand Down Expand Up @@ -438,7 +466,7 @@ function parse_tel(&$node, &$vcard)
$vcard->mobile = $tel['value'][0][0];
}

if (in_array_nc("FAX", $tel['param']['TYPE']))
if (isset($tel['param']['TYPE']) && in_array_nc("FAX", $tel['param']['TYPE']))
$vcard->fax = $tel['value'][0][0];
}
}
Expand Down Expand Up @@ -852,7 +880,7 @@ function toFritzBox()
if ($this->phoneonly && empty($card->home['phone']) && empty($card->work['phone']) && empty($card->mobile))
continue;

$name=array();
$name = array();
$firstname = $this->csv_encode($card->firstname, $delm, false);
$surname = $this->csv_encode($card->surname, $delm, false);
$organization = $this->csv_encode($card->organization, $delm, false);
Expand Down Expand Up @@ -950,6 +978,8 @@ function toImages($tmpdir)
*/
function csv_encode($str, $delm, $add_delm=true)
{
if ($str === NULL)
return "";
if (strpos($str, $delm))
$str = '"'.$str.'"';
return preg_replace('/\r?\n/', ' ', $str) . ($add_delm ? $delm : '');
Expand Down Expand Up @@ -982,7 +1012,7 @@ function ldif_encode($str, $encoding, $nowrap = false)
*/
function normalize_phone($phone)
{
if (strlen($this->accesscode))
if ($this->accesscode !== NULL && strlen($this->accesscode))
$phone = preg_replace('/^[\+|00]+' . $this->accesscode . '[- ]*(\d+)/', '0\1', $phone);
return $phone;
}
Expand Down
6 changes: 3 additions & 3 deletions vcfconvert.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env php -qC
#!/usr/bin/env -S php -q -C
<?php

/*
Expand Down Expand Up @@ -36,7 +36,7 @@ function get_args()
for ($j=1; $j < strlen($arg); $j++)
{
$key = $arg[$j];
$value = $_SERVER['argv'][$i+1]{0} != '-' ? preg_replace(array('/^["\']/', '/["\']$/'), '', $_SERVER['argv'][++$i]) : true;
$value = $_SERVER['argv'][$i+1][0] != '-' ? preg_replace(array('/^["\']/', '/["\']$/'), '', $_SERVER['argv'][++$i]) : true;
$args[$key] = $value;
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ Usage: vcfconvert.sh [-hilmpv] [-d delimiter] [-c utf-8] [-b identifier] [-o out
EOF;

// show help
if ($opt[0] == 'help')
if (count($opt) === 0 || $opt[0] == 'help')
die($usage);

// read arguments
Expand Down