Exemplo n.º 1
0
    public static function _transfer($source, $destination, $syncTaskId = 0, $ignoreUpdate = false, $whitelist = array())
    {
        // Stats
        $stats = array('contacts' => 0);
        // Get source data
        $contacts = BackupsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['contact']))->toArray();
        $destinationContacts = BackupsModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['contact']))->column('entity_title');
        $syncedContacts = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'destination_id' => $destination['username']['id'], 'status' => MigratedDataModel::STATUS_ACTIVE, 'kind' => static::$kind['contact']))->column('identifier');
        if ($contacts) {
            foreach ($contacts as $contact) {
                $contactData = json_decode($contact['entity'], true);
                // Whitelisting used for share feature
                $whiteListed = true;
                if ($whitelist) {
                    if (isset($whitelist['contacts']) && $whitelist['contacts']) {
                        if (!in_array($contactData['id'], $whitelist['contacts'])) {
                            $whiteListed = false;
                        }
                    }
                }
                if (!in_array(md5($contactData['name']), $syncedContacts) && $whiteListed) {
                    $body = <<<EOD
<?xml version='1.0' encoding='UTF-8'?>
<entry
\txmlns='http://www.w3.org/2005/Atom'
\txmlns:gd='http://schemas.google.com/g/2005'>

EOD;
                    if (in_array($contactData['name'], $destinationContacts) && !$ignoreUpdate) {
                        $contactData['name'] = $contactData['name'] . ' (2)';
                    } else {
                        $contactData['name'] = $contactData['name'];
                    }
                    $body .= '<title type="text">' . $contactData['name'] . '</title>';
                    // Emails
                    if (isset($contactData['emails']) && $contactData['emails']) {
                        foreach ($contactData['emails'] as $email) {
                            $body .= '<gd:email rel="' . (isset($email['rel']) ? $email['rel'] : 'http://schemas.google.com/g/2005#other') . '"';
                            if (isset($email['primary'])) {
                                $body .= ' primary="true" ';
                            }
                            $body .= ' address="' . $email['address'] . '" />';
                        }
                    }
                    // IM
                    if (isset($contactData['im']) && $contactData['im']) {
                        foreach ($contactData['im'] as $im) {
                            $body .= '<gd:im address="' . $im['address'] . '" protocol="' . $im['protocol'] . '" rel="' . $im['rel'] . '"/>';
                        }
                    }
                    // Numbers
                    if (isset($contactData['phoneNumbers']) && $contactData['phoneNumbers']) {
                        foreach ($contactData['phoneNumbers'] as $number) {
                            $body .= '<gd:phoneNumber rel="' . $number['rel'] . '">' . $number['number'] . '</gd:phoneNumber>';
                        }
                    }
                    // Postal
                    if (isset($contactData['postalAddress']) && $contactData['postalAddress']) {
                        foreach ($contactData['postalAddress'] as $address) {
                            $body .= '<gd:postalAddress rel="' . $address['rel'] . '">' . $address['address'] . '</gd:postalAddress>';
                        }
                    }
                    $body .= '</entry>';
                    $newContact = \Rest::postXML(static::$endpoints['contacts'], $body, $destination);
                    // Update playlist with new id
                    if (!$ignoreUpdate && $newContact) {
                        $newContactId = @basename($newContact->id);
                        if ($newContactId) {
                            $oldPlaylist = BackupsModel::first($contact['id']);
                            $oldPlaylist->entity_new_id = $newContactId;
                            $oldPlaylist->save();
                        }
                    }
                    $stats['contacts']++;
                    $syncedContact = MigratedDataModel::create();
                    $syncedContact->source_id = $source['username']['id'];
                    $syncedContact->destination_id = $destination['username']['id'];
                    $syncedContact->task_id = 0;
                    $syncedContact->sync_task_id = $syncTaskId;
                    $syncedContact->table = BackupsModel::$schema['table'];
                    $syncedContact->table_id = $contact['id'];
                    $syncedContact->kind = static::$kind['contact'];
                    $syncedContact->identifier = md5($contactData['name']);
                    $syncedContact->status = MigratedDataModel::STATUS_ACTIVE;
                    $syncedContact->created = date(DATE_TIME);
                    $syncedContact->save();
                }
            }
        }
        return $stats;
    }