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
/**
* Classes and functions for the template engine.
*
* @author The phpLDAPadmin development team
* @package phpLDAPadmin
*/
namespace App\Classes\LDAP;
/**
* Represents an attribute of a template.
*
* @package phpLDAPadmin
* @subpackage Templates
* Represents an attribute of an LDAP Object
*/
class Attribute {
class Attribute
{
# Attribute Name
public $name;
public string $name;
/*
# Source of this attribute definition
protected $source;
*/
# Current and Old Values
protected array $values;
/*
protected $oldvalues = array();
protected $values = array();
# MIN/MAX number of values
protected $min_value_count = -1;
@ -77,37 +74,13 @@ class Attribute {
# Configuration for automatically generated values
protected $autovalue = array();
protected $postvalue = array();
*/
public function __construct($name,$values,$server_id,$source=null) {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);
$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']);
}
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('<br>',$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);
}
*/
}
?>

View File

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

View File

@ -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
*

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) {
if (data.targetType == 'title') {
$.ajax({
url: 'render',
url: 'dn',
method: 'POST',
data: { key: data.node.data.item },
dataType: 'html',

View File

@ -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' }}
<table class="table table-borderless">
<tr>
<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>
<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>
</tr>
</table>
@endsection
@section('main-content')
<div class="bg-white p-3">
<table class="table">
@foreach ($attributes as $attribute => $value)
@foreach ($o->getAttributes() as $attribute => $value)
<tr>
<th>{{ $attribute }}</th>
<td>{!! is_array($value) ? join('<br>',$value) : $value !!}</td>
<td>{!! $value !!}</td>
</tr>
@endforeach
</table>

View File

@ -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');