An error occurred while loading the file. Please try again.
-
dockx thibault authored
Fixing codestyle
Verifiedbb69cf71
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2011-2018 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \file class_templateHandling.inc
* Source code for the class templateHandling
*/
/*! \brief this class stores static methods used to parse templates LDAP data
*/
class templateHandling
{
/*! \brief Fetch a template from LDAP and returns its attributes and dependencies information */
public static function fetch($dn)
{
global $config;
$ldap = $config->get_ldap_link();
$ldap->cat($dn);
$attrs = $ldap->fetch();
$attrs = static::fieldsFromLDAP($attrs);
list($depends, $errors) = static::attributesDependencies($attrs);
msg_dialog::displayChecks($errors);
$attrs = static::sortAttributes($attrs, $depends);
return [$attrs, $depends];
}
/*! \brief Translate template attrs into $attrs as if taken from LDAP */
public static function fieldsFromLDAP (array $template_attrs)
{
unset($template_attrs['fdTemplateField']['count']);
sort($template_attrs['fdTemplateField']);
$attrs = [];
foreach ($template_attrs['fdTemplateField'] as $field) {
preg_match('/^([^:]+):(.*)$/s', $field, $m);
if (isset($attrs[$m[1]])) {
$attrs[$m[1]][] = $m[2];
$attrs[$m[1]]['count']++;
} else {
$attrs[$m[1]] = [$m[2]];
$attrs[$m[1]]['count'] = 1;
}
}
return $attrs;
}
/*! \brief Translate $attrs into template attrs */
public static function fieldsToLDAP (array $template_attrs, array $attrs)
{
/* First a bit of cleanup */
unset($template_attrs['dn']);
unset($template_attrs['fdTemplateField']['count']);
unset($template_attrs['objectClass']['count']);
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
unset($template_attrs['cn']['count']);
if (isset($template_attrs['count'])) {
for ($i = 0; $i < $template_attrs['count']; ++$i) {
/* Remove numeric keys */
unset($template_attrs[$i]);
}
}
unset($template_attrs['count']);
/* Remove all concerned values */
foreach ($template_attrs['fdTemplateField'] as $key => $value) {
preg_match('/^([^:]+):(.*)$/s', $value, $m);
if (isset($attrs[$m[1]])) {
unset($template_attrs['fdTemplateField'][$key]);
}
}
/* Then insert non-empty values */
foreach ($attrs as $key => $value) {
if (is_array($value)) {
foreach ($value as $v) {
if ($value == "") {
continue;
}
$template_attrs['fdTemplateField'][] = $key.':'.$v;
}
} else {
if ($value == "") {
continue;
}
$template_attrs['fdTemplateField'][] = $key.':'.$value;
}
}
sort($template_attrs['fdTemplateField']);
return $template_attrs;
}
/*! \brief Check template fields
*
* Returns errors if there are recursive dependencies.
* Might check more things later
*/
public static function checkFields ($attrs)
{
list(, $errors) = static::attributesDependencies($attrs);
return $errors;
}
/*! \brief Parse a mask (without surrounding %) using $attrs attributes, apply modifiers and returns an array containing possible results */
public static function parseMask($mask, array $attrs)
{
if ($mask == '|') {
return ['%'];
}
$modifiers = '';
if (preg_match('/^([^|]+)\|/', $mask, $m)) {
$modifiers = $m[1];
$mask = substr($mask, strlen($m[0]));
}
$result = [''];
if (isset($attrs[$mask])) {
$result = [$attrs[$mask]];
if (is_array($result[0])) {
unset($result[0]['count']);
}
} elseif (($mask != '') && !preg_match('/c/', $modifiers)) {
trigger_error("'$mask' was not found in attributes");
}
$len = strlen($modifiers);
for ($i = 0; $i < $len; ++$i) {
$args = [];
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
$modifier = $modifiers[$i];
if (preg_match('/^\[([^\]]+)\].*$/', substr($modifiers, $i + 1), $m)) {
/* get modifier args */
$args = explode(',', $m[1]);
$i += strlen($m[1]) + 2;
}
$result_tmp = [];
foreach ($result as $r) {
$result_tmp = array_merge($result_tmp, static::applyModifier($modifier, $args, $r));
}
$result = $result_tmp;
}
foreach ($result as &$r) {
/* Array that were not converted by a modifier into a string are now converted to strings */
if (is_array($r)) {
$r = reset($r);
}
}
unset($r);
return $result;
}
/*! \brief Return attrs needed before applying template
*
* \return array An array of attributes which are needed by the template
*/
public static function neededAttrs(array &$attrs, array $flatdepends)
{
$needed = [];
foreach ($flatdepends as $attr => $depends) {
if ((isset($depends[0])) && ($depends[0] == 'askme')) {
$needed[] = $attr;
unset($flatdepends[$attr]);
unset($attrs[$attr]);
}
}
$dependencies = array_unique(call_user_func_array('array_merge', $flatdepends));
foreach ($dependencies as $attr) {
if (empty($flatdepends[$attr])) {
$needed[] = $attr;
}
}
return array_unique($needed);
}
/*! \brief Parse template masks in an array
*
* \return array An array with the final values of attributes
*/
public static function parseArray(array $attrs, array $specialAttrs)
{
foreach ($attrs as &$attr) {
if (is_array($attr)) {
foreach ($attr as $key => &$string) {
if (!is_numeric($key)) {
continue;
}
$string = static::parseString($string, array_merge($attrs, $specialAttrs));
}
unset($string);
}
}
unset($attr);
return $attrs;
}
/*! \brief Parse template masks in a single string
*
* \return string the string with patterns replaced by their values
*/