From e1de210bbd0f6a20945e4b5c334c08f426af17fc Mon Sep 17 00:00:00 2001 From: markjcrane Date: Tue, 25 Sep 2018 18:14:30 +0000 Subject: [PATCH] Add imports --- app/destination_imports/app_config.php | 37 + app/destination_imports/app_languages.php | 1084 +++++++++++++++++ app/destination_imports/app_menu.php | 3 + .../destination_imports.php | 1025 ++++++++++++++++ app/destination_imports/root.php | 90 ++ app/device_imports/app_config.php | 37 + app/device_imports/app_languages.php | 1084 +++++++++++++++++ app/device_imports/app_menu.php | 3 + app/device_imports/device_imports.php | 439 +++++++ app/device_imports/root.php | 90 ++ app/extension_imports/app_config.php | 37 + app/extension_imports/app_languages.php | 1084 +++++++++++++++++ app/extension_imports/app_menu.php | 3 + app/extension_imports/extension_imports.php | 438 +++++++ app/extension_imports/root.php | 90 ++ app/user_imports/app_config.php | 37 + app/user_imports/app_languages.php | 1084 +++++++++++++++++ app/user_imports/app_menu.php | 3 + app/user_imports/root.php | 90 ++ app/user_imports/user_imports.php | 477 ++++++++ 20 files changed, 7235 insertions(+) create mode 100644 app/destination_imports/app_config.php create mode 100644 app/destination_imports/app_languages.php create mode 100644 app/destination_imports/app_menu.php create mode 100644 app/destination_imports/destination_imports.php create mode 100644 app/destination_imports/root.php create mode 100644 app/device_imports/app_config.php create mode 100644 app/device_imports/app_languages.php create mode 100644 app/device_imports/app_menu.php create mode 100644 app/device_imports/device_imports.php create mode 100644 app/device_imports/root.php create mode 100644 app/extension_imports/app_config.php create mode 100644 app/extension_imports/app_languages.php create mode 100644 app/extension_imports/app_menu.php create mode 100644 app/extension_imports/extension_imports.php create mode 100644 app/extension_imports/root.php create mode 100644 app/user_imports/app_config.php create mode 100644 app/user_imports/app_languages.php create mode 100644 app/user_imports/app_menu.php create mode 100644 app/user_imports/root.php create mode 100644 app/user_imports/user_imports.php diff --git a/app/destination_imports/app_config.php b/app/destination_imports/app_config.php new file mode 100644 index 0000000000..1fc2a36c0f --- /dev/null +++ b/app/destination_imports/app_config.php @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/app/destination_imports/app_languages.php b/app/destination_imports/app_languages.php new file mode 100644 index 0000000000..c77d1da397 --- /dev/null +++ b/app/destination_imports/app_languages.php @@ -0,0 +1,1084 @@ + \ No newline at end of file diff --git a/app/destination_imports/app_menu.php b/app/destination_imports/app_menu.php new file mode 100644 index 0000000000..791a01bbb7 --- /dev/null +++ b/app/destination_imports/app_menu.php @@ -0,0 +1,3 @@ + diff --git a/app/destination_imports/destination_imports.php b/app/destination_imports/destination_imports.php new file mode 100644 index 0000000000..b3d0e65dce --- /dev/null +++ b/app/destination_imports/destination_imports.php @@ -0,0 +1,1025 @@ + + Portions created by the Initial Developer are Copyright (C) 2018 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +//includes + include "root.php"; + require_once "resources/require.php"; + require_once "resources/check_auth.php"; + +//check permissions + if (permission_exists('destination_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 = check_str($_POST["action"]); + $order_by = check_str($_POST["order_by"]); + $order = check_str($_POST["order"]); + $from_row = check_str($_POST["from_row"]); + $delimiter = check_str($_POST["data_delimiter"]); + $enclosure = check_str($_POST["data_enclosure"]); + $destination_type = check_str($_POST["destination_type"]); + $destination_action = check_str($_POST["destination_action"]); + $destination_context = check_str($_POST["destination_context"]); + $destination_record = check_str($_POST["destination_record"]); + +//set the defaults + if (strlen($destination_type) == 0) { $destination_type = 'inbound'; } + if (strlen($destination_context) == 0) { $destination_context = 'public'; } + if ($destination_type =="outbound" && $destination_context == "public") { $destination_context = $_SESSION['domain_name']; } + if ($destination_type =="outbound" && strlen($destination_context) == 0) { $destination_context = $_SESSION['domain_name']; } + +//save the data to the csv file + if (isset($_POST['data'])) { + $file = $_SESSION['server']['temp']['dir']."/destinations-".$_SESSION['domain_name'].".csv"; + file_put_contents($file, $_POST['data']); + $_SESSION['file'] = $file; + } + +//copy the csv file + //$_POST['submit'] == "Upload" && + if ( is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('destination_upload')) { + if (check_str($_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 (strlen($delimiter) > 0) { + //get the first line + $line = fgets(fopen($_SESSION['file'], 'r')); + $line_fields = explode($delimiter, $line); + + //get the schema + $x = 0; + include ("app/destinations/app_config.php"); + $i = 0; + foreach($apps[0]['db'] as $table) { + //get the table name and parent name + $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 == "destinations") { + $schema[$i]['table'] = $table_name; + $schema[$i]['parent'] = $parent_name; + foreach($table['fields'] as $row) { + 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++; + } + } + } + +//get the parent table + function get_parent($schema,$table_name) { + foreach ($schema as $row) { + if ($row['table'] == $table_name) { + return $row['parent']; + } + } + } + +//upload the destination csv + if (file_exists($_SESSION['file']) && $action == 'add') { + + //form to match the fields to the column names + //require_once "resources/header.php"; + + //user selected fields + $fields = $_POST['fields']; + $domain_uuid = $_POST['domain_uuid']; + $destination_record = $_POST['destination_record']; + $destination_type = $_POST['destination_type']; + $destination_context = $_POST['destination_context']; + $destination_enabled = $_POST['destination_enabled']; + + //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) { + //pre-set the numbers + $row_number = 1; + $row_id = 0; + + //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]); + } + + //build the data array + if (strlen($table_name) > 0) { + if (strlen($parent) == 0) { + $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]; + } + } + + //get the destination_number + if ($key === 'destination_number') { $destination_number = $result[$key]; } + if ($key === 'destination_description') { $destination_description = $result[$key]; } + if ($key === 'destination_app') { $destination_app = $result[$key]; echo "destination_app $destination_app\n"; } + if ($key === 'destination_data') { $destination_data = $result[$key]; echo "destination_data $destination_data\n"; } + } + + //add the actions + foreach ($array['destinations'] as $row) { + + //get the values + $destination_number = $row['destination_number']; + $destination_app = $row['destination_app']; + $destination_data = $row['destination_data']; + $destination_accountcode = $row['destination_accountcode']; + $destination_cid_name_prefix = $row['destination_cid_name_prefix']; + $destination_description = $row['destination_description']; + + //convert the number to a regular expression + $destination_number_regex = string_to_regex($destination_number); + + //add the additional fields + $dialplan_uuid = uuid(); + $array["destinations"][$row_id]['destination_type'] = $destination_type; + $array["destinations"][$row_id]['destination_record'] = $destination_record; + $array["destinations"][$row_id]['destination_context'] = $destination_context; + $array["destinations"][$row_id]['destination_enabled'] = $destination_enabled; + $array["destinations"][$row_id]['dialplan_uuid'] = $dialplan_uuid; + + //build the dialplan array + $array["dialplans"][$row_id]["app_uuid"] = "c03b422e-13a8-bd1b-e42b-b6b9b4d27ce4"; + $array["dialplans"][$row_id]["dialplan_uuid"] = $dialplan_uuid; + $array["dialplans"][$row_id]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_name"] = ($dialplan_name != '') ? $dialplan_name : format_phone($destination_number); + $array["dialplans"][$row_id]["dialplan_number"] = $destination_number; + $array["dialplans"][$row_id]["dialplan_context"] = $destination_context; + $array["dialplans"][$row_id]["dialplan_continue"] = "false"; + $array["dialplans"][$row_id]["dialplan_order"] = "100"; + $array["dialplans"][$row_id]["dialplan_enabled"] = $destination_enabled; + $array["dialplans"][$row_id]["dialplan_description"] = $destination_description; + $dialplan_detail_order = 10; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + + //set the dialplan detail type + if (strlen($_SESSION['dialplan']['destination']['text']) > 0) { + $dialplan_detail_type = $_SESSION['dialplan']['destination']['text']; + } + else { + $dialplan_detail_type = "destination_number"; + } + + //build the xml dialplan + if ($_SESSION['destinations']['dialplan_details']['boolean'] == "false") { + $array["dialplans"][$row_id]["dialplan_xml"] = "\n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + if (strlen($destination_cid_name_prefix) > 0) { + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + } + if (strlen($destination_record) > 0) { + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + } + if (strlen($destination_accountcode) > 0) { + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + } + if (strlen($destination_carrier) > 0) { + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + } + if (strlen($fax_uuid) > 0) { + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + } + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= " \n"; + $array["dialplans"][$row_id]["dialplan_xml"] .= "\n"; + + } + + //dialplan details + if ($_SESSION['destinations']['dialplan_details']['boolean'] == "true") { + + //check the destination number + $array["dialplans"][$row_id]["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_tag"] = "condition"; + if (strlen($_SESSION['dialplan']['destination']['text']) > 0) { + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = $_SESSION['dialplan']['destination']['text']; + } + else { + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = "destination_number"; + } + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_data"] = $destination_number_regex; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + + //set the caller id name prefix + if (strlen($destination_cid_name_prefix) > 0) { + $array["dialplans"][$row_id]["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_tag"] = "action"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = "set"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_data"] = "effective_caller_id_name=".$destination_cid_name_prefix."#\${caller_id_name}"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + } + + //enable call recordings + if ($destination_record == "true") { + + $array["dialplans"][$row_id]["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_tag"] = "action"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = "answer"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_data"] = ""; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + + $array["dialplans"][$row_id]["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_tag"] = "action"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = "set"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_data"] = "record_path=\${recordings_dir}/\${domain_name}/archive/\${strftime(%Y)}/\${strftime(%b)}/\${strftime(%d)}"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_inline"] = "true"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + + $array["dialplans"][$row_id]["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_tag"] = "action"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = "set"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_data"] = "record_name=\${uuid}.\${record_ext}"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_inline"] = "true"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + + //add a variable + $dialplan["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $dialplan["dialplan_details"][$y]["dialplan_uuid"] = $dialplan_uuid; + $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action"; + $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "set"; + $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "recording_follow_transfer=true"; + $dialplan["dialplan_details"][$y]["dialplan_detail_inline"] = "true"; + $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + + $array["dialplans"][$row_id]["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_tag"] = "action"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = "record_session"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_data"] = "\${record_path}/\${record_name}"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_inline"] = "false"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + } + + //set the call accountcode + if (strlen($destination_accountcode) > 0) { + $array["dialplans"][$row_id]["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_tag"] = "action"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = "set"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_data"] = "accountcode=".$destination_accountcode; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + } + + //set the call accountcode + if (strlen($destination_app) > 0 && strlen($destination_data) > 0) { + $array["dialplans"][$row_id]["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_tag"] = "action"; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_type"] = $destination_app; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_data"] = $destination_data; + $array["dialplans"][$row_id]["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; + $y++; + + //increment the dialplan detail order + $dialplan_detail_order = $dialplan_detail_order + 10; + } + + } //end if + } //foreach + + //process a chunk of the array + if ($row_id === 1000) { + + //save to the data + $database = new database; + $database->app_name = 'destinations'; + $database->app_uuid = '5ec89622-b19c-3559-64f0-afde802ab139'; + $database->save($array); + //$message = $database->message; + + //clear the array + unset($array); + + //set the row id back to 0 + $row_id = 0; + } + + //increment row id + $row_id++; + } + $row_number++; + } + fclose($handle); + + //debug info + //echo "
\n";
+					//print_r($array);
+					//echo "
\n"; + //exit; + + //save to the data + if (is_array($array)) { + $database = new database; + $database->app_name = 'destinations'; + $database->app_uuid = '5ec89622-b19c-3559-64f0-afde802ab139'; + $database->save($array); + $message = $database->message; + } + + //send the redirect header + header("Location: /app/destinations/destinations.php"); + return; + } + + //show the header + require_once "resources/header.php"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "
".$text['header-destinations_import']."\n"; + echo " \n"; + echo "
\n"; + echo " ".$text['message-results']."

