seeddms-code/op/op.ImportUsers.php
2020-07-30 10:55:40 +02:00

209 lines
7.9 KiB
PHP

<?php
// SeedDMS. Document Management System
// Copyright (C) 2010-2016 Uwe Steinmann
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
include("../inc/inc.Settings.php");
include("../inc/inc.LogInit.php");
include("../inc/inc.Utils.php");
include("../inc/inc.Language.php");
include("../inc/inc.Init.php");
include("../inc/inc.Extension.php");
include("../inc/inc.DBInit.php");
include("../inc/inc.ClassUI.php");
include("../inc/inc.Authentication.php");
function getBaseData($colname, $coldata, $objdata) { /* {{{ */
$objdata[$colname] = $coldata;
return $objdata;
} /* }}} */
function getPasswordPlainData($colname, $coldata, $objdata) { /* {{{ */
$objdata['passenc'] = seed_pass_hash($coldata);
return $objdata;
} /* }}} */
function getQuotaData($colname, $coldata, $objdata) { /* {{{ */
$objdata[$colname] = SeedDMS_Core_File::parse_filesize($coldata);
return $objdata;
} /* }}} */
function getFolderData($colname, $coldata, $objdata) { /* {{{ */
global $dms;
if($coldata) {
if($folder = $dms->getFolder((int)$coldata)) {
$objdata['homefolder'] = $folder;
}
} else {
$objdata['homefolder'] = null;
}
return $objdata;
} /* }}} */
function getGroupData($colname, $coldata, $objdata) { /* {{{ */
global $dms;
if($group = $dms->getGroupByName($coldata)) {
$objdata['groups'][] = $group;
}
return $objdata;
} /* }}} */
function getRoleData($colname, $coldata, $objdata) { /* {{{ */
switch($coldata) {
case 'admin':
$role = 1;
break;
case 'guest':
$role = 2;
break;
default:
$role = 0;
}
$objdata['role'] = $role;
return $objdata;
} /* }}} */
if (!$user->isAdmin()) {
UI::exitError(getMLText("admin_tools"),getMLText("access_denied"));
}
$log = array();
if (isset($_FILES['userdata']) && $_FILES['userdata']['error'] == 0) {
if(!is_uploaded_file($_FILES["userdata"]["tmp_name"]))
UI::exitError(getMLText("document_title", array("documentname" => $document->getName())),getMLText("error_occured"));
if($_FILES["userdata"]["size"] == 0)
UI::exitError(getMLText("document_title", array("documentname" => $document->getName())),getMLText("uploading_zerosize"));
$csvdelim = ';';
$csvencl = '"';
if($fp = fopen($_FILES['userdata']['tmp_name'], 'r')) {
$colmap = array();
if($header = fgetcsv($fp, 0, $csvdelim, $csvencl)) {
foreach($header as $i=>$colname) {
$colname = trim($colname);
if(substr($colname, 0, 5) == 'group') {
$colmap[$i] = array("getGroupData", $colname);
} elseif(in_array($colname, array('role'))) {
$colmap[$i] = array("getRoleData", $colname);
} elseif(in_array($colname, array('homefolder'))) {
$colmap[$i] = array("getFolderData", $colname);
} elseif(in_array($colname, array('quota'))) {
$colmap[$i] = array("getQuotaData", $colname);
} elseif(in_array($colname, array('password'))) {
$colmap[$i] = array("getPasswordPlainData", $colname);
} elseif(in_array($colname, array('login', 'name', 'passenc', 'email', 'comment', 'group'))) {
$colmap[$i] = array("getBaseData", $colname);
} elseif(substr($colname, 0, 5) == 'attr:') {
$kk = explode(':', $colname, 2);
if(($attrdef = $dms->getAttributeDefinitionByName($kk[1])) || ($attrdef = $dms->getAttributeDefinition((int) $kk[1]))) {
$colmap[$i] = array("getAttributeData", $attrdef);
}
}
}
}
// echo "<pre>";print_r($colmap);echo "</pre>";
if(count($colmap) > 1) {
$allusers = $dms->getAllUsers();
$userids = array();
foreach($allusers as $muser)
$userids[$muser->getLogin()] = $muser;
$newusers = array();
while(!feof($fp)) {
if($data = fgetcsv($fp, 0, $csvdelim, $csvencl)) {
$md = array();
foreach($data as $i=>$coldata) {
if(isset($colmap[$i])) {
$md = call_user_func($colmap[$i][0], $colmap[$i][1], $coldata, $md);
}
}
if($md)
$newusers[] = $md;
}
}
// echo "<pre>";print_r($newusers);echo "</pre>";
$makeupdate = !empty($_POST['update']);
foreach($newusers as $u) {
if($eu = $dms->getUserByLogin($u['login'])) {
if(isset($u['name']) && $u['name'] != $eu->getFullName()) {
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Name of user updated. '".$u['name']."' != '".$eu->getFullName()."'");
if($makeupdate)
$eu->setFullName($u['name']);
}
if(isset($u['email']) && $u['email'] != $eu->getEmail()) {
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Email of user updated. '".$u['email']."' != '".$eu->getEmail()."'");
if($makeupdate)
$eu->setEmail($u['email']);
}
if(isset($u['passenc']) && $u['passenc'] != $eu->getPwd()) {
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Encrypted password of user updated. '".$u['passenc']."' != '".$eu->getPwd()."'");
if($makeupdate)
$eu->setPwd($u['passenc']);
}
if(isset($u['comment']) && $u['comment'] != $eu->getComment()) {
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Comment of user updated. '".$u['comment']."' != '".$eu->getComment()."'");
if($makeupdate)
$eu->setComment($u['comment']);
}
if(isset($u['language']) && $u['language'] != $eu->getLanguage()) {
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Language of user updated. '".$u['language']."' != '".$eu->getLanguage()."'");
if($makeupdate)
$eu->setLanguage($u['language']);
}
if(isset($u['quota']) && $u['quota'] != $eu->getQuota()) {
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Quota of user updated. '".$u['quota']."' != '".$eu->getQuota()."'");
if($makeupdate)
$eu->setQuota($u['language']);
}
if(isset($u['homefolder']) && $u['homefolder']->getId() != $eu->getHomeFolder()) {
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Homefolder of user updated. '".(is_object($u['homefolder']) ? $u['homefolder']->getId() : '')."' != '".($eu->getHomeFolder() ? $eu->getHomeFolder() : '')."'");
if($makeupdate)
$eu->setHomeFolder($u['homefolder']);
}
$func = function($o) {return $o->getID();};
if(isset($u['groups']) && implode(',',array_map($func, $u['groups'])) != implode(',',array_map($func, $eu->getGroups()))) {
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Groups of user updated. '".implode(',',array_map($func, $u['groups']))."' != '".implode(',',array_map($func, $eu->getGroups()))."'");
if($makeupdate) {
foreach($eu->getGroups() as $g)
$eu->leaveGroup($g);
foreach($u['groups'] as $g)
$eu->joinGroup($g);
}
}
// $log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "User '".$eu->getLogin()."' updated.");
} else {
if(!empty($_POST['addnew'])) {
if(!empty($u['login']) && !empty($u['name']) && !empty($u['email'])) {
$ret = $dms->addUser($u['login'], '', $u['name'], $u['email'], !empty($u['language']) ? $u['language'] : 'en_GB', 'bootstrap', !empty($u['comment']) ? $u['comment'] : '', $u['role']);
var_dump($ret);
}
}
$log[] = array('id'=>$u['login'], 'type'=>'success', 'msg'=> "User '".$u['name']."' added.");
}
}
}
}
}
$tmp = explode('.', basename($_SERVER['SCRIPT_FILENAME']));
$view = UI::factory($theme, $tmp[1], array('dms'=>$dms, 'user'=>$user));
if($view) {
$view->setParam('log', $log);
$view($_GET);
exit;
}