From cbf5c98dac5cef937254cdd4c225b50325436821 Mon Sep 17 00:00:00 2001 From: Mark Crane Date: Mon, 10 Sep 2012 17:51:59 +0000 Subject: [PATCH] Add ability to import an outlook csv file containing contacts. --- app/contacts/contact_import.php | 471 ++++++++++++++++++++++++++++++++ app/contacts/contacts.php | 13 +- 2 files changed, 480 insertions(+), 4 deletions(-) create mode 100644 app/contacts/contact_import.php diff --git a/app/contacts/contact_import.php b/app/contacts/contact_import.php new file mode 100644 index 0000000000..8bee58f72d --- /dev/null +++ b/app/contacts/contact_import.php @@ -0,0 +1,471 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ +include "root.php"; +require_once "includes/require.php"; +require_once "includes/checkauth.php"; +if (permission_exists('contacts_add')) { + //access granted +} +else { + echo "access denied"; + exit; +} + +//built in str_getcsv requires PHP 5.3 or higher, this function can be used to reproduct the functionality but requirs PHP 5.1.0 or higher + if(!function_exists('str_getcsv')) { + function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") { + $fp = fopen("php://memory", 'r+'); + fputs($fp, $input); + rewind($fp); + $data = fgetcsv($fp, null, $delimiter, $enclosure); // $escape only got added in 5.3.0 + fclose($fp); + return $data; + } + } + +//set the max php execution time + ini_set(max_execution_time,7200); + +//get the http get values and set them as php variables + $order_by = check_str($_GET["order_by"]); + $order = check_str($_GET["order"]); + $delimiter = check_str($_GET["data_delimiter"]); + $enclosure = check_str($_GET["data_enclosure"]); + +//upload the contact csv + if (($_POST['submit'] == "Upload") && is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('recordings_upload')) { + //copy the csv file + if (check_str($_POST['type']) == 'csv') { + move_uploaded_file($_FILES['ulfile']['tmp_name'], $_SESSION['server']['temp']['dir'].'/'.$_FILES['ulfile']['name']); + $save_msg = "Uploaded file to ".$_SESSION['server']['temp']['dir']."/". htmlentities($_FILES['ulfile']['name']); + //system('chmod -R 744 '.$_SESSION['server']['temp']['dir'].'*'); + unset($_POST['txtCommand']); + } + //get the contents of the csv file + $handle = @fopen($_SESSION['server']['temp']['dir']."/". $_FILES['ulfile']['name'], "r"); + if ($handle) { + $x = 0; + while (($buffer = fgets($handle, 4096)) !== false) { + if ($x == 0) { + //set the column array + $column_array = str_getcsv($buffer, $delimiter, $enclosure); + } + else { + //format the data + $y = 0; + foreach ($column_array as $column) { + $result = str_getcsv($buffer, $delimiter, $enclosure); + $data[$column] = $result[$y]; + $y++; + } + + //set the variables + $contact_title = $data['Title']; + $contact_name_given = $data['FirstName']; + $contact_name_family = $data['LastName']; + $contact_organization = $data['Company']; + $contact_email = $data['EmailAddress']; + $contact_note = $data['Notes']; + $contact_url = $data['Web Page']; + + //add the 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_given, "; + $sql .= "contact_name_family, "; + //$sql .= "contact_nickname, "; + $sql .= "contact_title, "; + //$sql .= "contact_role, "; + $sql .= "contact_email, "; + $sql .= "contact_url, "; + //$sql .= "contact_time_zone, "; + $sql .= "contact_note "; + $sql .= ")"; + $sql .= "values "; + $sql .= "("; + $sql .= "'".$_SESSION['domain_uuid']."', "; + $sql .= "'$contact_uuid', "; + $sql .= "'$contact_type', "; + $sql .= "'$contact_organization', "; + $sql .= "'$contact_name_given', "; + $sql .= "'$contact_name_family', "; + //$sql .= "'$contact_nickname', "; + $sql .= "'$contact_title', "; + //$sql .= "'$contact_role', "; + $sql .= "'$contact_email', "; + $sql .= "'$contact_url', "; + //$sql .= "'$contact_time_zone', "; + $sql .= "'$contact_note' "; + $sql .= ")"; + $db->exec(check_sql($sql)); + unset($sql); + + //add the contact addresses + $x=0; + if (strlen($data['BusinessStreet']) > 0 && strlen($data['BusinessCity']) > 0 && strlen($data['BusinessState']) > 0) { + $address_array[$x]['address_street'] = $data['BusinessStreet']; + $address_array[$x]['address_locality'] = $data['BusinessCity']; + $address_array[$x]['address_region'] = $data['BusinessState']; + $address_array[$x]['address_postal_code'] = $data['BusinessPostalCode']; + $address_array[$x]['address_country'] = $data['BusinessCountry']; + $address_array[$x]['address_type'] = 'work'; + $x++; + } + if (strlen($data['HomeStreet']) > 0 && strlen($data['HomeCity']) > 0 && strlen($data['HomeState']) > 0) { + $address_array[$x]['address_street'] = $data['HomeStreet']; + $address_array[$x]['address_locality'] = $data['HomeCity']; + $address_array[$x]['address_region'] = $data['HomeState']; + $address_array[$x]['address_postal_code'] = $data['HomePostalCode']; + $address_array[$x]['address_country'] = $data['HomeCountry']; + $address_array[$x]['address_type'] = 'home'; + $x++; + } + if (strlen($data['OtherStreet']) > 0 && strlen($data['OtherCity']) > 0 && strlen($data['OtherState']) > 0) { + $address_array[$x]['address_street'] = $data['OtherStreet']; + $address_array[$x]['address_locality'] = $data['OtherCity']; + $address_array[$x]['address_region'] = $data['OtherState']; + $address_array[$x]['address_postal_code'] = $data['OtherPostalCode']; + $address_array[$x]['address_country'] = $data['OtherCountry']; + $address_array[$x]['address_type'] = 'work'; + } + foreach ($address_array as $row) { + $contact_address_uuid = uuid(); + $sql = "insert into v_contact_addresses "; + $sql .= "("; + $sql .= "domain_uuid, "; + $sql .= "contact_uuid, "; + $sql .= "contact_address_uuid, "; + $sql .= "address_type, "; + $sql .= "address_street, "; + //$sql .= "address_extended, "; + $sql .= "address_locality, "; + $sql .= "address_region, "; + $sql .= "address_postal_code, "; + $sql .= "address_country "; + //$sql .= "address_latitude, "; + //$sql .= "address_longitude "; + $sql .= ")"; + $sql .= "values "; + $sql .= "("; + $sql .= "'".$_SESSION['domain_uuid']."', "; + $sql .= "'$contact_uuid', "; + $sql .= "'$contact_address_uuid', "; + $sql .= "'".$row['address_type']."', "; + $sql .= "'".$row['address_street']."', "; + //$sql .= "'$address_extended', "; + $sql .= "'".$row['address_locality']."', "; + $sql .= "'".$row['address_region']."', "; + $sql .= "'".$row['address_postal_code']."', "; + $sql .= "'".$row['address_country']."' "; + //$sql .= "'$address_latitude', "; + //$sql .= "'$address_longitude' "; + $sql .= ")"; + $db->exec(check_sql($sql)); + unset($sql); + } + + //add the contact phone numbers + $x = 0; + if (strlen($data['BusinessFax']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['BusinessFax']); + $phone_array[$x]['phone_type'] = 'fax'; + $x++; + } + if (strlen($data['BusinessPhone']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['BusinessPhone']); + $phone_array[$x]['phone_type'] = 'work'; + $x++; + } + if (strlen($data['BusinessPhone2']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['BusinessPhone2']); + $phone_array[$x]['phone_type'] = 'work'; + $x++; + } + if (strlen($data['CompanyMainPhone']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['CompanyMainPhone']); + $phone_array[$x]['phone_type'] = 'pref'; + $x++; + } + if (strlen($data['HomeFax']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['HomeFax']); + $phone_array[$x]['phone_type'] = 'fax'; + $x++; + } + if (strlen($data['HomePhone']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['HomePhone']); + $phone_array[$x]['phone_type'] = 'home'; + $x++; + } + if (strlen($data['HomePhone2']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['HomePhone2']); + $phone_array[$x]['phone_type'] = 'home'; + $x++; + } + if (strlen($data['MobilePhone']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['MobilePhone']); + $phone_array[$x]['phone_type'] = 'cell'; + $x++; + } + if (strlen($data['OtherFax']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['OtherFax']); + $phone_array[$x]['phone_type'] = 'fax'; + $x++; + } + if (strlen($data['OtherPhone']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['OtherPhone']); + $phone_array[$x]['phone_type'] = 'home'; + $x++; + } + if (strlen($data['Pager']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['Pager']); + $phone_array[$x]['phone_type'] = 'page'; + $x++; + } + if (strlen($data['PrimaryPhone']) > 0) { + $phone_array[$x]['phone_number'] = preg_replace('{\D}', '', $data['PrimaryPhone']); + $phone_array[$x]['phone_type'] = 'pref'; + $x++; + } + foreach ($phone_array as $row) { + $contact_phone_uuid = uuid(); + $sql = "insert into v_contact_phones "; + $sql .= "("; + $sql .= "domain_uuid, "; + $sql .= "contact_uuid, "; + $sql .= "contact_phone_uuid, "; + $sql .= "phone_type, "; + $sql .= "phone_number "; + $sql .= ")"; + $sql .= "values "; + $sql .= "("; + $sql .= "'$domain_uuid', "; + $sql .= "'$contact_uuid', "; + $sql .= "'$contact_phone_uuid', "; + $sql .= "'".$row['phone_type']."', "; + $sql .= "'".$row['phone_number']."' "; + $sql .= ")"; + $db->exec(check_sql($sql)); + unset($sql); + } + //save the results into an array + $results[] = $data; + } + //increment $x + $x++; + } + if (!feof($handle)) { + echo "Error: Unable to open the file.\n"; + } + fclose($handle); + } + + //show the header + require_once "includes/header.php"; + echo "
\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "
Import Contacts\n"; + echo " \n"; + echo "
\n"; + echo " These contacts were added from the csv file.

\n"; + echo "
\n"; + + //show the results + echo "\n"; + echo "\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "\n"; + foreach($results as $row) { + echo "\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "\n"; + } + echo "
NameOrganizationEmailURL
\n"; + echo $row['FirstName'] ." ".$row['LastName']; + echo " \n"; + echo $row['Company']." \n"; + echo " \n"; + echo $row['EmailAddress']." \n"; + echo " \n"; + echo $row['Web Page']." \n"; + echo "
\n"; + + //include the footer + require_once "includes/footer.php"; + + //end the script + break; + } + +//include the header + require_once "includes/header.php"; + +//begin the content + echo "
"; + echo "\n"; + echo "\n"; + echo "
\n"; + echo "
"; + + echo "\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " Import Contacts
\n"; + echo " Export your contacts from outlook to a comma delimitted csv file. Then use this tool to upload and add the contacts from that file.\n"; + echo "
\n"; + echo " \n"; + echo "
"; + + 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 " Delimiter:\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo "Select the delimiter.\n"; + echo "
\n"; + echo " Enclosure:\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo "Select the enclosure.\n"; + echo "
\n"; + echo " File to upload:\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + //echo "Select the enclosure.\n"; + echo "
\n"; + echo "  \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo "
"; + +//include the footer + require_once "includes/footer.php"; + +/* +[Suffix] +[MiddleName] +[Department] +[JobTitle] +[Account] +[Anniversary] +[AssistantsName] +[BillingInformation] +[Birthday] +[Categories] +[Children] +[DirectoryServer] +[EmailDisplayName] +[Email2Address] +[Email2DisplayName] +[Email3Address] +[Email3DisplayName] +[Gender] +[GovernmentIDNumber] +[Hobby] +[Initials] +[InternetFreeBusy] +[Keywords] +[Language1] +[Location] +[ManagersName] +[Mileage] +[OfficeLocation] +[OrganizationalIDNumber] +[POBox] +[Priority] +[Private] +[Profession] +[ReferredBy] +[Sensitivity] +[Spouse] +[User 1] +[User 2] +[User 3] +[User 4] + +['Callback']; +['CarPhone']; +['ISDN']; +['RadioPhone']; +['TTYTDDPhone']; +['Telex']; +['AssistantsPhone']; +*/ +?> \ No newline at end of file diff --git a/app/contacts/contacts.php b/app/contacts/contacts.php index f7de7da1d5..dc6ee69ad5 100644 --- a/app/contacts/contacts.php +++ b/app/contacts/contacts.php @@ -50,7 +50,7 @@ require_once "includes/paging.php"; echo "
\n"; echo "
"; - echo "\n"; + echo "
\n"; echo " \n"; echo " \n"; + if (permission_exists('contacts_add')) { + echo " \n"; + } echo " \n"; echo "
Contacts
\n"; echo " The contact is a list of individuals and organizations.\n"; @@ -61,6 +61,11 @@ require_once "includes/paging.php"; echo " \n"; echo " \n"; echo "
\n"; + echo " \n"; + echo "
\n"; @@ -114,8 +119,8 @@ require_once "includes/paging.php"; $offset = $rows_per_page * $page; //get the list - $sql = " select * from v_contacts "; - $sql .= " where domain_uuid = '".$_SESSION['domain_uuid']."' "; + $sql = "select * from v_contacts "; + $sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' "; if (strlen($search_all) > 0) { if (is_numeric($search_all)) { $sql .= "and contact_uuid in (select contact_uuid from v_contact_phones where phone_number like '%".$search_all."%') \n"; @@ -140,7 +145,7 @@ require_once "includes/paging.php"; } } if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; } - $sql .= " limit $rows_per_page offset $offset "; + $sql .= "limit $rows_per_page offset $offset "; $prep_statement = $db->prepare(check_sql($sql)); $prep_statement->execute(); $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);