\n"; + echo "
\n"; + + //show the results + echo "\n"; + echo "\n"; + echo " \n"; + echo " \n"; + //echo " \n"; + echo " \n"; + echo "\n"; + if ($results) { + foreach($results as $row) { + echo "\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "\n"; + } + } + echo "
".$text['label-destination_name']."".$text['label-destination_organization']."".$text['label-destination_email']."".$text['label-destination_url']."
\n"; + echo $row['FirstName'] ." ".$row['LastName']; + echo " \n"; + echo $row['Company']." \n"; + echo " \n"; + echo $row['EmailAddress']." \n"; + echo " \n"; + echo $row['Web Page']." \n"; + echo "
\n"; + + //include the footer + require_once "resources/footer.php"; + + //end the script + exit; + } + + +//upload the destination csv + if (file_exists($_SESSION['file']) && $action == 'delete') { + + //form to match the fields to the column names + //require_once "resources/header.php"; + + //user selected fields + $fields = $_POST['fields']; + $domain_uuid = $_POST['domain_uuid']; + $destination_type = $_POST['destination_type']; + $destination_context = $_POST['destination_context']; + $destination_enabled = $_POST['destination_enabled']; + + //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; + $dialplan_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]); + } + + //build the data array + if (strlen($table_name) > 0) { + if (strlen($parent) == 0) { + $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]; + } + } + + //get the destination_number + if ($key === 'destination_number') { $destination_number = $result[$key]; } + if ($key === 'destination_uuid') { $destination_uuid = $result[$key]; } + if ($key === 'dialplan_uuid') { $destination_uuid = $result[$key]; } + } + + //delete the destinations + $row_number = 0; + foreach ($array['destinations'] as $row) { + //get the values + $domain_uuid = $row['domain_uuid']; + $destination_number = $row['destination_number']; + + //get the dialplan uuid + if (strlen($row['destination_number']) == 0 || strlen($row['dialplan_uuid']) == 0 ) { + $sql = "select * from v_destinations "; + $sql .= "where domain_uuid = '$domain_uuid' "; + $sql .= "and destination_number = '$destination_number'; "; + //echo $sql."
\n"; + $prep_statement = $db->prepare(check_sql($sql)); + $prep_statement->execute(); + $destinations = $prep_statement->fetchAll(PDO::FETCH_NAMED); + $row = $destinations[0]; + + //add to the array + //$array['destinations'][$row_id] = $destinations[0]; + $array['destinations'][$row_id]['destination_uuid'] = $destinations[0]['destination_uuid']; + if (strlen($row['dialplan_uuid']) > 0) { + $array['destinations'][$row_id]['dialplan_uuid'] = $destinations[0]['dialplan_uuid']; + //$array['dialplans'][$row_id]['dialplan_uuid'] = $destinations[0]['dialplan_uuid']; + } + } + } //foreach + + } //if ($from_row <= $row_number) + $row_number++; + + //process a chunk of the array + if ($row_id === 1000) { + //delete the destinations + $row_number = 0; + foreach ($array['destinations'] as $row) { + //delete the dialplan + if (strlen($row['dialplan_uuid']) > 0) { + $sql = "delete from v_dialplan_details "; + $sql .= "where dialplan_uuid = '".$row['dialplan_uuid']."';"; + //echo "$sql
\n"; + $db->query($sql); + unset($sql); + + $sql = "delete from v_dialplans "; + $sql .= "where dialplan_uuid = '".$row['dialplan_uuid']."';"; + //echo "$sql
\n"; + $db->query($sql); + unset($sql); + } + + //delete the destinations + if (strlen($row['destination_uuid']) > 0) { + $sql = "delete from v_destinations "; + $sql .= "where destination_uuid = '".$row['destination_uuid']."';"; + //echo "$sql
\n"; + $db->query($sql); + unset($sql); + } + } //foreach + + //delete to the data + //$database = new database; + //$database->app_name = 'destinations'; + //$database->app_uuid = '5ec89622-b19c-3559-64f0-afde802ab139'; + //$database->delete($array); + //$message = $database->message; + + //clear the array + unset($array); + + //set the row id back to 0 + $row_id = 0; + } + + //increment row id + $row_id++; + } + fclose($handle); + + //delete the remaining destinations + if ($row_id < 1000) { + foreach ($array['destinations'] as $row) { + //delete the dialplan + if (strlen($row['dialplan_uuid']) > 0) { + $sql = "delete from v_dialplan_details "; + $sql .= "where dialplan_uuid = '".$row['dialplan_uuid']."';"; + //echo "$sql
\n"; + $db->query($sql); + unset($sql); + + $sql = "delete from v_dialplans "; + $sql .= "where dialplan_uuid = '".$row['dialplan_uuid']."';"; + //echo "$sql
\n"; + $db->query($sql); + unset($sql); + } + + //delete the destinations + if (strlen($row['destination_uuid']) > 0) { + $sql = "delete from v_destinations "; + $sql .= "where destination_uuid = '".$row['destination_uuid']."';"; + //echo "$sql
\n"; + $db->query($sql); + unset($sql); + } + } //foreach + } + + //debug info + //echo "
\n";
+					//print_r($array);
+					//echo "
\n"; + //exit; + + //save to the data + //if (is_array($array)) { + // $database = new database; + // $database->app_name = 'destinations'; + // $database->app_uuid = '5ec89622-b19c-3559-64f0-afde802ab139'; + // $database->delete($array); + // //$message = $database->message; + //} + + //send the redirect header + header("Location: /app/destinations/destinations.php"); + return; + } + } + +//match the column names to the field names + if (strlen($delimiter) > 0 && file_exists($_SESSION['file']) && ($action !== 'add' or $action !== 'delete')) { + + //form to match the fields to the column names + require_once "resources/header.php"; + + 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"; + + //loop through user columns + $x = 0; + foreach ($line_fields as $line_field) { + $line_field = trim(trim($line_field), $enclosure); + echo "\n"; + echo "\n"; + echo "\n"; + echo " \n"; + $x++; + } + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + //if (permission_exists('destination_context')) { + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + //} + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + if (permission_exists('destination_domain')) { + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + } + else { + echo "\n"; + } + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo " \n"; + echo " \n"; + echo " \n"; + + echo "
\n"; + echo " ".$text['header-destination_import']."
\n"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " ".$text['description-destination_import']."\n"; + echo "
".$text['header-destinations_import']."\n"; + //echo " \n"; + //echo "
\n"; + //echo " ".$text['label-zzz']."\n"; + echo $line_field; + echo "\n"; + echo " \n"; + //echo "
\n"; + //echo $text['description-zzz']."\n"; + echo "
\n"; + echo " ".$text['label-destination_type']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-destination_type']."\n"; + echo "
\n"; + echo " ".$text['label-destination_record']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-destination_record']."\n"; + echo "
\n"; + echo " ".$text['label-destination_context']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-destination_context']."\n"; + echo "
\n"; + echo " ".$text['label-action']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-action']."\n"; + echo "
\n"; + echo " ".$text['label-domain']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-domain_name']."\n"; + echo "
\n"; + echo " ".$text['label-destination_enabled']."\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo $text['description-destination_enabled']."\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."destination_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", "destination_email", $line); + + //end the script + exit; + } + +//include the header + require_once "resources/header.php"; + +//begin the content + echo "\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " ".$text['header-destination_import']."
\n"; + echo " ".$text['description-destination_import']."\n"; + echo "
\n"; + echo " \n"; + //echo " \n"; + echo "
"; + + 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"; + + if (permission_exists('destination_upload')) { + 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 $text['description-import_file_upload']."\n"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " \n"; + echo "
\n"; + echo "

"; + echo "
"; + +//include the footer + require_once "resources/footer.php"; + +?> \ No newline at end of file diff --git a/app/destination_imports/root.php b/app/destination_imports/root.php new file mode 100644 index 0000000000..6fdf32f37b --- /dev/null +++ b/app/destination_imports/root.php @@ -0,0 +1,90 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +// make sure the PATH_SEPARATOR is defined + umask(2); + if (!defined("PATH_SEPARATOR")) { + if (strpos($_ENV["OS"], "Win") !== false) { + define("PATH_SEPARATOR", ";"); + } else { + define("PATH_SEPARATOR", ":"); + } + } + + if (!isset($output_format)) $output_format = (PHP_SAPI == 'cli') ? 'text' : 'html'; + + // make sure the document_root is set + $_SERVER["SCRIPT_FILENAME"] = str_replace("\\", '/', $_SERVER["SCRIPT_FILENAME"]); + if(PHP_SAPI == 'cli'){ + chdir(pathinfo(realpath($_SERVER["PHP_SELF"]), PATHINFO_DIRNAME)); + $script_full_path = str_replace("\\", '/', getcwd() . '/' . $_SERVER["SCRIPT_FILENAME"]); + $dirs = explode('/', pathinfo($script_full_path, PATHINFO_DIRNAME)); + if (file_exists('/project_root.php')) { + $path = '/'; + } else { + $i = 1; + $path = ''; + while ($i < count($dirs)) { + $path .= '/' . $dirs[$i]; + if (file_exists($path. '/project_root.php')) { + break; + } + $i++; + } + } + $_SERVER["DOCUMENT_ROOT"] = $path; + }else{ + $_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]); + } + $_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]); +// try to detect if a project path is being used + if (!defined('PROJECT_PATH')) { + if (is_dir($_SERVER["DOCUMENT_ROOT"]. '/fusionpbx')) { + define('PROJECT_PATH', '/fusionpbx'); + } elseif (file_exists($_SERVER["DOCUMENT_ROOT"]. '/project_root.php')) { + define('PROJECT_PATH', ''); + } else { + $dirs = explode('/', str_replace('\\', '/', pathinfo($_SERVER["PHP_SELF"], PATHINFO_DIRNAME))); + $i = 1; + $path = $_SERVER["DOCUMENT_ROOT"]; + while ($i < count($dirs)) { + $path .= '/' . $dirs[$i]; + if (file_exists($path. '/project_root.php')) { + break; + } + $i++; + } + if(!file_exists($path. '/project_root.php')){ + die("Failed to locate the Project Root by searching for project_root.php please contact support for assistance"); + } + $project_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $path); + define('PROJECT_PATH', $project_path); + } + $_SERVER["PROJECT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH); + set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER["PROJECT_ROOT"]); + } + +?> \ No newline at end of file diff --git a/app/device_imports/app_config.php b/app/device_imports/app_config.php new file mode 100644 index 0000000000..ac5bda3cc2 --- /dev/null +++ b/app/device_imports/app_config.php @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/app/device_imports/app_languages.php b/app/device_imports/app_languages.php new file mode 100644 index 0000000000..0033b5694f --- /dev/null +++ b/app/device_imports/app_languages.php @@ -0,0 +1,1084 @@ + \ No newline at end of file diff --git a/app/device_imports/app_menu.php b/app/device_imports/app_menu.php new file mode 100644 index 0000000000..791a01bbb7 --- /dev/null +++ b/app/device_imports/app_menu.php @@ -0,0 +1,3 @@ + diff --git a/app/device_imports/device_imports.php b/app/device_imports/device_imports.php new file mode 100644 index 0000000000..e6b9cc332b --- /dev/null +++ b/app/device_imports/device_imports.php @@ -0,0 +1,439 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2018 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +//includes + include "root.php"; + require_once "resources/require.php"; + require_once "resources/check_auth.php"; + +//check permissions + if (permission_exists('device_add')) { + //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 = check_str($_POST["action"]); + $from_row = check_str($_POST["from_row"]); + $order_by = check_str($_POST["order_by"]); + $order = check_str($_POST["order"]); + $delimiter = check_str($_POST["data_delimiter"]); + $enclosure = check_str($_POST["data_enclosure"]); + +//save the data to the csv file + if (isset($_POST['data'])) { + $file = $_SESSION['server']['temp']['dir']."/devices-".$_SESSION['domain_name'].".csv"; + file_put_contents($file, $_POST['data']); + $_SESSION['file'] = $file; + } + +//copy the csv file + //$_POST['submit'] == "Upload" && + if ( is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('device_imports')) { + if (check_str($_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 (strlen($delimiter) > 0) { + //get the first line + $line = fgets(fopen($_SESSION['file'], 'r')); + $line_fields = explode($delimiter, $line); + + //get the schema + $x = 0; + include ("app/devices/app_config.php"); + $i = 0; + foreach($apps[0]['db'] as $table) { + //get the table name and parent name + $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 == "devices" || $table_name == "device_lines" || + $table_name == "device_keys" || $table_name == "device_settings") { + $schema[$i]['table'] = $table_name; + $schema[$i]['parent'] = $parent_name; + foreach($table['fields'] as $row) { + 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++; + } + } + //echo "
\n";
+			//print_r($schema);
+			//echo "
\n"; + //exit; + } + +//match the column names to the field names + if (strlen($delimiter) > 0 && file_exists($_SESSION['file']) && $action != 'import') { + + //form to match the fields to the column names + require_once "resources/header.php"; + + 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"; + + //loop through user columns + $x = 0; + foreach ($line_fields as $line_field) { + $line_field = trim(trim($line_field), $enclosure); + echo "\n"; + echo "\n"; + echo "\n"; + echo " \n"; + $x++; + } + + echo " \n"; + echo " \n"; + echo " \n"; + + echo "
\n"; + echo " ".$text['header-import']."
\n"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " ".$text['description-import']."\n"; + echo "
".$text['header-import']."\n"; + //echo " \n"; + //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') { + + //form to match the fields to the column names + //require_once "resources/header.php"; + + //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]); + } + + //build the data array + if (strlen($table_name) > 0) { + if (strlen($parent) == 0) { + $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 = 'devices'; + $database->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e'; + $database->save($array); + //$message = $database->message; + + //clear the array + unset($array); + + //set the row id back to 0 + $row_id = 0; + } + + } //if ($from_row <= $row_id) + $row_number++; + $row_id++; + } //end while + fclose($handle); + + //debug info + //echo "
\n";
+					//print_r($array);
+					//echo "
\n"; + //exit; + + //save to the data + if (is_array($array)) { + $database = new database; + $database->app_name = 'devices'; + $database->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e'; + $database->save($array); + //$message = $database->message; + } + + //send the redirect header + header("Location: ../devices/devices.php"); + return; + } + } + +//include the header + require_once "resources/header.php"; + +//begin the content + echo "\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " ".$text['header-import']."
\n"; + echo " ".$text['description-import']."\n"; + echo "
\n"; + echo " \n"; + //echo " \n"; + echo "
"; + + 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 " \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 "  \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " \n"; + echo "
\n"; + echo "

