<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Copyright (c) 2008, Demian Turner |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +---------------------------------------------------------------------------+
// | Seagull 0.6 |
// +---------------------------------------------------------------------------+
// | UserImportMgr.php |
// +---------------------------------------------------------------------------+
// | Authors: |
// | Michal Pawlowski <misza@weblab.com.pl> |
// | Demian Turner <demian@phpkitchen.com> |
// +---------------------------------------------------------------------------+
// $Id: UserImportMgr.php,v 1.7 2005/05/24 10:48:46 demian Exp $
require_once SGL_MOD_DIR . '/user/classes/UserMgr.php';
require_once SGL_MOD_DIR . '/user/classes/UserDAO.php';
/**
* Module allows administrator to load users from CSV file.
*
*/
class UserImportMgr extends UserMgr
{
function UserImportMgr()
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
parent::UserMgr();
$this->pageTitle = 'User Import Manager';
$this->template = 'userImport.html';
$this->da = UserDAO::singleton();
$this->_aActionsMapping = array(
'list' => array('list'),
'insertImportedUsers' => array('insertImportedUsers'),
'uploadFile' => array('uploadFile', 'redirectToDefault'),
);
}
function validate($req, &$input)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
parent::validate($req, $input);
$this->validated = true;
$input->error = array();
$input->pageTitle = $this->pageTitle;
$input->masterTemplate = $this->masterTemplate;
$input->template = $this->template;
$input->action = ($req->get('action')) ? $req->get('action') : 'list';
$input->submitted = $req->get('submitted');
$input->csvFile = $req->get('frmCsvFile');
$input->organisation= $req->get('frmOrgId');
$input->role = $req->get('frmRoleId');
$input->FileUploadCsv = $req->get('uploadFileUCsv');
if ($input->action == 'uploadFile' && empty($input->FileUploadCsv)) {
$aErrors['csvFile'] = SGL_Output::translate('Please select a file');
}
if ($input->submitted) {
if (empty($input->csvFile)) {
$aErrors['csvfile'] = SGL_Output::translate('Please select a file');
}
if (empty($input->organisation)) {
$aErrors['organisation'] = SGL_Output::translate('Please select the organisation');
}
if (empty($input->role)) {
$aErrors['role'] = SGL_Output::translate('Please select the role');
}
}
// if errors have occured
if (isset($aErrors) && count($aErrors)) {
SGL::raiseMsg('Please fill in the indicated fields');
$input->error = $aErrors;
$this->validated = false;
}
}
function display(&$output)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
// build roles array
$aRoles = $this->da->getRoles();
$output->aRoles = $aRoles;
if ($this->conf['OrgMgr']['enabled']) {
$output->aOrgs = $this->da->getOrgs();
} else {
$output->aOrgs = array(1 => 'default');
}
$output->aFiles = $this->_getCsvFiles();
// if no .csv files, give this information to user
if (empty($output->aFiles)) {
$output->noCsvFile = true;
}
}
function _cmd_list(&$input, &$output)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
}
function _cmd_insertImportedUsers(&$input, &$output)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
// read in selected CSV file
$aUsers = $this->_readCsvFile($input->csvFile);
// wrap _insert method to import users
foreach ($aUsers as $aUser) {
$kEmail = $this->getKeyMail($aUser);
$email = (isset($aUser[$kEmail])) ? $aUser[$kEmail] : false;
if ($email != false && $this->da->isUniqueEmail($email) != false && isset($aUser['First Name']) && isset($aUser['Last Name'])) {
$user = new StdClass();
$user->first_name = $aUser['First Name'];
$user->last_name = $aUser['Last Name'];
$user->email = $email;
$user->role_id = $input->role;
$user->organisation_id = $input->organisation;
$user->username = strtolower($email);
$user->passwd = 'password';
// assign to input object
$input->user = $user;
$input->importUser = true;
// call parent _insert method
parent::_cmd_insert($input, $output);
}
}
SGL::raiseMsg('user successfully added', true, SGL_MESSAGE_INFO);
}
function getKeyMail($aMail) {
if (isset($aMail['Primary Email'])) {
return 'Primary Email';
} elseif (isset($aMail['E-mail Address'])) {
return 'E-mail Address';
} else {
return false;
}
}
/**
* Redirects to UserMgr::list()
*
* @access private
*/
function _cmd_redirectToUserMgr(&$input, &$output)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
// if no errors have occured, redirect
if (!SGL_Error::count()) {
SGL_HTTP::redirect(array('manager' => 'user'));
// else display error with blank template
} else {
$output->template = 'error.html';
}
}
function _cmd_uploadFile(&$input, &$output)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
if (is_dir(SGL_UPLOAD_DIR) == false) {
include_once 'System.php';
$success = System::mkDir(array(SGL_UPLOAD_DIR));
if (!$success) {
SGL::raiseMsg('The upload directory does not create');
}
}
if ($this->isFileCsv($input->FileUploadCsv['name']) === false) {
SGL::raiseMsg('please, upload file type .csv');
return false;
}
if ($input->FileUploadCsv['name'] == true) {
$pathFile = RvsLibs_File::buildPath(array(SGL_UPLOAD_DIR,$input->FileUploadCsv['name']));
$resCopy = move_uploaded_file($input->FileUploadCsv['tmp_name'], $pathFile);
if ($resCopy == false) {
SGL::raiseMsg('upload file fail');
} else {
SGL::raiseMsg('upload file successfully', true, SGL_MESSAGE_INFO);
}
}
}
function isFileCsv($filename)
{
if (RvsLibs_String::preg_match("/\.csv/i", $filename)) {
return true;
}
return false;
}
function _getCsvFiles()
{
// first check if upload folder exists, if not create it
if (!is_writable(SGL_UPLOAD_DIR)) {
include_once 'System.php';
$success = System::mkDir(array(SGL_UPLOAD_DIR));
if (!$success) {
SGL::raiseError('The upload directory does not appear to be writable, please give the
webserver permissions to write to it', SGL_ERROR_FILEUNWRITABLE);
return false;
}
}
// get array of files in upload folder
if (@$fh = opendir(SGL_UPLOAD_DIR)) {
while (false !== ($file = readdir($fh))) {
// remove unwanted dir elements
if ($file == '.' || $file == '..' || $file == 'svn') {
continue;
}
// and anything without csv extension
if (!preg_match('/\.csv$/i', $file)) {//($ext = end(explode('.', $file))) != 'csv') {
continue;
}
$aFiles[] = $file;
}
closedir($fh);
} else {
SGL::raiseError('There was a problem reading the upload dir, if nothing has been ' .
'uploaded yet, it will not have been created', SGL_ERROR_NOFILE);
}
// convert to hash needed by generateSelect
if (isset($aFiles) && count($aFiles)) {
foreach ($aFiles as $file) {
$aStartFiles[$file] = $file;
}
return $aStartFiles;
}
return array();
}
function _readCsvFile($file)
{
require_once 'File/CSV.php';
require_once 'Validate.php';
$csv = new File_CSV();
$val = new Validate();
///fix PHP 5.3
$val = &$val;
$aUserError = array();
$file = SGL_UPLOAD_DIR . '/' . $file;
$conf = $csv->discoverFormat($file);
$isKey = false;
$aKey = array();
$aData = array();
if (($handle = fopen($file, "r")) !== FALSE) {
while (($aLine = fgetcsv($handle, filesize($file), ",")) !== FALSE) {
// while (false !== ($aLine = $csv->read($file, $conf))) {
$aData = array();
$aRecord = array();
$firstName = (empty($aLine[0])) ? 'blank' : $aLine[0];
$lastName = (empty($aLine[1])) ? 'blank' : $aLine[1];
if (in_array('First Name', $aLine) === true && in_array('Last Name', $aLine) === true) {
$isKey = true;
} else {
$isKey = false;
}
if ($isKey === true) {
foreach($aLine as $k => $v) {
$aKey['key'][$k] = $v;
}
}
if ($isKey === false) {
foreach($aLine as $k => $v) {
$aData['value'][$aKey['key'][$k]] = $v;
}
} else {
continue;
}
$kEmail = $this->getKeyMail($aData['value']);
$email = (isset($aData['value'][$kEmail])) ? $aData['value'][$kEmail] : false;
if (!$val->email($email)) {
continue;
}
/*
$aRecord['firstname'] = @$aFnMatches[0];
$aRecord['lastname'] = $lastName;
*/
$aResults[] = $aData['value'];
unset($aRecord);
}
}
$aResults['aUserError'] = $aUserError;
return $aResults;
}
}
?>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez