From a80a2725bc533fb0094409ee8a5d86b5f5a30e8f Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 8 Dec 2021 23:26:12 +1100 Subject: [PATCH] Start of using Attribute objects, rendering jpegphoto --- {lib => app/Classes/LDAP}/Attribute.php | 85 +++++++------------ .../Classes/LDAP/Attribute/Binary.php | 18 ++-- .../LDAP/Attribute/Binary/JpegPhoto.php | 27 ++++++ app/Classes/LDAP/Attribute/Factory.php | 35 ++++++++ app/Http/Controllers/HomeController.php | 31 +++++-- app/Ldap/Entry.php | 12 +++ lib/JpegAttribute.php | 17 ---- public/js/custom.js | 2 +- resources/views/frames/dn.blade.php | 17 ++-- routes/web.php | 2 +- 10 files changed, 147 insertions(+), 99 deletions(-) rename {lib => app/Classes/LDAP}/Attribute.php (96%) rename lib/BinaryAttribute.php => app/Classes/LDAP/Attribute/Binary.php (85%) create mode 100644 app/Classes/LDAP/Attribute/Binary/JpegPhoto.php create mode 100644 app/Classes/LDAP/Attribute/Factory.php delete mode 100644 lib/JpegAttribute.php diff --git a/lib/Attribute.php b/app/Classes/LDAP/Attribute.php similarity index 96% rename from lib/Attribute.php rename to app/Classes/LDAP/Attribute.php index 9c4ef98..ca67b92 100644 --- a/lib/Attribute.php +++ b/app/Classes/LDAP/Attribute.php @@ -1,26 +1,23 @@ getServer($server_id); - - $sattr = $server->getSchemaAttribute($name); - if ($sattr) { - $this->name = $sattr->getName(false); - $this->setLDAPdetails($sattr); - - } else - $this->name = $name; - - $this->source = $source; - - # XML attributes are shown by default - switch ($source) { - case 'XML': $this->show(); - $this->setXML($values); - - break; - - default: - if (! isset($values['values'])) - debug_dump_backtrace('no index "values"',1); - - $this->initValue($values['values']); - } + public function __construct(string $name,array $values) { + $this->name = $name; + $this->values = $values; + /* # Should this attribute be hidden if ($server->isAttrHidden($this->name)) $this->forcehide = true; @@ -119,6 +92,12 @@ class Attribute { # Should this attribute value be unique if ($server->isAttrUnique($this->name)) $this->unique = true; + */ + } + + public function __toString(): string + { + return join('
',$this->values); } /** @@ -127,7 +106,7 @@ class Attribute { * @param boolean $lower - Return the attribute in normal or lower case (default lower) * @param boolean $real - Return the real attribute name (with ;binary, or just the name) * @return string Attribute name - */ + * public function getName($lower=true,$real=false) { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs,$this->name); @@ -168,7 +147,7 @@ class Attribute { /** * Autovalue is called after the attribute is initialised, and thus the values from the ldap server will be set. - */ + * public function autoValue($new_val) { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); @@ -648,7 +627,7 @@ class Attribute { * Return if this attribute is an RDN attribute * * @return boolean - */ + * public function isRDN() { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs,$this->rdn); @@ -660,7 +639,7 @@ class Attribute { * Capture all the LDAP details we are interested in * * @param sattr Schema Attribute - */ + * private function setLDAPdetails($sattr) { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); @@ -680,7 +659,7 @@ class Attribute { /** * Return a list of aliases for this Attribute (as defined by the schema) * This list will be lowercase. - */ + * public function getAliases() { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->aliases); @@ -846,7 +825,7 @@ class Attribute { /** * Display the values removed in an attribute. - */ + * public function getRemovedValues() { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); @@ -856,7 +835,7 @@ class Attribute { /** * Display the values removed in an attribute. - */ + * public function getAddedValues() { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); @@ -872,7 +851,7 @@ class Attribute { * * @param string $attr_name The name of the attribute to examine. * @return string - */ + * private function real_attr_name() { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->name); @@ -882,7 +861,7 @@ class Attribute { /** * Does this attribute need supporting JS - */ + * public function needJS($type=null) { if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); @@ -913,5 +892,5 @@ class Attribute { } else debug_dump_backtrace(sprintf('Unknown JS request %s',$type),1); } -} -?> + */ +} \ No newline at end of file diff --git a/lib/BinaryAttribute.php b/app/Classes/LDAP/Attribute/Binary.php similarity index 85% rename from lib/BinaryAttribute.php rename to app/Classes/LDAP/Attribute/Binary.php index 6b5053b..1ff5a18 100644 --- a/lib/BinaryAttribute.php +++ b/app/Classes/LDAP/Attribute/Binary.php @@ -1,10 +1,8 @@ filepaths[$i] = $path; } } -} -?> + */ +} \ No newline at end of file diff --git a/app/Classes/LDAP/Attribute/Binary/JpegPhoto.php b/app/Classes/LDAP/Attribute/Binary/JpegPhoto.php new file mode 100644 index 0000000..da3660d --- /dev/null +++ b/app/Classes/LDAP/Attribute/Binary/JpegPhoto.php @@ -0,0 +1,27 @@ +values as $value) { + switch ($x=$f->buffer($value,FILEINFO_MIME_TYPE)) { + case 'image/jpeg': + default: + $result .= sprintf("",$x,base64_encode($value)); + } + } + + return $result; + } +} \ No newline at end of file diff --git a/app/Classes/LDAP/Attribute/Factory.php b/app/Classes/LDAP/Attribute/Factory.php new file mode 100644 index 0000000..e67de98 --- /dev/null +++ b/app/Classes/LDAP/Attribute/Factory.php @@ -0,0 +1,35 @@ +Attribute\Binary\JpegPhoto::class, + ]; + + /** + * Returns new event instance + * + * @param string $attribute + * @param array $values + * @return Attribute + */ + public static function create(string $attribute,array $values): Attribute + { + $class = Arr::get(self::map,strtolower($attribute),Attribute::class); + Log::debug(sprintf('%s:Creating LDAP Attribute [%s] as [%s]',static::LOGKEY,$attribute,$class)); + + return new $class($attribute,$values); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index fee7abe..ad53005 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -15,6 +15,9 @@ use LdapRecord\Models\ModelNotFoundException; class HomeController extends Controller { + /** + * Application home page + */ public function home() { $base = (new Entry)->baseDN() ?: collect(); @@ -27,18 +30,24 @@ class HomeController extends Controller 'item'=>Crypt::encryptString($item->getDn()), 'lazy'=>TRUE, 'icon'=>'fa-fw fas fa-sitemap', - 'tooltip'=>$item, + 'tooltip'=>$item->getDn(), ]; })); } + /** + * LDAP Server INFO + * + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + * @throws ModelNotFoundException + */ public function info() { $root = (new Entry)->rootDSE(); try { $attrs = collect($root->getAttributes()) - ->transform(function($item,$key) { + ->transform(function($item) { foreach ($item as $k=>$v) { if (preg_match('/[0-9]+\.[0-9]+\.[0-9]+/',$v)) { $format = sprintf( @@ -60,19 +69,23 @@ class HomeController extends Controller } return view('frames.dn') - ->with('dn',__('Server Info')) - ->with('leaf',$root) - ->with('attributes',$this->sortAttrs($attrs)); + ->with('o',$root) + ->with('dn',__('Server Info')); } - public function render(Request $request) + /** + * Render a specific DN + * + * @param Request $request + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + */ + public function dn_frame(Request $request) { $dn = Crypt::decryptString($request->post('key')); return view('frames.dn') - ->with('dn',$dn) - ->with('leaf',$x=(new Server)->fetch($dn)) - ->with('attributes',$x ? $this->sortAttrs(collect($x->getAttributes())) : []); + ->with('o',(new Server)->fetch($dn)) + ->with('dn',$dn); } /** diff --git a/app/Ldap/Entry.php b/app/Ldap/Entry.php index 2ab7850..7f57794 100644 --- a/app/Ldap/Entry.php +++ b/app/Ldap/Entry.php @@ -8,6 +8,8 @@ use LdapRecord\Models\Model; use LdapRecord\Models\ModelNotFoundException; use LdapRecord\Query\Model\Builder; +use App\Classes\LDAP\Attribute\Factory; + class Entry extends Model { /** @@ -43,6 +45,16 @@ class Entry extends Model return $result; } + public function getAttributes(): array + { + $result = collect(); + foreach (parent::getAttributes() as $attribute => $value) { + $result->put($attribute,Factory::create($attribute,$value)); + } + + return $result->toArray(); + } + /** * Return an icon for a DN based on objectClass * diff --git a/lib/JpegAttribute.php b/lib/JpegAttribute.php deleted file mode 100644 index d440048..0000000 --- a/lib/JpegAttribute.php +++ /dev/null @@ -1,17 +0,0 @@ - diff --git a/public/js/custom.js b/public/js/custom.js index bc727b6..38ae6ef 100644 --- a/public/js/custom.js +++ b/public/js/custom.js @@ -30,7 +30,7 @@ $(document).ready(function() { click: function(event,data) { if (data.targetType == 'title') { $.ajax({ - url: 'render', + url: 'dn', method: 'POST', data: { key: data.node.data.item }, dataType: 'html', diff --git a/resources/views/frames/dn.blade.php b/resources/views/frames/dn.blade.php index d29c2f7..8cfb191 100644 --- a/resources/views/frames/dn.blade.php +++ b/resources/views/frames/dn.blade.php @@ -1,22 +1,21 @@ @extends('layouts.dn') @section('page_title') - {{ $dn }} -@endsection -@section('page_subtitle') - {{ $leaf->entryuuid[0] ?? '' }} -@endsection -@section('page_icon') - {{ $leaf->icon() ?? 'fas fa-info' }} + + + + + +
{!! $x ?: sprintf('
',$o->icon() ?? "fas fa-info") !!}
{{ $dn }}
{{ $o->entryuuid[0] ?? '' }}
@endsection @section('main-content')
- @foreach ($attributes as $attribute => $value) + @foreach ($o->getAttributes() as $attribute => $value) - + @endforeach
{{ $attribute }}{!! is_array($value) ? join('
',$value) : $value !!}
{!! $value !!}
diff --git a/routes/web.php b/routes/web.php index a390051..aecc5a1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -28,7 +28,7 @@ Route::group(['prefix' => LaravelLocalization::setLocale()], function() { Route::get('home',[HomeController::class,'home']); Route::get('info',[HomeController::class,'info']); - Route::post('render',[HomeController::class,'render']); + Route::post('dn',[HomeController::class,'dn_frame']); }); Route::redirect('/','home');