fusionpbx/core/users/user_imports.php

502 lines
16 KiB
PHP
Raw Normal View History

2018-09-25 20:14:30 +02:00
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2020
2018-09-25 20:14:30 +02:00
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
*/
//includes
require_once "root.php";
2018-09-25 20:14:30 +02:00
require_once "resources/require.php";
require_once "resources/check_auth.php";
//check permissions
2018-09-28 18:55:20 +02:00
if (permission_exists('user_import')) {
2018-09-25 20:14:30 +02:00
//access granted
}
else {
echo "access denied";
exit;
}
//add multi-lingual support
$language = new text;
$text = $language->get();
2019-07-19 23:05:18 +02:00
//built in str_getcsv requires PHP 5.3 or higher, this function can be used to reproduce the functionality but requires PHP 5.1.0 or higher
if (!function_exists('str_getcsv')) {
2018-09-25 20:14:30 +02:00
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;
}
}
//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"];
2018-09-25 20:14:30 +02:00
//save the data to the csv file
if (isset($_POST['data'])) {
2020-10-20 21:14:34 +02:00
$file = $_SESSION['server']['temp']['dir'].'/users-'.$_SESSION['domain_name'].'.csv';
if (file_put_contents($file, $_POST['data'])) {
$_SESSION['file'] = $file;
}
2018-09-25 20:14:30 +02:00
}
//copy the csv file
//$_POST['submit'] == "Upload" &&
2020-10-20 21:14:34 +02:00
if (is_uploaded_file($_FILES['ulfile']['tmp_name']) && permission_exists('user_import')) {
if ($_POST['type'] == 'csv') {
2020-10-20 21:14:34 +02:00
$file = $_SESSION['server']['temp']['dir'].'/users-'.$_SESSION['domain_name'].'.csv';
if (move_uploaded_file($_FILES['ulfile']['tmp_name'], $file)) {
$_SESSION['file'] = $file;
}
2018-09-25 20:14:30 +02:00
}
}
//get the schema
2020-10-20 21:14:34 +02:00
if (strlen($delimiter) > 0 && file_exists($_SESSION['file'])) {
2018-09-25 20:14:30 +02:00
//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) {
2018-09-25 20:14:30 +02:00
//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'];
}
2018-09-25 20:14:30 +02:00
$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++;
}
}
2019-02-09 18:36:38 +01:00
$schema[$i]['table'] = 'user_groups';
2018-10-19 03:21:32 +02:00
$schema[$i]['parent'] = 'users';
$schema[$i]['fields'][] = 'group_name';
//debug info
2019-07-19 23:05:18 +02:00
//view_array($schema);
2018-09-25 20:14:30 +02:00
}
//match the column names to the field names
if (strlen($delimiter) > 0 && file_exists($_SESSION['file']) && $action != 'import') {
//create token
$object = new token;
$token = $object->create($_SERVER['PHP_SELF']);
//include header
2020-01-06 18:22:13 +01:00
$document['title'] = $text['title-user_import'];
2018-09-25 20:14:30 +02:00
require_once "resources/header.php";
//form to match the fields to the column names
2020-01-08 21:27:07 +01:00
echo "<form name='frmUpload' method='post' enctype='multipart/form-data'>\n";
2018-09-25 20:14:30 +02:00
echo "<div class='action_bar' id='action_bar'>\n";
echo " <div class='heading'><b>".$text['header-user_import']."</b></div>\n";
echo " <div class='actions'>\n";
echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'user_imports.php']);
echo button::create(['type'=>'submit','label'=>$text['button-import'],'icon'=>$_SESSION['theme']['button_icon_import'],'id'=>'btn_save']);
echo " </div>\n";
echo " <div style='clear: both;'></div>\n";
echo "</div>\n";
echo $text['description-import']."\n";
echo "<br /><br />\n";
echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
2018-09-25 20:14:30 +02:00
//loop through user columns
$x = 0;
foreach ($line_fields as $line_field) {
$line_field = trim(trim($line_field), $enclosure);
echo "<tr>\n";
echo " <td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
2018-09-25 20:14:30 +02:00
//echo " ".$text['label-zzz']."\n";
echo $line_field;
echo " </td>\n";
echo " <td width='70%' class='vtable' align='left'>\n";
echo " <select class='formfld' style='' name='fields[$x]'>\n";
echo " <option value=''></option>\n";
2018-09-25 20:14:30 +02:00
foreach($schema as $row) {
echo " <optgroup label='".$row['table']."'>\n";
foreach($row['fields'] as $field) {
2018-09-28 08:58:06 +02:00
$selected = '';
if ($field == $line_field) {
$selected = "selected='selected'";
}
if ($field !== 'domain_uuid') {
echo " <option value='".$row['table'].".".$field."' ".$selected.">".$field."</option>\n";
2018-09-25 20:14:30 +02:00
}
}
echo " </optgroup>\n";
}
echo " </select>\n";
2018-09-25 20:14:30 +02:00
//echo "<br />\n";
//echo $text['description-zzz']."\n";
echo " </td>\n";
echo "</tr>\n";
2018-09-25 20:14:30 +02:00
$x++;
}
echo "</table>\n";
echo "<br /><br />\n";
echo "<input name='action' type='hidden' value='import'>\n";
echo "<input name='from_row' type='hidden' value='$from_row'>\n";
echo "<input name='data_delimiter' type='hidden' value='$delimiter'>\n";
echo "<input name='data_enclosure' type='hidden' value='$enclosure'>\n";
echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
2018-09-25 20:14:30 +02:00
echo "</form>\n";
2018-09-25 20:14:30 +02:00
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: users.php');
exit;
}
2018-09-25 20:14:30 +02:00
//form to match the fields to the column names
2020-01-06 18:22:13 +01:00
//$document['title'] = $text['title-user_import'];
2018-09-25 20:14:30 +02:00
//require_once "resources/header.php";
//user selected fields
$fields = $_POST['fields'];
//set the domain_uuid
$domain_uuid = $_SESSION['domain_uuid'];
2018-10-18 20:16:23 +02:00
//get the groups
$sql = "select * from v_groups where domain_uuid is null ";
2019-07-19 23:05:18 +02:00
$database = new database;
$groups = $database->select($sql, null, 'all');
unset($sql);
2018-09-25 20:14:30 +02:00
//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) {
2018-10-18 20:16:23 +02:00
//get the user_uuid
$user_uuid = uuid();
2018-09-25 20:14:30 +02:00
//format the data
$y = 0;
foreach ($fields as $key => $value) {
2018-10-18 20:16:23 +02:00
2018-09-25 20:14:30 +02:00
//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<br />\n";
//echo "table_name: $table_name<br />\n";
//echo "field_name: $field_name<br />\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 {
2018-10-19 03:21:32 +02:00
if ($field_name != "group_name") {
$array[$parent][$row_id][$table_name][$y]['domain_uuid'] = $domain_uuid;
$array[$parent][$row_id][$table_name][$y][$field_name] = $result[$key];
}
2018-09-25 20:14:30 +02:00
}
2018-10-18 20:16:23 +02:00
2018-10-19 03:21:32 +02:00
if ($field_name == "group_name") {
2018-10-20 00:58:36 +02:00
$group_name = '';
2018-10-18 20:16:23 +02:00
foreach ($groups as $field) {
if ($field['group_name'] == $result[$key]) {
2018-10-20 00:58:36 +02:00
$group_name = $field['group_name'];
2019-02-09 18:36:38 +01:00
$array['user_groups'][$row_id]['user_group_uuid'] = uuid();
$array['user_groups'][$row_id]['domain_uuid'] = $domain_uuid;
$array['user_groups'][$row_id]['group_name'] = $field['group_name'];
$array['user_groups'][$row_id]['group_uuid'] = $field['group_uuid'];
$array['user_groups'][$row_id]['user_uuid'] = $user_uuid;
2018-10-18 20:16:23 +02:00
}
}
2018-10-20 00:58:36 +02:00
//remove superadmin if not the correct permission
if ($group_name == 'superadmin') {
if (!permission_exists('group_domain')) {
2019-02-09 18:36:38 +01:00
unset($array['user_groups'][$row_id]);
2018-10-20 00:58:36 +02:00
}
}
2018-10-18 20:16:23 +02:00
}
2018-09-25 20:14:30 +02:00
}
}
2020-03-07 03:44:03 +01:00
//set the password hash cost
$options = array('cost' => 10);
//set the hash the user password
2018-09-25 20:14:30 +02:00
$password = $array['users'][$row_id]['password'];
2020-03-07 03:44:03 +01:00
$array['users'][$row_id]['password'] = password_hash($password, PASSWORD_DEFAULT, $options);
2018-09-25 20:14:30 +02:00
2018-10-19 03:21:32 +02:00
//set the user_uuid
2018-09-25 20:14:30 +02:00
$array['users'][$row_id]['user_uuid'] = $user_uuid;
2018-10-19 03:21:32 +02:00
//debug
2018-09-25 20:14:30 +02:00
//echo "<pre>\n";
//print_r($array);
//echo "</pre>\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 "<pre>\n";
//print_r($array);
//echo "</pre>\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;
2019-07-19 23:05:18 +02:00
unset($array);
2018-09-25 20:14:30 +02:00
}
//send the redirect header
2018-09-28 18:46:03 +02:00
header("Location: users.php");
2018-09-25 20:14:30 +02:00
return;
}
}
//create token
$object = new token;
$token = $object->create($_SERVER['PHP_SELF']);
2018-09-25 20:14:30 +02:00
//include the header
2020-01-06 18:22:13 +01:00
$document['title'] = $text['title-user_import'];
2018-09-25 20:14:30 +02:00
require_once "resources/header.php";
//show content
echo "<form name='frmUpload' method='post' enctype='multipart/form-data'>\n";
2018-09-25 20:14:30 +02:00
echo "<div class='action_bar' id='action_bar'>\n";
echo " <div class='heading'><b>".$text['header-user_import']."</b></div>\n";
echo " <div class='actions'>\n";
echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'users.php']);
echo button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>$_SESSION['theme']['button_icon_upload'],'id'=>'btn_save']);
echo " </div>\n";
echo " <div style='clear: both;'></div>\n";
echo "</div>\n";
echo $text['description-import']."\n";
echo "<br /><br />\n";
2018-09-25 20:14:30 +02:00
echo "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n";
2018-09-25 20:14:30 +02:00
echo "<tr>\n";
echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
2018-09-25 20:14:30 +02:00
echo " ".$text['label-import_data']."\n";
echo "</td>\n";
echo "<td width='70%' class='vtable' align='left'>\n";
echo " <textarea name='data' id='data' class='formfld' style='width: 100%; min-height: 150px;' wrap='off'>$data</textarea>\n";
2018-09-25 20:14:30 +02:00
echo "<br />\n";
echo $text['description-import_data']."\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-from_row']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <select class='formfld' name='from_row'>\n";
$i=2;
2018-09-25 20:14:30 +02:00
while($i<=99) {
$selected = ($i == $from_row) ? "selected" : null;
echo " <option value='$i' ".$selected.">$i</option>\n";
$i++;
}
echo " </select>\n";
echo "<br />\n";
echo $text['description-from_row']."\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-import_delimiter']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <select class='formfld' style='width:40px;' name='data_delimiter'>\n";
echo " <option value=','>,</option>\n";
echo " <option value='|'>|</option>\n";
echo " </select>\n";
echo "<br />\n";
echo $text['description-import_delimiter']."\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-import_enclosure']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <select class='formfld' style='width:40px;' name='data_enclosure'>\n";
echo " <option value='\"'>\"</option>\n";
echo " <option value=''></option>\n";
echo " </select>\n";
echo "<br />\n";
echo $text['description-import_enclosure']."\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-import_file_upload']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <input name='ulfile' type='file' class='formfld fileinput' id='ulfile'>\n";
echo "<br />\n";
echo "</td>\n";
echo "</tr>\n";
echo " <tr>\n";
echo " <td valign='bottom'>\n";
echo " &nbsp;\n";
echo " </td>\n";
echo " <td valign='bottom' align='right' nowrap>\n";
echo " <input name='type' type='hidden' value='csv'>\n";
echo " <input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
2018-09-25 20:14:30 +02:00
echo " </td>\n";
echo " </tr>\n";
echo " </table>\n";
echo "<br><br>";
echo "</form>";
//include the footer
require_once "resources/footer.php";
2020-03-07 03:44:03 +01:00
?>