function preferences_list($params)
 {
     $rcmail = rcmail::get_instance();
     if ($params['section'] == 'addressbook') {
         $params['blocks'][$this->id]['name'] = $this->abook_name;
         $field_id = 'rc_use_plugin';
         $checkbox = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
         $params['blocks'][$this->id]['options'][$field_id] = array('title' => html::label($field_id, $this->gettext('use') . $this->abook_name), 'content' => $checkbox->show($rcmail->config->get(google_func::$settings_key_use_plugin)));
         $field_id = 'rc_google_autosync';
         $checkbox = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
         $params['blocks'][$this->id]['options'][$field_id] = array('title' => html::label($field_id, $this->gettext('autosync')), 'content' => $checkbox->show($rcmail->config->get(google_func::$settings_key_auto_sync)));
         $field_id = 'rc_google_authcode';
         $input_auth = new html_inputfield(array('name' => $field_id, 'id' => $field_id, 'size' => 45));
         $params['blocks'][$this->id]['options'][$field_id] = array('title' => html::label($field_id, $this->gettext('authcode')), 'content' => $input_auth->show($rcmail->config->get(google_func::$settings_key_auth_code)));
         $params['blocks'][$this->id]['options']['link'] = array('title' => html::span('', ''), 'content' => html::a(array('href' => google_func::get_client()->createAuthUrl(), 'target' => '_blank'), $this->gettext('authcodelink')));
     }
     return $params;
 }
Ejemplo n.º 2
0
 static function google_sync_contacts($user)
 {
     $rcmail = rcmail::get_instance();
     $client = google_func::get_client();
     $auth_res = google_func::google_authenticate($client, $user);
     if (!$auth_res['success']) {
         return $auth_res;
     }
     $feed = 'https://www.google.com/m8/feeds/groups/default/full' . '?v=3.0';
     $val = $client->getAuth()->authenticatedRequest(new Google_Http_Request($feed));
     if ($val->getResponseHttpCode() == 401) {
         return array('success' => false, 'message' => "Authentication failed.");
     } else {
         if ($val->getResponseHttpCode() == 403) {
             return array('success' => false, 'message' => $rcmail->gettext('googleforbidden', 'google_addressbook'));
         } else {
             if ($val->getResponseHttpCode() != 200) {
                 return array('success' => false, 'message' => $rcmail->gettext('googleunreachable', 'google_addressbook'));
             }
         }
     }
     $xml = xml_utils::xmlstr_to_array($val->getResponseBody());
     $gid = $xml['entry'][0]['id'][0]['@text'];
     $feed = 'https://www.google.com/m8/feeds/contacts/default/full' . '?max-results=9999' . '&v=3.0' . '&group=' . urlencode($gid);
     $val = $client->getAuth()->authenticatedRequest(new Google_Http_Request($feed));
     if ($val->getResponseHttpCode() == 401) {
         return array('success' => false, 'message' => "Authentication failed.");
     } else {
         if ($val->getResponseHttpCode() == 403) {
             return array('success' => false, 'message' => $rcmail->gettext('googleforbidden', 'google_addressbook'));
         } else {
             if ($val->getResponseHttpCode() != 200) {
                 return array('success' => false, 'message' => $rcmail->gettext('googleunreachable', 'google_addressbook'));
             }
         }
     }
     $xml = xml_utils::xmlstr_to_array($val->getResponseBody());
     $backend = new google_addressbook_backend($rcmail->get_dbh(), $user->ID);
     $backend->delete_all();
     $num_entries = 0;
     foreach ($xml['entry'] as $entry) {
         //write_log('google_addressbook', 'getting contact: '.$entry['title'][0]['@text']);
         $record = array();
         $name = $entry['gd:name'][0];
         $record['name'] = trim($name['gd:fullName'][0]['@text']);
         $record['firstname'] = trim($name['gd:givenName'][0]['@text']);
         $record['surname'] = trim($name['gd:familyName'][0]['@text']);
         $record['middlename'] = trim($name['gd:additionalName'][0]['@text']);
         $record['prefix'] = $name['gd:namePrefix'][0]['@text'];
         $record['suffix'] = $name['gd:nameSuffix'][0]['@text'];
         if (empty($record['name'])) {
             $record['name'] = $entry['title'][0]['@text'];
         }
         if (empty($record['name']) && empty($record['firstname']) && empty($record['surname']) && empty($record['middlename']) && !array_key_exists('gd:email', $entry)) {
             continue;
         }
         if (array_key_exists('gd:email', $entry)) {
             foreach ($entry['gd:email'] as $email) {
                 list($rel, $type) = explode('#', $email['@attributes']['rel'], 2);
                 $type = empty($type) ? '' : ':' . $type;
                 $record['email' . $type][] = $email['@attributes']['address'];
             }
         }
         if (array_key_exists('gd:phoneNumber', $entry)) {
             foreach ($entry['gd:phoneNumber'] as $phone) {
                 list($rel, $type) = explode('#', $phone['@attributes']['rel'], 2);
                 $type = empty($type) ? '' : ':' . $type;
                 $record['phone' . $type][] = $phone['@text'];
             }
         }
         if (array_key_exists('link', $entry)) {
             foreach ($entry['link'] as $link) {
                 $rel = $link['@attributes']['rel'];
                 $href = $link['@attributes']['href'];
                 if ($rel == 'http://schemas.google.com/contacts/2008/rel#photo') {
                     // etag is only set if image is available
                     if (isset($link['@attributes']['etag'])) {
                         $resp = $client->getAuth()->authenticatedRequest(new Google_Http_Request($href));
                         if ($resp->getResponseHttpCode() == 200) {
                             $record['photo'] = $resp->getResponseBody();
                         }
                     }
                     break;
                 }
             }
         }
         $num_entries++;
         $backend->insert($record, false);
     }
     return array('success' => true, 'message' => $num_entries . $rcmail->gettext('contactsfound', 'google_addressbook'));
 }