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