Portions created by the Initial Developer are Copyright (C) 2008-2023 the Initial Developer. All Rights Reserved. Contributor(s): Mark J Crane */ //set the include path $conf = glob("{/usr/local/etc,/etc}/fusionpbx/config.conf", GLOB_BRACE); set_include_path(parse_ini_file($conf[0])['document.root']); //includes files require_once "resources/require.php"; require_once "resources/check_auth.php"; //check permissions if (permission_exists('device_export')) { //access granted } else { echo "access denied"; exit; } //add multi-lingual support $language = new text; $text = $language->get(); //define the functions function array2csv(array &$array) { if (count($array) == 0) { return null; } ob_start(); $df = fopen("php://output", 'w'); fputcsv($df, array_keys(reset($array))); foreach ($array as $row) { fputcsv($df, $row); } fclose($df); return ob_get_clean(); } function download_send_headers($filename) { // disable caching $now = gmdate("D, d M Y H:i:s"); header("Expires: Tue, 03 Jul 2001 06:00:00 GMT"); header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); header("Last-Modified: {$now} GMT"); // force download header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); // disposition / encoding on response body header("Content-Disposition: attachment;filename={$filename}"); header("Content-Transfer-Encoding: binary"); } //define possible columns in the array $available_columns['devices'][] = 'device_uuid'; $available_columns['devices'][] = 'device_profile_uuid'; $available_columns['devices'][] = 'device_mac_address'; $available_columns['devices'][] = 'device_label'; $available_columns['devices'][] = 'device_vendor'; $available_columns['devices'][] = 'device_template'; $available_columns['devices'][] = 'device_enabled_date'; $available_columns['devices'][] = 'device_username'; $available_columns['devices'][] = 'device_password'; $available_columns['devices'][] = 'device_uuid_alternate'; $available_columns['devices'][] = 'device_provisioned_date'; $available_columns['devices'][] = 'device_provisioned_method'; $available_columns['devices'][] = 'device_provisioned_ip'; $available_columns['devices'][] = 'device_enabled'; $available_columns['devices'][] = 'device_description'; $available_columns['device_lines'][] = 'device_line_uuid'; $available_columns['device_lines'][] = 'device_uuid'; $available_columns['device_lines'][] = 'line_number'; $available_columns['device_lines'][] = 'server_address'; $available_columns['device_lines'][] = 'server_address_primary'; $available_columns['device_lines'][] = 'server_address_secondary'; $available_columns['device_lines'][] = 'outbound_proxy_primary'; $available_columns['device_lines'][] = 'outbound_proxy_secondary'; $available_columns['device_lines'][] = 'display_name'; $available_columns['device_lines'][] = 'user_id'; $available_columns['device_lines'][] = 'auth_id'; $available_columns['device_lines'][] = 'password'; $available_columns['device_lines'][] = 'sip_port'; $available_columns['device_lines'][] = 'sip_transport'; $available_columns['device_lines'][] = 'register_expires'; $available_columns['device_lines'][] = 'shared_line'; $available_columns['device_lines'][] = 'enabled'; //get the devices and send them as output $column_group = $_REQUEST["column_group"] ?? null; if (is_array($column_group) && @sizeof($column_group) != 0) { //validate the token $token = new token; if (!$token->validate($_SERVER['PHP_SELF'])) { message::add($text['message-invalid_token'],'negative'); header('Location: device_download.php'); exit; } //validate table names foreach($column_group as $table_name => $columns) { if (!isset($available_columns[$table_name])) { unset($column_group[$table_name]); } } //validate columns foreach($column_group as $table_name => $columns) { foreach ($columns as $column_name) { if (!in_array($column_name, $available_columns[$table_name])) { unset($column_group[$table_name][$column_name]); } } } //iterate columns if (is_array($column_group) && @sizeof($column_group) != 0) { $column_names = implode(", ", $column_group['devices']); $sql = "select ".$column_names." from v_devices "; $sql .= " where domain_uuid = :domain_uuid "; $parameters['domain_uuid'] = $domain_uuid; $database = new database; $devices = $database->select($sql, $parameters, 'all'); unset($sql, $parameters, $column_names); foreach($column_group as $table_name => $columns) { if ($table_name !== 'devices') { $column_names = implode(", ", $columns); $sql = "select ".$column_names." from v_".$table_name." "; $sql .= " where domain_uuid = :domain_uuid "; $parameters['domain_uuid'] = $domain_uuid; $database = new database; $$table_name = $database->select($sql, $parameters, 'all'); $x = 0; foreach($devices as $device) { foreach($$table_name as $row) { if ($device['device_uuid'] == $row['device_uuid']) { foreach($row as $key => $value) { $devices[$x][$key] = $value; } break; } } $x++; } unset($sql, $parameters, $column_names); } } if (is_array($devices) && @sizeof($devices) != 0) { download_send_headers("device_export_".date("Y-m-d").".csv"); echo array2csv($devices); exit; } } unset($column_group); } //create token $object = new token; $token = $object->create($_SERVER['PHP_SELF']); //include the header $document['title'] = $text['title-device_export']; require_once "resources/header.php"; //show the content echo "
\n"; echo "
\n"; echo "
".$text['header-device_export']."
\n"; echo "
\n"; echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','link'=>'devices.php']); echo button::create(['type'=>'submit','label'=>$text['button-export'],'icon'=>$_SESSION['theme']['button_icon_export'],'id'=>'btn_save','style'=>'margin-left: 15px;']); echo "
\n"; echo "
\n"; echo "
\n"; echo $text['description-device_export']; echo "

\n"; if (is_array($available_columns) && @sizeof($available_columns) != 0) { $x = 0; foreach ($available_columns as $table_name => $columns) { $table_name_label = ucwords(str_replace(['-','_',],' ', $table_name)); echo "
\n"; echo "".$table_name_label."\n"; echo "
\n"; echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo "\n"; foreach ($columns as $column_name) { $list_row_onclick = "if (!this.checked) { document.getElementById('checkbox_all').checked = false; }"; echo "\n"; echo " \n"; echo " "; echo ""; $x++; } echo "
\n"; echo " \n"; echo " ".$text['label-column_name']."
\n"; echo " \n"; echo " ".$column_name."
\n"; echo "
\n"; echo "
\n"; } } //test the validation //echo " \n"; //echo " \n"; //echo " \n"; echo "\n"; echo "
\n"; //include the footer require_once "resources/footer.php"; ?>