From 16b64e42fc5d14413251e30554ec5e93731e84f1 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Fri, 27 Oct 2023 21:01:23 -0600 Subject: [PATCH] Add a new import for bridges --- app/bridges/app_config.php | 7 +- app/bridges/app_languages.php | 25 ++ app/bridges/bridge_imports.php | 436 +++++++++++++++++++++++++++++++++ app/bridges/bridges.php | 3 + 4 files changed, 469 insertions(+), 2 deletions(-) create mode 100644 app/bridges/bridge_imports.php diff --git a/app/bridges/app_config.php b/app/bridges/app_config.php index db0a7ccc08..d5cbbda9b0 100644 --- a/app/bridges/app_config.php +++ b/app/bridges/app_config.php @@ -3,9 +3,9 @@ //application details $apps[$x]['name'] = 'Bridges'; $apps[$x]['uuid'] = 'a6a7c4c5-340a-43ce-bcbc-2ed9bab8659d'; - $apps[$x]['category'] = ''; + $apps[$x]['category'] = 'switch'; $apps[$x]['subcategory'] = ''; - $apps[$x]['version'] = ''; + $apps[$x]['version'] = '1.2'; $apps[$x]['license'] = 'Mozilla Public License 1.1'; $apps[$x]['url'] = 'http://www.fusionpbx.com'; $apps[$x]['description']['en-us'] = ''; @@ -36,6 +36,9 @@ $apps[$x]['permissions'][$y]['groups'][] = 'superadmin'; $apps[$x]['permissions'][$y]['groups'][] = 'admin'; $y++; + $apps[$x]['permissions'][$y]['name'] = 'bridge_import'; + $apps[$x]['permissions'][$y]['groups'][] = 'superadmin'; + $y++; //destination details $y = 0; diff --git a/app/bridges/app_languages.php b/app/bridges/app_languages.php index 6e905e1c5f..23632ef0a8 100644 --- a/app/bridges/app_languages.php +++ b/app/bridges/app_languages.php @@ -75,6 +75,31 @@ $text['title_description-bridge']['zh-cn'] = "将桥接语句添加到目标选 $text['title_description-bridge']['ja-jp'] = "宛先選択リストにブリッジ ステートメントを追加します。"; $text['title_description-bridge']['ko-kr'] = "대상 선택 목록에 브리지 문을 추가합니다."; +$text['label-bridge_import']['en-us'] = "Bridge Import"; +$text['label-bridge_import']['en-gb'] = "Bridge Import"; +$text['label-bridge_import']['ar-eg'] = ""; +$text['label-bridge_import']['de-at'] = ""; +$text['label-bridge_import']['de-ch'] = ""; +$text['label-bridge_import']['de-de'] = ""; +$text['label-bridge_import']['el-gr'] = ""; +$text['label-bridge_import']['es-cl'] = ""; +$text['label-bridge_import']['es-mx'] = ""; +$text['label-bridge_import']['fr-ca'] = ""; +$text['label-bridge_import']['fr-fr'] = ""; +$text['label-bridge_import']['he-il'] = ""; +$text['label-bridge_import']['it-it'] = ""; +$text['label-bridge_import']['nl-nl'] = ""; +$text['label-bridge_import']['pl-pl'] = ""; +$text['label-bridge_import']['pt-br'] = ""; +$text['label-bridge_import']['pt-pt'] = ""; +$text['label-bridge_import']['ro-ro'] = ""; +$text['label-bridge_import']['ru-ru'] = ""; +$text['label-bridge_import']['sv-se'] = ""; +$text['label-bridge_import']['uk-ua'] = ""; +$text['label-bridge_import']['zh-cn'] = ""; +$text['label-bridge_import']['ja-jp'] = ""; +$text['label-bridge_import']['ko-kr'] = ""; + $text['label-bridge_name']['en-us'] = "Name"; $text['label-bridge_name']['en-gb'] = "Name"; $text['label-bridge_name']['ar-eg'] = "اسم"; diff --git a/app/bridges/bridge_imports.php b/app/bridges/bridge_imports.php new file mode 100644 index 0000000000..345a0575b6 --- /dev/null +++ b/app/bridges/bridge_imports.php @@ -0,0 +1,436 @@ + + Portions created by the Initial Developer are Copyright (C) 2019-2023 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +//includes files + require_once dirname(__DIR__, 2) . "/resources/require.php"; + require_once "resources/check_auth.php"; + +//check permissions + if (permission_exists('bridge_import')) { + //access granted + } + else { + echo "access denied"; + exit; + } + +//add multi-lingual support + $language = new text; + $text = $language->get(); + +//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 + if (!function_exists('str_getcsv')) { + function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") { + $fp = fopen("php://memory", 'r+'); + fputs($fp, $input); + rewind($fp); + $data = fgetcsv($fp, null, $delimiter, $enclosure); // $escape only got added in 5.3.0 + fclose($fp); + return $data; + } + } + +//set the max php execution time + ini_set('max_execution_time', 7200); + +//get the http get values and set them as php variables + $action = $_POST["action"] ?? ''; + $from_row = $_POST["from_row"] ?? ''; + $delimiter = $_POST["data_delimiter"] ?? ''; + $enclosure = $_POST["data_enclosure"] ?? ''; + +//save the data to the csv file + if (isset($_POST['data'])) { + $file = $_SESSION['server']['temp']['dir']."/bridges-".$_SESSION['domain_name'].".csv"; + file_put_contents($file, $_POST['data']); + $_SESSION['file'] = $file; + } + +//copy the csv file + if (!empty($_FILES['ulfile']['tmp_name']) && is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('bridge_import')) { + if ($_POST['type'] == 'csv') { + move_uploaded_file($_FILES['ulfile']['tmp_name'], $_SESSION['server']['temp']['dir'].'/'.$_FILES['ulfile']['name']); + $save_msg = "Uploaded file to ".$_SESSION['server']['temp']['dir']."/". htmlentities($_FILES['ulfile']['name']); + //system('chmod -R 744 '.$_SESSION['server']['temp']['dir'].'*'); + unset($_POST['txtCommand']); + $file = $_SESSION['server']['temp']['dir'].'/'.$_FILES['ulfile']['name']; + $_SESSION['file'] = $file; + } + } + +//get the schema + if (!empty($delimiter)) { + //get the first line + $line = fgets(fopen($_SESSION['file'], 'r')); + $line_fields = explode($delimiter, $line); + + //get the schema + $x = 0; + include ("app/bridges/app_config.php"); + $i = 0; + foreach ($apps[0]['db'] as $table) { + //get the table name and parent name + if (is_array($table["table"]['name'])) { + $table_name = $table["table"]['name']['text']; + } + else { + $table_name = $table["table"]['name']; + } + $parent_name = $table["table"]['parent']; + + //remove the v_ table prefix + if (substr($table_name, 0, 2) == 'v_') { + $table_name = substr($table_name, 2); + } + if (substr($parent_name, 0, 2) == 'v_') { + $parent_name = substr($parent_name, 2); + } + + //filter for specific tables and build the schema array + if ($table_name == "bridges") { + $schema[$i]['table'] = $table_name; + $schema[$i]['parent'] = $parent_name; + foreach($table['fields'] as $row) { + $row['deprecated'] = $row['deprecated'] ?? ''; + if ($row['deprecated'] !== 'true') { + if (is_array($row['name'])) { + $field_name = $row['name']['text']; + } + else { + $field_name = $row['name']; + } + $schema[$i]['fields'][] = $field_name; + } + } + $i++; + } + } + } + +//match the column names to the field names + if (!empty($delimiter) && file_exists($_SESSION['file']) && $action != 'import') { + + //create token + $object = new token; + $token = $object->create($_SERVER['PHP_SELF']); + + //include header + $document['title'] = $text['label-bridge_import']; + require_once "resources/header.php"; + + //form to match the fields to the column names + echo "
\n"; + + echo "
\n"; + echo "
".$text['label-bridge_import']."
\n"; + echo "
\n"; + echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'bridges.php']); + echo button::create(['type'=>'submit','label'=>$text['button-import'],'icon'=>$_SESSION['theme']['button_icon_import'],'id'=>'btn_save']); + echo "
\n"; + echo "
\n"; + echo "
\n"; + + echo $text['description-import']."\n"; + echo "

