phpldapadmin/app/Classes/LDAP/Server.php

137 lines
3.3 KiB
PHP
Raw Normal View History

2020-08-22 12:26:06 +00:00
<?php
2020-08-23 02:30:18 +00:00
namespace App\Classes\LDAP;
2020-08-22 12:26:06 +00:00
use Exception;
2020-09-18 14:08:00 +00:00
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
2020-08-22 12:26:06 +00:00
use LdapRecord\Models\Model;
use LdapRecord\Query\Collection;
use LdapRecord\Query\Model\Builder;
use App\Ldap\Entry;
2020-08-22 12:26:06 +00:00
class Server
{
2020-09-18 14:08:00 +00:00
/**
* Query the server for a DN and return it's children and if those children have children.
2020-09-18 14:08:00 +00:00
*
* @param string $dn
* @return array|Collection|null
2020-09-18 14:08:00 +00:00
*/
public function children(string $dn): ?Collection
2020-09-18 14:08:00 +00:00
{
return ($x=(new Entry)
->query()
->select(['*','hassubordinates'])
->setDn($dn)
->listing()
->get()) ? $x : NULL;
2020-09-18 14:08:00 +00:00
}
/**
* Fetch a DN from the server
*
* @param string $dn
* @param array $attrs
* @return array|Model|Collection|Builder|null
2020-08-22 12:26:06 +00:00
*/
public function fetch(string $dn,array $attrs=['*','+']): ?Entry
2020-08-22 12:26:06 +00:00
{
return ($x=(new Entry)
->query()
->select($attrs)
->find($dn)) ? $x : NULL;
2020-08-22 12:26:06 +00:00
}
2020-09-13 11:30:04 +00:00
/**
2020-09-18 14:08:00 +00:00
* Given an LDAP OID number, returns a verbose description of the OID.
* This function parses ldap_supported_oids.txt and looks up the specified
* OID, and returns the verbose message defined in that file.
2020-09-13 11:30:04 +00:00
*
2020-09-18 14:08:00 +00:00
* <code>
* Array (
* [title] => All Operational Attribute
* [ref] => RFC 3673
* [desc] => An LDAP extension which clients may use to request the return of all operational attributes.
* )
* </code>
*
* @param string $oid The OID number (ie, "1.3.6.1.4.1.4203.1.5.1") of the OID of interest.
* @param string $key The title|ref|desc to return
* @return string|null
* @testedby TranslateOidTest::testRootDSE()
2020-09-13 11:30:04 +00:00
*/
2020-09-18 14:08:00 +00:00
public static function getOID(string $oid,string $key): ?string
2020-09-13 11:30:04 +00:00
{
2020-09-18 14:08:00 +00:00
$oids = Cache::remember('oids',86400,function() {
try {
$f = fopen(config_path('ldap_supported_oids.txt'),'r');
2020-09-13 11:30:04 +00:00
} catch (Exception $e) {
2020-09-18 14:08:00 +00:00
return NULL;
}
2020-09-18 14:08:00 +00:00
$result = collect();
while (! feof($f)) {
$line = trim(fgets($f));
if (! $line OR preg_match('/^#/',$line))
continue;
$fields = explode(':',$line);
$result->put(Arr::get($fields,0),[
'title'=>Arr::get($fields,1),
'ref'=>Arr::get($fields,2),
'desc'=>Arr::get($fields,3),
]);
}
fclose($f);
return $result;
});
return Arr::get(
($oids ? $oids->get($oid) : []),
$key,
($key == 'desc' ? 'No description available, can you help with one?' : ($key == 'title' ? $oid : NULL))
2020-09-18 14:08:00 +00:00
);
}
public static function icon(Entry $dn): string
{
$objectclasses = array_map('strtolower',$dn->objectclass);
// Return icon based upon objectClass value
if (in_array('person',$objectclasses) ||
in_array('organizationalperson',$objectclasses) ||
in_array('inetorgperson',$objectclasses) ||
in_array('account',$objectclasses) ||
in_array('posixaccount',$objectclasses))
return 'fas fa-user';
elseif (in_array('organization',$objectclasses))
return 'fas fa-university';
elseif (in_array('organizationalunit',$objectclasses))
return 'fas fa-object-group';
elseif (in_array('dcobject',$objectclasses) ||
in_array('domainrelatedobject',$objectclasses) ||
in_array('domain',$objectclasses) ||
in_array('builtindomain',$objectclasses))
return 'fas fa-network-wired';
elseif (in_array('country',$objectclasses))
return sprintf('flag %s',strtolower(Arr::get($dn->c,0)));
// Default
return 'fa-fw fas fa-cog';
}
2020-08-23 02:30:18 +00:00
}