| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | if (!defined('DEBUG_MODE')) { die(); } |
| 10: | |
| 11: | require APP_PATH.'modules/gmail_contacts/hm-gmail-contacts.php'; |
| 12: | require_once APP_PATH.'modules/imap/hm-imap.php'; |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class Hm_Handler_load_gmail_contacts extends Hm_Handler_Module { |
| 18: | public function process() { |
| 19: | $contacts = $this->get('contact_store'); |
| 20: | if ($this->module_is_supported('imap')) { |
| 21: | $settings = $this->get('user_settings', array()); |
| 22: | $max_google_contacts_number = DEFAULT_MAX_GOOGLE_CONTACTS_NUMBER; |
| 23: | |
| 24: | if (array_key_exists('max_google_contacts_number', $settings)) { |
| 25: | $max_google_contacts_number = $settings['max_google_contacts_number']; |
| 26: | } |
| 27: | |
| 28: | $contacts = fetch_gmail_contacts($this->config, $contacts, $this->session, $max_google_contacts_number); |
| 29: | } |
| 30: | $this->append('contact_sources', 'gmail'); |
| 31: | $this->out('contact_store', $contacts, false); |
| 32: | } |
| 33: | } |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | if (!hm_exists('gmail_contacts_request')) { |
| 39: | function gmail_contacts_request($token, $url) { |
| 40: | $headers = array('Authorization: OAuth '.$token, 'GData-Version: 3.0'); |
| 41: | $api = new Hm_API_Curl('xml'); |
| 42: | return $api->command($url, $headers); |
| 43: | }} |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | if (!hm_exists('parse_contact_xml')) { |
| 49: | function parse_contact_xml($xml, $source) { |
| 50: | $parser = new Hm_Gmail_Contact_XML($xml); |
| 51: | $results = array(); |
| 52: | $exists = array(); |
| 53: | foreach ($parser->parse() as $contact) { |
| 54: | if (!array_key_exists('email_address', $contact)) { |
| 55: | continue; |
| 56: | } |
| 57: | if (in_array($contact['email_address'], $exists, true)) { |
| 58: | continue; |
| 59: | } |
| 60: | if (!array_key_exists('display_name', $contact)) { |
| 61: | $contact['display_name'] = ''; |
| 62: | } |
| 63: | $exists[] = $contact['email_address']; |
| 64: | $contact['source'] = $source; |
| 65: | $contact['type'] = 'gmail'; |
| 66: | $results[] = $contact; |
| 67: | } |
| 68: | return $results; |
| 69: | }} |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | if (!hm_exists('fetch_gmail_contacts')) { |
| 75: | function fetch_gmail_contacts($config, $contact_store, $session=false, $max_google_contacts_number = 500) { |
| 76: | if ($session && $session->get('gmail_contacts') && is_array($session->get('gmail_contacts')) && count($session->get('gmail_contacts')) > 0) { |
| 77: | $contact_store->import($session->get('gmail_contacts')); |
| 78: | return $contact_store; |
| 79: | } |
| 80: | $all_contacts = array(); |
| 81: | foreach(Hm_IMAP_List::dump(false, true) as $id => $server) { |
| 82: | if ($server['server'] == 'imap.gmail.com' && array_key_exists('auth', $server) && $server['auth'] == 'xoauth2') { |
| 83: | $results = imap_refresh_oauth2_token($server, $config); |
| 84: | if (!empty($results)) { |
| 85: | if (Hm_IMAP_List::update_oauth2_token($id, $results[1], $results[0])) { |
| 86: | Hm_Debug::add(sprintf('Oauth2 token refreshed for IMAP server id %s', $id), 'info'); |
| 87: | $server = Hm_IMAP_List::dump($id, true); |
| 88: | } |
| 89: | } |
| 90: | |
| 91: | $url = 'https://www.google.com/m8/feeds/contacts/' . $server['user'] . '/full?max-results='. $max_google_contacts_number; |
| 92: | $contacts = parse_contact_xml(gmail_contacts_request($server['pass'], $url), $server['name']); |
| 93: | if (count($contacts) > 0) { |
| 94: | $contact_store->import($contacts); |
| 95: | $all_contacts = array_merge($all_contacts, $contacts); |
| 96: | } |
| 97: | } |
| 98: | if ($session && count($all_contacts) > 0) { |
| 99: | $session->set('gmail_contacts', $all_contacts); |
| 100: | } |
| 101: | } |
| 102: | return $contact_store; |
| 103: | }} |
| 104: | |