"; + echo "
"; + +//include the footer + require_once "resources/footer.php"; + +?> \ No newline at end of file diff --git a/app/device_imports/root.php b/app/device_imports/root.php new file mode 100644 index 0000000000..6fdf32f37b --- /dev/null +++ b/app/device_imports/root.php @@ -0,0 +1,90 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +// make sure the PATH_SEPARATOR is defined + umask(2); + if (!defined("PATH_SEPARATOR")) { + if (strpos($_ENV["OS"], "Win") !== false) { + define("PATH_SEPARATOR", ";"); + } else { + define("PATH_SEPARATOR", ":"); + } + } + + if (!isset($output_format)) $output_format = (PHP_SAPI == 'cli') ? 'text' : 'html'; + + // make sure the document_root is set + $_SERVER["SCRIPT_FILENAME"] = str_replace("\\", '/', $_SERVER["SCRIPT_FILENAME"]); + if(PHP_SAPI == 'cli'){ + chdir(pathinfo(realpath($_SERVER["PHP_SELF"]), PATHINFO_DIRNAME)); + $script_full_path = str_replace("\\", '/', getcwd() . '/' . $_SERVER["SCRIPT_FILENAME"]); + $dirs = explode('/', pathinfo($script_full_path, PATHINFO_DIRNAME)); + if (file_exists('/project_root.php')) { + $path = '/'; + } else { + $i = 1; + $path = ''; + while ($i < count($dirs)) { + $path .= '/' . $dirs[$i]; + if (file_exists($path. '/project_root.php')) { + break; + } + $i++; + } + } + $_SERVER["DOCUMENT_ROOT"] = $path; + }else{ + $_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]); + } + $_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]); +// try to detect if a project path is being used + if (!defined('PROJECT_PATH')) { + if (is_dir($_SERVER["DOCUMENT_ROOT"]. '/fusionpbx')) { + define('PROJECT_PATH', '/fusionpbx'); + } elseif (file_exists($_SERVER["DOCUMENT_ROOT"]. '/project_root.php')) { + define('PROJECT_PATH', ''); + } else { + $dirs = explode('/', str_replace('\\', '/', pathinfo($_SERVER["PHP_SELF"], PATHINFO_DIRNAME))); + $i = 1; + $path = $_SERVER["DOCUMENT_ROOT"]; + while ($i < count($dirs)) { + $path .= '/' . $dirs[$i]; + if (file_exists($path. '/project_root.php')) { + break; + } + $i++; + } + if(!file_exists($path. '/project_root.php')){ + die("Failed to locate the Project Root by searching for project_root.php please contact support for assistance"); + } + $project_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $path); + define('PROJECT_PATH', $project_path); + } + $_SERVER["PROJECT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH); + set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER["PROJECT_ROOT"]); + } + +?> \ No newline at end of file diff --git a/app/extension_imports/app_config.php b/app/extension_imports/app_config.php new file mode 100644 index 0000000000..e380e1753a --- /dev/null +++ b/app/extension_imports/app_config.php @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/app/extension_imports/app_languages.php b/app/extension_imports/app_languages.php new file mode 100644 index 0000000000..0033b5694f --- /dev/null +++ b/app/extension_imports/app_languages.php @@ -0,0 +1,1084 @@ + \ No newline at end of file diff --git a/app/extension_imports/app_menu.php b/app/extension_imports/app_menu.php new file mode 100644 index 0000000000..791a01bbb7 --- /dev/null +++ b/app/extension_imports/app_menu.php @@ -0,0 +1,3 @@ + diff --git a/app/extension_imports/extension_imports.php b/app/extension_imports/extension_imports.php new file mode 100644 index 0000000000..78839b7231 --- /dev/null +++ b/app/extension_imports/extension_imports.php @@ -0,0 +1,438 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2018 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +//includes + include "root.php"; + require_once "resources/require.php"; + require_once "resources/check_auth.php"; + +//check permissions + if (permission_exists('extension_add')) { + //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 = check_str($_POST["action"]); + $from_row = check_str($_POST["from_row"]); + $order_by = check_str($_POST["order_by"]); + $order = check_str($_POST["order"]); + $delimiter = check_str($_POST["data_delimiter"]); + $enclosure = check_str($_POST["data_enclosure"]); + +//save the data to the csv file + if (isset($_POST['data'])) { + $file = $_SESSION['server']['temp']['dir']."/extensions-".$_SESSION['domain_name'].".csv"; + file_put_contents($file, $_POST['data']); + $_SESSION['file'] = $file; + } + +//copy the csv file + //$_POST['submit'] == "Upload" && + if ( is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('extension_imports')) { + if (check_str($_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 (strlen($delimiter) > 0) { + //get the first line + $line = fgets(fopen($_SESSION['file'], 'r')); + $line_fields = explode($delimiter, $line); + + //get the schema + $x = 0; + include ("app/extensions/app_config.php"); + $i = 0; + foreach($apps[0]['db'] as $table) { + //get the table name and parent name + $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 == "extensions") { + $schema[$i]['table'] = $table_name; + $schema[$i]['parent'] = $parent_name; + foreach($table['fields'] as $row) { + 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++; + } + } + //echo "
\n";
+			//print_r($schema);
+			//echo "
\n"; + //exit; + } + +//match the column names to the field names + if (strlen($delimiter) > 0 && file_exists($_SESSION['file']) && $action != 'import') { + + //form to match the fields to the column names + require_once "resources/header.php"; + + 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"; + + //loop through user columns + $x = 0; + foreach ($line_fields as $line_field) { + $line_field = trim(trim($line_field), $enclosure); + echo "\n"; + echo "\n"; + echo "\n"; + echo " \n"; + $x++; + } + + echo " \n"; + echo " \n"; + echo " \n"; + + echo "
\n"; + echo " ".$text['header-import']."
\n"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " ".$text['description-import']."\n"; + echo "
".$text['header-import']."\n"; + //echo " \n"; + //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') { + + //form to match the fields to the column names + //require_once "resources/header.php"; + + //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]); + } + + //build the data array + if (strlen($table_name) > 0) { + if (strlen($parent) == 0) { + $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 = 'extensions'; + $database->app_uuid = 'e68d9689-2769-e013-28fa-6214bf47fca3'; + $database->save($array); + //$message = $database->message; + + //clear the array + unset($array); + + //set the row id back to 0 + $row_id = 0; + } + + } //if ($from_row <= $row_id) + $row_number++; + $row_id++; + } //end while + fclose($handle); + + //debug info + //echo "
\n";
+					//print_r($array);
+					//echo "
\n"; + //exit; + + //save to the data + if (is_array($array)) { + $database = new database; + $database->app_name = 'extensions'; + $database->app_uuid = 'e68d9689-2769-e013-28fa-6214bf47fca3'; + $database->save($array); + //$message = $database->message; + } + + //send the redirect header + header("Location: ../extensions/extensions.php"); + return; + } + } + +//include the header + require_once "resources/header.php"; + +//begin the content + echo "\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " ".$text['header-import']."
\n"; + echo " ".$text['description-import']."\n"; + echo "
\n"; + echo " \n"; + //echo " \n"; + echo "
"; + + 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 " \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 "  \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " \n"; + echo "
\n"; + echo "

