Fork of FusionPBX but with LDAP kinda working
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

462 lines
15 KiB

2 years ago
2 years ago
  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 <markjcrane@fusionpbx.com>
  16. Portions created by the Initial Developer are Copyright (C) 2018-2020
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <markjcrane@fusionpbx.com>
  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('extension_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 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. //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']."/extensions-".$_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('extension_import')) {
  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 "app/extensions/app_config.php";
  78. $i = 0;
  79. foreach ($apps[0]['db'] as $table) {
  80. //get the table name and parent name
  81. $table_name = $table["table"]['name'];
  82. $parent_name = $table["table"]['parent'];
  83. //remove the v_ table prefix
  84. if (substr($table_name, 0, 2) == 'v_') {
  85. $table_name = substr($table_name, 2);
  86. }
  87. if (substr($parent_name, 0, 2) == 'v_') {
  88. $parent_name = substr($parent_name, 2);
  89. }
  90. //filter for specific tables and build the schema array
  91. if ($table_name == "extensions") {
  92. $schema[$i]['table'] = $table_name;
  93. $schema[$i]['parent'] = $parent_name;
  94. foreach ($table['fields'] as $row) {
  95. if ($row['deprecated'] !== 'true') {
  96. if (is_array($row['name'])) {
  97. $field_name = $row['name']['text'];
  98. }
  99. else {
  100. $field_name = $row['name'];
  101. }
  102. $schema[$i]['fields'][] = $field_name;
  103. }
  104. }
  105. $i++;
  106. }
  107. }
  108. $i++;
  109. $schema[$i]['table'] = 'extension_users';
  110. $schema[$i]['parent'] = 'extensions';
  111. $schema[$i]['fields'][] = 'username';
  112. }
  113. //match the column names to the field names
  114. if (strlen($delimiter) > 0 && file_exists($_SESSION['file']) && $action != 'import') {
  115. //validate the token
  116. $token = new token;
  117. if (!$token->validate($_SERVER['PHP_SELF'])) {
  118. message::add($text['message-invalid_token'],'negative');
  119. header('Location: extension_imports.php');
  120. exit;
  121. }
  122. //create token
  123. $object = new token;
  124. $token = $object->create($_SERVER['PHP_SELF']);
  125. //include header
  126. $document['title'] = $text['title-extension_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-extension_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'=>'extension_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 = trim(trim($line_field), $enclosure);
  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: extension_imports.php');
  208. exit;
  209. }
  210. //user selected fields
  211. $fields = $_POST['fields'];
  212. //set the domain_uuid
  213. $domain_uuid = $_SESSION['domain_uuid'];
  214. //get the users
  215. $sql = "select * from v_users where domain_uuid = :domain_uuid ";
  216. $parameters['domain_uuid'] = $domain_uuid;
  217. $database = new database;
  218. $users = $database->select($sql, $parameters, 'all');
  219. unset($sql, $parameters);
  220. //get the contents of the csv file and convert them into an array
  221. $handle = @fopen($_SESSION['file'], "r");
  222. if ($handle) {
  223. //set the starting identifiers
  224. $row_id = 0;
  225. $row_number = 1;
  226. //loop through the array
  227. while (($line = fgets($handle, 4096)) !== false) {
  228. if ($from_row <= $row_number) {
  229. //format the data
  230. $y = 0;
  231. foreach ($fields as $key => $value) {
  232. //get the line
  233. $result = str_getcsv($line, $delimiter, $enclosure);
  234. //get the table and field name
  235. $field_array = explode(".",$value);
  236. $table_name = $field_array[0];
  237. $field_name = $field_array[1];
  238. //echo "value: $value<br />\n";
  239. //echo "table_name: $table_name<br />\n";
  240. //echo "field_name: $field_name<br />\n";
  241. //get the parent table name
  242. $parent = get_parent($schema, $table_name);
  243. //remove formatting from the phone number
  244. if ($field_name == "phone_number") {
  245. $result[$key] = preg_replace('{\D}', '', $result[$key]);
  246. }
  247. //set the extension enabled to lower case
  248. if ($field_name == 'enabled') {
  249. $result[$key] = strtolower($result[$key]);
  250. }
  251. //build the data array
  252. if (strlen($table_name) > 0) {
  253. if (strlen($parent) == 0) {
  254. $array[$table_name][$row_id]['domain_uuid'] = $domain_uuid;
  255. $array[$table_name][$row_id][$field_name] = $result[$key];
  256. }
  257. elseif ($field_name != "username") {
  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. if ($field_name == "username") {
  262. foreach ($users as $field) {
  263. if ($field['username'] == $result[$key]) {
  264. //$array[$parent][$row_id]['extension_users'][$y]['cextension_user_uuid'] = uuid();
  265. $array[$parent][$row_id]['extension_users'][$y]['domain_uuid'] = $domain_uuid;
  266. //$array[$parent][$row_id]['extension_users'] = $row['extension_uuid'];
  267. $array[$parent][$row_id]['extension_users'][$y]['user_uuid'] = $field['user_uuid'];
  268. }
  269. }
  270. }
  271. }
  272. }
  273. //process a chunk of the array
  274. if ($row_id === 1000) {
  275. //save to the data
  276. $database = new database;
  277. $database->app_name = 'extensions';
  278. $database->app_uuid = 'e68d9689-2769-e013-28fa-6214bf47fca3';
  279. $database->save($array);
  280. //clear the array
  281. unset($array);
  282. //set the row id back to 0
  283. $row_id = 0;
  284. }
  285. } //if ($from_row <= $row_number)
  286. $row_number++;
  287. $row_id++;
  288. } //end while
  289. fclose($handle);
  290. //save to the data
  291. if (is_array($array)) {
  292. $database = new database;
  293. $database->app_name = 'extensions';
  294. $database->app_uuid = 'e68d9689-2769-e013-28fa-6214bf47fca3';
  295. $database->save($array);
  296. unset($array);
  297. }
  298. //send the redirect header
  299. header("Location: extensions.php");
  300. exit;
  301. }
  302. }
  303. //Hmm I mean this seems to be easier to implement
  304. //create token
  305. $object = new token;
  306. $token = $object->create($_SERVER['PHP_SELF']);
  307. //include the header
  308. $document['title'] = $text['title-extension_import'];
  309. require_once "resources/header.php";
  310. //show content
  311. echo "<form name='frmUpload' method='post' enctype='multipart/form-data'>\n";
  312. echo "<div class='action_bar' id='action_bar'>\n";
  313. echo " <div class='heading'><b>".$text['header-extension_import']."</b></div>\n";
  314. echo " <div class='actions'>\n";
  315. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'extensions.php']);
  316. echo button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>$_SESSION['theme']['button_icon_upload'],'id'=>'btn_save']);
  317. echo " </div>\n";
  318. echo " <div style='clear: both;'></div>\n";
  319. echo "</div>\n";
  320. echo $text['description-import']."\n";
  321. echo "<br /><br />\n";
  322. echo "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n";
  323. echo "<tr>\n";
  324. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  325. echo " ".$text['label-import_data']."\n";
  326. echo "</td>\n";
  327. echo "<td width='70%' class='vtable' align='left'>\n";
  328. echo " <textarea name='data' id='data' class='formfld' style='width: 100%; min-height: 150px;' wrap='off'>$data</textarea>\n";
  329. echo "<br />\n";
  330. echo $text['description-import_data']."\n";
  331. echo "</td>\n";
  332. echo "</tr>\n";
  333. echo "<tr>\n";
  334. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  335. echo " ".$text['label-from_row']."\n";
  336. echo "</td>\n";
  337. echo "<td class='vtable' align='left'>\n";
  338. echo " <select class='formfld' name='from_row'>\n";
  339. $i=2;
  340. while($i<=99) {
  341. $selected = ($i == $from_row) ? "selected" : null;
  342. echo " <option value='$i' ".$selected.">$i</option>\n";
  343. $i++;
  344. }
  345. echo " </select>\n";
  346. echo "<br />\n";
  347. echo $text['description-from_row']."\n";
  348. echo "</td>\n";
  349. echo "</tr>\n";
  350. echo "<tr>\n";
  351. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  352. echo " ".$text['label-import_delimiter']."\n";
  353. echo "</td>\n";
  354. echo "<td class='vtable' align='left'>\n";
  355. echo " <select class='formfld' style='width:40px;' name='data_delimiter'>\n";
  356. echo " <option value=','>,</option>\n";
  357. echo " <option value='|'>|</option>\n";
  358. echo " </select>\n";
  359. echo "<br />\n";
  360. echo $text['description-import_delimiter']."\n";
  361. echo "</td>\n";
  362. echo "</tr>\n";
  363. echo "<tr>\n";
  364. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  365. echo " ".$text['label-import_enclosure']."\n";
  366. echo "</td>\n";
  367. echo "<td class='vtable' align='left'>\n";
  368. echo " <select class='formfld' style='width:40px;' name='data_enclosure'>\n";
  369. echo " <option value='\"'>\"</option>\n";
  370. echo " <option value=''></option>\n";
  371. echo " </select>\n";
  372. echo "<br />\n";
  373. echo $text['description-import_enclosure']."\n";
  374. echo "</td>\n";
  375. echo "</tr>\n";
  376. echo "<tr>\n";
  377. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  378. echo " ".$text['label-import_file_upload']."\n";
  379. echo "</td>\n";
  380. echo "<td class='vtable' align='left'>\n";
  381. echo " <input name='ulfile' type='file' class='formfld fileinput' id='ulfile'>\n";
  382. echo "<br />\n";
  383. echo "</td>\n";
  384. echo "</tr>\n";
  385. echo "</table>\n";
  386. echo "<br><br>";
  387. echo "<input name='type' type='hidden' value='csv'>\n";
  388. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  389. echo "</form>";
  390. //include the footer
  391. require_once "resources/footer.php";
  392. ?>