user_imports.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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-2023
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. //check permissions
  25. if (permission_exists('user_import')) {
  26. //access granted
  27. }
  28. else {
  29. echo "access denied";
  30. exit;
  31. }
  32. //connect to the database
  33. $database = new database;
  34. //add multi-lingual support
  35. $language = new text;
  36. $text = $language->get();
  37. //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
  38. if (!function_exists('str_getcsv')) {
  39. function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") {
  40. $fp = fopen("php://memory", 'r+');
  41. fputs($fp, $input);
  42. rewind($fp);
  43. $data = fgetcsv($fp, null, $delimiter, $enclosure); // $escape only got added in 5.3.0
  44. fclose($fp);
  45. return $data;
  46. }
  47. }
  48. //get the http get values and set them as php variables
  49. $action = $_POST["action"] ?? '';
  50. $from_row = $_POST["from_row"] ?? '';
  51. $delimiter = $_POST["data_delimiter"] ?? '';
  52. $enclosure = $_POST["data_enclosure"] ?? '';
  53. //save the data to the csv file
  54. if (isset($_POST['data'])) {
  55. $file = $_SESSION['server']['temp']['dir'].'/users-'.$_SESSION['domain_name'].'.csv';
  56. if (file_put_contents($file, $_POST['data'])) {
  57. $_SESSION['file'] = $file;
  58. }
  59. }
  60. //copy the csv file
  61. //$_POST['submit'] == "Upload" &&
  62. if (!empty($_FILES['ulfile']['tmp_name']) && is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('user_import')) {
  63. if (!empty($_POST['type']) && $_POST['type'] == 'csv') {
  64. $file = $_SESSION['server']['temp']['dir'].'/users-'.$_SESSION['domain_name'].'.csv';
  65. if (move_uploaded_file($_FILES['ulfile']['tmp_name'], $file)) {
  66. $_SESSION['file'] = $file;
  67. }
  68. }
  69. }
  70. //get the schema
  71. if (!empty($delimiter) && file_exists($_SESSION['file'] ?? '')) {
  72. //get the first line
  73. $line = fgets(fopen($_SESSION['file'], 'r'));
  74. $line_fields = explode($delimiter, $line);
  75. //get the schema
  76. $x = 0;
  77. include ("core/users/app_config.php");
  78. $i = 0;
  79. foreach ($apps[0]['db'] as $table) {
  80. //get the table name and parent name
  81. if (is_array($table["table"]['name'])) {
  82. $table_name = $table["table"]['name']['text'];
  83. }
  84. else {
  85. $table_name = $table["table"]['name'];
  86. }
  87. $parent_name = $table["table"]['parent'];
  88. //remove the v_ table prefix
  89. if (substr($table_name, 0, 2) == 'v_') {
  90. $table_name = substr($table_name, 2);
  91. }
  92. if (substr($parent_name, 0, 2) == 'v_') {
  93. $parent_name = substr($parent_name, 2);
  94. }
  95. //filter for specific tables and build the schema array
  96. if ($table_name == "users") {
  97. $schema[$i]['table'] = $table_name;
  98. $schema[$i]['parent'] = $parent_name;
  99. foreach($table['fields'] as $row) {
  100. $row['deprecated'] = $row['deprecated'] ?? '';
  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 "<div class='card'>\n";
  141. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  142. //loop through user columns
  143. $x = 0;
  144. foreach ($line_fields as $line_field) {
  145. $line_field = preg_replace('#[^a-zA-Z0-9_]#', '', $line_field);
  146. echo "<tr>\n";
  147. echo " <td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  148. //echo " ".$text['label-zzz']."\n";
  149. echo $line_field;
  150. echo " </td>\n";
  151. echo " <td width='70%' class='vtable' align='left'>\n";
  152. echo " <select class='formfld' style='' name='fields[$x]'>\n";
  153. echo " <option value=''></option>\n";
  154. foreach($schema as $row) {
  155. echo " <optgroup label='".$row['table']."'>\n";
  156. foreach($row['fields'] as $field) {
  157. $selected = '';
  158. if ($field == $line_field) {
  159. $selected = "selected='selected'";
  160. }
  161. if ($field !== 'domain_uuid') {
  162. echo " <option value='".$row['table'].".".$field."' ".$selected.">".$field."</option>\n";
  163. }
  164. }
  165. echo " </optgroup>\n";
  166. }
  167. echo " </select>\n";
  168. //echo "<br />\n";
  169. //echo $text['description-zzz']."\n";
  170. echo " </td>\n";
  171. echo "</tr>\n";
  172. $x++;
  173. }
  174. echo "</table>\n";
  175. echo "</div>\n";
  176. echo "<br /><br />\n";
  177. echo "<input name='action' type='hidden' value='import'>\n";
  178. echo "<input name='from_row' type='hidden' value='$from_row'>\n";
  179. echo "<input name='data_delimiter' type='hidden' value='$delimiter'>\n";
  180. echo "<input name='data_enclosure' type='hidden' value='$enclosure'>\n";
  181. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  182. echo "</form>\n";
  183. require_once "resources/footer.php";
  184. //normalize the column names
  185. //$line = strtolower($line);
  186. //$line = str_replace("-", "_", $line);
  187. //$line = str_replace($delimiter."title".$delimiter, $delimiter."contact_title".$delimiter, $line);
  188. //$line = str_replace("firstname", "name_given", $line);
  189. //$line = str_replace("lastname", "name_family", $line);
  190. //$line = str_replace("company", "organization", $line);
  191. //$line = str_replace("company", "contact_email", $line);
  192. //end the script
  193. exit;
  194. }
  195. //get the parent table
  196. function get_parent($schema,$table_name) {
  197. foreach ($schema as $row) {
  198. if ($row['table'] == $table_name) {
  199. return $row['parent'];
  200. }
  201. }
  202. }
  203. //upload the csv
  204. if (file_exists($_SESSION['file'] ?? '') && $action == 'import') {
  205. //validate the token
  206. $token = new token;
  207. if (!$token->validate($_SERVER['PHP_SELF'])) {
  208. message::add($text['message-invalid_token'],'negative');
  209. header('Location: users.php');
  210. exit;
  211. }
  212. //form to match the fields to the column names
  213. //$document['title'] = $text['title-user_import'];
  214. //require_once "resources/header.php";
  215. //user selected fields
  216. $fields = $_POST['fields'];
  217. //set the domain_uuid
  218. $domain_uuid = $_SESSION['domain_uuid'];
  219. //get the groups
  220. $sql = "select * from v_groups where domain_uuid is null ";
  221. $groups = $database->select($sql, null, 'all');
  222. unset($sql);
  223. //get the contents of the csv file and convert them into an array
  224. $handle = @fopen($_SESSION['file'], "r");
  225. if ($handle) {
  226. //set the starting identifiers
  227. $row_id = 0;
  228. $row_number = 1;
  229. //loop through the array
  230. while (($line = fgets($handle, 4096)) !== false) {
  231. if ($from_row <= $row_number) {
  232. //get the user_uuid
  233. $user_uuid = uuid();
  234. //format the data
  235. $y = 0;
  236. foreach ($fields as $key => $value) {
  237. //get the line
  238. $result = str_getcsv($line, $delimiter, $enclosure);
  239. //get the table and field name
  240. $field_array = explode(".",$value);
  241. $table_name = $field_array[0];
  242. $field_name = $field_array[1];
  243. //echo "value: $value<br />\n";
  244. //echo "table_name: $table_name<br />\n";
  245. //echo "field_name: $field_name<br />\n";
  246. //get the parent table name
  247. $parent = get_parent($schema, $table_name);
  248. //clean the phone number
  249. //if ($field_name == "phone") {
  250. // $result[$key] = preg_replace('{\D}', '', $result[$key]);
  251. //}
  252. //build the data array
  253. if (!empty($table_name)) {
  254. if (empty($parent)) {
  255. $array[$table_name][$row_id]['domain_uuid'] = $domain_uuid;
  256. $array[$table_name][$row_id][$field_name] = $result[$key];
  257. }
  258. else {
  259. if ($field_name != "group_name") {
  260. $array[$parent][$row_id][$table_name][$y]['domain_uuid'] = $domain_uuid;
  261. $array[$parent][$row_id][$table_name][$y][$field_name] = $result[$key];
  262. }
  263. }
  264. if ($field_name == "group_name") {
  265. $group_name = '';
  266. foreach ($groups as $field) {
  267. if ($field['group_name'] == $result[$key]) {
  268. $group_name = $field['group_name'];
  269. $array['user_groups'][$row_id]['user_group_uuid'] = uuid();
  270. $array['user_groups'][$row_id]['domain_uuid'] = $domain_uuid;
  271. $array['user_groups'][$row_id]['group_name'] = $field['group_name'];
  272. $array['user_groups'][$row_id]['group_uuid'] = $field['group_uuid'];
  273. $array['user_groups'][$row_id]['user_uuid'] = $user_uuid;
  274. }
  275. }
  276. //remove superadmin if not the correct permission
  277. if ($group_name == 'superadmin') {
  278. if (!permission_exists('group_domain')) {
  279. unset($array['user_groups'][$row_id]);
  280. }
  281. }
  282. }
  283. }
  284. }
  285. //set the password hash cost
  286. $options = array('cost' => 10);
  287. //set the hash the user password
  288. $password = $array['users'][$row_id]['password'];
  289. $array['users'][$row_id]['password'] = password_hash($password, PASSWORD_DEFAULT, $options);
  290. //set the user_uuid
  291. $array['users'][$row_id]['user_uuid'] = $user_uuid;
  292. //debug
  293. //echo "<pre>\n";
  294. //print_r($array);
  295. //echo "</pre>\n";
  296. //exit;
  297. //process a chunk of the array
  298. if ($row_id === 1000) {
  299. //save to the data
  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 (!empty($array)) {
  321. $database->app_name = 'users';
  322. $database->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e';
  323. $database->save($array);
  324. //$message = $database->message;
  325. unset($array);
  326. }
  327. //send the redirect header
  328. header("Location: users.php");
  329. return;
  330. }
  331. }
  332. //create token
  333. $object = new token;
  334. $token = $object->create($_SERVER['PHP_SELF']);
  335. //include the header
  336. $document['title'] = $text['title-user_import'];
  337. require_once "resources/header.php";
  338. //show content
  339. echo "<form name='frmUpload' method='post' enctype='multipart/form-data'>\n";
  340. echo "<div class='action_bar' id='action_bar'>\n";
  341. echo " <div class='heading'><b>".$text['header-user_import']."</b></div>\n";
  342. echo " <div class='actions'>\n";
  343. 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']);
  344. echo button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>$_SESSION['theme']['button_icon_upload'],'id'=>'btn_save']);
  345. echo " </div>\n";
  346. echo " <div style='clear: both;'></div>\n";
  347. echo "</div>\n";
  348. echo $text['description-import']."\n";
  349. echo "<br /><br />\n";
  350. echo "<div class='card'>\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'></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 "</div>\n";
  425. echo "</form>";
  426. echo "<br><br>";
  427. //include the footer
  428. require_once "resources/footer.php";
  429. ?>