\n"; + + echo "\n"; + + //loop through user columns + $x = 0; + foreach ($line_fields as $line_field) { + $line_field = preg_replace('#[^a-zA-Z0-9_]#', '', $line_field); + echo "\n"; + echo " \n"; + echo " \n"; + echo "\n"; + $x++; + } + + echo "
\n"; + //echo " ".$text['label-zzz']."\n"; + echo $line_field; + echo " \n"; + echo " \n"; + //echo "
\n"; + //echo $text['description-zzz']."\n"; + echo "
\n"; + echo "

\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "
\n"; + + require_once "resources/footer.php"; + + //normalize the column names + //$line = strtolower($line); + //$line = str_replace("-", "_", $line); + //$line = str_replace($delimiter."title".$delimiter, $delimiter."contact_title".$delimiter, $line); + //$line = str_replace("firstname", "name_given", $line); + //$line = str_replace("lastname", "name_family", $line); + //$line = str_replace("company", "organization", $line); + //$line = str_replace("company", "contact_email", $line); + + //end the script + exit; + } + +//get the parent table + function get_parent($schema,$table_name) { + foreach ($schema as $row) { + if ($row['table'] == $table_name) { + return $row['parent']; + } + } + } + +//upload the csv + if (file_exists($_SESSION['file'] ?? '') && $action == 'import') { + + //validate the token + $token = new token; + if (!$token->validate($_SERVER['PHP_SELF'])) { + message::add($text['message-invalid_token'],'negative'); + header('Location: extension_imports.php'); + exit; + } + + //user selected fields + $fields = $_POST['fields']; + + //set the domain_uuid + $domain_uuid = $_SESSION['domain_uuid']; + + //get the contents of the csv file and convert them into an array + $handle = @fopen($_SESSION['file'], "r"); + if ($handle) { + //set the starting identifiers + $row_id = 0; + $row_number = 1; + + //loop through the array + while (($line = fgets($handle, 4096)) !== false) { + if ($from_row <= $row_number) { + //format the data + $y = 0; + foreach ($fields as $key => $value) { + //get the line + $result = str_getcsv($line, $delimiter, $enclosure); + + //get the table and field name + $field_array = explode(".",$value); + $table_name = $field_array[0]; + $field_name = $field_array[1]; + //echo "value: $value
\n"; + //echo "table_name: $table_name
\n"; + //echo "field_name: $field_name
\n"; + + //get the parent table name + $parent = get_parent($schema, $table_name); + + //remove formatting from the phone number + if ($field_name == "phone_number") { + $result[$key] = preg_replace('{\D}', '', $result[$key]); + } + + //set the voicemail_local_after_email, voicemail_tutorial, and voicemail_enabled to lower case + if ($field_name == 'voicemail_local_after_email' || $field_name == 'voicemail_tutorial' || $field_name == 'voicemail_enabled') { + $result[$key] = strtolower($result[$key]); + } + + //build the data array + if (!empty($table_name)) { + if (empty($parent)) { + $array[$table_name][$row_id]['domain_uuid'] = $domain_uuid; + $array[$table_name][$row_id][$field_name] = $result[$key]; + } + else { + $array[$parent][$row_id][$table_name][$y]['domain_uuid'] = $domain_uuid; + $array[$parent][$row_id][$table_name][$y][$field_name] = $result[$key]; + } + } + } + + //process a chunk of the array + if ($row_id === 1000) { + + //save to the data + $database = new database; + $database->app_name = 'bridges'; + $database->app_uuid = 'b523c2d2-64cd-46f1-9520-ca4b4098e044'; + $database->save($array); + + //clear the array + unset($array); + + //set the row id back to 0 + $row_id = 0; + } + + } //if ($from_row <= $row_number) + $row_number++; + $row_id++; + } //end while + fclose($handle); + + //save to the data + if (!empty($array) && is_array($array)) { + $database = new database; + $database->app_name = 'bridges'; + $database->app_uuid = 'b523c2d2-64cd-46f1-9520-ca4b4098e044'; + $database->save($array); + unset($array); + } + + //send the redirect header + header("Location: bridges.php"); + exit; + } + } + +//create token + $object = new token; + $token = $object->create($_SERVER['PHP_SELF']); + +//include the header + $document['title'] = $text['label-bridge_import']; + require_once "resources/header.php"; + +//show content + echo "
\n"; + + echo "
\n"; + echo "
".$text['label-bridge_import']."
\n"; + echo "
\n"; + echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'bridges.php']); + echo button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>$_SESSION['theme']['button_icon_upload'],'id'=>'btn_save']); + echo "
\n"; + echo "
\n"; + echo "
\n"; + + echo $text['description-import']."\n"; + echo "

