contact_import_google.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2020
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes
  22. require_once "root.php";
  23. require_once "resources/require.php";
  24. require_once "resources/check_auth.php";
  25. require_once "resources/functions/google_get_groups.php";
  26. require_once "resources/functions/google_get_contacts.php";
  27. //check permissions
  28. if (permission_exists('contact_add')) {
  29. //access granted
  30. }
  31. else {
  32. echo "access denied";
  33. exit;
  34. }
  35. //add multi-lingual support
  36. $language = new text;
  37. $text = $language->get();
  38. //handle import
  39. if ($_POST['a'] == 'import') {
  40. if (sizeof($_POST['group_id']) > 0) {
  41. //get contact ids for those in the submitted groups
  42. if (sizeof($_SESSION['contact_auth']['google']) > 0) {
  43. foreach ($_SESSION['contact_auth']['google'] as $contact['id'] => $contact) {
  44. foreach ($contact['groups'] as $contact_group['id'] => $meh) {
  45. if (in_array($contact_group['id'], $_POST['group_id'])) {
  46. $import_ids[] = $contact['id'];
  47. }
  48. }
  49. }
  50. }
  51. }
  52. if (sizeof($_POST['contact_id']) > 0) {
  53. foreach ($_POST['contact_id'] as $contact_id) {
  54. $import_ids[] = $contact_id;
  55. }
  56. }
  57. //iterate selected contact ids, insert contact into database
  58. $contacts_imported = 0;
  59. $contacts_skipped = 0;
  60. $contacts_replaced = 0;
  61. if (sizeof($import_ids) > 0) {
  62. $import_ids = array_unique($import_ids);
  63. foreach ($import_ids as $index_1 => $contact_id) {
  64. //check for duplicate contact (already exists, previously imported, etc)
  65. $sql = "select contact_uuid from v_contact_settings ";
  66. $sql .= "where domain_uuid = :domain_uuid ";
  67. $sql .= "and contact_setting_category = 'google' ";
  68. $sql .= "and contact_setting_subcategory = 'id' ";
  69. $sql .= "and contact_setting_value = :contact_setting_value ";
  70. $sql .= "and contact_setting_enabled = 'true' ";
  71. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  72. $parameters['contact_setting_value'] = $contact_id;
  73. $database = new database;
  74. $result = $database->select($sql, $parameters, 'row');
  75. if (is_uuid($result['contact_uuid'])) {
  76. $duplicate_exists = true;
  77. $duplicate_contact_uuid = $result['contact_uuid'];
  78. }
  79. else {
  80. $duplicate_exists = false;
  81. }
  82. unset($sql, $parameters, $result);
  83. //skip importing contact
  84. if ($duplicate_exists && $_POST['import_duplicates'] == 'skip') {
  85. $contacts_skipped++;
  86. continue;
  87. }
  88. //replace contact (delete before inserts below)
  89. else if ($duplicate_exists && $_POST['import_duplicates'] == 'replace') {
  90. //build array
  91. $array[0]['checked'] = 'true';
  92. $array[0]['uuid'] = $duplicate_contact_uuid;
  93. unset($duplicate_contact_uuid);
  94. //grant temporary permissions
  95. $p = new permissions;
  96. $p->add('contact_delete', 'temp');
  97. //delete duplicate contact
  98. $obj = new contacts;
  99. $obj->delete($array);
  100. unset($array);
  101. //revoke temporary permissions
  102. $p->delete('contact_delete', 'temp');
  103. //increase counter
  104. $contacts_replaced++;
  105. }
  106. //extract contact record from array using contact id
  107. $contact = $_SESSION['contact_auth']['google'][$contact_id];
  108. //insert contact
  109. $contact_uuid = uuid();
  110. $array['contacts'][$index_1]['domain_uuid'] = $_SESSION['domain_uuid'];
  111. $array['contacts'][$index_1]['contact_uuid'] = $contact_uuid;
  112. $array['contacts'][$index_1]['contact_type'] = $_POST['import_type'];
  113. $array['contacts'][$index_1]['contact_organization'] = $contact['organization'];
  114. $array['contacts'][$index_1]['contact_name_prefix'] = $contact['name_prefix'];
  115. $array['contacts'][$index_1]['contact_name_given'] = $contact['name_given'];
  116. $array['contacts'][$index_1]['contact_name_middle'] = $contact['name_middle'];
  117. $array['contacts'][$index_1]['contact_name_family'] = $contact['name_family'];
  118. $array['contacts'][$index_1]['contact_name_suffix'] = $contact['name_suffix'];
  119. $array['contacts'][$index_1]['contact_nickname'] = $contact['nickname'];
  120. $array['contacts'][$index_1]['contact_title'] = $contact['title'];
  121. $array['contacts'][$index_1]['contact_category'] = $_POST['import_category'];
  122. $array['contacts'][$index_1]['contact_note'] = $contact['notes'];
  123. //set sharing
  124. if ($_POST['import_shared'] != 'true') {
  125. $contact_group_uuid = uuid();
  126. $array['contact_groups'][$index_1]['contact_group_uuid'] = $contact_group_uuid;
  127. $array['contact_groups'][$index_1]['domain_uuid'] = $_SESSION['domain_uuid'];
  128. $array['contact_groups'][$index_1]['contact_uuid'] = $contact_uuid;
  129. $array['contact_groups'][$index_1]['group_uuid'] = $_SESSION["user_uuid"];
  130. }
  131. //insert emails
  132. if ($_POST['import_fields']['email'] && is_array($contact['emails']) && @sizeof($contact['emails']) != 0) {
  133. foreach ($contact['emails'] as $index_2 => $contact_email) {
  134. $contact_email_uuid = uuid();
  135. $array['contact_emails'][$index_2]['domain_uuid'] = $_SESSION['domain_uuid'];
  136. $array['contact_emails'][$index_2]['contact_uuid'] = $contact_uuid;
  137. $array['contact_emails'][$index_2]['contact_email_uuid'] = $contact_email_uuid;
  138. $array['contact_emails'][$index_2]['email_label'] = $contact_email['label'];
  139. $array['contact_emails'][$index_2]['email_address'] = $contact_email['address'];
  140. $array['contact_emails'][$index_2]['email_primary'] = $contact_email['primary'] ? 1 : 0;
  141. }
  142. }
  143. //insert numbers
  144. if ($_POST['import_fields']['number'] && is_array($contact['numbers']) && @sizeof($contact['numbers']) != 0) {
  145. foreach ($contact['numbers'] as $index_3 => $contact_number) {
  146. $contact_phone_uuid = uuid();
  147. $array['contact_phones'][$index_3]['domain_uuid'] = $domain_uuid;
  148. $array['contact_phones'][$index_3]['contact_uuid'] = $contact_uuid;
  149. $array['contact_phones'][$index_3]['contact_phone_uuid'] = $contact_phone_uuid;
  150. $array['contact_phones'][$index_3]['phone_type_voice'] = substr_count(strtoupper($contact_number['label']), strtoupper($text['label-fax'])) == 0 ? 1 : null;
  151. $array['contact_phones'][$index_3]['phone_type_fax'] = substr_count(strtoupper($contact_number['label']), strtoupper($text['label-fax'])) != 0 ? 1 : null;
  152. $array['contact_phones'][$index_3]['phone_label'] = $contact_number['label'];
  153. $array['contact_phones'][$index_3]['phone_number'] = $contact_number['number'];
  154. $array['contact_phones'][$index_3]['phone_primary'] = @sizeof($contact['numbers']) == 1 ? 1 : 0;
  155. }
  156. }
  157. //insert urls
  158. if ($_POST['import_fields']['url'] && is_array($contact['urls']) && @sizeof($contact['urls']) != 0) {
  159. foreach ($contact['urls'] as $index_4 => $contact_url) {
  160. $contact_url_uuid = uuid();
  161. $array['contact_urls'][$index_4]['domain_uuid'] = $_SESSION['domain_uuid'];
  162. $array['contact_urls'][$index_4]['contact_uuid'] = $contact_uuid;
  163. $array['contact_urls'][$index_4]['contact_url_uuid'] = $contact_url_uuid;
  164. $array['contact_urls'][$index_4]['url_label'] = $contact_url['label'];
  165. $array['contact_urls'][$index_4]['url_address'] = $contact_url['url'];
  166. $array['contact_urls'][$index_4]['url_primary'] = @sizeof($contact['urls']) == 1 ? 1 : 0;
  167. }
  168. }
  169. //insert addresses
  170. if ($_POST['import_fields']['address'] && is_array($contact['addresses']) && @sizeof($contact['addresses']) != 0) {
  171. foreach ($contact['addresses'] as $index_5 => $contact_address) {
  172. $contact_address_uuid = uuid();
  173. $array['contact_addresses'][$index_5]['domain_uuid'] = $_SESSION['domain_uuid'];
  174. $array['contact_addresses'][$index_5]['contact_uuid'] = $contact_uuid;
  175. $array['contact_addresses'][$index_5]['contact_address_uuid'] = $contact_address_uuid;
  176. if (substr_count(strtoupper($contact_address['label']), strtoupper($text['option-home'])) != 0) {
  177. $array['contact_addresses'][$index_5]['address_type'] = 'home';
  178. }
  179. else if (substr_count(strtoupper($contact_address['label']), strtoupper($text['option-work'])) != 0) {
  180. $array['contact_addresses'][$index_5]['address_type'] = 'work';
  181. }
  182. else {
  183. $array['contact_addresses'][$index_5]['address_type'] = null;
  184. }
  185. $array['contact_addresses'][$index_5]['address_label'] = $contact_address['label'];
  186. $array['contact_addresses'][$index_5]['address_street'] = $contact_address['street'];
  187. $array['contact_addresses'][$index_5]['address_extended'] = $contact_address['extended'];
  188. $array['contact_addresses'][$index_5]['address_community'] = $contact_address['community'];
  189. $array['contact_addresses'][$index_5]['address_locality'] = $contact_address['locality'];
  190. $array['contact_addresses'][$index_5]['address_region'] = $contact_address['region'];
  191. $array['contact_addresses'][$index_5]['address_postal_code'] = $contact_address['postal_code'];
  192. $array['contact_addresses'][$index_5]['address_country'] = $contact_address['country'];
  193. $array['contact_addresses'][$index_5]['address_primary'] = @sizeof($contact['addresses']) == 1 ? 1 : 0;
  194. }
  195. }
  196. //add google contact id, etag and updated date to contact settings
  197. $contact['updated'] = str_replace('T', ' ', $contact['updated']);
  198. $contact['updated'] = str_replace('Z', '', $contact['updated']);
  199. $contact_setting_columns = array('contact_setting_category', 'contact_setting_subcategory', 'contact_setting_name', 'contact_setting_value', 'contact_setting_order', 'contact_setting_enabled');
  200. $contact_setting_array[] = array('sync', 'source', 'array', 'google', 0, 'true');
  201. $contact_setting_array[] = array('google', 'id', 'text', $contact_id, 0, 'true');
  202. $contact_setting_array[] = array('google', 'updated', 'date', $contact['updated'], 0, 'true');
  203. $contact_setting_array[] = array('google', 'etag', 'text', $contact['etag'], 0, 'true');
  204. foreach ($contact_setting_array as $index_6 => $values) {
  205. $contact_setting_uuid = uuid();
  206. $array['contact_settings'][$index_6]['contact_setting_uuid'] = $contact_setting_uuid;
  207. $array['contact_settings'][$index_6]['contact_uuid'] = $contact_uuid;
  208. $array['contact_settings'][$index_6]['domain_uuid'] = $_SESSION['domain_uuid'];
  209. foreach ($values as $index_7 => $value) {
  210. foreach ($contact_setting_columns as $column) {
  211. $array['contact_settings'][$index_6][$contact_setting_columns[$index_7]] = $value;
  212. }
  213. }
  214. }
  215. unset($contact_setting_columns, $contact_setting_array);
  216. //insert records
  217. $database = new database;
  218. $database->app_name = 'contacts';
  219. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  220. $database->save($array);
  221. unset($array);
  222. //increment counter
  223. $contacts_imported++;
  224. }
  225. $message = $text['message-contacts_imported']." ".$contacts_imported;
  226. if ($contacts_replaced > 0) { $message .= " (".$text['message_contacts_imported_replaced']." ".$contacts_replaced.")"; }
  227. if ($contacts_skipped > 0) { $message .= ", ".$text['message_contacts_imported_skipped']." ".$contacts_skipped; }
  228. message::add($message);
  229. header("Location: contacts.php");
  230. exit;
  231. }
  232. else {
  233. // no contacts imported
  234. message::add($text['message-contacts_imported']." ".$contacts_imported, 'negative');
  235. }
  236. }
  237. //*******************************************************************************************
  238. //check if authenticated
  239. if ($_SESSION['contact_auth']['token'] == '') {
  240. $_SESSION['contact_auth']['referer'] = substr($_SERVER["HTTP_REFERER"], strrpos($_SERVER["HTTP_REFERER"],'/')+1);
  241. header("Location: contact_auth.php?source=google&target=".substr($_SERVER["PHP_SELF"], strrpos($_SERVER["PHP_SELF"],'/')+1));
  242. exit;
  243. }
  244. unset($_SESSION['contact_auth']['source'], $_SESSION['contact_auth']['target']);
  245. //get groups & contacts
  246. $groups = google_get_groups($_SESSION['contact_auth']['token']);
  247. $contacts = google_get_contacts($_SESSION['contact_auth']['token'], 1000);
  248. //store in session variable for use on import
  249. $_SESSION['contact_auth']['google'] = $contacts;
  250. //include the header
  251. $document['title'] = $text['title-contacts_import_google'];
  252. require_once "resources/header.php";
  253. echo "<table cellpadding='0' cellspacing='0' border='0' align='right'>";
  254. echo " <tr>";
  255. echo " <td style='text-align: right;'>";
  256. echo " <input type='button' class='btn' id='btn_back' onclick=\"document.location.href='contact_import.php';\" value=\"".$text['button-back']."\">";
  257. echo " <input type='button' class='btn' id='btn_refresh' onclick='document.location.reload();' value=\"".$text['button-reload']."\">";
  258. echo " <input type='button' class='btn' id='btn_signout' onclick=\"document.location.href='contact_auth.php?source=google&signout'\" value=\"".$text['button-sign_out']."\">";
  259. echo " </td>";
  260. echo " </tr>";
  261. echo " <tr>";
  262. echo " <td style='text-align: right; white-space: nowrap; padding-top: 8px;'><span style='font-weight: bold; color: #000;'>".$_SESSION['contact_auth']['name']."</a> (<a href='https://www.google.com/contacts/#contacts' target='_blank'>".$_SESSION['contact_auth']['email']."</a>)"."</td>";
  263. echo " </tr>";
  264. echo "</table>";
  265. echo "<b>".$text['header-contacts_import_google']."</b>";
  266. echo "<br><br>";
  267. echo $text['description-contacts_import_google'];
  268. echo "<br><br><br>";
  269. $row_style["0"] = "row_style0";
  270. $row_style["1"] = "row_style1";
  271. echo "<form name='frm_import' id='frm_import' method='post'>\n";
  272. echo "<input type='hidden' name='a' value='import'>\n";
  273. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  274. echo "<tr>\n";
  275. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  276. echo " ".$text['label-import_fields']."\n";
  277. echo "</td>\n";
  278. echo "<td width='70%' class='vtable' align='left'>\n";
  279. echo " <input type='checkbox' disabled='disabled' checked>&nbsp;".$text['label-contact_name']."&nbsp;\n";
  280. echo " <input type='checkbox' disabled='disabled' checked>&nbsp;".$text['label-contact_organization']."&nbsp;\n";
  281. echo " <input type='checkbox' name='import_fields[email]' id='field_email' value='1' checked><label for='field_email'>&nbsp;".$text['label-contact_email']."</label>&nbsp;\n";
  282. echo " <input type='checkbox' name='import_fields[number]' id='field_number' value='1' checked><label for='field_number'>&nbsp;".$text['label-phone_number']."</label>&nbsp;\n";
  283. echo " <input type='checkbox' name='import_fields[url]' id='field_url' value='1' checked><label for='field_url'>&nbsp;".$text['label-contact_url']."</label>&nbsp;\n";
  284. echo " <input type='checkbox' name='import_fields[address]' id='field_address' value='1' checked><label for='field_address'>&nbsp;".$text['label-address_address']."</label>\n";
  285. echo "<br />\n";
  286. echo $text['description-import_fields']."\n";
  287. echo "</td>\n";
  288. echo "</tr>\n";
  289. echo "<tr>\n";
  290. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  291. echo " ".$text['label-contact_type']."\n";
  292. echo "</td>\n";
  293. echo "<td class='vtable' align='left'>\n";
  294. if (is_array($_SESSION["contact"]["type"])) {
  295. sort($_SESSION["contact"]["type"]);
  296. echo " <select class='formfld' name='import_type'>\n";
  297. echo " <option value=''></option>\n";
  298. foreach($_SESSION["contact"]["type"] as $row) {
  299. echo " <option value='".$row."'>".$row."</option>\n";
  300. }
  301. echo " </select>\n";
  302. }
  303. else {
  304. echo " <select class='formfld' name='import_type'>\n";
  305. echo " <option value=''></option>\n";
  306. echo " <option value='customer'>".$text['option-contact_type_customer']."</option>\n";
  307. echo " <option value='contractor'>".$text['option-contact_type_contractor']."</option>\n";
  308. echo " <option value='friend'>".$text['option-contact_type_friend']."</option>\n";
  309. echo " <option value='lead'>".$text['option-contact_type_lead']."</option>\n";
  310. echo " <option value='member'>".$text['option-contact_type_member']."</option>\n";
  311. echo " <option value='family'>".$text['option-contact_type_family']."</option>\n";
  312. echo " <option value='subscriber'>".$text['option-contact_type_subscriber']."</option>\n";
  313. echo " <option value='supplier'>".$text['option-contact_type_supplier']."</option>\n";
  314. echo " <option value='provider'>".$text['option-contact_type_provider']."</option>\n";
  315. echo " <option value='user'>".$text['option-contact_type_user']."</option>\n";
  316. echo " <option value='volunteer'>".$text['option-contact_type_volunteer']."</option>\n";
  317. echo " </select>\n";
  318. }
  319. echo "<br />\n";
  320. echo $text['description-contact_type_import']."\n";
  321. echo "</td>\n";
  322. echo "</tr>\n";
  323. echo "<tr>\n";
  324. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  325. echo " ".$text['label-contact_category']."\n";
  326. echo "</td>\n";
  327. echo "<td class='vtable' align='left'>\n";
  328. if (is_array($_SESSION["contact"]["category"])) {
  329. sort($_SESSION["contact"]["category"]);
  330. echo " <select class='formfld' name='import_category'>\n";
  331. echo " <option value=''></option>\n";
  332. foreach($_SESSION["contact"]["category"] as $row) {
  333. echo " <option value='".$row."'>".$row."</option>\n";
  334. }
  335. echo " </select>\n";
  336. }
  337. else {
  338. echo " <input class='formfld' type='text' name='import_category' maxlength='255'>\n";
  339. }
  340. echo "<br />\n";
  341. echo $text['description-contact_category_import']."\n";
  342. echo "</td>\n";
  343. echo "</tr>\n";
  344. echo "<tr>\n";
  345. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  346. echo " ".$text['label-shared']."\n";
  347. echo "</td>\n";
  348. echo "<td class='vtable' align='left'>\n";
  349. echo " <select class='formfld' name='import_shared' id='import_shared'>\n";
  350. echo " <option value='false'>".$text['option-false']."</option>\n";
  351. echo " <option value='true'>".$text['option-true']."</option>\n";
  352. echo " </select>\n";
  353. echo " <br />\n";
  354. echo $text['description-shared_import']."\n";
  355. echo "</td>\n";
  356. echo "</tr>\n";
  357. echo "<tr>\n";
  358. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  359. echo " ".$text['label-import_duplicates']."\n";
  360. echo "</td>\n";
  361. echo "<td class='vtable' align='left'>\n";
  362. echo " <select class='formfld' style='width: 150px;' name='import_duplicates'>\n";
  363. echo " <option value='skip'>".$text['option-import_duplicates_skip']."</option>\n";
  364. echo " <option value='replace'>".$text['option-import_duplicates_replace']."</option>\n";
  365. echo " </select>\n";
  366. echo "<br />\n";
  367. echo $text['description-import_duplicates']."\n";
  368. echo "</td>\n";
  369. echo "</tr>\n";
  370. echo "</table>";
  371. echo "<br><br>";
  372. //display groups
  373. echo "<b>".$text['label-groups']."</b>";
  374. echo "<br><br>";
  375. echo "<table class='tr_hover' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  376. echo "<tr>\n";
  377. echo " <th style='width: 30px; text-align: center; padding: 0px;'>&nbsp;</th>";
  378. echo " <th>".$text['label-contact_name']."</th>\n";
  379. echo "</tr>\n";
  380. //determine contact count in groups
  381. foreach ($contacts as $contact) {
  382. foreach ($contact['groups'] as $group_id => $meh) {
  383. $groups[$group_id]['count']++;
  384. }
  385. }
  386. $c = 0;
  387. foreach ($groups as $group['id'] => $group) {
  388. if ($group['count'] > 0) {
  389. echo "<tr>\n";
  390. echo " <td valign='top' class='".$row_style[$c]."' style='text-align: center; padding: 3px 0px 0px 0px;'><input type='checkbox' name='group_id[]' id='group_id_".$group['id']."' value='".$group['id']."'></td>\n";
  391. echo " <td valign='top' class='".$row_style[$c]."' onclick=\"document.getElementById('group_id_".$group['id']."').checked = (document.getElementById('group_id_".$group['id']."').checked) ? false : true;\">".$group['name']." (".$group['count'].")</td>\n";
  392. echo "</tr>\n";
  393. $c=($c)?0:1;
  394. }
  395. }
  396. echo "</table>\n";
  397. echo "<br>";
  398. echo "<div style='text-align: right;'><input type='submit' class='btn' id='btn_submit' value=\"".$text['button-import']."\"></div>";
  399. echo "<br>";
  400. //display contacts
  401. echo "<b>".$text['header-contacts']."</b>";
  402. echo "<br><br>";
  403. echo "<table class='tr_hover' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  404. echo "<tr>\n";
  405. echo " <th style='width: 30px; text-align: center; padding: 0px;'><input type='checkbox' onchange=\"(this.checked) ? check('all') : check('none');\"></th>";
  406. echo " <th>".$text['label-contact_name']."</th>\n";
  407. echo " <th>".$text['label-contact_organization']."</th>\n";
  408. echo " <th>".$text['label-contact_email']."</th>\n";
  409. echo " <th>".$text['label-phone_number']."</th>\n";
  410. echo " <th>".$text['label-contact_url']."</th>\n";
  411. echo " <th>".$text['label-address_address']."</th>\n";
  412. echo " <th>".$text['label-group']."</th>\n";
  413. echo "</tr>\n";
  414. $c = 0;
  415. foreach ($contacts as $contact['id'] => $contact) {
  416. $contact_ids[] = $contact['id'];
  417. echo "<tr>\n";
  418. echo " <td valign='top' class='".$row_style[$c]."' style='text-align: center; padding: 3px 0px 0px 0px;'><input type='checkbox' name='contact_id[]' id='contact_id_".$contact['id']."' value='".$contact['id']."'></td>\n";
  419. echo " <td valign='top' class='".$row_style[$c]."' onclick=\"document.getElementById('contact_id_".$contact['id']."').checked = (document.getElementById('contact_id_".$contact['id']."').checked) ? false : true;\">";
  420. $contact_name[] = $contact['name_prefix'];
  421. $contact_name[] = $contact['name_given'];
  422. $contact_name[] = $contact['name_middle'];
  423. $contact_name[] = $contact['name_family'];
  424. $contact_name[] = $contact['name_suffix'];
  425. echo " ".implode(' ', $contact_name)."&nbsp;";
  426. unset($contact_name);
  427. echo " </td>\n";
  428. echo " <td valign='top' class='".$row_style[$c]."' style='max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
  429. echo " ".(($contact['title']) ? $contact['title']."<br>" : null).$contact['organization']."&nbsp;";
  430. echo " </td>\n";
  431. echo " <td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
  432. if (sizeof($contact['emails']) > 0) {
  433. foreach ($contact['emails'] as $contact_email) {
  434. $contact_emails[] = "<span style='font-size: 80%;'>".$contact_email['label'].":</span> <a href='mailto: ".$contact_email['address']."'>".$contact_email['address']."</a>";
  435. }
  436. echo implode('<br>', $contact_emails);
  437. unset($contact_emails);
  438. } else { echo "&nbsp;"; }
  439. echo " </td>\n";
  440. echo " <td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
  441. if (sizeof($contact['numbers']) > 0) {
  442. foreach ($contact['numbers'] as $contact_number) {
  443. $contact_number_part = "<span style='font-size: 80%;'>".$contact_number['label'].":</span> ";
  444. if (substr_count(strtoupper($contact_number['label']), 'FAX') == 0) {
  445. $contact_number_part .= "<a href='javascript:void(0);' onclick=\"send_cmd('".PROJECT_PATH."/app/click_to_call/click_to_call.php?src_cid_name=".urlencode($contact_number['number'])."&src_cid_number=".urlencode($contact_number['number'])."&dest_cid_name=".urlencode($_SESSION['user']['extension'][0]['outbound_caller_id_name'])."&dest_cid_number=".urlencode($_SESSION['user']['extension'][0]['outbound_caller_id_number'])."&src=".urlencode($_SESSION['user']['extension'][0]['user'])."&dest=".urlencode($contact_number['number'])."&rec=false&ringback=us-ring&auto_answer=true');\">";
  446. }
  447. $contact_number_part .= format_phone($contact_number['number']);
  448. if (substr_count(strtoupper($contact_number['label']), 'FAX') == 0) {
  449. $contact_number_part .= "</a>";
  450. }
  451. $contact_numbers[] = $contact_number_part;
  452. unset($contact_number_part);
  453. }
  454. echo implode('<br>', $contact_numbers);
  455. unset($contact_numbers);
  456. } else { echo "&nbsp;"; }
  457. echo " </td>\n";
  458. echo " <td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
  459. if (sizeof($contact['urls']) > 0) {
  460. foreach ($contact['urls'] as $contact_url) {
  461. $contact_urls[] = "<span style='font-size: 80%;'>".$contact_url['label'].":</span> <a href='".$contact_url['url']."' target='_blank'>".str_replace("http://", "", str_replace("https://", "", $contact_url['url']))."</a>";
  462. }
  463. echo implode('<br>', $contact_urls);
  464. unset($contact_urls);
  465. } else { echo "&nbsp;"; }
  466. echo " </td>\n";
  467. echo " <td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
  468. if (sizeof($contact['addresses']) > 0) {
  469. foreach ($contact['addresses'] as $contact_address) {
  470. if ($contact_address['street'] != '') { $contact_address_parts[] = $contact_address['street']; }
  471. if ($contact_address['extended'] != '') { $contact_address_parts[] = $contact_address['extended']; }
  472. if ($contact_address['community'] != '') { $contact_address_parts[] = $contact_address['community']; }
  473. if ($contact_address['locality'] != '') { $contact_address_parts[] = $contact_address['locality']; }
  474. if ($contact_address['region'] != '') { $contact_address_parts[] = $contact_address['region']; }
  475. if ($contact_address['postal_code'] != '') { $contact_address_parts[] = $contact_address['postal_code']; }
  476. if ($contact_address['country'] != '') { $contact_address_parts[] = $contact_address['country']; }
  477. $contact_addresses[] = "<span style='font-size: 80%;'>".$contact_address['label'].":</span> ".implode(', ', $contact_address_parts);
  478. unset($contact_address_parts);
  479. }
  480. echo implode('<br>', $contact_addresses);
  481. unset($contact_addresses);
  482. } else { echo "&nbsp;"; }
  483. echo " </td>\n";
  484. echo " <td valign='top' class='".$row_style[$c]."' style='white-space: nowrap;'>";
  485. foreach ($contact['groups'] as $contact_group['id'] => $contact_group['name']) {
  486. $contact_groups[] = $contact_group['name'];
  487. }
  488. echo " ".implode('<br>', $contact_groups);
  489. unset($contact_groups);
  490. echo " </td>\n";
  491. echo "</tr>\n";
  492. $c=($c)?0:1;
  493. }
  494. echo "</table>\n";
  495. echo "<br>";
  496. echo "<div style='text-align: right;'><input type='submit' class='btn' id='btn_submit' value=\"".$text['button-import']."\"></div>";
  497. echo "</form>";
  498. echo "<br><br>";
  499. // check or uncheck all contact checkboxes
  500. if (sizeof($contact_ids) > 0) {
  501. echo "<script>\n";
  502. echo " function check(what) {\n";
  503. foreach ($contact_ids as $contact_id) {
  504. echo " document.getElementById('contact_id_".$contact_id."').checked = (what == 'all') ? true : false;\n";
  505. }
  506. echo " }\n";
  507. echo "</script>\n";
  508. }
  509. /*
  510. echo "<pre>";
  511. print_r($contacts);
  512. echo "</pre>";
  513. echo "<br><br>";
  514. echo "<hr>";
  515. echo "<br><br><b>SOURCE JSON DECODED ARRAY</b>...<br><br><pre>";
  516. print_r($records);
  517. echo "</pre>";
  518. */
  519. //include the footer
  520. require_once "resources/footer.php";
  521. // used above
  522. function curl_file_get_contents($url) {
  523. $curl = curl_init();
  524. $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
  525. curl_setopt($curl, CURLOPT_URL, $url); //The URL to retrieve. This can also be set when initializing a session with curl_init().
  526. 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.
  527. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); //The number of seconds to wait while trying to connect.
  528. curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request.
  529. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header.
  530. curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
  531. curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute.
  532. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //To stop cURL from verifying the peer's certificate.
  533. $contents = curl_exec($curl);
  534. curl_close($curl);
  535. return $contents;
  536. }
  537. ?>