contact_import.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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-2021
  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. //check permissions
  26. if (permission_exists('contact_add')) {
  27. //access granted
  28. }
  29. else {
  30. echo "access denied";
  31. exit;
  32. }
  33. //add multi-lingual support
  34. $language = new text;
  35. $text = $language->get();
  36. //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
  37. if (!function_exists('str_getcsv')) {
  38. function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") {
  39. $fp = fopen("php://memory", 'r+');
  40. fputs($fp, $input);
  41. rewind($fp);
  42. $data = fgetcsv($fp, null, $delimiter, $enclosure); // $escape only got added in 5.3.0
  43. fclose($fp);
  44. return $data;
  45. }
  46. }
  47. //set the max php execution time
  48. ini_set('max_execution_time', 7200);
  49. //get the http get values and set them as php variables
  50. $action = $_POST["action"];
  51. $from_row = $_POST["from_row"];
  52. $delimiter = $_POST["data_delimiter"];
  53. $enclosure = $_POST["data_enclosure"];
  54. //save the data to the csv file
  55. if (isset($_POST['data'])) {
  56. $file = $_SESSION['server']['temp']['dir']."/contacts-".$_SESSION['domain_name'].".csv";
  57. file_put_contents($file, $_POST['data']);
  58. $_SESSION['file'] = $file;
  59. }
  60. //copy the csv file
  61. //$_POST['submit'] == "Upload" &&
  62. if ( is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('contact_upload')) {
  63. if ($_POST['type'] == 'csv') {
  64. move_uploaded_file($_FILES['ulfile']['tmp_name'], $_SESSION['server']['temp']['dir'].'/'.$_FILES['ulfile']['name']);
  65. $save_msg = "Uploaded file to ".$_SESSION['server']['temp']['dir']."/". htmlentities($_FILES['ulfile']['name']);
  66. //system('chmod -R 744 '.$_SESSION['server']['temp']['dir'].'*');
  67. $file = $_SESSION['server']['temp']['dir'].'/'.$_FILES['ulfile']['name'];
  68. $_SESSION['file'] = $file;
  69. }
  70. }
  71. //get the schema
  72. if (strlen($delimiter) > 0) {
  73. //get the first line
  74. $line = fgets(fopen($_SESSION['file'], 'r'));
  75. $line_fields = explode($delimiter, $line);
  76. //get the schema
  77. $x = 0;
  78. include "app/contacts/app_config.php";
  79. $i = 0;
  80. foreach ($apps[0]['db'] as $table) {
  81. //get the table name and parent name
  82. $table_name = $table["table"]['name'];
  83. $parent_name = $table["table"]['parent'];
  84. //remove the v_ table prefix
  85. if (substr($table_name, 0, 2) == 'v_') {
  86. $table_name = substr($table_name, 2);
  87. }
  88. if (substr($parent_name, 0, 2) == 'v_') {
  89. $parent_name = substr($parent_name, 2);
  90. }
  91. //filter for specific tables and build the schema array
  92. if ($table_name == "contacts" || $table_name == "contact_addresses" ||
  93. $table_name == "contact_phones" || $table_name == "contact_emails" ||
  94. $table_name == "contact_urls") {
  95. $schema[$i]['table'] = $table_name;
  96. $schema[$i]['parent'] = $parent_name;
  97. foreach ($table['fields'] as $row) {
  98. if ($row['deprecated'] !== 'true') {
  99. if (is_array($row['name'])) {
  100. $field_name = $row['name']['text'];
  101. }
  102. else {
  103. $field_name = $row['name'];
  104. }
  105. $schema[$i]['fields'][] = $field_name;
  106. }
  107. }
  108. $i++;
  109. }
  110. }
  111. $schema[$i]['table'] = 'contact_groups';
  112. $schema[$i]['parent'] = 'contacts';
  113. $schema[$i]['fields'][] = 'group_name';
  114. $i++;
  115. $schema[$i]['table'] = 'contact_users';
  116. $schema[$i]['parent'] = 'contacts';
  117. $schema[$i]['fields'][] = 'username';
  118. }
  119. //match the column names to the field names
  120. if (strlen($delimiter) > 0 && file_exists($_SESSION['file']) && $action != 'import') {
  121. //validate the token
  122. $token = new token;
  123. if (!$token->validate($_SERVER['PHP_SELF'])) {
  124. message::add($text['message-invalid_token'],'negative');
  125. header('Location: contact_import.php');
  126. exit;
  127. }
  128. //create token
  129. $object = new token;
  130. $token = $object->create($_SERVER['PHP_SELF']);
  131. //include header
  132. $document['title'] = $text['title-contacts_import'];
  133. require_once "resources/header.php";
  134. //form to match the fields to the column names
  135. echo "<form name='frmUpload' method='post' enctype='multipart/form-data'>\n";
  136. echo "<div class='action_bar' id='action_bar'>\n";
  137. echo " <div class='heading'><b>".$text['header-contacts_import']."</b></div>\n";
  138. echo " <div class='actions'>\n";
  139. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'contact_import.php']);
  140. echo button::create(['type'=>'submit','label'=>$text['button-import'],'icon'=>$_SESSION['theme']['button_icon_import'],'id'=>'btn_save']);
  141. echo " </div>\n";
  142. echo " <div style='clear: both;'></div>\n";
  143. echo "</div>\n";
  144. echo $text['description-contacts_import']."\n";
  145. echo "<br /><br />\n";
  146. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  147. //define phone label options
  148. if (is_array($_SESSION["contact"]["phone_label"]) && @sizeof($_SESSION["contact"]["phone_label"]) != 0) {
  149. sort($_SESSION["contact"]["phone_label"]);
  150. foreach($_SESSION["contact"]["phone_label"] as $row) {
  151. $label_options[] = "<option value='".$row.">".$row."</option>";
  152. }
  153. }
  154. else {
  155. $default_labels[] = $text['option-work'];
  156. $default_labels[] = $text['option-home'];
  157. $default_labels[] = $text['option-mobile'];
  158. $default_labels[] = $text['option-main'];
  159. $default_labels[] = $text['option-fax'];
  160. $default_labels[] = $text['option-pager'];
  161. $default_labels[] = $text['option-voicemail'];
  162. $default_labels[] = $text['option-text'];
  163. $default_labels[] = $text['option-other'];
  164. foreach ($default_labels as $default_label) {
  165. $label_options[] = "<option value='".$default_label."'>".$default_label."</option>";
  166. }
  167. }
  168. //loop through user columns
  169. $x = 0;
  170. foreach ($line_fields as $line_field) {
  171. $line_field = trim(trim($line_field), $enclosure);
  172. echo "<tr>\n";
  173. echo " <td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  174. echo $line_field;
  175. echo " </td>\n";
  176. echo " <td width='70%' class='vtable' align='left'>\n";
  177. echo " <select class='formfld' style='' name='fields[$x]' onchange=\"document.getElementById('labels_$x').style.display = this.options[this.selectedIndex].value == 'contact_phones.phone_number' ? 'inline' : 'none';\">\n";
  178. echo " <option value=''></option>\n";
  179. foreach($schema as $row) {
  180. echo " <optgroup label='".$row['table']."'>\n";
  181. foreach($row['fields'] as $field) {
  182. //if ($field == 'phone_label') { continue; }
  183. //if ($field == 'contact_url') { continue; } // can remove this after field is removed from the table
  184. $selected = '';
  185. if ($field == $line_field) {
  186. $selected = "selected='selected'";
  187. }
  188. if (substr($field, -5) != '_uuid') {
  189. echo " <option value='".$row['table'].".".$field."' ".$selected.">".$field."</option>\n";
  190. }
  191. }
  192. echo " </optgroup>\n";
  193. }
  194. echo " </select>\n";
  195. //echo " <select class='formfld' style='display: none;' id='labels_$x' name='labels[$x]'>\n";
  196. //echo is_array($label_options) ? implode("\n", $label_options) : null;
  197. //echo " </select>\n";
  198. echo " </td>\n";
  199. echo "</tr>\n";
  200. $x++;
  201. }
  202. echo "</table>\n";
  203. echo "<br /><br />\n";
  204. echo "<input name='action' type='hidden' value='import'>\n";
  205. echo "<input name='from_row' type='hidden' value='$from_row'>\n";
  206. echo "<input name='data_delimiter' type='hidden' value='$delimiter'>\n";
  207. echo "<input name='data_enclosure' type='hidden' value='$enclosure'>\n";
  208. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  209. echo "</form>\n";
  210. require_once "resources/footer.php";
  211. //normalize the column names
  212. //$line = strtolower($line);
  213. //$line = str_replace("-", "_", $line);
  214. //$line = str_replace($delimiter."title".$delimiter, $delimiter."contact_title".$delimiter, $line);
  215. //$line = str_replace("firstname", "name_given", $line);
  216. //$line = str_replace("lastname", "name_family", $line);
  217. //$line = str_replace("company", "organization", $line);
  218. //$line = str_replace("company", "contact_email", $line);
  219. //end the script
  220. exit;
  221. }
  222. //get the parent table
  223. function get_parent($schema,$table_name) {
  224. foreach ($schema as $row) {
  225. if ($row['table'] == $table_name) {
  226. return $row['parent'];
  227. }
  228. }
  229. }
  230. //upload the csv
  231. if (file_exists($_SESSION['file']) && $action == 'import') {
  232. //validate the token
  233. $token = new token;
  234. if (!$token->validate($_SERVER['PHP_SELF'])) {
  235. message::add($text['message-invalid_token'],'negative');
  236. header('Location: contact_import.php');
  237. exit;
  238. }
  239. //user selected fields, labels
  240. $fields = $_POST['fields'];
  241. $labels = $_POST['labels'];
  242. //set the domain_uuid
  243. $domain_uuid = $_SESSION['domain_uuid'];
  244. //get the groups
  245. $sql = "select * from v_groups where domain_uuid is null ";
  246. $database = new database;
  247. $groups = $database->select($sql, null, 'all');
  248. unset($sql);
  249. //get the users
  250. $sql = "select * from v_users where domain_uuid = :domain_uuid ";
  251. $parameters['domain_uuid'] = $domain_uuid;
  252. $database = new database;
  253. $users = $database->select($sql, $parameters, 'all');
  254. unset($sql, $parameters);
  255. //get the contents of the csv file and convert them into an array
  256. $handle = @fopen($_SESSION['file'], "r");
  257. if ($handle) {
  258. //set the starting identifiers
  259. $row_id = 0;
  260. $row_number = 1;
  261. //loop through the array
  262. while (($line = fgets($handle, 4096)) !== false) {
  263. if ($from_row <= $row_number) {
  264. //format the data
  265. $y = 0;
  266. foreach ($fields as $key => $value) {
  267. //get the line
  268. $result = str_getcsv($line, $delimiter, $enclosure);
  269. //get the table and field name
  270. $field_array = explode(".",$value);
  271. $table_name = $field_array[0];
  272. $field_name = $field_array[1];
  273. //echo "value: $value<br />\n";
  274. //echo "table_name: $table_name<br />\n";
  275. //echo "field_name: $field_name<br />\n";
  276. //get the parent table name
  277. $parent = get_parent($schema, $table_name);
  278. //count the field names
  279. if (isset($field_count[$table_name][$field_name])) {
  280. $field_count[$table_name][$field_name]++;
  281. }
  282. else {
  283. $field_count[$table_name][$field_name] = 0;
  284. }
  285. //set the ordinal ID
  286. $id = $field_count[$table_name][$field_name];
  287. //remove formatting from the phone number
  288. if ($field_name == "phone_number") {
  289. $result[$key] = preg_replace('{(?!^\+)[\D]}', '', $result[$key]);
  290. }
  291. //build the data array
  292. if (strlen($table_name) > 0) {
  293. if (strlen($parent) == 0) {
  294. $array[$table_name][$row_id]['domain_uuid'] = $domain_uuid;
  295. $array[$table_name][$row_id][$field_name] = $result[$key];
  296. }
  297. else {
  298. if ($field_name != "username" && $field_name != "group_name") {
  299. $array[$parent][$row_id][$table_name][$id]['domain_uuid'] = $domain_uuid;
  300. $array[$parent][$row_id][$table_name][$id][$field_name] = $result[$key];
  301. //if ($field_name == 'phone_number') {
  302. // $array[$parent][$row_id][$table_name][$id]['phone_label'] = $labels[$key];
  303. //}
  304. }
  305. }
  306. if ($field_name == "group_name") {
  307. foreach ($groups as $field) {
  308. if ($field['group_name'] == $result[$key]) {
  309. //$array[$parent][$row_id]['contact_group_uuid'] = uuid();
  310. $array[$parent][$row_id]['contact_groups'][$id]['domain_uuid'] = $domain_uuid;
  311. //$array['contact_groups'][$x]['contact_uuid'] = $row['contact_uuid'];
  312. $array[$parent][$row_id]['contact_groups'][$id]['group_uuid'] = $field['group_uuid'];
  313. }
  314. }
  315. }
  316. if ($field_name == "username") {
  317. foreach ($users as $field) {
  318. if ($field['username'] == $result[$key]) {
  319. //$array[$parent][$row_id]['contact_users'][$id]['contact_group_uuid'] = uuid();
  320. $array[$parent][$row_id]['contact_users'][$id]['domain_uuid'] = $domain_uuid;
  321. //$array['contact_groups'][$x]['contact_uuid'] = $row['contact_uuid'];
  322. $array[$parent][$row_id]['contact_users'][$id]['user_uuid'] = $field['user_uuid'];
  323. }
  324. }
  325. }
  326. }
  327. if (is_array($array[$parent][$row_id])) { $y++; }
  328. }
  329. //debug information
  330. //view_array($field_count);
  331. //process a chunk of the array
  332. if ($row_id === 1000) {
  333. //save to the data
  334. $database = new database;
  335. $database->app_name = 'contacts';
  336. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  337. $database->save($array);
  338. //clear the array
  339. unset($array);
  340. //set the row id back to 0
  341. $row_id = 0;
  342. }
  343. } //if ($from_row <= $row_number)
  344. unset($field_count);
  345. $row_number++;
  346. $row_id++;
  347. } //end while
  348. fclose($handle);
  349. //debug information
  350. //view_array($array);
  351. //save to the data
  352. if (is_array($array)) {
  353. $database = new database;
  354. $database->app_name = 'contacts';
  355. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  356. $database->save($array);
  357. unset($array);
  358. }
  359. //send the redirect header
  360. header("Location: contacts.php");
  361. exit;
  362. }
  363. }
  364. //create token
  365. $object = new token;
  366. $token = $object->create($_SERVER['PHP_SELF']);
  367. //include the header
  368. $document['title'] = $text['title-contacts_import'];
  369. require_once "resources/header.php";
  370. //show content
  371. echo "<form name='frmUpload' method='post' enctype='multipart/form-data'>\n";
  372. echo "<div class='action_bar' id='action_bar'>\n";
  373. echo " <div class='heading'><b>".$text['header-contacts_import']."</b></div>\n";
  374. echo " <div class='actions'>\n";
  375. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'contacts.php']);
  376. echo button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>$_SESSION['theme']['button_icon_upload'],'id'=>'btn_save']);
  377. echo " </div>\n";
  378. echo " <div style='clear: both;'></div>\n";
  379. echo "</div>\n";
  380. echo $text['description-contacts_import']."\n";
  381. echo "<br /><br />\n";
  382. echo "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n";
  383. echo "<tr>\n";
  384. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  385. echo " ".$text['label-import_data']."\n";
  386. echo "</td>\n";
  387. echo "<td width='70%' class='vtable' align='left'>\n";
  388. echo " <textarea name='data' id='data' class='formfld' style='width: 100%; min-height: 150px;' wrap='off'>$data</textarea>\n";
  389. echo "<br />\n";
  390. echo $text['description-import_data']."\n";
  391. echo "</td>\n";
  392. echo "</tr>\n";
  393. echo "<tr>\n";
  394. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  395. echo " ".$text['label-from_row']."\n";
  396. echo "</td>\n";
  397. echo "<td class='vtable' align='left'>\n";
  398. echo " <select class='formfld' name='from_row'>\n";
  399. $i=2;
  400. while($i<=99) {
  401. $selected = ($i == $from_row) ? "selected" : null;
  402. echo " <option value='$i' ".$selected.">$i</option>\n";
  403. $i++;
  404. }
  405. echo " </select>\n";
  406. echo "<br />\n";
  407. echo $text['description-from_row']."\n";
  408. echo "</td>\n";
  409. echo "</tr>\n";
  410. echo "<tr>\n";
  411. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  412. echo " ".$text['label-import_delimiter']."\n";
  413. echo "</td>\n";
  414. echo "<td class='vtable' align='left'>\n";
  415. echo " <select class='formfld' style='width:40px;' name='data_delimiter'>\n";
  416. echo " <option value=','>,</option>\n";
  417. echo " <option value='|'>|</option>\n";
  418. echo " </select>\n";
  419. echo "<br />\n";
  420. echo $text['description-import_delimiter']."\n";
  421. echo "</td>\n";
  422. echo "</tr>\n";
  423. echo "<tr>\n";
  424. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  425. echo " ".$text['label-import_enclosure']."\n";
  426. echo "</td>\n";
  427. echo "<td class='vtable' align='left'>\n";
  428. echo " <select class='formfld' style='width:40px;' name='data_enclosure'>\n";
  429. echo " <option value='\"'>\"</option>\n";
  430. echo " <option value=''></option>\n";
  431. echo " </select>\n";
  432. echo "<br />\n";
  433. echo $text['description-import_enclosure']."\n";
  434. echo "</td>\n";
  435. echo "</tr>\n";
  436. echo "<tr>\n";
  437. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  438. echo " ".$text['label-import_file_upload']."\n";
  439. echo "</td>\n";
  440. echo "<td class='vtable' align='left'>\n";
  441. echo " <input name='ulfile' type='file' class='formfld fileinput' id='ulfile'>\n";
  442. echo "<br />\n";
  443. echo "</td>\n";
  444. echo "</tr>\n";
  445. echo "</table>\n";
  446. echo "<br />\n";
  447. if (function_exists('curl_version') && $_SESSION['contact']['google_oauth_client_id']['text'] != '' && $_SESSION['contact']['google_oauth_client_secret']['text'] != '') {
  448. echo "<a href='contact_import_google.php'><img src='resources/images/icon_gcontacts.png' style='width: 21px; height: 21px; border: none; text-decoration: none; margin-right: 5px;' align='absmiddle'>".$text['header-contacts_import_google']."</a>\n";
  449. }
  450. echo "<br />\n";
  451. echo "<input name='type' type='hidden' value='csv'>\n";
  452. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  453. echo "</form>";
  454. //include the footer
  455. require_once "resources/footer.php";
  456. ?>