Issues (23)

ExportVCard.php (3 issues)

1
<?php
2
declare(strict_types=1);
3
4
require_once 'autoloader.php';
5
require_once 'displayApiError.php';
6
7
use SKien\Google\GClient;
8
use SKien\Google\GContact;
9
use SKien\Google\GContactGroups;
10
use SKien\Google\GContactToVCard;
11
use SKien\Google\GContacts;
12
use SKien\Google\GSecrets;
13
use SKien\VCard\VCard;
0 ignored issues
show
The type SKien\VCard\VCard was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
16
 * This example is only intended to demonstrate the use of the package. The UI
17
 * is only coded 'quick and dirty', contains no validations and should only be
18
 * used as a starting point for your own implementation.
19
 *
20
 * @author Stefanius <[email protected]>
21
 * @copyright MIT License - see the LICENSE file for details
22
 */
23
24
$oSecrets = new GSecrets(-1, GSecrets::TOKEN_FILE);
25
$oClient = new GClient();
26
$oClient->setAccessToken($oSecrets->getAccessToken());
27
if ($oClient->isAccessTokenExpired()) {
28
    // try to refresh the accesstoken
29
    $strRefreshToken = $oSecrets->getRefreshToken();
30
    if (empty($strRefreshToken)) {
31
        // no refresh token available - redirect to google login
32
        header('Location: ./GoogleLogin.php');
33
        exit;
34
    }
35
    $oClient->setOAuthClient($oSecrets->getClientSecrets());
36
    $oSecrets->saveAccessToken($oClient->refreshAccessToken($strRefreshToken));
37
}
38
39
$oContacts = new GContacts($oClient);
40
$oContacts->addPersonFields(GContacts::DEF_DETAIL_PERSON_FIELDS);
41
$strResourceName = rawurldecode($_GET['res'] ?? '');
42
43
$oGroups = new GContactGroups($oClient);
44
$aGroups = $oGroups->list(GContactGroups::GT_USER_CONTACT_GROUPS, GContactGroups::RES_LIST);
45
46
$oVCard = new VCard();
47
48
if (empty($strResourceName)) {
49
    // no resource specified - export all contacts
50
    $aContactList = $oContacts->list(GContacts::SO_LAST_NAME_ASCENDING);
51
    if ($aContactList === false) {
0 ignored issues
show
The condition $aContactList === false is always true.
Loading history...
52
        displayApiError(
53
            'list contacts',
54
            'group: ',
55
            $oClient->getLastResponseCode(),
56
            $oClient->getLastError(),
57
            $oClient->getLastStatus()
58
            );
59
        exit;
60
    }
61
    foreach ($aContactList as $aContact) {
62
        $oContact = GContact::fromArray($aContact);
63
        $oVContact = GContactToVCard::fromGContact($oContact);
64
65
        if ($aGroups !== false) {
66
            foreach ($oContact['memberships'] as $aMembership) {
67
                if (isset($aMembership['contactGroupMembership'])) {
68
                    $strResourceName = $aMembership['contactGroupMembership']['contactGroupResourceName'] ?? '';
69
                    if (isset($aGroups[$strResourceName])) {
70
                        $oVContact->addCategory($aGroups[$strResourceName]);
71
                    }
72
                }
73
            }
74
        }
75
76
        $oVCard->addContact($oVContact);
77
    }
78
} else {
79
    $oContact = $oContacts->getContact($strResourceName);
80
    if ($oContact === false) {
81
        displayApiError(
82
            'reading contact',
83
            $strResourceName,
84
            $oClient->getLastResponseCode(),
85
            $oClient->getLastError(),
86
            $oClient->getLastStatus()
87
            );
88
        exit;
89
    }
90
    $oVContact = GContactToVCard::fromGContact($oContact);
91
92
    if ($aGroups !== false) {
0 ignored issues
show
The condition $aGroups !== false is always false.
Loading history...
93
        foreach ($oContact['memberships'] as $aMembership) {
94
            if (isset($aMembership['contactGroupMembership'])) {
95
                $strResourceName = $aMembership['contactGroupMembership']['contactGroupResourceName'] ?? '';
96
                if (isset($aGroups[$strResourceName])) {
97
                    $oVContact->addCategory($aGroups[$strResourceName]);
98
                }
99
            }
100
        }
101
    }
102
103
    $oVCard->addContact($oVContact);
104
}
105
106
// and write to file
107
$oVCard->write('gcontacts.vcf', false);