\n"; + + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "
\n"; + echo " ".$text['label-import_data']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-import_data']."\n"; + echo "
\n"; + echo " ".$text['label-from_row']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-from_row']."\n"; + echo "
\n"; + echo " ".$text['label-import_delimiter']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-import_delimiter']."\n"; + echo "
\n"; + echo " ".$text['label-import_enclosure']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-import_enclosure']."\n"; + echo "
\n"; + echo " ".$text['label-import_file_upload']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo "
\n"; + echo "

"; + + echo "\n"; + echo "\n"; + + echo "
"; + +//include the footer + require_once "resources/footer.php"; + +?> diff --git a/app/bridges/bridges.php b/app/bridges/bridges.php index 3d1c415068..d9c7866071 100644 --- a/app/bridges/bridges.php +++ b/app/bridges/bridges.php @@ -145,6 +145,9 @@ echo "
\n"; echo "
".$text['title-bridges']." (".$num_rows.")
\n"; echo "
\n"; + if (permission_exists('bridge_import')) { + echo button::create(['type'=>'button','label'=>$text['button-import'],'icon'=>$_SESSION['theme']['button_icon_import'],'style'=>'margin-right: 15px;','link'=>'bridge_imports.php']); + } if (permission_exists('bridge_add')) { echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','link'=>'bridge_edit.php']); }