"; + echo "
"; + +//include the footer + require_once "resources/footer.php"; + +?> \ No newline at end of file diff --git a/app/extension_imports/root.php b/app/extension_imports/root.php new file mode 100644 index 0000000000..6fdf32f37b --- /dev/null +++ b/app/extension_imports/root.php @@ -0,0 +1,90 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +// make sure the PATH_SEPARATOR is defined + umask(2); + if (!defined("PATH_SEPARATOR")) { + if (strpos($_ENV["OS"], "Win") !== false) { + define("PATH_SEPARATOR", ";"); + } else { + define("PATH_SEPARATOR", ":"); + } + } + + if (!isset($output_format)) $output_format = (PHP_SAPI == 'cli') ? 'text' : 'html'; + + // make sure the document_root is set + $_SERVER["SCRIPT_FILENAME"] = str_replace("\\", '/', $_SERVER["SCRIPT_FILENAME"]); + if(PHP_SAPI == 'cli'){ + chdir(pathinfo(realpath($_SERVER["PHP_SELF"]), PATHINFO_DIRNAME)); + $script_full_path = str_replace("\\", '/', getcwd() . '/' . $_SERVER["SCRIPT_FILENAME"]); + $dirs = explode('/', pathinfo($script_full_path, PATHINFO_DIRNAME)); + if (file_exists('/project_root.php')) { + $path = '/'; + } else { + $i = 1; + $path = ''; + while ($i < count($dirs)) { + $path .= '/' . $dirs[$i]; + if (file_exists($path. '/project_root.php')) { + break; + } + $i++; + } + } + $_SERVER["DOCUMENT_ROOT"] = $path; + }else{ + $_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]); + } + $_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]); +// try to detect if a project path is being used + if (!defined('PROJECT_PATH')) { + if (is_dir($_SERVER["DOCUMENT_ROOT"]. '/fusionpbx')) { + define('PROJECT_PATH', '/fusionpbx'); + } elseif (file_exists($_SERVER["DOCUMENT_ROOT"]. '/project_root.php')) { + define('PROJECT_PATH', ''); + } else { + $dirs = explode('/', str_replace('\\', '/', pathinfo($_SERVER["PHP_SELF"], PATHINFO_DIRNAME))); + $i = 1; + $path = $_SERVER["DOCUMENT_ROOT"]; + while ($i < count($dirs)) { + $path .= '/' . $dirs[$i]; + if (file_exists($path. '/project_root.php')) { + break; + } + $i++; + } + if(!file_exists($path. '/project_root.php')){ + die("Failed to locate the Project Root by searching for project_root.php please contact support for assistance"); + } + $project_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $path); + define('PROJECT_PATH', $project_path); + } + $_SERVER["PROJECT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH); + set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER["PROJECT_ROOT"]); + } + +?> \ No newline at end of file diff --git a/app/user_imports/app_config.php b/app/user_imports/app_config.php new file mode 100644 index 0000000000..dcca502e18 --- /dev/null +++ b/app/user_imports/app_config.php @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/app/user_imports/app_languages.php b/app/user_imports/app_languages.php new file mode 100644 index 0000000000..0033b5694f --- /dev/null +++ b/app/user_imports/app_languages.php @@ -0,0 +1,1084 @@ + \ No newline at end of file diff --git a/app/user_imports/app_menu.php b/app/user_imports/app_menu.php new file mode 100644 index 0000000000..791a01bbb7 --- /dev/null +++ b/app/user_imports/app_menu.php @@ -0,0 +1,3 @@ + diff --git a/app/user_imports/root.php b/app/user_imports/root.php new file mode 100644 index 0000000000..6fdf32f37b --- /dev/null +++ b/app/user_imports/root.php @@ -0,0 +1,90 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +// make sure the PATH_SEPARATOR is defined + umask(2); + if (!defined("PATH_SEPARATOR")) { + if (strpos($_ENV["OS"], "Win") !== false) { + define("PATH_SEPARATOR", ";"); + } else { + define("PATH_SEPARATOR", ":"); + } + } + + if (!isset($output_format)) $output_format = (PHP_SAPI == 'cli') ? 'text' : 'html'; + + // make sure the document_root is set + $_SERVER["SCRIPT_FILENAME"] = str_replace("\\", '/', $_SERVER["SCRIPT_FILENAME"]); + if(PHP_SAPI == 'cli'){ + chdir(pathinfo(realpath($_SERVER["PHP_SELF"]), PATHINFO_DIRNAME)); + $script_full_path = str_replace("\\", '/', getcwd() . '/' . $_SERVER["SCRIPT_FILENAME"]); + $dirs = explode('/', pathinfo($script_full_path, PATHINFO_DIRNAME)); + if (file_exists('/project_root.php')) { + $path = '/'; + } else { + $i = 1; + $path = ''; + while ($i < count($dirs)) { + $path .= '/' . $dirs[$i]; + if (file_exists($path. '/project_root.php')) { + break; + } + $i++; + } + } + $_SERVER["DOCUMENT_ROOT"] = $path; + }else{ + $_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]); + } + $_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]); +// try to detect if a project path is being used + if (!defined('PROJECT_PATH')) { + if (is_dir($_SERVER["DOCUMENT_ROOT"]. '/fusionpbx')) { + define('PROJECT_PATH', '/fusionpbx'); + } elseif (file_exists($_SERVER["DOCUMENT_ROOT"]. '/project_root.php')) { + define('PROJECT_PATH', ''); + } else { + $dirs = explode('/', str_replace('\\', '/', pathinfo($_SERVER["PHP_SELF"], PATHINFO_DIRNAME))); + $i = 1; + $path = $_SERVER["DOCUMENT_ROOT"]; + while ($i < count($dirs)) { + $path .= '/' . $dirs[$i]; + if (file_exists($path. '/project_root.php')) { + break; + } + $i++; + } + if(!file_exists($path. '/project_root.php')){ + die("Failed to locate the Project Root by searching for project_root.php please contact support for assistance"); + } + $project_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $path); + define('PROJECT_PATH', $project_path); + } + $_SERVER["PROJECT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH); + set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER["PROJECT_ROOT"]); + } + +?> \ No newline at end of file diff --git a/app/user_imports/user_imports.php b/app/user_imports/user_imports.php new file mode 100644 index 0000000000..35e0b14da3 --- /dev/null +++ b/app/user_imports/user_imports.php @@ -0,0 +1,477 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2018 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +//includes + include "root.php"; + require_once "resources/require.php"; + require_once "resources/check_auth.php"; + +//check permissions + if (permission_exists('user_add')) { + //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 = check_str($_POST["action"]); + $from_row = check_str($_POST["from_row"]); + $order_by = check_str($_POST["order_by"]); + $order = check_str($_POST["order"]); + $delimiter = check_str($_POST["data_delimiter"]); + $enclosure = check_str($_POST["data_enclosure"]); + +//save the data to the csv file + if (isset($_POST['data'])) { + $file = $_SESSION['server']['temp']['dir']."/users-".$_SESSION['domain_name'].".csv"; + file_put_contents($file, $_POST['data']); + $_SESSION['file'] = $file; + } + +//copy the csv file + //$_POST['submit'] == "Upload" && + if ( is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('user_imports')) { + if (check_str($_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 (strlen($delimiter) > 0) { + //get the first line + $line = fgets(fopen($_SESSION['file'], 'r')); + $line_fields = explode($delimiter, $line); + + //get the schema + $x = 0; + include ("core/users/app_config.php"); + $i = 0; + foreach($apps[0]['db'] as $table) { + //get the table name and parent name + $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 == "users") { + $schema[$i]['table'] = $table_name; + $schema[$i]['parent'] = $parent_name; + foreach($table['fields'] as $row) { + 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++; + } + } + //echo "
\n";
+			//print_r($schema);
+			//echo "
\n"; + //exit; + } + +//match the column names to the field names + if (strlen($delimiter) > 0 && file_exists($_SESSION['file']) && $action != 'import') { + + //form to match the fields to the column names + require_once "resources/header.php"; + + 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"; + + //loop through user columns + $x = 0; + foreach ($line_fields as $line_field) { + $line_field = trim(trim($line_field), $enclosure); + echo "\n"; + echo "\n"; + echo "\n"; + echo " \n"; + $x++; + } + + echo " \n"; + echo " \n"; + echo " \n"; + + echo "
\n"; + echo " ".$text['header-import']."
\n"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " ".$text['description-import']."\n"; + echo "
".$text['header-import']."\n"; + //echo " \n"; + //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') { + + //form to match the fields to the column names + //require_once "resources/header.php"; + + //user selected fields + $fields = $_POST['fields']; + + //set the domain_uuid + $domain_uuid = $_SESSION['domain_uuid']; + + //get the user group_uuid + $sql = "select group_uuid from v_groups "; + $sql .= "where group_name = 'user' "; + $prep_statement = $db->prepare($sql); + + if ($prep_statement) { + $prep_statement->execute(); + $row = $prep_statement->fetch(PDO::FETCH_ASSOC); + $group_uuid = $row['group_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); + + //clean the phone number + //if ($field_name == "phone") { + // $result[$key] = preg_replace('{\D}', '', $result[$key]); + //} + + //build the data array + if (strlen($table_name) > 0) { + if (strlen($parent) == 0) { + $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]; + } + } + } + + //get the password, salt and hash the user password + $password = $array['users'][$row_id]['password']; + if (isset($array['users'][$row_id]['salt'])) { + $salt = $array['users'][$row_id]['salt']; + } + else { + $salt = uuid(); + $array['users'][$row_id]['salt'] = $salt; + } + $array['users'][$row_id]['password'] = md5($salt.$password); + + //set the user_uuid + $user_uuid = uuid(); + $array['users'][$row_id]['user_uuid'] = $user_uuid; + + //add the users to the users group + $array['group_users'][$row_id]['group_user_uuid'] = uuid(); + $array['group_users'][$row_id]['domain_uuid'] = $domain_uuid; + $array['group_users'][$row_id]['group_name'] = 'user'; + $array['group_users'][$row_id]['group_uuid'] = $group_uuid; + $array['group_users'][$row_id]['user_uuid'] = $user_uuid; + + //debug + //echo "
\n";
+								//print_r($array);
+								//echo "
\n"; + //exit; + + //process a chunk of the array + if ($row_id === 1000) { + + //save to the data + $database = new database; + $database->app_name = 'users'; + $database->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e'; + $database->save($array); + //$message = $database->message; + + //clear the array + unset($array); + + //set the row id back to 0 + $row_id = 0; + } + + } //if ($from_row <= $row_id) + $row_number++; + $row_id++; + } //end while + fclose($handle); + + //debug info + //echo "
\n";
+					//print_r($array);
+					//echo "
\n"; + //exit; + + //save to the data + if (is_array($array)) { + $database = new database; + $database->app_name = 'users'; + $database->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e'; + $database->save($array); + //$message = $database->message; + } + + //send the redirect header + header("Location: ../users/users.php"); + return; + } + } + +//include the header + require_once "resources/header.php"; + +//begin the content + echo "\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " ".$text['header-import']."
\n"; + echo " ".$text['description-import']."\n"; + echo "
\n"; + echo " \n"; + //echo " \n"; + echo "
"; + + 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 " \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 "  \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " \n"; + echo "
\n"; + echo "

"; + echo "
"; + +//include the footer + require_once "resources/footer.php"; + +?> \ No newline at end of file