1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Google; |
5
|
|
|
|
6
|
|
|
use SKien\VCard\VCard; |
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Export one or more GContacts to a VCard file. |
10
|
|
|
* |
11
|
|
|
* A single GContact, all members of a group or all contacts can be exported in |
12
|
|
|
* VCard format (v3.0). |
13
|
|
|
* > The group membership(s) of a contact is stored as category(s) in the VCard |
14
|
|
|
* > contact. |
15
|
|
|
* > If the google system groups should be taken into account, this hve to be |
16
|
|
|
* > activated explicitly. |
17
|
|
|
* > The treatment of the system group 'contactGroups/starred', which is used by |
18
|
|
|
* > Google for 'starred' contacts (i.e. for the 'favorites'), can be configured |
19
|
|
|
* > separately. |
20
|
|
|
* |
21
|
|
|
* Since the GContact contains only a reference (the `resourceName`) to the |
22
|
|
|
* group(s) it belongs to, an internal list (`resourceName` => `groupName`) of |
23
|
|
|
* the available groups is loaded once before the export is started.<br/> |
24
|
|
|
* |
25
|
|
|
* @author Stefanius <[email protected]> |
26
|
|
|
* @copyright MIT License - see the LICENSE file for details |
27
|
|
|
*/ |
28
|
|
|
class VCardExport |
29
|
|
|
{ |
30
|
|
|
/** also map system groups to catagories */ |
31
|
|
|
public const OPT_MAP_GROUPS_TO_CATEGORY = 0x0001; |
32
|
|
|
/** also map system groups to catagories */ |
33
|
|
|
public const OPT_MAP_SYSTEM_GROUPS = 0x0002; |
34
|
|
|
/** export contined photo as portrait */ |
35
|
|
|
public const OPT_EXPORT_PHOTO = 0x0004; |
36
|
|
|
/** use default photo from google, if no custom photo is set */ |
37
|
|
|
public const OPT_USE_DEFAULT_PHOTO = 0x0008; |
38
|
|
|
|
39
|
|
|
/** @var GClient the client we need for the import */ |
40
|
|
|
protected GClient $oClient; |
41
|
|
|
/** @var array<string,string> array of available google contact groups groupName -> groupResourcename */ |
42
|
|
|
protected array $aGroupNames = []; |
43
|
|
|
/** @var int options for the import */ |
44
|
|
|
protected int $iOptions = 0; |
45
|
|
|
/** @var string charset for the created file */ |
46
|
|
|
protected string $strCharset = 'UTF-8'; |
47
|
|
|
/** @var string surrogate category name to use for 'starred' contacts |
48
|
|
|
* (-> contacts belonging to the predefined system group 'starred') */ |
49
|
|
|
protected string $strStarredCategory = ''; |
50
|
|
|
/** @var int count of contacts beeing imported */ |
51
|
|
|
protected int $iExportCount = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create an instance of the class. |
55
|
|
|
* @param GClient $oClient |
56
|
|
|
* @param int $iOptions |
57
|
|
|
*/ |
58
|
|
|
public function __construct(GClient &$oClient, int $iOptions = null) |
59
|
|
|
{ |
60
|
|
|
$this->oClient = $oClient; |
61
|
|
|
$this->iOptions = $iOptions ?? self::OPT_EXPORT_PHOTO | self::OPT_MAP_GROUPS_TO_CATEGORY; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create the VCard file containing the specified contact(s). |
66
|
|
|
* If param `$strResourceName` specifies a contact group, all member |
67
|
|
|
* of this group are exported, otherwise the specifeid contact is used. |
68
|
|
|
* If `$strResourceName` is empty, all contacts are exported. |
69
|
|
|
* @param string $strResourceName resourcename of contact or group |
70
|
|
|
* @param string $strFilename |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function exportVCard(string $strResourceName, string $strFilename) : bool |
74
|
|
|
{ |
75
|
|
|
$result = false; |
76
|
|
|
|
77
|
|
|
$oContacts = new GContacts($this->oClient); |
78
|
|
|
$oContacts->addPersonFields(GContacts::DEF_DETAIL_PERSON_FIELDS); |
79
|
|
|
|
80
|
|
|
// load list or contact |
81
|
|
|
$aContactList = false; |
82
|
|
|
if (empty($strResourceName) || strpos($strResourceName, 'contactGroups/') === 0) { |
83
|
|
|
$aContactList = $oContacts->list(GContacts::SO_LAST_NAME_ASCENDING, $strResourceName); |
84
|
|
|
} else { |
85
|
|
|
// load specified contact |
86
|
|
|
$oContact = $oContacts->getContact($strResourceName); |
87
|
|
|
if ($oContact !== false) { |
88
|
|
|
$aContactList = [$oContact->getArrayCopy()]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
$this->iExportCount = 0; |
92
|
|
|
if ($aContactList !== false) { |
93
|
|
|
$result = true; |
94
|
|
|
$oVCard = new VCard(); |
95
|
|
|
VCard::setEncoding($this->strCharset); |
96
|
|
|
|
97
|
|
|
foreach ($aContactList as $aContact) { |
98
|
|
|
$oGContact = GContact::fromArray($aContact); |
99
|
|
|
$oVCContact = new GContactToVCard($this->aGroupNames, $this->iOptions); |
100
|
|
|
|
101
|
|
|
$oVCContact->setStarredCategory($this->strStarredCategory); |
102
|
|
|
if ($oVCContact->loadGContact($oGContact)) { |
103
|
|
|
$oVCard->addContact($oVCContact); |
104
|
|
|
|
105
|
|
|
$this->iExportCount++; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
if ($this->iExportCount > 0) { |
109
|
|
|
// and write to file |
110
|
|
|
$oVCard->write($strFilename, (strlen($strFilename) > 0)); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
return $result; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
|
|
public function getExportCount() : int |
120
|
|
|
{ |
121
|
|
|
return $this->iExportCount; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets the category to use for 'starred' contacts. |
126
|
|
|
* @param string $strStarredCategory |
127
|
|
|
*/ |
128
|
|
|
public function setStarredCategory(string $strStarredCategory) : void |
129
|
|
|
{ |
130
|
|
|
$this->strStarredCategory = $strStarredCategory; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sets the charset to use for exportfile. |
135
|
|
|
* If the generated file is to be imported into another Googler account or e.g. |
136
|
|
|
* with Thunderbird, UTF-8 is the correct character set, MS Outlook expects |
137
|
|
|
* (at least in the German/European version) an ISO-1234 encoded file (otherwise |
138
|
|
|
* umlauts will not be imported correctly) |
139
|
|
|
* @param string $strCharset |
140
|
|
|
*/ |
141
|
|
|
public function setCharset(string $strCharset) : void |
142
|
|
|
{ |
143
|
|
|
$this->strCharset = $strCharset; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Load available contact groups. |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
private function loadContactGroups() : bool |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$bOK = true; |
153
|
|
|
if (($this->iOptions & self::OPT_MAP_GROUPS_TO_CATEGORY) != 0) { |
154
|
|
|
$oGroups = new GContactGroups($this->oClient); |
155
|
|
|
$aGroups = $oGroups->list(GContactGroups::GT_ALL_CONTACT_GROUPS, GContactGroups::RES_LIST); |
156
|
|
|
if ($aGroups !== false) { |
|
|
|
|
157
|
|
|
$this->aGroupResourcenames = $aGroups; |
158
|
|
|
} |
159
|
|
|
$bOK = ($aGroups !== false); |
160
|
|
|
} |
161
|
|
|
return $bOK; |
162
|
|
|
} |
163
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths