user_imports.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. //set the include path
  22. $conf = glob("{/usr/local/etc,/etc}/fusionpbx/config.conf", GLOB_BRACE);
  23. set_include_path(parse_ini_file($conf[0])['document.root']);
  24. //includes files
  25. require_once "resources/require.php";
  26. require_once "resources/check_auth.php";
  27. //check permissions
  28. if (permission_exists('user_import')) {
  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. //built in str_getcsv requires PHP 5.3 or higher, this function can be used to reproduce the functionality but requires PHP 5.1.0 or higher
  39. if (!function_exists('str_getcsv')) {
  40. function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") {
  41. $fp = fopen("php://memory", 'r+');
  42. fputs($fp, $input);
  43. rewind($fp);
  44. $data = fgetcsv($fp, null, $delimiter, $enclosure); // $escape only got added in 5.3.0
  45. fclose($fp);
  46. return $data;
  47. }
  48. }
  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'].'/users-'.$_SESSION['domain_name'].'.csv';
  57. if (file_put_contents($file, $_POST['data'])) {
  58. $_SESSION['file'] = $file;
  59. }
  60. }
  61. //copy the csv file
  62. //$_POST['submit'] == "Upload" &&
  63. if (is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('user_import')) {
  64. if ($_POST['type'] == 'csv') {
  65. $file = $_SESSION['server']['temp']['dir'].'/users-'.$_SESSION['domain_name'].'.csv';
  66. if (move_uploaded_file($_FILES['ulfile']['tmp_name'], $file)) {
  67. $_SESSION['file'] = $file;
  68. }
  69. }
  70. }
  71. //get the schema
  72. if (!empty($delimiter) && file_exists($_SESSION['file'] ?? '')) {
  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 ("core/users/app_config.php");
  79. $i = 0;
  80. foreach ($apps[0]['db'] as $table) {
  81. //get the table name and parent name
  82. if (is_array($table["table"]['name'])) {
  83. $table_name = $table["table"]['name']['text'];
  84. }
  85. else {
  86. $table_name = $table["table"]['name'];
  87. }
  88. $parent_name = $table["table"]['parent'];
  89. //remove the v_ table prefix
  90. if (substr($table_name, 0, 2) == 'v_') {
  91. $table_name = substr($table_name, 2);
  92. }
  93. if (substr($parent_name, 0, 2) == 'v_') {
  94. $parent_name = substr($parent_name, 2);
  95. }
  96. //filter for specific tables and build the schema array
  97. if ($table_name == "users") {
  98. $schema[$i]['table'] = $table_name;
  99. $schema[$i]['parent'] = $parent_name;
  100. foreach($table['fields'] as $row) {
  101. if ($row['deprecated'] !== 'true') {
  102. if (is_array($row['name'])) {
  103. $field_name = $row['name']['text'];
  104. }
  105. else {
  106. $field_name = $row['name'];
  107. }
  108. $schema[$i]['fields'][] = $field_name;
  109. }
  110. }
  111. $i++;
  112. }
  113. }
  114. $schema[$i]['table'] = 'user_groups';
  115. $schema[$i]['parent'] = 'users';
  116. $schema[$i]['fields'][] = 'group_name';
  117. //debug info
  118. //view_array($schema);
  119. }
  120. //match the column names to the field names
  121. if (!empty($delimiter) && file_exists($_SESSION['file'] ?? '') && $action != 'import') {
  122. //create token
  123. $object = new token;
  124. $token = $object->create($_SERVER['PHP_SELF']);
  125. //include header
  126. $document['title'] = $text['title-user_import'];
  127. require_once "resources/header.php";
  128. //form to match the fields to the column names
  129. echo "<form name='frmUpload' method='post' enctype='multipart/form-data'>\n";
  130. echo "<div class='action_bar' id='action_bar'>\n";
  131. echo " <div class='heading'><b>".$text['header-user_import']."</b></div>\n";
  132. echo " <div class='actions'>\n";
  133. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'user_imports.php']);
  134. echo button::create(['type'=>'submit','label'=>$text['button-import'],'icon'=>$_SESSION['theme']['button_icon_import'],'id'=>'btn_save']);
  135. echo " </div>\n";
  136. echo " <div style='clear: both;'></div>\n";
  137. echo "</div>\n";
  138. echo $text['description-import']."\n";
  139. echo "<br /><br />\n";
  140. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  141. //loop through user columns
  142. $x = 0;
  143. foreach ($line_fields as $line_field) {
  144. $line_field = preg_replace('#[^a-zA-Z0-9_]#', '', $line_field);
  145. echo "<tr>\n";
  146. echo " <td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  147. //echo " ".$text['label-zzz']."\n";
  148. echo $line_field;
  149. echo " </td>\n";
  150. echo " <td width='70%' class='vtable' align='left'>\n";
  151. echo " <select class='formfld' style='' name='fields[$x]'>\n";
  152. echo " <option value=''></option>\n";
  153. foreach($schema as $row) {
  154. echo " <optgroup label='".$row['table']."'>\n";
  155. foreach($row['fields'] as $field) {
  156. $selected = '';
  157. if ($field == $line_field) {
  158. $selected = "selected='selected'";
  159. }
  160. if ($field !== 'domain_uuid') {
  161. echo " <option value='".$row['table'].".".$field."' ".$selected.">".$field."</option>\n";
  162. }
  163. }
  164. echo " </optgroup>\n";
  165. }
  166. echo " </select>\n";
  167. //echo "<br />\n";
  168. //echo $text['description-zzz']."\n";
  169. echo " </td>\n";
  170. echo "</tr>\n";
  171. $x++;
  172. }
  173. echo "</table>\n";
  174. echo "<br /><br />\n";
  175. echo "<input name='action' type='hidden' value='import'>\n";
  176. echo "<input name='from_row' type='hidden' value='$from_row'>\n";
  177. echo "<input name='data_delimiter' type='hidden' value='$delimiter'>\n";
  178. echo "<input name='data_enclosure' type='hidden' value='$enclosure'>\n";
  179. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  180. echo "</form>\n";
  181. require_once "resources/footer.php";
  182. //normalize the column names
  183. //$line = strtolower($line);
  184. //$line = str_replace("-", "_", $line);
  185. //$line = str_replace($delimiter."title".$delimiter, $delimiter."contact_title".$delimiter, $line);
  186. //$line = str_replace("firstname", "name_given", $line);
  187. //$line = str_replace("lastname", "name_family", $line);
  188. //$line = str_replace("company", "organization", $line);
  189. //$line = str_replace("company", "contact_email", $line);
  190. //end the script
  191. exit;
  192. }
  193. //get the parent table
  194. function get_parent($schema,$table_name) {
  195. foreach ($schema as $row) {
  196. if ($row['table'] == $table_name) {
  197. return $row['parent'];
  198. }
  199. }
  200. }
  201. //upload the csv
  202. if (file_exists($_SESSION['file'] ?? '') && $action == 'import') {
  203. //validate the token
  204. $token = new token;
  205. if (!$token->validate($_SERVER['PHP_SELF'])) {
  206. message::add($text['message-invalid_token'],'negative');
  207. header('Location: users.php');
  208. exit;
  209. }
  210. //form to match the fields to the column names
  211. //$document['title'] = $text['title-user_import'];
  212. //require_once "resources/header.php";
  213. //user selected fields
  214. $fields = $_POST['fields'];
  215. //set the domain_uuid
  216. $domain_uuid = $_SESSION['domain_uuid'];
  217. //get the groups
  218. $sql = "select * from v_groups where domain_uuid is null ";
  219. $database = new database;
  220. $groups = $database->select($sql, null, 'all');
  221. unset($sql);
  222. //get the contents of the csv file and convert them into an array
  223. $handle = @fopen($_SESSION['file'], "r");
  224. if ($handle) {
  225. //set the starting identifiers
  226. $row_id = 0;
  227. $row_number = 1;
  228. //loop through the array
  229. while (($line = fgets($handle, 4096)) !== false) {
  230. if ($from_row <= $row_number) {
  231. //get the user_uuid
  232. $user_uuid = uuid();
  233. //format the data
  234. $y = 0;
  235. foreach ($fields as $key => $value) {
  236. //get the line
  237. $result = str_getcsv($line, $delimiter, $enclosure);
  238. //get the table and field name
  239. $field_array = explode(".",$value);
  240. $table_name = $field_array[0];
  241. $field_name = $field_array[1];
  242. //echo "value: $value<br />\n";
  243. //echo "table_name: $table_name<br />\n";
  244. //echo "field_name: $field_name<br />\n";
  245. //get the parent table name
  246. $parent = get_parent($schema, $table_name);
  247. //clean the phone number
  248. //if ($field_name == "phone") {
  249. // $result[$key] = preg_replace('{\D}', '', $result[$key]);
  250. //}
  251. //build the data array
  252. if (!empty($table_name)) {
  253. if (empty($parent)) {
  254. $array[$table_name][$row_id]['domain_uuid'] = $domain_uuid;
  255. $array[$table_name][$row_id][$field_name] = $result[$key];
  256. }
  257. else {
  258. if ($field_name != "group_name") {
  259. $array[$parent][$row_id][$table_name][$y]['domain_uuid'] = $domain_uuid;
  260. $array[$parent][$row_id][$table_name][$y][$field_name] = $result[$key];
  261. }
  262. }
  263. if ($field_name == "group_name") {
  264. $group_name = '';
  265. foreach ($groups as $field) {
  266. if ($field['group_name'] == $result[$key]) {
  267. $group_name = $field['group_name'];
  268. $array['user_groups'][$row_id]['user_group_uuid'] = uuid();
  269. $array['user_groups'][$row_id]['domain_uuid'] = $domain_uuid;
  270. $array['user_groups'][$row_id]['group_name'] = $field['group_name'];
  271. $array['user_groups'][$row_id]['group_uuid'] = $field['group_uuid'];
  272. $array['user_groups'][$row_id]['user_uuid'] = $user_uuid;
  273. }
  274. }
  275. //remove superadmin if not the correct permission
  276. if ($group_name == 'superadmin') {
  277. if (!permission_exists('group_domain')) {
  278. unset($array['user_groups'][$row_id]);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. //set the password hash cost
  285. $options = array('cost' => 10);
  286. //set the hash the user password
  287. $password = $array['users'][$row_id]['password'];
  288. $array['users'][$row_id]['password'] = password_hash($password, PASSWORD_DEFAULT, $options);
  289. //set the user_uuid
  290. $array['users'][$row_id]['user_uuid'] = $user_uuid;
  291. //debug
  292. //echo "<pre>\n";
  293. //print_r($array);
  294. //echo "</pre>\n";
  295. //exit;
  296. //process a chunk of the array
  297. if ($row_id === 1000) {
  298. //save to the data
  299. $database = new database;
  300. $database->app_name = 'users';
  301. $database->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e';
  302. $database->save($array);
  303. //$message = $database->message;
  304. //clear the array
  305. unset($array);
  306. //set the row id back to 0
  307. $row_id = 0;
  308. }
  309. } //if ($from_row <= $row_id)
  310. $row_number++;
  311. $row_id++;
  312. } //end while
  313. fclose($handle);
  314. //debug info
  315. //echo "<pre>\n";
  316. //print_r($array);
  317. //echo "</pre>\n";
  318. //exit;
  319. //save to the data
  320. if (is_array($array)) {
  321. $database = new database;
  322. $database->app_name = 'users';
  323. $database->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e';
  324. $database->save($array);
  325. //$message = $database->message;
  326. unset($array);
  327. }
  328. //send the redirect header
  329. header("Location: users.php");
  330. return;
  331. }
  332. }
  333. //create token
  334. $object = new token;
  335. $token = $object->create($_SERVER['PHP_SELF']);
  336. //include the header
  337. $document['title'] = $text['title-user_import'];
  338. require_once "resources/header.php";
  339. //show content
  340. echo "<form name='frmUpload' method='post' enctype='multipart/form-data'>\n";
  341. echo "<div class='action_bar' id='action_bar'>\n";
  342. echo " <div class='heading'><b>".$text['header-user_import']."</b></div>\n";
  343. echo " <div class='actions'>\n";
  344. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'users.php']);
  345. echo button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>$_SESSION['theme']['button_icon_upload'],'id'=>'btn_save']);
  346. echo " </div>\n";
  347. echo " <div style='clear: both;'></div>\n";
  348. echo "</div>\n";
  349. echo $text['description-import']."\n";
  350. echo "<br /><br />\n";
  351. echo "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n";
  352. echo "<tr>\n";
  353. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  354. echo " ".$text['label-import_data']."\n";
  355. echo "</td>\n";
  356. echo "<td width='70%' class='vtable' align='left'>\n";
  357. echo " <textarea name='data' id='data' class='formfld' style='width: 100%; min-height: 150px;' wrap='off'>$data</textarea>\n";
  358. echo "<br />\n";
  359. echo $text['description-import_data']."\n";
  360. echo "</td>\n";
  361. echo "</tr>\n";
  362. echo "<tr>\n";
  363. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  364. echo " ".$text['label-from_row']."\n";
  365. echo "</td>\n";
  366. echo "<td class='vtable' align='left'>\n";
  367. echo " <select class='formfld' name='from_row'>\n";
  368. $i=2;
  369. while($i<=99) {
  370. $selected = ($i == $from_row) ? "selected" : null;
  371. echo " <option value='$i' ".$selected.">$i</option>\n";
  372. $i++;
  373. }
  374. echo " </select>\n";
  375. echo "<br />\n";
  376. echo $text['description-from_row']."\n";
  377. echo "</td>\n";
  378. echo "</tr>\n";
  379. echo "<tr>\n";
  380. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  381. echo " ".$text['label-import_delimiter']."\n";
  382. echo "</td>\n";
  383. echo "<td class='vtable' align='left'>\n";
  384. echo " <select class='formfld' style='width:40px;' name='data_delimiter'>\n";
  385. echo " <option value=','>,</option>\n";
  386. echo " <option value='|'>|</option>\n";
  387. echo " </select>\n";
  388. echo "<br />\n";
  389. echo $text['description-import_delimiter']."\n";
  390. echo "</td>\n";
  391. echo "</tr>\n";
  392. echo "<tr>\n";
  393. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  394. echo " ".$text['label-import_enclosure']."\n";
  395. echo "</td>\n";
  396. echo "<td class='vtable' align='left'>\n";
  397. echo " <select class='formfld' style='width:40px;' name='data_enclosure'>\n";
  398. echo " <option value='\"'>\"</option>\n";
  399. echo " <option value=''></option>\n";
  400. echo " </select>\n";
  401. echo "<br />\n";
  402. echo $text['description-import_enclosure']."\n";
  403. echo "</td>\n";
  404. echo "</tr>\n";
  405. echo "<tr>\n";
  406. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  407. echo " ".$text['label-import_file_upload']."\n";
  408. echo "</td>\n";
  409. echo "<td class='vtable' align='left'>\n";
  410. echo " <input name='ulfile' type='file' class='formfld fileinput' id='ulfile'>\n";
  411. echo "<br />\n";
  412. echo "</td>\n";
  413. echo "</tr>\n";
  414. echo " <tr>\n";
  415. echo " <td valign='bottom'>\n";
  416. echo " &nbsp;\n";
  417. echo " </td>\n";
  418. echo " <td valign='bottom' align='right' nowrap>\n";
  419. echo " <input name='type' type='hidden' value='csv'>\n";
  420. echo " <input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  421. echo " </td>\n";
  422. echo " </tr>\n";
  423. echo " </table>\n";
  424. echo "<br><br>";
  425. echo "</form>";
  426. //include the footer
  427. require_once "resources/footer.php";
  428. ?>