Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gruberroland committed Jul 15, 2024
1 parent 7a95a06 commit a50e4cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 57 deletions.
75 changes: 24 additions & 51 deletions lam/lib/import.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LAM\TOOLS\IMPORT_EXPORT;

use \htmlStatusMessage;
use \LAMException;
use htmlStatusMessage;
use LAMException;

/*
Expand Down Expand Up @@ -44,16 +44,16 @@ include_once('ldap.inc');
*/
class Importer {

const SESSION_KEY_TASKS = 'import_tasks';
const SESSION_KEY_COUNT = 'import_count';
const SESSION_KEY_STOP_ON_ERROR = 'import_stop_on_error';
const STATUS = 'status';
const PROGRESS = 'progress';
const DATA = 'data';
public const SESSION_KEY_TASKS = 'import_tasks';
public const SESSION_KEY_COUNT = 'import_count';
public const SESSION_KEY_STOP_ON_ERROR = 'import_stop_on_error';
public const STATUS = 'status';
public const PROGRESS = 'progress';
public const DATA = 'data';
private const TIME_LIMIT = 10;
const KEY = 0;
const VALUE = 1;
const CHANGETYPE = 'changetype';
public const KEY = 0;
public const VALUE = 1;
private const CHANGETYPE = 'changetype';

/**
* Extracts the single entries in the file.
Expand All @@ -67,7 +67,7 @@ class Importer {
$currentEntry = [];
foreach ($lines as $line) {
$trimmedLine = trim($line);
if (substr($trimmedLine, 0, 1) === '#') {
if (str_starts_with($trimmedLine, '#')) {
// skip comments
continue;
}
Expand All @@ -78,7 +78,7 @@ class Importer {
$currentEntry = [];
}
}
elseif (substr($line, 0, 1) === ' ') {
elseif (str_starts_with($line, ' ')) {
// append to last line if starting with a space
if (empty($currentEntry)) {
throw new LAMException(_('Invalid data'), htmlspecialchars($line));
Expand Down Expand Up @@ -246,7 +246,7 @@ class Importer {
private function processVersion($entry) {
$keyValue = $this->getLineKeyValue($entry[0]);
if (($keyValue[Importer::VALUE] != '1') || (sizeof($entry) > 1)) {
$escapedLines = array_map('htmlspecialchars', $entry);
$escapedLines = array_map(htmlspecialchars(...), $entry);
throw new LAMException(_('LDIF import only supports version 1'), implode('<br>', $escapedLines));
}
}
Expand All @@ -267,7 +267,7 @@ class Importer {
}
$firstAttributeLine = array_shift($entry);
$firstAttribute = $this->getLineKeyValue($firstAttributeLine);
if ($firstAttribute[Importer::KEY] != Importer::CHANGETYPE) {
if ($firstAttribute[Importer::KEY] != self::CHANGETYPE) {
// complete DN
$attributes = [
$firstAttribute[Importer::KEY] => [$firstAttribute[Importer::VALUE]]
Expand Down Expand Up @@ -332,7 +332,7 @@ class Importer {
}
$newRdnData = $this->getLineKeyValue($entry[0]);
if ($newRdnData[Importer::KEY] !== 'newrdn') {
throw new LAMException(_('Invalid data'), htmlspecialchars($dn) . '<br>' . $newRdnData);
throw new LAMException(_('Invalid data'), htmlspecialchars($dn) . '<br>' . print_r($newRdnData, true));
}
$newRdn = $newRdnData[Importer::VALUE];
$delOldRdnData = $this->getLineKeyValue($entry[1]);
Expand Down Expand Up @@ -403,7 +403,7 @@ class Importer {
if (sizeof($parts) !== 2) {
throw new LAMException(_('Invalid data'), htmlspecialchars($line));
}
if (substr($parts[Importer::VALUE], 0, 1) == ':') {
if (str_starts_with($parts[Importer::VALUE], ':')) {
$value = base64_decode(trim(substr($parts[Importer::VALUE], 1)));
}
else {
Expand Down Expand Up @@ -453,7 +453,7 @@ class AddEntryTask implements ImporterTask {

/**
* {@inheritDoc}
* @see \LAM\TOOLS\IMPORT_EXPORT\ImporterTask::run()
* @see ImporterTask::run
*/
public function run() {
$ldap = $_SESSION['ldap']->server();
Expand Down Expand Up @@ -492,7 +492,7 @@ class RenameEntryTask implements ImporterTask {

/**
* {@inheritDoc}
* @see \LAM\TOOLS\IMPORT_EXPORT\ImporterTask::run()
* @see ImporterTask::run
*/
public function run() {
$ldap = $_SESSION['ldap']->server();
Expand Down Expand Up @@ -525,7 +525,7 @@ class DeleteEntryTask implements ImporterTask {

/**
* {@inheritDoc}
* @see \LAM\TOOLS\IMPORT_EXPORT\ImporterTask::run()
* @see ImporterTask::run
*/
public function run() {
$ldap = $_SESSION['ldap']->server();
Expand Down Expand Up @@ -567,7 +567,7 @@ class MultiTask implements ImporterTask {

/**
* {@inheritDoc}
* @see \LAM\TOOLS\IMPORT_EXPORT\ImporterTask::run()
* @see ImporterTask::run
*/
public function run() {
foreach ($this->tasks as $task) {
Expand Down Expand Up @@ -610,7 +610,7 @@ class AddAttributesTask implements ImporterTask {

/**
* {@inheritDoc}
* @see \LAM\TOOLS\IMPORT_EXPORT\ImporterTask::run()
* @see ImporterTask::run
*/
public function run() {
$ldap = $_SESSION['ldap']->server();
Expand Down Expand Up @@ -667,7 +667,7 @@ class DeleteAttributesTask implements ImporterTask {

/**
* {@inheritDoc}
* @see \LAM\TOOLS\IMPORT_EXPORT\ImporterTask::run()
* @see ImporterTask::run
*/
public function run() {
$ldap = $_SESSION['ldap']->server();
Expand Down Expand Up @@ -735,7 +735,7 @@ class ReplaceAttributesTask implements ImporterTask {

/**
* {@inheritDoc}
* @see \LAM\TOOLS\IMPORT_EXPORT\ImporterTask::run()
* @see ImporterTask::run
*/
public function run() {
$ldap = $_SESSION['ldap']->server();
Expand All @@ -746,31 +746,4 @@ class ReplaceAttributesTask implements ImporterTask {
throw new LAMException(sprintf(_('Was unable to create DN: %s.'), $this->dn), getExtendedLDAPErrorMessage($ldap));
}

/**
* Returns the DN.
*
* @return string DN
*/
public function getDn() {
return $this->dn;
}

/**
* Returns the attributes to add.
*
* @return string[] attributes (array('attr' => array('val1', 'val2')))
*/
public function getAttributes() {
return $this->attributes;
}

/**
* Returns the attributes name.
*
* @return string name
*/
public function getAttributeName() {
return $this->attributeName;
}

}
11 changes: 5 additions & 6 deletions lam/lib/ldap.inc
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ class Ldap {
// return error number
$errorNumber = ldap_errno($this->server);
$clientSource = empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR'];
if (($errorNumber === False)
|| ($errorNumber == 81)) {
if ($errorNumber == 81) {
// connection failed
logNewMessage(LOG_ERR, 'User ' . $user . ' (' . $clientSource . ') failed to log in (LDAP error: ' . getDefaultLDAPErrorString($this->server) . ').');
throw new LAMException(_("Cannot connect to specified LDAP server. Please try again."), null, null, $errorNumber);
}
elseif ($errorNumber == 49) {
// user name/password invalid. Return to login page.
// username/password invalid. Return to login page.
logNewMessage(LOG_ERR, 'User ' . $user . ' (' . $clientSource . ') failed to log in (wrong password). ' . getDefaultLDAPErrorString($this->server));
throw new LAMException(_("Wrong password/user name combination. Please try again."), getDefaultLDAPErrorString($this->server), null, $errorNumber);
}
Expand Down Expand Up @@ -177,7 +176,7 @@ class Ldap {
$file = @readdir($dir);
while ($file) {
$path = $tmpDir . $file;
if ((substr($file, 0, 1) != '.')
if ((!str_starts_with($file, '.'))
&& !is_dir($path)
&& ($time - filemtime($path) > 900)) {
@unlink($path);
Expand All @@ -191,7 +190,7 @@ class Ldap {
$dir = @opendir($tmpDir);
$file = @readdir($dir);
while ($file) {
if (substr($file, -4) == '.tmp') {
if (str_ends_with($file, '.tmp')) {
$path = $tmpDir . $file;
if ($time - filemtime($path) > (3600 * 24)) {
@unlink($path);
Expand All @@ -215,7 +214,7 @@ class Ldap {
}

/**
* Returns the LDAP user name.
* Returns the LDAP username.
*
* @return string user name
*/
Expand Down

0 comments on commit a50e4cd

Please sign in to comment.