/** * Bitrix Framework * @package bitrix * @subpackage main * @copyright 2001-2013 Bitrix */ /** * Bitrix vars * @global CUser $USER * @global CMain $APPLICATION * @global CDatabase $DB */ require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php"); require_once($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/prolog.php"); require_once($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/classes/general/csv_user_import.php"); if(!$USER->CanDoOperation('edit_php')) $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED")); //Download sample $filename = $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/admin/sample.csv"; if(isset($_REQUEST["getSample"]) && $_REQUEST["getSample"] == "csv" && is_file($filename)) { $file = @fopen($filename, "rb"); $contents = @fread($file, filesize($filename)); fclose($file); header("Content-Type: application/octet-stream"); header("Content-Length: ".mb_strlen($contents)); header("Content-Disposition: attachment; filename=\"sample.csv\""); header("Expires: 0"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); echo $contents; die(); } set_time_limit(0); define("HELP_FILE", "users/user_import.php"); IncludeModuleLangFile(__FILE__); $ldapExists = CModule::IncludeModule("ldap"); $importFrom1C = IsModuleInstalled("iblock") && IsModuleInstalled("intranet"); $arDelimeters = array( "semicolon" => ";", "comma" => ",", "tabulation" => "\t", "space" => " ", ); define("USER_IMPORT_EXECUTION_TIME", 20); //Params $strError = false; $dataSource = (isset($_REQUEST["dataSource"]) && in_array($_REQUEST["dataSource"], array("ldap", "1c")) ? $_REQUEST["dataSource"] : "csv"); $csvDataFile = ($_REQUEST["csvDataFile"] ?? ""); $delimeter = (isset($_REQUEST["delimeter"]) && array_key_exists($_REQUEST["delimeter"], $arDelimeters) ? $_REQUEST["delimeter"] : "semicolon"); $sendEmail = (isset($_REQUEST["sendEmail"]) && $_REQUEST["sendEmail"] == "Y" ? "Y" : "N"); $eventLangID = ($_REQUEST["eventLangID"] ?? ""); $eventLdapLangID = ($_REQUEST["eventLdapLangID"] ?? ""); $userGroups = (isset($_REQUEST["userGroups"]) && is_array($_REQUEST["userGroups"]) ? $_REQUEST["userGroups"] : array()); $ignoreDuplicate = (isset($_REQUEST["ignoreDuplicate"]) && $_REQUEST["ignoreDuplicate"] == "Y" ? "Y" : "N"); $pathToImages = ($_REQUEST["pathToImages"] ?? ""); $ldapServer = (isset($_REQUEST["ldapServer"]) ? intval($_REQUEST["ldapServer"]) : 0); $attachIBlockID = (isset($_REQUEST["attachIBlockID"]) && intval($_REQUEST["attachIBlockID"]) > 0 ? intval($_REQUEST["attachIBlockID"]) : 0); $create1cUser = (isset($_REQUEST["create1cUser"]) && $_REQUEST["create1cUser"] == "Y" ? "Y" : "N"); $newUserLogin = (isset($_REQUEST["newUserLogin"]) && $_REQUEST["newUserLogin"] <> '' ? $_REQUEST["newUserLogin"] : ""); $newUserPass = (isset($_REQUEST["newUserPass"]) && $_REQUEST["newUserPass"] <> '' ? $_REQUEST["newUserPass"] : ""); $newUserConfirmPass = (isset($_REQUEST["newUserConfirmPass"]) && $_REQUEST["newUserConfirmPass"] <> '' ? $_REQUEST["newUserConfirmPass"] : ""); $newUserEmail = (isset($_REQUEST["newUserEmail"]) && $_REQUEST["newUserEmail"] <> '' ? $_REQUEST["newUserEmail"] : ""); $newUserGroups = isset($_REQUEST['newUserGroups']) && is_array($_REQUEST['newUserGroups']) ? $_REQUEST['newUserGroups'] : array(); //Step $tabStep = (isset($_REQUEST["tabStep"]) && intval($_REQUEST["tabStep"]) > 1 ? intval($_REQUEST["tabStep"]) : 1); if (isset($_REQUEST["backButton"])) $tabStep = $tabStep - 2; else if (isset($_REQUEST["backToStart"])) $tabStep = 1; if (!$ldapExists && $dataSource == "ldap") $tabStep = 1; if (!$importFrom1C && $dataSource == "1c") $tabStep = 1; //Functions $cntUsersImport = 0; $defaultUserEmail = ""; function _OnUserAdd(&$arFields, &$userID) { $GLOBALS["cntUsersImport"]++; unset($arFields["GROUP_ID"]); $arFields["ID"] = $arFields["USER_ID"] = $userID; $arFields["URL_LOGIN"] = urlencode($arFields["LOGIN"]); if (isset($arFields["EXTERNAL_AUTH_ID"]) && $arFields["EXTERNAL_AUTH_ID"] <> '' && $GLOBALS["eventLdapLangID"] <> '') { $arFields["BACK_URL"] = "/"; $event = new CEvent; $event->Send("LDAP_USER_CONFIRM", $GLOBALS["eventLdapLangID"], $arFields); } elseif ($GLOBALS["sendEmail"] == "Y" && $arFields["EMAIL"] <> '' && $arFields["EMAIL"] <> $GLOBALS["defaultUserEmail"] && $GLOBALS["eventLangID"] <> '') { $event = new CEvent; $event->Send("USER_INVITE", $GLOBALS["eventLangID"], $arFields); } } //Check and save data if ($_SERVER["REQUEST_METHOD"] == "POST" && $tabStep > 2 && check_bitrix_sessid()) { //Check $csvImport = false; $ldp = false; if ($dataSource == "csv") { $csvFilePath = $_SERVER["DOCUMENT_ROOT"].Rel2Abs("/", $csvDataFile); if (is_file($csvFilePath) && is_readable($csvFilePath)) { $csvImport = new CSVUserImport($csvFilePath, $arDelimeters[$delimeter]); $csvImport->SetUserGroups($userGroups); $csvImport->IgnoreDuplicate($ignoreDuplicate == "Y"); $csvImport->SetCallback("_OnUserAdd"); $csvImport->SetImageFilePath($pathToImages); $csvImport->AttachUsersToIBlock($attachIBlockID); $defaultUserEmail = $csvImport->GetDefaultEmail(); if ($ldapServer > 0 && $ldapExists) { $dbLdap = CLdapServer::GetByID($ldapServer); if ($dbLdap->Fetch()) $csvImport->externalAuthID = "LDAP#".$ldapServer; } if ($csvImport->IsErrorOccured()) $strError = $csvImport->GetErrorMessage(); } else $strError = GetMessage("USER_IMPORT_CSV_NOT_FOUND"); } elseif ($dataSource == "ldap") { $dbLdap = CLdapServer::GetByID($ldapServer); if ($arLdap = $dbLdap->Fetch()) { // this is a test connection, thus any parameters other than related to establishing a connection, have no effect here $ldap = new CLDAP(); $ldap->arFields = array( "SERVER" => $arLdap['SERVER'], "PORT" => $arLdap['PORT'], "ADMIN_LOGIN" => $arLdap['ADMIN_LOGIN'], "ADMIN_PASSWORD" => $arLdap['ADMIN_PASSWORD'], "BASE_DN" => $arLdap['BASE_DN'], "GROUP_FILTER" => $arLdap['GROUP_FILTER'], "GROUP_ID_ATTR" => $arLdap['GROUP_ID_ATTR'], "USER_GROUP_ACCESSORY" => $arLdap['USER_GROUP_ACCESSORY'], "USER_FILTER" => $arLdap['USER_FILTER'], "GROUP_NAME_ATTR" => $arLdap['GROUP_NAME_ATTR'], "CONVERT_UTF8" => $arLdap['CONVERT_UTF8'], "MAX_PAGE_SIZE" => $arLdap['MAX_PAGE_SIZE'] ); if(isset($arLdap['CONNECTION_TYPE'])) $ldap->arFields["CONNECTION_TYPE"] = $arLdap['CONNECTION_TYPE']; if($ldap->Connect()) { $ldp = $ldap; if(!$ldp->BindAdmin()) { $strError = GetMessage("USER_IMPORT_LDAP_SERVER_AUTH_ERROR"); } $ldp->Disconnect(); } else { $strError = GetMessage("USER_IMPORT_LDAP_SERVER_CONN_ERROR"); } } else $strError = GetMessage("USER_IMPORT_LDAP_SERVER_NOT_FOUND"); } elseif ($dataSource == "1c" && $create1cUser == "Y") { $user = new CUser; $arFields = array( "EMAIL" => $newUserEmail, "LOGIN" => $newUserLogin, "ACTIVE" => "Y", "PASSWORD" => $newUserPass, "CONFIRM_PASSWORD" => $newUserConfirmPass, ); $arGroups = array(); if (!empty($newUserGroups)) { $arGroups = array_column( \Bitrix\Main\GroupTable::getList(array( 'select' => array('ID'), 'filter' => array('@ID' => $newUserGroups), ))->fetchAll(), 'ID' ); } if (empty($arGroups)) { $strError = getMessage('USER_IMPORT_1C_USER_GROUP_EMPTY'); } else { $userID = $user->add($arFields); if (intval($userID) > 1) { \CUser::setUserGroup($userID, $arGroups); } else { $strError = $user->LAST_ERROR; } } } if ($strError !== false) $tabStep = 2; //Ajax (Main form action) if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "import" && $strError === false) { if ($csvImport) { $csvFile =& $csvImport->GetCsvObject(); $position = (isset($_REQUEST["position"]) && intval($_REQUEST["position"]) > 0 ? intval($_REQUEST["position"]) : false); if ($position !== false) $csvFile->SetPos($position); while ($csvImport->ImportUser()) { if(($mess = $csvImport->GetErrorMessage()) <> '') echo ""; if (USER_IMPORT_EXECUTION_TIME > 0 && (getmicrotime()-START_EXEC_TIME) > USER_IMPORT_EXECUTION_TIME) die(""); } die(""); } elseif ($ldp) { //Ldap Ajax function OnLdapBeforeSyncMainImport($arParams) { if(empty($arParams['oLdapServer'])) return; if(!($arParams['oLdapServer'] instanceof CLDAP)) return; $arParams['oLdapServer']->arFields["SYNC_USER_ADD"] = 'Y'; if(is_array($_REQUEST['LDAPMAP'])) { $arParams['oLdapServer']->arFields["FIELD_MAP"] = array_merge( $arParams['oLdapServer']->arFields["FIELD_MAP"], $_REQUEST['LDAPMAP'] ); } } AddEventHandler("ldap", "OnLdapBeforeSync", "OnLdapBeforeSyncMainImport"); $cntUsersImport = \CLdapServer::Sync($ldapServer); //avoid module dependencies if(property_exists('CLdapServer', 'syncErrors')) $strUserImportError = implode("\n", CLdapServer::$syncErrors); else $strUserImportError = ""; if (!empty($strUserImportError)) { echo ""; } die(""); } } } $APPLICATION->SetTitle(GetMessage("USER_IMPORT_TITLE")); require_once($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/prolog_admin_after.php"); CAdminMessage::ShowMessage($strError); $arTabs = array( array("DIV" => "tabSource", "TAB" => GetMessage("USER_IMPORT_SOURCE_TAB"), "TITLE"=>GetMessage("USER_IMPORT_SOURCE_TAB_DESC")), array("DIV" => "tabSettings", "TAB" => GetMessage("USER_IMPORT_SETTINGS_TAB"), "TITLE"=>GetMessage("USER_IMPORT_SETTINGS_TAB_DESC")), array("DIV" => "tabResults", "TAB" => GetMessage("USER_IMPORT_RESULT_TAB"), "TITLE"=>GetMessage("USER_IMPORT_RESULT_TAB_DESC")), ); $tabControl = new CAdminTabControl("tabControl", $arTabs, false, true); ?>