Start of using Attribute objects, rendering jpegphoto

This commit is contained in:
Deon George 2021-12-08 23:26:12 +11:00
parent 2ccc1d3b83
commit a80a2725bc
10 changed files with 147 additions and 99 deletions

View File

@ -1,26 +1,23 @@
<?php <?php
/**
* Classes and functions for the template engine. namespace App\Classes\LDAP;
*
* @author The phpLDAPadmin development team
* @package phpLDAPadmin
*/
/** /**
* Represents an attribute of a template. * Represents an attribute of an LDAP Object
*
* @package phpLDAPadmin
* @subpackage Templates
*/ */
class Attribute { class Attribute
{
# Attribute Name # Attribute Name
public $name; public string $name;
/*
# Source of this attribute definition # Source of this attribute definition
protected $source; protected $source;
*/
# Current and Old Values # Current and Old Values
protected array $values;
/*
protected $oldvalues = array(); protected $oldvalues = array();
protected $values = array();
# MIN/MAX number of values # MIN/MAX number of values
protected $min_value_count = -1; protected $min_value_count = -1;
@ -77,37 +74,13 @@ class Attribute {
# Configuration for automatically generated values # Configuration for automatically generated values
protected $autovalue = array(); protected $autovalue = array();
protected $postvalue = array(); protected $postvalue = array();
*/
public function __construct($name,$values,$server_id,$source=null) { public function __construct(string $name,array $values) {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) $this->name = $name;
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); $this->values = $values;
$server = $_SESSION[APPCONFIG]->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']);
}
/*
# Should this attribute be hidden # Should this attribute be hidden
if ($server->isAttrHidden($this->name)) if ($server->isAttrHidden($this->name))
$this->forcehide = true; $this->forcehide = true;
@ -119,6 +92,12 @@ class Attribute {
# Should this attribute value be unique # Should this attribute value be unique
if ($server->isAttrUnique($this->name)) if ($server->isAttrUnique($this->name))
$this->unique = true; $this->unique = true;
*/
}
public function __toString(): string
{
return join('<br>',$this->values);
} }
/** /**
@ -127,7 +106,7 @@ class Attribute {
* @param boolean $lower - Return the attribute in normal or lower case (default lower) * @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) * @param boolean $real - Return the real attribute name (with ;binary, or just the name)
* @return string Attribute name * @return string Attribute name
*/ *
public function getName($lower=true,$real=false) { public function getName($lower=true,$real=false) {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs,$this->name); 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. * Autovalue is called after the attribute is initialised, and thus the values from the ldap server will be set.
*/ *
public function autoValue($new_val) { public function autoValue($new_val) {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);
@ -648,7 +627,7 @@ class Attribute {
* Return if this attribute is an RDN attribute * Return if this attribute is an RDN attribute
* *
* @return boolean * @return boolean
*/ *
public function isRDN() { public function isRDN() {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs,$this->rdn); 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 * Capture all the LDAP details we are interested in
* *
* @param sattr Schema Attribute * @param sattr Schema Attribute
*/ *
private function setLDAPdetails($sattr) { private function setLDAPdetails($sattr) {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); 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) * Return a list of aliases for this Attribute (as defined by the schema)
* This list will be lowercase. * This list will be lowercase.
*/ *
public function getAliases() { public function getAliases() {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->aliases); debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->aliases);
@ -846,7 +825,7 @@ class Attribute {
/** /**
* Display the values removed in an attribute. * Display the values removed in an attribute.
*/ *
public function getRemovedValues() { public function getRemovedValues() {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);
@ -856,7 +835,7 @@ class Attribute {
/** /**
* Display the values removed in an attribute. * Display the values removed in an attribute.
*/ *
public function getAddedValues() { public function getAddedValues() {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); 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. * @param string $attr_name The name of the attribute to examine.
* @return string * @return string
*/ *
private function real_attr_name() { private function real_attr_name() {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->name); debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->name);
@ -882,7 +861,7 @@ class Attribute {
/** /**
* Does this attribute need supporting JS * Does this attribute need supporting JS
*/ *
public function needJS($type=null) { public function needJS($type=null) {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS')) if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs); debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);
@ -913,5 +892,5 @@ class Attribute {
} else } else
debug_dump_backtrace(sprintf('Unknown JS request %s',$type),1); debug_dump_backtrace(sprintf('Unknown JS request %s',$type),1);
} }
} */
?> }

View File

@ -1,10 +1,8 @@
<?php <?php
/**
* Classes and functions for the template engine. namespace App\Classes\LDAP\Attribute;
*
* @author The phpLDAPadmin development team use App\Classes\LDAP\Attribute;
* @package phpLDAPadmin
*/
/** /**
* Represents an attribute whose values are binary * Represents an attribute whose values are binary
@ -12,7 +10,9 @@
* @package phpLDAPadmin * @package phpLDAPadmin
* @subpackage Templates * @subpackage Templates
*/ */
class BinaryAttribute extends Attribute { class Binary extends Attribute
{
/*
protected $filepaths; protected $filepaths;
protected $filenames; protected $filenames;
@ -56,5 +56,5 @@ class BinaryAttribute extends Attribute {
$this->filepaths[$i] = $path; $this->filepaths[$i] = $path;
} }
} }
} */
?> }

View File

@ -0,0 +1,27 @@
<?php
namespace App\Classes\LDAP\Attribute\Binary;
use App\Classes\LDAP\Attribute\Binary;
/**
* Represents an attribute whose values are jpeg pictures
*/
class JpegPhoto extends Binary
{
public function __toString(): string
{
$result = '';
$f = new \finfo;
foreach ($this->values as $value) {
switch ($x=$f->buffer($value,FILEINFO_MIME_TYPE)) {
case 'image/jpeg':
default:
$result .= sprintf("<img style='display:block; width:100px;height:100px;' src='data:%s;base64, %s' />",$x,base64_encode($value));
}
}
return $result;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Classes\LDAP\Attribute;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use App\Classes\LDAP\{Attribute};
class Factory
{
private const LOGKEY = 'LAf';
/**
* @var array event type to event class mapping
*/
public const map = [
'jpegphoto'=>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);
}
}

View File

@ -15,6 +15,9 @@ use LdapRecord\Models\ModelNotFoundException;
class HomeController extends Controller class HomeController extends Controller
{ {
/**
* Application home page
*/
public function home() public function home()
{ {
$base = (new Entry)->baseDN() ?: collect(); $base = (new Entry)->baseDN() ?: collect();
@ -27,18 +30,24 @@ class HomeController extends Controller
'item'=>Crypt::encryptString($item->getDn()), 'item'=>Crypt::encryptString($item->getDn()),
'lazy'=>TRUE, 'lazy'=>TRUE,
'icon'=>'fa-fw fas fa-sitemap', '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() public function info()
{ {
$root = (new Entry)->rootDSE(); $root = (new Entry)->rootDSE();
try { try {
$attrs = collect($root->getAttributes()) $attrs = collect($root->getAttributes())
->transform(function($item,$key) { ->transform(function($item) {
foreach ($item as $k=>$v) { foreach ($item as $k=>$v) {
if (preg_match('/[0-9]+\.[0-9]+\.[0-9]+/',$v)) { if (preg_match('/[0-9]+\.[0-9]+\.[0-9]+/',$v)) {
$format = sprintf( $format = sprintf(
@ -60,19 +69,23 @@ class HomeController extends Controller
} }
return view('frames.dn') return view('frames.dn')
->with('dn',__('Server Info')) ->with('o',$root)
->with('leaf',$root) ->with('dn',__('Server Info'));
->with('attributes',$this->sortAttrs($attrs));
} }
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')); $dn = Crypt::decryptString($request->post('key'));
return view('frames.dn') return view('frames.dn')
->with('dn',$dn) ->with('o',(new Server)->fetch($dn))
->with('leaf',$x=(new Server)->fetch($dn)) ->with('dn',$dn);
->with('attributes',$x ? $this->sortAttrs(collect($x->getAttributes())) : []);
} }
/** /**

View File

@ -8,6 +8,8 @@ use LdapRecord\Models\Model;
use LdapRecord\Models\ModelNotFoundException; use LdapRecord\Models\ModelNotFoundException;
use LdapRecord\Query\Model\Builder; use LdapRecord\Query\Model\Builder;
use App\Classes\LDAP\Attribute\Factory;
class Entry extends Model class Entry extends Model
{ {
/** /**
@ -43,6 +45,16 @@ class Entry extends Model
return $result; 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 * Return an icon for a DN based on objectClass
* *

View File

@ -1,17 +0,0 @@
<?php
/**
* Classes and functions for the template engine.
*
* @author The phpLDAPadmin development team
* @package phpLDAPadmin
*/
/**
* Represents an attribute whose values are jpeg pictures
*
* @package phpLDAPadmin
* @subpackage Templates
*/
class JpegAttribute extends BinaryAttribute {
}
?>

2
public/js/custom.js vendored
View File

@ -30,7 +30,7 @@ $(document).ready(function() {
click: function(event,data) { click: function(event,data) {
if (data.targetType == 'title') { if (data.targetType == 'title') {
$.ajax({ $.ajax({
url: 'render', url: 'dn',
method: 'POST', method: 'POST',
data: { key: data.node.data.item }, data: { key: data.node.data.item },
dataType: 'html', dataType: 'html',

View File

@ -1,22 +1,21 @@
@extends('layouts.dn') @extends('layouts.dn')
@section('page_title') @section('page_title')
{{ $dn }} <table class="table table-borderless">
@endsection <tr>
@section('page_subtitle') <td class="{{ ($x=Arr::get($o->getAttributes(),'jpegphoto')) ? 'border' : '' }}" style="border-radius: 5px;">{!! $x ?: sprintf('<div class="page-title-icon f32"><i class="%s"></i></div>',$o->icon() ?? "fas fa-info") !!}</td>
{{ $leaf->entryuuid[0] ?? '' }} <td class="top text-right align-text-top p-0 {{ $x ? 'pl-5' : 'pt-2' }}"><strong>{{ $dn }}</strong><br><small>{{ $o->entryuuid[0] ?? '' }}</small></td>
@endsection </tr>
@section('page_icon') </table>
{{ $leaf->icon() ?? 'fas fa-info' }}
@endsection @endsection
@section('main-content') @section('main-content')
<div class="bg-white p-3"> <div class="bg-white p-3">
<table class="table"> <table class="table">
@foreach ($attributes as $attribute => $value) @foreach ($o->getAttributes() as $attribute => $value)
<tr> <tr>
<th>{{ $attribute }}</th> <th>{{ $attribute }}</th>
<td>{!! is_array($value) ? join('<br>',$value) : $value !!}</td> <td>{!! $value !!}</td>
</tr> </tr>
@endforeach @endforeach
</table> </table>

View File

@ -28,7 +28,7 @@ Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
Route::get('home',[HomeController::class,'home']); Route::get('home',[HomeController::class,'home']);
Route::get('info',[HomeController::class,'info']); Route::get('info',[HomeController::class,'info']);
Route::post('render',[HomeController::class,'render']); Route::post('dn',[HomeController::class,'dn_frame']);
}); });
Route::redirect('/','home'); Route::redirect('/','home');