Portions created by the Initial Developer are Copyright (C) 2008-2013 the Initial Developer. All Rights Reserved. Contributor(s): Mark J Crane */ require_once "root.php"; require_once "resources/require.php"; require_once "resources/check_auth.php"; require_once "resources/functions/google_get_groups.php"; require_once "resources/functions/google_get_contacts.php"; if (permission_exists('contact_add')) { //access granted } else { echo "access denied"; exit; } //add multi-lingual support $language = new text; $text = $language->get(); //handle import if ($_POST['a'] == 'import') { if (sizeof($_POST['group_id']) > 0) { //get contact ids for those in the submitted groups if (sizeof($_SESSION['contact_auth']['google']) > 0) { foreach ($_SESSION['contact_auth']['google'] as $contact['id'] => $contact) { foreach ($contact['groups'] as $contact_group['id'] => $meh) { if (in_array($contact_group['id'], $_POST['group_id'])) { $import_ids[] = $contact['id']; } } } } } if (sizeof($_POST['contact_id']) > 0) { foreach ($_POST['contact_id'] as $contact_id) { $import_ids[] = $contact_id; } } //iterate selected contact ids, insert contact into database $contacts_imported = 0; $contacts_skipped = 0; $contacts_replaced = 0; if (sizeof($import_ids) > 0) { $import_ids = array_unique($import_ids); foreach ($import_ids as $contact_id) { //check for duplicate contact (already exists, previously imported, etc) $sql = "select contact_uuid from v_contact_settings "; $sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' "; $sql .= "and contact_setting_category = 'google' "; $sql .= "and contact_setting_subcategory = 'id' "; $sql .= "and contact_setting_value = '".$contact_id."' "; $sql .= "and contact_setting_enabled = 'true' "; $prep_statement = $db->prepare($sql); $prep_statement->execute(); $result = $prep_statement->fetch(PDO::FETCH_ASSOC); if ($result['contact_uuid'] != '') { $duplicate_exists = true; $duplicate_contact_uuid = $result['contact_uuid']; } else { $duplicate_exists = false; } unset($sql, $prep_statement, $result); //skip importing contact if ($duplicate_exists && $_POST['import_duplicates'] == 'skip') { $contacts_skipped++; continue; } //replace contact (delete before inserts below) else if ($duplicate_exists && $_POST['import_duplicates'] == 'replace') { $contact_uuid = $duplicate_contact_uuid; $included = true; require_once "contact_delete.php"; unset($contact_uuid, $duplicate_contact_uuid); $contacts_replaced++; } //extract contact record from array using contact id $contact = $_SESSION['contact_auth']['google'][$contact_id]; //insert contact $contact_uuid = uuid(); $sql = "insert into v_contacts "; $sql .= "( "; $sql .= "domain_uuid, "; $sql .= "contact_uuid, "; $sql .= "contact_type, "; $sql .= "contact_organization, "; $sql .= "contact_name_prefix, "; $sql .= "contact_name_given, "; $sql .= "contact_name_middle, "; $sql .= "contact_name_family, "; $sql .= "contact_name_suffix, "; $sql .= "contact_nickname, "; $sql .= "contact_title, "; $sql .= "contact_category, "; $sql .= "contact_note "; $sql .= ") "; $sql .= "values "; $sql .= "( "; $sql .= "'".$_SESSION['domain_uuid']."', "; $sql .= "'".$contact_uuid."', "; $sql .= "'".check_str($_POST['import_type'])."', "; $sql .= "'".check_str($contact['organization'])."', "; $sql .= "'".check_str($contact['name_prefix'])."', "; $sql .= "'".check_str($contact['name_given'])."', "; $sql .= "'".check_str($contact['name_middle'])."', "; $sql .= "'".check_str($contact['name_family'])."', "; $sql .= "'".check_str($contact['name_suffix'])."', "; $sql .= "'".check_str($contact['nickname'])."', "; $sql .= "'".check_str($contact['title'])."', "; $sql .= "'".check_str($_POST['import_category'])."', "; $sql .= "'".check_str($contact['notes'])."' "; $sql .= ")"; $db->exec(check_sql($sql)); unset($sql); //set sharing if ($_POST['import_shared'] != 'true') { $sql = "insert into v_contact_groups "; $sql .= "( "; $sql .= "contact_group_uuid, "; $sql .= "domain_uuid, "; $sql .= "contact_uuid, "; $sql .= "group_uuid "; $sql .= ") "; $sql .= "values "; $sql .= "( "; $sql .= "'".uuid()."', "; $sql .= "'".$_SESSION['domain_uuid']."', "; $sql .= "'".$contact_uuid."', "; $sql .= "'".$_SESSION["user_uuid"]."' "; $sql .= ")"; $db->exec(check_sql($sql)); unset($sql); } //insert emails if ($_POST['import_fields']['email'] && sizeof($contact['emails']) > 0) { foreach ($contact['emails'] as $contact_email) { $sql = "insert into v_contact_emails "; $sql .= "("; $sql .= "domain_uuid, "; $sql .= "contact_uuid, "; $sql .= "contact_email_uuid, "; $sql .= "email_label, "; $sql .= "email_address, "; $sql .= "email_primary "; $sql .= ") "; $sql .= "values "; $sql .= "("; $sql .= "'".$_SESSION['domain_uuid']."', "; $sql .= "'".$contact_uuid."', "; $sql .= "'".uuid()."', "; $sql .= "'".check_str($contact_email['label'])."', "; $sql .= "'".check_str($contact_email['address'])."', "; $sql .= (($contact_email['primary']) ? 1 : 0)." "; $sql .= ")"; $db->exec(check_sql($sql)); unset($sql); } } //insert numbers if ($_POST['import_fields']['number'] && sizeof($contact['numbers']) > 0) { foreach ($contact['numbers'] as $contact_number) { $sql = "insert into v_contact_phones "; $sql .= "("; $sql .= "domain_uuid, "; $sql .= "contact_uuid, "; $sql .= "contact_phone_uuid, "; $sql .= "phone_type_voice, "; $sql .= "phone_type_fax, "; $sql .= "phone_label, "; $sql .= "phone_number, "; $sql .= "phone_primary "; $sql .= ") "; $sql .= "values "; $sql .= "("; $sql .= "'".$domain_uuid."', "; $sql .= "'".$contact_uuid."', "; $sql .= "'".uuid()."', "; $sql .= ((substr_count(strtoupper($contact_number['label']), strtoupper($text['label-fax'])) == 0) ? 1 : 'null').", "; $sql .= ((substr_count(strtoupper($contact_number['label']), strtoupper($text['label-fax'])) != 0) ? 1 : 'null').", "; $sql .= "'".check_str($contact_number['label'])."', "; $sql .= "'".check_str($contact_number['number'])."', "; $sql .= ((sizeof($contact['numbers']) == 1) ? 1 : 0)." "; $sql .= ")"; $db->exec(check_sql($sql)); unset($sql); } } //insert urls if ($_POST['import_fields']['url'] && sizeof($contact['urls']) > 0) { foreach ($contact['urls'] as $contact_url) { $sql = "insert into v_contact_urls "; $sql .= "("; $sql .= "domain_uuid, "; $sql .= "contact_uuid, "; $sql .= "contact_url_uuid, "; $sql .= "url_label, "; $sql .= "url_address, "; $sql .= "url_primary "; $sql .= ") "; $sql .= "values "; $sql .= "("; $sql .= "'".$_SESSION['domain_uuid']."', "; $sql .= "'".$contact_uuid."', "; $sql .= "'".uuid()."', "; $sql .= "'".check_str($contact_url['label'])."', "; $sql .= "'".check_str($contact_url['url'])."', "; $sql .= ((sizeof($contact['urls']) == 1) ? 1 : 0)." "; $sql .= ")"; $db->exec(check_sql($sql)); unset($sql); } } //insert addresses if ($_POST['import_fields']['address'] && sizeof($contact['addresses']) > 0) { foreach ($contact['addresses'] as $contact_address) { $sql = "insert into v_contact_addresses "; $sql .= "("; $sql .= "domain_uuid, "; $sql .= "contact_uuid, "; $sql .= "contact_address_uuid, "; $sql .= "address_type, "; $sql .= "address_label, "; $sql .= "address_street, "; $sql .= "address_extended, "; $sql .= "address_community, "; $sql .= "address_locality, "; $sql .= "address_region, "; $sql .= "address_postal_code, "; $sql .= "address_country, "; $sql .= "address_primary "; $sql .= ") "; $sql .= "values "; $sql .= "("; $sql .= "'".$_SESSION['domain_uuid']."', "; $sql .= "'".$contact_uuid."', "; $sql .= "'".uuid()."', "; if (substr_count(strtoupper($contact_address['label']), strtoupper($text['option-home'])) != 0) { $sql .= "'home', "; // vcard address type } else if (substr_count(strtoupper($contact_address['label']), strtoupper($text['option-work'])) != 0) { $sql .= "'work', "; // vcard address type } else { $sql .= "'', "; } $sql .= "'".check_str($contact_address['label'])."', "; $sql .= "'".check_str($contact_address['street'])."', "; $sql .= "'".check_str($contact_address['extended'])."', "; $sql .= "'".check_str($contact_address['community'])."', "; $sql .= "'".check_str($contact_address['locality'])."', "; $sql .= "'".check_str($contact_address['region'])."', "; $sql .= "'".check_str($contact_address['postal_code'])."', "; $sql .= "'".check_str($contact_address['country'])."', "; $sql .= ((sizeof($contact['addresses']) == 1) ? 1 : 0)." "; $sql .= ")"; $db->exec(check_sql($sql)); unset($sql); } } //add google contact id, etag and updated date to contact settings $contact['updated'] = str_replace('T', ' ', $contact['updated']); $contact['updated'] = str_replace('Z', '', $contact['updated']); $sql = "insert into v_contact_settings "; $sql .= "("; $sql .= "contact_setting_uuid, "; $sql .= "contact_uuid, "; $sql .= "domain_uuid, "; $sql .= "contact_setting_category, "; $sql .= "contact_setting_subcategory, "; $sql .= "contact_setting_name, "; $sql .= "contact_setting_value, "; $sql .= "contact_setting_order, "; $sql .= "contact_setting_enabled "; $sql .= ") "; $sql .= "values "; $sql .= "('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'sync', 'source', 'array', 'google', 0, 'true' )"; $sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'id', 'text', '".check_str($contact_id)."', 0, 'true' )"; $sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'updated', 'date', '".check_str($contact['updated'])."', 0, 'true' )"; $sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'etag', 'text', '".check_str($contact['etag'])."', 0, 'true' )"; $db->exec(check_sql($sql)); unset($sql); $contacts_imported++; } $message = $text['message-contacts_imported']." ".$contacts_imported; if ($contacts_replaced > 0) { $message .= " (".$text['message_contacts_imported_replaced']." ".$contacts_replaced.")"; } if ($contacts_skipped > 0) { $message .= ", ".$text['message_contacts_imported_skipped']." ".$contacts_skipped; } $_SESSION["message"] = $message; header("Location: contacts.php"); exit; } else { // no contacts imported messages::add($text['message-contacts_imported']." ".$contacts_imported, 'negative'); } } //******************************************************************************************* //check if authenticated if ($_SESSION['contact_auth']['token'] == '') { $_SESSION['contact_auth']['referer'] = substr($_SERVER["HTTP_REFERER"], strrpos($_SERVER["HTTP_REFERER"],'/')+1); header("Location: contact_auth.php?source=google&target=".substr($_SERVER["PHP_SELF"], strrpos($_SERVER["PHP_SELF"],'/')+1)); exit; } unset($_SESSION['contact_auth']['source'], $_SESSION['contact_auth']['target']); //get groups & contacts $groups = google_get_groups($_SESSION['contact_auth']['token']); $contacts = google_get_contacts($_SESSION['contact_auth']['token'], 1000); //store in session variable for use on import $_SESSION['contact_auth']['google'] = $contacts; //include the header $document['title'] = $text['title-contacts_import_google']; require_once "resources/header.php"; echo ""; echo " "; echo " "; echo " "; echo " "; echo " "; echo " "; echo "
"; echo " "; echo " "; echo " "; echo "
".$_SESSION['contact_auth']['name']." (".$_SESSION['contact_auth']['email'].")"."
"; echo "".$text['header-contacts_import_google'].""; echo "

"; echo $text['description-contacts_import_google']; echo "


"; $row_style["0"] = "row_style0"; $row_style["1"] = "row_style1"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo " ".$text['label-import_fields']."\n"; echo "\n"; echo "  ".$text['label-contact_name']." \n"; echo "  ".$text['label-contact_organization']." \n"; echo "  \n"; echo "  \n"; echo "  \n"; echo " \n"; echo "
\n"; echo $text['description-import_fields']."\n"; echo "
\n"; echo " ".$text['label-contact_type']."\n"; echo "\n"; if (is_array($_SESSION["contact"]["type"])) { sort($_SESSION["contact"]["type"]); echo " \n"; } else { echo " \n"; } echo "
\n"; echo $text['description-contact_type_import']."\n"; echo "
\n"; echo " ".$text['label-contact_category']."\n"; echo "\n"; if (is_array($_SESSION["contact"]["category"])) { sort($_SESSION["contact"]["category"]); echo " \n"; } else { echo " \n"; } echo "
\n"; echo $text['description-contact_category_import']."\n"; echo "
\n"; echo " ".$text['label-shared']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-shared_import']."\n"; echo "
\n"; echo " ".$text['label-import_duplicates']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-import_duplicates']."\n"; echo "
"; echo "

"; //display groups echo "".$text['label-groups'].""; echo "

"; echo "\n"; echo "\n"; echo " "; echo " \n"; echo "\n"; //determine contact count in groups foreach ($contacts as $contact) { foreach ($contact['groups'] as $group_id => $meh) { $groups[$group_id]['count']++; } } $c = 0; foreach ($groups as $group['id'] => $group) { if ($group['count'] > 0) { echo "\n"; echo " \n"; echo " \n"; echo "\n"; $c=($c)?0:1; } } echo "
 ".$text['label-contact_name']."
".$group['name']." (".$group['count'].")
\n"; echo "
"; echo "
"; echo "
"; //display contacts echo "".$text['header-contacts'].""; echo "

"; echo "\n"; echo "\n"; echo " "; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "\n"; $c = 0; foreach ($contacts as $contact['id'] => $contact) { $contact_ids[] = $contact['id']; echo "\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "\n"; $c=($c)?0:1; } echo "
".$text['label-contact_name']."".$text['label-contact_organization']."".$text['label-contact_email']."".$text['label-phone_number']."".$text['label-contact_url']."".$text['label-address_address']."".$text['label-group']."
"; $contact_name[] = $contact['name_prefix']; $contact_name[] = $contact['name_given']; $contact_name[] = $contact['name_middle']; $contact_name[] = $contact['name_family']; $contact_name[] = $contact['name_suffix']; echo " ".implode(' ', $contact_name)." "; unset($contact_name); echo " "; echo " ".(($contact['title']) ? $contact['title']."
" : null).$contact['organization']." "; echo "
"; if (sizeof($contact['emails']) > 0) { foreach ($contact['emails'] as $contact_email) { $contact_emails[] = "".$contact_email['label'].": ".$contact_email['address'].""; } echo implode('
', $contact_emails); unset($contact_emails); } else { echo " "; } echo "
"; if (sizeof($contact['numbers']) > 0) { foreach ($contact['numbers'] as $contact_number) { $contact_number_part = "".$contact_number['label'].": "; if (substr_count(strtoupper($contact_number['label']), 'FAX') == 0) { $contact_number_part .= ""; } $contact_number_part .= format_phone($contact_number['number']); if (substr_count(strtoupper($contact_number['label']), 'FAX') == 0) { $contact_number_part .= ""; } $contact_numbers[] = $contact_number_part; unset($contact_number_part); } echo implode('
', $contact_numbers); unset($contact_numbers); } else { echo " "; } echo "
"; if (sizeof($contact['urls']) > 0) { foreach ($contact['urls'] as $contact_url) { $contact_urls[] = "".$contact_url['label'].": ".str_replace("http://", "", str_replace("https://", "", $contact_url['url'])).""; } echo implode('
', $contact_urls); unset($contact_urls); } else { echo " "; } echo "
"; if (sizeof($contact['addresses']) > 0) { foreach ($contact['addresses'] as $contact_address) { if ($contact_address['street'] != '') { $contact_address_parts[] = $contact_address['street']; } if ($contact_address['extended'] != '') { $contact_address_parts[] = $contact_address['extended']; } if ($contact_address['community'] != '') { $contact_address_parts[] = $contact_address['community']; } if ($contact_address['locality'] != '') { $contact_address_parts[] = $contact_address['locality']; } if ($contact_address['region'] != '') { $contact_address_parts[] = $contact_address['region']; } if ($contact_address['postal_code'] != '') { $contact_address_parts[] = $contact_address['postal_code']; } if ($contact_address['country'] != '') { $contact_address_parts[] = $contact_address['country']; } $contact_addresses[] = "".$contact_address['label'].": ".implode(', ', $contact_address_parts); unset($contact_address_parts); } echo implode('
', $contact_addresses); unset($contact_addresses); } else { echo " "; } echo "
"; foreach ($contact['groups'] as $contact_group['id'] => $contact_group['name']) { $contact_groups[] = $contact_group['name']; } echo " ".implode('
', $contact_groups); unset($contact_groups); echo "
\n"; echo "
"; echo "
"; echo "
"; echo "

"; // check or uncheck all contact checkboxes if (sizeof($contact_ids) > 0) { echo "\n"; } /* echo "
";
print_r($contacts);
echo "
"; echo "

"; echo "
"; echo "

SOURCE JSON DECODED ARRAY...

";
print_r($records);
echo "
"; */ //include the footer require_once "resources/footer.php"; // used above function curl_file_get_contents($url) { $curl = curl_init(); $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; curl_setopt($curl, CURLOPT_URL, $url); //The URL to fetch. This can also be set when initializing a session with curl_init(). curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); //The number of seconds to wait while trying to connect. curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header. curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect. curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //To stop cURL from verifying the peer's certificate. $contents = curl_exec($curl); curl_close($curl); return $contents; } ?>